From e54fe6cb4334fb0bf27cbf05c40bb2a899e1ebb9 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Sun, 17 Apr 2016 20:52:39 -0700 Subject: [PATCH] fixed close buttons --- demo/main.lua | 6 +++++- demo/pop/elements/window.lua | 14 ++++++-------- lib/pop/elements/window.lua | 14 ++++++-------- src/pop/elements/window.moon | 19 +++++++++++-------- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/demo/main.lua b/demo/main.lua index 85617c0..138e504 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -43,7 +43,6 @@ function love.load() w2:setClose(false) local t2 = pop.text("Click here to toggle close\nbutton on this window."):setMargin(10):setColor(0,0,0) t2.clicked = function() - print("CALLED") --NOTE not working! w2:setClose(not w2:hasClose()) return true end @@ -106,6 +105,11 @@ function love.keypressed(key) debugDraw = not debugDraw end + if (key == "w") and (not handled) then + local w = pop.window() + w.title:align("center") + end + if (key == "escape") and (not handled) then love.event.quit() end diff --git a/demo/pop/elements/window.lua b/demo/pop/elements/window.lua index 96af1d9..70a149d 100644 --- a/demo/pop/elements/window.lua +++ b/demo/pop/elements/window.lua @@ -17,12 +17,7 @@ local path = sub(..., 1, len(...) - len("/window")) local element = require(tostring(path) .. "/element") local box = require(tostring(path) .. "/box") local text = require(tostring(path) .. "/text") -local closeImage = { - 100, - 0, - 0, - 80 -} +local closeImage = graphics.newImage(tostring(path) .. "/img/close.png") local left = 1 local mousemoved_event = true do @@ -196,6 +191,10 @@ do setClose = function(self, enabled) if enabled then self.close = box(self, closeImage) + self.close.clicked = function() + self:delete() + return true + end local height = self.head:getHeight() self.close:align("right"):setSize(height, height) self.head:setWidth(self.w - height) @@ -250,7 +249,7 @@ do end _class_0.__parent.__init(self, parent) self.head = box(self, tBackground) - self.title = text(self, title, tColor) + self.title = text(self.head, title, tColor) self.window = box(self, wBackground) self.close = box(self, closeImage) local height = self.title:getHeight() @@ -266,7 +265,6 @@ do } self.titleOverflow = "trunicate" self.close.clicked = function() - print("CLOSE WAS CLICKED") self:delete() return true end diff --git a/lib/pop/elements/window.lua b/lib/pop/elements/window.lua index 96af1d9..70a149d 100644 --- a/lib/pop/elements/window.lua +++ b/lib/pop/elements/window.lua @@ -17,12 +17,7 @@ local path = sub(..., 1, len(...) - len("/window")) local element = require(tostring(path) .. "/element") local box = require(tostring(path) .. "/box") local text = require(tostring(path) .. "/text") -local closeImage = { - 100, - 0, - 0, - 80 -} +local closeImage = graphics.newImage(tostring(path) .. "/img/close.png") local left = 1 local mousemoved_event = true do @@ -196,6 +191,10 @@ do setClose = function(self, enabled) if enabled then self.close = box(self, closeImage) + self.close.clicked = function() + self:delete() + return true + end local height = self.head:getHeight() self.close:align("right"):setSize(height, height) self.head:setWidth(self.w - height) @@ -250,7 +249,7 @@ do end _class_0.__parent.__init(self, parent) self.head = box(self, tBackground) - self.title = text(self, title, tColor) + self.title = text(self.head, title, tColor) self.window = box(self, wBackground) self.close = box(self, closeImage) local height = self.title:getHeight() @@ -266,7 +265,6 @@ do } self.titleOverflow = "trunicate" self.close.clicked = function() - print("CLOSE WAS CLICKED") self:delete() return true end diff --git a/src/pop/elements/window.moon b/src/pop/elements/window.moon index c6708d6..65a1517 100644 --- a/src/pop/elements/window.moon +++ b/src/pop/elements/window.moon @@ -7,7 +7,7 @@ element = require "#{path}/element" box = require "#{path}/box" text = require "#{path}/text" -closeImage = {100, 0, 0, 80} --graphics.newImage "#{path}/img/close.png" +closeImage = graphics.newImage "#{path}/img/close.png" -- version compatibility left = 1 -- what is the left mouse button? @@ -34,10 +34,11 @@ class window extends element new: (parent, title="window", tBackground={25, 180, 230, 255}, tColor={255, 255, 255, 255}, wBackground={200, 200, 210, 255}) => super parent - @head = box @, tBackground -- title box at top - @title = text @, title, tColor -- text at top - @window = box @, wBackground -- main window area - @close = box @, closeImage -- close button + -- NOTE @title having @head as its parent might break things horribly + @head = box @, tBackground -- title box at top + @title = text @head, title, tColor -- text at top + @window = box @, wBackground -- main window area + @close = box @, closeImage -- close button -- correct placement / sizes of elements height = @title\getHeight! @@ -55,7 +56,6 @@ class window extends element @titleOverflow = "trunicate" -- defaults to trunicating title to fit in window @close.clicked = -> - print "CLOSE WAS CLICKED" @delete! return true @@ -261,15 +261,18 @@ class window extends element setClose: (enabled) => if enabled @close = box @, closeImage + @close.clicked = -> + @delete! + return true height = @head\getHeight! @close\align("right")\setSize height, height @head\setWidth @w - height - @title\align! -- new might not be working? + @title\align! insert @child, @close else @close\delete! @head\setWidth @w - @title\align! -- new might not be working? + @title\align! @close = false return @