From ad0a8262b0e4c99a4bf98d493cb42d8cab02ad9d Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Sun, 27 Aug 2017 23:40:09 -0700 Subject: [PATCH] added relative positioning, fixed error with window alignment --- elements/element.lua | 34 ++++++++++++++++++++++------------ elements/element.moon | 28 ++++++++++++++++++---------- elements/window.lua | 22 +++++++++++++++++----- elements/window.moon | 16 +++++++++++++--- main.lua | 10 +++++----- main.moon | 8 ++++---- 6 files changed, 79 insertions(+), 39 deletions(-) diff --git a/elements/element.lua b/elements/element.lua index d5cab19..ef2f21d 100644 --- a/elements/element.lua +++ b/elements/element.lua @@ -23,28 +23,38 @@ do return self end if horizontal then - self.data.horizontal = horizontal + self.data.horizontalAlign = horizontal end if vertical then - self.data.vertical = vertical + self.data.verticalAlign = vertical end self.data.x = self.parent.data.x self.data.y = self.parent.data.y - local _exp_0 = self.data.horizontal + local _exp_0 = self.data.horizontalAlign if "left" == _exp_0 then self.data.x = self.data.x + max(self.parent.data.padding + self.parent.data.horizontalPadding, self.data.margin + self.data.horizontalMargin) elseif "center" == _exp_0 then self.data.x = self.data.x + ((self.parent.data.w - self.data.w) / 2) elseif "right" == _exp_0 then self.data.x = self.data.x + (self.parent.data.w - self.data.w - max(self.parent.data.padding + self.parent.data.horizontalPadding, self.data.margin + self.data.horizontalMargin)) + else + self.data.x = self.data.x + (self.parent.data.w * self.data.horizontalAlign) + if self.data.horizontalAlign < 0 then + self.data.x = self.data.x + self.parent.data.w + end end - local _exp_1 = self.data.vertical + local _exp_1 = self.data.verticalAlign if "top" == _exp_1 then self.data.y = self.data.y + (self.parent.data.padding + self.data.margin + self.data.verticalMargin) elseif "center" == _exp_1 then self.data.y = self.data.y + ((self.parent.data.h - self.data.h) / 2) elseif "bottom" == _exp_1 then self.data.y = self.data.y + (self.parent.data.h - self.data.h - max(self.parent.data.padding + self.parent.data.verticalPadding, self.data.margin + self.data.verticalMargin)) + else + self.data.y = self.data.y + (self.parent.data.h * self.data.verticalAlign) + if self.data.verticalAlign < 0 then + self.data.y = self.data.y + self.parent.data.h + end end if toPixel then self.data.x = floor(self.data.x) @@ -59,7 +69,7 @@ do local dx, dy = self.data.x, self.data.y if x then self.data.x = x - local _exp_0 = self.data.horizontal + local _exp_0 = self.data.horizontalAlign if "center" == _exp_0 then self.data.x = self.data.x - (self.data.w / 2) elseif "right" == _exp_0 then @@ -68,7 +78,7 @@ do end if y then self.data.y = y - local _exp_0 = self.data.vertical + local _exp_0 = self.data.verticalAlign if "center" == _exp_0 then self.data.y = self.data.y - (self.data.h / 2) elseif "bottom" == _exp_0 then @@ -90,13 +100,13 @@ do end, getPosition = function(self) local x, y = self.data.x, self.data.y - local _exp_0 = self.data.horizontal + local _exp_0 = self.data.horizontalAlign if "center" == _exp_0 then x = x + (self.data.w / 2) elseif "right" == _exp_0 then y = y + self.data.w end - local _exp_1 = self.data.vertical + local _exp_1 = self.data.verticalAlign if "center" == _exp_1 then y = y + (self.data.h / 2) elseif "bottom" == _exp_1 then @@ -289,11 +299,11 @@ do if (self.data.align == nil) and self.parent then self.data.align = true end - if not (self.data.vertical) then - self.data.vertical = "top" + if not (self.data.verticalAlign) then + self.data.verticalAlign = "top" end - if not (self.data.horizontal) then - self.data.horizontal = "left" + if not (self.data.horizontalAlign) then + self.data.horizontalAlign = "left" end if not (self.data.margin) then self.data.margin = 0 diff --git a/elements/element.moon b/elements/element.moon index 357d93b..3f578b6 100644 --- a/elements/element.moon +++ b/elements/element.moon @@ -44,8 +44,8 @@ class element --@data.static = false if @data.static == nil @data.align = true if (@data.align == nil) and @parent - @data.vertical = "top" unless @data.vertical - @data.horizontal = "left" unless @data.horizontal + @data.verticalAlign = "top" unless @data.verticalAlign + @data.horizontalAlign = "left" unless @data.horizontalAlign @data.margin = 0 unless @data.margin @data.horizontalMargin = 0 unless @data.horizontalMargin @@ -61,27 +61,35 @@ class element align: (horizontal, vertical, toPixel=true) => unless @data.align return @ - @data.horizontal = horizontal if horizontal - @data.vertical = vertical if vertical + @data.horizontalAlign = horizontal if horizontal + @data.verticalAlign = vertical if vertical @data.x = @parent.data.x @data.y = @parent.data.y - switch @data.horizontal + switch @data.horizontalAlign when "left" @data.x += max(@parent.data.padding + @parent.data.horizontalPadding, @data.margin + @data.horizontalMargin) when "center" @data.x += (@parent.data.w - @data.w) / 2 when "right" @data.x += @parent.data.w - @data.w - max(@parent.data.padding + @parent.data.horizontalPadding, @data.margin + @data.horizontalMargin) + else + @data.x += @parent.data.w * @data.horizontalAlign + if @data.horizontalAlign < 0 + @data.x += @parent.data.w - switch @data.vertical + switch @data.verticalAlign when "top" @data.y += @parent.data.padding + @data.margin + @data.verticalMargin when "center" @data.y += (@parent.data.h - @data.h) / 2 when "bottom" @data.y += @parent.data.h - @data.h - max(@parent.data.padding + @parent.data.verticalPadding, @data.margin + @data.verticalMargin) + else + @data.y += @parent.data.h * @data.verticalAlign + if @data.verticalAlign < 0 + @data.y += @parent.data.h if toPixel @data.x = floor @data.x @@ -95,7 +103,7 @@ class element if x @data.x = x - switch @data.horizontal + switch @data.horizontalAlign when "center" @data.x -= @data.w / 2 when "right" @@ -103,7 +111,7 @@ class element if y @data.y = y - switch @data.vertical + switch @data.verticalAlign when "center" @data.y -= @data.h / 2 when "bottom" @@ -125,13 +133,13 @@ class element getPosition: => x, y = @data.x, @data.y - switch @data.horizontal + switch @data.horizontalAlign when "center" x += @data.w / 2 when "right" y += @data.w - switch @data.vertical + switch @data.verticalAlign when "center" y += @data.h / 2 when "bottom" diff --git a/elements/window.lua b/elements/window.lua index 318a4d2..d4367b3 100644 --- a/elements/window.lua +++ b/elements/window.lua @@ -46,11 +46,17 @@ do local x = 0 local y = 0 if w then - local _exp_0 = self.data.horizontal - if "center" == _exp_0 then + local _exp_0 = self.data.horizontalAlign + if "left" == _exp_0 then + local _ + elseif "center" == _exp_0 then x = x - ((w - self.data.w) / 2) elseif "right" == _exp_0 then x = x - (w - self.data.w) + else + if self.data.horizontalAlign < 0 then + x = x - (w - self.data.w) + end end self.header:setWidth(w - self.data.header_width_reduction) self.window_area:setWidth(w) @@ -59,11 +65,17 @@ do self.title:align() end if h then - local _exp_0 = self.data.vertical - if "center" == _exp_0 then + local _exp_0 = self.data.verticalAlign + if "top" == _exp_0 then + local _ + elseif "center" == _exp_0 then y = y - ((h - self.data.h) / 2) - elseif "right" == _exp_0 then + elseif "bottom" == _exp_0 then y = y - (h - self.data.h) + else + if self.data.horizontalAlign < 0 then + y = y - (h - self.data.h) + end end if self.data.titleBar then self.window_area:setHeight(h - self.header:getHeight()) diff --git a/elements/window.moon b/elements/window.moon index 2a1db96..ffb62f0 100644 --- a/elements/window.moon +++ b/elements/window.moon @@ -186,11 +186,16 @@ class window extends element y = 0 if w - switch @data.horizontal + switch @data.horizontalAlign + when "left" + local _ -- do nothing when "center" x -= (w - @data.w) / 2 when "right" x -= w - @data.w + else + if @data.horizontalAlign < 0 + x -= w - @data.w @header\setWidth w - @data.header_width_reduction @window_area\setWidth w @@ -200,11 +205,16 @@ class window extends element @title\align! if h - switch @data.vertical + switch @data.verticalAlign + when "top" + local _ -- do nothing when "center" y -= (h - @data.h) / 2 - when "right" + when "bottom" y -= h - @data.h + else + if @data.horizontalAlign < 0 + y -= h - @data.h if @data.titleBar @window_area\setHeight h - @header\getHeight! diff --git a/main.lua b/main.lua index 2190f8e..c2d1a6b 100644 --- a/main.lua +++ b/main.lua @@ -3,8 +3,8 @@ graphics = love.graphics local pop = require("") local debug = false love.load = function() - local old_method - old_method = function() + local test_original + test_original = function() pop.text("Hello World!"):align("center", "center") local testWindow = pop.window({ windowBackground = { @@ -153,8 +153,8 @@ love.load = function() w = 125 }, "Body can't leave") end - local new_method - new_method = function() + local test_obession + test_obession = function() local partsGrid = pop.dynamicGrid() return pop.window({ w = graphics.getWidth() / 2, @@ -172,7 +172,7 @@ love.load = function() }) }) end - return old_method() + return test_original() end love.update = function(dt) return pop.update(dt) diff --git a/main.moon b/main.moon index 2603d20..ab4d39d 100644 --- a/main.moon +++ b/main.moon @@ -8,7 +8,7 @@ pop = require "" debug = false love.load = -> - old_method = -> + test_original = -> pop.text("Hello World!")\align "center", "center" testWindow = pop.window({windowBackground: {200, 200, 200}, closeable: true, maximizeable: true, minimizeable: true}, "Testing Window")\move(20, 20)\setSize(200, 100)\align "right", "top" print testWindow.window_area @@ -40,7 +40,7 @@ love.load = -> pop.window(centerBox, {containMethod: "title", w: 125}, "Title can't leave") pop.window(centerBox, {containMethod: "body", w: 125}, "Body can't leave") - new_method = -> + test_obession = -> partsGrid = pop.dynamicGrid! pop.window({w: graphics.getWidth!/2, h: graphics.getHeight!, titleBar: false})\add({ pop.box({h: 17}) -- temporary height @@ -54,8 +54,8 @@ love.load = -> }) }) - old_method! - --new_method! + test_original! + -- test_obession! love.update = (dt) -> pop.update dt