Class Component
Superclass for all GUI components.
Usage:
-- Creating a custom component:
CustomComponent = class(Component) -- Your new component needs to extend Component
-- Set any variables that have a default value
CustomComponent.somefield = "somedefault"
-- Constructor for your component
function CustomComponent:_init(args)
Component._init(self) -- This line must be the first line of your constructor
-- Do other initialization stuff
self.someotherfield = "somevalue"
-- If you want your component to block the mouse (i.e. know when the mouse
-- is over it, you must set mouseOver to not nil
self.mouseOver = false
end
-- Put all your component logic in update
function CustomComponent:update(dt)
-- Do some logic
end
-- Put all your component rendering in draw
function CustomComponent:draw(dt)
-- Do some drawing
end
-- If you want your component to be notified of click events, create a
-- clickEvent method.
function CustomComponent:clickEvent(position, button, pressed)
-- Process click
end
-- If you want your component to be notified of key events, create a keyEvent
-- method.
function CustomComponent:keyEvent(keyCode, pressed)
-- Process key
end
Fields
-
Component.hasFocus
-
Whether this component has keyboard focus.
mouseOver needs to be not nil for this to be set.
-
Component.height
-
The height of this component.
-
Component.mouseOver
-
Whether the mouse is hovering over this component.
Set this to not nil to allow this to be set.
-
Component.width
-
The width of this component.
-
Component.x
-
The x location of this component, relative to its parent.
-
Component.y
-
The y location of this component, relative to its parent.
Constructor
-
Component:_init ()
-
Constructs a component.
Methods
-
Component:add (child)
-
Adds a child component to this component.
Parameters:
- child
The component to add.
-
Component:calculateOffset ()
-
Calculates the offset from the origin that this component should use, based
on its parents.
Returns:
The calculated offset.
-
Component:clickEvent (position, button, pressed)
-
Called when the mouse is pressed or released over this component.
Parameters:
- position
The position of the mouse where the click happened.
- button
The mouse button used.
- pressed
Whether the mouse was pressed or released.
Returns:
If true, consumes the mouse event, blocking it from any underlying
components.
-
Component:contains (position)
-
Checks if the given position is within this component.
Parameters:
- position
The position to check.
Returns:
Whether or not the position is within the bounds of this component.
-
Component:draw (dt)
-
Draws this component Components should override this to implement their own draw functions.
Parameters:
- dt
The time elapsed since the last update, in seconds.
-
Component:keyEvent (keyCode, pressed)
-
Called when the user presses or releases a key when this component has focus.
Parameters:
- keyCode
The key code that was pressed or released.
- pressed
Whether the key was pressed or released.
Returns:
If true, consumes the key event, blocking it from any parents.
-
Component:pack ([padding])
-
Resizes this component around its children.
Parameters:
- padding
Amount of padding to put between the component's
children and this component's borders. If nil, this will
not shrink the component.
(optional)
-
Component:remove (child)
-
Removes a child component.
Parameters:
- child
The component to remove
Returns:
Whether or not the child was removed
-
Component:removeSelf ()
-
Remove self from parent.
-
Component:setParent (parent)
-
Sets the parent of this component, and updates the offset of this component.
Parameters:
- parent
The new parent of this component, or nil if this is to be a
top level component.
-
Component:update (dt)
-
Updates this component.
Components should override this to implement their own update functions.
Parameters:
- dt
The time elapsed since the last update, in seconds.