From 80ea0baf5d005d06d296ed8d88d12a0e583a0d5b Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Mon, 15 Jul 2024 14:14:03 +0100 Subject: [PATCH 1/2] block: Use RichTextLabel for tooltip By default, a plain-text label is used which does not support rich text formatting. Define a `Tooltip` scene; and override `Control._make_custom_tooltip()` in `Block` to return an instance of this scene. `Tooltip` is initially just a `RichTextLabel` with BBCode enabled and a custom minimum size set as suggested by the `_make_custom_tooltip()` documentation: > Note: The tooltip is shrunk to minimal size. If you want to ensure it's fully > visible, you might want to set its `custom_minimum_size` to some non-zero > value. We override the theme styles on the `RichTextLabel` to match the styles used in the built-in inspector's help tooltips. In particular, bold, italic and bold-italic text doesn't work without overriding these styles. The [BBCode in RichTextLabel][0] documentation claims: > If no custom bold or italic fonts are defined, faux bold and italic fonts > will be generated by Godot. but empirically this is not true. The Editor Theme Explorer plugin was useful, as was reading the editor source code, specifically `editor_fonts.cpp` and `editor_help.cpp`. None of the existing `tooltip_text` properties in the codebase use the `[` character that is used by BBCode tags, so there is no need to adjust the existing tooltips to keep displaying as they previously did. https://phabricator.endlessm.com/T35540 [0]: https://docs.godotengine.org/en/stable/tutorials/ui/bbcode_in_richtextlabel.html#reference --- addons/block_code/ui/blocks/block/block.gd | 6 ++++++ addons/block_code/ui/tooltip/tooltip.gd | 24 ++++++++++++++++++++++ addons/block_code/ui/tooltip/tooltip.tscn | 14 +++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 addons/block_code/ui/tooltip/tooltip.gd create mode 100644 addons/block_code/ui/tooltip/tooltip.tscn diff --git a/addons/block_code/ui/blocks/block/block.gd b/addons/block_code/ui/blocks/block/block.gd index dd7d4526..127e4bfa 100644 --- a/addons/block_code/ui/blocks/block/block.gd +++ b/addons/block_code/ui/blocks/block/block.gd @@ -74,3 +74,9 @@ func serialize_props(prop_names: Array) -> Array: for p in prop_names: pairs.append([p, self.get(p)]) return pairs + + +func _make_custom_tooltip(for_text) -> Control: + var tooltip = preload("res://addons/block_code/ui/tooltip/tooltip.tscn").instantiate() + tooltip.text = for_text + return tooltip diff --git a/addons/block_code/ui/tooltip/tooltip.gd b/addons/block_code/ui/tooltip/tooltip.gd new file mode 100644 index 00000000..a0e44f26 --- /dev/null +++ b/addons/block_code/ui/tooltip/tooltip.gd @@ -0,0 +1,24 @@ +@tool +class_name Tooltip +extends RichTextLabel +## Rich-text control for block tooltips that matches the built-in inspector's tooltips' font styles + + +func override_font(font_name: StringName, editor_font_name: StringName) -> Font: + var font = get_theme_font(editor_font_name, &"EditorFonts") + add_theme_font_override(font_name, font) + return font + + +func _ready(): + # Set fonts to match documentation tooltips in inspector + override_font(&"normal_font", &"doc") + override_font(&"mono_font", &"doc_source") + override_font(&"bold_font", &"doc_bold") + var italics = override_font(&"italics_font", &"doc_italic") + + # No doc_ style for bold italic; fake it by emboldening the italic style + var bold_italics = FontVariation.new() + bold_italics.set_base_font(italics) + bold_italics.set_variation_embolden(1.2) + add_theme_font_override(&"bold_italics_font", bold_italics) diff --git a/addons/block_code/ui/tooltip/tooltip.tscn b/addons/block_code/ui/tooltip/tooltip.tscn new file mode 100644 index 00000000..4caa4514 --- /dev/null +++ b/addons/block_code/ui/tooltip/tooltip.tscn @@ -0,0 +1,14 @@ +[gd_scene load_steps=2 format=3 uid="uid://bmxco03vqyq2t"] + +[ext_resource type="Script" path="res://addons/block_code/ui/tooltip/tooltip.gd" id="1_4ko6a"] + +[node name="Tooltip" type="RichTextLabel"] +custom_minimum_size = Vector2(360, 48) +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +bbcode_enabled = true +fit_content = true +script = ExtResource("1_4ko6a") From db819ee14c690e68e7b078df94de207ed9c6fb8b Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Mon, 15 Jul 2024 14:23:24 +0100 Subject: [PATCH 2/2] Add a couple of rich-text tooltips --- .../block_code/ui/picker/categories/category_factory.gd | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/addons/block_code/ui/picker/categories/category_factory.gd b/addons/block_code/ui/picker/categories/category_factory.gd index ba85a11e..90fa8f65 100644 --- a/addons/block_code/ui/picker/categories/category_factory.gd +++ b/addons/block_code/ui/picker/categories/category_factory.gd @@ -206,12 +206,21 @@ static func get_general_blocks() -> Array[Block]: b.block_formats = ["repeat {number: INT}"] b.statements = ["for i in {number}:"] b.category = "Loops" + b.tooltip_text = "Run the connected blocks [i]number[/i] times" block_list.append(b) b = BLOCKS["control_block"].instantiate() b.block_formats = ["while {condition: BOOL}"] b.statements = ["while {condition}:"] b.category = "Loops" + b.tooltip_text = ( + """ + Run the connected blocks as long as [i]condition[/i] is true. + + Hint: snap a [b]Comparison[/b] block into the condition. + """ + . dedent() + ) block_list.append(b) b = BLOCKS["statement_block"].instantiate()