From 99fade66258e0d1f23f02b5fdee54ad2cd35236f Mon Sep 17 00:00:00 2001 From: zorfmorf Date: Thu, 12 Feb 2015 13:17:47 +0100 Subject: [PATCH 1/5] Added group decorating support: Border, bkg, padding --- button.lua | 2 +- checkbox.lua | 2 +- core.lua | 10 +++++++--- group.lua | 50 +++++++++++++++++++++++++++++++++++++++++++---- input.lua | 2 +- label.lua | 2 +- slider.lua | 2 +- slider2d.lua | 2 +- style-default.lua | 14 ++++++++++++- 9 files changed, 72 insertions(+), 14 deletions(-) diff --git a/button.lua b/button.lua index a1f0589..c63c6fe 100644 --- a/button.lua +++ b/button.lua @@ -69,7 +69,7 @@ return function(w) -- core.registerDraw(id, drawfunction, drawfunction-arguments...) -- shows widget when core.draw() is called. - core.registerDraw(id, w.draw or core.style.Button, + core.registerDraw(id, w.draw or core.style.Button, false, w.text, pos[1],pos[2], size[1],size[2]) return mouse.releasedOn(id) or keyboard.pressedOn(id, 'return') diff --git a/checkbox.lua b/checkbox.lua index 5137bb0..dbaf953 100644 --- a/checkbox.lua +++ b/checkbox.lua @@ -60,7 +60,7 @@ return function(w) w.checked = not w.checked end - core.registerDraw(id, w.draw or core.style.Checkbox, + core.registerDraw(id, w.draw or core.style.Checkbox, false, w.checked, w.text, w.align or 'left', pos[1], pos[2], size[1], size[2]) return w.checked ~= checked diff --git a/core.lua b/core.lua index 42b1ae1..d1dd999 100644 --- a/core.lua +++ b/core.lua @@ -66,7 +66,7 @@ end -- Drawing / Frame update -- local draw_items = {n = 0} -local function registerDraw(id, f, ...) +local function registerDraw(id, f, isGroup, ...) assert(type(f) == 'function' or (getmetatable(f) or {}).__call, 'Drawing function is not a callable type!') @@ -78,10 +78,14 @@ local function registerDraw(id, f, ...) end local rest = {n = select('#', ...), ...} draw_items.n = draw_items.n + 1 - draw_items[draw_items.n] = function() + + local targetindex = draw_items.n + if isGroup then targetindex = 1 end + + table.insert(draw_items, targetindex, function() if font then love.graphics.setFont(font) end f(state, unpack(rest, 1, rest.n)) - end + end) end -- actually update-and-draw diff --git a/group.lua b/group.lua index 4889cd4..b02e65d 100644 --- a/group.lua +++ b/group.lua @@ -24,6 +24,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]]-- +local BASE = (...):match("(.-)[^%.]+$") +local core = nil -- cant load here bc of circular dependencies + local stack = {n = 0} local default = { pos = {0,0}, @@ -32,6 +35,7 @@ local default = { size = {100, 30}, upper_left = {0,0}, lower_right = {0,0}, + pad = 0 } local current = default @@ -43,10 +47,14 @@ local Grow = { right = { 1, 0} } --- {grow = grow, spacing = spacing, size = size, pos = pos} +-- {grow = grow, spacing = spacing, size = size, pos = pos, draw = draw, border = bool, bkg = bool, padding = padding} local function push(info) + + if not core then core = require(BASE .. 'core') end + local grow = info.grow or "none" local spacing = info.spacing or default.spacing + local pad = info.pad or default.pad local size = { info.size and info.size[1] or current.size[1], @@ -54,6 +62,14 @@ local function push(info) } local pos = {current.pos[1], current.pos[2]} + + -- apply parent container padding (if any) + local parent = stack[stack.n] + if parent then + pos[1] = pos[1] + parent.pad + pos[2] = pos[2] + parent.pad + end + if info.pos then pos[1] = pos[1] + (info.pos[1] or 0) pos[2] = pos[2] + (info.pos[2] or 0) @@ -62,12 +78,20 @@ local function push(info) assert(size, "Size neither specified nor derivable from parent group.") assert(pos, "Position neither specified nor derivable from parent group.") grow = assert(Grow[grow], "Invalid grow: " .. tostring(grow)) - + + local id = info.id + if not id then id = core.generateID() end + current = { pos = pos, grow = grow, size = size, spacing = spacing, + pad = pad, + border = info.border, + bkg = info.bkg, + draw = info.draw, + id = id, upper_left = { math.huge, math.huge}, lower_right = {-math.huge, -math.huge}, } @@ -87,6 +111,11 @@ local function advance(pos, size) if current.grow[2] ~= 0 then current.pos[2] = pos[2] + current.grow[2] * (size[2] + current.spacing) end + + -- adjust for padding + --pos[1] = pos[1] + current.pad + --pos[2] = pos[2] + current.pad + return pos, size end @@ -104,21 +133,34 @@ local function getRect(pos, size) pos[1] = pos[1] + current.pos[1] pos[2] = pos[2] + current.pos[2] - - return advance(pos, size) + + local pos, size = advance(pos, size) + pos[1] = pos[1] + current.pad + pos[2] = pos[2] + current.pad + return pos, size end local function pop() assert(stack.n > 0, "Group stack is empty.") + stack.n = stack.n - 1 local child = current current = stack[stack.n] or default + + -- adjust for padding + child.lower_right[1] = child.lower_right[1] + child.pad * 2 + child.lower_right[2] = child.lower_right[2] + child.pad * 2 local size = { child.lower_right[1] - math.max(child.upper_left[1], current.pos[1]), child.lower_right[2] - math.max(child.upper_left[2], current.pos[2]) } advance(current.pos, size) + + if child.bkg or child.border then + core.registerDraw(child.id, child.draw or core.style.Group, true, + child.bkg, child.border, child.upper_left[1], child.upper_left[2], child.lower_right[1] - child.upper_left[1], child.lower_right[2] - child.upper_left[2]) + end end local function beginFrame() diff --git a/input.lua b/input.lua index b161993..a7e1753 100644 --- a/input.lua +++ b/input.lua @@ -73,7 +73,7 @@ return function(w) w.info.cursor = w.info.cursor + 1 end - core.registerDraw(id, w.draw or core.style.Input, + core.registerDraw(id, w.draw or core.style.Input, false, w.info.text, w.info.cursor, pos[1],pos[2], size[1],size[2]) return mouse.releasedOn(id) or keyboard.pressedOn(id, 'return') diff --git a/label.lua b/label.lua index 345516c..58e5cbc 100644 --- a/label.lua +++ b/label.lua @@ -53,7 +53,7 @@ return function(w) keyboard.clearFocus() end - core.registerDraw(id, w.draw or core.style.Label, + core.registerDraw(id, w.draw or core.style.Label, false, w.text, w.align, pos[1],pos[2], size[1],size[2]) return mouse.releasedOn(id) diff --git a/slider.lua b/slider.lua index 4700ae5..2977f80 100644 --- a/slider.lua +++ b/slider.lua @@ -72,7 +72,7 @@ return function(w) end end - core.registerDraw(id, w.draw or core.style.Slider, + core.registerDraw(id, w.draw or core.style.Slider, false, fraction, w.vertical, pos[1],pos[2], size[1],size[2]) return changed diff --git a/slider2d.lua b/slider2d.lua index 19e264c..ca732f6 100644 --- a/slider2d.lua +++ b/slider2d.lua @@ -92,7 +92,7 @@ return function(w) end end - core.registerDraw(id, w.draw or core.style.Slider2D, + core.registerDraw(id, w.draw or core.style.Slider2D, false, fraction, pos[1],pos[2], size[1],size[2]) return changed diff --git a/style-default.lua b/style-default.lua index 71f412a..1609a5c 100644 --- a/style-default.lua +++ b/style-default.lua @@ -31,7 +31,8 @@ local utf8 = require(BASE .. 'utf8') local color = { normal = {bg = {78,78,78}, fg = {200,200,200}, border={20,20,20}}, hot = {bg = {98,98,98}, fg = {69,201,84}, border={30,30,30}}, - active = {bg = {88,88,88}, fg = {49,181,64}, border={10,10,10}} + active = {bg = {88,88,88}, fg = {49,181,64}, border={10,10,10}}, + group = {bg = {88,88,88}, border = {255,255,255}} } -- box drawing @@ -189,6 +190,16 @@ local function Checkbox(state, checked, label, align, x,y,w,h) love.graphics.print(label, tx, ty) end +local function Group(state, bkg, border, x,y,w,h) + if bkg then + love.graphics.setColor(color.group.bg) + love.graphics.rectangle("fill", x, y, w, h) + end + if border then + love.graphics.setColor(color.group.border) + love.graphics.rectangle("line", x, y, w, h) + end +end -- the style return { @@ -201,4 +212,5 @@ return { Slider2D = Slider2D, Input = Input, Checkbox = Checkbox, + Group = Group, } From 67f1d33ac90bd000cb3cc1d7db7beb5294d7c9c9 Mon Sep 17 00:00:00 2001 From: zorfmorf Date: Thu, 12 Feb 2015 13:25:32 +0100 Subject: [PATCH 2/5] Cleaned up and fixed some tab issues --- group.lua | 71 +++++++++++++++++++++++------------------------ style-default.lua | 20 ++++++------- 2 files changed, 44 insertions(+), 47 deletions(-) diff --git a/group.lua b/group.lua index b02e65d..7441189 100644 --- a/group.lua +++ b/group.lua @@ -47,14 +47,15 @@ local Grow = { right = { 1, 0} } --- {grow = grow, spacing = spacing, size = size, pos = pos, draw = draw, border = bool, bkg = bool, padding = padding} +-- {grow = grow, spacing = spacing, size = size, pos = pos, draw = draw, +-- border = bool, bkg = bool, padding = padding} local function push(info) - - if not core then core = require(BASE .. 'core') end - + + if not core then core = require(BASE .. 'core') end + local grow = info.grow or "none" local spacing = info.spacing or default.spacing - local pad = info.pad or default.pad + local pad = info.pad or default.pad local size = { info.size and info.size[1] or current.size[1], @@ -62,35 +63,35 @@ local function push(info) } local pos = {current.pos[1], current.pos[2]} - - -- apply parent container padding (if any) - local parent = stack[stack.n] - if parent then - pos[1] = pos[1] + parent.pad - pos[2] = pos[2] + parent.pad - end + + -- apply parent container padding (if any) + local parent = stack[stack.n] + if parent then + pos[1] = pos[1] + parent.pad + pos[2] = pos[2] + parent.pad + end if info.pos then pos[1] = pos[1] + (info.pos[1] or 0) pos[2] = pos[2] + (info.pos[2] or 0) end - + assert(size, "Size neither specified nor derivable from parent group.") assert(pos, "Position neither specified nor derivable from parent group.") grow = assert(Grow[grow], "Invalid grow: " .. tostring(grow)) - local id = info.id - if not id then id = core.generateID() end + local id = info.id + if not id then id = core.generateID() end current = { pos = pos, grow = grow, size = size, spacing = spacing, - pad = pad, - border = info.border, - bkg = info.bkg, - draw = info.draw, + pad = pad, + border = info.border, + bkg = info.bkg, + draw = info.draw, id = id, upper_left = { math.huge, math.huge}, lower_right = {-math.huge, -math.huge}, @@ -111,11 +112,7 @@ local function advance(pos, size) if current.grow[2] ~= 0 then current.pos[2] = pos[2] + current.grow[2] * (size[2] + current.spacing) end - - -- adjust for padding - --pos[1] = pos[1] + current.pad - --pos[2] = pos[2] + current.pad - + return pos, size end @@ -130,26 +127,26 @@ local function getRect(pos, size) if current.grow[2] < 0 and current.size[2] ~= size[2] then current.pos[2] = current.pos[2] - (current.size[2] - size[2]) end - + pos[1] = pos[1] + current.pos[1] pos[2] = pos[2] + current.pos[2] - - local pos, size = advance(pos, size) - pos[1] = pos[1] + current.pad - pos[2] = pos[2] + current.pad + + pos, size = advance(pos, size) + pos[1] = pos[1] + current.pad + pos[2] = pos[2] + current.pad return pos, size end local function pop() assert(stack.n > 0, "Group stack is empty.") - + stack.n = stack.n - 1 local child = current current = stack[stack.n] or default - - -- adjust for padding - child.lower_right[1] = child.lower_right[1] + child.pad * 2 - child.lower_right[2] = child.lower_right[2] + child.pad * 2 + + -- adjust for padding + child.lower_right[1] = child.lower_right[1] + child.pad * 2 + child.lower_right[2] = child.lower_right[2] + child.pad * 2 local size = { child.lower_right[1] - math.max(child.upper_left[1], current.pos[1]), @@ -157,10 +154,10 @@ local function pop() } advance(current.pos, size) - if child.bkg or child.border then - core.registerDraw(child.id, child.draw or core.style.Group, true, + if child.bkg or child.border then + core.registerDraw(child.id, child.draw or core.style.Group, true, child.bkg, child.border, child.upper_left[1], child.upper_left[2], child.lower_right[1] - child.upper_left[1], child.lower_right[2] - child.upper_left[2]) - end + end end local function beginFrame() diff --git a/style-default.lua b/style-default.lua index 1609a5c..0cfaf38 100644 --- a/style-default.lua +++ b/style-default.lua @@ -32,7 +32,7 @@ local color = { normal = {bg = {78,78,78}, fg = {200,200,200}, border={20,20,20}}, hot = {bg = {98,98,98}, fg = {69,201,84}, border={30,30,30}}, active = {bg = {88,88,88}, fg = {49,181,64}, border={10,10,10}}, - group = {bg = {88,88,88}, border = {255,255,255}} + group = {bg = {88,88,88}, border = {255,255,255}} } -- box drawing @@ -191,14 +191,14 @@ local function Checkbox(state, checked, label, align, x,y,w,h) end local function Group(state, bkg, border, x,y,w,h) - if bkg then - love.graphics.setColor(color.group.bg) - love.graphics.rectangle("fill", x, y, w, h) - end - if border then - love.graphics.setColor(color.group.border) - love.graphics.rectangle("line", x, y, w, h) - end + if bkg then + love.graphics.setColor(color.group.bg) + love.graphics.rectangle("fill", x, y, w, h) + end + if border then + love.graphics.setColor(color.group.border) + love.graphics.rectangle("line", x, y, w, h) + end end -- the style @@ -212,5 +212,5 @@ return { Slider2D = Slider2D, Input = Input, Checkbox = Checkbox, - Group = Group, + Group = Group, } From e6685ed5e70511c37f1d5035c73a508090b3d0db Mon Sep 17 00:00:00 2001 From: zorfmorf Date: Thu, 12 Feb 2015 13:51:30 +0100 Subject: [PATCH 3/5] Abused id parameter to not break compatibility --- button.lua | 2 +- checkbox.lua | 2 +- core.lua | 4 ++-- group.lua | 9 +++------ input.lua | 2 +- label.lua | 2 +- slider.lua | 2 +- slider2d.lua | 2 +- 8 files changed, 11 insertions(+), 14 deletions(-) diff --git a/button.lua b/button.lua index c63c6fe..a1f0589 100644 --- a/button.lua +++ b/button.lua @@ -69,7 +69,7 @@ return function(w) -- core.registerDraw(id, drawfunction, drawfunction-arguments...) -- shows widget when core.draw() is called. - core.registerDraw(id, w.draw or core.style.Button, false, + core.registerDraw(id, w.draw or core.style.Button, w.text, pos[1],pos[2], size[1],size[2]) return mouse.releasedOn(id) or keyboard.pressedOn(id, 'return') diff --git a/checkbox.lua b/checkbox.lua index dbaf953..5137bb0 100644 --- a/checkbox.lua +++ b/checkbox.lua @@ -60,7 +60,7 @@ return function(w) w.checked = not w.checked end - core.registerDraw(id, w.draw or core.style.Checkbox, false, + core.registerDraw(id, w.draw or core.style.Checkbox, w.checked, w.text, w.align or 'left', pos[1], pos[2], size[1], size[2]) return w.checked ~= checked diff --git a/core.lua b/core.lua index d1dd999..bec356c 100644 --- a/core.lua +++ b/core.lua @@ -66,7 +66,7 @@ end -- Drawing / Frame update -- local draw_items = {n = 0} -local function registerDraw(id, f, isGroup, ...) +local function registerDraw(id, f, ...) assert(type(f) == 'function' or (getmetatable(f) or {}).__call, 'Drawing function is not a callable type!') @@ -80,7 +80,7 @@ local function registerDraw(id, f, isGroup, ...) draw_items.n = draw_items.n + 1 local targetindex = draw_items.n - if isGroup then targetindex = 1 end + if id == "group_draw" then targetindex = 1 end table.insert(draw_items, targetindex, function() if font then love.graphics.setFont(font) end diff --git a/group.lua b/group.lua index 7441189..82c1111 100644 --- a/group.lua +++ b/group.lua @@ -79,10 +79,7 @@ local function push(info) assert(size, "Size neither specified nor derivable from parent group.") assert(pos, "Position neither specified nor derivable from parent group.") grow = assert(Grow[grow], "Invalid grow: " .. tostring(grow)) - - local id = info.id - if not id then id = core.generateID() end - + current = { pos = pos, grow = grow, @@ -92,7 +89,6 @@ local function push(info) border = info.border, bkg = info.bkg, draw = info.draw, - id = id, upper_left = { math.huge, math.huge}, lower_right = {-math.huge, -math.huge}, } @@ -154,8 +150,9 @@ local function pop() } advance(current.pos, size) + -- skip the draw call if we don't need it if child.bkg or child.border then - core.registerDraw(child.id, child.draw or core.style.Group, true, + core.registerDraw("group_draw", child.draw or core.style.Group, child.bkg, child.border, child.upper_left[1], child.upper_left[2], child.lower_right[1] - child.upper_left[1], child.lower_right[2] - child.upper_left[2]) end end diff --git a/input.lua b/input.lua index a7e1753..b161993 100644 --- a/input.lua +++ b/input.lua @@ -73,7 +73,7 @@ return function(w) w.info.cursor = w.info.cursor + 1 end - core.registerDraw(id, w.draw or core.style.Input, false, + core.registerDraw(id, w.draw or core.style.Input, w.info.text, w.info.cursor, pos[1],pos[2], size[1],size[2]) return mouse.releasedOn(id) or keyboard.pressedOn(id, 'return') diff --git a/label.lua b/label.lua index 58e5cbc..345516c 100644 --- a/label.lua +++ b/label.lua @@ -53,7 +53,7 @@ return function(w) keyboard.clearFocus() end - core.registerDraw(id, w.draw or core.style.Label, false, + core.registerDraw(id, w.draw or core.style.Label, w.text, w.align, pos[1],pos[2], size[1],size[2]) return mouse.releasedOn(id) diff --git a/slider.lua b/slider.lua index 2977f80..4700ae5 100644 --- a/slider.lua +++ b/slider.lua @@ -72,7 +72,7 @@ return function(w) end end - core.registerDraw(id, w.draw or core.style.Slider, false, + core.registerDraw(id, w.draw or core.style.Slider, fraction, w.vertical, pos[1],pos[2], size[1],size[2]) return changed diff --git a/slider2d.lua b/slider2d.lua index ca732f6..19e264c 100644 --- a/slider2d.lua +++ b/slider2d.lua @@ -92,7 +92,7 @@ return function(w) end end - core.registerDraw(id, w.draw or core.style.Slider2D, false, + core.registerDraw(id, w.draw or core.style.Slider2D, fraction, pos[1],pos[2], size[1],size[2]) return changed From 66cba587ba92c7a27530538b50f01f47aec93672 Mon Sep 17 00:00:00 2001 From: zorfmorf Date: Thu, 12 Feb 2015 13:55:17 +0100 Subject: [PATCH 4/5] Fixed some more tab issues. Jesus Christ. --- core.lua | 8 ++++---- group.lua | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/core.lua b/core.lua index bec356c..2116e76 100644 --- a/core.lua +++ b/core.lua @@ -78,10 +78,10 @@ local function registerDraw(id, f, ...) end local rest = {n = select('#', ...), ...} draw_items.n = draw_items.n + 1 - - local targetindex = draw_items.n - if id == "group_draw" then targetindex = 1 end - + + local targetindex = draw_items.n + if id == "group_draw" then targetindex = 1 end + table.insert(draw_items, targetindex, function() if font then love.graphics.setFont(font) end f(state, unpack(rest, 1, rest.n)) diff --git a/group.lua b/group.lua index 82c1111..c7dceb4 100644 --- a/group.lua +++ b/group.lua @@ -35,7 +35,7 @@ local default = { size = {100, 30}, upper_left = {0,0}, lower_right = {0,0}, - pad = 0 + pad = 0 } local current = default From ebf0b1fc696dc8ea4f0ef6768c3b54d69f06d110 Mon Sep 17 00:00:00 2001 From: zorfmorf Date: Thu, 12 Feb 2015 15:04:10 +0100 Subject: [PATCH 5/5] Update README.md Added example background, border and padding to group widget --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 734202c..5d70de6 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Quickie is an [immediate mode gui][IMGUI] library for [LÖVE][LOVE]. Initial gui.group.pop{} if menu_open.demo then - gui.group{grow = "down", pos = {200, 80}, function() + gui.group{grow = "down", pos = {200, 80}, pad = 10, border = true, bkg = true, function() love.graphics.setFont(fonts[20]) gui.Label{text = "Widgets"} love.graphics.setFont(fonts[12])