Procházet zdrojové kódy

Merge pull request #105892 from timothyqiu/color-picker-improvements

Improve `ColorPicker`
Thaddeus Crews před 4 měsíci
rodič
revize
81283460fd

+ 1 - 1
editor/editor_node.cpp

@@ -3658,7 +3658,7 @@ void EditorNode::_update_file_menu_opened() {
 
 void EditorNode::_palette_quick_open_dialog() {
 	quick_open_color_palette->popup_dialog({ "ColorPalette" }, palette_file_selected_callback);
-	quick_open_color_palette->set_title(TTR("Quick Open Color Palette..."));
+	quick_open_color_palette->set_title(TTRC("Quick Open Color Palette..."));
 }
 
 void EditorNode::replace_resources_in_object(Object *p_object, const Vector<Ref<Resource>> &p_source_resources, const Vector<Ref<Resource>> &p_target_resource) {

+ 1 - 1
scene/gui/color_mode.h

@@ -132,7 +132,7 @@ public:
 	float slider_max[4] = { 359, 100, 100, 255 };
 	float cached_hue = 0.0;
 	float cached_saturation = 0.0;
-	Ref<GradientTexture2D> hue_texture = nullptr;
+	Ref<GradientTexture2D> hue_texture;
 
 	virtual String get_name() const override { return "OKHSL"; }
 

+ 66 - 65
scene/gui/color_picker.cpp

@@ -31,7 +31,6 @@
 #include "color_picker.h"
 
 #include "core/io/image.h"
-#include "scene/gui/aspect_ratio_container.h"
 #include "scene/gui/color_mode.h"
 #include "scene/gui/color_picker_shape.h"
 #include "scene/gui/file_dialog.h"
@@ -48,7 +47,6 @@
 #include "scene/gui/texture_rect.h"
 #include "scene/resources/atlas_texture.h"
 #include "scene/resources/color_palette.h"
-#include "scene/resources/gradient_texture.h"
 #include "scene/resources/image_texture.h"
 #include "scene/resources/style_box_flat.h"
 #include "scene/resources/style_box_texture.h"
@@ -95,28 +93,13 @@ void ColorPicker::_notification(int p_what) {
 			}
 		} break;
 
-		case NOTIFICATION_TRANSLATION_CHANGED: {
-			List<BaseButton *> buttons;
-			preset_group->get_buttons(&buttons);
-			for (List<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
-				Color preset_color = ((ColorPresetButton *)E->get())->get_preset_color();
-				E->get()->set_tooltip_text(vformat(atr(ETR("Color: #%s\nLMB: Apply color\nRMB: Remove preset")), preset_color.to_html(preset_color.a < 1)));
-			}
-
-			buttons.clear();
-			recent_preset_group->get_buttons(&buttons);
-			for (List<BaseButton *>::Element *E = buttons.front(); E; E = E->next()) {
-				Color preset_color = ((ColorPresetButton *)E->get())->get_preset_color();
-				E->get()->set_tooltip_text(vformat(atr(ETR("Color: #%s\nLMB: Apply color")), preset_color.to_html(preset_color.a < 1)));
-			}
-		} break;
-
 		case NOTIFICATION_THEME_CHANGED: {
 			btn_pick->set_button_icon(theme_cache.screen_picker);
 			_update_drop_down_arrow(btn_preset->is_pressed(), btn_preset);
 			_update_drop_down_arrow(btn_recent_preset->is_pressed(), btn_recent_preset);
 			btn_add_preset->set_button_icon(theme_cache.add_preset);
 			menu_btn->set_button_icon(theme_cache.menu_option);
+			btn_mode->set_button_icon(theme_cache.menu_option);
 
 			btn_pick->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
 			btn_shape->set_custom_minimum_size(Size2(28 * theme_cache.base_scale, 0));
@@ -779,7 +762,7 @@ void ColorPicker::_update_presets() {
 
 			if (palette_edited) {
 				palette_name->set_text(vformat("%s*", palette_name->get_text().remove_char('*')));
-				palette_name->set_tooltip_text(ETR("The changes to this palette have not been saved to a file."));
+				palette_name->set_tooltip_text(TTRC("The changes to this palette have not been saved to a file."));
 			}
 		}
 	}
@@ -821,16 +804,15 @@ void ColorPicker::_update_recent_presets() {
 #endif
 }
 
+#ifdef TOOLS_ENABLED
 void ColorPicker::_text_type_toggled() {
 	text_is_constructor = !text_is_constructor;
 	if (text_is_constructor) {
 		text_type->set_text("");
-#ifdef TOOLS_ENABLED
 		text_type->set_button_icon(get_editor_theme_icon(SNAME("Script")));
-#endif
 
 		c_text->set_editable(false);
-		c_text->set_tooltip_text(RTR("Copy this constructor in a script."));
+		c_text->set_tooltip_text(TTRC("Copy this constructor in a script."));
 	} else {
 		text_type->set_text("#");
 		text_type->set_button_icon(nullptr);
@@ -840,6 +822,7 @@ void ColorPicker::_text_type_toggled() {
 	}
 	_update_color();
 }
+#endif // TOOLS_ENABLED
 
 Color ColorPicker::get_pick_color() const {
 	return color;
@@ -886,9 +869,7 @@ inline int ColorPicker::_get_preset_size() {
 }
 
 void ColorPicker::_add_preset_button(int p_size, const Color &p_color) {
-	ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size));
-	btn_preset_new->set_tooltip_text(vformat(atr(ETR("Color: #%s\nLMB: Apply color\nRMB: Remove preset")), p_color.to_html(p_color.a < 1)));
-	btn_preset_new->set_accessibility_name(vformat(atr(ETR("Color: #%s")), p_color.to_html(p_color.a < 1)));
+	ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size, false));
 	SET_DRAG_FORWARDING_GCDU(btn_preset_new, ColorPicker);
 	btn_preset_new->set_button_group(preset_group);
 	preset_container->add_child(btn_preset_new);
@@ -897,9 +878,7 @@ void ColorPicker::_add_preset_button(int p_size, const Color &p_color) {
 }
 
 void ColorPicker::_add_recent_preset_button(int p_size, const Color &p_color) {
-	ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size));
-	btn_preset_new->set_tooltip_text(vformat(atr(ETR("Color: #%s\nLMB: Apply color")), p_color.to_html(p_color.a < 1)));
-	btn_preset_new->set_accessibility_name(vformat(atr(ETR("Color: #%s")), p_color.to_html(p_color.a < 1)));
+	ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size, true));
 	btn_preset_new->set_button_group(recent_preset_group);
 	recent_preset_hbc->add_child(btn_preset_new);
 	recent_preset_hbc->move_child(btn_preset_new, 0);
@@ -911,7 +890,7 @@ void ColorPicker::_load_palette() {
 	List<String> extensions;
 	ResourceLoader::get_recognized_extensions_for_type("ColorPalette", &extensions);
 
-	file_dialog->set_title(RTR("Load Color Palette"));
+	file_dialog->set_title(ETR("Load Color Palette"));
 	file_dialog->clear_filters();
 	for (const String &K : extensions) {
 		file_dialog->add_filter("*." + K);
@@ -931,7 +910,7 @@ void ColorPicker::_save_palette(bool p_is_save_as) {
 		List<String> extensions;
 		ResourceLoader::get_recognized_extensions_for_type("ColorPalette", &extensions);
 
-		file_dialog->set_title(RTR("Save Color Palette"));
+		file_dialog->set_title(ETR("Save Color Palette"));
 		file_dialog->clear_filters();
 		for (const String &K : extensions) {
 			file_dialog->add_filter("*." + K);
@@ -943,17 +922,13 @@ void ColorPicker::_save_palette(bool p_is_save_as) {
 	}
 }
 
+#ifdef TOOLS_ENABLED
 void ColorPicker::_quick_open_palette_file_selected(const String &p_path) {
-	if (!file_dialog) {
-		file_dialog = memnew(FileDialog);
-		add_child(file_dialog, false, INTERNAL_MODE_FRONT);
-		file_dialog->connect("file_selected", callable_mp(this, &ColorPicker::_palette_file_selected));
-		file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
-		file_dialog->set_current_dir(Engine::get_singleton()->is_editor_hint() ? "res://" : "user://");
-	}
+	_ensure_file_dialog();
 	file_dialog->set_file_mode(FileDialog::FILE_MODE_OPEN_FILE);
 	_palette_file_selected(p_path);
 }
+#endif // ifdef TOOLS_ENABLED
 
 void ColorPicker::_palette_file_selected(const String &p_path) {
 	switch (file_dialog->get_file_mode()) {
@@ -1047,7 +1022,7 @@ Variant ColorPicker::_get_drag_data_fw(const Point2 &p_point, Control *p_from_co
 		return Variant();
 	}
 
-	ColorPresetButton *drag_preview = memnew(ColorPresetButton(dragged_preset_button->get_preset_color(), _get_preset_size()));
+	ColorPresetButton *drag_preview = memnew(ColorPresetButton(dragged_preset_button->get_preset_color(), _get_preset_size(), false));
 	set_drag_preview(drag_preview);
 
 	Dictionary drag_data;
@@ -1082,6 +1057,19 @@ void ColorPicker::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Co
 	}
 }
 
+void ColorPicker::_ensure_file_dialog() {
+	if (file_dialog) {
+		return;
+	}
+
+	file_dialog = memnew(FileDialog);
+	file_dialog->set_mode_overrides_title(false);
+	file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
+	file_dialog->set_current_dir(Engine::get_singleton()->is_editor_hint() ? "res://" : "user://");
+	add_child(file_dialog, false, INTERNAL_MODE_FRONT);
+	file_dialog->connect("file_selected", callable_mp(this, &ColorPicker::_palette_file_selected));
+}
+
 void ColorPicker::add_preset(const Color &p_color) {
 	List<Color>::Element *e = presets.find(p_color);
 	if (e) {
@@ -1602,36 +1590,31 @@ void ColorPicker::_update_menu_items() {
 	options_menu->clear();
 	options_menu->reset_size();
 
-	if (!presets.is_empty()) {
-		options_menu->add_icon_item(get_theme_icon(SNAME("save"), SNAME("FileDialog")), RTR("Save"), static_cast<int>(MenuOption::MENU_SAVE));
-		options_menu->set_item_tooltip(-1, ETR("Save the current color palette to reuse later."));
-	}
-	if (!palette_path.is_empty()) {
-		options_menu->add_icon_item(get_theme_icon(SNAME("save"), SNAME("FileDialog")), RTR("Save As"), static_cast<int>(MenuOption::MENU_SAVE_AS));
-		options_menu->set_item_tooltip(-1, ETR("Save the current color palette as a new to reuse later."));
-	}
-	options_menu->add_icon_item(get_theme_icon(SNAME("load"), SNAME("FileDialog")), RTR("Load"), static_cast<int>(MenuOption::MENU_LOAD));
+	options_menu->add_icon_item(get_theme_icon(SNAME("save"), SNAME("FileDialog")), ETR("Save"), static_cast<int>(MenuOption::MENU_SAVE));
+	options_menu->set_item_tooltip(-1, ETR("Save the current color palette to reuse later."));
+	options_menu->set_item_disabled(-1, presets.is_empty());
+
+	options_menu->add_icon_item(get_theme_icon(SNAME("save"), SNAME("FileDialog")), ETR("Save As"), static_cast<int>(MenuOption::MENU_SAVE_AS));
+	options_menu->set_item_tooltip(-1, ETR("Save the current color palette as a new to reuse later."));
+	options_menu->set_item_disabled(-1, palette_path.is_empty());
+
+	options_menu->add_icon_item(get_theme_icon(SNAME("load"), SNAME("FileDialog")), ETR("Load"), static_cast<int>(MenuOption::MENU_LOAD));
 	options_menu->set_item_tooltip(-1, ETR("Load existing color palette."));
 
+#ifdef TOOLS_ENABLED
 	if (Engine::get_singleton()->is_editor_hint()) {
-		options_menu->add_icon_item(get_theme_icon(SNAME("load"), SNAME("FileDialog")), RTR("Quick Load"), static_cast<int>(MenuOption::MENU_QUICKLOAD));
-		options_menu->set_item_tooltip(-1, ETR("Load existing color palette."));
+		options_menu->add_icon_item(get_theme_icon(SNAME("load"), SNAME("FileDialog")), TTRC("Quick Load"), static_cast<int>(MenuOption::MENU_QUICKLOAD));
+		options_menu->set_item_tooltip(-1, TTRC("Load existing color palette."));
 	}
+#endif // TOOLS_ENABLED
 
-	if (!presets.is_empty()) {
-		options_menu->add_icon_item(get_theme_icon(SNAME("clear"), SNAME("FileDialog")), RTR("Clear"), static_cast<int>(MenuOption::MENU_CLEAR));
-		options_menu->set_item_tooltip(-1, ETR("Clear the currently loaded color palettes in the picker."));
-	}
+	options_menu->add_icon_item(get_theme_icon(SNAME("clear"), SNAME("FileDialog")), ETR("Clear"), static_cast<int>(MenuOption::MENU_CLEAR));
+	options_menu->set_item_tooltip(-1, ETR("Clear the currently loaded color palettes in the picker."));
+	options_menu->set_item_disabled(-1, presets.is_empty());
 }
 
 void ColorPicker::_options_menu_cbk(int p_which) {
-	if (!file_dialog) {
-		file_dialog = memnew(FileDialog);
-		add_child(file_dialog, false, INTERNAL_MODE_FRONT);
-		file_dialog->connect("file_selected", callable_mp(this, &ColorPicker::_palette_file_selected));
-		file_dialog->set_access(FileDialog::ACCESS_FILESYSTEM);
-		file_dialog->set_current_dir(Engine::get_singleton()->is_editor_hint() ? "res://" : "user://");
-	}
+	_ensure_file_dialog();
 
 	MenuOption option = static_cast<MenuOption>(p_which);
 	switch (option) {
@@ -2087,7 +2070,6 @@ ColorPicker::ColorPicker() {
 	mode_btns[0]->set_pressed(true);
 
 	btn_mode = memnew(MenuButton);
-	btn_mode->set_text("...");
 	btn_mode->set_flat(false);
 	mode_hbc->add_child(btn_mode);
 	btn_mode->set_toggle_mode(true);
@@ -2131,12 +2113,20 @@ ColorPicker::ColorPicker() {
 
 	text_type = memnew(Button);
 	hex_hbc->add_child(text_type);
+	text_type->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
+	text_type->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_ALWAYS);
 	text_type->set_text("#");
-	text_type->set_accessibility_name(ETR("Hexadecimal/Code Values"));
-	text_type->set_tooltip_text(ETR("Switch between hexadecimal and code values."));
+#ifdef TOOLS_ENABLED
 	if (Engine::get_singleton()->is_editor_hint()) {
+		text_type->set_accessibility_name(TTRC("Hexadecimal/Code Values"));
+		text_type->set_tooltip_text(TTRC("Switch between hexadecimal and code values."));
 		text_type->connect(SceneStringName(pressed), callable_mp(this, &ColorPicker::_text_type_toggled));
 	} else {
+		text_type->set_accessibility_name(ETR("Hexadecimal Values"));
+#else
+	{
+		text_type->set_accessibility_name(ETR("Hexadecimal Values"));
+#endif // TOOLS_ENABLED
 		text_type->set_flat(true);
 		text_type->set_focus_mode(FOCUS_NONE);
 		text_type->set_mouse_filter(MOUSE_FILTER_IGNORE);
@@ -2181,7 +2171,7 @@ ColorPicker::ColorPicker() {
 	palette_box->add_child(btn_preset);
 
 	menu_btn = memnew(MenuButton);
-	menu_btn->set_flat(true);
+	menu_btn->set_flat(false);
 	menu_btn->set_focus_mode(FOCUS_ALL);
 	menu_btn->set_tooltip_text(ETR("Show all options available."));
 	menu_btn->set_accessibility_name(ETR("All Options"));
@@ -2516,6 +2506,14 @@ Color ColorPresetButton::get_preset_color() const {
 	return preset_color;
 }
 
+String ColorPresetButton::get_tooltip(const Point2 &p_pos) const {
+	Color color = get_preset_color();
+	if (recent) {
+		return vformat(atr(ETR("Color: #%s\nLMB: Apply color")), color.to_html(color.a < 1));
+	}
+	return vformat(atr(ETR("Color: #%s\nLMB: Apply color\nRMB: Remove preset")), color.to_html(color.a < 1));
+}
+
 void ColorPresetButton::_bind_methods() {
 	BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPresetButton, foreground_style, "preset_fg");
 	BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, ColorPresetButton, focus_style, "preset_focus");
@@ -2523,10 +2521,13 @@ void ColorPresetButton::_bind_methods() {
 	BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, ColorPresetButton, overbright_indicator);
 }
 
-ColorPresetButton::ColorPresetButton(Color p_color, int p_size) {
+ColorPresetButton::ColorPresetButton(Color p_color, int p_size, bool p_recent) {
 	preset_color = p_color;
+	recent = p_recent;
 	set_toggle_mode(true);
 	set_custom_minimum_size(Size2(p_size, p_size));
+	set_accessibility_name(vformat(atr(ETR("Color: #%s")), p_color.to_html(p_color.a < 1)));
+	set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
 }
 
 ColorPresetButton::~ColorPresetButton() {

+ 13 - 3
scene/gui/color_picker.h

@@ -54,6 +54,7 @@ class ColorPresetButton : public BaseButton {
 	GDCLASS(ColorPresetButton, BaseButton);
 
 	Color preset_color;
+	bool recent = false;
 
 	struct ThemeCache {
 		Ref<StyleBox> foreground_style;
@@ -71,7 +72,9 @@ public:
 	void set_preset_color(const Color &p_color);
 	Color get_preset_color() const;
 
-	ColorPresetButton(Color p_color, int p_size);
+	virtual String get_tooltip(const Point2 &p_pos) const override;
+
+	ColorPresetButton(Color p_color, int p_size, bool p_recent);
 	~ColorPresetButton();
 };
 
@@ -181,7 +184,7 @@ private:
 	HBoxContainer *hex_hbc = nullptr;
 	MenuButton *btn_mode = nullptr;
 	Button *mode_btns[MODE_BUTTON_COUNT];
-	Ref<ButtonGroup> mode_group = nullptr;
+	Ref<ButtonGroup> mode_group;
 	ColorPresetButton *selected_recent_preset = nullptr;
 	Ref<ButtonGroup> preset_group;
 	Ref<ButtonGroup> recent_preset_group;
@@ -301,7 +304,9 @@ private:
 	void _update_controls();
 	void _update_color(bool p_update_sliders = true);
 	void _update_text_value();
+#ifdef TOOLS_ENABLED
 	void _text_type_toggled();
+#endif // TOOLS_ENABLED
 	void _sample_input(const Ref<InputEvent> &p_event);
 	void _sample_draw();
 	void _slider_draw(int p_which);
@@ -344,6 +349,8 @@ private:
 	bool _can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const;
 	void _drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control);
 
+	void _ensure_file_dialog();
+
 protected:
 	virtual void _update_theme_item_cache() override;
 
@@ -355,7 +362,10 @@ public:
 	void set_editor_settings(Object *p_editor_settings);
 	void set_quick_open_callback(const Callable &p_file_selected);
 	void set_palette_saved_callback(const Callable &p_palette_saved);
+
+	void _quick_open_palette_file_selected(const String &p_path);
 #endif
+
 	HSlider *get_slider(int idx);
 	Vector<float> get_active_slider_values();
 
@@ -373,7 +383,7 @@ public:
 	Color get_pick_color() const;
 	void set_old_color(const Color &p_color);
 	Color get_old_color() const;
-	void _quick_open_palette_file_selected(const String &p_path);
+
 	void _palette_file_selected(const String &p_path);
 
 	void set_display_old_color(bool p_enabled);