Skip to content

Commit 3578aa6

Browse files
committed
Fix item translation and icon in the Anchors Preset dropdown
- Some items were not translated. - Item icons did not react to light/dark theme switch.
1 parent 1b4ed4c commit 3578aa6

File tree

3 files changed

+105
-51
lines changed

3 files changed

+105
-51
lines changed

editor/plugins/control_editor_plugin.cpp

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@
3131
#include "control_editor_plugin.h"
3232

3333
#include "editor/editor_node.h"
34-
#include "editor/editor_string_names.h"
3534
#include "editor/editor_undo_redo_manager.h"
3635
#include "editor/plugins/canvas_item_editor_plugin.h"
3736
#include "editor/themes/editor_scale.h"
37+
#include "scene/gui/button.h"
38+
#include "scene/gui/check_box.h"
3839
#include "scene/gui/check_button.h"
3940
#include "scene/gui/grid_container.h"
41+
#include "scene/gui/option_button.h"
42+
#include "scene/gui/panel_container.h"
4043
#include "scene/gui/separator.h"
44+
#include "scene/gui/texture_rect.h"
4145

4246
// Inspector controls.
4347

@@ -160,6 +164,41 @@ void EditorPropertyAnchorsPreset::_set_read_only(bool p_read_only) {
160164
options->set_disabled(p_read_only);
161165
}
162166

167+
void EditorPropertyAnchorsPreset::_notification(int p_what) {
168+
switch (p_what) {
169+
case NOTIFICATION_THEME_CHANGED: {
170+
for (int i = 0; i < options->get_item_count(); i++) {
171+
if (options->is_item_separator(i)) {
172+
continue;
173+
}
174+
int64_t preset = options->get_item_metadata(i);
175+
if (preset < 0 || PRESET_FULL_RECT < preset) {
176+
continue;
177+
}
178+
static const StringName icon_names[] = {
179+
StringName("ControlAlignTopLeft", true),
180+
StringName("ControlAlignTopRight", true),
181+
StringName("ControlAlignBottomLeft", true),
182+
StringName("ControlAlignBottomRight", true),
183+
StringName("ControlAlignCenterLeft", true),
184+
StringName("ControlAlignCenterTop", true),
185+
StringName("ControlAlignCenterRight", true),
186+
StringName("ControlAlignCenterBottom", true),
187+
StringName("ControlAlignCenter", true),
188+
StringName("ControlAlignLeftWide", true),
189+
StringName("ControlAlignTopWide", true),
190+
StringName("ControlAlignRightWide", true),
191+
StringName("ControlAlignBottomWide", true),
192+
StringName("ControlAlignVCenterWide", true),
193+
StringName("ControlAlignHCenterWide", true),
194+
StringName("ControlAlignFullRect", true),
195+
};
196+
options->set_item_icon(i, get_editor_theme_icon(icon_names[preset]));
197+
}
198+
} break;
199+
}
200+
}
201+
163202
void EditorPropertyAnchorsPreset::_option_selected(int p_which) {
164203
int64_t val = options->get_item_metadata(p_which);
165204
emit_changed(get_edited_property(), val);
@@ -180,30 +219,22 @@ void EditorPropertyAnchorsPreset::update_property() {
180219
void EditorPropertyAnchorsPreset::setup(const Vector<String> &p_options) {
181220
options->clear();
182221

183-
Vector<String> split_after;
184-
split_after.append("Custom");
185-
split_after.append("PresetFullRect");
186-
split_after.append("PresetBottomLeft");
187-
split_after.append("PresetCenter");
222+
const Vector<int> split_after = {
223+
-1,
224+
PRESET_FULL_RECT,
225+
PRESET_BOTTOM_LEFT,
226+
PRESET_CENTER,
227+
};
188228

189-
for (int i = 0, j = 0; i < p_options.size(); i++, j++) {
229+
for (int i = 0; i < p_options.size(); i++) {
190230
Vector<String> text_split = p_options[i].split(":");
191231
int64_t current_val = text_split[1].to_int();
192232

193233
const String &option_name = text_split[0];
194-
if (option_name.begins_with("Preset")) {
195-
String preset_name = option_name.trim_prefix("Preset");
196-
String humanized_name = preset_name.capitalize();
197-
String icon_name = "ControlAlign" + preset_name;
198-
options->add_icon_item(EditorNode::get_singleton()->get_editor_theme()->get_icon(icon_name, EditorStringName(EditorIcons)), humanized_name);
199-
} else {
200-
options->add_item(option_name);
201-
}
202-
203-
options->set_item_metadata(j, current_val);
204-
if (split_after.has(option_name)) {
234+
options->add_item(option_name);
235+
options->set_item_metadata(-1, current_val);
236+
if (split_after.has(current_val)) {
205237
options->add_separator();
206-
j++;
207238
}
208239
}
209240
}
@@ -613,33 +644,33 @@ AnchorPresetPicker::AnchorPresetPicker() {
613644
top_row->add_theme_constant_override("separation", grid_separation);
614645
main_vb->add_child(top_row);
615646

616-
_add_row_button(top_row, PRESET_TOP_LEFT, TTR("Top Left"));
617-
_add_row_button(top_row, PRESET_CENTER_TOP, TTR("Center Top"));
618-
_add_row_button(top_row, PRESET_TOP_RIGHT, TTR("Top Right"));
647+
_add_row_button(top_row, PRESET_TOP_LEFT, TTRC("Top Left"));
648+
_add_row_button(top_row, PRESET_CENTER_TOP, TTRC("Center Top"));
649+
_add_row_button(top_row, PRESET_TOP_RIGHT, TTRC("Top Right"));
619650
_add_separator(top_row, memnew(VSeparator));
620-
_add_row_button(top_row, PRESET_TOP_WIDE, TTR("Top Wide"));
651+
_add_row_button(top_row, PRESET_TOP_WIDE, TTRC("Top Wide"));
621652

622653
HBoxContainer *mid_row = memnew(HBoxContainer);
623654
mid_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
624655
mid_row->add_theme_constant_override("separation", grid_separation);
625656
main_vb->add_child(mid_row);
626657

627-
_add_row_button(mid_row, PRESET_CENTER_LEFT, TTR("Center Left"));
628-
_add_row_button(mid_row, PRESET_CENTER, TTR("Center"));
629-
_add_row_button(mid_row, PRESET_CENTER_RIGHT, TTR("Center Right"));
658+
_add_row_button(mid_row, PRESET_CENTER_LEFT, TTRC("Center Left"));
659+
_add_row_button(mid_row, PRESET_CENTER, TTRC("Center"));
660+
_add_row_button(mid_row, PRESET_CENTER_RIGHT, TTRC("Center Right"));
630661
_add_separator(mid_row, memnew(VSeparator));
631-
_add_row_button(mid_row, PRESET_HCENTER_WIDE, TTR("HCenter Wide"));
662+
_add_row_button(mid_row, PRESET_HCENTER_WIDE, TTRC("HCenter Wide"));
632663

633664
HBoxContainer *bot_row = memnew(HBoxContainer);
634665
bot_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
635666
bot_row->add_theme_constant_override("separation", grid_separation);
636667
main_vb->add_child(bot_row);
637668

638-
_add_row_button(bot_row, PRESET_BOTTOM_LEFT, TTR("Bottom Left"));
639-
_add_row_button(bot_row, PRESET_CENTER_BOTTOM, TTR("Center Bottom"));
640-
_add_row_button(bot_row, PRESET_BOTTOM_RIGHT, TTR("Bottom Right"));
669+
_add_row_button(bot_row, PRESET_BOTTOM_LEFT, TTRC("Bottom Left"));
670+
_add_row_button(bot_row, PRESET_CENTER_BOTTOM, TTRC("Center Bottom"));
671+
_add_row_button(bot_row, PRESET_BOTTOM_RIGHT, TTRC("Bottom Right"));
641672
_add_separator(bot_row, memnew(VSeparator));
642-
_add_row_button(bot_row, PRESET_BOTTOM_WIDE, TTR("Bottom Wide"));
673+
_add_row_button(bot_row, PRESET_BOTTOM_WIDE, TTRC("Bottom Wide"));
643674

644675
_add_separator(main_vb, memnew(HSeparator));
645676

@@ -648,11 +679,11 @@ AnchorPresetPicker::AnchorPresetPicker() {
648679
extra_row->add_theme_constant_override("separation", grid_separation);
649680
main_vb->add_child(extra_row);
650681

651-
_add_row_button(extra_row, PRESET_LEFT_WIDE, TTR("Left Wide"));
652-
_add_row_button(extra_row, PRESET_VCENTER_WIDE, TTR("VCenter Wide"));
653-
_add_row_button(extra_row, PRESET_RIGHT_WIDE, TTR("Right Wide"));
682+
_add_row_button(extra_row, PRESET_LEFT_WIDE, TTRC("Left Wide"));
683+
_add_row_button(extra_row, PRESET_VCENTER_WIDE, TTRC("VCenter Wide"));
684+
_add_row_button(extra_row, PRESET_RIGHT_WIDE, TTRC("Right Wide"));
654685
_add_separator(extra_row, memnew(VSeparator));
655-
_add_row_button(extra_row, PRESET_FULL_RECT, TTR("Full Rect"));
686+
_add_row_button(extra_row, PRESET_FULL_RECT, TTRC("Full Rect"));
656687
}
657688

658689
void SizeFlagPresetPicker::_preset_button_pressed(const int p_preset) {

editor/plugins/control_editor_plugin.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,17 @@
3333
#include "editor/editor_inspector.h"
3434
#include "editor/plugins/editor_plugin.h"
3535
#include "scene/gui/box_container.h"
36-
#include "scene/gui/button.h"
37-
#include "scene/gui/check_box.h"
38-
#include "scene/gui/control.h"
39-
#include "scene/gui/label.h"
4036
#include "scene/gui/margin_container.h"
41-
#include "scene/gui/option_button.h"
42-
#include "scene/gui/panel_container.h"
43-
#include "scene/gui/popup.h"
44-
#include "scene/gui/separator.h"
45-
#include "scene/gui/texture_rect.h"
4637

38+
class CheckBox;
4739
class CheckButton;
4840
class EditorSelection;
4941
class GridContainer;
42+
class Label;
43+
class OptionButton;
44+
class PanelContainer;
45+
class Separator;
46+
class TextureRect;
5047

5148
// Inspector controls.
5249
class ControlPositioningWarning : public MarginContainer {
@@ -84,6 +81,7 @@ class EditorPropertyAnchorsPreset : public EditorProperty {
8481

8582
protected:
8683
virtual void _set_read_only(bool p_read_only) override;
84+
void _notification(int p_what);
8785

8886
public:
8987
void setup(const Vector<String> &p_options);

scene/gui/control.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@
3333
#include "container.h"
3434
#include "core/config/project_settings.h"
3535
#include "core/input/input_map.h"
36-
#include "core/math/geometry_2d.h"
3736
#include "core/os/os.h"
37+
#include "core/string/string_builder.h"
3838
#include "core/string/translation_server.h"
39-
#include "scene/gui/label.h"
40-
#include "scene/gui/panel.h"
4139
#include "scene/gui/scroll_container.h"
4240
#include "scene/main/canvas_layer.h"
4341
#include "scene/main/window.h"
@@ -3992,10 +3990,37 @@ void Control::_bind_methods() {
39923990
ADD_PROPERTY(PropertyInfo(Variant::INT, "layout_mode", PROPERTY_HINT_ENUM, "Position,Anchors,Container,Uncontrolled", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_layout_mode", "_get_layout_mode");
39933991
ADD_PROPERTY_DEFAULT("layout_mode", LayoutMode::LAYOUT_MODE_POSITION);
39943992

3995-
const String anchors_presets_options = "Custom:-1,PresetFullRect:15,"
3996-
"PresetTopLeft:0,PresetTopRight:1,PresetBottomRight:3,PresetBottomLeft:2,"
3997-
"PresetCenterLeft:4,PresetCenterTop:5,PresetCenterRight:6,PresetCenterBottom:7,PresetCenter:8,"
3998-
"PresetLeftWide:9,PresetTopWide:10,PresetRightWide:11,PresetBottomWide:12,PresetVCenterWide:13,PresetHCenterWide:14";
3993+
constexpr struct {
3994+
const char *name;
3995+
LayoutPreset value;
3996+
} anchors_presets[] = {
3997+
{ TTRC("Full Rect"), PRESET_FULL_RECT },
3998+
{ TTRC("Top Left"), PRESET_TOP_LEFT },
3999+
{ TTRC("Top Right"), PRESET_TOP_RIGHT },
4000+
{ TTRC("Bottom Right"), PRESET_BOTTOM_RIGHT },
4001+
{ TTRC("Bottom Left"), PRESET_BOTTOM_LEFT },
4002+
{ TTRC("Center Left"), PRESET_CENTER_LEFT },
4003+
{ TTRC("Center Top"), PRESET_CENTER_TOP },
4004+
{ TTRC("Center Right"), PRESET_CENTER_RIGHT },
4005+
{ TTRC("Center Bottom"), PRESET_CENTER_BOTTOM },
4006+
{ TTRC("Center"), PRESET_CENTER },
4007+
{ TTRC("Left Wide"), PRESET_LEFT_WIDE },
4008+
{ TTRC("Top Wide"), PRESET_TOP_WIDE },
4009+
{ TTRC("Right Wide"), PRESET_RIGHT_WIDE },
4010+
{ TTRC("Bottom Wide"), PRESET_BOTTOM_WIDE },
4011+
{ TTRC("VCenter Wide"), PRESET_VCENTER_WIDE },
4012+
{ TTRC("HCenter Wide"), PRESET_HCENTER_WIDE },
4013+
};
4014+
StringBuilder builder;
4015+
builder.append(TTRC("Custom"));
4016+
builder.append(":-1");
4017+
for (size_t i = 0; i < std::size(anchors_presets); i++) {
4018+
builder.append(",");
4019+
builder.append(anchors_presets[i].name);
4020+
builder.append(":");
4021+
builder.append(itos(anchors_presets[i].value));
4022+
}
4023+
const String anchors_presets_options = builder.as_string();
39994024

40004025
ADD_PROPERTY(PropertyInfo(Variant::INT, "anchors_preset", PROPERTY_HINT_ENUM, anchors_presets_options, PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "_set_anchors_layout_preset", "_get_anchors_layout_preset");
40014026
ADD_PROPERTY_DEFAULT("anchors_preset", -1);

0 commit comments

Comments
 (0)