From 619e5fc8ceeb565c3916d109b26023c61dad8f25 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Sun, 13 Aug 2017 19:28:32 -0700 Subject: [PATCH] added state machine (for #57) --- init.lua | 41 ++++++++++++++++++++++++++++++++++++++--- init.moon | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/init.lua b/init.lua index c0d7dd2..83b3450 100644 --- a/init.lua +++ b/init.lua @@ -59,6 +59,8 @@ end pop.elements = { } pop.skins = { } pop.extensions = { } +pop.states = { } +pop.state = false pop.screen = false pop.focused = false pop.hovered = false @@ -145,9 +147,42 @@ pop.load = function(load_path) end end if not (pop.screen) then - pop.screen = pop.create("element", false):setSize(graphics.getWidth(), graphics.getHeight()) - pop.screen.data.update = true - return log("Created \"pop.screen\"") + return pop.setState(pop.newState()) + end +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 pop.create = function(element, parent, data, ...) diff --git a/init.moon b/init.moon index a6eebbf..b1e59a7 100644 --- a/init.moon +++ b/init.moon @@ -76,6 +76,8 @@ else pop.elements = {} pop.skins = {} pop.extensions = {} +pop.states = {} +pop.state = false pop.screen = false pop.focused = false pop.hovered = false @@ -166,9 +168,39 @@ pop.load = (load_path=path) -> -- Initialize pop.screen (top element, GUI area) unless pop.screen - pop.screen = pop.create("element", false)\setSize(graphics.getWidth!, graphics.getHeight!) - pop.screen.data.update = true - log "Created \"pop.screen\"" + pop.setState(pop.newState()) + + + +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}\""