mirror of
https://github.com/TangentFoxy/Pop.Box.git
synced 2024-12-15 12:44:20 +00:00
STUFFS EVERYWHERE
This commit is contained in:
parent
44ab484fec
commit
a2db191adf
@ -1,7 +1,10 @@
|
||||
file = {
|
||||
"init.moon",
|
||||
"elements/box.moon",
|
||||
"elements/element.moon",
|
||||
"elements/text.moon",
|
||||
"elements/window.moon",
|
||||
"extensions/streamlined_get_set.moon",
|
||||
"main.moon",
|
||||
"util.moon",
|
||||
"Element.luadoc",
|
||||
|
@ -22,16 +22,20 @@ do
|
||||
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
|
||||
if "left" == _exp_0 then
|
||||
self.data.x = self.data.x + self.data.padding
|
||||
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)
|
||||
self.data.x = self.data.x + (self.parent.data.w - self.data.w - self.data.padding)
|
||||
end
|
||||
local _exp_1 = self.data.vertical
|
||||
if "center" == _exp_1 then
|
||||
if "top" == _exp_1 then
|
||||
self.data.y = self.data.y + self.data.padding
|
||||
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)
|
||||
self.data.y = self.data.y + (self.parent.data.h - self.data.h - self.data.padding)
|
||||
end
|
||||
if toPixel then
|
||||
self.data.x = floor(self.data.x)
|
||||
@ -46,22 +50,22 @@ do
|
||||
local dx, dy = self.data.x, self.data.y
|
||||
if x then
|
||||
self.data.x = x
|
||||
end
|
||||
if y then
|
||||
self.data.y = y
|
||||
end
|
||||
local _exp_0 = self.data.horizontal
|
||||
if "center" == _exp_0 then
|
||||
self.data.x = self.data.x - (self.data.w / 2)
|
||||
elseif "right" == _exp_0 then
|
||||
self.data.x = self.data.x - self.data.w
|
||||
end
|
||||
local _exp_1 = self.data.vertical
|
||||
if "center" == _exp_1 then
|
||||
end
|
||||
if y then
|
||||
self.data.y = y
|
||||
local _exp_0 = self.data.vertical
|
||||
if "center" == _exp_0 then
|
||||
self.data.y = self.data.y - (self.data.h / 2)
|
||||
elseif "bottom" == _exp_1 then
|
||||
elseif "bottom" == _exp_0 then
|
||||
self.data.y = self.data.y - self.data.h
|
||||
end
|
||||
end
|
||||
if toPixel then
|
||||
self.data.x = floor(self.data.x)
|
||||
self.data.y = floor(self.data.y)
|
||||
@ -76,7 +80,20 @@ do
|
||||
return self
|
||||
end,
|
||||
getPosition = function(self)
|
||||
return self.data.x, self.data.y
|
||||
local x, y = self.data.x, self.data.y
|
||||
local _exp_0 = self.data.horizontal
|
||||
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
|
||||
if "center" == _exp_1 then
|
||||
y = y + (self.data.h / 2)
|
||||
elseif "bottom" == _exp_1 then
|
||||
y = y + self.data.h
|
||||
end
|
||||
return x, y
|
||||
end,
|
||||
setSize = function(self, w, h)
|
||||
if w then
|
||||
@ -93,6 +110,7 @@ do
|
||||
end,
|
||||
setWidth = function(self, w)
|
||||
self.data.w = w
|
||||
self:align()
|
||||
return self
|
||||
end,
|
||||
getWidth = function(self)
|
||||
@ -100,11 +118,23 @@ do
|
||||
end,
|
||||
setHeight = function(self, h)
|
||||
self.data.h = h
|
||||
self:align()
|
||||
return self
|
||||
end,
|
||||
getHeight = function(self)
|
||||
return self.data.h
|
||||
end,
|
||||
adjustSize = function(self, w, h)
|
||||
local W, H = self:getSize()
|
||||
if w then
|
||||
W = W + w
|
||||
end
|
||||
if h then
|
||||
H = H + h
|
||||
end
|
||||
self:setSize(W, H)
|
||||
return self
|
||||
end,
|
||||
move = function(self, x, y)
|
||||
if x == nil then
|
||||
x = 0
|
||||
@ -112,15 +142,23 @@ do
|
||||
if y == nil then
|
||||
y = 0
|
||||
end
|
||||
self.data.x = self.data.x + x
|
||||
self.data.y = self.data.y + y
|
||||
local _list_0 = self.child
|
||||
for _index_0 = 1, #_list_0 do
|
||||
local child = _list_0[_index_0]
|
||||
child:move(x, y)
|
||||
end
|
||||
self.data.x = self.data.x + x
|
||||
self.data.y = self.data.y + y
|
||||
return self
|
||||
end,
|
||||
setPadding = function(self, padding)
|
||||
self.data.padding = padding
|
||||
self:align()
|
||||
return self
|
||||
end,
|
||||
getPadding = function(self)
|
||||
return self.data.padding
|
||||
end,
|
||||
delete = function(self)
|
||||
for i = #self.child, 1, -1 do
|
||||
self.child[i]:delete()
|
||||
@ -166,11 +204,19 @@ do
|
||||
self.data.type = "element"
|
||||
end
|
||||
if not (self.data.x) then
|
||||
if self.parent then
|
||||
self.data.x = self.parent.data.x
|
||||
else
|
||||
self.data.x = 0
|
||||
end
|
||||
end
|
||||
if not (self.data.y) then
|
||||
if self.parent then
|
||||
self.data.y = self.parent.data.y
|
||||
else
|
||||
self.data.y = 0
|
||||
end
|
||||
end
|
||||
if not (self.data.w) then
|
||||
self.data.w = 0
|
||||
end
|
||||
@ -195,6 +241,9 @@ do
|
||||
if not (self.data.horizontal) then
|
||||
self.data.horizontal = "left"
|
||||
end
|
||||
if not (self.data.padding) then
|
||||
self.data.padding = 0
|
||||
end
|
||||
self.child = { }
|
||||
end,
|
||||
__base = _base_0,
|
||||
|
@ -19,18 +19,28 @@ class element
|
||||
@data.child = {} unless @data.child
|
||||
@data.type = "element" unless @data.type
|
||||
|
||||
@data.x = 0 unless @data.x
|
||||
@data.y = 0 unless @data.y
|
||||
unless @data.x
|
||||
if @parent
|
||||
@data.x = @parent.data.x
|
||||
else
|
||||
@data.x = 0
|
||||
unless @data.y
|
||||
if @parent
|
||||
@data.y = @parent.data.y
|
||||
else
|
||||
@data.y = 0
|
||||
@data.w = 0 unless @data.w
|
||||
@data.h = 0 unless @data.h
|
||||
|
||||
@data.update = true if @data.update == nil
|
||||
@data.draw = true if @data.draw == nil
|
||||
@data.hoverable = true if @data.hoverable == nil
|
||||
--@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.padding = 0 unless @data.padding
|
||||
|
||||
@child = {}
|
||||
|
||||
@ -45,16 +55,20 @@ class element
|
||||
@data.y = @parent.data.y
|
||||
|
||||
switch @data.horizontal
|
||||
when "left"
|
||||
@data.x += @data.padding
|
||||
when "center"
|
||||
@data.x += (@parent.data.w - @data.w) / 2
|
||||
when "right"
|
||||
@data.x += @parent.data.w - @data.w
|
||||
@data.x += @parent.data.w - @data.w - @data.padding
|
||||
|
||||
switch @data.vertical
|
||||
when "top"
|
||||
@data.y += @data.padding
|
||||
when "center"
|
||||
@data.y += (@parent.data.h - @data.h) / 2
|
||||
when "bottom"
|
||||
@data.y += @parent.data.h - @data.h
|
||||
@data.y += @parent.data.h - @data.h - @data.padding
|
||||
|
||||
if toPixel
|
||||
@data.x = floor @data.x
|
||||
@ -67,18 +81,15 @@ class element
|
||||
dx, dy = @data.x, @data.y
|
||||
|
||||
if x
|
||||
--dx = x - @data.x
|
||||
@data.x = x
|
||||
if y
|
||||
--dy = y - @data.y
|
||||
@data.y = y
|
||||
|
||||
switch @data.horizontal
|
||||
when "center"
|
||||
@data.x -= @data.w / 2
|
||||
when "right"
|
||||
@data.x -= @data.w
|
||||
|
||||
if y
|
||||
@data.y = y
|
||||
switch @data.vertical
|
||||
when "center"
|
||||
@data.y -= @data.h / 2
|
||||
@ -98,9 +109,22 @@ class element
|
||||
return @
|
||||
|
||||
--- @todo doc me
|
||||
--- @todo rewrite me to return value based on alignment instead of just x/y
|
||||
getPosition: =>
|
||||
return @data.x, @data.y
|
||||
x, y = @data.x, @data.y
|
||||
|
||||
switch @data.horizontal
|
||||
when "center"
|
||||
x += @data.w / 2
|
||||
when "right"
|
||||
y += @data.w
|
||||
|
||||
switch @data.vertical
|
||||
when "center"
|
||||
y += @data.h / 2
|
||||
when "bottom"
|
||||
y += @data.h
|
||||
|
||||
return x, y
|
||||
|
||||
--- Sets an element's width/height. Fixes alignment if needed.
|
||||
--- @tparam integer w[opt] Width.
|
||||
@ -127,6 +151,7 @@ class element
|
||||
--- @treturn element self
|
||||
setWidth: (w) =>
|
||||
@data.w = w
|
||||
@align!
|
||||
return @
|
||||
|
||||
--- Returns an element's width.
|
||||
@ -139,6 +164,7 @@ class element
|
||||
--- @treturn element self
|
||||
setHeight: (h) =>
|
||||
@data.h = h
|
||||
@align!
|
||||
return @
|
||||
|
||||
--- Returns an element's height.
|
||||
@ -146,16 +172,40 @@ class element
|
||||
getHeight: =>
|
||||
return @data.h
|
||||
|
||||
--- @todo doc me
|
||||
adjustSize: (w, h) =>
|
||||
W, H = @getSize!
|
||||
|
||||
if w
|
||||
W += w
|
||||
if h
|
||||
H += h
|
||||
|
||||
@setSize W, H
|
||||
|
||||
return @
|
||||
|
||||
--- Moves an element by specified x/y.
|
||||
--- @treturn element self
|
||||
move: (x=0, y=0) =>
|
||||
for child in *@child
|
||||
child\move x, y
|
||||
--if @data.static return @
|
||||
|
||||
@data.x += x
|
||||
@data.y += y
|
||||
|
||||
for child in *@child
|
||||
child\move x, y
|
||||
|
||||
return @
|
||||
|
||||
setPadding: (padding) =>
|
||||
@data.padding = padding
|
||||
@align!
|
||||
return @
|
||||
|
||||
getPadding: =>
|
||||
return @data.padding
|
||||
|
||||
--- Deletes references to this element and then deletes it.
|
||||
delete: =>
|
||||
for i=#@child, 1, -1
|
||||
|
39
extensions/streamlined_get_set.lua
Normal file
39
extensions/streamlined_get_set.lua
Normal file
@ -0,0 +1,39 @@
|
||||
local graphics
|
||||
graphics = love.graphics
|
||||
local sub, len
|
||||
do
|
||||
local _obj_0 = string
|
||||
sub, len = _obj_0.sub, _obj_0.len
|
||||
end
|
||||
local path = sub(..., 1, len(...) - len("/extensions/streamlined_get_set"))
|
||||
local element = require(tostring(path) .. "/elements/element")
|
||||
local box = require(tostring(path) .. "/elements/box")
|
||||
local text = require(tostring(path) .. "/elements/text")
|
||||
element.__base.position = function(self, x, y)
|
||||
if x or y then
|
||||
return self:setPosition(x, y)
|
||||
else
|
||||
return self:getPosition()
|
||||
end
|
||||
end
|
||||
element.__base.size = function(self, w, h)
|
||||
if w or h then
|
||||
return self:setSize(w, h)
|
||||
else
|
||||
return self:getSize()
|
||||
end
|
||||
end
|
||||
element.__base.width = function(self, w)
|
||||
if w then
|
||||
return self:setWidth(w)
|
||||
else
|
||||
return self:getWidth()
|
||||
end
|
||||
end
|
||||
elements.__base.height = function(self, h)
|
||||
if h then
|
||||
return self:setHeight(h)
|
||||
else
|
||||
return self:getHeight()
|
||||
end
|
||||
end
|
37
extensions/streamlined_get_set.moon
Normal file
37
extensions/streamlined_get_set.moon
Normal file
@ -0,0 +1,37 @@
|
||||
--- @todo doc me (and add me to config.ld)
|
||||
-- Adds methods to elements using a single function for get and set operations.
|
||||
-- ex: instead of getWidth() and setWidth(value), width() and width(value)
|
||||
|
||||
import graphics from love
|
||||
import sub, len from string
|
||||
|
||||
path = sub ..., 1, len(...) - len "/extensions/streamlined_get_set"
|
||||
element = require "#{path}/elements/element"
|
||||
box = require "#{path}/elements/box"
|
||||
text = require "#{path}/elements/text"
|
||||
|
||||
element.__base.position = (x, y) =>
|
||||
if x or y
|
||||
return @setPosition x, y
|
||||
else
|
||||
return @getPosition!
|
||||
|
||||
element.__base.size = (w, h) =>
|
||||
if w or h
|
||||
return @setSize w, h
|
||||
else
|
||||
return @getSize!
|
||||
|
||||
element.__base.width = (w) =>
|
||||
if w
|
||||
return @setWidth w
|
||||
else
|
||||
return @getWidth!
|
||||
|
||||
elements.__base.height = (h) =>
|
||||
if h
|
||||
return @setHeight h
|
||||
else
|
||||
return @getHeight!
|
||||
|
||||
--- @todo continue copying from old version... (and add new things or whatever)
|
15
extensions/utility.lua
Normal file
15
extensions/utility.lua
Normal file
@ -0,0 +1,15 @@
|
||||
local graphics
|
||||
graphics = love.graphics
|
||||
local sub, len
|
||||
do
|
||||
local _obj_0 = string
|
||||
sub, len = _obj_0.sub, _obj_0.len
|
||||
end
|
||||
local path = sub(..., 1, len(...) - len("/extensions/streamlined_get_set"))
|
||||
local element = require(tostring(path) .. "/elements/element")
|
||||
element.__base.fill = function(self)
|
||||
self.data.x = self.parent.data.x + self.data.padding
|
||||
self.data.y = self.parent.data.y + self.data.padding
|
||||
self.data.w = self.parent.data.w - self.data.padding * 2
|
||||
self.data.h = self.parent.data.h - self.data.padding * 2
|
||||
end
|
17
extensions/utility.moon
Normal file
17
extensions/utility.moon
Normal file
@ -0,0 +1,17 @@
|
||||
--- @todo doc me
|
||||
--- Functions I am not certain should be part of base classes, but nevertheless
|
||||
--- may be useful.
|
||||
|
||||
import graphics from love
|
||||
import sub, len from string
|
||||
|
||||
path = sub ..., 1, len(...) - len "/extensions/streamlined_get_set"
|
||||
element = require "#{path}/elements/element"
|
||||
--box = require "#{path}/elements/box"
|
||||
--text = require "#{path}/elements/text"
|
||||
|
||||
element.__base.fill = =>
|
||||
@data.x = @parent.data.x + @data.padding
|
||||
@data.y = @parent.data.y + @data.padding
|
||||
@data.w = @parent.data.w - @data.padding*2
|
||||
@data.h = @parent.data.h - @data.padding*2
|
138
main.lua
138
main.lua
@ -1,10 +1,144 @@
|
||||
local pop = require("")
|
||||
pop.text("Hello World!"):align("center", "center")
|
||||
local debug = false
|
||||
love.load = function()
|
||||
pop.text("Hello World!"):align("center", "center")
|
||||
local centerBox = pop.box({
|
||||
w = 200,
|
||||
h = 200
|
||||
}, {
|
||||
255,
|
||||
255,
|
||||
0,
|
||||
120
|
||||
}):align("center", "center")
|
||||
pop.box(centerBox, {
|
||||
w = 10,
|
||||
h = 20
|
||||
}):align("left", "top")
|
||||
pop.box(centerBox, {
|
||||
w = 30,
|
||||
h = 30
|
||||
}):align("center", "top")
|
||||
pop.box(centerBox, {
|
||||
w = 5,
|
||||
h = 40
|
||||
}):align("left", "center")
|
||||
pop.box(centerBox, {
|
||||
w = 50,
|
||||
h = 50
|
||||
}):align("right", "center")
|
||||
pop.box(centerBox):align("left", "bottom"):setSize(5, 5)
|
||||
pop.box(centerBox, {
|
||||
w = 25,
|
||||
h = 10
|
||||
}):align("center", "bottom")
|
||||
pop.text(centerBox, "Align me!"):align("right", "top")
|
||||
pop.window(centerBox):align("right", "bottom")
|
||||
pop.box(centerBox, {
|
||||
padding = 5,
|
||||
w = 10,
|
||||
h = 20,
|
||||
background = {
|
||||
0,
|
||||
0,
|
||||
255,
|
||||
100
|
||||
}
|
||||
}):align("left", "top")
|
||||
pop.box(centerBox, {
|
||||
padding = 5,
|
||||
w = 30,
|
||||
h = 30,
|
||||
background = {
|
||||
0,
|
||||
0,
|
||||
255,
|
||||
100
|
||||
}
|
||||
}):align("center", "top")
|
||||
pop.box(centerBox, {
|
||||
padding = 5,
|
||||
w = 5,
|
||||
h = 40,
|
||||
background = {
|
||||
0,
|
||||
0,
|
||||
255,
|
||||
100
|
||||
}
|
||||
}):align("left", "center")
|
||||
pop.box(centerBox, {
|
||||
padding = 5,
|
||||
w = 50,
|
||||
h = 50,
|
||||
background = {
|
||||
0,
|
||||
0,
|
||||
255,
|
||||
100
|
||||
}
|
||||
}):align("right", "center")
|
||||
pop.text(centerBox, {
|
||||
padding = 5,
|
||||
color = {
|
||||
0,
|
||||
0,
|
||||
255,
|
||||
100
|
||||
}
|
||||
}, "Text!"):align("left", "bottom")
|
||||
pop.box(centerBox, {
|
||||
padding = 5,
|
||||
w = 25,
|
||||
h = 10,
|
||||
background = {
|
||||
0,
|
||||
0,
|
||||
255,
|
||||
100
|
||||
}
|
||||
}):align("center", "bottom")
|
||||
pop.text(centerBox, {
|
||||
padding = 5,
|
||||
color = {
|
||||
0,
|
||||
0,
|
||||
255,
|
||||
100
|
||||
}
|
||||
}, "Align me!"):align("right", "top")
|
||||
return pop.window(centerBox, {
|
||||
padding = 5,
|
||||
titleColor = {
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
150
|
||||
},
|
||||
titleBackground = {
|
||||
0,
|
||||
0,
|
||||
255,
|
||||
100
|
||||
},
|
||||
windowBackground = {
|
||||
200,
|
||||
200,
|
||||
255,
|
||||
100
|
||||
}
|
||||
}):align("right", "bottom")
|
||||
end
|
||||
love.draw = function()
|
||||
return pop.draw()
|
||||
pop.draw()
|
||||
if debug then
|
||||
return pop.debugDraw()
|
||||
end
|
||||
end
|
||||
love.keypressed = function(key)
|
||||
if key == "escape" then
|
||||
return love.event.quit()
|
||||
elseif key == "d" then
|
||||
debug = not debug
|
||||
end
|
||||
end
|
||||
|
28
main.moon
28
main.moon
@ -3,18 +3,42 @@
|
||||
--- @license The MIT License (MIT)
|
||||
|
||||
pop = require ""
|
||||
debug = false
|
||||
|
||||
pop.text("Hello World!")\align "center", "center"
|
||||
love.load = ->
|
||||
pop.text("Hello World!")\align "center", "center"
|
||||
|
||||
-- alignment testing
|
||||
centerBox = pop.box({w: 200, h: 200}, {255, 255, 0, 120})\align "center", "center"
|
||||
pop.box(centerBox, {w: 10, h: 20})\align "left", "top"
|
||||
pop.box(centerBox, {w: 30, h: 30})\align "center", "top"
|
||||
pop.box(centerBox, {w: 5, h: 40})\align "left", "center"
|
||||
pop.box(centerBox, {w: 50, h: 50})\align "right", "center"
|
||||
pop.box(centerBox)\align("left", "bottom")\setSize 5, 5
|
||||
pop.box(centerBox, {w: 25, h: 10})\align "center", "bottom"
|
||||
pop.text(centerBox, "Align me!")\align "right", "top"
|
||||
pop.window(centerBox)\align "right", "bottom"
|
||||
|
||||
pop.box(centerBox, {padding: 5, w: 10, h: 20, background: {0, 0, 255, 100}})\align "left", "top"
|
||||
pop.box(centerBox, {padding: 5, w: 30, h: 30, background: {0, 0, 255, 100}})\align "center", "top"
|
||||
pop.box(centerBox, {padding: 5, w: 5, h: 40, background: {0, 0, 255, 100}})\align "left", "center"
|
||||
pop.box(centerBox, {padding: 5, w: 50, h: 50, background: {0, 0, 255, 100}})\align "right", "center"
|
||||
pop.text(centerBox, {padding: 5, color: {0, 0, 255, 100}}, "Text!")\align("left", "bottom")--\setSize 5, 5
|
||||
pop.box(centerBox, {padding: 5, w: 25, h: 10, background: {0, 0, 255, 100}})\align "center", "bottom"
|
||||
pop.text(centerBox, {padding: 5, color: {0, 0, 255, 100}}, "Align me!")\align "right", "top"
|
||||
pop.window(centerBox, {padding: 5, titleColor: {0, 0, 0, 150}, titleBackground: {0, 0, 255, 100}, windowBackground: {200, 200, 255, 100}})\align "right", "bottom"
|
||||
|
||||
--- @todo finish writing callbacks!
|
||||
|
||||
love.draw = ->
|
||||
pop.draw!
|
||||
--pop.debugDraw!
|
||||
pop.debugDraw! if debug
|
||||
|
||||
love.keypressed = (key) ->
|
||||
if key == "escape"
|
||||
love.event.quit!
|
||||
elseif key == "d"
|
||||
debug = not debug
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user