From cfa7c83b04b118252e7b1dde1aabdb73b97bcd9d Mon Sep 17 00:00:00 2001 From: David McMackins II Date: Mon, 1 Jan 2018 11:45:48 -0600 Subject: Change to new GUI model --- gamestate.lua | 21 ++++++++++++++++----- gui.lua | 38 +++++++++++++++++++++++++++----------- mainmenustate.lua | 12 ++++-------- state.lua | 41 +++++++++++++++-------------------------- 4 files changed, 62 insertions(+), 50 deletions(-) diff --git a/gamestate.lua b/gamestate.lua index 91b6eee..4047a40 100644 --- a/gamestate.lua +++ b/gamestate.lua @@ -1,6 +1,6 @@ -- -- Agario Checkers - Checkers-like game with inspiration from agar.io --- Copyright (C) 2015-2017 Delwink, LLC +-- Copyright (C) 2015-2018 Delwink, LLC -- -- Redistributions, modified or unmodified, in whole or in part, must retain -- applicable copyright or other legal privilege notices, these conditions, and @@ -70,7 +70,7 @@ function GameState:__init() resetbutton:setvisible(true) resetbutton:addclicklistener(resetgamestate) - self._gui = { resetbutton } + self._gui:addcomponent(resetbutton) end function GameState:_initrow(start, y) @@ -353,11 +353,13 @@ function GameState:_makemove() end function GameState:mousepressed(x, y, button) - self._base.mousepressed(self, x, y, button) + if self._base.mousepressed(self, x, y, button) then + return true + end if button == 1 then if self._winner ~= 0 then - return + return true end if not self._selected then @@ -365,12 +367,21 @@ function GameState:mousepressed(x, y, button) elseif self._targetspace then self:_makemove() end + + return true elseif button == 2 then self._selected = nil self._targetspace = nil + return true end + + return false end function GameState:mousereleased(x, y, button) - self._base.mousereleased(self, x, y, button) + if self._base.mousereleased(self, x, y, button) then + return true + end + + return false end diff --git a/gui.lua b/gui.lua index ebd5c25..7ad7097 100644 --- a/gui.lua +++ b/gui.lua @@ -1,6 +1,6 @@ -- -- Agario Checkers - Checkers-like game with inspiration from agar.io --- Copyright (C) 2015-2017 Delwink, LLC +-- Copyright (C) 2015-2018 Delwink, LLC -- -- Redistributions, modified or unmodified, in whole or in part, must retain -- applicable copyright or other legal privilege notices, these conditions, and @@ -49,6 +49,7 @@ end function Gui:addcomponent(component) table.insert(self._components, component) + component.gui = self end function Gui:mousepressed(x, y, button) @@ -72,10 +73,9 @@ function Gui:mousereleased(x, y, button) end function Gui:textinput(c) - for _,component in ipairs(self._components) do - if component:textinput(c) then - return true - end + if self._active and self._active.usereditable then + self._active:textinput(c) + return true end return false @@ -91,6 +91,12 @@ function Gui:keypressed(key, isrepeat) return false end +function Gui:draw() + for _,component in ipairs(self._components) do + component:draw() + end +end + local function normalizedim(d) if type(d) ~= 'function' then return function(self) return d end @@ -118,6 +124,14 @@ function GuiComponent:__init(x, y, w, h, bg, fg, state) self.visible = false end +function GuiComponent:draw() + if not self.visible then + return false + end + + return true +end + function GuiComponent:addclicklistener(listener) table.insert(self.clicklisteners, listener) end @@ -128,11 +142,11 @@ function GuiComponent:_triggerclicklisteners() end end -function GuiComponent:onpress() +function GuiComponent:mousepressed() self.clicked = true end -function GuiComponent:onrelease(x, y) +function GuiComponent:mousereleased(x, y) if self.clicked then self.clicked = false @@ -142,7 +156,7 @@ function GuiComponent:onrelease(x, y) end end -function GuiComponent:onkey(key) +function GuiComponent:keypressed(key, isrepeat) end @@ -163,7 +177,7 @@ function GuiComponent:settext(text) self.text = text end -function GuiComponent:type(c) +function GuiComponent:textinput(c) end @@ -174,8 +188,8 @@ end Button = class(GuiComponent) function Button:draw() - if not self.visible then - return + if not self._base.draw(self) then + return false end local bg = {self.bg[1], self.bg[2], self.bg[3]} @@ -202,6 +216,8 @@ function Button:draw() (self:y() + (self:h() / 2)) - self:_halfheight(), self:w(), 'center') love.graphics.setFont(defaultfont) + + return true end TextField = class(GuiComponent) diff --git a/mainmenustate.lua b/mainmenustate.lua index a8a661f..d927159 100644 --- a/mainmenustate.lua +++ b/mainmenustate.lua @@ -1,6 +1,6 @@ -- -- Agario Checkers - Checkers-like game with inspiration from agar.io --- Copyright (C) 2016-2017 Delwink, LLC +-- Copyright (C) 2016-2018 Delwink, LLC -- -- Redistributions, modified or unmodified, in whole or in part, must retain -- applicable copyright or other legal privilege notices, these conditions, and @@ -38,10 +38,6 @@ require 'gui' -local function entergamestate() - setstate(GameState()) -end - MainMenuState = class(State) local function dynbuttonx(self) @@ -55,7 +51,7 @@ end local bheight = 50 function MainMenuState:_nexty() - return 180 + (#self._gui * (bheight + 10)) + return 180 + (#self._gui._components * (bheight + 10)) end function MainMenuState:_addbutton(text, listeners) @@ -70,14 +66,14 @@ function MainMenuState:_addbutton(text, listeners) end end - table.insert(self._gui, btn) + self._gui:addcomponent(btn) return btn end function MainMenuState:__init() self._base.__init(self) - self:_addbutton('Local Game', {entergamestate}) + self:_addbutton('Local Game', {function() setstate(GameState()) end}) self:_addbutton('Host TCP/IP Game') self:_addbutton('Join TCP/IP Game') self:_addbutton('Quit', {love.event.quit}) diff --git a/state.lua b/state.lua index 0700f8c..4207793 100644 --- a/state.lua +++ b/state.lua @@ -1,6 +1,6 @@ -- -- Agario Checkers - Checkers-like game with inspiration from agar.io --- Copyright (C) 2016-2017 Delwink, LLC +-- Copyright (C) 2016-2018 Delwink, LLC -- -- Redistributions, modified or unmodified, in whole or in part, must retain -- applicable copyright or other legal privilege notices, these conditions, and @@ -54,7 +54,7 @@ local authorlogo = love.graphics.newImage('res/author.png') State = class() function State:__init() - self._gui = {} + self._gui = Gui() end function State:load() @@ -93,10 +93,8 @@ function State:draw() diff = diff + gridsize end - -- draw all buttons - for _,button in ipairs(self._gui) do - button:draw() - end + -- draw gui + self._gui:draw() -- draw author logo love.graphics.setColor(0, 0, 0) @@ -105,38 +103,29 @@ function State:draw() end function State:textinput(c) - if self._activecomponent and self._activecomponent.usereditable then - self._activecomponent:type(c) - end + self._gui:textinput(c) end function State:keypressed(key, isrepeat) - + self._gui:keypressed(key, isrepeat) end function State:mousepressed(x, y, button) - if button == 1 then - for _,component in ipairs(self._gui) do - if component:isvisible() and component:contains(x, y) then - component:onpress() - return - end - end - - self._activecomponent = nil -- user clicked away from text field - love.keyboard.setTextInput(false) + if self._gui:mousepressed(x, y, button) then + return true end + + return false end function State:mousereleased(x, y, button) - if button == 1 then - for _,component in ipairs(self._gui) do - if component:isvisible() then - component:onrelease(x, y) - end - end + if self._gui:mousereleased(x, y, button) then + return true end + + return false end require 'gamestate' require 'mainmenustate' +--require 'hostsetupstate' -- cgit v1.2.3