summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid McMackins II <contact@mcmackins.org>2017-08-28 06:32:49 -0500
committerDavid McMackins II <contact@mcmackins.org>2017-08-28 06:32:49 -0500
commit03d8c2dde37201590bc67e00488cc637117a1c82 (patch)
treed933e97ba09caf05b6852ce737645e45ff771b37
parent9faedad2ea3507e16b453f9a6093a96e94e0fe56 (diff)
Move GUI to generic file to make adding new component types simpler
-rw-r--r--gamestate.lua7
-rw-r--r--gui.lua (renamed from button.lua)11
-rw-r--r--main.lua4
-rw-r--r--mainmenustate.lua6
-rw-r--r--state.lua21
5 files changed, 26 insertions, 23 deletions
diff --git a/gamestate.lua b/gamestate.lua
index 3448c51..bcbf70a 100644
--- a/gamestate.lua
+++ b/gamestate.lua
@@ -1,6 +1,6 @@
--
-- Agario Checkers - Checkers-like game with inspiration from agar.io
--- Copyright (C) 2015-2016 Delwink, LLC
+-- Copyright (C) 2015-2017 Delwink, LLC
--
-- Redistributions, modified or unmodified, in whole or in part, must retain
-- applicable copyright or other legal privilege notices, these conditions, and
@@ -37,10 +37,9 @@
--
require 'board'
-require 'button'
+require 'gui'
require 'geom'
require 'piece'
-require 'state'
local mainfont = love.graphics.getFont()
local winfont = love.graphics.newFont(25)
@@ -71,7 +70,7 @@ function GameState:__init()
resetbutton:setvisible(true)
resetbutton:addlistener(resetgamestate)
- self._buttons = { resetbutton }
+ self._gui = { resetbutton }
end
function GameState:_initrow(start, y)
diff --git a/button.lua b/gui.lua
index 7f6fb49..c7b17ea 100644
--- a/button.lua
+++ b/gui.lua
@@ -1,6 +1,6 @@
--
-- Agario Checkers - Checkers-like game with inspiration from agar.io
--- Copyright (C) 2015-2016 Delwink, LLC
+-- Copyright (C) 2015-2017 Delwink, LLC
--
-- Redistributions, modified or unmodified, in whole or in part, must retain
-- applicable copyright or other legal privilege notices, these conditions, and
@@ -63,8 +63,6 @@ function Button:__init(x, y, w, h, bg, fg)
self.listeners = {}
self.text = ''
self.visible = false
-
- self.font = love.graphics.newFont(self:h() * 0.8)
end
function Button:addlistener(listener)
@@ -127,7 +125,12 @@ function Button:draw()
love.graphics.setColor(self.fg)
love.graphics.rectangle('line', self:x(), self:y(), self:w(), self:h())
- love.graphics.setFont(self.font)
+ if not self._lasth or self:h() ~= self._lasth then
+ self._font = love.graphics.newFont(self:h() * 0.8)
+ self._lasth = self:h()
+ end
+
+ love.graphics.setFont(self._font)
love.graphics.printf(self.text, self:x(),
(self:y() + (self:h() / 2)) - self:_halfheight(),
self:w(), 'center')
diff --git a/main.lua b/main.lua
index a275d50..5d351ed 100644
--- a/main.lua
+++ b/main.lua
@@ -1,6 +1,6 @@
--
-- Agario Checkers - Checkers-like game with inspiration from agar.io
--- Copyright (C) 2016 Delwink, LLC
+-- Copyright (C) 2016-2017 Delwink, LLC
--
-- Redistributions, modified or unmodified, in whole or in part, must retain
-- applicable copyright or other legal privilege notices, these conditions, and
@@ -37,8 +37,6 @@
--
require 'state'
-require 'gamestate'
-require 'mainmenustate'
function love.load()
love.graphics.setBackgroundColor(255, 255, 255)
diff --git a/mainmenustate.lua b/mainmenustate.lua
index 8365d13..a50befa 100644
--- a/mainmenustate.lua
+++ b/mainmenustate.lua
@@ -36,7 +36,7 @@
-- OF OR OTHER DEALINGS IN THE WORK.
--
-require 'button'
+require 'gui'
local function entergamestate()
setstate(GameState())
@@ -55,7 +55,7 @@ end
local bheight = 50
function MainMenuState:_nexty()
- return 180 + (#self._buttons * (bheight + 10))
+ return 180 + (#self._gui * (bheight + 10))
end
function MainMenuState:_addbutton(text, listeners)
@@ -70,7 +70,7 @@ function MainMenuState:_addbutton(text, listeners)
end
end
- table.insert(self._buttons, btn)
+ table.insert(self._gui, btn)
return btn
end
diff --git a/state.lua b/state.lua
index f700020..3de1f33 100644
--- a/state.lua
+++ b/state.lua
@@ -36,7 +36,7 @@
-- OF OR OTHER DEALINGS IN THE WORK.
--
-require 'class'
+require 'gui'
activestate = nil
@@ -54,7 +54,7 @@ local authorlogo = love.graphics.newImage('res/author.png')
State = class()
function State:__init()
- self._buttons = {}
+ self._gui = {}
end
function State:load()
@@ -94,7 +94,7 @@ function State:draw()
end
-- draw all buttons
- for _,button in ipairs(self._buttons) do
+ for _,button in ipairs(self._gui) do
button:draw()
end
@@ -110,9 +110,9 @@ end
function State:mousepressed(x, y, button)
if button == 1 then
- for _,button in ipairs(self._buttons) do
- if button:isvisible() and button:contains(x, y) then
- button:onpress()
+ for _,component in ipairs(self._gui) do
+ if component:isvisible() and component:contains(x, y) then
+ component:onpress()
return
end
end
@@ -121,10 +121,13 @@ end
function State:mousereleased(x, y, button)
if button == 1 then
- for _,button in ipairs(self._buttons) do
- if button:isvisible() then
- button:onrelease(x, y)
+ for _,component in ipairs(self._gui) do
+ if component:isvisible() then
+ component:onrelease(x, y)
end
end
end
end
+
+require 'gamestate'
+require 'mainmenustate'