1
Lua / Re: Numstrat - Stumbling into Lua
« on: September 23, 2012, 07:08:17 am »
A screen manager is something that allows you to devide your Lua applications in several pieces.
Basically, you will have a paint handler for every scene in your program, without needing to put all your stuff in on.paint.
Here is a simple screen manager, so you can understand how it works a bit:
Adriweb and Jim Bauwens created a more advanced screen manager together with a nice widget API, you can find it on the github page for EEPro I think.
Basically, you will have a paint handler for every scene in your program, without needing to put all your stuff in on.paint.
Here is a simple screen manager, so you can understand how it works a bit:
Code: [Select]
------ Screen Manager -------
-- In this table we will contain all 'screen' objects.
Screens = {}
-- This function is used to push a screen into the above table (Screens)
function pushScreen(scrn)
table.insert(Screens, scrn)
platform.window:invalidate()
end
-- This function is used to remove the last item in the 'Screens' table
function pullScreen()
table.remove(Screens)
platform.window:invalidate()
end
-- Return the last item of 'Screens', the current screen
function currentScreen()
return Screens[#Screens]
end
-- This function will loop through all screens in the 'Screens' table and draw them
function paintScreens(gc)
for _, screen in ipairs(Screens) do
screen:paint(gc)
end
end
-- Our screen object class
Screen = class()
function Screen:arrowKey() end
function Screen:paint() end
function Screen:charIn() end
function Screen:enterKey() end
-- Link the Screen manager to 'on'
function on.paint(gc) paintScreens(gc) end
function on.arrowKey(arrow) currentScreen():arrowKey(arrow) end
function on.charIn(ch) currentScreen():charIn(ch) end
function on.enterKey() currentScreen():enterKey() end
--- User code ----
popup = Screen()
function popup:paint(gc)
gc:setColorRGB(255,255,255)
gc:fillRect(50,50,200,100)
gc:setColorRGB(0,0,0)
gc:drawRect(50,50,200,100)
gc:drawString("Hey!", 52, 50, "top")
gc:drawString("Press enter to hide this popup", 52, 80, "top")
end
function popup:enterKey()
pullScreen()
end
main = Screen()
function main:paint(gc)
gc:drawString("Hello World! Press enter :D", 10, 10, "top")
end
function main:enterKey()
pushScreen(popup)
end
pushScreen(main)
Adriweb and Jim Bauwens created a more advanced screen manager together with a nice widget API, you can find it on the github page for EEPro I think.