From 5309f35dfc0e6f1ee9d29a387ca39dee94fe94c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elm=C4=81rs=20=C4=80boli=C5=86=C5=A1?= Date: Tue, 15 Jun 2021 03:36:09 +0300 Subject: [PATCH] implemented the rest of the state callbacks redid layout luadoc + emmylua annotations everywhere grid layout started --- core/atlas.lua | 11 +- core/element.lua | 62 +++++-- core/input.lua | 18 +- core/scene.lua | 17 +- hooks/callback.lua | 17 +- hooks/context.lua | 7 + hooks/onDestroy.lua | 14 ++ hooks/onLayout.lua | 0 hooks/onLoad.lua | 14 ++ hooks/onPosChange.lua | 16 ++ hooks/onSizeChange.lua | 16 ++ hooks/onUpdate.lua | 15 ++ hooks/{position.lua => setPos.lua} | 0 hooks/{size.lua => setSize.lua} | 0 hooks/state.lua | 4 + layout/column.lua | 19 +- layout/grid.lua | 277 +++++++++++++++++++++++++++++ layout/init.lua | 57 +++--- layout/row.lua | 20 ++- shell/button.lua | 14 ++ shell/checkbox.lua | 16 ++ shell/input.lua | 16 ++ 22 files changed, 578 insertions(+), 52 deletions(-) delete mode 100644 hooks/onLayout.lua rename hooks/{position.lua => setPos.lua} (100%) rename hooks/{size.lua => setSize.lua} (100%) create mode 100644 layout/grid.lua diff --git a/core/atlas.lua b/core/atlas.lua index bba977c..782ee15 100644 --- a/core/atlas.lua +++ b/core/atlas.lua @@ -80,8 +80,17 @@ function atlases:unassignAll() self.atlases[2].taken_area = 0 end -function atlases.onscreenchange(newW, newH) +function atlases:onscreenchange(newW, newH) + for i, e in ipairs(self.atlases[1].users) do + e:reassignCanvas() + end + for i, e in ipairs(self.atlases[2].users) do + e:reassignCanvas() + end + + self.atlases[1] = atlas.new(newW, newH) + self.atlases[2] = atlas.new(newW, newH) end function atlas.new(w, h) diff --git a/core/element.lua b/core/element.lua index e2e27a4..6d29934 100644 --- a/core/element.lua +++ b/core/element.lua @@ -11,9 +11,8 @@ element.__index = element local type,pcall = type,pcall setmetatable(element, { - __call = function(cls, ...) + __call = function(cls, func, param, w, h, flags) local self - local func, param, w, h = ... --Make the object inherit class self = setmetatable({}, element) @@ -22,6 +21,7 @@ setmetatable(element, { self:new(param,nil, w, h) self:createProxies() + ---@type Element return self end }) @@ -32,6 +32,9 @@ function element:new(param, immediate, w, h) self.parameters = {} self.baseParams = param + --Internal state callbacks + self.callbacks = {} + --Internal settings self.settings = { isSetup = false, @@ -73,6 +76,18 @@ function element:new(param, immediate, w, h) self.view = self.baseView end +function element:reassignCanvas() + self.settings.failedCanvas = false + self.settings.hasCanvas = false + + self.canvas = nil + self.quad = nil + self.interQuad = nil + self.deferResize = nil + + self.context:bubbleUpdate() +end + function element:sizeChange(i, v) local increase = self.baseView[i] < v @@ -90,6 +105,12 @@ function element:sizeChange(i, v) else self.deferResize = { increased = increase } end + + if self.callbacks.onSizeChange then + for i, cb in ipairs(self.callbacks.onSizeChange) do + cb(self.view.w, self.view.h) + end + end end function element:posChange(i, v) @@ -99,22 +120,37 @@ function element:posChange(i, v) if not self.deferRepos then self.deferRepos = true end + + + if self.callbacks.onPosChange then + for i, cb in ipairs(self.callbacks.onPosChange) do + cb(self.view.x, self.view.y) + end + end end function element:onUpdate() - -end - -function element:onDraw() - + if self.callbacks.onUpdate then + for i, cb in ipairs(self.callbacks.onUpdate) do + cb() + end + end end function element:onLoad() - + if self.callbacks.onLoad then + for i, cb in ipairs(self.callbacks.onLoad) do + cb() + end + end end function element:onDestroy() - + if self.callbacks.onDestroy then + for i, cb in ipairs(self.callbacks.onDestroy) do + cb() + end + end end function element:createProxies() @@ -204,6 +240,7 @@ function element:setup() self.context:unset() self.settings.isSetup = true + self:onLoad() end local setColor, rectangle, setFont, printf = love.graphics.setColor, love.graphics.rectangle, love.graphics.setFont, love.graphics.printf @@ -294,6 +331,7 @@ function element:externalUpdate() end if self.settings.pendingUpdate then + self:onUpdate() self.settings.needsRendering = true self.settings.pendingUpdate = false end @@ -321,8 +359,8 @@ end local insert = table.insert ---External functions ---Acts as the entrypoint for beginning rendering +---External functions +---Acts as the entrypoint for beginning rendering ---@param x number ---@param y number function element:draw(x, y, w, h) @@ -355,10 +393,12 @@ function element:getSize() return self.view.w, self.view.h end +---Destroys this element function element:destroy() self.settings.remove = true self.settings.firstDraw = true self.settings.isSetup = false + self:onDestroy() self.context:destroy() end diff --git a/core/input.lua b/core/input.lua index d28a4fe..8c8ae82 100644 --- a/core/input.lua +++ b/core/input.lua @@ -174,14 +174,18 @@ function subscription:checkOutside(x, y) return not (x>self.x and xself.y and y