Widgets WIP

This commit is contained in:
Insality
2024-11-18 22:50:42 +02:00
parent 99f75dd626
commit 299f8501e8
15 changed files with 181 additions and 265 deletions

View File

@@ -134,8 +134,9 @@ end
--- Set new size of layout node
---@param width number|nil
---@param height number|nil
---@param anchor_pivot constant|nil If set will keep the corner possition relative to the new size
---@return druid.container Container
function M:set_size(width, height)
function M:set_size(width, height, anchor_pivot)
width = width or self.size.x
height = height or self.size.y
@@ -149,11 +150,23 @@ function M:set_size(width, height)
if (width and width ~= self.size.x) or (height and height ~= self.size.y) then
self.center_offset.x = -width * self.pivot_offset.x
self.center_offset.y = -height * self.pivot_offset.y
local dx = self.size.x - width
local dy = self.size.y - height
self.size.x = width
self.size.y = height
self.size.z = 0
gui.set_size(self.node, self.size)
if anchor_pivot then
local pivot = gui.get_pivot(self.node)
local pivot_offset = helper.get_pivot_offset(pivot)
local new_pivot_offset = helper.get_pivot_offset(anchor_pivot)
local position_dx = dx * (pivot_offset.x - new_pivot_offset.x)
local position_dy = dy * (pivot_offset.y - new_pivot_offset.y)
self:set_position(self._position.x + position_dx, self._position.y - position_dy)
end
self:update_child_containers()
self.on_size_changed:trigger(self:get_context(), self.size)
end
@@ -162,6 +175,11 @@ function M:set_size(width, height)
end
function M:get_position()
return self._position
end
---@param pos_x number
---@param pos_y number
function M:set_position(pos_x, pos_y)

View File

@@ -1,8 +1,12 @@
local event = require("druid.event")
local helper = require("druid.helper")
local component = require("druid.component")
---@alias druid.layout.mode "horizontal"|"vertical"|"horizontal_wrap"
---@class druid.event.on_size_changed: druid.event
---@field subscribe fun(_, callback: fun(new_size: vector3), context: any|nil)
---@class druid.layout.row_data
---@field width number
---@field height number
@@ -25,6 +29,7 @@ local component = require("druid.component")
---@field is_resize_width boolean
---@field is_resize_height boolean
---@field is_justify boolean
---@field on_size_changed druid.event.on_size_changed
local M = component.create("layout")
---Layout component constructor
@@ -46,6 +51,8 @@ function M:init(node_or_node_id, layout_type)
self.is_resize_width = false
self.is_resize_height = false
self.is_justify = false
self.on_size_changed = event.create()
end
@@ -145,6 +152,21 @@ function M:add(node_or_node_id)
end
function M:remove(node_or_node_id)
local node = type(node_or_node_id) == "table" and node_or_node_id.node or self:get_node(node_or_node_id)
for index = #self.entities, 1, -1 do
if self.entities[index] == node then
table.remove(self.entities, index)
self.is_dirty = true
break
end
end
return self
end
---@return druid.layout
function M:refresh_layout()
local layout_node = self.node
@@ -273,6 +295,8 @@ function M:refresh_layout()
size.y = rows_data.total_height + padding.y + padding.w
end
gui.set_size(layout_node, size)
self.on_size_changed(size)
end
self.is_dirty = false