Godot Engine – Multi-platform 2D and 3D game engine
Add admonition support to editor help and make_rst.py
Cyril B.0861f2868b3c
diff --git a/doc/tools/make_rst.py b/doc/tools/make_rst.pyindex d625434a56d..f784720f053 100755--- a/doc/tools/make_rst.py+++ b/doc/tools/make_rst.py@@ -89,7 +89,7 @@ BASE_STRINGS = ["This operator may be changed or removed in future versions.","This theme property may be changed or removed in future versions.",# See also `make_rst_class()` and `editor/doc/editor_help.cpp`.- "[b]Note:[/b] The returned array is [i]copied[/i] and any changes to it will not update the original property value. See [%s] for more details.",+ "[note]The returned array is [i]copied[/i] and any changes to it will not update the original property value. See [%s] for more details.[/note]",]strings_l10n: dict[str, str] = {}writing_translation = False@@ -1346,7 +1346,7 @@ def make_rst_class(class_def: ClassDef, state: State, dry_run: bool, output_dir:# Add copy note to built-in properties returning `Packed*Array`.if property_def.type_name.type_name in PACKED_ARRAY_TYPES:# See also `BASE_STRINGS` and `editor/doc/editor_help.cpp`.- copy_note = f"[b]Note:[/b] The returned array is [i]copied[/i] and any changes to it will not update the original property value. See [{property_def.type_name.type_name}] for more details."+ copy_note = f"[note]The returned array is [i]copied[/i] and any changes to it will not update the original property value. See [{property_def.type_name.type_name}] for more details.[/note]"f.write(f"{format_text_block(copy_note, property_def, state)}\n\n")index += 1@@ -1831,6 +1831,7 @@ RESERVED_CROSSLINK_TAGS = ["theme_item","param",]+RESERVED_ADMONITION_TAGS = ["important", "note", "tip", "warning"]def is_in_tagset(tag_text: str, tagset: list[str]) -> bool:@@ -1908,10 +1909,12 @@ def format_text_block(has_codeblocks_csharp = Falsepos = 0+ admonition_pos = -1state.reserved_tag_check.reset()while True:if state.reserved_tag_check.tag_depth > 2 or (state.reserved_tag_check.tag_depth == 2+ and admonition_pos == -1and not (len(state.reserved_tag_check.tag_stack) == 2and state.reserved_tag_check.tag_stack[0] == "codeblocks"@@ -2414,6 +2417,38 @@ def format_text_block(tag_text = ":kbd:" + tag_textstate.reserved_tag_check.add_opening_tag(tag_state.name)escape_pre = True+ elif tag_state.name in RESERVED_ADMONITION_TAGS:+ if tag_state.closing and admonition_pos == -1:+ print_error(+ f'{state.current_class}.xml: Closing admonition tag "[{tag_state.raw}]" has no opening counterpart.',+ state,+ )+ break+ elif tag_state.closing:+ admonition_type = tag_state.name.removeprefix("/")+ admonition_contents = (+ (text[admonition_pos : pos + len(tag_state.name) + 3])+ .removeprefix(f"[{admonition_type}]")+ .removesuffix(f"[/{admonition_type}]")+ .strip()+ )+ lines = admonition_contents.splitlines()+ for i in range(len(lines)):+ lines[i] = " " + lines[i]+ admonition_contents = "\n".join(lines)+ admonition_text = f".. classref_{admonition_type}::\n\n{admonition_contents}"+ if pre_text.rstrip() != "":+ pre_text = text[:admonition_pos].rstrip() + "\n\n"+ pre_text = pre_text + admonition_text+ tag_text = ""+ if post_text.lstrip() != "":+ post_text = "\n\n" + post_text.lstrip()+ admonition_pos = -1+ state.reserved_tag_check.add_closing_tag(tag_state.name)+ else:+ tag_text = f"[{tag_state.name}]"+ admonition_pos = pos+ state.reserved_tag_check.add_opening_tag(tag_state.name)# Invalid syntax.else:diff --git a/editor/doc/editor_help.cpp b/editor/doc/editor_help.cppindex e46824f056d..cf0f2d26736 100644--- a/editor/doc/editor_help.cpp+++ b/editor/doc/editor_help.cpp@@ -60,6 +60,7 @@#include "editor/script/syntax_highlighters.h"#include "editor/settings/editor_settings.h"#include "editor/themes/editor_scale.h"+#include "editor/themes/editor_theme_manager.h"#include "scene/gui/line_edit.h"#include "servers/display/display_server.h"@@ -2496,6 +2497,15 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, const Cconst Color code_color = p_owner_node->get_theme_color(SNAME("code_color"), SNAME("EditorHelp"));const Color kbd_color = p_owner_node->get_theme_color(SNAME("kbd_color"), SNAME("EditorHelp"));const Color code_dark_color = Color(code_color, 0.8);+ const Color note_color = p_owner_node->get_theme_color(SNAME("note_color"), SNAME("EditorHelp"));+ const Color warning_color = p_owner_node->get_theme_color(SNAME("warning_color"), SNAME("EditorHelp"));+ const Color important_color = p_owner_node->get_theme_color(SNAME("important_color"), SNAME("EditorHelp"));+ const Color tip_color = p_owner_node->get_theme_color(SNAME("tip_color"), SNAME("EditorHelp"));++ const Ref<Texture2D> note_icon = p_owner_node->get_editor_theme_icon(SNAME("NodeInfo"));+ const Ref<Texture2D> warning_icon = p_owner_node->get_editor_theme_icon(SNAME("NodeWarning"));+ const Ref<Texture2D> important_icon = p_owner_node->get_editor_theme_icon(SNAME("StatusWarning"));+ const Ref<Texture2D> tip_icon = p_owner_node->get_editor_theme_icon(SNAME("StatusSuccess"));const Color link_color = p_owner_node->get_theme_color(SNAME("link_color"), SNAME("EditorHelp"));const Color link_method_color = p_owner_node->get_theme_color(SNAME("accent_color"), EditorStringName(Editor));@@ -2837,6 +2847,36 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, const Cp_rt->add_newline();}}+ } else if (tag == "note" || tag == "warning" || tag == "important" || tag == "tip") {+ // Admonition block+ const Color &admonition_color = tag == "warning" ? warning_color : (tag == "important" ? important_color : (tag == "tip" ? tip_color : note_color));+ p_rt->push_color(admonition_color);+ p_rt->push_font(doc_bold_font);++ if (tag == "note") {+ // Note block.+ p_rt->add_image(note_icon, note_icon->get_width(), note_icon->get_height(), note_color * (EditorThemeManager::is_dark_theme() ? Color(1, 1, 1) : Color(3.92, 3.92, 3.92)));+ p_rt->add_text(nbsp + TTR("Note:") + " ");+ } else if (tag == "warning") {+ // Warning block.+ // The source icon is already colored, so don't tint it further.+ p_rt->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height());+ p_rt->add_text(nbsp + TTR("Warning:") + " ");+ } else if (tag == "important") {+ // Important block.+ // The source icon is already colored, adjust it to match text color.+ p_rt->add_image(important_icon, important_icon->get_width(), important_icon->get_height(), important_color / warning_color);+ p_rt->add_text(nbsp + TTR("Important:") + " ");+ } else if (tag == "tip") {+ // Tip block.+ // The source icon is already colored, adjust it to match text color.+ p_rt->add_image(tip_icon, tip_icon->get_width(), tip_icon->get_height(), Color(2.68, 1, 1.69) * tip_color);+ p_rt->add_text(nbsp + TTR("Tip:") + " ");+ }++ p_rt->pop(); // font+ pos = brk_end + 1;+ tag_stack.push_front(tag);} else if (tag == "kbd") {int end_pos = bbcode.find("[/kbd]", brk_end + 1);if (end_pos < 0) {diff --git a/editor/themes/editor_theme_manager.h b/editor/themes/editor_theme_manager.hindex d2eb4540f1f..fe738b42219 100644--- a/editor/themes/editor_theme_manager.h+++ b/editor/themes/editor_theme_manager.h@@ -111,6 +111,7 @@ public:Color contrast_color_2;Color highlight_color;Color highlight_disabled_color;+ Color info_color;Color success_color;Color warning_color;Color error_color;diff --git a/editor/themes/theme_classic.cpp b/editor/themes/theme_classic.cppindex 8639f2a9158..7a1af9ce1c2 100644--- a/editor/themes/theme_classic.cpp+++ b/editor/themes/theme_classic.cpp@@ -66,6 +66,7 @@ void ThemeClassic::populate_shared_styles(const Ref<EditorTheme> &p_theme, Editop_config.highlight_color = Color(p_config.accent_color.r, p_config.accent_color.g, p_config.accent_color.b, 0.275);p_config.highlight_disabled_color = p_config.highlight_color.lerp(p_config.dark_icon_and_font ? Color(0, 0, 0) : Color(1, 1, 1), 0.5);+ p_config.info_color = Color(0.7, 0.8, 1);p_config.success_color = Color(0.45, 0.95, 0.5);p_config.warning_color = Color(1, 0.87, 0.4);p_config.error_color = Color(1, 0.47, 0.42);@@ -79,6 +80,7 @@ void ThemeClassic::populate_shared_styles(const Ref<EditorTheme> &p_theme, Editoif (!p_config.dark_icon_and_font) {// Darken some colors to be readable on a light background.+ p_config.info_color = Color(0.35, 0.6, 0.9);p_config.success_color = p_config.success_color.lerp(p_config.mono_color_font, 0.35);p_config.warning_color = Color(0.82, 0.56, 0.1);p_config.error_color = Color(0.8, 0.22, 0.22);@@ -2402,6 +2404,10 @@ void ThemeClassic::populate_editor_styles(const Ref<EditorTheme> &p_theme, Editop_theme->set_color("override_color", "EditorHelp", p_config.warning_color);p_theme->set_color("selection_color", "EditorHelp", p_config.selection_color);p_theme->set_color("link_color", "EditorHelp", p_config.accent_color.lerp(p_config.mono_color_font, 0.8));+ p_theme->set_color("note_color", "EditorHelp", p_config.info_color);+ p_theme->set_color("warning_color", "EditorHelp", p_config.warning_color);+ p_theme->set_color("important_color", "EditorHelp", p_config.warning_color.lerp(p_config.error_color, 0.5));+ p_theme->set_color("tip_color", "EditorHelp", p_config.success_color.lerp(p_config.mono_color, 0.2));p_theme->set_color("code_color", "EditorHelp", p_config.accent_color.lerp(p_config.mono_color_font, 0.6));p_theme->set_color("kbd_color", "EditorHelp", p_config.accent_color.lerp(kbd_color, 0.6));p_theme->set_color("code_bg_color", "EditorHelp", p_config.dark_color_3);diff --git a/editor/themes/theme_modern.cpp b/editor/themes/theme_modern.cppindex ca4248b2c16..4be8425f1a9 100644--- a/editor/themes/theme_modern.cpp+++ b/editor/themes/theme_modern.cpp@@ -75,6 +75,7 @@ void ThemeModern::populate_shared_styles(const Ref<EditorTheme> &p_theme, Editorp_config.highlight_color = Color(p_config.accent_color.r, p_config.accent_color.g, p_config.accent_color.b, 0.275);p_config.highlight_disabled_color = p_config.highlight_color.lerp(p_config.dark_theme ? Color(0, 0, 0) : Color(1, 1, 1), 0.5);+ p_config.info_color = Color(0.7, 0.8, 1);p_config.success_color = Color(0.45, 0.95, 0.5);p_config.warning_color = Color(0.83, 0.78, 0.62);p_config.error_color = Color(1, 0.47, 0.42);@@ -88,6 +89,7 @@ void ThemeModern::populate_shared_styles(const Ref<EditorTheme> &p_theme, Editorif (!p_config.dark_icon_and_font) {// Darken some colors to be readable on a light background.+ p_config.info_color = Color(0.35, 0.6, 0.9);p_config.success_color = p_config.success_color.lerp(p_config.mono_color_font, 0.35);p_config.warning_color = Color(0.83, 0.49, 0.01);p_config.error_color = Color(0.8, 0.22, 0.22);@@ -2725,6 +2727,10 @@ void ThemeModern::populate_editor_styles(const Ref<EditorTheme> &p_theme, Editorp_theme->set_color("override_color", "EditorHelp", p_config.warning_color);p_theme->set_color("selection_color", "EditorHelp", p_config.selection_color);p_theme->set_color("link_color", "EditorHelp", p_config.accent_color.lerp(p_config.mono_color_font, 0.8));+ p_theme->set_color("note_color", "EditorHelp", p_config.info_color);+ p_theme->set_color("warning_color", "EditorHelp", p_config.warning_color);+ p_theme->set_color("important_color", "EditorHelp", p_config.warning_color.lerp(p_config.error_color, 0.5));+ p_theme->set_color("tip_color", "EditorHelp", p_config.success_color.lerp(p_config.mono_color, 0.2));p_theme->set_color("code_color", "EditorHelp", p_config.accent_color.lerp(p_config.mono_color_font, 0.6));p_theme->set_color("kbd_color", "EditorHelp", p_config.accent_color.lerp(kbd_color, 0.6));p_theme->set_color("code_bg_color", "EditorHelp", _get_base_color(p_config, 1.6, 0.8));