From 978562774abe1804933baad7b20480703d1ca2f8 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Sat, 8 Apr 2017 17:10:44 -0700 Subject: [PATCH] improvements/new features --- docs/classes/element.html | 2 +- docs/classes/window.html | 2 +- docs/index.html | 2 +- docs/modules/Element.html | 2 +- docs/modules/main.html | 2 +- docs/modules/pop.html | 11 +++++++++-- docs/modules/util.html | 2 +- elements/element.lua | 2 +- elements/element.moon | 2 +- elements/text.lua | 26 ++++++++++++++++++++++++-- elements/text.moon | 34 ++++++++++++++++++++++++++++++++-- init.lua | 21 ++++++++++++++++++--- init.moon | 22 +++++++++++++++++++--- 13 files changed, 110 insertions(+), 20 deletions(-) diff --git a/docs/classes/element.html b/docs/classes/element.html index d783c9b..04015b0 100644 --- a/docs/classes/element.html +++ b/docs/classes/element.html @@ -412,7 +412,7 @@
generated by LDoc 1.4.3 -Last updated 2017-04-08 13:43:44 +Last updated 2017-04-08 17:10:29
diff --git a/docs/classes/window.html b/docs/classes/window.html index 4b20f86..a398c84 100644 --- a/docs/classes/window.html +++ b/docs/classes/window.html @@ -144,7 +144,7 @@
generated by LDoc 1.4.3 -Last updated 2017-04-08 13:43:44 +Last updated 2017-04-08 17:10:29
diff --git a/docs/index.html b/docs/index.html index 7085441..236677d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -84,7 +84,7 @@
generated by LDoc 1.4.3 -Last updated 2017-04-08 13:43:44 +Last updated 2017-04-08 17:10:29
diff --git a/docs/modules/Element.html b/docs/modules/Element.html index bbb56ed..e2afd70 100644 --- a/docs/modules/Element.html +++ b/docs/modules/Element.html @@ -491,7 +491,7 @@
generated by LDoc 1.4.3 -Last updated 2017-04-08 13:43:44 +Last updated 2017-04-08 17:10:29
diff --git a/docs/modules/main.html b/docs/modules/main.html index f2de0a9..715d204 100644 --- a/docs/modules/main.html +++ b/docs/modules/main.html @@ -105,7 +105,7 @@
generated by LDoc 1.4.3 -Last updated 2017-04-08 13:43:44 +Last updated 2017-04-08 17:10:29
diff --git a/docs/modules/pop.html b/docs/modules/pop.html index 1ca6652..5ac5fa6 100644 --- a/docs/modules/pop.html +++ b/docs/modules/pop.html @@ -137,7 +137,8 @@ mousemoved-todo2 - Implement a way for an element to attach itself to love.mousemoved() events? + Implement a way for an element to attach itself to love.mousemoved() events? + checking element against pop.screen so that this only gets called once mousereleased-todo3 @@ -603,6 +604,11 @@ table.insert element.parent, element.parent\removeChild(element), The currently focused GUI element (or false if none is focused). +
  • hovered + Element or false + The GUI element the mouse is hovering over + (or false if none is hovered over). +
  • @@ -640,6 +646,7 @@ table.insert element.parent, element.parent\removeChild(element),
    Implement a way for an element to attach itself to love.mousemoved() events? + checking element against pop.screen so that this only gets called once @@ -716,7 +723,7 @@ table.insert element.parent, element.parent\removeChild(element),
    generated by LDoc 1.4.3 -Last updated 2017-04-08 13:43:44 +Last updated 2017-04-08 17:10:29
    diff --git a/docs/modules/util.html b/docs/modules/util.html index bf4c965..492101e 100644 --- a/docs/modules/util.html +++ b/docs/modules/util.html @@ -126,7 +126,7 @@
    generated by LDoc 1.4.3 -Last updated 2017-04-08 13:43:44 +Last updated 2017-04-08 17:10:29
    diff --git a/elements/element.lua b/elements/element.lua index f11194e..b2430d7 100644 --- a/elements/element.lua +++ b/elements/element.lua @@ -21,7 +21,7 @@ do toPixel = true end if not (self.data.align) then - return false + return self end if horizontal then self.data.horizontal = horizontal diff --git a/elements/element.moon b/elements/element.moon index 76d156b..0d6a206 100644 --- a/elements/element.moon +++ b/elements/element.moon @@ -47,7 +47,7 @@ class element --- @todo doc me align: (horizontal, vertical, toPixel=true) => - unless @data.align return false + unless @data.align return @ @data.horizontal = horizontal if horizontal @data.vertical = vertical if vertical diff --git a/elements/text.lua b/elements/text.lua index e182bfe..2ef1ec5 100644 --- a/elements/text.lua +++ b/elements/text.lua @@ -17,9 +17,26 @@ do self.data.h = self.font:getHeight() * (select(2, self.data.text:gsub("\n", "\n")) + 1) return self end, + getText = function(self) + return self.data.text + end, setText = function(self, text) - self.data.text = text - return self:setSize() + self.data.text = tostring(text) + self:setSize() + return self:align() + end, + setColor = function(self, r, g, b, a) + if "table" == type(r) then + self.data.color = r + else + self.data.color = { + r, + g, + b, + a + } + end + return self end } _base_0.__index = _base_0 @@ -37,6 +54,11 @@ do end self.parent, self.data = parent, data _class_0.__parent.__init(self, self.parent, self.data) + if "number" == type(text) then + fontSize = fontFile + fontFile = text + text = "" + end self.data.type = "text" if not (self.data.text) then self.data.text = text diff --git a/elements/text.moon b/elements/text.moon index 3d0757f..86ad45e 100644 --- a/elements/text.moon +++ b/elements/text.moon @@ -12,6 +12,12 @@ class text extends element new: (@parent, @data={}, text="", fontFile, fontSize=14) => super @parent, @data + -- this makes text optional, a number can be passed to set font size immediately + if "number" == type text + fontSize = fontFile + fontFile = text + text = "" + @data.type = "text" @data.text = text unless @data.text @data.fontFile = fontFile unless @data.fontFile @@ -35,12 +41,36 @@ class text extends element return @ --- Size is dependant on the text and font, so you cannot specify a size. + --- @treturn element self setSize: => @data.w = @font\getWidth @data.text @data.h = @font\getHeight! * (select(2, @data.text\gsub("\n", "\n")) + 1) --hack to get height of multiple lines return @ + --- Returns text. + --- @treturn string text + getText: => + return @data.text + --- Text should be set this way, or the object will not be the correct size. + --- @tparam string text The text to set. + --- @treturn element self setText: (text) => - @data.text = text - return @setSize! + @data.text = tostring text + @setSize! + return @align! + + --- Change text color. Uses LOVE's 0-255 values for components of colors. + --- @tparam ?number|table r The red component or a table of RGBA values. + --- @tparam number g The green component. + --- @tparam number b The blue component. + --- @tparam number a The alpha component. While not technically required, if + --- ANYTHING uses an alpha component and you don't, it could cause bugs in + --- rendering. + --- @treturn element self + setColor: (r, g, b, a) => + if "table" == type r + @data.color = r + else + @data.color = {r, g, b, a} + return @ diff --git a/init.lua b/init.lua index 7ffc430..38218e2 100644 --- a/init.lua +++ b/init.lua @@ -61,6 +61,7 @@ pop.skins = { } pop.extensions = { } pop.screen = false pop.focused = false +pop.hovered = false pop.log = log pop.load = function(load_path) if load_path == nil then @@ -145,6 +146,7 @@ pop.load = function(load_path) 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\"") end end @@ -207,8 +209,17 @@ pop.draw = function(element) end end end -pop.mousemoved = function(x, y, dx, dy) - if pop.focused and pop.focused.mousemoved then +pop.mousemoved = function(x, y, dx, dy, element) + if element == nil then + element = pop.screen + end + if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) then + pop.hovered = element + for i = #element.child, 1, -1 do + pop.mousemoved(x, y, dx, dy, element.child[i]) + end + end + if pop.focused and pop.focused.mousemoved and element == pop.screen then return pop.focused:mousemoved(x - pop.focused.data.x, y - pop.focused.data.y, dx, dy) end return false @@ -352,7 +363,11 @@ pop.printElementTree = function(element, depth) end cls = cls .. " (" .. tostring(bg) .. ")" end - log(string.rep("-", depth) .. " " .. tostring(cls)) + if depth > 0 then + log(string.rep("-", depth) .. " " .. tostring(cls)) + else + log(cls) + end for i = 1, #element.child do pop.printElementTree(element.child[i], depth + 1) end diff --git a/init.moon b/init.moon index 4a83aeb..b6cf021 100644 --- a/init.moon +++ b/init.moon @@ -42,6 +42,8 @@ import dumps, loads from require "#{path}/lib/bitser/bitser" --- screen. Initialized in `pop.load()` --- @tfield ?Element|false focused The currently focused GUI element (or `false` --- if none is focused). +--- @tfield ?Element|false hovered The GUI element the mouse is hovering over +--- (or `false` if none is hovered over). --- @see pop.load --- @see Element @@ -75,6 +77,7 @@ pop.skins = {} pop.extensions = {} pop.screen = false pop.focused = false +pop.hovered = false pop.log = log @@ -163,6 +166,7 @@ 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\"" @@ -261,9 +265,18 @@ pop.draw = (element=pop.screen) -> --- @tparam number dy The distance on the y axis the mouse was moved. --- @treturn boolean Was the event handled? -pop.mousemoved = (x, y, dx, dy) -> +pop.mousemoved = (x, y, dx, dy, element=pop.screen) -> + -- first we find out if we're hovering over anything and set pop.hovered + if (x >= element.data.x) and (x <= element.data.x + element.data.w) and (y >= element.data.y) and (y <= element.data.y + element.data.h) + -- okay, we're over this element for sure, but let's check its children + pop.hovered = element + -- check in reverse order, it will set pop.hovered to any that match + for i = #element.child, 1, -1 + pop.mousemoved x, y, dx, dy, element.child[i] + --- @todo Implement a way for an element to attach itself to `love.mousemoved()` events? - if pop.focused and pop.focused.mousemoved + -- checking element against pop.screen so that this only gets called once + if pop.focused and pop.focused.mousemoved and element == pop.screen return pop.focused\mousemoved x - pop.focused.data.x, y - pop.focused.data.y, dx, dy return false @@ -480,7 +493,10 @@ pop.printElementTree = (element=pop.screen, depth=0) -> cls = cls .. " (#{bg})" - log string.rep("-", depth) .. " #{cls}" + if depth > 0 + log string.rep("-", depth) .. " #{cls}" + else + log cls for i = 1, #element.child pop.printElementTree element.child[i], depth + 1