Blocker by default is enabled, update color palette, add rich text split by characters option, able to pass a data to GO widgets

This commit is contained in:
Insality 2025-05-27 23:10:32 +03:00
parent fe955b6e64
commit 2e1f280944
8 changed files with 58 additions and 27 deletions

View File

@ -20,7 +20,7 @@ local M = component.create("blocker")
---@param node node|string The node to use as a blocker
function M:init(node)
self.node = self:get_node(node)
self._is_enabled = gui.is_enabled(self.node, true)
self._is_enabled = true
end

View File

@ -8,9 +8,13 @@ local M = {}
---Get color by string (hex or from palette)
---@param color_id string Color id from palette or hex color
---@param color_id string|vector4 Color id from palette or hex color
---@return vector4
function M.get_color(color_id)
if type(color_id) == "vector4" then
return color_id
end
if PALETTE_DATA[color_id] then
return PALETTE_DATA[color_id]
end
@ -175,21 +179,15 @@ function M.rgb2hex(red, green, blue)
end
local load_palette_from_json = function(path)
local data = sys.load_resource(path)
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)
local data = loaded_palette and json.decode(loaded_palette)
if not data then
return
end
return json.decode(data)
end
local DEFAULT_PALETTE_PATH = sys.get_config_string("druid.palette_path")
if DEFAULT_PALETTE_PATH then
local loaded_palette = load_palette_from_json(DEFAULT_PALETTE_PATH)
if loaded_palette and loaded_palette["default"] then
M.add_palette(loaded_palette["default"])
end
M.add_palette(data)
end

View File

@ -181,6 +181,7 @@ function M.create(text, settings, style)
shadow = settings.shadow,
outline = settings.outline,
font = gui.get_font(settings.text_prefab),
split_to_characters = settings.split_to_characters,
-- Image params
---@type druid.rich_text.word.image
image = nil,

View File

@ -29,6 +29,7 @@ local function add_word(text, settings, words)
end
words[#words + 1] = data
return data
end
@ -44,8 +45,17 @@ local function split_line(line, settings, words)
else
local wi = #words
for word in trimmed_text:gmatch("%S+") do
if settings.split_to_characters then
for i = 1, #word do
local symbol = utf8.sub(word, i, i)
local w = add_word(symbol, settings, words)
w.nobr = true
end
add_word(" ", settings, words)
else
add_word(word .. " ", settings, words)
end
end
local first = words[wi + 1]
first.text = ws_start .. first.text
first.source_text = first.text

View File

@ -2,7 +2,8 @@
-- Author: Britzl
-- Modified by: Insality
local color = require("druid.custom.rich_text.module.rt_color")
--local color = require("druid.custom.rich_text.module.rt_color")
local color = require("druid.color")
local M = {}
local tags = {}
@ -44,19 +45,19 @@ end
-- Example: <color=FF0000>Rich Text</color>
M.register("color", function(params, settings, style)
params = style.COLORS[params] or params
settings.color = color.parse(params)
settings.color = color.get_color(params)
end)
M.register("shadow", function(params, settings, style)
params = style.COLORS[params] or params
settings.shadow = color.parse(params)
settings.shadow = color.get_color(params)
end)
M.register("outline", function(params, settings, style)
params = style.COLORS[params] or params
settings.outline = color.parse(params)
settings.outline = color.get_color(params)
end)

View File

@ -13,6 +13,7 @@ local rich_text = require("druid.custom.rich_text.module.rt")
---@field image_pixel_grid_snap boolean
---@field combine_words boolean
---@field default_animation string
---@field split_by_character boolean
---@field text_prefab node
---@field adjust_scale number
---@field default_texture string
@ -194,6 +195,15 @@ function M:tagged(tag)
end
---Set if the rich text should split to characters, not words
---@param value boolean
---@return druid.rich_text self
function M:set_split_to_characters(value)
self._settings.split_to_characters = value
return self
end
---Get all current created words, each word is a table that contains the information about the word
---@return druid.rich_text.word[]
function M:get_words()
@ -239,6 +249,7 @@ function M:_create_settings()
outline = gui.get_outline(self.root),
text_leading = gui.get_leading(self.root),
is_multiline = gui.get_line_break(self.root),
split_to_characters = false,
-- Image settings
image_pixel_grid_snap = false, -- disabled now

View File

@ -114,6 +114,12 @@ local function wrap_widget(widget)
end
end
for key, value in pairs(widget) do
if event.is_event(value) then
wrapped_widget[key] = value
end
end
return wrapped_widget
end
@ -127,22 +133,21 @@ end
---@generic T: druid.widget
---@param widget_class T The class of the widget to return
---@param gui_url url GUI url
---@return T? widget The new created widget,
function M.get_widget(widget_class, gui_url)
---@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)
gui_url = gui_url or msg.url()
local registered_druids = REGISTERED_GUI_WIDGETS[gui_url.socket]
if not registered_druids then
return nil
end
assert(registered_druids, "Druid widget not registered for this game object")
for index = 1, #registered_druids do
local druid = registered_druids[index]
if druid.fragment == gui_url.fragment and druid.path == gui_url.path then
return druid.new_widget:trigger(widget_class)
return druid.new_widget:trigger(widget_class, nil, nil, params)
end
end
return nil
error("Druid widget not found for this game object: " .. gui_url)
end
@ -156,8 +161,8 @@ function M.register_druid_as_widget(druid)
table.insert(REGISTERED_GUI_WIDGETS[gui_url.socket], {
path = gui_url.path,
fragment = gui_url.fragment,
new_widget = event.create(function(widget_class)
return wrap_widget(druid:new_widget(widget_class))
new_widget = event.create(function(widget_class, template, nodes, params)
return wrap_widget(druid:new_widget(widget_class, template, nodes, params))
end),
})
end

View File

@ -343,6 +343,11 @@ function M:set_hidden(is_hidden)
self.container:set_size(new_size.x, new_size.y, gui.PIVOT_N)
gui.set_enabled(self.content, not self._is_hidden)
gui.set_enabled(self.button_refresh.node, not self._is_hidden)
if not self._is_hidden then
self.is_dirty = true
end
end