Skip to content

Commit

Permalink
[feat] Save color palette as resources to reuse later
Browse files Browse the repository at this point in the history
godotengine/godot-proposals#7946

The lack of a palette library slows down development time because swatches contain many colors that may not match the mood an artist is trying to achieve. This can hinder their workflow as they search for the right color within a large set of mostly irrelevant options.
  • Loading branch information
nongvantinh committed May 5, 2024
1 parent 7ebc866 commit 99416da
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
70 changes: 69 additions & 1 deletion scene/gui/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "core/math/color.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "editor/editor_node.h"
#include "scene/gui/color_mode.h"
#include "scene/gui/margin_container.h"
#include "scene/resources/image_texture.h"
Expand Down Expand Up @@ -75,6 +76,8 @@ void ColorPicker::_notification(int p_what) {
_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_icon(theme_cache.add_preset);
load_swatches->set_icon(get_editor_theme_icon(SNAME("Load")));
save_swatches->set_icon(get_editor_theme_icon(SNAME("Save")));

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));
Expand Down Expand Up @@ -774,6 +777,43 @@ void ColorPicker::_add_recent_preset_button(int p_size, const Color &p_color) {
btn_preset_new->connect("toggled", callable_mp(this, &ColorPicker::_recent_preset_pressed).bind(btn_preset_new));
}

void ColorPicker::_load_palette() {
List<String> extensions;
ResourceLoader::get_recognized_extensions_for_type("Palette", &extensions);

file_dialog->set_title(TTR("Load Palette"));
file_dialog->clear_filters();
for (const String &K : extensions) {
file_dialog->add_filter("*." + K);
}

file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
file_dialog->set_current_file("");
file_dialog->popup_centered_ratio();
}

void ColorPicker::_save_palette() {
Palette *palette = memnew(Palette);
palette->set_colors(get_presets());
EditorNode::get_singleton()->save_resource(palette);
}

void ColorPicker::_load_file(const String &p_path) {
Ref<Palette> palette = ResourceLoader::load(p_path, "", ResourceFormatLoader::CACHE_MODE_IGNORE);
if (palette.is_valid()) {
preset_cache.clear();
presets.clear();

PackedColorArray saved_presets = palette->get_colors();
for (int i = 0; i < saved_presets.size(); ++i) {
preset_cache.push_back(saved_presets[i]);
presets.push_back(saved_presets[i]);
}

_update_presets();
}
}

void ColorPicker::_show_hide_preset(const bool &p_is_btn_pressed, Button *p_btn_preset, Container *p_preset_container) {
if (p_is_btn_pressed) {
p_preset_container->show();
Expand Down Expand Up @@ -1809,6 +1849,10 @@ void ColorPicker::_bind_methods() {
}

ColorPicker::ColorPicker() {
file_dialog = memnew(EditorFileDialog);
add_child(file_dialog);
file_dialog->connect("file_selected", callable_mp(this, &ColorPicker::_load_file));

internal_margin = memnew(MarginContainer);
add_child(internal_margin, false, INTERNAL_MODE_FRONT);

Expand Down Expand Up @@ -1991,14 +2035,38 @@ ColorPicker::ColorPicker() {

preset_group.instantiate();

HBoxContainer *swatch_box = memnew(HBoxContainer);
swatch_box->set_h_size_flags(SIZE_EXPAND_FILL);
real_vbox->add_child(swatch_box);

btn_preset = memnew(Button);
btn_preset->set_text("Swatches");
btn_preset->set_flat(true);
btn_preset->set_toggle_mode(true);
btn_preset->set_focus_mode(FOCUS_NONE);
btn_preset->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
btn_preset->connect("toggled", callable_mp(this, &ColorPicker::_show_hide_preset).bind(btn_preset, preset_container));
real_vbox->add_child(btn_preset);
swatch_box->add_child(btn_preset);

HBoxContainer *padding_box = memnew(HBoxContainer);
padding_box->set_h_size_flags(SIZE_EXPAND_FILL);
swatch_box->add_child(padding_box);

load_swatches = memnew(Button);
load_swatches->set_flat(true);
load_swatches->set_tooltip_text("Load existing color palette");
load_swatches->set_toggle_mode(true);
load_swatches->set_focus_mode(FOCUS_NONE);
load_swatches->connect("pressed", callable_mp(this, &ColorPicker::_load_palette));
swatch_box->add_child(load_swatches);

save_swatches = memnew(Button);
save_swatches->set_flat(true);
save_swatches->set_tooltip_text("Save the current color palette to reuse latter");
save_swatches->set_toggle_mode(true);
save_swatches->set_focus_mode(FOCUS_NONE);
save_swatches->connect("pressed", callable_mp(this, &ColorPicker::_save_palette));
swatch_box->add_child(save_swatches);

real_vbox->add_child(preset_container);

Expand Down
8 changes: 8 additions & 0 deletions scene/gui/color_picker.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef COLOR_PICKER_H
#define COLOR_PICKER_H

#include "editor/gui/editor_file_dialog.h"

Check failure on line 34 in scene/gui/color_picker.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release)

editor/gui/editor_file_dialog.h: No such file or directory

Check failure on line 34 in scene/gui/color_picker.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Minimal template (target=template_release, everything disabled)

editor/gui/editor_file_dialog.h: No such file or directory
#include "scene/gui/aspect_ratio_container.h"
#include "scene/gui/box_container.h"
#include "scene/gui/button.h"
Expand All @@ -46,6 +47,7 @@
#include "scene/gui/slider.h"
#include "scene/gui/spin_box.h"
#include "scene/gui/texture_rect.h"
#include "scene/resources/palette.h"
#include "scene/resources/style_box_flat.h"

class ColorMode;
Expand Down Expand Up @@ -134,6 +136,9 @@ class ColorPicker : public VBoxContainer {
Label *picker_preview_label = nullptr;
Ref<StyleBoxFlat> picker_preview_style_box;
Color picker_color;
EditorFileDialog *file_dialog = nullptr;
Button *load_swatches = nullptr;
Button *save_swatches = nullptr;

MarginContainer *internal_margin = nullptr;
Control *uv_edit = nullptr;
Expand Down Expand Up @@ -286,6 +291,9 @@ class ColorPicker : public VBoxContainer {
inline int _get_preset_size();
void _add_preset_button(int p_size, const Color &p_color);
void _add_recent_preset_button(int p_size, const Color &p_color);
void _save_palette();
void _load_palette();
void _load_file(const String &p_path);

void _show_hide_preset(const bool &p_is_btn_pressed, Button *p_btn_preset, Container *p_preset_container);
void _update_drop_down_arrow(const bool &p_is_btn_pressed, Button *p_btn_preset);
Expand Down
2 changes: 2 additions & 0 deletions scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
#include "scene/resources/multimesh.h"
#include "scene/resources/navigation_mesh.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/palette.h"
#include "scene/resources/particle_process_material.h"
#include "scene/resources/physics_material.h"
#include "scene/resources/placeholder_textures.h"
Expand Down Expand Up @@ -960,6 +961,7 @@ void register_scene_types() {
GDREGISTER_CLASS(FontFile);
GDREGISTER_CLASS(FontVariation);
GDREGISTER_CLASS(SystemFont);
GDREGISTER_CLASS(Palette);

GDREGISTER_CLASS(Curve);

Expand Down
51 changes: 51 additions & 0 deletions scene/resources/palette.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**************************************************************************/
/* palette.cpp */
/**************************************************************************/
/* 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. */
/**************************************************************************/

#include "palette.h"

void Palette::set_colors(const PackedColorArray &p_colors) {
if (colors == p_colors) {
return;
}
colors = p_colors;
}

PackedColorArray Palette::get_colors() const {
return colors;
}

void Palette::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_colors", "color"), &Palette::set_colors);
ClassDB::bind_method(D_METHOD("get_colors"), &Palette::get_colors);

ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "colors"), "set_colors", "get_colors");
}
Palette::Palette() {
}
54 changes: 54 additions & 0 deletions scene/resources/palette.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**************************************************************************/
/* palette.h */
/**************************************************************************/
/* 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 PALETTE_H
#define PALETTE_H

#include "core/io/resource.h"
#include "core/variant/typed_array.h"
#include "scene/resources/animation.h"

class Palette : public Resource {
GDCLASS(Palette, Resource)

private:
PackedColorArray colors;

protected:
static void _bind_methods();

public:
void set_colors(const PackedColorArray &p_colors);
PackedColorArray get_colors() const;

Palette();
};

#endif // PALETTE_H

0 comments on commit 99416da

Please sign in to comment.