Skip to content

Commit

Permalink
Widgets WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Nov 18, 2024
1 parent 99f75dd commit 299f850
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 265 deletions.
3 changes: 3 additions & 0 deletions druid/druid.atlas
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ images {
images {
image: "/druid/images/pixel.png"
}
images {
image: "/druid/images/panels/rect_round2_width2.png"
}
extrude_borders: 2
9 changes: 9 additions & 0 deletions druid/druid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ function M.set_sound_function(callback)
end


---Subscribe Druid to the window listener. It will override your previous
---window listener, so if you have one, you should call M.on_window_callback manually.
function M.init_window_listener()
window.set_listener(function(_, window_event)
M.on_window_callback(window_event)
end)
end


---Set the window callback to enable Druid window events.
---@param event constant Event param from window listener
function M.on_window_callback(event)
Expand Down
8 changes: 4 additions & 4 deletions druid/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local tinsert = table.insert
local tremove = table.remove

--- Return new event instance
---@param callback fun()|nil Subscribe the callback on new event, if callback exist
---@param callback function|nil Subscribe the callback on new event, if callback exist
---@param callback_context any|nil Additional context as first param to callback call
---@return druid.event
---@nodiscard
Expand All @@ -30,7 +30,7 @@ end


--- Check is event subscribed.
---@param callback fun() Callback itself
---@param callback function Callback itself
---@param callback_context any|nil Additional context as first param to callback call
---@return boolean, number|nil Is event subscribed, return index of callback in event as second param
function M:is_subscribed(callback, callback_context)
Expand All @@ -50,7 +50,7 @@ end


---Subscribe callback on event
---@param callback fun() Callback itself
---@param callback function Callback itself
---@param callback_context any|nil Additional context as first param to callback call, usually it's self
---@return boolean
function M:subscribe(callback, callback_context)
Expand All @@ -67,7 +67,7 @@ end


---Unsubscribe callback on event
---@param callback fun() Callback itself
---@param callback function Callback itself
---@param callback_context any|nil Additional context as first param to callback call
---@return boolean
function M:unsubscribe(callback, callback_context)
Expand Down
20 changes: 19 additions & 1 deletion druid/extended/container.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions druid/extended/layout.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Binary file added druid/images/panels/rect_round2_width2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion druid/styles/default/style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ M["hover"] = {
}

M["drag"] = {
DRAG_DEADZONE = 10, -- Size in pixels of drag deadzone
DRAG_DEADZONE = 4, -- Size in pixels of drag deadzone
NO_USE_SCREEN_KOEF = false,
}

Expand Down
2 changes: 1 addition & 1 deletion druid/system/druid_instance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ end
local layout = require("druid.extended.layout")
---Create Layout component
---@param node string|node The_node id or gui.get_node(node_id).
---@param mode string The layout mode
---@param mode string vertical|horizontal|horizontal_wrap
---@return druid.layout Layout component
function M:new_layout(node, mode)
return self:new(layout, node, mode)
Expand Down
16 changes: 0 additions & 16 deletions druid/widget/debug_panel/debug_panel.gui

This file was deleted.

Loading

0 comments on commit 299f850

Please sign in to comment.