Skip to content

Commit

Permalink
feat: start std.ui.slide
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Nov 7, 2024
1 parent 9c797de commit f143ad5
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/cli/commands/build.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/lib/cli/assets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/lib/engine/api/i18n.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down
2 changes: 2 additions & 0 deletions src/lib/engine/draw/ui.lua
Original file line number Diff line number Diff line change
@@ -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

Expand Down
48 changes: 2 additions & 46 deletions src/lib/engine/draw/ui/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -100,7 +57,6 @@ end

local P = {
add=add,
apply=apply,
style=style,
get_item=get_item,
add_items=add_items,
Expand Down
51 changes: 45 additions & 6 deletions src/lib/engine/draw/ui/grid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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
}

Expand All @@ -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
Expand Down
125 changes: 125 additions & 0 deletions src/lib/engine/draw/ui/slide.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f143ad5

Please sign in to comment.