Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RTL] Add support for image dynamic updating, padding, tooltips and size in percent. #80410

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions doc/classes/RichTextLabel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,17 @@
<param index="3" name="color" type="Color" default="Color(1, 1, 1, 1)" />
<param index="4" name="inline_align" type="int" enum="InlineAlignment" default="5" />
<param index="5" name="region" type="Rect2" default="Rect2(0, 0, 0, 0)" />
<param index="6" name="key" type="Variant" default="null" />
<param index="7" name="pad" type="bool" default="false" />
<param index="8" name="tooltip" type="String" default="&quot;&quot;" />
<param index="9" name="size_in_percent" type="bool" default="false" />
Comment on lines +27 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full list of parameters is starting to be quite convoluted, that will be an awkward API to use IMO.
Struct support would be great here, passing a configuration struct with selected optional members defined would probably be better.

<description>
Adds an image's opening and closing tags to the tag stack, optionally providing a [param width] and [param height] to resize the image, a [param color] to tint the image and a [param region] to only use parts of the image.
If [param width] or [param height] is set to 0, the image size will be adjusted in order to keep the original aspect ratio.
If [param width] and [param height] are not set, but [param region] is, the region's rect will be used.
[param key] is an optional identifier, that can be used to modify the image via [method update_image].
If [param pad] is set, and the image is smaller than the size specified by [param width] and [param height], the image padding is added to match the size instead of upscaling.
If [param size_in_percent] is set, [param width] and [param height] values are percentages of the control width instead of pixels.
</description>
</method>
<method name="add_text">
Expand Down Expand Up @@ -539,6 +546,23 @@
If [param expand] is [code]false[/code], the column will not contribute to the total ratio.
</description>
</method>
<method name="update_image">
<return type="void" />
<param index="0" name="key" type="Variant" />
<param index="1" name="mask" type="int" enum="RichTextLabel.ImageUpdateMask" is_bitfield="true" />
<param index="2" name="image" type="Texture2D" />
<param index="3" name="width" type="int" default="0" />
<param index="4" name="height" type="int" default="0" />
<param index="5" name="color" type="Color" default="Color(1, 1, 1, 1)" />
<param index="6" name="inline_align" type="int" enum="InlineAlignment" default="5" />
<param index="7" name="region" type="Rect2" default="Rect2(0, 0, 0, 0)" />
<param index="8" name="pad" type="bool" default="false" />
<param index="9" name="tooltip" type="String" default="&quot;&quot;" />
<param index="10" name="size_in_percent" type="bool" default="false" />
<description>
Updates the existing images with the key [param key]. Only properties specified by [param mask] bits are updated. See [method add_image].
</description>
</method>
</methods>
<members>
<member name="autowrap_mode" type="int" setter="set_autowrap_mode" getter="get_autowrap_mode" enum="TextServer.AutowrapMode" default="3">
Expand Down Expand Up @@ -667,6 +691,30 @@
<constant name="MENU_MAX" value="2" enum="MenuItems">
Represents the size of the [enum MenuItems] enum.
</constant>
<constant name="UPDATE_TEXTURE" value="1" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes image texture.
</constant>
<constant name="UPDATE_SIZE" value="2" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes image size.
</constant>
<constant name="UPDATE_COLOR" value="4" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes image color.
</constant>
<constant name="UPDATE_ALIGNMENT" value="8" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes image inline alignment.
</constant>
<constant name="UPDATE_REGION" value="16" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes image texture region.
</constant>
<constant name="UPDATE_PAD" value="32" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes image padding.
</constant>
<constant name="UPDATE_TOOLTIP" value="64" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes image tooltip.
</constant>
<constant name="UPDATE_WIDTH_IN_PERCENT" value="128" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes image width from/to percents.
</constant>
</constants>
<theme_items>
<theme_item name="default_color" data_type="color" type="Color" default="Color(1, 1, 1, 1)">
Expand Down
38 changes: 32 additions & 6 deletions editor/editor_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2269,20 +2269,46 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, Control

pos = brk_end + 1;
tag_stack.push_front("url");
} else if (tag == "img") {
} else if (tag.begins_with("img")) {
int width = 0;
int height = 0;
bool size_in_percent = false;
if (tag.length() > 4) {
Vector<String> subtags = tag.substr(4, tag.length()).split(" ");
HashMap<String, String> bbcode_options;
for (int i = 0; i < subtags.size(); i++) {
const String &expr = subtags[i];
int value_pos = expr.find("=");
if (value_pos > -1) {
bbcode_options[expr.substr(0, value_pos)] = expr.substr(value_pos + 1).unquote();
}
}
HashMap<String, String>::Iterator width_option = bbcode_options.find("width");
if (width_option) {
width = width_option->value.to_int();
if (width_option->value.ends_with("%")) {
size_in_percent = true;
}
}

HashMap<String, String>::Iterator height_option = bbcode_options.find("height");
if (height_option) {
height = height_option->value.to_int();
if (height_option->value.ends_with("%")) {
size_in_percent = true;
}
}
}
int end = bbcode.find("[", brk_end);
if (end == -1) {
end = bbcode.length();
}
String image = bbcode.substr(brk_end + 1, end - brk_end - 1);

Ref<Texture2D> texture = ResourceLoader::load(base_path.path_join(image), "Texture2D");
if (texture.is_valid()) {
p_rt->add_image(texture);
}
p_rt->add_image(ResourceLoader::load(base_path.path_join(image), "Texture2D"), width, height, Color(1, 1, 1), INLINE_ALIGNMENT_CENTER, Rect2(), Variant(), false, String(), size_in_percent);

pos = end;
tag_stack.push_front(tag);
tag_stack.push_front("img");
} else if (tag.begins_with("color=")) {
String col = tag.substr(6, tag.length());
Color color = Color::from_string(col, Color());
Expand Down
7 changes: 7 additions & 0 deletions misc/extension_api_validation/4.1-stable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ Validate extension JSON: API was removed: classes/GraphNode/signals/raise_reques
Validate extension JSON: API was removed: classes/GraphNode/signals/resize_request

Refactor GraphNode (splitup in GraphElement and GraphNode)


GH-81070
--------
Validate extension JSON: API was removed: classes/TileMap/methods/get_quadrant_size
Expand All @@ -190,4 +192,9 @@ GH-79965
--------
Validate extension JSON: JSON file: Field was added in a way that breaks compatibility 'classes/PopupMenu/methods/clear': arguments


GH-80410
--------
Validate extension JSON: Error: Field 'classes/RichTextLabel/methods/add_image/arguments': size changed value in new API, from 6 to 10.

Added optional argument. Compatibility method registered.
7 changes: 7 additions & 0 deletions modules/mono/glue/GodotSharp/GodotSharp/Compat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ public long DrawListBegin(Rid framebuffer, InitialAction initialColorAction, Fin

partial class RichTextLabel
{
/// <inheritdoc cref="AddImage(Texture2D, int, int, Nullable{Color}, InlineAlignment, Nullable{Rect2}, Variant, bool, string, bool)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public void AddImage(Texture2D image, int width, int height, Nullable<Color> color, InlineAlignment inlineAlign, Nullable<Rect2> region)
{
AddImage(image, width, height, color, inlineAlign, region, key: default, pad: false, tooltip: "", sizeInPercent: false);
}

/// <inheritdoc cref="PushList(int, ListType, bool, string)"/>
[EditorBrowsable(EditorBrowsableState.Never)]
public void PushList(int level, ListType type, bool capitalize)
Expand Down
41 changes: 41 additions & 0 deletions scene/gui/rich_text_label.compat.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**************************************************************************/
/* rich_text_label.compat.inc */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef DISABLE_DEPRECATED

void RichTextLabel::_add_image_bind_compat_80410(const Ref<Texture2D> &p_image, const int p_width, const int p_height, const Color &p_color, InlineAlignment p_alignment, const Rect2 &p_region) {
add_image(p_image, p_width, p_height, p_color, p_alignment, p_region, Variant(), false, String(), false);
}

void RichTextLabel::_bind_compatibility_methods() {
ClassDB::bind_compatibility_method(D_METHOD("add_image", "image", "width", "height", "color", "inline_align", "region"), &RichTextLabel::_add_image_bind_compat_80410, DEFVAL(0), DEFVAL(0), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(INLINE_ALIGNMENT_CENTER), DEFVAL(Rect2()));
}

#endif // DISABLE_DEPRECATED
Loading