From 65d700954850dd25995587a17728a23930283be0 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Wed, 20 Dec 2023 00:42:04 -0500 Subject: [PATCH] Working on gui canvas --- core/canvas.lua | 98 +++++++++++++++++++++++++++++++++++++++++++++++++ core/init.lua | 0 init.lua | 86 +++++++++++++++++++++++++++++++++---------- 3 files changed, 165 insertions(+), 19 deletions(-) create mode 100644 core/canvas.lua delete mode 100644 core/init.lua diff --git a/core/canvas.lua b/core/canvas.lua new file mode 100644 index 0000000..0592e6b --- /dev/null +++ b/core/canvas.lua @@ -0,0 +1,98 @@ +local gui = require("gui") +local multi, thread = require("multi"):init() + +local canvas = {} + +-- Run the canvas logic within another processor +local proc = gui:newProcessor("Canvas Handler") +local updater = proc:newLoop().OnLoop + +local draw_handler = gui.draw_handler + +function canvas:newCanvas() + local c = {} + setmetatable(c, gui) + local active = false + c.type = gui.TYPE_FRAME + c.children = {} + c.dualDim = gui:newDualDim() + c.x = 0 + c.y = 0 + + local w, h = love.graphics.getDimensions() + c.dualDim.offset.size.x = w + c.dualDim.offset.size.y = h + c.w = w + c.h = h + c.parent = c + + table.insert(canvas, c) + + proc:newThread(function() + while true do + print("Holding...") + thread.hold(c.isActive) + local children = c:getAllChildren() + for i = 1, #children do + local child = children[i] + if child.effect then + child.effect(function() draw_handler(child, true) end) + else + draw_handler(child, true) + end + end + first_loop = true + end + end) + + function c:isActive(b) + if b == nil then + return active + end + active = b + end + + function c:OnUpdate(func) + if type(self) == "function" then func = self end + updater(function() func(c) end) + end + + function c:newThread(func) + return proc:newThread("ThreadHandler<" .. self.type .. ">", func, self, thread) + end + + function c:swap(c1, c2) + local ch1 = c1:getChildren() + local ch2 = c2:getChildren() + for i = 1, #ch1 do + ch1[i]:setParent(c2) + end + for i = 1, #ch2 do + ch2[i]:setParent(c1) + end + end + + return c +end + +gui.Events.OnResized(proc:newFunction(function(w, h) + for i = 1, #canvas do + local c = canvas[i] + if gui.aspect_ratio then + local nw, nh, xt, yt = gui.GetSizeAdjustedToAspectRatio(w, h) + c.x = xt + c.y = yt + c.dualDim.offset.size.x = nw + c.dualDim.offset.size.y = nh + c.w = nw + c.h = nh + else + c.dualDim.offset.size.x = w + c.dualDim.offset.size.y = h + c.w = w + c.h = h + end + end +end)) + +return canvas \ No newline at end of file diff --git a/core/init.lua b/core/init.lua deleted file mode 100644 index e69de29..0000000 diff --git a/init.lua b/init.lua index 368a27f..133325d 100644 --- a/init.lua +++ b/init.lua @@ -433,6 +433,11 @@ function gui:isActive() return self.active and not (self:isDescendantOf(gui.virtual)) end +function gui:isOnScreen() + + return +end + -- Base get uniques function gui:getUniques(tab) local base = { @@ -742,10 +747,8 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh) self.OnFontUpdated:Fire(self) end - function c:fitFont(n, max) - local max = max or math.huge + function c:fitFont(min_size, max_size) local font - local isdefault = false if self.fontFile then if self.fontFile:match("ttf") then font = function(n) @@ -757,27 +760,67 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh) end end else - isdefault = true font = function(n) return love.graphics.setNewFont(n) end end - local x, y, width, height = self:getAbsolutes() - local Font, text = self.Font, self.text - local s = 3 - Font = font(s) - while height < max and Font:getHeight() < height and Font:getWidth(text) < width do - s = s + 1 - Font = font(s) + local text = self.text + local x, y, max_width, max_height = self:getAbsolutes() + local min_size = min_size or 1 + local max_size = max_size or 100 -- You can adjust the maximum font size as needed + local tolerance = 0.1 + local f + while max_size - min_size > tolerance do + local size = (min_size + max_size) / 2 + + f = font(size) + local text_width = f:getWidth(text) + local text_height = f:getHeight() + + if text_width > max_width or text_height > max_height then + max_size = size + else + min_size = size + end end - Font = font(s - (4 + (n or 0))) - Font:setFilter("linear", "nearest", 4) - self.font = Font - self.textOffsetY = 0 - local top, bottom = self:calculateFontOffset(Font, 0) - self.textOffsetY = floor(((height - bottom) - top) / 2) - self.OnFontUpdated:Fire(self) - return s - (4 + (n or 0)) + self:setFont(f) + return min_size end + -- function c:fitFont(n, max) + -- local max = max or math.huge + -- local font + -- local isdefault = false + -- if self.fontFile then + -- if self.fontFile:match("ttf") then + -- font = function(n) + -- return love.graphics.newFont(self.fontFile, n, "normal") + -- end + -- else + -- font = function(n) + -- return love.graphics.newFont(self.fontFile, n) + -- end + -- end + -- else + -- isdefault = true + -- font = function(n) return love.graphics.setNewFont(n) end + -- end + -- local x, y, width, height = self:getAbsolutes() + -- local Font, text = self.Font, self.text + -- local s = 3 + -- Font = font(s) + -- while height < max and Font:getHeight() < height and Font:getWidth(text) < width do + -- s = s + 1 + -- Font = font(s) + -- end + -- Font = font(s - (4 + (n or 0))) + -- Font:setFilter("linear", "nearest", 4) + -- self.font = Font + -- self.textOffsetY = 0 + -- local top, bottom = self:calculateFontOffset(Font, 0) + -- self.textOffsetY = floor(((height - bottom) - top) / 2) + -- self.OnFontUpdated:Fire(self) + -- return s - (4 + (n or 0)) + -- end + function c:centerFont() local x, y, width, height = self:getAbsolutes() local top, bottom = self:calculateFontOffset(self.font, 0) @@ -1406,6 +1449,8 @@ local draw_handler = function(child, no_draw) end end +gui.draw_handler = draw_handler + drawer:newLoop(function() local children = gui:getAllChildren() for i = 1, #children do @@ -1443,6 +1488,7 @@ local processors = { gui.draw = drawer.run gui.update = function() for i = 1, #processors do + print(i) processors[i]() end end @@ -1500,6 +1546,8 @@ local function GetSizeAdjustedToAspectRatio(dWidth, dHeight) return newWidth, newHeight, (dWidth-newWidth)/2, (dHeight-newHeight)/2 end +gui.GetSizeAdjustedToAspectRatio = GetSizeAdjustedToAspectRatio + function gui:setAspectSize(w, h) if w and h then g_width, g_height = w, h