diff --git a/druid/custom/rich_text/rich_text.lua b/druid/custom/rich_text/rich_text.lua index e9da496..64472cb 100644 --- a/druid/custom/rich_text/rich_text.lua +++ b/druid/custom/rich_text/rich_text.lua @@ -64,16 +64,25 @@ end function RichText:_get_settings() return { - width = self.root_size.x, + adjust_scale = 1, parent = self.root, + width = self.root_size.x, + height = self.root_size.y, text_prefab = self.text_prefab, node_prefab = self.icon_prefab, + + size = gui.get_scale(self.text_prefab).x, shadow = gui.get_shadow(self.text_prefab), outline = gui.get_outline(self.text_prefab), - size = gui.get_scale(self.text_prefab).x, + text_scale = gui.get_scale(self.text_prefab), + text_leading = gui.get_leading(self.text_prefab), + is_multiline = gui.get_line_break(self.text_prefab), + + combine_words = false, + image_pixel_grid_snap = false, + node_scale = gui.get_scale(self.icon_prefab), image_scale = gui.get_scale(self.icon_prefab), default_animation = gui.get_flipbook(self.icon_prefab), - --combine_words = true, } end diff --git a/druid/custom/rich_text/rich_text/richtext.lua b/druid/custom/rich_text/rich_text/richtext.lua index 31206f4..a2e14d9 100755 --- a/druid/custom/rich_text/rich_text/richtext.lua +++ b/druid/custom/rich_text/rich_text/richtext.lua @@ -54,7 +54,6 @@ local V3_ZERO = vmath.vector3(0) ---@field shadow vector4 ---@field outline vector4 ---@field position vector3 ----@field line_spacing number ---@field image_pixel_grid_snap boolean ---@field combine_words boolean ---@field default_animation string @@ -146,15 +145,11 @@ local function get_text_metrics(word, previous_word, settings) local previous_word_metrics = resource.get_text_metrics(font_resource, previous_word.text) local union_metrics = resource.get_text_metrics(font_resource, previous_word.text .. text) - print("prev word metrics", previous_word_metrics.width, previous_word.text) - print("union", union_metrics.width, previous_word.text .. text) - print("current width", metrics.width, text) local without_previous_width = metrics.width metrics.width = (union_metrics.width - previous_word_metrics.width) * word_scale_x -- Since the several characters can be ajusted to fit the space between the previous word and this word -- For example: chars: [.,?!] metrics.offset_x = metrics.width - without_previous_width - print("with prev word offset", metrics.offset_x, previous_word.text, text) end end @@ -202,22 +197,6 @@ end function M.create(text, settings) assert(text, "You must provide a text") - ---@class rich_text.settings - settings = settings or {} - settings.adjust_scale = 1 - settings.position = settings.position or V3_ZERO - settings.line_spacing = settings.line_spacing or 1 - settings.image_pixel_grid_snap = settings.image_pixel_grid_snap or false - settings.combine_words = settings.combine_words or false - settings.default_animation = settings.default_animation or nil - settings.node_prefab = settings.node_prefab - settings.text_prefab = settings.text_prefab - settings.text_leading = gui.get_leading(settings.text_prefab) - settings.text_scale = gui.get_scale(settings.text_prefab) - settings.node_scale = gui.get_scale(settings.node_prefab) - settings.is_multiline = gui.get_line_break(settings.text_prefab) - settings.parent = settings.parent - -- default settings for a word -- will be assigned to each word unless tags override the values local font = gui.get_font(settings.text_prefab) @@ -518,18 +497,17 @@ end function M.adjust_to_area(words, settings, lines_metrics) local last_line_metrics = lines_metrics - local area_size = gui.get_size(settings.parent) if not settings.is_multiline then - if lines_metrics.text_width > area_size.x then - last_line_metrics = M.set_text_scale(words, settings, area_size.x / lines_metrics.text_width) + if lines_metrics.text_width > settings.width then + last_line_metrics = M.set_text_scale(words, settings, settings.width / lines_metrics.text_width) end else -- Multiline adjusting is very tricky stuff... -- It's do a lot of calculations, beware! - if lines_metrics.text_width > area_size.x or lines_metrics.text_height > area_size.y then - local scale_koef = math.sqrt(area_size.y / lines_metrics.text_height) - if lines_metrics.text_width * scale_koef > area_size.x then - scale_koef = math.sqrt(area_size.x / lines_metrics.text_width) + 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 + scale_koef = math.sqrt(settings.width / lines_metrics.text_width) end local adjust_scale = math.min(scale_koef, 1) diff --git a/example/examples/custom/rich_text/rich_text.gui b/example/examples/custom/rich_text/rich_text.gui index 91071c8..a2b0f13 100644 --- a/example/examples/custom/rich_text/rich_text.gui +++ b/example/examples/custom/rich_text/rich_text.gui @@ -2599,7 +2599,7 @@ nodes { id: "rich_text_5_NW/icon_prefab" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER + pivot: PIVOT_N adjust_mode: ADJUST_MODE_FIT parent: "rich_text_5_NW/root" layer: "" @@ -2615,6 +2615,7 @@ nodes { clipping_inverted: false alpha: 1.0 overridden_fields: 3 + overridden_fields: 14 template_node_child: true size_mode: SIZE_MODE_AUTO custom_type: 0 @@ -2893,7 +2894,7 @@ nodes { id: "rich_text_5_N/icon_prefab" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER + pivot: PIVOT_N adjust_mode: ADJUST_MODE_FIT parent: "rich_text_5_N/root" layer: "" @@ -2909,6 +2910,7 @@ nodes { clipping_inverted: false alpha: 1.0 overridden_fields: 3 + overridden_fields: 14 template_node_child: true size_mode: SIZE_MODE_AUTO custom_type: 0 @@ -3187,7 +3189,7 @@ nodes { id: "rich_text_5_NE/icon_prefab" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER + pivot: PIVOT_N adjust_mode: ADJUST_MODE_FIT parent: "rich_text_5_NE/root" layer: "" @@ -3203,6 +3205,7 @@ nodes { clipping_inverted: false alpha: 1.0 overridden_fields: 3 + overridden_fields: 14 template_node_child: true size_mode: SIZE_MODE_AUTO custom_type: 0 @@ -4362,7 +4365,7 @@ nodes { id: "rich_text_5_SW/icon_prefab" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER + pivot: PIVOT_S adjust_mode: ADJUST_MODE_FIT parent: "rich_text_5_SW/root" layer: "" @@ -4378,6 +4381,7 @@ nodes { clipping_inverted: false alpha: 1.0 overridden_fields: 3 + overridden_fields: 14 template_node_child: true size_mode: SIZE_MODE_AUTO custom_type: 0 @@ -4656,7 +4660,7 @@ nodes { id: "rich_text_5_S/icon_prefab" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER + pivot: PIVOT_S adjust_mode: ADJUST_MODE_FIT parent: "rich_text_5_S/root" layer: "" @@ -4672,6 +4676,7 @@ nodes { clipping_inverted: false alpha: 1.0 overridden_fields: 3 + overridden_fields: 14 template_node_child: true size_mode: SIZE_MODE_AUTO custom_type: 0 @@ -4950,7 +4955,7 @@ nodes { id: "rich_text_5_SE/icon_prefab" xanchor: XANCHOR_NONE yanchor: YANCHOR_NONE - pivot: PIVOT_CENTER + pivot: PIVOT_S adjust_mode: ADJUST_MODE_FIT parent: "rich_text_5_SE/root" layer: "" @@ -4966,6 +4971,7 @@ nodes { clipping_inverted: false alpha: 1.0 overridden_fields: 3 + overridden_fields: 14 template_node_child: true size_mode: SIZE_MODE_AUTO custom_type: 0 diff --git a/example/examples/custom/rich_text/rich_text.gui_script b/example/examples/custom/rich_text/rich_text.gui_script index 89211f9..b548d16 100644 --- a/example/examples/custom/rich_text/rich_text.gui_script +++ b/example/examples/custom/rich_text/rich_text.gui_script @@ -48,15 +48,16 @@ function init(self) self.druid:new(RichText, "rich_text_4_2"):set_text("Text with image scaled in GUI") self.druid:new(RichText, "rich_text_4_3"):set_text("Text with image with fixed height") - self.druid:new(RichText, "rich_text_5_NW"):set_text("Example text for pivots ") - self.druid:new(RichText, "rich_text_5_N"):set_text("Example text for pivots ") - self.druid:new(RichText, "rich_text_5_NE"):set_text("Example text for pivots ") - self.druid:new(RichText, "rich_text_5_W"):set_text("Example text for pivots ") - self.druid:new(RichText, "rich_text_5_C"):set_text("Example text for pivots ") - self.druid:new(RichText, "rich_text_5_E"):set_text("Example text for pivots ") - self.druid:new(RichText, "rich_text_5_SW"):set_text("Example text for pivots ") - self.druid:new(RichText, "rich_text_5_S"):set_text("Example text for pivots ") - self.druid:new(RichText, "rich_text_5_SE"):set_text("Example text for pivots ") + -- Docs: For images vertical anchor takes from ImagePrefab + self.druid:new(RichText, "rich_text_5_NW"):set_text("Example text for pivots ") + self.druid:new(RichText, "rich_text_5_N"):set_text("Example text for pivots ") + self.druid:new(RichText, "rich_text_5_NE"):set_text("Example text for pivots ") + self.druid:new(RichText, "rich_text_5_W"):set_text("Example text for pivots ") + self.druid:new(RichText, "rich_text_5_C"):set_text("Example text for pivots ") + self.druid:new(RichText, "rich_text_5_E"):set_text("Example text for pivots ") + self.druid:new(RichText, "rich_text_5_SW"):set_text("Example text for pivots ") + self.druid:new(RichText, "rich_text_5_S"):set_text("Example text for pivots ") + self.druid:new(RichText, "rich_text_5_SE"):set_text("Example text for pivots ") --self.rich_text_3 = self.druid:new(RichText, "rich_text_3") --local words3 = self.rich_text_3:set_text("Energy is full. To restore")