Skip to content

Commit

Permalink
Update annotations, fix rare errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Oct 4, 2024
1 parent b9b7473 commit d39f471
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
9 changes: 8 additions & 1 deletion druid/annotations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1846,19 +1846,24 @@ function helper.table_to_string(t) end
---@class druid.rich_text.word
---@field node node
---@field relative_scale number
---@field adjust_scale number
---@field color vector4
---@field position vector3
---@field offset vector3
---@field scale vector3
---@field size vector3
---@field metrics druid.rich_text.metrics
---@field pivot number @ The gui.PIVOT_* constant
---@field pivot userdata @ The gui.PIVOT_* constant
---@field text string
---@field source_text string
---@field text_color vector4
---@field shadow vector4
---@field outline vector4
---@field font string
---@field image druid.rich_text.image
---@field image_color vector4
---@field default_animation string
---@field default_texture string
---@field anchor number
---@field br boolean
---@field nobr boolean
Expand All @@ -1880,8 +1885,10 @@ function helper.table_to_string(t) end
---@field image_pixel_grid_snap boolean
---@field combine_words boolean
---@field default_animation string
---@field default_texture string
---@field node_prefab node
---@field text_prefab node
---@field is_multiline boolean

---@class GUITextMetrics
---@field width number
Expand Down
2 changes: 1 addition & 1 deletion druid/base/scroll.lua
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ function Scroll.set_size(self, size, offset)
end


--- Set scroll view size.
--- Set new scroll view size in case the node size was changed.
-- @tparam Scroll self @{Scroll}
-- @tparam vector3 size The new size for view node
-- @treturn druid.scroll Current scroll instance
Expand Down
55 changes: 27 additions & 28 deletions druid/custom/rich_text/module/rt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ function M.length(text)
end


---@param word rich_text.word
---@param previous_word rich_text.word|nil
---@param settings rich_text.settings
---@return rich_text.metrics
---@param word druid.rich_text.word
---@param previous_word druid.rich_text.word|nil
---@param settings druid.rich_text.settings
---@return druid.rich_text.metrics
local function get_text_metrics(word, previous_word, settings)
local text = word.text
local font_resource = gui.get_font_resource(word.font)
Expand Down Expand Up @@ -117,7 +117,8 @@ end
---@return druid.rich_text.metrics
local function get_image_metrics(word, settings)
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 aspect = node_size.x / node_size.y
node_size.x = word.image.width or node_size.x
Expand Down Expand Up @@ -145,8 +146,9 @@ end
--- @param text string The text to create rich text nodes from
--- @param settings table Optional settings table (refer to documentation for details)
--- @param style druid.rich_text.style
--- @return words
--- @return metrics
--- @return druid.rich_text.word[]
--- @return druid.rich_text.settings
--- @return druid.rich_text.lines_metrics
function M.create(text, settings, style)
assert(text, "You must provide a text")

Expand All @@ -171,10 +173,9 @@ function M.create(text, settings, style)
outline = settings.outline,
font = font,
-- Image params
---@type rich_text.word.image
---@type druid.rich_text.image
image = nil,
image_color = gui.get_color(settings.node_prefab),
default_animation = nil,
-- Tags
br = nil,
nobr = nil,
Expand Down Expand Up @@ -237,9 +238,6 @@ function M._split_on_lines(words, settings)
if word == nil then
break
end
if word.image then
word.default_animation = settings.default_animation
end

-- Reset texts to start measure again
word.text = word.source_text
Expand Down Expand Up @@ -308,9 +306,9 @@ function M._split_on_lines(words, settings)
end


---@param lines rich_text.word[][]
---@param settings rich_text.settings
---@return rich_text.lines_metrics
---@param lines druid.rich_text.word[][]
---@param settings druid.rich_text.settings
---@return druid.rich_text.lines_metrics
function M._position_lines(lines, settings)
local lines_metrics = M._get_lines_metrics(lines, settings)
-- current x-y is left top point of text spawn
Expand Down Expand Up @@ -355,9 +353,9 @@ function M._position_lines(lines, settings)
end


---@param lines rich_text.word[][]
---@param settings rich_text.settings
---@return rich_text.lines_metrics
---@param lines druid.rich_text.word[][]
---@param settings druid.rich_text.settings
---@return druid.rich_text.lines_metrics
function M._get_lines_metrics(lines, settings)
local metrics = {}
local text_width = 0
Expand Down Expand Up @@ -389,7 +387,7 @@ function M._get_lines_metrics(lines, settings)
}
end

---@type rich_text.lines_metrics
---@type druid.rich_text.lines_metrics
local lines_metrics = {
text_width = text_width,
text_height = text_height,
Expand All @@ -400,8 +398,8 @@ function M._get_lines_metrics(lines, settings)
end


---@param lines rich_text.word[][]
---@param settings rich_text.settings
---@param lines druid.rich_text.word[][]
---@param settings druid.rich_text.settings
function M._update_nodes(lines, settings)
for line_index = 1, #lines do
local line = lines[line_index]
Expand All @@ -411,7 +409,8 @@ function M._update_nodes(lines, settings)
if word.image then
node = word.node or gui.clone(settings.node_prefab)
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)
else
node = word.node or gui.clone(settings.text_prefab)
Expand All @@ -432,10 +431,10 @@ function M._update_nodes(lines, settings)
end


---@param words rich_text.word[]
---@param settings rich_text.settings
---@param words druid.rich_text.word[]
---@param settings druid.rich_text.settings
---@param scale number
---@return rich_text.lines_metrics
---@return druid.rich_text.lines_metrics
function M.set_text_scale(words, settings, scale)
settings.adjust_scale = scale

Expand Down Expand Up @@ -499,15 +498,15 @@ function M.adjust_to_area(words, settings, lines_metrics, style)
end


---@return boolean @If we fit into area size
---@return druid.rich_text.word[][] lines
function M.apply_scale_without_update(words, settings, scale)
settings.adjust_scale = scale
return M._split_on_lines(words, settings)
end


---@param lines rich_text.word[][]
---@param settings rich_text.settings
---@param lines druid.rich_text.word[][]
---@param settings druid.rich_text.settings
function M.is_fit_info_area(lines, settings)
local lines_metrics = M._get_lines_metrics(lines, settings)
local area_size = gui.get_size(settings.parent)
Expand Down
2 changes: 1 addition & 1 deletion druid/custom/rich_text/module/rt_parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function M.parse(text, default_settings, style)
end

-- 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_empty = empty == "/"
Expand Down
18 changes: 18 additions & 0 deletions druid/custom/rich_text/rich_text.lua
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,15 @@ function RichText.set_text(self, text)
end


function RichText.get_text(self)
return self._last_value
end


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()
end

Expand Down Expand Up @@ -238,6 +246,15 @@ function RichText.tagged(self, tag)
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.
-- @treturn table druid.rich_text.word[]
function RichText:get_words()
Expand Down Expand Up @@ -277,6 +294,7 @@ function RichText:_create_settings()
node_scale = gui.get_scale(self.icon_prefab),
image_scale = gui.get_scale(self.icon_prefab),
default_animation = gui.get_flipbook(self.icon_prefab),
default_texture = gui.get_texture(self.icon_prefab),
}
end

Expand Down

0 comments on commit d39f471

Please sign in to comment.