mirror of
https://github.com/Insality/druid.git
synced 2025-09-27 18:12:19 +02:00
Update Rich text style params
This commit is contained in:
@@ -13,9 +13,6 @@ local utf8 = utf8 or utf8_lua
|
||||
|
||||
local M = {}
|
||||
|
||||
M.ADJUST_STEPS = 20
|
||||
M.ADJUST_SCALE_DELTA = 0.02
|
||||
|
||||
-- Trim spaces on string start
|
||||
local function ltrim(text)
|
||||
return text:match('^%s*(.*)')
|
||||
@@ -144,12 +141,13 @@ local function measure_node(word, settings, previous_word)
|
||||
end
|
||||
|
||||
|
||||
--- Create rich text gui nodes from text
|
||||
-- @param text The text to create rich text nodes from
|
||||
-- @param settings Optional settings table (refer to documentation for details)
|
||||
-- @return words
|
||||
-- @return metrics
|
||||
function M.create(text, settings)
|
||||
-- Create rich text gui nodes from text
|
||||
--- @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
|
||||
function M.create(text, settings, style)
|
||||
assert(text, "You must provide a text")
|
||||
|
||||
-- default settings for a word
|
||||
@@ -178,12 +176,11 @@ function M.create(text, settings)
|
||||
image_color = gui.get_color(settings.node_prefab),
|
||||
default_animation = nil,
|
||||
-- Tags
|
||||
anchor = nil,
|
||||
br = nil,
|
||||
nobr = nil,
|
||||
}
|
||||
|
||||
local parsed_words = parser.parse(text, word_params)
|
||||
local parsed_words = parser.parse(text, word_params, style)
|
||||
local lines = M._split_on_lines(parsed_words, settings)
|
||||
local lines_metrics = M._position_lines(lines, settings)
|
||||
M._update_nodes(lines, settings)
|
||||
@@ -450,7 +447,8 @@ end
|
||||
---@param words druid.rich_text.word[]
|
||||
---@param settings druid.rich_text.settings
|
||||
---@param lines_metrics druid.rich_text.lines_metrics
|
||||
function M.adjust_to_area(words, settings, lines_metrics)
|
||||
---@param style druid.rich_text.style
|
||||
function M.adjust_to_area(words, settings, lines_metrics, style)
|
||||
local last_line_metrics = lines_metrics
|
||||
|
||||
if not settings.is_multiline then
|
||||
@@ -459,7 +457,7 @@ function M.adjust_to_area(words, settings, lines_metrics)
|
||||
end
|
||||
else
|
||||
-- Multiline adjusting is very tricky stuff...
|
||||
-- It's do a lot of calculations, beware!
|
||||
-- It's doing a lot of calculations, beware!
|
||||
if lines_metrics.text_width > settings.width or lines_metrics.text_height > settings.height then
|
||||
local scale_koef = math.sqrt(settings.height / lines_metrics.text_height)
|
||||
if lines_metrics.text_width * scale_koef > settings.width then
|
||||
@@ -469,9 +467,9 @@ function M.adjust_to_area(words, settings, lines_metrics)
|
||||
|
||||
local lines = M.apply_scale_without_update(words, settings, adjust_scale)
|
||||
local is_fit = M.is_fit_info_area(lines, settings)
|
||||
local step = is_fit and M.ADJUST_SCALE_DELTA or -M.ADJUST_SCALE_DELTA
|
||||
local step = is_fit and style.ADJUST_SCALE_DELTA or -style.ADJUST_SCALE_DELTA
|
||||
|
||||
for i = 1, M.ADJUST_STEPS do
|
||||
for i = 1, style.ADJUST_STEPS do
|
||||
-- Grow down to check if we fit
|
||||
if step < 0 and is_fit then
|
||||
last_line_metrics = M.set_text_scale(words, settings, adjust_scale)
|
||||
@@ -487,7 +485,7 @@ function M.adjust_to_area(words, settings, lines_metrics)
|
||||
lines = M.apply_scale_without_update(words, settings, adjust_scale)
|
||||
is_fit = M.is_fit_info_area(lines, settings)
|
||||
|
||||
if i == M.ADJUST_STEPS then
|
||||
if i == style.ADJUST_STEPS then
|
||||
last_line_metrics = M.set_text_scale(words, settings, adjust_scale)
|
||||
end
|
||||
end
|
||||
|
@@ -3,44 +3,47 @@
|
||||
-- Modified by: Insality
|
||||
|
||||
local M = {}
|
||||
local cache = {}
|
||||
|
||||
function M.parse_hex(hex)
|
||||
if cache[hex] then
|
||||
return cache[hex]
|
||||
end
|
||||
|
||||
local r,g,b,a = hex:match("#?(%x%x)(%x%x)(%x%x)(%x?%x?)")
|
||||
if a == "" then a = "ff" end
|
||||
if r and g and b and a then
|
||||
return vmath.vector4(
|
||||
local color = vmath.vector4(
|
||||
tonumber(r, 16) / 255,
|
||||
tonumber(g, 16) / 255,
|
||||
tonumber(b, 16) / 255,
|
||||
tonumber(a, 16) / 255)
|
||||
tonumber(a, 16) / 255
|
||||
)
|
||||
|
||||
cache[hex] = color
|
||||
return color
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
function M.parse_decimal(dec)
|
||||
if cache[dec] then
|
||||
return cache[dec]
|
||||
end
|
||||
|
||||
local r,g,b,a = dec:match("(%d*%.?%d*),(%d*%.?%d*),(%d*%.?%d*),(%d*%.?%d*)")
|
||||
if r and g and b and a then
|
||||
return vmath.vector4(tonumber(r), tonumber(g), tonumber(b), tonumber(a))
|
||||
local color = vmath.vector4(tonumber(r), tonumber(g), tonumber(b), tonumber(a))
|
||||
cache[dec] = color
|
||||
return color
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
function M.add(name, color)
|
||||
if type(color) == "string" then
|
||||
color = M.parse_hex(color) or M.parse_decimal(color)
|
||||
end
|
||||
assert(type(color) == "userdata" and color.x and color.y and color.z and color.w, "Unable to add color")
|
||||
M.COLORS[name] = color
|
||||
end
|
||||
|
||||
|
||||
M.COLORS = {}
|
||||
|
||||
|
||||
function M.parse(c)
|
||||
return M.COLORS[c] or M.parse_hex(c) or M.parse_decimal(c)
|
||||
return M.parse_hex(c) or M.parse_decimal(c)
|
||||
end
|
||||
|
||||
|
||||
|
@@ -8,9 +8,9 @@ local utf8 = utf8 or utf8_lua
|
||||
|
||||
local M = {}
|
||||
|
||||
local function parse_tag(tag, params)
|
||||
local function parse_tag(tag, params, style)
|
||||
local settings = { tags = { [tag] = params }, tag = tag }
|
||||
if not tags.apply(tag, params, settings) then
|
||||
if not tags.apply(tag, params, settings, style) then
|
||||
settings[tag] = params
|
||||
end
|
||||
|
||||
@@ -110,8 +110,9 @@ end
|
||||
--- Parse the text into individual words
|
||||
-- @param text The text to parse
|
||||
-- @param default_settings Default settings for each word
|
||||
-- @param color_aliases Color aliases table
|
||||
-- @return List of all words
|
||||
function M.parse(text, default_settings)
|
||||
function M.parse(text, default_settings, style)
|
||||
assert(text)
|
||||
assert(default_settings)
|
||||
|
||||
@@ -151,12 +152,12 @@ function M.parse(text, default_settings)
|
||||
if is_empty then
|
||||
-- empty tag, ie tag without content
|
||||
-- example <br/> and <img=texture:image/>
|
||||
local empty_tag_settings = parse_tag(name, params)
|
||||
local empty_tag_settings = parse_tag(name, params, style)
|
||||
merge_tags(empty_tag_settings, word_settings)
|
||||
add_word("", empty_tag_settings, all_words)
|
||||
elseif not is_endtag then
|
||||
-- open tag - parse and add it
|
||||
local tag_settings = parse_tag(name, params)
|
||||
local tag_settings = parse_tag(name, params, style)
|
||||
open_tags[#open_tags + 1] = tag_settings
|
||||
else
|
||||
-- end tag - remove it from the list of open tags
|
||||
|
@@ -5,17 +5,16 @@
|
||||
local color = require("druid.custom.rich_text.module.rt_color")
|
||||
|
||||
local M = {}
|
||||
|
||||
local tags = {}
|
||||
|
||||
|
||||
function M.apply(tag, params, settings)
|
||||
function M.apply(tag, params, settings, style)
|
||||
local fn = tags[tag]
|
||||
if not fn then
|
||||
return false
|
||||
end
|
||||
|
||||
fn(params, settings)
|
||||
fn(params, settings, style)
|
||||
return true
|
||||
end
|
||||
|
||||
@@ -44,17 +43,20 @@ end
|
||||
-- Format: <color=[#]{HEX_VALUE}>{Text}</color>
|
||||
-- Format: <color={COLOR_NAME}>{Text}</color>
|
||||
-- Example: <color=FF0000>Rich Text</color>
|
||||
M.register("color", function(params, settings)
|
||||
M.register("color", function(params, settings, style)
|
||||
params = style.COLORS[params] or params
|
||||
settings.color = color.parse(params)
|
||||
end)
|
||||
|
||||
|
||||
M.register("shadow", function(params, settings)
|
||||
M.register("shadow", function(params, settings, style)
|
||||
params = style.COLORS[params] or params
|
||||
settings.shadow = color.parse(params)
|
||||
end)
|
||||
|
||||
|
||||
M.register("outline", function(params, settings)
|
||||
M.register("outline", function(params, settings, style)
|
||||
params = style.COLORS[params] or params
|
||||
settings.outline = color.parse(params)
|
||||
end)
|
||||
|
||||
@@ -69,11 +71,6 @@ M.register("size", function(params, settings)
|
||||
end)
|
||||
|
||||
|
||||
M.register("a", function(params, settings)
|
||||
settings.anchor = true
|
||||
end)
|
||||
|
||||
|
||||
-- Example: </br>
|
||||
M.register("br", function(params, settings)
|
||||
settings.br = true
|
||||
|
Reference in New Issue
Block a user