summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid McMackins II <contact@mcmackins.org>2017-08-28 06:43:24 -0500
committerDavid McMackins II <contact@mcmackins.org>2017-08-28 06:43:24 -0500
commitba972843876ed5fea79036f3a20afc2fc65e54f2 (patch)
treeb8ab48ca33b570a36f33a6729ef23f5dac697416
parent03d8c2dde37201590bc67e00488cc637117a1c82 (diff)
Add generic GUI component class
-rw-r--r--gamestate.lua2
-rw-r--r--gui.lua30
-rw-r--r--mainmenustate.lua2
3 files changed, 18 insertions, 16 deletions
diff --git a/gamestate.lua b/gamestate.lua
index bcbf70a..91b6eee 100644
--- a/gamestate.lua
+++ b/gamestate.lua
@@ -68,7 +68,7 @@ function GameState:__init()
local resetbutton = Button(5, 5, 50, 18, {245, 245, 245}, {0, 0, 0})
resetbutton:settext('Reset')
resetbutton:setvisible(true)
- resetbutton:addlistener(resetgamestate)
+ resetbutton:addclicklistener(resetgamestate)
self._gui = { resetbutton }
end
diff --git a/gui.lua b/gui.lua
index c7b17ea..b167130 100644
--- a/gui.lua
+++ b/gui.lua
@@ -38,8 +38,6 @@
require 'class'
-Button = class()
-
local defaultfont = love.graphics.getFont()
local function normalizedim(d)
@@ -50,7 +48,9 @@ local function normalizedim(d)
return d
end
-function Button:__init(x, y, w, h, bg, fg)
+GuiComponent = class()
+
+function GuiComponent:__init(x, y, w, h, bg, fg)
self.x = normalizedim(x)
self.y = normalizedim(y)
self.w = normalizedim(w)
@@ -60,52 +60,54 @@ function Button:__init(x, y, w, h, bg, fg)
self.fg = fg
self.clicked = false
- self.listeners = {}
+ self.clicklisteners = {}
self.text = ''
self.visible = false
end
-function Button:addlistener(listener)
- table.insert(self.listeners, listener)
+function GuiComponent:addclicklistener(listener)
+ table.insert(self.clicklisteners, listener)
end
-function Button:onpress()
+function GuiComponent:onpress()
self.clicked = true
end
-function Button:onrelease(x, y)
+function GuiComponent:onrelease(x, y)
if self.clicked then
self.clicked = false
if self:contains(x, y) then
- for _,listener in ipairs(self.listeners) do
+ for _,listener in ipairs(self.clicklisteners) do
listener()
end
end
end
end
-function Button:contains(x, y)
+function GuiComponent:contains(x, y)
return (x >= self:x() and x <= (self:x() + self:w())
and y >= self:y() and y <= (self:y() + self:h()))
end
-function Button:setvisible(b)
+function GuiComponent:setvisible(b)
self.visible = b
end
-function Button:isvisible()
+function GuiComponent:isvisible()
return self.visible
end
-function Button:settext(text)
+function GuiComponent:settext(text)
self.text = text
end
-function Button:_halfheight()
+function GuiComponent:_halfheight()
return self:h() / 2
end
+Button = class(GuiComponent)
+
function Button:draw()
if not self.visible then
return
diff --git a/mainmenustate.lua b/mainmenustate.lua
index a50befa..321fc58 100644
--- a/mainmenustate.lua
+++ b/mainmenustate.lua
@@ -66,7 +66,7 @@ function MainMenuState:_addbutton(text, listeners)
if listeners then
for _,listener in ipairs(listeners) do
- btn:addlistener(listener)
+ btn:addclicklistener(listener)
end
end