Rich text based only on text node

This commit is contained in:
Insality 2024-10-14 22:13:57 +03:00
parent 59d3b116ac
commit 5d5fe7f374
2 changed files with 20 additions and 30 deletions

View File

@ -117,8 +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.set_texture(node_prefab, word.image.texture or settings.default_texture) gui.set_texture(node_prefab, word.image.texture)
gui.play_flipbook(node_prefab, word.image.anim or settings.default_animation) gui.play_flipbook(node_prefab, word.image.anim)
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
@ -175,7 +175,7 @@ function M.create(text, settings, style)
-- Image params -- Image params
---@type druid.rich_text.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),
-- Tags -- Tags
br = nil, br = nil,
nobr = nil, nobr = nil,
@ -204,8 +204,8 @@ function M._fill_properties(word, metrics, settings)
if word.image then if word.image then
-- Image properties -- Image properties
word.scale = gui.get_scale(settings.node_prefab) * word.relative_scale * settings.adjust_scale word.scale = vmath.vector3(word.relative_scale * settings.adjust_scale)
word.pivot = gui.get_pivot(settings.node_prefab) word.pivot = gui.PIVOT_CENTER
word.size = metrics.node_size word.size = metrics.node_size
word.offset = vmath.vector3(0, 0, 0) word.offset = vmath.vector3(0, 0, 0)
if word.image.width then if word.image.width then
@ -407,7 +407,7 @@ function M._update_nodes(lines, settings)
local word = line[word_index] local word = line[word_index]
local node local node
if word.image then if word.image then
node = word.node or gui.clone(settings.node_prefab) node = word.node or gui.new_box_node(vmath.vector3(0), word.size)
gui.set_size_mode(node, gui.SIZE_MODE_MANUAL) gui.set_size_mode(node, gui.SIZE_MODE_MANUAL)
gui.set_texture(node, word.image.texture or settings.default_texture) gui.set_texture(node, word.image.texture or settings.default_texture)
gui.play_flipbook(node, hash(word.image.anim or settings.default_animation)) gui.play_flipbook(node, hash(word.image.anim or settings.default_animation))

View File

@ -106,29 +106,23 @@ local rich_text = require("druid.custom.rich_text.module.rt")
local RichText = component.create("rich_text") local RichText = component.create("rich_text")
local SCHEME = {
ROOT = "root",
TEXT_PREFAB = "text_prefab",
ICON_PREFAB = "icon_prefab"
}
--- The @{RichText} constructor --- The @{RichText} constructor
-- @tparam RichText self @{RichText} -- @tparam RichText self @{RichText}
-- @tparam string template The Rich Text template name -- @tparam string template The Rich Text template name
-- @tparam table nodes The node table, if prefab was copied by gui.clone_tree() -- @tparam table nodes The node table, if prefab was copied by gui.clone_tree()
function RichText.init(self, template, nodes) function RichText.init(self, text_node, value)
self.druid = self:get_druid(template, nodes) self.root = self:get_node(text_node)
self.text_prefab = self.root
self.root = self:get_node(SCHEME.ROOT)
self.text_prefab = self:get_node(SCHEME.TEXT_PREFAB)
self.icon_prefab = self:get_node(SCHEME.ICON_PREFAB)
gui.set_enabled(self.text_prefab, false)
gui.set_enabled(self.icon_prefab, false)
self._last_value = value or gui.get_text(self.text_prefab)
gui.set_text(self.root, "")
self._settings = self:_create_settings() self._settings = self:_create_settings()
if value then
self:set_text(value)
end
end end
@ -228,9 +222,6 @@ 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
@ -292,7 +283,6 @@ function RichText:_create_settings()
height = root_size.y, height = root_size.y,
combine_words = false, -- disabled now combine_words = false, -- disabled now
text_prefab = self.text_prefab, text_prefab = self.text_prefab,
node_prefab = self.icon_prefab,
-- Text Settings -- Text Settings
shadow = gui.get_shadow(self.text_prefab), shadow = gui.get_shadow(self.text_prefab),
@ -303,10 +293,10 @@ function RichText:_create_settings()
-- Image settings -- Image settings
image_pixel_grid_snap = false, -- disabled now image_pixel_grid_snap = false, -- disabled now
node_scale = gui.get_scale(self.icon_prefab), node_scale = gui.get_scale(self.text_prefab),
image_scale = gui.get_scale(self.icon_prefab), image_scale = gui.get_scale(self.text_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), --default_texture = gui.get_texture(self.icon_prefab),
} }
end end