Introduced more complete parent-child API,

This commit is contained in:
Josh
2018-06-08 13:17:39 -05:00
parent d64c40f1dd
commit 9eec6786dd
8 changed files with 74 additions and 14 deletions

15
API.md
View File

@@ -13,6 +13,9 @@ Methods:
```lua
render()
update(number deltatime) -- deltatime should be passed from love.update function
table<UIBase> getChildren()
UIBase getChild(string childName)
UIBase getParent()
```
### UIContainer class
@@ -108,10 +111,16 @@ Dimension - Dimension
### Vector2D datatype
### Color datatype
Represents color values on a 0-1 decimal range.
Represents color values.
### HexColor datatype
Represents color values as a hexadecimal string.
Constructors:
```lua
Color:new() -- will default to fully black
Color:new(number red, number green, number blue, (optional) number alpha) -- 0-1 double range, alpha defaults to 1 and is not required
Color:fromRGB(number red, number green, number blue, (optional) number alpha) -- 0-255 integer range
Color:fromHex(string HexColor, (optional) number alpha) -- takes hexadecimal color codes, can use 3 and 6 character formats
Color:fromHSL(number hue, number saturation, number lightness, (optional) number alpha) -- HSL color
```
### IntColor datatype

View File

@@ -18,4 +18,9 @@ return {
parent:addChild(child)
child:setParent(parent)
end,
unparent = function(self, parent, child)
parent:removeChild(child.name)
child:setParent(nil)
end
}

View File

@@ -5,7 +5,6 @@ local Button = UIButton:subclass("Button")
function Button:init()
self.super:init()
self.text = "Button"
self.textColor = Color:new(0, 0, 0)
self.textAlignment = "left"

View File

@@ -28,7 +28,20 @@ local Vector2D = require("lib.datatypes.Vector2D")
]]
local UIBase = newclass("UIBase")
local charset = {} do -- [0-9a-zA-Z]
for c = 48, 57 do table.insert(charset, string.char(c)) end
for c = 65, 90 do table.insert(charset, string.char(c)) end
for c = 97, 122 do table.insert(charset, string.char(c)) end
end
local function randomString(length)
if not length or length <= 0 then return '' end
math.randomseed(os.clock()^5)
return randomString(length - 1) .. charset[math.random(1, #charset)]
end
function UIBase:init()
self.name = randomString(16)
self.isUIObject = true
self.active = true
self.children = {}
@@ -53,13 +66,36 @@ function UIBase:updateChildren(delta)
end
function UIBase:render()
if not self.parent then return end
end
function UIBase:update(delta)
if not self.parent then return end
self:updateChildren(delta)
end
function UIBase:getName()
return self.name
end
function UIBase:setName(name)
self.name = name
end
function UIBase:getChild(name)
for _, obj in pairs(self:getChildren()) do
if obj:getName() == name then
return obj
end
end
end
function UIBase:getFirstChildOfType(type)
for _, obj in pairs(self:getChildren()) do
if obj.class == type then
return obj
end
end
end
function UIBase:getChildren()
return self.children
@@ -69,6 +105,17 @@ function UIBase:addChild(instance)
self.children[#self.children+1] = instance
end
function UIBase:removeChild(instName)
for index, obj in pairs(self:getChildren()) do
if obj:getName() == instName then
local object = obj
self.children[index] = nil
return obj
end
end
end
function UIBase:getParent()
return self.parent
end

View File

@@ -33,12 +33,14 @@ function UIButton:update(delta)
if self.mouseOver and love.mouse.isDown(1) then
self.mouseDown = true
self.mouseClickDown:call()
self.pressed:call()
end
if self.mouseDown == true then
if not love.mouse.isDown(1) then
self.mouseDown = false
self.mouseClickUp:call()
self.released:call()
if not self.mouseOver then
self.cancelled:call()
end

View File

@@ -154,6 +154,7 @@ function UIRect:update(delta)
end
function UIRect:getAbsoluteSize()
local pos = self:getPosition()
local size = self:getSize()

View File

@@ -2,10 +2,10 @@ local Color = {}
setmetatable(Color,{
__index = Color,
__add = function(a,b) return Color:new() end,
__tostring = function(a) return "("..a.x..','..a.y..")" end
__tostring = function(a) return "JUI.Color datatype" end
})
function Color:new(red, green, blue, alpha)
return setmetatable({
r = red or 0,

View File

@@ -10,18 +10,17 @@ local bigboy = JUI.Dimension:new(0.6, 0.6)
local mainmenu = JUI.JUIScene:new()
local background = JUI.Frame:new()
background:setName("Background")
background:setSize(bigboy)
local emptyColor = JUI.Label:new()
local emptyColor = JUI.Label:new("EmptyColor")
emptyColor:setSize(JUI.Dimension:new(0.3, 0.1))
emptyColor:setPosition(JUI.Dimension:new(0.2, 0.8))
emptyColor:setBorderColor(JUI.Color:new(0.5, 0.5, 0.5))
emptyColor:setTextSize(22)
emptyColor:setBackgroundColor(JUI.Color:fromRGB(200, 69, 128, 128))
emptyColor:setTextAlignment("center")
emptyColor:setText("Bottom Text")
emptyColor:setText("Bottom Text")
local button = JUI.Button:new()
button:setSize(JUI.Dimension:new(0.3, 0.5))
@@ -48,7 +47,6 @@ JUI:parent(mainmenu, background)
JUI:parent(mainmenu, emptyColor)
JUI:parent(background, button)
print((JUI.Color:fromHex("#FFF")):out())
print((JUI.Color:fromHex("#ADFAAD")):out())
print((JUI.Color:fromHSL(1, 128, 196)):out())
@@ -74,7 +72,6 @@ function love.load()
minheight = 480,
})
love.graphics.setBackgroundColor(0.7, 0.8, 1)
end
function love.update(delta)