added state machine (for #57)

This commit is contained in:
Paul Liverman III 2017-08-13 19:28:32 -07:00
parent 5a6891ea7d
commit 619e5fc8ce
2 changed files with 73 additions and 6 deletions

View File

@ -59,6 +59,8 @@ end
pop.elements = { } pop.elements = { }
pop.skins = { } pop.skins = { }
pop.extensions = { } pop.extensions = { }
pop.states = { }
pop.state = false
pop.screen = false pop.screen = false
pop.focused = false pop.focused = false
pop.hovered = false pop.hovered = false
@ -145,9 +147,42 @@ pop.load = function(load_path)
end end
end end
if not (pop.screen) then if not (pop.screen) then
pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight()) return pop.setState(pop.newState())
pop.screen.data.update = true end
return log("Created \"pop.screen\"") end
pop.newState = function(name)
local screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight())
screen.data.update = true
if not (name) then
if pop.states.default then
name = #pop.states + 1
else
name = "default"
end
end
pop.states[name] = {
screen = screen,
focused = false,
hovered = false
}
log("Created state: \"" .. tostring(name) .. "\"")
return name
end
pop.setState = function(name)
if name == nil then
name = default
end
do
local state = pop.states[name]
if state then
pop.state = name
pop.screen = state.screen
pop.focused = state.focused
pop.hovered = state.hovered
return true
else
return error("Invalid state: \"" .. tostring(name) .. "\"")
end
end end
end end
pop.create = function(element, parent, data, ...) pop.create = function(element, parent, data, ...)

View File

@ -76,6 +76,8 @@ else
pop.elements = {} pop.elements = {}
pop.skins = {} pop.skins = {}
pop.extensions = {} pop.extensions = {}
pop.states = {}
pop.state = false
pop.screen = false pop.screen = false
pop.focused = false pop.focused = false
pop.hovered = false pop.hovered = false
@ -166,9 +168,39 @@ pop.load = (load_path=path) ->
-- Initialize pop.screen (top element, GUI area) -- Initialize pop.screen (top element, GUI area)
unless pop.screen unless pop.screen
pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!) pop.setState(pop.newState())
pop.screen.data.update = true
log "Created \"pop.screen\""
pop.newState = (name) ->
screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!)
screen.data.update = true
unless name
if pop.states.default
name = #pop.states + 1
else
name = "default"
pop.states[name] = {
screen: screen
focused: false
hovered: false
}
log "Created state: \"#{name}\""
return name
pop.setState = (name=default) ->
if state = pop.states[name]
pop.state = name
pop.screen = state.screen
pop.focused = state.focused
pop.hovered = state.hovered
return true
else
error "Invalid state: \"#{name}\""