diff --git a/druid/bindings.lua b/druid/bindings.lua deleted file mode 100644 index e63a11e..0000000 --- a/druid/bindings.lua +++ /dev/null @@ -1,48 +0,0 @@ -local event = require("event.event") - -local M = {} -local WRAPPED_WIDGETS = {} - ----Set a widget to the current game object. The game object can acquire the widget by calling `bindings.get_widget` ----It wraps with events only top level functions cross-context, so no access to nested widgets functions ----@param widget druid.widget -function M.set_widget(widget) - local object = msg.url() - object.fragment = nil - - -- Make a copy of the widget with all functions wrapped in events - -- It makes available to call gui functions from game objects - local wrapped_widget = setmetatable({}, { __index = widget }) - local parent_table = getmetatable(widget).__index - - -- Go through all functions and wrap them in events - for key, value in pairs(parent_table) do - if type(value) == "function" then - wrapped_widget[key] = event.create(function(_, ...) - return value(widget, ...) - end) - end - end - - WRAPPED_WIDGETS[object.socket] = WRAPPED_WIDGETS[object.socket] or {} - WRAPPED_WIDGETS[object.socket][object.path] = wrapped_widget -end - - ----@param object_url string|userdata|url @root object ----@return druid.widget|nil -function M.get_widget(object_url) - assert(object_url, "You must provide an object_url") - - object_url = msg.url(object_url --[[@as string]]) - - local socket_widgets = WRAPPED_WIDGETS[object_url.socket] - if not socket_widgets then - return nil - end - - return socket_widgets[object_url.path] -end - - -return M diff --git a/druid/color.lua b/druid/color.lua index 11489ea..0f7bdcd 100644 --- a/druid/color.lua +++ b/druid/color.lua @@ -86,32 +86,20 @@ function M.lerp(t, color1, color2) return vmath.vector4(r, g, b, a) end - ---@param hex string ----@param alpha number|nil ----@return number, number, number, number -function M.hex2rgb(hex, alpha) - alpha = alpha or 1 - if alpha > 1 then - alpha = alpha / 100 +---@return number, number, number +function M.hex2rgb(hex) + if not hex or #hex < 3 then + return 0, 0, 0 end - -- Remove leading # - if string.sub(hex, 1, 1) == "#" then - hex = string.sub(hex, 2) - end - - -- Expand 3-digit hex codes to 6 digits + hex = hex:gsub("^#", "") if #hex == 3 then - hex = string.rep(string.sub(hex, 1, 1), 2) .. - string.rep(string.sub(hex, 2, 2), 2) .. - string.rep(string.sub(hex, 3, 3), 2) + hex = hex:gsub("(.)", "%1%1") end - - local r = tonumber("0x" .. string.sub(hex, 1, 2)) / 255 - local g = tonumber("0x" .. string.sub(hex, 3, 4)) / 255 - local b = tonumber("0x" .. string.sub(hex, 5, 6)) / 255 - return r, g, b, alpha + return tonumber("0x" .. hex:sub(1,2)) / 255, + tonumber("0x" .. hex:sub(3,4)) / 255, + tonumber("0x" .. hex:sub(5,6)) / 255 end @@ -119,8 +107,8 @@ end ---@param alpha number|nil ---@return vector4 function M.hex2vector4(hex, alpha) - local r, g, b, a = M.hex2rgb(hex, alpha) - return vmath.vector4(r, g, b, a) + local r, g, b = M.hex2rgb(hex) + return vmath.vector4(r, g, b, alpha or 1) end @@ -226,4 +214,4 @@ end M.load_palette() -return M \ No newline at end of file +return M diff --git a/druid/druid.lua b/druid/druid.lua index ce3f034..dadb2f7 100644 --- a/druid/druid.lua +++ b/druid/druid.lua @@ -1,3 +1,4 @@ +local event = require("event.event") local events = require("event.events") local settings = require("druid.system.settings") local druid_instance = require("druid.system.druid_instance") @@ -83,4 +84,49 @@ function M.on_language_change() end +local WRAPPED_WIDGETS = {} + +---Set a widget to the current game object. The game object can acquire the widget by calling `bindings.get_widget` +---It wraps with events only top level functions cross-context, so no access to nested widgets functions +---@param widget druid.widget +function M.set_widget(widget) + local object = msg.url() + object.fragment = nil + + -- Make a copy of the widget with all functions wrapped in events + -- It makes available to call gui functions from game objects + local wrapped_widget = setmetatable({}, { __index = widget }) + local parent_table = getmetatable(widget).__index + + -- Go through all functions and wrap them in events + for key, value in pairs(parent_table) do + if type(value) == "function" then + wrapped_widget[key] = event.create(function(_, ...) + return value(widget, ...) + end) + end + end + + WRAPPED_WIDGETS[object.socket] = WRAPPED_WIDGETS[object.socket] or {} + WRAPPED_WIDGETS[object.socket][object.path] = wrapped_widget +end + + +---@param object_url string|userdata|url|nil Root object, if nil current object will be used +---@return druid.widget|nil +function M.get_widget(object_url) + object_url = object_url or msg.url() + if object_url then + object_url = msg.url(object_url --[[@as string]]) + end + + local socket_widgets = WRAPPED_WIDGETS[object_url.socket] + if not socket_widgets then + return nil + end + + return socket_widgets[object_url.path] +end + + return M diff --git a/druid/widget/fps_panel/fps_panel.lua b/druid/widget/fps_panel/fps_panel.lua index c86c66b..acd067c 100644 --- a/druid/widget/fps_panel/fps_panel.lua +++ b/druid/widget/fps_panel/fps_panel.lua @@ -42,6 +42,9 @@ function M:init() self.container = self.druid:new_container(self.root) self.container:add_container(self.mini_graph.container) + local container_content = self.container:add_container("content") + container_content:add_container("text_min_fps") + container_content:add_container("text_fps") end @@ -98,4 +101,4 @@ function M:push_fps_value() end -return M \ No newline at end of file +return M diff --git a/druid/widget/memory_panel/memory_panel.gui b/druid/widget/memory_panel/memory_panel.gui index 80b3b4a..1892972 100644 --- a/druid/widget/memory_panel/memory_panel.gui +++ b/druid/widget/memory_panel/memory_panel.gui @@ -117,6 +117,7 @@ nodes { y: 1.0 z: 1.0 } + adjust_mode: ADJUST_MODE_STRETCH parent: "content" inherit_alpha: true outline_alpha: 0.0 @@ -155,6 +156,7 @@ nodes { y: 1.0 z: 1.0 } + adjust_mode: ADJUST_MODE_STRETCH parent: "content" inherit_alpha: true outline_alpha: 0.0 diff --git a/druid/widget/memory_panel/memory_panel.lua b/druid/widget/memory_panel/memory_panel.lua index 3a72ae0..cbe2915 100644 --- a/druid/widget/memory_panel/memory_panel.lua +++ b/druid/widget/memory_panel/memory_panel.lua @@ -40,6 +40,9 @@ function M:init() self.container = self.druid:new_container(self.root) self.container:add_container(self.mini_graph.container) + local container_content = self.container:add_container("content") + container_content:add_container("text_max_value") + container_content:add_container("text_per_second") end @@ -93,4 +96,4 @@ function M:update_text_memory() end -return M \ No newline at end of file +return M diff --git a/druid/widget/mini_graph/mini_graph.gui b/druid/widget/mini_graph/mini_graph.gui index 11ad730..ed5fa6d 100644 --- a/druid/widget/mini_graph/mini_graph.gui +++ b/druid/widget/mini_graph/mini_graph.gui @@ -23,6 +23,7 @@ nodes { type: TYPE_BOX texture: "druid/ui_circle_16" id: "root" + adjust_mode: ADJUST_MODE_STRETCH inherit_alpha: true slice9 { x: 8.0 @@ -120,6 +121,7 @@ nodes { texture: "druid/ui_circle_16" id: "content" pivot: PIVOT_S + adjust_mode: ADJUST_MODE_STRETCH parent: "root" inherit_alpha: true slice9 { diff --git a/druid/widget/mini_graph/mini_graph.lua b/druid/widget/mini_graph/mini_graph.lua index 3cebd5b..d237dc8 100644 --- a/druid/widget/mini_graph/mini_graph.lua +++ b/druid/widget/mini_graph/mini_graph.lua @@ -11,6 +11,7 @@ function M:init() self.root = self:get_node("root") self.text_header = self.druid:new_text("text_header") + self.icon_drag = self:get_node("icon_drag") self.druid:new_drag("header", self.on_drag_widget) self.druid:new_button("icon_drag", self.toggle_hide) :set_style(nil) @@ -34,7 +35,11 @@ function M:init() self.values = {} self.container = self.druid:new_container(self.root) - self.container:add_container("header") + local container_header = self.container:add_container("header", "stretch_x") + container_header:add_container("text_header") + container_header:add_container("icon_drag") + + self.container:add_container("content", "stretch_x") self.default_size = self.container:get_size() end @@ -141,6 +146,10 @@ end function M:on_drag_widget(dx, dy) + if not gui.is_enabled(self.icon_drag) then + return + end + local position = self.container:get_position() self.container:set_position(position.x + dx, position.y + dy) end @@ -159,4 +168,4 @@ function M:toggle_hide() end -return M \ No newline at end of file +return M diff --git a/druid/widget/properties_panel/properties_panel.gui b/druid/widget/properties_panel/properties_panel.gui index 7ba5a5c..43dbf4c 100644 --- a/druid/widget/properties_panel/properties_panel.gui +++ b/druid/widget/properties_panel/properties_panel.gui @@ -13,7 +13,7 @@ textures { nodes { size { x: 400.0 - y: 240.0 + y: 400.0 } color { x: 0.173 @@ -23,6 +23,7 @@ nodes { type: TYPE_BOX texture: "druid/ui_circle_16" id: "root" + pivot: PIVOT_N inherit_alpha: true slice9 { x: 8.0 @@ -32,9 +33,6 @@ nodes { } } nodes { - position { - y: 120.0 - } size { x: 400.0 y: 40.0 @@ -105,15 +103,15 @@ nodes { } nodes { position { - y: -120.0 + y: -50.0 } size { x: 400.0 - y: 190.0 + y: 350.0 } type: TYPE_BOX id: "content" - pivot: PIVOT_S + pivot: PIVOT_N parent: "root" inherit_alpha: true size_mode: SIZE_MODE_AUTO @@ -122,11 +120,10 @@ nodes { nodes { position { x: -200.0 - y: 190.0 } size { x: 400.0 - y: 190.0 + y: 350.0 } type: TYPE_BOX texture: "druid/empty" @@ -141,7 +138,7 @@ nodes { nodes { size { x: 400.0 - y: 190.0 + y: 350.0 } type: TYPE_BOX texture: "druid/pixel" @@ -159,7 +156,7 @@ nodes { } nodes { position { - y: 170.0 + y: -10.0 } type: TYPE_BOX texture: "druid/empty" diff --git a/druid/widget/properties_panel/properties_panel.lua b/druid/widget/properties_panel/properties_panel.lua index cae5d3f..7b80451 100644 --- a/druid/widget/properties_panel/properties_panel.lua +++ b/druid/widget/properties_panel/properties_panel.lua @@ -14,6 +14,7 @@ local property_vector3 = require("druid.widget.properties_panel.properties.prope ---@field container_content druid.container ---@field container_scroll_view druid.container ---@field contaienr_scroll_content druid.container +---@field button_hidden druid.button ---@field text_header druid.text ---@field paginator widget.property_left_right_selector ---@field properties druid.widget[] List of created properties @@ -47,8 +48,9 @@ function M:init() self.layout.on_size_changed:subscribe(self.on_size_changed, self) self.druid:new_drag("header", self.on_drag_widget) - self.druid:new_button("icon_drag", self.toggle_hide) - :set_style(nil) + self.button_hidden = self.druid:new_button("icon_drag", function() + self:set_hidden(not self._is_hidden) + end):set_style(nil) self.property_checkbox_prefab = self:get_node("property_checkbox/root") gui.set_enabled(self.property_checkbox_prefab, false) @@ -130,7 +132,7 @@ function M:on_size_changed(new_size) self.container_content:set_size(new_size.x, new_size.y, gui.PIVOT_N) self.default_size = vmath.vector3(new_size.x, new_size.y + 50, 0) - if not self.is_hidden then + if not self._is_hidden then self.container:set_size(self.default_size.x, self.default_size.y, gui.PIVOT_N) end @@ -292,15 +294,19 @@ function M:remove(widget) end -function M:toggle_hide() - self.is_hidden = not self.is_hidden +function M:set_hidden(is_hidden) + self._is_hidden = is_hidden local hidden_size = gui.get_size(self:get_node("header")) - local new_size = self.is_hidden and hidden_size or self.default_size + local new_size = self._is_hidden and hidden_size or self.default_size self.container:set_size(new_size.x, new_size.y, gui.PIVOT_N) - gui.set_enabled(self.content, not self.is_hidden) - return self + gui.set_enabled(self.content, not self._is_hidden) +end + + +function M:is_hidden() + return self._is_hidden end