mirror of
https://github.com/Insality/druid.git
synced 2025-09-27 18:12:19 +02:00
Compare commits
23 Commits
properties
...
copilot/fi
Author | SHA1 | Date | |
---|---|---|---|
|
cc818e4c6e | ||
|
92db5319a7 | ||
|
b6a7df5ff2 | ||
|
a94121a1c6 | ||
|
097963bdb3 | ||
|
574f764559 | ||
|
379c9acfc3 | ||
|
4c8796e17d | ||
|
ba7ee40510 | ||
|
d019247ae4 | ||
|
5dac7ed8e2 | ||
|
b35b584fad | ||
|
616c513fbd | ||
|
3606c9f49f | ||
|
a8aea0adee | ||
|
8fde964e0e | ||
|
7b5264aca0 | ||
|
8fc5f4d144 | ||
|
61111536a8 | ||
|
b5d2f313cc | ||
|
d0067e5496 | ||
|
9a0f341a67 | ||
|
ce49c3bb41 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -17,3 +17,7 @@ manifest.private.der
|
||||
manifest.public.der
|
||||
/.editor_settings
|
||||
/.deployer_cache
|
||||
|
||||
# Example and documentation files (not part of core library)
|
||||
example_color_api_usage.lua
|
||||
DRUID_COLOR_API_IMPROVEMENTS.md
|
||||
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -33,7 +33,8 @@
|
||||
"Lua.runtime.version": "Lua 5.1",
|
||||
"Lua.workspace.library": [
|
||||
"~/Library/Application Support/Cursor/User/globalStorage/astronachos.defold",
|
||||
"~/Library/Application Support/Cursor/User/workspaceStorage/1446075a23c89451a63f0e82b2291def/astronachos.defold"
|
||||
"~/Library/Application Support/Cursor/User/workspaceStorage/1446075a23c89451a63f0e82b2291def/astronachos.defold",
|
||||
"~/Library/Application Support/Cursor/User/workspaceStorage/7975bec62a9fa9724d190779fa01ec63/astronachos.defold"
|
||||
],
|
||||
"files.exclude": {
|
||||
"**/*.gui": true
|
||||
|
@@ -39,13 +39,13 @@ Open your `game.project` file and add the following lines to the dependencies fi
|
||||
**[Druid](https://github.com/Insality/druid/)**
|
||||
|
||||
```
|
||||
https://github.com/Insality/druid/archive/refs/tags/1.1.3.zip
|
||||
https://github.com/Insality/druid/archive/refs/tags/1.1.5.zip
|
||||
```
|
||||
|
||||
**[Defold Event](https://github.com/Insality/defold-event)**
|
||||
|
||||
```
|
||||
https://github.com/Insality/defold-event/archive/refs/tags/11.zip
|
||||
https://github.com/Insality/defold-event/archive/refs/tags/12.zip
|
||||
```
|
||||
|
||||
After that, select `Project ▸ Fetch Libraries` to update [library dependencies]((https://defold.com/manuals/libraries/#setting-up-library-dependencies)). This happens automatically whenever you open a project so you will only need to do this if the dependencies change without re-opening the project.
|
||||
|
@@ -37,7 +37,7 @@ local utf8 = utf8 or utf8_lua --[[@as utf8]]
|
||||
---@field on_update_text_scale event fun(self: druid.text, scale: vector3, metrics: table) The event triggered when the text scale is updated
|
||||
---@field on_set_pivot event fun(self: druid.text, pivot: userdata) The event triggered when the text pivot is set
|
||||
---@field style druid.text.style The style of the text
|
||||
---@field start_pivot userdata The start pivot of the text
|
||||
---@field start_pivot number The start pivot of the text
|
||||
---@field start_scale vector3 The start scale of the text
|
||||
---@field scale vector3 The current scale of the text
|
||||
local M = component.create("text")
|
||||
@@ -224,7 +224,7 @@ end
|
||||
|
||||
|
||||
---Set text pivot. Text will re-anchor inside text area
|
||||
---@param pivot userdata The gui.PIVOT_* constant
|
||||
---@param pivot number The gui.PIVOT_* constant
|
||||
---@return druid.text self Current text instance
|
||||
function M:set_pivot(pivot)
|
||||
local prev_pivot = gui.get_pivot(self.node)
|
||||
|
223
druid/color.lua
223
druid/color.lua
@@ -6,12 +6,16 @@ local COLOR_Z = hash("color.z")
|
||||
|
||||
local M = {}
|
||||
|
||||
-- =============================================================================
|
||||
-- COLOR PARSING AND CREATION FUNCTIONS
|
||||
-- =============================================================================
|
||||
|
||||
---Get color by string (hex or from palette)
|
||||
---@param color_id string|vector4 Color id from palette or hex color
|
||||
|
||||
---Get color by string (hex or from palette) or pass through vector4
|
||||
---@param color_id string|vector4 Color id from palette, hex color, or existing vector4
|
||||
---@return vector4
|
||||
function M.get_color(color_id)
|
||||
if type(color_id) == "vector4" then
|
||||
if type(color_id) ~= "string" then
|
||||
return color_id
|
||||
end
|
||||
|
||||
@@ -30,8 +34,82 @@ function M.get_color(color_id)
|
||||
end
|
||||
|
||||
|
||||
---Parse color from various formats (hex, palette ID, or vector4)
|
||||
---This is a clearer alias for get_color
|
||||
---@param color_value string|vector4 Color value to parse
|
||||
---@return vector4
|
||||
function M.parse(color_value)
|
||||
return M.get_color(color_value)
|
||||
end
|
||||
|
||||
|
||||
---Create a color from RGBA values
|
||||
---@param r number Red component (0-1)
|
||||
---@param g number Green component (0-1)
|
||||
---@param b number Blue component (0-1)
|
||||
---@param a number|nil Alpha component (0-1), defaults to 1
|
||||
---@return vector4
|
||||
function M.new(r, g, b, a)
|
||||
return vmath.vector4(r, g, b, a or 1)
|
||||
end
|
||||
|
||||
|
||||
---Create a color from hex string
|
||||
---@param hex string Hex color. #00BBAA or 00BBAA or #0BA or 0BA
|
||||
---@param alpha number|nil Alpha value. Default is 1
|
||||
---@return vector4
|
||||
function M.from_hex(hex, alpha)
|
||||
return M.hex2vector4(hex, alpha)
|
||||
end
|
||||
|
||||
|
||||
---Create a color from HSB values
|
||||
---@param h number Hue (0-1)
|
||||
---@param s number Saturation (0-1)
|
||||
---@param b number Brightness/Value (0-1)
|
||||
---@param a number|nil Alpha value. Default is 1
|
||||
---@return vector4
|
||||
function M.from_hsb(h, s, b, a)
|
||||
local r, g, b_val, alpha = M.hsb2rgb(h, s, b, a)
|
||||
return vmath.vector4(r, g, b_val, alpha or 1)
|
||||
end
|
||||
|
||||
|
||||
-- =============================================================================
|
||||
-- COLOR FORMAT CONVERSION FUNCTIONS
|
||||
-- =============================================================================
|
||||
|
||||
---Convert color to hex string
|
||||
---@param color vector4 Color to convert
|
||||
---@return string Hex color string (without #)
|
||||
function M.to_hex(color)
|
||||
return M.rgb2hex(color.x, color.y, color.z)
|
||||
end
|
||||
|
||||
|
||||
---Convert color to RGB values
|
||||
---@param color vector4 Color to convert
|
||||
---@return number, number, number, number r, g, b, a
|
||||
function M.to_rgb(color)
|
||||
return color.x, color.y, color.z, color.w
|
||||
end
|
||||
|
||||
|
||||
---Convert color to HSB values
|
||||
---@param color vector4 Color to convert
|
||||
---@return number, number, number, number h, s, b, a
|
||||
function M.to_hsb(color)
|
||||
return M.rgb2hsb(color.x, color.y, color.z, color.w)
|
||||
end
|
||||
|
||||
|
||||
-- =============================================================================
|
||||
-- PALETTE MANAGEMENT FUNCTIONS
|
||||
-- =============================================================================
|
||||
|
||||
|
||||
---Add palette to palette data
|
||||
---@param palette_data table<string, vector4>
|
||||
---@param palette_data table<string, vector4|string>
|
||||
function M.add_palette(palette_data)
|
||||
for color_id, color in pairs(palette_data) do
|
||||
if type(color) == "string" then
|
||||
@@ -43,11 +121,59 @@ function M.add_palette(palette_data)
|
||||
end
|
||||
|
||||
|
||||
---Set a single color in the palette
|
||||
---@param color_id string Color identifier
|
||||
---@param color vector4|string Color value (vector4 or hex string)
|
||||
function M.set_palette_color(color_id, color)
|
||||
if type(color) == "string" then
|
||||
PALETTE_DATA[color_id] = M.hex2vector4(color)
|
||||
else
|
||||
PALETTE_DATA[color_id] = color
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
---Remove a color from the palette
|
||||
---@param color_id string Color identifier to remove
|
||||
function M.remove_palette_color(color_id)
|
||||
PALETTE_DATA[color_id] = nil
|
||||
end
|
||||
|
||||
|
||||
---Check if a color exists in the palette
|
||||
---@param color_id string Color identifier to check
|
||||
---@return boolean
|
||||
function M.has_palette_color(color_id)
|
||||
return PALETTE_DATA[color_id] ~= nil
|
||||
end
|
||||
|
||||
|
||||
---Get a color from the palette
|
||||
---@param color_id string Color identifier
|
||||
---@return vector4|nil Color or nil if not found
|
||||
function M.get_palette_color(color_id)
|
||||
return PALETTE_DATA[color_id]
|
||||
end
|
||||
|
||||
|
||||
---Get the entire palette
|
||||
---@return table<string, vector4>
|
||||
function M.get_palette()
|
||||
return PALETTE_DATA
|
||||
end
|
||||
|
||||
|
||||
---Clear the entire palette
|
||||
function M.clear_palette()
|
||||
PALETTE_DATA = {}
|
||||
end
|
||||
|
||||
|
||||
-- =============================================================================
|
||||
-- GUI NODE OPERATIONS
|
||||
-- =============================================================================
|
||||
|
||||
|
||||
---Set color of gui node without changing alpha
|
||||
---@param gui_node node
|
||||
---@param color vector4|vector3|string Color in vector4, vector3 or color id from palette
|
||||
@@ -62,6 +188,34 @@ function M.set_color(gui_node, color)
|
||||
end
|
||||
|
||||
|
||||
---Apply color to gui node (clearer alias for set_color)
|
||||
---@param gui_node node GUI node to apply color to
|
||||
---@param color vector4|vector3|string Color in vector4, vector3 or color id from palette
|
||||
function M.apply_to_node(gui_node, color)
|
||||
M.set_color(gui_node, color)
|
||||
end
|
||||
|
||||
|
||||
---Set node color including alpha
|
||||
---@param gui_node node GUI node to apply color to
|
||||
---@param color vector4|string Color with alpha channel
|
||||
function M.set_node_color_with_alpha(gui_node, color)
|
||||
if type(color) == "string" then
|
||||
color = M.get_color(color)
|
||||
end
|
||||
|
||||
gui.set(gui_node, COLOR_X, color.x)
|
||||
gui.set(gui_node, COLOR_Y, color.y)
|
||||
gui.set(gui_node, COLOR_Z, color.z)
|
||||
gui.set(gui_node, "color.w", color.w)
|
||||
end
|
||||
|
||||
|
||||
-- =============================================================================
|
||||
-- COLOR MANIPULATION FUNCTIONS
|
||||
-- =============================================================================
|
||||
|
||||
|
||||
---Lerp colors via color HSB values
|
||||
---@param t number Lerp value. 0 - color1, 1 - color2
|
||||
---@param color1 vector4 Color 1
|
||||
@@ -81,6 +235,55 @@ function M.lerp(t, color1, color2)
|
||||
end
|
||||
|
||||
|
||||
---Mix two colors using linear RGB interpolation (simpler than lerp)
|
||||
---@param color1 vector4 First color
|
||||
---@param color2 vector4 Second color
|
||||
---@param ratio number Mixing ratio (0 = color1, 1 = color2)
|
||||
---@return vector4 Mixed color
|
||||
function M.mix(color1, color2, ratio)
|
||||
local inv_ratio = 1 - ratio
|
||||
return vmath.vector4(
|
||||
color1.x * inv_ratio + color2.x * ratio,
|
||||
color1.y * inv_ratio + color2.y * ratio,
|
||||
color1.z * inv_ratio + color2.z * ratio,
|
||||
color1.w * inv_ratio + color2.w * ratio
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
---Lighten a color by mixing with white
|
||||
---@param color vector4 Color to lighten
|
||||
---@param amount number Amount to lighten (0-1)
|
||||
---@return vector4 Lightened color
|
||||
function M.lighten(color, amount)
|
||||
return M.mix(color, COLOR_WHITE, amount)
|
||||
end
|
||||
|
||||
|
||||
---Darken a color by mixing with black
|
||||
---@param color vector4 Color to darken
|
||||
---@param amount number Amount to darken (0-1)
|
||||
---@return vector4 Darkened color
|
||||
function M.darken(color, amount)
|
||||
local black = vmath.vector4(0, 0, 0, color.w)
|
||||
return M.mix(color, black, amount)
|
||||
end
|
||||
|
||||
|
||||
---Adjust color alpha
|
||||
---@param color vector4 Color to adjust
|
||||
---@param alpha number New alpha value (0-1)
|
||||
---@return vector4 Color with adjusted alpha
|
||||
function M.with_alpha(color, alpha)
|
||||
return vmath.vector4(color.x, color.y, color.z, alpha)
|
||||
end
|
||||
|
||||
|
||||
-- =============================================================================
|
||||
-- LOW-LEVEL CONVERSION FUNCTIONS
|
||||
-- =============================================================================
|
||||
|
||||
|
||||
|
||||
---Convert hex color to rgb values.
|
||||
---@param hex string Hex color. #00BBAA or 00BBAA or #0BA or 0BA
|
||||
@@ -168,9 +371,10 @@ end
|
||||
|
||||
|
||||
---Convert rgb color to hex color
|
||||
---@param red number Red value
|
||||
---@param green number Green value
|
||||
---@param blue number Blue value
|
||||
---@param red number Red value (0-1)
|
||||
---@param green number Green value (0-1)
|
||||
---@param blue number Blue value (0-1)
|
||||
---@return string Hex color string (without #)
|
||||
function M.rgb2hex(red, green, blue)
|
||||
local r = string.format("%x", math.floor(red * 255))
|
||||
local g = string.format("%x", math.floor(green * 255))
|
||||
@@ -179,6 +383,11 @@ function M.rgb2hex(red, green, blue)
|
||||
end
|
||||
|
||||
|
||||
-- =============================================================================
|
||||
-- INITIALIZATION
|
||||
-- =============================================================================
|
||||
|
||||
|
||||
local DEFAULT_PALETTE_PATH = sys.get_config_string("druid.palette_path")
|
||||
if DEFAULT_PALETTE_PATH then
|
||||
local loaded_palette = sys.load_resource(DEFAULT_PALETTE_PATH)
|
||||
|
@@ -374,7 +374,6 @@ end
|
||||
|
||||
|
||||
---Сreate a new component class, which will inherit from the base Druid component.
|
||||
---@protected
|
||||
---@param name string|nil The name of the component
|
||||
---@param input_priority number|nil The input priority. The bigger number processed first. Default value: 10
|
||||
---@return druid.component
|
||||
|
@@ -132,10 +132,14 @@ end
|
||||
--- msg.url(nil, object_url, "gui_widget") -- other game object
|
||||
---@generic T: druid.widget
|
||||
---@param widget_class T The class of the widget to return
|
||||
---@param gui_url url GUI url
|
||||
---@param gui_url url|string GUI url or string of component name near current script
|
||||
---@param params any|nil Additional parameters to pass to the widget's init function
|
||||
---@return T widget The new created widget,
|
||||
function M.get_widget(widget_class, gui_url, params)
|
||||
if type(gui_url) == "string" then
|
||||
gui_url = msg.url(nil, nil, gui_url)
|
||||
end
|
||||
|
||||
gui_url = gui_url or msg.url()
|
||||
local registered_druids = REGISTERED_GUI_WIDGETS[gui_url.socket]
|
||||
assert(registered_druids, "Druid widget not registered for this game object")
|
||||
|
@@ -5,7 +5,7 @@
|
||||
|
||||
local queues = require("event.queues")
|
||||
|
||||
---Usage: defer.push("druid.get_atlas_path", {
|
||||
---Usage: queues.push("druid.get_atlas_path", {
|
||||
--- texture_name = gui.get_texture(self.node),
|
||||
--- sender = msg.url(),
|
||||
---}, callback, [context])
|
||||
@@ -30,7 +30,7 @@ local function get_atlas_path(self, request)
|
||||
return nil
|
||||
end
|
||||
|
||||
return go.get(request.sender, "textures", { key = request.texture_name })
|
||||
return go.get(request.sender, "textures", { key = request.texture_name }) --[[@as string]]
|
||||
end
|
||||
|
||||
|
||||
|
@@ -273,7 +273,7 @@ end
|
||||
|
||||
|
||||
---@param node_or_container node|string|druid.container|table The node or container to add
|
||||
---@param mode string|nil stretch, fit, stretch_x, stretch_y. Default: Pick from node, "fit" or "stretch"
|
||||
---@param mode druid.container.mode|nil stretch, fit, stretch_x, stretch_y. Default: Pick from node, "fit" or "stretch"
|
||||
---@param on_resize_callback fun(self: userdata, size: vector3)|nil
|
||||
---@return druid.container Container New created layout instance
|
||||
function M:add_container(node_or_container, mode, on_resize_callback)
|
||||
|
@@ -69,6 +69,7 @@ function M:init(node_or_node_id, layout_type)
|
||||
self.is_resize_width = false
|
||||
self.is_resize_height = false
|
||||
self.is_justify = false
|
||||
self._set_position_function = gui.set_position
|
||||
|
||||
self.on_size_changed = event.create() --[[@as event.on_size_changed]]
|
||||
end
|
||||
@@ -79,7 +80,7 @@ function M:update()
|
||||
return
|
||||
end
|
||||
|
||||
self:refresh_layout()
|
||||
self:refresh_layout(false)
|
||||
end
|
||||
|
||||
|
||||
@@ -235,8 +236,9 @@ function M:get_content_size()
|
||||
end
|
||||
|
||||
|
||||
---@param is_instant boolean|nil If true, node position update instantly, otherwise with set_position_function callback
|
||||
---@return druid.layout self Current layout instance
|
||||
function M:refresh_layout()
|
||||
function M:refresh_layout(is_instant)
|
||||
local layout_node = self.node
|
||||
|
||||
local entities = self.entities
|
||||
@@ -354,7 +356,7 @@ function M:refresh_layout()
|
||||
end
|
||||
end
|
||||
|
||||
self:set_node_position(node, position_x, position_y)
|
||||
self:set_node_position(node, position_x, position_y, is_instant)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -499,13 +501,27 @@ local TEMP_VECTOR = vmath.vector3(0, 0, 0)
|
||||
---@param x number
|
||||
---@param y number
|
||||
---@return node
|
||||
function M:set_node_position(node, x, y)
|
||||
function M:set_node_position(node, x, y, is_instant)
|
||||
TEMP_VECTOR.x = x
|
||||
TEMP_VECTOR.y = y
|
||||
|
||||
if is_instant then
|
||||
gui.set_position(node, TEMP_VECTOR)
|
||||
else
|
||||
self._set_position_function(node, TEMP_VECTOR)
|
||||
end
|
||||
|
||||
return node
|
||||
end
|
||||
|
||||
|
||||
---Set custom position function for layout nodes. It will call on update poses on layout elements. Default: gui.set_position
|
||||
---@param callback function
|
||||
---@return druid.layout self Current layout instance
|
||||
function M:set_position_function(callback)
|
||||
self._set_position_function = callback or gui.set_position
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
return M
|
||||
|
@@ -473,6 +473,9 @@ function M.get_text_metrics_from_node(text_node)
|
||||
options.tracking = gui.get_tracking(text_node)
|
||||
options.line_break = gui.get_line_break(text_node)
|
||||
|
||||
options.width = 0
|
||||
options.leading = 0
|
||||
|
||||
-- Gather other options only if it used in node
|
||||
if options.line_break then
|
||||
options.width = gui.get_size(text_node).x
|
||||
|
@@ -1,3 +1,4 @@
|
||||
---@diagnostic disable: invisible
|
||||
-- Hello, Defolder! Wish you a good day!
|
||||
|
||||
local events = require("event.events")
|
||||
|
@@ -33,21 +33,34 @@ nodes {
|
||||
}
|
||||
}
|
||||
nodes {
|
||||
position {
|
||||
x: 200.0
|
||||
}
|
||||
size {
|
||||
x: 400.0
|
||||
y: 40.0
|
||||
}
|
||||
color {
|
||||
x: 0.173
|
||||
y: 0.184
|
||||
z: 0.204
|
||||
}
|
||||
type: TYPE_BOX
|
||||
texture: "druid/ui_circle_16"
|
||||
id: "header"
|
||||
pivot: PIVOT_N
|
||||
pivot: PIVOT_NE
|
||||
parent: "root"
|
||||
inherit_alpha: true
|
||||
size_mode: SIZE_MODE_AUTO
|
||||
visible: false
|
||||
slice9 {
|
||||
x: 8.0
|
||||
y: 8.0
|
||||
z: 8.0
|
||||
w: 8.0
|
||||
}
|
||||
}
|
||||
nodes {
|
||||
position {
|
||||
x: -192.0
|
||||
x: -392.0
|
||||
y: -8.0
|
||||
}
|
||||
scale {
|
||||
@@ -85,7 +98,7 @@ nodes {
|
||||
}
|
||||
nodes {
|
||||
position {
|
||||
x: 192.0
|
||||
x: -8.0
|
||||
y: -4.0
|
||||
}
|
||||
color {
|
||||
@@ -103,7 +116,7 @@ nodes {
|
||||
}
|
||||
nodes {
|
||||
position {
|
||||
x: 152.0
|
||||
x: -48.0
|
||||
y: -4.0
|
||||
}
|
||||
color {
|
||||
@@ -121,7 +134,7 @@ nodes {
|
||||
}
|
||||
nodes {
|
||||
position {
|
||||
x: 112.0
|
||||
x: -88.0
|
||||
y: -4.0
|
||||
}
|
||||
scale {
|
||||
|
@@ -39,6 +39,7 @@ function M:init()
|
||||
self.contaienr_scroll_content = self.container_scroll_view:add_container("scroll_content")
|
||||
|
||||
self.default_size = self.container:get_size()
|
||||
self.header_size = gui.get_size(self:get_node("header"))
|
||||
|
||||
-- To have ability to go back to previous scene, collections of all properties to rebuild
|
||||
self.scenes = {}
|
||||
@@ -362,11 +363,18 @@ end
|
||||
|
||||
function M:set_hidden(is_hidden)
|
||||
self._is_hidden = is_hidden
|
||||
local hidden_size = gui.get_size(self:get_node("header"))
|
||||
local node_header = self:get_node("header")
|
||||
|
||||
local new_size = self._is_hidden and hidden_size or self.default_size
|
||||
local new_size = self._is_hidden and self.header_size or self.default_size
|
||||
self.container:set_size(new_size.x, new_size.y, gui.PIVOT_N)
|
||||
|
||||
local hidden_width = self.header_size.y + 8
|
||||
gui.set(node_header, "size.x", self._is_hidden and hidden_width or self.header_size.x)
|
||||
|
||||
gui.set_visible(node_header, self._is_hidden)
|
||||
gui.set_visible(self.root, not self._is_hidden)
|
||||
|
||||
gui.set_enabled(self.text_header.node, not self._is_hidden)
|
||||
gui.set_enabled(self.content, not self._is_hidden)
|
||||
gui.set_enabled(self.button_refresh.node, not self._is_hidden)
|
||||
|
||||
|
@@ -14,16 +14,16 @@ update_frequency = 60
|
||||
|
||||
[project]
|
||||
title = Druid
|
||||
version = 1.1.3
|
||||
version = 1.1.5
|
||||
publisher = Insality
|
||||
developer = Maksim Tuprikov
|
||||
custom_resources = /example/locales
|
||||
dependencies#0 = https://github.com/britzl/deftest/archive/refs/tags/2.8.0.zip
|
||||
dependencies#1 = https://github.com/Insality/defold-saver/archive/refs/heads/develop.zip
|
||||
dependencies#1 = https://github.com/Insality/defold-saver/archive/refs/tags/5.zip
|
||||
dependencies#2 = https://github.com/Insality/defold-tweener/archive/refs/tags/3.zip
|
||||
dependencies#3 = https://github.com/Insality/panthera/archive/refs/heads/develop.zip
|
||||
dependencies#3 = https://github.com/Insality/panthera/archive/refs/tags/runtime.4.zip
|
||||
dependencies#4 = https://github.com/Insality/defold-lang/archive/refs/tags/3.zip
|
||||
dependencies#5 = https://github.com/Insality/defold-event/archive/refs/heads/develop.zip
|
||||
dependencies#5 = https://github.com/Insality/defold-event/archive/refs/tags/12.zip
|
||||
dependencies#6 = https://github.com/subsoap/defos/archive/refs/tags/v2.8.0.zip
|
||||
|
||||
[library]
|
||||
|
@@ -5,6 +5,7 @@ function init(self)
|
||||
deftest.add(require("test.tests.test_back_handler"))
|
||||
deftest.add(require("test.tests.test_blocker"))
|
||||
deftest.add(require("test.tests.test_button"))
|
||||
deftest.add(require("test.tests.test_color"))
|
||||
deftest.add(require("test.tests.test_container"))
|
||||
deftest.add(require("test.tests.test_drag"))
|
||||
deftest.add(require("test.tests.test_grid"))
|
||||
|
313
test/tests/test_color.lua
Normal file
313
test/tests/test_color.lua
Normal file
@@ -0,0 +1,313 @@
|
||||
return function()
|
||||
describe("Color Module", function()
|
||||
local color = nil
|
||||
|
||||
before(function()
|
||||
color = require("druid.color")
|
||||
end)
|
||||
|
||||
describe("Color Creation and Parsing", function()
|
||||
it("Should create colors from RGBA values", function()
|
||||
local test_color = color.new(1, 0.5, 0, 0.8)
|
||||
assert(test_color.x == 1)
|
||||
assert(test_color.y == 0.5)
|
||||
assert(test_color.z == 0)
|
||||
assert(test_color.w == 0.8)
|
||||
end)
|
||||
|
||||
it("Should create colors from hex strings", function()
|
||||
local red = color.from_hex("#FF0000")
|
||||
assert(red.x == 1)
|
||||
assert(red.y == 0)
|
||||
assert(red.z == 0)
|
||||
assert(red.w == 1)
|
||||
|
||||
local blue_with_alpha = color.from_hex("0000FF", 0.5)
|
||||
assert(blue_with_alpha.x == 0)
|
||||
assert(blue_with_alpha.y == 0)
|
||||
assert(blue_with_alpha.z == 1)
|
||||
assert(blue_with_alpha.w == 0.5)
|
||||
end)
|
||||
|
||||
it("Should create colors from HSB values", function()
|
||||
local red = color.from_hsb(0, 1, 1) -- HSB for red
|
||||
assert(red.x == 1)
|
||||
assert(red.y == 0)
|
||||
assert(red.z == 0)
|
||||
assert(red.w == 1)
|
||||
end)
|
||||
|
||||
it("Should parse various color formats", function()
|
||||
-- Test parsing alias for get_color
|
||||
local hex_color = color.parse("#FF0000")
|
||||
assert(hex_color.x == 1)
|
||||
|
||||
local vector_color = color.parse(vmath.vector4(0, 1, 0, 1))
|
||||
assert(vector_color.y == 1)
|
||||
end)
|
||||
|
||||
it("Should parse hex colors correctly with get_color", function()
|
||||
-- Test with # prefix
|
||||
local color1 = color.get_color("#FF0000")
|
||||
assert(color1.x == 1)
|
||||
assert(color1.y == 0)
|
||||
assert(color1.z == 0)
|
||||
assert(color1.w == 1)
|
||||
|
||||
-- Test without # prefix
|
||||
local color2 = color.get_color("00FF00")
|
||||
assert(color2.x == 0)
|
||||
assert(color2.y == 1)
|
||||
assert(color2.z == 0)
|
||||
assert(color2.w == 1)
|
||||
|
||||
-- Test 3-digit hex
|
||||
local color3 = color.get_color("F0F")
|
||||
assert(color3.x == 1)
|
||||
assert(color3.y == 0)
|
||||
assert(color3.z == 1)
|
||||
assert(color3.w == 1)
|
||||
end)
|
||||
|
||||
it("Should handle vector4 input in get_color", function()
|
||||
local input_color = vmath.vector4(1, 0.5, 0, 1)
|
||||
local result = color.get_color(input_color)
|
||||
assert(result == input_color)
|
||||
end)
|
||||
|
||||
it("Should return white for unknown color IDs", function()
|
||||
local result = color.get_color("unknown_color")
|
||||
assert(result.x == 1)
|
||||
assert(result.y == 1)
|
||||
assert(result.z == 1)
|
||||
assert(result.w == 1)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("Color Format Conversion", function()
|
||||
it("Should convert colors to hex", function()
|
||||
local red = vmath.vector4(1, 0, 0, 1)
|
||||
local hex = color.to_hex(red)
|
||||
assert(hex == "FF0000")
|
||||
end)
|
||||
|
||||
it("Should convert colors to RGB values", function()
|
||||
local test_color = vmath.vector4(1, 0.5, 0.25, 0.8)
|
||||
local r, g, b, a = color.to_rgb(test_color)
|
||||
assert(r == 1)
|
||||
assert(g == 0.5)
|
||||
assert(b == 0.25)
|
||||
assert(a == 0.8)
|
||||
end)
|
||||
|
||||
it("Should convert colors to HSB values", function()
|
||||
local red = vmath.vector4(1, 0, 0, 1)
|
||||
local h, s, b, a = color.to_hsb(red)
|
||||
assert(h == 0) -- Red hue
|
||||
assert(s == 1) -- Full saturation
|
||||
assert(b == 1) -- Full brightness
|
||||
assert(a == 1) -- Full alpha
|
||||
end)
|
||||
|
||||
it("Should convert hex to rgb values", function()
|
||||
local r, g, b = color.hex2rgb("#FF8000")
|
||||
assert(r == 1)
|
||||
assert(g == 0.5019607843137255) -- 128/255
|
||||
assert(b == 0)
|
||||
end)
|
||||
|
||||
it("Should convert hex to vector4", function()
|
||||
local vec = color.hex2vector4("#FF8000", 0.5)
|
||||
assert(vec.x == 1)
|
||||
assert(vec.y == 0.5019607843137255)
|
||||
assert(vec.z == 0)
|
||||
assert(vec.w == 0.5)
|
||||
end)
|
||||
|
||||
it("Should convert rgb to hex", function()
|
||||
local hex = color.rgb2hex(1, 0.5019607843137255, 0)
|
||||
assert(hex == "FF8000")
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("Color Space Conversion", function()
|
||||
it("Should convert RGB to HSB correctly", function()
|
||||
local h, s, v, a = color.rgb2hsb(1, 0, 0, 1) -- Red
|
||||
assert(h == 0)
|
||||
assert(s == 1)
|
||||
assert(v == 1)
|
||||
assert(a == 1)
|
||||
end)
|
||||
|
||||
it("Should convert HSB to RGB correctly", function()
|
||||
local r, g, b, a = color.hsb2rgb(0, 1, 1, 1) -- Red
|
||||
assert(r == 1)
|
||||
assert(g == 0)
|
||||
assert(b == 0)
|
||||
assert(a == 1)
|
||||
end)
|
||||
|
||||
it("Should handle round-trip HSB conversion", function()
|
||||
local original_r, original_g, original_b = 0.5, 0.7, 0.3
|
||||
local h, s, v = color.rgb2hsb(original_r, original_g, original_b)
|
||||
local converted_r, converted_g, converted_b = color.hsb2rgb(h, s, v)
|
||||
|
||||
-- Allow for small floating point differences
|
||||
assert(math.abs(converted_r - original_r) < 0.001)
|
||||
assert(math.abs(converted_g - original_g) < 0.001)
|
||||
assert(math.abs(converted_b - original_b) < 0.001)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("Palette Management", function()
|
||||
it("Should add and retrieve palette colors", function()
|
||||
local test_palette = {
|
||||
primary = vmath.vector4(1, 0, 0, 1),
|
||||
secondary = "#00FF00",
|
||||
tertiary = vmath.vector4(0, 0, 1, 1)
|
||||
}
|
||||
|
||||
color.add_palette(test_palette)
|
||||
|
||||
local primary = color.get_color("primary")
|
||||
assert(primary.x == 1)
|
||||
assert(primary.y == 0)
|
||||
assert(primary.z == 0)
|
||||
assert(primary.w == 1)
|
||||
|
||||
local secondary = color.get_color("secondary")
|
||||
assert(secondary.x == 0)
|
||||
assert(secondary.y == 1)
|
||||
assert(secondary.z == 0)
|
||||
assert(secondary.w == 1)
|
||||
end)
|
||||
|
||||
it("Should manage individual palette colors", function()
|
||||
-- Set a palette color
|
||||
color.set_palette_color("test_red", vmath.vector4(1, 0, 0, 1))
|
||||
assert(color.has_palette_color("test_red") == true)
|
||||
|
||||
local retrieved = color.get_palette_color("test_red")
|
||||
assert(retrieved.x == 1)
|
||||
assert(retrieved.y == 0)
|
||||
assert(retrieved.z == 0)
|
||||
|
||||
-- Remove the color
|
||||
color.remove_palette_color("test_red")
|
||||
assert(color.has_palette_color("test_red") == false)
|
||||
assert(color.get_palette_color("test_red") == nil)
|
||||
end)
|
||||
|
||||
it("Should return the palette", function()
|
||||
local palette = color.get_palette()
|
||||
assert(type(palette) == "table")
|
||||
end)
|
||||
|
||||
it("Should clear the palette", function()
|
||||
color.set_palette_color("temp_color", vmath.vector4(1, 1, 1, 1))
|
||||
color.clear_palette()
|
||||
assert(color.has_palette_color("temp_color") == false)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("Color Manipulation", function()
|
||||
it("Should lerp colors correctly", function()
|
||||
local color1 = vmath.vector4(1, 0, 0, 1) -- Red
|
||||
local color2 = vmath.vector4(0, 1, 0, 1) -- Green
|
||||
|
||||
local mid_color = color.lerp(0.5, color1, color2)
|
||||
-- Note: lerp uses HSB interpolation, so the result might not be a simple average
|
||||
assert(type(mid_color.x) == "number")
|
||||
assert(type(mid_color.y) == "number")
|
||||
assert(type(mid_color.z) == "number")
|
||||
assert(mid_color.w == 1)
|
||||
|
||||
-- Test endpoints
|
||||
local start_color = color.lerp(0, color1, color2)
|
||||
local end_color = color.lerp(1, color1, color2)
|
||||
|
||||
assert(math.abs(start_color.x - color1.x) < 0.001)
|
||||
assert(math.abs(end_color.x - color2.x) < 0.001)
|
||||
end)
|
||||
|
||||
it("Should mix colors using linear RGB interpolation", function()
|
||||
local red = vmath.vector4(1, 0, 0, 1)
|
||||
local blue = vmath.vector4(0, 0, 1, 1)
|
||||
|
||||
local mixed = color.mix(red, blue, 0.5)
|
||||
assert(mixed.x == 0.5) -- Half red
|
||||
assert(mixed.y == 0) -- No green
|
||||
assert(mixed.z == 0.5) -- Half blue
|
||||
assert(mixed.w == 1) -- Full alpha
|
||||
end)
|
||||
|
||||
it("Should lighten colors", function()
|
||||
local dark_red = vmath.vector4(0.5, 0, 0, 1)
|
||||
local lightened = color.lighten(dark_red, 0.5)
|
||||
|
||||
assert(lightened.x > dark_red.x) -- Should be lighter
|
||||
assert(lightened.y > dark_red.y) -- Should have some green/white
|
||||
assert(lightened.z > dark_red.z) -- Should have some blue/white
|
||||
end)
|
||||
|
||||
it("Should darken colors", function()
|
||||
local bright_red = vmath.vector4(1, 0, 0, 1)
|
||||
local darkened = color.darken(bright_red, 0.5)
|
||||
|
||||
assert(darkened.x < bright_red.x) -- Should be darker
|
||||
assert(darkened.y == 0) -- Should still have no green
|
||||
assert(darkened.z == 0) -- Should still have no blue
|
||||
end)
|
||||
|
||||
it("Should adjust alpha", function()
|
||||
local opaque_red = vmath.vector4(1, 0, 0, 1)
|
||||
local semi_transparent = color.with_alpha(opaque_red, 0.5)
|
||||
|
||||
assert(semi_transparent.x == 1) -- Color unchanged
|
||||
assert(semi_transparent.y == 0)
|
||||
assert(semi_transparent.z == 0)
|
||||
assert(semi_transparent.w == 0.5) -- Alpha changed
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("GUI Node Operations", function()
|
||||
it("Should set color on GUI node", function()
|
||||
-- Create a test node
|
||||
local test_node = gui.new_box_node(vmath.vector3(0, 0, 0), vmath.vector3(100, 100, 0))
|
||||
|
||||
-- Test with vector4
|
||||
local test_color = vmath.vector4(1, 0.5, 0, 1)
|
||||
color.set_color(test_node, test_color)
|
||||
|
||||
-- Verify color was set (we can't easily read it back, but we can verify no errors)
|
||||
assert(true) -- If we get here, no error occurred
|
||||
|
||||
-- Test with string color
|
||||
color.set_color(test_node, "#FF0000")
|
||||
assert(true) -- If we get here, no error occurred
|
||||
|
||||
-- Clean up
|
||||
gui.delete_node(test_node)
|
||||
end)
|
||||
|
||||
it("Should apply color to node using alias", function()
|
||||
local test_node = gui.new_box_node(vmath.vector3(0, 0, 0), vmath.vector3(100, 100, 0))
|
||||
|
||||
-- Test the alias function
|
||||
color.apply_to_node(test_node, vmath.vector4(0, 1, 0, 1))
|
||||
assert(true) -- If we get here, no error occurred
|
||||
|
||||
gui.delete_node(test_node)
|
||||
end)
|
||||
|
||||
it("Should set node color including alpha", function()
|
||||
local test_node = gui.new_box_node(vmath.vector3(0, 0, 0), vmath.vector3(100, 100, 0))
|
||||
|
||||
color.set_node_color_with_alpha(test_node, vmath.vector4(1, 0, 0, 0.5))
|
||||
assert(true) -- If we get here, no error occurred
|
||||
|
||||
gui.delete_node(test_node)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
@@ -712,3 +712,22 @@ Please support me if you like this project! It will help me keep engaged to upda
|
||||
|
||||
#### Druid 1.1.3
|
||||
- Fix for node_id of cloned nodes with `gui.clone_tree`
|
||||
|
||||
#### Druid 1.1.4
|
||||
- [#312] Fix for text metrics issue if returned height is 0 sometimes
|
||||
|
||||
#### Druid 1.1.5
|
||||
- Update for using `defold-event` library v12
|
||||
|
||||
### Druid 1.2.0
|
||||
- Fix for blocker internal enabled state depends from GUI node
|
||||
- Move to druid colors for rich text
|
||||
- Fix for container stretch mode (stretch and fit is not worked in init function)
|
||||
- Add split_to_characters in rich text for making fancy text
|
||||
- Druid GO Widgets now can wrap an events to (before only top level functions)
|
||||
- Ability to pass params to Druid GO Widgets
|
||||
- Update properties panel:
|
||||
- Add "scenes" to manage a list of properties with back button support
|
||||
- Add "refresh" button, which active a 1-sec refresh for current page
|
||||
- Add "Render lua table" to easily render your lua tables with a various types support (any simple types and vector, functions, events etc)
|
||||
|
||||
|
BIN
wiki/manuals/media/memory_fps_panel.mov
Normal file
BIN
wiki/manuals/media/memory_fps_panel.mov
Normal file
Binary file not shown.
BIN
wiki/manuals/media/memory_fps_panel_add.png
Normal file
BIN
wiki/manuals/media/memory_fps_panel_add.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 76 KiB |
BIN
wiki/manuals/media/memory_fps_panel_select.png
Normal file
BIN
wiki/manuals/media/memory_fps_panel_select.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 99 KiB |
98
wiki/manuals/widget_memory_fps_panels.md
Normal file
98
wiki/manuals/widget_memory_fps_panels.md
Normal file
@@ -0,0 +1,98 @@
|
||||
# Memory and FPS Panel Widgets
|
||||
|
||||
The `Druid 1.1` comes with widgets and two included widgets are `Memory Panel` and `FPS Panel` which allow you to monitor memory and FPS in your game.
|
||||
|
||||
Widgets in Druid usually consists from two files: GUI, which is used to placed as a template on your GUI scene and Lua script, which is used to be created with Druid.
|
||||
|
||||
<!-- Video -->
|
||||
|
||||
## Memory Panel
|
||||
|
||||
The `Memory Panel` is a widget which allows you to monitor memory of your game. It displays the last 3 seconds of memory allocations in graph, largest memory allocation step, total Lua memory and memory per second.
|
||||
|
||||
When you see an empty space in graphs - it means the garbage collector is working at this moment.
|
||||
|
||||
### How to add:
|
||||
|
||||
- Add `/druid/widget/memory_panel/memory_panel.gui` to your `*.gui` scene
|
||||
|
||||

|
||||

|
||||
|
||||
- You can adjust a scale of the template if required
|
||||
- Add Druid and widget setup to your `*.gui_script`
|
||||
```lua
|
||||
local druid = require("druid.druid")
|
||||
local memory_panel = require("druid.widget.memory_panel.memory_panel")
|
||||
|
||||
function init(self)
|
||||
self.druid = druid.new(self)
|
||||
-- "memory_panel" is a name of the template in the GUI scene, often it matches the name of the template file
|
||||
self.memory_panel = self.druid:new_widget(memory_panel, "memory_panel")
|
||||
end
|
||||
|
||||
function final(self)
|
||||
self.druid:final()
|
||||
end
|
||||
|
||||
function update(self, dt)
|
||||
self.druid:update(dt)
|
||||
end
|
||||
|
||||
function on_message(self, message_id, message, sender)
|
||||
self.druid:on_message(message_id, message, sender)
|
||||
end
|
||||
|
||||
function on_input(self, action_id, action)
|
||||
return self.druid:on_input(action_id, action)
|
||||
end
|
||||
```
|
||||
|
||||
And make sure of:
|
||||
- The `*.gui_script` is attached to your `*.gui` scene
|
||||
- The GUI component is added to your game scene
|
||||
|
||||
|
||||
## FPS Panel
|
||||
|
||||
The `FPS Panel` is a widget which allows you to monitor FPS of your game. It displays the last 3 seconds of FPS graph, lowest and current FPS values
|
||||
|
||||
### How to add:
|
||||
|
||||
- Add `/druid/widget/fps_panel/fps_panel.gui` to your `*.gui` scene
|
||||
- You can adjust a scale of the template if required
|
||||
- Add Druid and widget setup to your `*.gui_script`
|
||||
```lua
|
||||
local druid = require("druid.druid")
|
||||
local fps_panel = require("druid.widget.fps_panel.fps_panel")
|
||||
|
||||
function init(self)
|
||||
self.druid = druid.new(self)
|
||||
-- "fps_panel" is a name of the template in the GUI scene, often it matches the name of the template file
|
||||
self.fps_panel = self.druid:new_widget(fps_panel, "fps_panel")
|
||||
end
|
||||
|
||||
function final(self)
|
||||
self.druid:final()
|
||||
end
|
||||
|
||||
function update(self, dt)
|
||||
self.druid:update(dt)
|
||||
end
|
||||
|
||||
function on_message(self, message_id, message, sender)
|
||||
self.druid:on_message(message_id, message, sender)
|
||||
end
|
||||
|
||||
function on_input(self, action_id, action)
|
||||
return self.druid:on_input(action_id, action)
|
||||
end
|
||||
```
|
||||
|
||||
And make sure of:
|
||||
- The `*.gui_script` is attached to your `*.gui` scene
|
||||
- The GUI component is added to your game scene
|
||||
|
||||
These widgets not only can be useful for development and profiling your game, but also as an example of how to create custom widgets with Druid and use them in your game.
|
||||
|
||||
Thanks for reading!
|
Reference in New Issue
Block a user