Update rich text

This commit is contained in:
Insality 2024-10-15 00:56:06 +03:00
parent 66f4a1c8d5
commit 87f52551e2
4 changed files with 17 additions and 165 deletions

View File

@ -1997,6 +1997,7 @@ function helper.table_to_string(t) end
---@field parent node
---@field size number
---@field fonts table<string, string>
---@field scale vector3
---@field color vector4
---@field shadow vector4
---@field outline vector4
@ -2005,10 +2006,8 @@ function helper.table_to_string(t) end
---@field combine_words boolean
---@field default_animation string
---@field text_prefab node
---@field text_scale vector3
---@field adjust_scale number
---@field default_texture string
---@field node_scale vector3
---@field is_multiline boolean
---@field text_leading number
---@field font hash

View File

@ -12,6 +12,7 @@ local utf8_lua = require("druid.system.utf8")
local utf8 = utf8 or utf8_lua
local VECTOR_ZERO = vmath.vector3(0)
local VECTOR_ONE = vmath.vector3(1)
local COLOR_WHITE = vmath.vector4(1)
local M = {}
@ -84,8 +85,8 @@ local function get_text_metrics(word, previous_word, settings)
---@type druid.rich_text.metrics
local metrics
local word_scale_x = word.relative_scale * settings.text_scale.x * settings.adjust_scale
local word_scale_y = word.relative_scale * settings.text_scale.y * settings.adjust_scale
local word_scale_x = word.relative_scale * settings.scale.x * settings.adjust_scale
local word_scale_y = word.relative_scale * settings.scale.y * settings.adjust_scale
if utf8.len(text) == 0 then
metrics = resource.get_text_metrics(font_resource, "|")
@ -128,8 +129,8 @@ local function get_image_metrics(word, settings)
node_size.y = word.image.height or (node_size.x / aspect)
return {
width = node_size.x * word.relative_scale * settings.node_scale.x * settings.adjust_scale,
height = node_size.y * word.relative_scale * settings.node_scale.y * settings.adjust_scale,
width = node_size.x * word.relative_scale * settings.scale.x * settings.adjust_scale,
height = node_size.y * word.relative_scale * settings.scale.y * settings.adjust_scale,
node_size = node_size,
}
end
@ -225,7 +226,7 @@ function M._fill_properties(word, metrics, settings)
end
else
-- Text properties
word.scale = gui.get_scale(settings.text_prefab) * word.relative_scale * settings.adjust_scale
word.scale = settings.scale * word.relative_scale * settings.adjust_scale
word.pivot = gui.PIVOT_W -- With this pivot adjustments works correctly, but with another some misalignment
word.size = vmath.vector3(metrics.width, metrics.height, 0)
word.offset = vmath.vector3(metrics.offset_x, metrics.offset_y, 0)
@ -339,7 +340,7 @@ function M._position_lines(lines, settings)
local pivot_offset = helper.get_pivot_offset(word.pivot)
local word_width = word.metrics.width
word.position.x = current_x + word_width * (pivot_offset.x + 0.5) + word.offset.x
word.position.y = current_y + word.offset.y
word.position.y = current_y + word.metrics.height * (pivot_offset.y - 0.5) + word.offset.y
-- Align item on text line depends on anchor
word.position.y = word.position.y - (word.metrics.height - line_metrics.height) * (pivot_offset.y - 0.5)
@ -477,7 +478,7 @@ function M.adjust_to_area(words, settings, lines_metrics, style)
if lines_metrics.text_width * scale_koef > settings.width then
scale_koef = math.sqrt(settings.width / lines_metrics.text_width)
end
local adjust_scale = math.min(scale_koef, 1)
local adjust_scale = math.min(scale_koef, settings.scale.x)
local lines = M.apply_scale_without_update(words, settings, adjust_scale)
local is_fit = M.is_fit_info_area(lines, settings)

View File

@ -1,4 +1,3 @@
script: ""
fonts {
name: "game"
font: "/example/assets/fonts/game.font"
@ -7,193 +6,40 @@ textures {
name: "items"
texture: "/example/assets/images/kenney.atlas"
}
background_color {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 400.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "root"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_MANUAL
custom_type: 0
enabled: true
visible: false
}
nodes {
position {
x: -200.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 400.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "Rich text"
font: "game"
id: "text_prefab"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_W
outline {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
shadow {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "root"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 0.0
shadow_alpha: 0.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
custom_type: 0
enabled: true
visible: true
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 21.0
y: 20.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: "items/checkmark"
id: "icon_prefab"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
parent: "root"
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_AUTO
custom_type: 0
enabled: true
visible: true
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512

View File

@ -273,11 +273,19 @@ end
function RichText:_create_settings()
local root_size = gui.get_size(self.root)
local scale = gui.get_scale(self.root)
root_size.x = root_size.x * scale.x
root_size.y = root_size.y * scale.y
gui.set_size(self.root, root_size)
gui.set_scale(self.root, vmath.vector3(1))
return {
-- General settings
-- Adjust scale using to fit the text to the root node area
adjust_scale = 1,
parent = self.root,
scale = scale,
width = root_size.x,
height = root_size.y,
combine_words = false, -- disabled now
@ -287,13 +295,11 @@ function RichText:_create_settings()
-- Text Settings
shadow = gui.get_shadow(self.root),
outline = gui.get_outline(self.root),
text_scale = gui.get_scale(self.root),
text_leading = gui.get_leading(self.root),
is_multiline = gui.get_line_break(self.root),
-- Image settings
image_pixel_grid_snap = false, -- disabled now
node_scale = gui.get_scale(self.root),
}
end