Skip to content

Commit 3fb2d3f

Browse files
committed
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 <https://github.com/YuriSizov/godot-editor-theme-explorer> 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
1 parent 296613e commit 3fb2d3f

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

addons/block_code/ui/blocks/block/block.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,8 @@ func serialize_props(prop_names: Array) -> Array:
7474
for p in prop_names:
7575
pairs.append([p, self.get(p)])
7676
return pairs
77+
78+
func _make_custom_tooltip(for_text) -> Control:
79+
var tooltip = preload("res://addons/block_code/ui/tooltip/tooltip.tscn").instantiate()
80+
tooltip.text = for_text
81+
return tooltip
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@tool
2+
class_name Tooltip
3+
extends RichTextLabel
4+
## Rich-text control for block tooltips that matches the built-in inspector's tooltips' font styles
5+
6+
func override_font(font_name: StringName, editor_font_name: StringName) -> Font:
7+
var font = get_theme_font(editor_font_name, &"EditorFonts")
8+
add_theme_font_override(font_name, font)
9+
return font
10+
11+
func _ready():
12+
# Set fonts to match documentation tooltips in inspector
13+
override_font(&"normal_font", &"doc")
14+
override_font(&"mono_font", &"doc_source")
15+
override_font(&"bold_font", &"doc_bold")
16+
var italics = override_font(&"italics_font", &"doc_italic")
17+
18+
# No doc_ style for bold italic; fake it by emboldening the italic style
19+
var bold_italics = FontVariation.new()
20+
bold_italics.set_base_font(italics)
21+
bold_italics.set_variation_embolden(1.2)
22+
add_theme_font_override(&"bold_italics_font", bold_italics)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://bmxco03vqyq2t"]
2+
3+
[ext_resource type="Script" path="res://addons/block_code/ui/tooltip/tooltip.gd" id="1_4ko6a"]
4+
5+
[node name="Tooltip" type="RichTextLabel"]
6+
custom_minimum_size = Vector2(360, 48)
7+
anchors_preset = 15
8+
anchor_right = 1.0
9+
anchor_bottom = 1.0
10+
grow_horizontal = 2
11+
grow_vertical = 2
12+
bbcode_enabled = true
13+
fit_content = true
14+
script = ExtResource("1_4ko6a")

0 commit comments

Comments
 (0)