Skip to content

Commit

Permalink
Added Label3D auto localization
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaW0x committed Jan 26, 2024
1 parent 5034478 commit c3a72a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/classes/Label3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<member name="alpha_scissor_threshold" type="float" setter="set_alpha_scissor_threshold" getter="get_alpha_scissor_threshold" default="0.5">
Threshold at which the alpha scissor will discard values.
</member>
<member name="auto_translate" type="bool" setter="set_auto_translate" getter="is_auto_translating" default="true">
Toggles if any text should automatically change to its translated version depending on the current locale.
Also decides if the node's strings should be parsed for POT generation.
</member>
<member name="autowrap_mode" type="int" setter="set_autowrap_mode" getter="get_autowrap_mode" enum="TextServer.AutowrapMode" default="0">
If set to something other than [constant TextServer.AUTOWRAP_OFF], the text gets wrapped inside the node's bounding rectangle. If you resize the node, it will change its height automatically to show all the text. To see how each mode behaves, see [enum TextServer.AutowrapMode].
</member>
Expand Down
27 changes: 25 additions & 2 deletions scene/3d/label_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ void Label3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_language", "language"), &Label3D::set_language);
ClassDB::bind_method(D_METHOD("get_language"), &Label3D::get_language);

ClassDB::bind_method(D_METHOD("set_auto_translate", "enable"), &Label3D::set_auto_translate);
ClassDB::bind_method(D_METHOD("is_auto_translating"), &Label3D::is_auto_translating);

ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &Label3D::set_structured_text_bidi_override);
ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &Label3D::get_structured_text_bidi_override);

Expand Down Expand Up @@ -164,6 +167,9 @@ void Label3D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");

ADD_GROUP("Localization", "");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_translate"), "set_auto_translate", "is_auto_translating");

BIND_ENUM_CONSTANT(FLAG_SHADED);
BIND_ENUM_CONSTANT(FLAG_DOUBLE_SIDED);
BIND_ENUM_CONSTANT(FLAG_DISABLE_DEPTH_TEST);
Expand Down Expand Up @@ -208,7 +214,7 @@ void Label3D::_notification(int p_what) {
viewport->disconnect("size_changed", callable_mp(this, &Label3D::_font_changed));
} break;
case NOTIFICATION_TRANSLATION_CHANGED: {
String new_text = tr(text);
String new_text = atr(text);
if (new_text == xl_text) {
return; // Nothing new.
}
Expand Down Expand Up @@ -635,8 +641,11 @@ void Label3D::_shape() {
}

void Label3D::set_text(const String &p_string) {
if (text == p_string) {
return;
}
text = p_string;
xl_text = tr(p_string);
xl_text = atr(p_string);
dirty_text = true;
_queue_update();
}
Expand Down Expand Up @@ -685,6 +694,20 @@ TextServer::Direction Label3D::get_text_direction() const {
return text_direction;
}

void Label3D::set_auto_translate(bool p_enable) {
if (p_enable == auto_translate) {
return;
}

auto_translate = p_enable;

notification(MainLoop::NOTIFICATION_TRANSLATION_CHANGED);
}

bool Label3D::is_auto_translating() const {
return auto_translate;
}

void Label3D::set_language(const String &p_language) {
if (language != p_language) {
language = p_language;
Expand Down
9 changes: 9 additions & 0 deletions scene/3d/label_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class Label3D : public GeometryInstance3D {
TextServer::StructuredTextParser st_parser = TextServer::STRUCTURED_TEXT_DEFAULT;
Array st_args;

bool auto_translate = true;

RID text_rid;
Vector<RID> lines_rid;

Expand Down Expand Up @@ -182,6 +184,13 @@ class Label3D : public GeometryInstance3D {
void set_text_direction(TextServer::Direction p_text_direction);
TextServer::Direction get_text_direction() const;

void set_auto_translate(bool p_enable);
bool is_auto_translating() const;

_FORCE_INLINE_ String atr(const String p_string) const {
return is_auto_translating() ? tr(p_string) : p_string;
}

void set_language(const String &p_language);
String get_language() const;

Expand Down

0 comments on commit c3a72a0

Please sign in to comment.