From d0394c596ac5fa5c03f78ae7ecdeebf766285805 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Sun, 30 Apr 2017 19:24:47 -0700 Subject: [PATCH] work in progress attempts at fixing weird problems and improvements --- elements/element.lua | 7 +++++++ elements/element.moon | 5 +++++ elements/window.lua | 9 +++++++-- elements/window.moon | 19 +++++++------------ init.lua | 27 ++++++++++++++++++++++++--- init.moon | 31 ++++++++++++++++++++++--------- 6 files changed, 72 insertions(+), 26 deletions(-) diff --git a/elements/element.lua b/elements/element.lua index cb419a6..66a09cf 100644 --- a/elements/element.lua +++ b/elements/element.lua @@ -169,6 +169,13 @@ do end end end, + dataIndexOf = function(self, data) + for i = 1, #self.data.child do + if self.data.child[i] == data then + return i + end + end + end, delete = function(self) for i = #self.child, 1, -1 do self.child[i]:delete() diff --git a/elements/element.moon b/elements/element.moon index 3b49b5a..56527f6 100644 --- a/elements/element.moon +++ b/elements/element.moon @@ -218,6 +218,11 @@ class element if @child[i] == element return i + dataIndexOf: (data) => + for i = 1, #@data.child + if @data.child[i] == data + return i + --- Deletes references to this element and then deletes it. delete: => for i=#@child, 1, -1 diff --git a/elements/window.lua b/elements/window.lua index 3f444af..7b61ad0 100644 --- a/elements/window.lua +++ b/elements/window.lua @@ -6,8 +6,6 @@ do end local path = (...):sub(1, -7) local element = require(tostring(path) .. "/element") -local box = require(tostring(path) .. "/box") -local text = require(tostring(path) .. "/text") path = path:sub(1, -11) local maximizeImage = graphics.newImage(tostring(path) .. "/images/maximize.png") local minimizeImage = graphics.newImage(tostring(path) .. "/images/minimize.png") @@ -85,6 +83,13 @@ do getPadding = function(self) return self.window_area:getPadding() end, + childAdded = function(self, element) + table.insert(self.window_area.data, table.remove(self.data.child, self:dataIndexOf(element.data))) + table.insert(self.window_area, table.remove(self.child, self:indexOf(element))) + element:align() + print("worked?") + return self + end, maximize = function(self) if self.data.maximized then self.data.x = self.data.previous.x diff --git a/elements/window.moon b/elements/window.moon index a13526d..f6e35c6 100644 --- a/elements/window.moon +++ b/elements/window.moon @@ -11,8 +11,6 @@ import graphics, mouse from love path = (...)\sub 1, -7 element = require "#{path}/element" -box = require "#{path}/box" -text = require "#{path}/text" path = path\sub 1, -11 maximizeImage = graphics.newImage "#{path}/images/maximize.png" @@ -124,21 +122,15 @@ class window extends element @header.mousereleased = (x, y, button) => if button == pop.constants.left_mouse selected = false - --pop.focused = false -- we have to manually clear our focus return true return false - -- unsure if needed or how needed - --@setSize @data.w, @data.h -- or 100, 80 @align! align: (...) => unless @data.align return @ super ... - -- don't know if this is needed or why - --for i = 1, #@child - -- @child[i]\align! @header\align! @title\align! @window_area\align! @@ -164,8 +156,6 @@ class window extends element when "right" x -= w - @data.w - -- close button stuff - @header\setWidth w - @data.header_width_reduction @window_area\setWidth w @data.w = w @@ -173,8 +163,6 @@ class window extends element @title\align! - -- close button stuff 2 ? - if h switch @data.vertical when "center" @@ -205,6 +193,13 @@ class window extends element getPadding: => return @window_area\getPadding! + childAdded: (element) => + table.insert @window_area.data, table.remove @data.child, @dataIndexOf element.data + table.insert @window_area, table.remove @child, @indexOf element + element\align! + print "worked?" + return @ + maximize: => if @data.maximized @data.x = @data.previous.x diff --git a/init.lua b/init.lua index 6131954..b496bcc 100644 --- a/init.lua +++ b/init.lua @@ -162,6 +162,7 @@ pop.create = function(element, parent, data, ...) end insert(parent.child, element) insert(parent.data.child, element.data) + element.parent = parent element.data.parent = parent.data elseif parent == false then if type(data) == "table" then @@ -179,7 +180,12 @@ pop.create = function(element, parent, data, ...) end insert(pop.screen.child, element) insert(pop.screen.data.child, element.data) + element.parent = pop.screen element.data.parent = pop.screen.data + if element.parent and element.parent.childAdded then + print("working?") + element.parent:childAdded(element) + end end return element end @@ -368,16 +374,31 @@ pop.debugDraw = function(element) pop.debugDraw(element.child[i]) end end -pop.printElementTree = function(element, depth) +pop.printElementTree = function(element, fn, depth) if element == nil then element = pop.screen end if depth == nil then depth = 0 end + if "table" ~= type(element) then + depth = fn + fn = element + element = pop.screen + end + if "number" == type(fn) then + depth = fn + fn = nil + end + if not (depth) then + depth = 0 + end local cls = element.__class.__name if element.debugInfo then - cls = tostring(cls) .. " (" .. tostring(element:debugInfo()) .. ")" + cls = cls .. " (" .. tostring(element:debugInfo()) .. ")" + end + if fn then + cls = cls .. " " .. tostring(fn(element)) end if depth > 0 then log(string.rep("-", depth) .. " " .. tostring(cls)) @@ -385,7 +406,7 @@ pop.printElementTree = function(element, depth) log(cls) end for i = 1, #element.child do - pop.printElementTree(element.child[i], depth + 1) + pop.printElementTree(element.child[i], fn, depth + 1) end end pop.load() diff --git a/init.moon b/init.moon index df96402..f452b8d 100644 --- a/init.moon +++ b/init.moon @@ -194,7 +194,7 @@ pop.create = (element, parent=pop.screen, data, ...) -> element = pop.elements[element](parent, {}, data, ...) insert parent.child, element insert parent.data.child, element.data - --element.parent = parent + element.parent = parent element.data.parent = parent.data -- if explicitly no parent, just create the element elseif parent == false @@ -210,15 +210,15 @@ pop.create = (element, parent=pop.screen, data, ...) -> element = pop.elements[element](pop.screen, parent, data, ...) else -- parent must be an argument element = pop.elements[element](pop.screen, {}, parent, data, ...) - --if type(data) == "table" - -- element = pop.elements[element](pop.screen, parent, data, ...) - --else - -- element = pop.elements[element](pop.screen, parent, {}, data, ...) insert pop.screen.child, element insert pop.screen.data.child, element.data - --element.parent = pop.screen + element.parent = pop.screen element.data.parent = pop.screen.data + if element.parent and element.parent.childAdded + print "working?" + element.parent\childAdded element + return element @@ -513,12 +513,25 @@ pop.debugDraw = (element=pop.screen) -> --- `pop.screen`. --- @see Element -pop.printElementTree = (element=pop.screen, depth=0) -> +pop.printElementTree = (element=pop.screen, fn, depth=0) -> + if "table" != type element + depth = fn + fn = element + element = pop.screen + if "number" == type fn + depth = fn + fn = nil + unless depth + depth=0 + --- @todo Write debugInfo things for elements. cls = element.__class.__name if element.debugInfo - cls = "#{cls} (#{element\debugInfo!})" + cls ..= " (#{element\debugInfo!})" + + if fn + cls ..= " #{fn element}" if depth > 0 log string.rep("-", depth) .. " #{cls}" @@ -526,7 +539,7 @@ pop.printElementTree = (element=pop.screen, depth=0) -> log cls for i = 1, #element.child - pop.printElementTree element.child[i], depth + 1 + pop.printElementTree element.child[i], fn, depth + 1