2.3 KiB
Element
The element is helium's basic building block, in practice it consists of 2 nested functions, the basic pattern looks like this
element = function()
return function()
end
end
So, what's what?
Well this outer part is what i call the loader function, it runs once per created element
element = function()
end
It's where you'll set up variables for later, callbacks and state
element = function()
local x = 'blah'
end
Of course, the variables in this first function become attached to the 'closure'
element = function()
local x = 'blah'
return function()
--so you can use X in here
print(x)
end
end
So now we have a function, but this isn't an element yet, to create an element you need to import helium and call the function with helium
local helium = require('helium')
local elementFactory = helium(function()
return function()
end
end)
This returns a new 'factory', that is a function that will create a new element when called:
local element = elementFactory(params, width, height, flags)
Param is an arbitrary table that you can pass in for the element to use, width and height are the 'minimum' dimensions of the new element and flags is an arbitrary table of flags, currently only used in layout.
Now that we've learned about params, the first function gets passed them and another table:
element = function(param, view)
return function()
end
end
The first parameter is what got passed to the factory, view table looks like : {x, y, w, h} and it contains the size and coordinates of the element.
so:
local elementFactory = helium(function(param, view)
print(param.x)
return function()
end
end)
elementFactory({x = 10}, 10, 10)
Methods
The element has a few methods meant for user interaction, these are:
Element:draw(x, y, w, h)
Use this to draw something onscreen, you can also call this every frame if it's something that moves with your game objects This checks equality to the current viewport so it'll only get re-rendered if something actually changes, furthermore translations don't cause re-renders.
Element:destroy()
Use destroy to remove this element from the scene
Element:setParam(newParam)
Use setParam to pass new parameters to this element