Update annotations, fix rare errors

This commit is contained in:
Insality 2024-10-05 01:40:46 +03:00
parent b9b74736a7
commit d39f4715f3
5 changed files with 55 additions and 31 deletions

View File

@ -1846,19 +1846,24 @@ function helper.table_to_string(t) end
---@class druid.rich_text.word ---@class druid.rich_text.word
---@field node node ---@field node node
---@field relative_scale number ---@field relative_scale number
---@field adjust_scale number
---@field color vector4 ---@field color vector4
---@field position vector3 ---@field position vector3
---@field offset vector3 ---@field offset vector3
---@field scale vector3 ---@field scale vector3
---@field size vector3 ---@field size vector3
---@field metrics druid.rich_text.metrics ---@field metrics druid.rich_text.metrics
---@field pivot number @ The gui.PIVOT_* constant ---@field pivot userdata @ The gui.PIVOT_* constant
---@field text string ---@field text string
---@field source_text string
---@field text_color vector4
---@field shadow vector4 ---@field shadow vector4
---@field outline vector4 ---@field outline vector4
---@field font string ---@field font string
---@field image druid.rich_text.image ---@field image druid.rich_text.image
---@field image_color vector4
---@field default_animation string ---@field default_animation string
---@field default_texture string
---@field anchor number ---@field anchor number
---@field br boolean ---@field br boolean
---@field nobr boolean ---@field nobr boolean
@ -1880,8 +1885,10 @@ function helper.table_to_string(t) end
---@field image_pixel_grid_snap boolean ---@field image_pixel_grid_snap boolean
---@field combine_words boolean ---@field combine_words boolean
---@field default_animation string ---@field default_animation string
---@field default_texture string
---@field node_prefab node ---@field node_prefab node
---@field text_prefab node ---@field text_prefab node
---@field is_multiline boolean
---@class GUITextMetrics ---@class GUITextMetrics
---@field width number ---@field width number

View File

@ -351,7 +351,7 @@ function Scroll.set_size(self, size, offset)
end end
--- Set scroll view size. --- Set new scroll view size in case the node size was changed.
-- @tparam Scroll self @{Scroll} -- @tparam Scroll self @{Scroll}
-- @tparam vector3 size The new size for view node -- @tparam vector3 size The new size for view node
-- @treturn druid.scroll Current scroll instance -- @treturn druid.scroll Current scroll instance

View File

@ -71,10 +71,10 @@ function M.length(text)
end end
---@param word rich_text.word ---@param word druid.rich_text.word
---@param previous_word rich_text.word|nil ---@param previous_word druid.rich_text.word|nil
---@param settings rich_text.settings ---@param settings druid.rich_text.settings
---@return rich_text.metrics ---@return druid.rich_text.metrics
local function get_text_metrics(word, previous_word, settings) local function get_text_metrics(word, previous_word, settings)
local text = word.text local text = word.text
local font_resource = gui.get_font_resource(word.font) local font_resource = gui.get_font_resource(word.font)
@ -117,7 +117,8 @@ end
---@return druid.rich_text.metrics ---@return druid.rich_text.metrics
local function get_image_metrics(word, settings) local function get_image_metrics(word, settings)
local node_prefab = settings.node_prefab local node_prefab = settings.node_prefab
gui.play_flipbook(node_prefab, word.image.anim) gui.set_texture(node_prefab, word.image.texture or settings.default_texture)
gui.play_flipbook(node_prefab, word.image.anim or settings.default_animation)
local node_size = gui.get_size(node_prefab) local node_size = gui.get_size(node_prefab)
local aspect = node_size.x / node_size.y local aspect = node_size.x / node_size.y
node_size.x = word.image.width or node_size.x node_size.x = word.image.width or node_size.x
@ -145,8 +146,9 @@ end
--- @param text string The text to create rich text nodes from --- @param text string The text to create rich text nodes from
--- @param settings table Optional settings table (refer to documentation for details) --- @param settings table Optional settings table (refer to documentation for details)
--- @param style druid.rich_text.style --- @param style druid.rich_text.style
--- @return words --- @return druid.rich_text.word[]
--- @return metrics --- @return druid.rich_text.settings
--- @return druid.rich_text.lines_metrics
function M.create(text, settings, style) function M.create(text, settings, style)
assert(text, "You must provide a text") assert(text, "You must provide a text")
@ -171,10 +173,9 @@ function M.create(text, settings, style)
outline = settings.outline, outline = settings.outline,
font = font, font = font,
-- Image params -- Image params
---@type rich_text.word.image ---@type druid.rich_text.image
image = nil, image = nil,
image_color = gui.get_color(settings.node_prefab), image_color = gui.get_color(settings.node_prefab),
default_animation = nil,
-- Tags -- Tags
br = nil, br = nil,
nobr = nil, nobr = nil,
@ -237,9 +238,6 @@ function M._split_on_lines(words, settings)
if word == nil then if word == nil then
break break
end end
if word.image then
word.default_animation = settings.default_animation
end
-- Reset texts to start measure again -- Reset texts to start measure again
word.text = word.source_text word.text = word.source_text
@ -308,9 +306,9 @@ function M._split_on_lines(words, settings)
end end
---@param lines rich_text.word[][] ---@param lines druid.rich_text.word[][]
---@param settings rich_text.settings ---@param settings druid.rich_text.settings
---@return rich_text.lines_metrics ---@return druid.rich_text.lines_metrics
function M._position_lines(lines, settings) function M._position_lines(lines, settings)
local lines_metrics = M._get_lines_metrics(lines, settings) local lines_metrics = M._get_lines_metrics(lines, settings)
-- current x-y is left top point of text spawn -- current x-y is left top point of text spawn
@ -355,9 +353,9 @@ function M._position_lines(lines, settings)
end end
---@param lines rich_text.word[][] ---@param lines druid.rich_text.word[][]
---@param settings rich_text.settings ---@param settings druid.rich_text.settings
---@return rich_text.lines_metrics ---@return druid.rich_text.lines_metrics
function M._get_lines_metrics(lines, settings) function M._get_lines_metrics(lines, settings)
local metrics = {} local metrics = {}
local text_width = 0 local text_width = 0
@ -389,7 +387,7 @@ function M._get_lines_metrics(lines, settings)
} }
end end
---@type rich_text.lines_metrics ---@type druid.rich_text.lines_metrics
local lines_metrics = { local lines_metrics = {
text_width = text_width, text_width = text_width,
text_height = text_height, text_height = text_height,
@ -400,8 +398,8 @@ function M._get_lines_metrics(lines, settings)
end end
---@param lines rich_text.word[][] ---@param lines druid.rich_text.word[][]
---@param settings rich_text.settings ---@param settings druid.rich_text.settings
function M._update_nodes(lines, settings) function M._update_nodes(lines, settings)
for line_index = 1, #lines do for line_index = 1, #lines do
local line = lines[line_index] local line = lines[line_index]
@ -411,7 +409,8 @@ function M._update_nodes(lines, settings)
if word.image then if word.image then
node = word.node or gui.clone(settings.node_prefab) node = word.node or gui.clone(settings.node_prefab)
gui.set_size_mode(node, gui.SIZE_MODE_MANUAL) gui.set_size_mode(node, gui.SIZE_MODE_MANUAL)
gui.play_flipbook(node, hash(word.image.anim or word.default_animation)) gui.set_texture(node, word.image.texture or settings.default_texture)
gui.play_flipbook(node, hash(word.image.anim or settings.default_animation))
gui.set_color(node, word.color or word.image_color) gui.set_color(node, word.color or word.image_color)
else else
node = word.node or gui.clone(settings.text_prefab) node = word.node or gui.clone(settings.text_prefab)
@ -432,10 +431,10 @@ function M._update_nodes(lines, settings)
end end
---@param words rich_text.word[] ---@param words druid.rich_text.word[]
---@param settings rich_text.settings ---@param settings druid.rich_text.settings
---@param scale number ---@param scale number
---@return rich_text.lines_metrics ---@return druid.rich_text.lines_metrics
function M.set_text_scale(words, settings, scale) function M.set_text_scale(words, settings, scale)
settings.adjust_scale = scale settings.adjust_scale = scale
@ -499,15 +498,15 @@ function M.adjust_to_area(words, settings, lines_metrics, style)
end end
---@return boolean @If we fit into area size ---@return druid.rich_text.word[][] lines
function M.apply_scale_without_update(words, settings, scale) function M.apply_scale_without_update(words, settings, scale)
settings.adjust_scale = scale settings.adjust_scale = scale
return M._split_on_lines(words, settings) return M._split_on_lines(words, settings)
end end
---@param lines rich_text.word[][] ---@param lines druid.rich_text.word[][]
---@param settings rich_text.settings ---@param settings druid.rich_text.settings
function M.is_fit_info_area(lines, settings) function M.is_fit_info_area(lines, settings)
local lines_metrics = M._get_lines_metrics(lines, settings) local lines_metrics = M._get_lines_metrics(lines, settings)
local area_size = gui.get_size(settings.parent) local area_size = gui.get_size(settings.parent)

View File

@ -149,7 +149,7 @@ function M.parse(text, default_settings, style)
end end
-- parse the tag, split into name and optional parameters -- parse the tag, split into name and optional parameters
local endtag, name, params, empty = tag:match("<(/?)(%a+)=?(%S-)(/?)>") local endtag, name, params, empty = tag:match("<(/?)([%a_]+)=?(%S-)(/?)>")
local is_endtag = endtag == "/" local is_endtag = endtag == "/"
local is_empty = empty == "/" local is_empty = empty == "/"

View File

@ -210,7 +210,15 @@ function RichText.set_text(self, text)
end end
function RichText.get_text(self)
return self._last_value
end
function RichText:on_remove() function RichText:on_remove()
pcall(gui.set_texture, self.icon_prefab, self._settings.default_texture)
pcall(gui.play_flipbook, self.icon_prefab, self._settings.default_animation)
self:clear() self:clear()
end end
@ -238,6 +246,15 @@ function RichText.tagged(self, tag)
end end
---Split a word into it's characters
-- @tparam RichText self @{RichText}
-- @tparam druid.rich_text.word word
-- @treturn druid.rich_text.word[] characters
function RichText.characters(self, word)
return rich_text.characters(word)
end
--- Get all current words. --- Get all current words.
-- @treturn table druid.rich_text.word[] -- @treturn table druid.rich_text.word[]
function RichText:get_words() function RichText:get_words()
@ -277,6 +294,7 @@ function RichText:_create_settings()
node_scale = gui.get_scale(self.icon_prefab), node_scale = gui.get_scale(self.icon_prefab),
image_scale = gui.get_scale(self.icon_prefab), image_scale = gui.get_scale(self.icon_prefab),
default_animation = gui.get_flipbook(self.icon_prefab), default_animation = gui.get_flipbook(self.icon_prefab),
default_texture = gui.get_texture(self.icon_prefab),
} }
end end