some trek-based improvements

This commit is contained in:
Paul Liverman III 2016-11-24 19:37:53 -08:00
parent 84fe9f95f5
commit 92da91076e
4 changed files with 83 additions and 6 deletions

View File

@ -1,5 +1,7 @@
local graphics
graphics = love.graphics
local floor
floor = math.floor
local element
do
local _class_0
@ -13,6 +15,39 @@ do
graphics.setColor(200, 255, 200, 255)
return graphics.print("e", self.data.x, self.data.y)
end,
align = function(self, horizontal, vertical, toPixel)
if toPixel == nil then
toPixel = true
end
if not (self.data.align) then
return false
end
if horizontal then
self.data.horizontal = horizontal
end
if vertical then
self.data.vertical = vertical
end
self.data.x = self.parent.data.x
self.data.y = self.parent.data.y
local _exp_0 = self.data.horizontal
if "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)
end
local _exp_1 = self.data.vertical
if "center" == _exp_1 then
self.data.y = self.data.y + ((self.parent.data.h - self.data.h) / 2)
elseif "right" == _exp_1 then
self.data.y = self.data.y + (self.parent.data.h - self.data.h)
end
if toPixel then
self.data.x = floor(self.data.x)
self.data.y = floor(self.data.y)
end
return self
end,
setSize = function(self, w, h)
if w then
self.data.w = w
@ -47,7 +82,7 @@ do
data = { }
end
self.parent, self.data = parent, data
if type(self.data ~= "table") then
if type(self.data) ~= "table" then
self.data = { }
end
if not (self.data.parent) then
@ -77,7 +112,17 @@ do
if not (self.data.type) then
self.data.type = "element"
end
if (self.data.align == nil) and self.parent then
self.data.align = true
end
if not (self.data.vertical) then
self.data.vertical = "top"
end
if not (self.data.horizontal) then
self.data.horizontal = "left"
end
self.child = { }
return self:align()
end,
__base = _base_0,
__name = "element"

View File

@ -4,6 +4,7 @@
--- @license The MIT License (MIT)
import graphics from love
import floor from math
class element
--- Constructor expects nothing, or a data table describing it.
@ -11,7 +12,7 @@ class element
--- @tparam table data[opt] The data (state) for this element.
--- @treturn element self
new: (@parent, @data={}) =>
if type @data != "table"
if type(@data) != "table"
@data = {}
@data.parent = false unless @data.parent
@ -23,9 +24,14 @@ class element
@data.update = false if @data.update == nil
@data.draw = true if @data.draw == nil
@data.type = "element" unless @data.type
@data.align = true if (@data.align == nil) and @parent
@data.vertical = "top" unless @data.vertical
@data.horizontal = "left" unless @data.horizontal
@child = {}
@align!
--- Slightly modified from pop.debugDraw
--- @see pop.debugDraw
debugDraw: =>
@ -37,6 +43,34 @@ class element
graphics.setColor 200, 255, 200, 255
graphics.print "e", @data.x, @data.y
--- @todo doc me
align: (horizontal, vertical, toPixel=true) =>
unless @data.align return false
@data.horizontal = horizontal if horizontal
@data.vertical = vertical if vertical
@data.x = @parent.data.x
@data.y = @parent.data.y
switch @data.horizontal
when "center"
@data.x += (@parent.data.w - @data.w) / 2
when "right"
@data.x += @parent.data.w - @data.w
switch @data.vertical
when "center"
@data.y += (@parent.data.h - @data.h) / 2
when "right"
@data.y += @parent.data.h - @data.h
if toPixel
@data.x = floor @data.x
@data.y = floor @data.y
return @
--- Sets an element's width/height.
--- @tparam integer w[opt] Width.
--- @tparam integer h[opt] Height.

View File

@ -15,9 +15,7 @@ do
end
self.parent, self.data = parent, data
_class_0.__parent.__init(self, self.parent, self.data)
if self.data.type == "element" then
self.data.type = "window"
end
self.data.type = "window"
end,
__base = _base_0,
__name = "window",

View File

@ -10,7 +10,7 @@ class window extends element
new: (@parent, @data={}) =>
super @parent, @data
@data.type = "window" if @data.type == "element"
@data.type = "window"
--- @todo if data, do stuff about it