added relative positioning, fixed error with window alignment

This commit is contained in:
Paul Liverman III 2017-08-27 23:40:09 -07:00
parent 169599f86c
commit ad0a8262b0
6 changed files with 79 additions and 39 deletions

View File

@ -23,28 +23,38 @@ do
return self return self
end end
if horizontal then if horizontal then
self.data.horizontal = horizontal self.data.horizontalAlign = horizontal
end end
if vertical then if vertical then
self.data.vertical = vertical self.data.verticalAlign = vertical
end end
self.data.x = self.parent.data.x self.data.x = self.parent.data.x
self.data.y = self.parent.data.y self.data.y = self.parent.data.y
local _exp_0 = self.data.horizontal local _exp_0 = self.data.horizontalAlign
if "left" == _exp_0 then 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) 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 elseif "center" == _exp_0 then
self.data.x = self.data.x + ((self.parent.data.w - self.data.w) / 2) self.data.x = self.data.x + ((self.parent.data.w - self.data.w) / 2)
elseif "right" == _exp_0 then 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)) 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 end
local _exp_1 = self.data.verticalAlign
if "top" == _exp_1 then if "top" == _exp_1 then
self.data.y = self.data.y + (self.parent.data.padding + self.data.margin + self.data.verticalMargin) self.data.y = self.data.y + (self.parent.data.padding + self.data.margin + self.data.verticalMargin)
elseif "center" == _exp_1 then elseif "center" == _exp_1 then
self.data.y = self.data.y + ((self.parent.data.h - self.data.h) / 2) self.data.y = self.data.y + ((self.parent.data.h - self.data.h) / 2)
elseif "bottom" == _exp_1 then 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)) 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 end
if toPixel then if toPixel then
self.data.x = floor(self.data.x) self.data.x = floor(self.data.x)
@ -59,7 +69,7 @@ do
local dx, dy = self.data.x, self.data.y local dx, dy = self.data.x, self.data.y
if x then if x then
self.data.x = x self.data.x = x
local _exp_0 = self.data.horizontal local _exp_0 = self.data.horizontalAlign
if "center" == _exp_0 then if "center" == _exp_0 then
self.data.x = self.data.x - (self.data.w / 2) self.data.x = self.data.x - (self.data.w / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
@ -68,7 +78,7 @@ do
end end
if y then if y then
self.data.y = y self.data.y = y
local _exp_0 = self.data.vertical local _exp_0 = self.data.verticalAlign
if "center" == _exp_0 then if "center" == _exp_0 then
self.data.y = self.data.y - (self.data.h / 2) self.data.y = self.data.y - (self.data.h / 2)
elseif "bottom" == _exp_0 then elseif "bottom" == _exp_0 then
@ -90,13 +100,13 @@ do
end, end,
getPosition = function(self) getPosition = function(self)
local x, y = self.data.x, self.data.y 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 if "center" == _exp_0 then
x = x + (self.data.w / 2) x = x + (self.data.w / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
y = y + self.data.w y = y + self.data.w
end end
local _exp_1 = self.data.vertical local _exp_1 = self.data.verticalAlign
if "center" == _exp_1 then if "center" == _exp_1 then
y = y + (self.data.h / 2) y = y + (self.data.h / 2)
elseif "bottom" == _exp_1 then elseif "bottom" == _exp_1 then
@ -289,11 +299,11 @@ do
if (self.data.align == nil) and self.parent then if (self.data.align == nil) and self.parent then
self.data.align = true self.data.align = true
end end
if not (self.data.vertical) then if not (self.data.verticalAlign) then
self.data.vertical = "top" self.data.verticalAlign = "top"
end end
if not (self.data.horizontal) then if not (self.data.horizontalAlign) then
self.data.horizontal = "left" self.data.horizontalAlign = "left"
end end
if not (self.data.margin) then if not (self.data.margin) then
self.data.margin = 0 self.data.margin = 0

View File

@ -44,8 +44,8 @@ class element
--@data.static = false if @data.static == nil --@data.static = false if @data.static == nil
@data.align = true if (@data.align == nil) and @parent @data.align = true if (@data.align == nil) and @parent
@data.vertical = "top" unless @data.vertical @data.verticalAlign = "top" unless @data.verticalAlign
@data.horizontal = "left" unless @data.horizontal @data.horizontalAlign = "left" unless @data.horizontalAlign
@data.margin = 0 unless @data.margin @data.margin = 0 unless @data.margin
@data.horizontalMargin = 0 unless @data.horizontalMargin @data.horizontalMargin = 0 unless @data.horizontalMargin
@ -61,27 +61,35 @@ class element
align: (horizontal, vertical, toPixel=true) => align: (horizontal, vertical, toPixel=true) =>
unless @data.align return @ unless @data.align return @
@data.horizontal = horizontal if horizontal @data.horizontalAlign = horizontal if horizontal
@data.vertical = vertical if vertical @data.verticalAlign = vertical if vertical
@data.x = @parent.data.x @data.x = @parent.data.x
@data.y = @parent.data.y @data.y = @parent.data.y
switch @data.horizontal switch @data.horizontalAlign
when "left" when "left"
@data.x += max(@parent.data.padding + @parent.data.horizontalPadding, @data.margin + @data.horizontalMargin) @data.x += max(@parent.data.padding + @parent.data.horizontalPadding, @data.margin + @data.horizontalMargin)
when "center" when "center"
@data.x += (@parent.data.w - @data.w) / 2 @data.x += (@parent.data.w - @data.w) / 2
when "right" when "right"
@data.x += @parent.data.w - @data.w - max(@parent.data.padding + @parent.data.horizontalPadding, @data.margin + @data.horizontalMargin) @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" when "top"
@data.y += @parent.data.padding + @data.margin + @data.verticalMargin @data.y += @parent.data.padding + @data.margin + @data.verticalMargin
when "center" when "center"
@data.y += (@parent.data.h - @data.h) / 2 @data.y += (@parent.data.h - @data.h) / 2
when "bottom" when "bottom"
@data.y += @parent.data.h - @data.h - max(@parent.data.padding + @parent.data.verticalPadding, @data.margin + @data.verticalMargin) @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 if toPixel
@data.x = floor @data.x @data.x = floor @data.x
@ -95,7 +103,7 @@ class element
if x if x
@data.x = x @data.x = x
switch @data.horizontal switch @data.horizontalAlign
when "center" when "center"
@data.x -= @data.w / 2 @data.x -= @data.w / 2
when "right" when "right"
@ -103,7 +111,7 @@ class element
if y if y
@data.y = y @data.y = y
switch @data.vertical switch @data.verticalAlign
when "center" when "center"
@data.y -= @data.h / 2 @data.y -= @data.h / 2
when "bottom" when "bottom"
@ -125,13 +133,13 @@ class element
getPosition: => getPosition: =>
x, y = @data.x, @data.y x, y = @data.x, @data.y
switch @data.horizontal switch @data.horizontalAlign
when "center" when "center"
x += @data.w / 2 x += @data.w / 2
when "right" when "right"
y += @data.w y += @data.w
switch @data.vertical switch @data.verticalAlign
when "center" when "center"
y += @data.h / 2 y += @data.h / 2
when "bottom" when "bottom"

View File

@ -46,11 +46,17 @@ do
local x = 0 local x = 0
local y = 0 local y = 0
if w then if w then
local _exp_0 = self.data.horizontal local _exp_0 = self.data.horizontalAlign
if "center" == _exp_0 then if "left" == _exp_0 then
local _
elseif "center" == _exp_0 then
x = x - ((w - self.data.w) / 2) x = x - ((w - self.data.w) / 2)
elseif "right" == _exp_0 then elseif "right" == _exp_0 then
x = x - (w - self.data.w) x = x - (w - self.data.w)
else
if self.data.horizontalAlign < 0 then
x = x - (w - self.data.w)
end
end end
self.header:setWidth(w - self.data.header_width_reduction) self.header:setWidth(w - self.data.header_width_reduction)
self.window_area:setWidth(w) self.window_area:setWidth(w)
@ -59,11 +65,17 @@ do
self.title:align() self.title:align()
end end
if h then if h then
local _exp_0 = self.data.vertical local _exp_0 = self.data.verticalAlign
if "center" == _exp_0 then if "top" == _exp_0 then
local _
elseif "center" == _exp_0 then
y = y - ((h - self.data.h) / 2) y = y - ((h - self.data.h) / 2)
elseif "right" == _exp_0 then elseif "bottom" == _exp_0 then
y = y - (h - self.data.h) y = y - (h - self.data.h)
else
if self.data.horizontalAlign < 0 then
y = y - (h - self.data.h)
end
end end
if self.data.titleBar then if self.data.titleBar then
self.window_area:setHeight(h - self.header:getHeight()) self.window_area:setHeight(h - self.header:getHeight())

View File

@ -186,11 +186,16 @@ class window extends element
y = 0 y = 0
if w if w
switch @data.horizontal switch @data.horizontalAlign
when "left"
local _ -- do nothing
when "center" when "center"
x -= (w - @data.w) / 2 x -= (w - @data.w) / 2
when "right" when "right"
x -= w - @data.w x -= w - @data.w
else
if @data.horizontalAlign < 0
x -= w - @data.w
@header\setWidth w - @data.header_width_reduction @header\setWidth w - @data.header_width_reduction
@window_area\setWidth w @window_area\setWidth w
@ -200,10 +205,15 @@ class window extends element
@title\align! @title\align!
if h if h
switch @data.vertical switch @data.verticalAlign
when "top"
local _ -- do nothing
when "center" when "center"
y -= (h - @data.h) / 2 y -= (h - @data.h) / 2
when "right" when "bottom"
y -= h - @data.h
else
if @data.horizontalAlign < 0
y -= h - @data.h y -= h - @data.h
if @data.titleBar if @data.titleBar

View File

@ -3,8 +3,8 @@ graphics = love.graphics
local pop = require("") local pop = require("")
local debug = false local debug = false
love.load = function() love.load = function()
local old_method local test_original
old_method = function() test_original = function()
pop.text("Hello World!"):align("center", "center") pop.text("Hello World!"):align("center", "center")
local testWindow = pop.window({ local testWindow = pop.window({
windowBackground = { windowBackground = {
@ -153,8 +153,8 @@ love.load = function()
w = 125 w = 125
}, "Body can't leave") }, "Body can't leave")
end end
local new_method local test_obession
new_method = function() test_obession = function()
local partsGrid = pop.dynamicGrid() local partsGrid = pop.dynamicGrid()
return pop.window({ return pop.window({
w = graphics.getWidth() / 2, w = graphics.getWidth() / 2,
@ -172,7 +172,7 @@ love.load = function()
}) })
}) })
end end
return old_method() return test_original()
end end
love.update = function(dt) love.update = function(dt)
return pop.update(dt) return pop.update(dt)

View File

@ -8,7 +8,7 @@ pop = require ""
debug = false debug = false
love.load = -> love.load = ->
old_method = -> test_original = ->
pop.text("Hello World!")\align "center", "center" 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" 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 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: "title", w: 125}, "Title can't leave")
pop.window(centerBox, {containMethod: "body", w: 125}, "Body can't leave") pop.window(centerBox, {containMethod: "body", w: 125}, "Body can't leave")
new_method = -> test_obession = ->
partsGrid = pop.dynamicGrid! partsGrid = pop.dynamicGrid!
pop.window({w: graphics.getWidth!/2, h: graphics.getHeight!, titleBar: false})\add({ pop.window({w: graphics.getWidth!/2, h: graphics.getHeight!, titleBar: false})\add({
pop.box({h: 17}) -- temporary height pop.box({h: 17}) -- temporary height
@ -54,8 +54,8 @@ love.load = ->
}) })
}) })
old_method! test_original!
--new_method! -- test_obession!
love.update = (dt) -> love.update = (dt) ->
pop.update dt pop.update dt