Working on gui canvas
This commit is contained in:
parent
869db0c347
commit
65d7009548
98
core/canvas.lua
Normal file
98
core/canvas.lua
Normal file
@ -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
|
||||||
86
init.lua
86
init.lua
@ -433,6 +433,11 @@ function gui:isActive()
|
|||||||
return self.active and not (self:isDescendantOf(gui.virtual))
|
return self.active and not (self:isDescendantOf(gui.virtual))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function gui:isOnScreen()
|
||||||
|
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
-- Base get uniques
|
-- Base get uniques
|
||||||
function gui:getUniques(tab)
|
function gui:getUniques(tab)
|
||||||
local base = {
|
local base = {
|
||||||
@ -742,10 +747,8 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh)
|
|||||||
self.OnFontUpdated:Fire(self)
|
self.OnFontUpdated:Fire(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function c:fitFont(n, max)
|
function c:fitFont(min_size, max_size)
|
||||||
local max = max or math.huge
|
|
||||||
local font
|
local font
|
||||||
local isdefault = false
|
|
||||||
if self.fontFile then
|
if self.fontFile then
|
||||||
if self.fontFile:match("ttf") then
|
if self.fontFile:match("ttf") then
|
||||||
font = function(n)
|
font = function(n)
|
||||||
@ -757,27 +760,67 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
isdefault = true
|
|
||||||
font = function(n) return love.graphics.setNewFont(n) end
|
font = function(n) return love.graphics.setNewFont(n) end
|
||||||
end
|
end
|
||||||
local x, y, width, height = self:getAbsolutes()
|
local text = self.text
|
||||||
local Font, text = self.Font, self.text
|
local x, y, max_width, max_height = self:getAbsolutes()
|
||||||
local s = 3
|
local min_size = min_size or 1
|
||||||
Font = font(s)
|
local max_size = max_size or 100 -- You can adjust the maximum font size as needed
|
||||||
while height < max and Font:getHeight() < height and Font:getWidth(text) < width do
|
local tolerance = 0.1
|
||||||
s = s + 1
|
local f
|
||||||
Font = font(s)
|
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
|
end
|
||||||
Font = font(s - (4 + (n or 0)))
|
self:setFont(f)
|
||||||
Font:setFilter("linear", "nearest", 4)
|
return min_size
|
||||||
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
|
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()
|
function c:centerFont()
|
||||||
local x, y, width, height = self:getAbsolutes()
|
local x, y, width, height = self:getAbsolutes()
|
||||||
local top, bottom = self:calculateFontOffset(self.font, 0)
|
local top, bottom = self:calculateFontOffset(self.font, 0)
|
||||||
@ -1406,6 +1449,8 @@ local draw_handler = function(child, no_draw)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
gui.draw_handler = draw_handler
|
||||||
|
|
||||||
drawer:newLoop(function()
|
drawer:newLoop(function()
|
||||||
local children = gui:getAllChildren()
|
local children = gui:getAllChildren()
|
||||||
for i = 1, #children do
|
for i = 1, #children do
|
||||||
@ -1443,6 +1488,7 @@ local processors = {
|
|||||||
gui.draw = drawer.run
|
gui.draw = drawer.run
|
||||||
gui.update = function()
|
gui.update = function()
|
||||||
for i = 1, #processors do
|
for i = 1, #processors do
|
||||||
|
print(i)
|
||||||
processors[i]()
|
processors[i]()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -1500,6 +1546,8 @@ local function GetSizeAdjustedToAspectRatio(dWidth, dHeight)
|
|||||||
return newWidth, newHeight, (dWidth-newWidth)/2, (dHeight-newHeight)/2
|
return newWidth, newHeight, (dWidth-newWidth)/2, (dHeight-newHeight)/2
|
||||||
end
|
end
|
||||||
|
|
||||||
|
gui.GetSizeAdjustedToAspectRatio = GetSizeAdjustedToAspectRatio
|
||||||
|
|
||||||
function gui:setAspectSize(w, h)
|
function gui:setAspectSize(w, h)
|
||||||
if w and h then
|
if w and h then
|
||||||
g_width, g_height = w, h
|
g_width, g_height = w, h
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user