From f143ad50481942f716ca6f000ea1c36b6bf73d1a Mon Sep 17 00:00:00 2001 From: RodrigoDornelles Date: Thu, 7 Nov 2024 15:13:59 -0300 Subject: [PATCH] feat: start std.ui.slide --- src/cli/commands/build.lua | 2 +- src/lib/cli/assets.lua | 1 - src/lib/engine/api/i18n.lua | 4 +- src/lib/engine/draw/ui.lua | 2 + src/lib/engine/draw/ui/common.lua | 48 +----------- src/lib/engine/draw/ui/grid.lua | 51 ++++++++++-- src/lib/engine/draw/ui/slide.lua | 125 ++++++++++++++++++++++++++++++ 7 files changed, 177 insertions(+), 56 deletions(-) create mode 100644 src/lib/engine/draw/ui/slide.lua diff --git a/src/cli/commands/build.lua b/src/cli/commands/build.lua index bcbf286..065403c 100644 --- a/src/cli/commands/build.lua +++ b/src/cli/commands/build.lua @@ -159,7 +159,7 @@ local function build(args) if args.game then local game = zeebo_module.loadgame(dist..'game.lua') - zeebo_assets.build(game.assets, dist) + zeebo_assets.build(game and game.assets or {}, dist) end if args.run then diff --git a/src/lib/cli/assets.lua b/src/lib/cli/assets.lua index c2b6257..b77e57b 100644 --- a/src/lib/cli/assets.lua +++ b/src/lib/cli/assets.lua @@ -8,7 +8,6 @@ local function build(assets, dist) local separator = asset:find(':') local from = util_fs.file(separator and asset:sub(1, separator -1) or asset).get_fullfilepath() local to = util_fs.file(separator and asset:sub(separator + 1) or asset).get_fullfilepath():gsub('^./', '') - print(from, dist..to) zeebo_fs.move(from, dist..to) index = index + 1 end diff --git a/src/lib/engine/api/i18n.lua b/src/lib/engine/api/i18n.lua index 538fdaa..4567933 100644 --- a/src/lib/engine/api/i18n.lua +++ b/src/lib/engine/api/i18n.lua @@ -171,8 +171,8 @@ local function install(std, engine, system_language) std.i18n.get_text = get_text std.i18n.get_language = get_language std.i18n.set_language = set_language - std.i18n.back_language = back_language - std.i18n.next_language = next_language + std.i18n.back = back_language + std.i18n.next = next_language return { std={ diff --git a/src/lib/engine/draw/ui.lua b/src/lib/engine/draw/ui.lua index d1db6e3..5f200e4 100644 --- a/src/lib/engine/draw/ui.lua +++ b/src/lib/engine/draw/ui.lua @@ -1,11 +1,13 @@ local math = require('math') local ui_grid = require('src/lib/engine/draw/ui/grid') +local ui_slide = require('src/lib/engine/draw/ui/slide') local ui_style = require('src/lib/engine/draw/ui/style') local util_decorator = require('src/lib/util/decorator') local function install(std, engine, application) std.ui = std.ui or {} std.ui.grid = util_decorator.prefix2(std, engine, ui_grid.component) + std.ui.slide = util_decorator.prefix2(std, engine, ui_slide.component) std.ui.style = util_decorator.prefix2(std, engine, ui_style.component) end diff --git a/src/lib/engine/draw/ui/common.lua b/src/lib/engine/draw/ui/common.lua index 2878d3a..d86d3f7 100644 --- a/src/lib/engine/draw/ui/common.lua +++ b/src/lib/engine/draw/ui/common.lua @@ -47,51 +47,8 @@ local function get_item(self, id) end --! @renamefunc style -local function style(self, classlist) - self.classlist = classlist - return self -end - ---! @hideparam std ---! @hideparam engine -local function apply(std, engine, self) - local index = 1 - local x, y = 0, 0 - - local index2 = 1 - local pipeline = std.ui.style(self.classlist).pipeline - - while index2 <= #pipeline do - pipeline[index2](std, self.node, self.node.config.parent, engine.root) - index2 = index2 + 1 - end - - local hem = self.node.data.width / self.rows - local vem = self.node.data.height / self.cols - - while index <= #self.items_node do - local node = self.items_node[index] - local size = self.items_size[index] - local ui = self.items_ui[node] - - node.config.offset_x = x * hem - node.config.offset_y = y * vem - node.data.width = size * hem - node.data.height = vem - - x = x + size - if x >= self.rows then - y = y + 1 - x = 0 - end - - if ui then - ui:apply() - end - - index = index + 1 - end - +local function style(classkey, self, classlist) + self[classkey] = classlist return self end @@ -100,7 +57,6 @@ end local P = { add=add, - apply=apply, style=style, get_item=get_item, add_items=add_items, diff --git a/src/lib/engine/draw/ui/grid.lua b/src/lib/engine/draw/ui/grid.lua index 1ee8fcd..a6b1044 100644 --- a/src/lib/engine/draw/ui/grid.lua +++ b/src/lib/engine/draw/ui/grid.lua @@ -97,6 +97,49 @@ local util_decorator = require('src/lib/util/decorator') --! @} --! @} +--! @hideparam std +--! @hideparam engine +local function apply(std, engine, self) + local index = 1 + local x, y = 0, 0 + + local index2 = 1 + local pipeline = std.ui.style(self.classlist).pipeline + + while index2 <= #pipeline do + pipeline[index2](std, self.node, self.node.config.parent, engine.root) + index2 = index2 + 1 + end + + local hem = self.node.data.width / self.rows + local vem = self.node.data.height / self.cols + + while index <= #self.items_node do + local node = self.items_node[index] + local size = self.items_size[index] + local ui = self.items_ui[node] + + node.config.offset_x = x * hem + node.config.offset_y = y * vem + node.data.width = size * hem + node.data.height = vem + + x = x + size + if x >= self.rows then + y = y + 1 + x = 0 + end + + if ui then + ui:apply() + end + + index = index + 1 + end + + return self +end + local function component(std, engine, layout) local rows, cols = layout:match('(%d+)x(%d+)') local node = std.node.load({ @@ -115,7 +158,7 @@ local function component(std, engine, layout) add=util_decorator.prefix2(std, engine, ui_common.add), add_items=util_decorator.prefix2(std, engine, ui_common.add_items), style=ui_common.style, - apply=util_decorator.prefix2(std, engine, ui_common.apply), + apply=util_decorator.prefix2(std, engine, apply), get_item=ui_common.get_item } @@ -125,11 +168,7 @@ local function component(std, engine, layout) node.callbacks.resize = nil return end - if self.px_height then - node.data.height = self.px_height - else - node.data.height = engine.root.data.height - end + node.data.height = engine.root.data.height node.data.width = engine.root.data.width self:apply() end diff --git a/src/lib/engine/draw/ui/slide.lua b/src/lib/engine/draw/ui/slide.lua new file mode 100644 index 0000000..010850d --- /dev/null +++ b/src/lib/engine/draw/ui/slide.lua @@ -0,0 +1,125 @@ +local ui_common = require('src/lib/engine/draw/ui/common') +local util_decorator = require('src/lib/util/decorator') + +local function slider_next(self, to) + local incr = to or 1 + self.index = self.index + incr + if self.index == 0 then + self.index = #self.items_node + end + if self.index > #self.items_node then + self.index = 1 + end + return self +end + +local function slider_back(self) + return slider_next(self, -1) +end + +--! @hideparam std +--! @hideparam engine +local function apply(std, engine, self) + local index = 1 + local x, y = 0, 0 + + local index2 = 1 + local pipeline = std.ui.style(self.classlist).pipeline + + while index2 <= #pipeline do + pipeline[index2](std, self.node, self.node.config.parent, engine.root) + index2 = index2 + 1 + end + + local hem = self.node.data.width / self.rows + local vem = self.node.data.height / self.cols + + while index <= #self.items_node do + local node = self.items_node[index] + local size = self.items_size[index] + local ui = self.items_ui[node] + + node.config.offset_x = x * hem + node.config.offset_y = y * vem + node.data.width = size * hem + node.data.height = vem + + x = x + size + if x >= self.rows then + y = y + 1 + x = 0 + end + + if index == self.index then + local index3 = 1 + local pipeline2 = std.ui.style(self.classlist_selected).pipeline + while index3 <= #pipeline2 do + pipeline2[index3](std, node, node.config.parent, engine.root) + index3 = index3 + 1 + end + end + + if ui then + ui:apply() + end + + index = index + 1 + end + + return self +end + +local function component(std, engine, layout) + local rows, cols = layout:match('(%d+)x(%d+)') + + if rows ~= '1' and cols ~= '1' then + error('invalid grid layout') + end + + local node = std.node.load({ + width = engine.current.data.width, + height = engine.current.data.height + }) + + local self = { + index = 1, + rows=tonumber(rows), + cols=tonumber(cols), + items_node = {}, + items_size = {}, + items_ui = {}, + node=node, + classlist='', + classlist_selected='', + -- methods + next=slider_next, + back=slider_back, + add=util_decorator.prefix2(std, engine, ui_common.add), + add_items=util_decorator.prefix2(std, engine, ui_common.add_items), + style_item_select=util_decorator.prefix1('classlist_selected', ui_common.style), + style=util_decorator.prefix1('classlist', ui_common.style), + apply=util_decorator.prefix2(std, engine, apply), + get_item=ui_common.get_item + } + + if engine.root == engine.current then + node.callbacks.resize = function() + if node.config.parent ~= engine.root then + node.callbacks.resize = nil + return + end + node.data.height = engine.root.data.height + node.data.width = engine.root.data.width + self:apply() + end + end + + std.node.spawn(node) + return self +end + +local P = { + component = component +} + +return P