2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* color_picker.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "color_picker.h"
|
|
|
|
|
2020-04-28 13:19:37 +00:00
|
|
|
#include "core/input/input.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/keyboard.h"
|
|
|
|
#include "core/os/os.h"
|
2022-02-15 17:06:48 +00:00
|
|
|
#include "scene/main/window.h"
|
2018-09-24 14:28:18 +00:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2019-10-09 14:11:04 +00:00
|
|
|
#include "editor/editor_settings.h"
|
2018-09-24 14:28:18 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-07-07 18:17:44 +00:00
|
|
|
List<Color> ColorPicker::preset_cache;
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ColorPicker::_notification(int p_what) {
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2015-11-21 05:51:42 +00:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2016-02-02 00:32:47 +00:00
|
|
|
_update_color();
|
2018-09-24 14:28:18 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2021-07-07 18:17:44 +00:00
|
|
|
if (preset_cache.is_empty()) {
|
|
|
|
PackedColorArray saved_presets = EditorSettings::get_singleton()->get_project_metadata("color_picker", "presets", PackedColorArray());
|
|
|
|
for (int i = 0; i < saved_presets.size(); i++) {
|
|
|
|
preset_cache.push_back(saved_presets[i]);
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 14:28:18 +00:00
|
|
|
|
2021-07-07 18:17:44 +00:00
|
|
|
for (int i = 0; i < preset_cache.size(); i++) {
|
|
|
|
presets.push_back(preset_cache[i]);
|
2018-09-24 14:28:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-10-02 20:07:42 +00:00
|
|
|
[[fallthrough]];
|
|
|
|
}
|
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
btn_pick->set_icon(get_theme_icon(SNAME("screen_picker"), SNAME("ColorPicker")));
|
|
|
|
btn_add_preset->set_icon(get_theme_icon(SNAME("add_preset")));
|
|
|
|
|
|
|
|
uv_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("sv_width")), get_theme_constant(SNAME("sv_height"))));
|
|
|
|
w_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("h_width")), 0));
|
|
|
|
|
|
|
|
wheel_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("sv_width")), get_theme_constant(SNAME("sv_height"))));
|
2022-02-08 09:14:58 +00:00
|
|
|
wheel_margin->add_theme_constant_override("margin_bottom", 8 * get_theme_default_base_scale());
|
2021-10-02 20:07:42 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2021-10-02 20:07:42 +00:00
|
|
|
labels[i]->set_custom_minimum_size(Size2(get_theme_constant(SNAME("label_width")), 0));
|
2021-07-17 21:22:52 +00:00
|
|
|
set_offset((Side)i, get_offset((Side)i) + get_theme_constant(SNAME("margin")));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-10-02 20:07:42 +00:00
|
|
|
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
// Adjust for the width of the "Script" icon.
|
|
|
|
text_type->set_custom_minimum_size(Size2(28 * get_theme_default_base_scale(), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
_update_presets();
|
|
|
|
_update_controls();
|
2017-07-03 22:52:45 +00:00
|
|
|
} break;
|
2021-10-02 20:07:42 +00:00
|
|
|
|
2017-07-03 22:52:45 +00:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
2017-08-24 20:58:51 +00:00
|
|
|
Popup *p = Object::cast_to<Popup>(get_parent());
|
2020-05-14 14:41:43 +00:00
|
|
|
if (p) {
|
2021-07-17 21:22:52 +00:00
|
|
|
p->set_size(Size2(get_combined_minimum_size().width + get_theme_constant(SNAME("margin")) * 2, get_combined_minimum_size().height + get_theme_constant(SNAME("margin")) * 2));
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-07-03 22:52:45 +00:00
|
|
|
} break;
|
2021-10-02 20:07:42 +00:00
|
|
|
|
2020-03-04 16:36:09 +00:00
|
|
|
case NOTIFICATION_WM_CLOSE_REQUEST: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (screen != nullptr && screen->is_visible()) {
|
2019-01-28 03:16:40 +00:00
|
|
|
screen->hide();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-06-18 20:16:54 +00:00
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 13:51:46 +00:00
|
|
|
Ref<Shader> ColorPicker::wheel_shader;
|
|
|
|
Ref<Shader> ColorPicker::circle_shader;
|
|
|
|
|
|
|
|
void ColorPicker::init_shaders() {
|
2021-06-17 22:03:09 +00:00
|
|
|
wheel_shader.instantiate();
|
2021-07-19 06:06:51 +00:00
|
|
|
wheel_shader->set_code(R"(
|
2021-08-18 01:09:22 +00:00
|
|
|
// ColorPicker wheel shader.
|
|
|
|
|
2021-07-19 06:06:51 +00:00
|
|
|
shader_type canvas_item;
|
|
|
|
|
|
|
|
void fragment() {
|
|
|
|
float x = UV.x - 0.5;
|
|
|
|
float y = UV.y - 0.5;
|
|
|
|
float a = atan(y, x);
|
|
|
|
x += 0.001;
|
|
|
|
y += 0.001;
|
|
|
|
float b = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
|
|
|
|
x -= 0.002;
|
|
|
|
float b2 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
|
|
|
|
y -= 0.002;
|
|
|
|
float b3 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
|
|
|
|
x += 0.002;
|
|
|
|
float b4 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
|
|
|
|
|
|
|
|
COLOR = vec4(clamp((abs(fract(((a - TAU) / TAU) + vec3(3.0, 2.0, 1.0) / 3.0) * 6.0 - 3.0) - 1.0), 0.0, 1.0), (b + b2 + b3 + b4) / 4.00);
|
|
|
|
}
|
|
|
|
)");
|
2021-05-13 13:51:46 +00:00
|
|
|
|
2021-06-17 22:03:09 +00:00
|
|
|
circle_shader.instantiate();
|
2021-07-19 06:06:51 +00:00
|
|
|
circle_shader->set_code(R"(
|
2021-08-18 01:09:22 +00:00
|
|
|
// ColorPicker circle shader.
|
|
|
|
|
2021-07-19 06:06:51 +00:00
|
|
|
shader_type canvas_item;
|
|
|
|
|
|
|
|
uniform float v = 1.0;
|
|
|
|
|
|
|
|
void fragment() {
|
|
|
|
float x = UV.x - 0.5;
|
|
|
|
float y = UV.y - 0.5;
|
|
|
|
float a = atan(y, x);
|
|
|
|
x += 0.001;
|
|
|
|
y += 0.001;
|
|
|
|
float b = float(sqrt(x * x + y * y) < 0.5);
|
|
|
|
x -= 0.002;
|
|
|
|
float b2 = float(sqrt(x * x + y * y) < 0.5);
|
|
|
|
y -= 0.002;
|
|
|
|
float b3 = float(sqrt(x * x + y * y) < 0.5);
|
|
|
|
x += 0.002;
|
|
|
|
float b4 = float(sqrt(x * x + y * y) < 0.5);
|
|
|
|
|
|
|
|
COLOR = vec4(mix(vec3(1.0), clamp(abs(fract(vec3((a - TAU) / TAU) + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - vec3(3.0)) - vec3(1.0), 0.0, 1.0), ((float(sqrt(x * x + y * y)) * 2.0)) / 1.0) * vec3(v), (b + b2 + b3 + b4) / 4.00);
|
|
|
|
})");
|
2021-05-13 13:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::finish_shaders() {
|
|
|
|
wheel_shader.unref();
|
|
|
|
circle_shader.unref();
|
|
|
|
}
|
|
|
|
|
2016-07-01 13:55:35 +00:00
|
|
|
void ColorPicker::set_focus_on_line_edit() {
|
2021-07-17 21:22:52 +00:00
|
|
|
c_text->call_deferred(SNAME("grab_focus"));
|
2016-07-01 13:55:35 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ColorPicker::_update_controls() {
|
2019-05-03 17:38:45 +00:00
|
|
|
const char *rgb[3] = { "R", "G", "B" };
|
|
|
|
const char *hsv[3] = { "H", "S", "V" };
|
|
|
|
|
|
|
|
if (hsv_mode_enabled) {
|
2020-05-14 14:41:43 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2019-05-03 17:38:45 +00:00
|
|
|
labels[i]->set_text(hsv[i]);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-05-03 17:38:45 +00:00
|
|
|
} else {
|
2020-05-14 14:41:43 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2019-05-03 17:38:45 +00:00
|
|
|
labels[i]->set_text(rgb[i]);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-05-03 17:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (hsv_mode_enabled) {
|
|
|
|
set_raw_mode(false);
|
|
|
|
btn_raw->set_disabled(true);
|
|
|
|
} else if (raw_mode_enabled) {
|
|
|
|
set_hsv_mode(false);
|
|
|
|
btn_hsv->set_disabled(true);
|
|
|
|
} else {
|
|
|
|
btn_raw->set_disabled(false);
|
|
|
|
btn_hsv->set_disabled(false);
|
|
|
|
}
|
|
|
|
|
2021-02-17 05:30:56 +00:00
|
|
|
if (raw_mode_enabled) {
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2022-02-08 09:14:58 +00:00
|
|
|
scroll[i]->remove_theme_icon_override("grabber");
|
|
|
|
scroll[i]->remove_theme_icon_override("grabber_highlight");
|
|
|
|
scroll[i]->remove_theme_style_override("slider");
|
|
|
|
scroll[i]->remove_theme_style_override("grabber_area");
|
|
|
|
scroll[i]->remove_theme_style_override("grabber_area_highlight");
|
2021-02-17 05:30:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-07 11:15:29 +00:00
|
|
|
Ref<StyleBoxEmpty> style_box_empty(memnew(StyleBoxEmpty));
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> bar_arrow = get_theme_icon(SNAME("bar_arrow"));
|
2021-04-07 11:15:29 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2022-02-08 09:14:58 +00:00
|
|
|
scroll[i]->add_theme_icon_override("grabber", bar_arrow);
|
|
|
|
scroll[i]->add_theme_icon_override("grabber_highlight", bar_arrow);
|
|
|
|
scroll[i]->add_theme_style_override("slider", style_box_empty);
|
|
|
|
scroll[i]->add_theme_style_override("grabber_area", style_box_empty);
|
|
|
|
scroll[i]->add_theme_style_override("grabber_area_highlight", style_box_empty);
|
2021-02-17 05:30:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
if (edit_alpha) {
|
|
|
|
values[3]->show();
|
|
|
|
scroll[3]->show();
|
|
|
|
labels[3]->show();
|
|
|
|
} else {
|
|
|
|
values[3]->hide();
|
|
|
|
scroll[3]->hide();
|
|
|
|
labels[3]->hide();
|
|
|
|
}
|
2021-02-23 10:22:46 +00:00
|
|
|
|
|
|
|
switch (picker_type) {
|
|
|
|
case SHAPE_HSV_RECTANGLE:
|
|
|
|
wheel_edit->hide();
|
|
|
|
w_edit->show();
|
|
|
|
uv_edit->show();
|
|
|
|
break;
|
|
|
|
case SHAPE_HSV_WHEEL:
|
|
|
|
wheel_edit->show();
|
|
|
|
w_edit->hide();
|
|
|
|
uv_edit->hide();
|
|
|
|
|
|
|
|
wheel->set_material(wheel_mat);
|
|
|
|
break;
|
|
|
|
case SHAPE_VHS_CIRCLE:
|
|
|
|
wheel_edit->show();
|
|
|
|
w_edit->show();
|
|
|
|
uv_edit->hide();
|
|
|
|
|
|
|
|
wheel->set_material(circle_mat);
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:52:54 +00:00
|
|
|
void ColorPicker::_set_pick_color(const Color &p_color, bool p_update_sliders) {
|
2017-03-05 15:44:50 +00:00
|
|
|
color = p_color;
|
2016-02-06 02:35:15 +00:00
|
|
|
if (color != last_hsv) {
|
2017-03-05 15:44:50 +00:00
|
|
|
h = color.get_h();
|
|
|
|
s = color.get_s();
|
|
|
|
v = color.get_v();
|
2016-02-06 02:35:15 +00:00
|
|
|
last_hsv = color;
|
|
|
|
}
|
2016-02-02 00:32:47 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree()) {
|
2016-02-02 00:32:47 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-02-02 00:32:47 +00:00
|
|
|
|
2019-05-03 17:38:45 +00:00
|
|
|
_update_color(p_update_sliders);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:52:54 +00:00
|
|
|
void ColorPicker::set_pick_color(const Color &p_color) {
|
|
|
|
_set_pick_color(p_color, true); //because setters can't have more arguments
|
|
|
|
}
|
|
|
|
|
2020-01-05 17:01:24 +00:00
|
|
|
void ColorPicker::set_old_color(const Color &p_color) {
|
|
|
|
old_color = p_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::set_display_old_color(bool p_enabled) {
|
|
|
|
display_old_color = p_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColorPicker::is_displaying_old_color() const {
|
|
|
|
return display_old_color;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ColorPicker::set_edit_alpha(bool p_show) {
|
2017-03-05 15:44:50 +00:00
|
|
|
edit_alpha = p_show;
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_controls();
|
2016-02-02 00:32:47 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree()) {
|
2016-02-02 00:32:47 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-02-02 00:32:47 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_color();
|
2015-11-21 05:51:42 +00:00
|
|
|
sample->update();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ColorPicker::is_editing_alpha() const {
|
|
|
|
return edit_alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::_value_changed(double) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (updating) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-05-03 17:38:45 +00:00
|
|
|
if (hsv_mode_enabled) {
|
2021-02-19 16:53:00 +00:00
|
|
|
h = scroll[0]->get_value() / 360.0;
|
|
|
|
s = scroll[1]->get_value() / 100.0;
|
|
|
|
v = scroll[2]->get_value() / 100.0;
|
|
|
|
color.set_hsv(h, s, v, scroll[3]->get_value() / 255.0);
|
|
|
|
|
|
|
|
last_hsv = color;
|
|
|
|
|
2019-05-03 17:38:45 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
color.components[i] = scroll[i]->get_value() / (raw_mode_enabled ? 1.0 : 255.0);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 12:52:54 +00:00
|
|
|
_set_pick_color(color, false);
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 16:43:34 +00:00
|
|
|
void ColorPicker::_html_submitted(const String &p_html) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (updating || text_is_constructor || !c_text->is_visible()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-10-05 06:15:41 +00:00
|
|
|
float last_alpha = color.a;
|
2014-02-10 01:10:30 +00:00
|
|
|
color = Color::html(p_html);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_editing_alpha()) {
|
2018-10-05 06:15:41 +00:00
|
|
|
color.a = last_alpha;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-02-02 00:32:47 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree()) {
|
2016-02-02 00:32:47 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-02-02 00:32:47 +00:00
|
|
|
|
2017-01-14 14:07:57 +00:00
|
|
|
set_pick_color(color);
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-03 17:38:45 +00:00
|
|
|
void ColorPicker::_update_color(bool p_update_sliders) {
|
2017-03-05 15:44:50 +00:00
|
|
|
updating = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-05-03 17:38:45 +00:00
|
|
|
if (p_update_sliders) {
|
|
|
|
if (hsv_mode_enabled) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2019-06-28 14:25:00 +00:00
|
|
|
scroll[i]->set_step(1.0);
|
2019-05-03 17:38:45 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:25:00 +00:00
|
|
|
scroll[0]->set_max(359);
|
2019-05-03 17:38:45 +00:00
|
|
|
scroll[0]->set_value(h * 360.0);
|
|
|
|
scroll[1]->set_max(100);
|
|
|
|
scroll[1]->set_value(s * 100.0);
|
|
|
|
scroll[2]->set_max(100);
|
|
|
|
scroll[2]->set_value(v * 100.0);
|
2019-06-28 14:25:00 +00:00
|
|
|
scroll[3]->set_max(255);
|
|
|
|
scroll[3]->set_value(color.components[3] * 255.0);
|
2016-07-03 01:08:25 +00:00
|
|
|
} else {
|
2019-05-03 17:38:45 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (raw_mode_enabled) {
|
|
|
|
scroll[i]->set_step(0.01);
|
|
|
|
scroll[i]->set_max(100);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (i == 3) {
|
2019-05-03 17:38:45 +00:00
|
|
|
scroll[i]->set_max(1);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-05-03 17:38:45 +00:00
|
|
|
scroll[i]->set_value(color.components[i]);
|
|
|
|
} else {
|
|
|
|
scroll[i]->set_step(1);
|
|
|
|
const float byte_value = color.components[i] * 255.0;
|
|
|
|
scroll[i]->set_max(next_power_of_2(MAX(255, byte_value)) - 1);
|
|
|
|
scroll[i]->set_value(byte_value);
|
|
|
|
}
|
|
|
|
}
|
2016-07-03 01:08:25 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 01:08:25 +00:00
|
|
|
_update_text_value();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2015-11-21 05:51:42 +00:00
|
|
|
sample->update();
|
2017-06-15 03:22:33 +00:00
|
|
|
uv_edit->update();
|
|
|
|
w_edit->update();
|
2021-02-17 05:30:56 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
scroll[i]->update();
|
|
|
|
}
|
2021-02-23 10:22:46 +00:00
|
|
|
wheel->update();
|
|
|
|
wheel_uv->update();
|
2017-03-05 15:44:50 +00:00
|
|
|
updating = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ColorPicker::_update_presets() {
|
2021-08-18 00:09:48 +00:00
|
|
|
int preset_size = _get_preset_size();
|
|
|
|
// Only update the preset button size if it has changed.
|
|
|
|
if (preset_size != prev_preset_size) {
|
|
|
|
prev_preset_size = preset_size;
|
|
|
|
btn_add_preset->set_custom_minimum_size(Size2(preset_size, preset_size));
|
|
|
|
for (int i = 1; i < preset_container->get_child_count(); i++) {
|
|
|
|
ColorPresetButton *cpb = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
|
|
|
|
cpb->set_custom_minimum_size(Size2(preset_size, preset_size));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Only load preset buttons when the only child is the add-preset button.
|
|
|
|
if (preset_container->get_child_count() == 1) {
|
|
|
|
for (int i = 0; i < preset_cache.size(); i++) {
|
|
|
|
_add_preset_button(preset_size, preset_cache[i]);
|
|
|
|
}
|
|
|
|
_notification(NOTIFICATION_VISIBILITY_CHANGED);
|
2017-09-27 19:44:48 +00:00
|
|
|
}
|
2015-12-24 02:17:09 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ColorPicker::_text_type_toggled() {
|
2016-01-26 22:10:56 +00:00
|
|
|
text_is_constructor = !text_is_constructor;
|
|
|
|
if (text_is_constructor) {
|
|
|
|
text_type->set_text("");
|
2021-07-17 21:22:52 +00:00
|
|
|
text_type->set_icon(get_theme_icon(SNAME("Script"), SNAME("EditorIcons")));
|
2019-01-26 21:07:32 +00:00
|
|
|
|
|
|
|
c_text->set_editable(false);
|
2016-01-26 22:10:56 +00:00
|
|
|
} else {
|
|
|
|
text_type->set_text("#");
|
2020-04-01 23:20:12 +00:00
|
|
|
text_type->set_icon(nullptr);
|
2019-01-26 21:07:32 +00:00
|
|
|
|
|
|
|
c_text->set_editable(true);
|
2016-01-26 22:10:56 +00:00
|
|
|
}
|
|
|
|
_update_color();
|
|
|
|
}
|
|
|
|
|
2017-01-14 14:07:57 +00:00
|
|
|
Color ColorPicker::get_pick_color() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2021-02-23 10:22:46 +00:00
|
|
|
void ColorPicker::set_picker_shape(PickerShapeType p_picker_type) {
|
|
|
|
ERR_FAIL_INDEX(p_picker_type, SHAPE_MAX);
|
|
|
|
picker_type = p_picker_type;
|
|
|
|
|
|
|
|
_update_controls();
|
|
|
|
_update_color();
|
|
|
|
}
|
|
|
|
|
|
|
|
ColorPicker::PickerShapeType ColorPicker::get_picker_shape() const {
|
|
|
|
return picker_type;
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:09:48 +00:00
|
|
|
inline int ColorPicker::_get_preset_size() {
|
|
|
|
return (int(get_minimum_size().width) - (preset_container->get_theme_constant(SNAME("hseparation")) * (preset_column_count - 1))) / preset_column_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::_add_preset_button(int p_size, const Color &p_color) {
|
|
|
|
ColorPresetButton *btn_preset = memnew(ColorPresetButton(p_color));
|
|
|
|
btn_preset->set_preset_color(p_color);
|
|
|
|
btn_preset->set_custom_minimum_size(Size2(p_size, p_size));
|
|
|
|
btn_preset->connect("gui_input", callable_mp(this, &ColorPicker::_preset_input), varray(p_color));
|
|
|
|
btn_preset->set_tooltip(vformat(RTR("Color: #%s\nLMB: Apply color\nRMB: Remove preset"), p_color.to_html(p_color.a < 1)));
|
|
|
|
preset_container->add_child(btn_preset);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ColorPicker::add_preset(const Color &p_color) {
|
2015-12-24 02:17:09 +00:00
|
|
|
if (presets.find(p_color)) {
|
|
|
|
presets.move_to_back(presets.find(p_color));
|
2021-08-18 00:09:48 +00:00
|
|
|
|
|
|
|
// Find button to move to the end.
|
|
|
|
for (int i = 1; i < preset_container->get_child_count(); i++) {
|
|
|
|
ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
|
|
|
|
if (current_btn && p_color == current_btn->get_preset_color()) {
|
|
|
|
preset_container->move_child(current_btn, preset_container->get_child_count() - 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-24 02:17:09 +00:00
|
|
|
} else {
|
|
|
|
presets.push_back(p_color);
|
2021-07-07 18:17:44 +00:00
|
|
|
preset_cache.push_back(p_color);
|
2021-08-18 00:09:48 +00:00
|
|
|
|
|
|
|
_add_preset_button(_get_preset_size(), p_color);
|
2015-12-24 02:17:09 +00:00
|
|
|
}
|
2018-12-12 20:55:29 +00:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2020-02-17 21:06:54 +00:00
|
|
|
PackedColorArray arr_to_save = get_presets();
|
2018-12-12 20:55:29 +00:00
|
|
|
EditorSettings::get_singleton()->set_project_metadata("color_picker", "presets", arr_to_save);
|
|
|
|
}
|
|
|
|
#endif
|
2015-12-24 02:17:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-03 14:12:38 +00:00
|
|
|
void ColorPicker::erase_preset(const Color &p_color) {
|
|
|
|
if (presets.find(p_color)) {
|
|
|
|
presets.erase(presets.find(p_color));
|
2021-07-07 18:17:44 +00:00
|
|
|
preset_cache.erase(preset_cache.find(p_color));
|
2021-08-18 00:09:48 +00:00
|
|
|
|
|
|
|
// Find preset button to remove.
|
|
|
|
for (int i = 1; i < preset_container->get_child_count(); i++) {
|
|
|
|
ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
|
|
|
|
if (current_btn && p_color == current_btn->get_preset_color()) {
|
|
|
|
current_btn->queue_delete();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-12 20:55:29 +00:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2020-02-17 21:06:54 +00:00
|
|
|
PackedColorArray arr_to_save = get_presets();
|
2018-12-12 20:55:29 +00:00
|
|
|
EditorSettings::get_singleton()->set_project_metadata("color_picker", "presets", arr_to_save);
|
|
|
|
}
|
|
|
|
#endif
|
2018-10-03 14:12:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
PackedColorArray ColorPicker::get_presets() const {
|
|
|
|
PackedColorArray arr;
|
2018-10-03 14:12:38 +00:00
|
|
|
arr.resize(presets.size());
|
|
|
|
for (int i = 0; i < presets.size(); i++) {
|
|
|
|
arr.set(i, presets[i]);
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2019-05-03 17:38:45 +00:00
|
|
|
void ColorPicker::set_hsv_mode(bool p_enabled) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (hsv_mode_enabled == p_enabled || raw_mode_enabled) {
|
2019-05-03 17:38:45 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-05-03 17:38:45 +00:00
|
|
|
hsv_mode_enabled = p_enabled;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (btn_hsv->is_pressed() != p_enabled) {
|
2019-05-03 17:38:45 +00:00
|
|
|
btn_hsv->set_pressed(p_enabled);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-05-03 17:38:45 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree()) {
|
2019-05-03 17:38:45 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-05-03 17:38:45 +00:00
|
|
|
|
|
|
|
_update_controls();
|
|
|
|
_update_color();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColorPicker::is_hsv_mode() const {
|
|
|
|
return hsv_mode_enabled;
|
|
|
|
}
|
|
|
|
|
2015-11-21 05:51:42 +00:00
|
|
|
void ColorPicker::set_raw_mode(bool p_enabled) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (raw_mode_enabled == p_enabled || hsv_mode_enabled) {
|
2015-11-21 05:51:42 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
raw_mode_enabled = p_enabled;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (btn_raw->is_pressed() != p_enabled) {
|
2019-05-03 17:38:45 +00:00
|
|
|
btn_raw->set_pressed(p_enabled);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!is_inside_tree()) {
|
2016-02-02 00:32:47 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-02-02 00:32:47 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_controls();
|
|
|
|
_update_color();
|
|
|
|
}
|
|
|
|
|
2015-11-21 05:51:42 +00:00
|
|
|
bool ColorPicker::is_raw_mode() const {
|
|
|
|
return raw_mode_enabled;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 12:21:06 +00:00
|
|
|
void ColorPicker::set_deferred_mode(bool p_enabled) {
|
|
|
|
deferred_mode_enabled = p_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColorPicker::is_deferred_mode() const {
|
|
|
|
return deferred_mode_enabled;
|
|
|
|
}
|
|
|
|
|
2016-07-03 01:08:25 +00:00
|
|
|
void ColorPicker::_update_text_value() {
|
2018-03-09 20:02:58 +00:00
|
|
|
bool visible = true;
|
2016-07-03 01:08:25 +00:00
|
|
|
if (text_is_constructor) {
|
2019-01-26 21:07:32 +00:00
|
|
|
String t = "Color(" + String::num(color.r) + ", " + String::num(color.g) + ", " + String::num(color.b);
|
2020-05-14 14:41:43 +00:00
|
|
|
if (edit_alpha && color.a < 1) {
|
2019-01-26 21:07:32 +00:00
|
|
|
t += ", " + String::num(color.a) + ")";
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
t += ")";
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2016-07-03 01:08:25 +00:00
|
|
|
c_text->set_text(t);
|
|
|
|
}
|
2019-01-26 21:07:32 +00:00
|
|
|
|
|
|
|
if (color.r > 1 || color.g > 1 || color.b > 1 || color.r < 0 || color.g < 0 || color.b < 0) {
|
|
|
|
visible = false;
|
|
|
|
} else if (!text_is_constructor) {
|
|
|
|
c_text->set_text(color.to_html(edit_alpha && color.a < 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
text_type->set_visible(visible);
|
2018-03-09 20:02:58 +00:00
|
|
|
c_text->set_visible(visible);
|
2016-07-03 01:08:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 14:59:46 +00:00
|
|
|
void ColorPicker::_sample_input(const Ref<InputEvent> &p_event) {
|
|
|
|
const Ref<InputEventMouseButton> mb = p_event;
|
2021-08-13 21:31:57 +00:00
|
|
|
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
|
2021-05-10 14:59:46 +00:00
|
|
|
const Rect2 rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
|
|
|
|
if (rect_old.has_point(mb->get_position())) {
|
|
|
|
// Revert to the old color when left-clicking the old color sample.
|
|
|
|
color = old_color;
|
|
|
|
_update_color();
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2021-05-10 14:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 05:51:42 +00:00
|
|
|
void ColorPicker::_sample_draw() {
|
2020-01-05 17:01:24 +00:00
|
|
|
// Covers the right half of the sample if the old color is being displayed,
|
|
|
|
// or the whole sample if it's not being displayed.
|
|
|
|
Rect2 rect_new;
|
|
|
|
|
|
|
|
if (display_old_color) {
|
|
|
|
rect_new = Rect2(Point2(sample->get_size().width * 0.5, 0), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
|
|
|
|
|
|
|
|
// Draw both old and new colors for easier comparison (only if spawned from a ColorPickerButton).
|
|
|
|
const Rect2 rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
|
|
|
|
|
|
|
|
if (display_old_color && old_color.a < 1.0) {
|
2021-08-18 00:09:48 +00:00
|
|
|
sample->draw_texture_rect(get_theme_icon(SNAME("sample_bg"), SNAME("ColorPicker")), rect_old, true);
|
2020-01-05 17:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sample->draw_rect(rect_old, old_color);
|
|
|
|
|
|
|
|
if (old_color.r > 1 || old_color.g > 1 || old_color.b > 1) {
|
|
|
|
// Draw an indicator to denote that the old color is "overbright" and can't be displayed accurately in the preview.
|
2021-07-17 21:22:52 +00:00
|
|
|
sample->draw_texture(get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), Point2());
|
2020-01-05 17:01:24 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rect_new = Rect2(Point2(), Size2(sample->get_size().width, sample->get_size().height * 0.95));
|
|
|
|
}
|
2019-09-26 23:07:41 +00:00
|
|
|
|
2017-09-27 19:44:48 +00:00
|
|
|
if (color.a < 1.0) {
|
2021-08-18 00:09:48 +00:00
|
|
|
sample->draw_texture_rect(get_theme_icon(SNAME("sample_bg"), SNAME("ColorPicker")), rect_new, true);
|
2017-09-27 19:44:48 +00:00
|
|
|
}
|
2019-09-26 23:07:41 +00:00
|
|
|
|
2020-01-05 17:01:24 +00:00
|
|
|
sample->draw_rect(rect_new, color);
|
2019-09-26 23:07:41 +00:00
|
|
|
|
|
|
|
if (color.r > 1 || color.g > 1 || color.b > 1) {
|
2020-01-05 17:01:24 +00:00
|
|
|
// Draw an indicator to denote that the new color is "overbright" and can't be displayed accurately in the preview.
|
2021-07-17 21:22:52 +00:00
|
|
|
sample->draw_texture(get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), Point2(uv_edit->get_size().width * 0.5, 0));
|
2019-09-26 23:07:41 +00:00
|
|
|
}
|
2015-11-21 05:51:42 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-07-03 22:52:45 +00:00
|
|
|
void ColorPicker::_hsv_draw(int p_which, Control *c) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!c) {
|
2015-12-25 20:59:23 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-07-03 22:52:45 +00:00
|
|
|
if (p_which == 0) {
|
2016-10-19 22:43:49 +00:00
|
|
|
Vector<Point2> points;
|
|
|
|
Vector<Color> colors;
|
|
|
|
Vector<Color> colors2;
|
|
|
|
Color col = color;
|
2021-02-23 10:22:46 +00:00
|
|
|
Vector2 center = c->get_size() / 2.0;
|
|
|
|
|
|
|
|
switch (picker_type) {
|
|
|
|
case SHAPE_HSV_WHEEL: {
|
|
|
|
points.resize(4);
|
|
|
|
colors.resize(4);
|
|
|
|
colors2.resize(4);
|
|
|
|
real_t ring_radius_x = Math_SQRT12 * c->get_size().width * 0.42;
|
|
|
|
real_t ring_radius_y = Math_SQRT12 * c->get_size().height * 0.42;
|
|
|
|
|
|
|
|
points.set(0, center - Vector2(ring_radius_x, ring_radius_y));
|
|
|
|
points.set(1, center + Vector2(ring_radius_x, -ring_radius_y));
|
|
|
|
points.set(2, center + Vector2(ring_radius_x, ring_radius_y));
|
|
|
|
points.set(3, center + Vector2(-ring_radius_x, ring_radius_y));
|
|
|
|
colors.set(0, Color(1, 1, 1, 1));
|
|
|
|
colors.set(1, Color(1, 1, 1, 1));
|
|
|
|
colors.set(2, Color(0, 0, 0, 1));
|
|
|
|
colors.set(3, Color(0, 0, 0, 1));
|
|
|
|
c->draw_polygon(points, colors);
|
|
|
|
|
|
|
|
col.set_hsv(h, 1, 1);
|
|
|
|
col.a = 0;
|
|
|
|
colors2.set(0, col);
|
|
|
|
col.a = 1;
|
|
|
|
colors2.set(1, col);
|
|
|
|
col.set_hsv(h, 1, 0);
|
|
|
|
colors2.set(2, col);
|
|
|
|
col.a = 0;
|
|
|
|
colors2.set(3, col);
|
|
|
|
c->draw_polygon(points, colors2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SHAPE_HSV_RECTANGLE: {
|
|
|
|
points.resize(4);
|
|
|
|
colors.resize(4);
|
|
|
|
colors2.resize(4);
|
|
|
|
points.set(0, Vector2());
|
|
|
|
points.set(1, Vector2(c->get_size().x, 0));
|
|
|
|
points.set(2, c->get_size());
|
|
|
|
points.set(3, Vector2(0, c->get_size().y));
|
|
|
|
colors.set(0, Color(1, 1, 1, 1));
|
|
|
|
colors.set(1, Color(1, 1, 1, 1));
|
|
|
|
colors.set(2, Color(0, 0, 0, 1));
|
|
|
|
colors.set(3, Color(0, 0, 0, 1));
|
|
|
|
c->draw_polygon(points, colors);
|
|
|
|
col = color;
|
|
|
|
col.set_hsv(h, 1, 1);
|
|
|
|
col.a = 0;
|
|
|
|
colors2.set(0, col);
|
|
|
|
col.a = 1;
|
|
|
|
colors2.set(1, col);
|
|
|
|
col.set_hsv(h, 1, 0);
|
|
|
|
colors2.set(2, col);
|
|
|
|
col.a = 0;
|
|
|
|
colors2.set(3, col);
|
|
|
|
c->draw_polygon(points, colors2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> cursor = get_theme_icon(SNAME("picker_cursor"), SNAME("ColorPicker"));
|
2021-02-23 10:22:46 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
if (picker_type == SHAPE_VHS_CIRCLE) {
|
|
|
|
x = center.x + (center.x * Math::cos(h * Math_TAU) * s) - (cursor->get_width() / 2);
|
|
|
|
y = center.y + (center.y * Math::sin(h * Math_TAU) * s) - (cursor->get_height() / 2);
|
|
|
|
} else {
|
|
|
|
real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
|
|
|
|
real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
|
|
|
|
|
|
|
|
Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
|
|
|
|
x = CLAMP(real_size.x * s, 0, real_size.x) + corner_x - (cursor->get_width() / 2);
|
|
|
|
y = CLAMP(real_size.y - real_size.y * v, 0, real_size.y) + corner_y - (cursor->get_height() / 2);
|
|
|
|
}
|
|
|
|
c->draw_texture(cursor, Point2(x, y));
|
|
|
|
|
2017-06-18 14:00:36 +00:00
|
|
|
col.set_hsv(h, 1, 1);
|
2021-02-23 10:22:46 +00:00
|
|
|
if (picker_type == SHAPE_HSV_WHEEL) {
|
|
|
|
points.resize(4);
|
|
|
|
double h1 = h - (0.5 / 360);
|
|
|
|
double h2 = h + (0.5 / 360);
|
|
|
|
points.set(0, Point2(center.x + (center.x * Math::cos(h1 * Math_TAU)), center.y + (center.y * Math::sin(h1 * Math_TAU))));
|
|
|
|
points.set(1, Point2(center.x + (center.x * Math::cos(h1 * Math_TAU) * 0.84), center.y + (center.y * Math::sin(h1 * Math_TAU) * 0.84)));
|
|
|
|
points.set(2, Point2(center.x + (center.x * Math::cos(h2 * Math_TAU)), center.y + (center.y * Math::sin(h2 * Math_TAU))));
|
|
|
|
points.set(3, Point2(center.x + (center.x * Math::cos(h2 * Math_TAU) * 0.84), center.y + (center.y * Math::sin(h2 * Math_TAU) * 0.84)));
|
|
|
|
c->draw_multiline(points, col.inverted());
|
|
|
|
}
|
|
|
|
|
2017-07-03 22:52:45 +00:00
|
|
|
} else if (p_which == 1) {
|
2021-02-23 10:22:46 +00:00
|
|
|
if (picker_type == SHAPE_HSV_RECTANGLE) {
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> hue = get_theme_icon(SNAME("color_hue"), SNAME("ColorPicker"));
|
2021-02-23 10:22:46 +00:00
|
|
|
c->draw_texture_rect(hue, Rect2(Point2(), c->get_size()));
|
|
|
|
int y = c->get_size().y - c->get_size().y * (1.0 - h);
|
|
|
|
Color col;
|
|
|
|
col.set_hsv(h, 1, 1);
|
|
|
|
c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
|
|
|
|
} else if (picker_type == SHAPE_VHS_CIRCLE) {
|
|
|
|
Vector<Point2> points;
|
|
|
|
Vector<Color> colors;
|
|
|
|
Color col;
|
|
|
|
col.set_hsv(h, s, 1);
|
|
|
|
points.resize(4);
|
|
|
|
colors.resize(4);
|
|
|
|
points.set(0, Vector2());
|
|
|
|
points.set(1, Vector2(c->get_size().x, 0));
|
|
|
|
points.set(2, c->get_size());
|
|
|
|
points.set(3, Vector2(0, c->get_size().y));
|
|
|
|
colors.set(0, col);
|
|
|
|
colors.set(1, col);
|
|
|
|
colors.set(2, Color(0, 0, 0));
|
|
|
|
colors.set(3, Color(0, 0, 0));
|
|
|
|
c->draw_polygon(points, colors);
|
|
|
|
int y = c->get_size().y - c->get_size().y * CLAMP(v, 0, 1);
|
|
|
|
col.set_hsv(h, 1, v);
|
|
|
|
c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
|
|
|
|
}
|
|
|
|
} else if (p_which == 2) {
|
|
|
|
c->draw_rect(Rect2(Point2(), c->get_size()), Color(1, 1, 1));
|
|
|
|
if (picker_type == SHAPE_VHS_CIRCLE) {
|
|
|
|
circle_mat->set_shader_param("v", v);
|
|
|
|
}
|
2015-12-25 20:59:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-17 05:30:56 +00:00
|
|
|
void ColorPicker::_slider_draw(int p_which) {
|
|
|
|
Vector<Vector2> pos;
|
|
|
|
pos.resize(4);
|
|
|
|
Vector<Color> col;
|
|
|
|
col.resize(4);
|
|
|
|
Size2 size = scroll[p_which]->get_size();
|
|
|
|
Color left_color;
|
|
|
|
Color right_color;
|
2021-10-02 20:07:42 +00:00
|
|
|
const real_t margin = 4 * get_theme_default_base_scale();
|
2021-02-17 05:30:56 +00:00
|
|
|
|
|
|
|
if (p_which == 3) {
|
2021-08-18 00:09:48 +00:00
|
|
|
scroll[p_which]->draw_texture_rect(get_theme_icon(SNAME("sample_bg"), SNAME("ColorPicker")), Rect2(Point2(0, margin), Size2(size.x, margin)), true);
|
2021-02-17 05:30:56 +00:00
|
|
|
|
|
|
|
left_color = color;
|
|
|
|
left_color.a = 0;
|
|
|
|
right_color = color;
|
|
|
|
right_color.a = 1;
|
|
|
|
} else {
|
|
|
|
if (raw_mode_enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (hsv_mode_enabled) {
|
|
|
|
if (p_which == 0) {
|
2021-07-17 21:22:52 +00:00
|
|
|
Ref<Texture2D> hue = get_theme_icon(SNAME("color_hue"), SNAME("ColorPicker"));
|
2021-02-17 05:30:56 +00:00
|
|
|
scroll[p_which]->draw_set_transform(Point2(), -Math_PI / 2, Size2(1.0, 1.0));
|
|
|
|
scroll[p_which]->draw_texture_rect(hue, Rect2(Vector2(margin * -2, 0), Vector2(scroll[p_which]->get_size().x, margin)), false, Color(1, 1, 1), true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Color s_col;
|
|
|
|
Color v_col;
|
|
|
|
s_col.set_hsv(h, 0, v);
|
|
|
|
left_color = (p_which == 1) ? s_col : Color(0, 0, 0);
|
|
|
|
s_col.set_hsv(h, 1, v);
|
|
|
|
v_col.set_hsv(h, s, 1);
|
|
|
|
right_color = (p_which == 1) ? s_col : v_col;
|
|
|
|
} else {
|
|
|
|
left_color = Color(
|
|
|
|
p_which == 0 ? 0 : color.r,
|
|
|
|
p_which == 1 ? 0 : color.g,
|
|
|
|
p_which == 2 ? 0 : color.b);
|
|
|
|
right_color = Color(
|
|
|
|
p_which == 0 ? 1 : color.r,
|
|
|
|
p_which == 1 ? 1 : color.g,
|
|
|
|
p_which == 2 ? 1 : color.b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
col.set(0, left_color);
|
|
|
|
col.set(1, right_color);
|
|
|
|
col.set(2, right_color);
|
|
|
|
col.set(3, left_color);
|
|
|
|
pos.set(0, Vector2(0, margin));
|
|
|
|
pos.set(1, Vector2(size.x, margin));
|
|
|
|
pos.set(2, Vector2(size.x, margin * 2));
|
|
|
|
pos.set(3, Vector2(0, margin * 2));
|
|
|
|
|
|
|
|
scroll[p_which]->draw_polygon(pos, col);
|
|
|
|
}
|
|
|
|
|
2021-02-23 10:22:46 +00:00
|
|
|
void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
|
2017-08-11 19:10:05 +00:00
|
|
|
Ref<InputEventMouseButton> bev = p_event;
|
2017-05-20 15:38:03 +00:00
|
|
|
|
|
|
|
if (bev.is_valid()) {
|
2021-08-13 21:31:57 +00:00
|
|
|
if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
|
2021-02-23 10:22:46 +00:00
|
|
|
Vector2 center = c->get_size() / 2.0;
|
|
|
|
if (picker_type == SHAPE_VHS_CIRCLE) {
|
|
|
|
real_t dist = center.distance_to(bev->get_position());
|
|
|
|
|
|
|
|
if (dist <= center.x) {
|
2021-11-17 17:21:53 +00:00
|
|
|
real_t rad = center.angle_to_point(bev->get_position());
|
2021-02-23 10:22:46 +00:00
|
|
|
h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
|
|
|
|
s = CLAMP(dist / center.x, 0, 1);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
|
|
|
|
real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
|
|
|
|
Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
|
|
|
|
|
|
|
|
if (bev->get_position().x < corner_x || bev->get_position().x > c->get_size().x - corner_x ||
|
|
|
|
bev->get_position().y < corner_y || bev->get_position().y > c->get_size().y - corner_y) {
|
|
|
|
{
|
|
|
|
real_t dist = center.distance_to(bev->get_position());
|
|
|
|
|
|
|
|
if (dist >= center.x * 0.84 && dist <= center.x) {
|
2021-11-17 17:21:53 +00:00
|
|
|
real_t rad = center.angle_to_point(bev->get_position());
|
2021-02-23 10:22:46 +00:00
|
|
|
h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
|
|
|
|
spinning = true;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!spinning) {
|
|
|
|
real_t x = CLAMP(bev->get_position().x, corner_x, c->get_size().x - corner_x);
|
|
|
|
real_t y = CLAMP(bev->get_position().y, corner_x, c->get_size().y - corner_y);
|
|
|
|
|
|
|
|
s = (x - c->get_position().x - corner_x) / real_size.x;
|
|
|
|
v = 1.0 - (y - c->get_position().y - corner_y) / real_size.y;
|
|
|
|
}
|
|
|
|
}
|
2015-11-21 05:51:42 +00:00
|
|
|
changing_color = true;
|
2017-03-05 15:44:50 +00:00
|
|
|
color.set_hsv(h, s, v, color.a);
|
2016-02-06 02:35:15 +00:00
|
|
|
last_hsv = color;
|
2017-01-14 14:07:57 +00:00
|
|
|
set_pick_color(color);
|
2015-12-24 02:17:09 +00:00
|
|
|
_update_color();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!deferred_mode_enabled) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-08-13 21:31:57 +00:00
|
|
|
} else if (deferred_mode_enabled && !bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2018-05-29 12:21:06 +00:00
|
|
|
changing_color = false;
|
2021-02-23 10:22:46 +00:00
|
|
|
spinning = false;
|
2015-11-21 05:51:42 +00:00
|
|
|
} else {
|
|
|
|
changing_color = false;
|
2021-02-23 10:22:46 +00:00
|
|
|
spinning = false;
|
2015-11-21 05:51:42 +00:00
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
Ref<InputEventMouseMotion> mev = p_event;
|
2017-05-20 15:38:03 +00:00
|
|
|
|
|
|
|
if (mev.is_valid()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!changing_color) {
|
2015-11-21 05:51:42 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-02-23 10:22:46 +00:00
|
|
|
|
|
|
|
Vector2 center = c->get_size() / 2.0;
|
|
|
|
if (picker_type == SHAPE_VHS_CIRCLE) {
|
|
|
|
real_t dist = center.distance_to(mev->get_position());
|
2021-11-17 17:21:53 +00:00
|
|
|
real_t rad = center.angle_to_point(mev->get_position());
|
2021-02-23 10:22:46 +00:00
|
|
|
h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
|
|
|
|
s = CLAMP(dist / center.x, 0, 1);
|
|
|
|
} else {
|
|
|
|
if (spinning) {
|
2021-11-17 17:21:53 +00:00
|
|
|
real_t rad = center.angle_to_point(mev->get_position());
|
2021-02-23 10:22:46 +00:00
|
|
|
h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
|
|
|
|
} else {
|
|
|
|
real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
|
|
|
|
real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
|
|
|
|
Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
|
|
|
|
|
|
|
|
real_t x = CLAMP(mev->get_position().x, corner_x, c->get_size().x - corner_x);
|
|
|
|
real_t y = CLAMP(mev->get_position().y, corner_x, c->get_size().y - corner_y);
|
|
|
|
|
|
|
|
s = (x - corner_x) / real_size.x;
|
|
|
|
v = 1.0 - (y - corner_y) / real_size.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
color.set_hsv(h, s, v, color.a);
|
2016-02-06 02:35:15 +00:00
|
|
|
last_hsv = color;
|
2017-01-14 14:07:57 +00:00
|
|
|
set_pick_color(color);
|
2015-12-24 02:17:09 +00:00
|
|
|
_update_color();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!deferred_mode_enabled) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-11-21 05:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
|
|
|
|
Ref<InputEventMouseButton> bev = p_event;
|
2017-05-20 15:38:03 +00:00
|
|
|
|
|
|
|
if (bev.is_valid()) {
|
2021-08-13 21:31:57 +00:00
|
|
|
if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
|
2015-11-21 05:51:42 +00:00
|
|
|
changing_color = true;
|
2017-06-18 14:00:36 +00:00
|
|
|
float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
|
2021-02-23 10:22:46 +00:00
|
|
|
if (picker_type == SHAPE_VHS_CIRCLE) {
|
|
|
|
v = 1.0 - (y / w_edit->get_size().height);
|
|
|
|
} else {
|
|
|
|
h = y / w_edit->get_size().height;
|
|
|
|
}
|
2015-11-21 05:51:42 +00:00
|
|
|
} else {
|
|
|
|
changing_color = false;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
color.set_hsv(h, s, v, color.a);
|
2016-02-06 02:35:15 +00:00
|
|
|
last_hsv = color;
|
2017-01-14 14:07:57 +00:00
|
|
|
set_pick_color(color);
|
2015-12-24 02:17:09 +00:00
|
|
|
_update_color();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!deferred_mode_enabled) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2021-08-13 21:31:57 +00:00
|
|
|
} else if (!bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
Ref<InputEventMouseMotion> mev = p_event;
|
2017-05-20 15:38:03 +00:00
|
|
|
|
|
|
|
if (mev.is_valid()) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!changing_color) {
|
2015-11-21 05:51:42 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-06-18 14:00:36 +00:00
|
|
|
float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
|
2021-02-23 10:22:46 +00:00
|
|
|
if (picker_type == SHAPE_VHS_CIRCLE) {
|
|
|
|
v = 1.0 - (y / w_edit->get_size().height);
|
|
|
|
} else {
|
|
|
|
h = y / w_edit->get_size().height;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
color.set_hsv(h, s, v, color.a);
|
2016-02-06 02:35:15 +00:00
|
|
|
last_hsv = color;
|
2017-01-14 14:07:57 +00:00
|
|
|
set_pick_color(color);
|
2015-12-24 02:17:09 +00:00
|
|
|
_update_color();
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!deferred_mode_enabled) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-11-21 05:51:42 +00:00
|
|
|
}
|
2015-12-24 02:17:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-18 00:09:48 +00:00
|
|
|
void ColorPicker::_preset_input(const Ref<InputEvent> &p_event, const Color &p_color) {
|
2017-08-11 19:10:05 +00:00
|
|
|
Ref<InputEventMouseButton> bev = p_event;
|
2017-05-20 15:38:03 +00:00
|
|
|
|
|
|
|
if (bev.is_valid()) {
|
2021-08-13 21:31:57 +00:00
|
|
|
if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
|
2021-08-18 00:09:48 +00:00
|
|
|
set_pick_color(p_color);
|
2018-12-12 20:55:29 +00:00
|
|
|
_update_color();
|
2021-08-18 00:09:48 +00:00
|
|
|
emit_signal(SNAME("color_changed"), p_color);
|
2021-08-13 21:31:57 +00:00
|
|
|
} else if (bev->is_pressed() && bev->get_button_index() == MouseButton::RIGHT && presets_enabled) {
|
2021-08-18 00:09:48 +00:00
|
|
|
erase_preset(p_color);
|
|
|
|
emit_signal(SNAME("preset_removed"), p_color);
|
2015-12-24 02:17:09 +00:00
|
|
|
}
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|
2015-12-24 02:17:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
void ColorPicker::_screen_input(const Ref<InputEvent> &p_event) {
|
2021-02-18 12:53:32 +00:00
|
|
|
if (!is_inside_tree()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
Ref<InputEventMouseButton> bev = p_event;
|
2021-08-13 21:31:57 +00:00
|
|
|
if (bev.is_valid() && bev->get_button_index() == MouseButton::LEFT && !bev->is_pressed()) {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2019-01-28 03:16:40 +00:00
|
|
|
screen->hide();
|
2017-05-20 15:38:03 +00:00
|
|
|
}
|
|
|
|
|
2017-08-11 19:10:05 +00:00
|
|
|
Ref<InputEventMouseMotion> mev = p_event;
|
2017-05-20 15:38:03 +00:00
|
|
|
if (mev.is_valid()) {
|
2017-03-05 15:44:50 +00:00
|
|
|
Viewport *r = get_tree()->get_root();
|
2021-09-23 14:58:43 +00:00
|
|
|
if (!r->get_visible_rect().has_point(mev->get_global_position())) {
|
2015-12-25 14:08:02 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-01-28 03:16:40 +00:00
|
|
|
|
2021-03-28 11:32:17 +00:00
|
|
|
Ref<Image> img = r->get_texture()->get_image();
|
2020-12-15 12:04:21 +00:00
|
|
|
if (img.is_valid() && !img->is_empty()) {
|
2017-06-18 20:16:54 +00:00
|
|
|
Vector2 ofs = mev->get_global_position() - r->get_visible_rect().get_position();
|
|
|
|
Color c = img->get_pixel(ofs.x, r->get_visible_rect().size.height - ofs.y);
|
2020-02-17 21:06:54 +00:00
|
|
|
|
2017-01-14 14:07:57 +00:00
|
|
|
set_pick_color(c);
|
2016-10-03 19:33:42 +00:00
|
|
|
}
|
2015-12-25 14:08:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 02:17:09 +00:00
|
|
|
void ColorPicker::_add_preset_pressed() {
|
|
|
|
add_preset(color);
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("preset_added"), color);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ColorPicker::_screen_pick_pressed() {
|
2021-02-18 12:53:32 +00:00
|
|
|
if (!is_inside_tree()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Viewport *r = get_tree()->get_root();
|
2015-12-25 14:08:02 +00:00
|
|
|
if (!screen) {
|
2017-03-05 15:44:50 +00:00
|
|
|
screen = memnew(Control);
|
2015-12-25 14:08:02 +00:00
|
|
|
r->add_child(screen);
|
2020-10-01 07:17:33 +00:00
|
|
|
screen->set_as_top_level(true);
|
2020-12-22 16:24:29 +00:00
|
|
|
screen->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
|
2017-06-18 20:16:54 +00:00
|
|
|
screen->set_default_cursor_shape(CURSOR_POINTING_HAND);
|
2020-02-21 17:28:45 +00:00
|
|
|
screen->connect("gui_input", callable_mp(this, &ColorPicker::_screen_input));
|
2019-01-28 03:16:40 +00:00
|
|
|
// It immediately toggles off in the first press otherwise.
|
2021-07-17 21:22:52 +00:00
|
|
|
screen->call_deferred(SNAME("connect"), "hidden", Callable(btn_pick, "set_pressed"), varray(false));
|
2015-12-25 14:08:02 +00:00
|
|
|
}
|
|
|
|
screen->raise();
|
2020-03-14 16:06:39 +00:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
#warning show modal no longer works, needs to be converted to a popup
|
|
|
|
#endif
|
|
|
|
//screen->show_modal();
|
2015-12-25 14:08:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 20:02:58 +00:00
|
|
|
void ColorPicker::_focus_enter() {
|
2020-03-24 11:41:09 +00:00
|
|
|
bool has_ctext_focus = c_text->has_focus();
|
|
|
|
if (has_ctext_focus) {
|
2018-03-09 20:02:58 +00:00
|
|
|
c_text->select_all();
|
2020-03-24 11:41:09 +00:00
|
|
|
} else {
|
|
|
|
c_text->select(0, 0);
|
2018-03-09 20:02:58 +00:00
|
|
|
}
|
2020-03-24 11:41:09 +00:00
|
|
|
|
2018-03-09 20:02:58 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2020-03-24 11:41:09 +00:00
|
|
|
if (values[i]->get_line_edit()->has_focus() && !has_ctext_focus) {
|
2018-03-09 20:02:58 +00:00
|
|
|
values[i]->get_line_edit()->select_all();
|
2020-03-24 11:41:09 +00:00
|
|
|
} else {
|
|
|
|
values[i]->get_line_edit()->select(0, 0);
|
2018-03-09 20:02:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::_focus_exit() {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!values[i]->get_line_edit()->get_menu()->is_visible()) {
|
2020-03-24 11:41:09 +00:00
|
|
|
values[i]->get_line_edit()->select(0, 0);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-03-09 20:02:58 +00:00
|
|
|
}
|
|
|
|
c_text->select(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::_html_focus_exit() {
|
2021-07-16 21:36:05 +00:00
|
|
|
if (c_text->is_menu_visible()) {
|
2020-03-24 11:41:09 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2021-06-16 16:43:34 +00:00
|
|
|
_html_submitted(c_text->get_text());
|
2018-03-09 20:02:58 +00:00
|
|
|
_focus_exit();
|
|
|
|
}
|
|
|
|
|
2019-02-24 20:42:46 +00:00
|
|
|
void ColorPicker::set_presets_enabled(bool p_enabled) {
|
|
|
|
presets_enabled = p_enabled;
|
|
|
|
if (!p_enabled) {
|
2021-08-18 00:09:48 +00:00
|
|
|
btn_add_preset->set_disabled(true);
|
|
|
|
btn_add_preset->set_focus_mode(FOCUS_NONE);
|
2019-02-24 20:42:46 +00:00
|
|
|
} else {
|
2021-08-18 00:09:48 +00:00
|
|
|
btn_add_preset->set_disabled(false);
|
|
|
|
btn_add_preset->set_focus_mode(FOCUS_ALL);
|
2019-02-24 20:42:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColorPicker::are_presets_enabled() const {
|
|
|
|
return presets_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::set_presets_visible(bool p_visible) {
|
|
|
|
presets_visible = p_visible;
|
|
|
|
preset_separator->set_visible(p_visible);
|
|
|
|
preset_container->set_visible(p_visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColorPicker::are_presets_visible() const {
|
|
|
|
return presets_visible;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ColorPicker::_bind_methods() {
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPicker::set_pick_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPicker::get_pick_color);
|
2021-10-08 21:26:13 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_hsv_mode", "enabled"), &ColorPicker::set_hsv_mode);
|
2019-05-03 17:38:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("is_hsv_mode"), &ColorPicker::is_hsv_mode);
|
2021-10-08 21:26:13 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_raw_mode", "enabled"), &ColorPicker::set_raw_mode);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("is_raw_mode"), &ColorPicker::is_raw_mode);
|
2018-05-29 12:21:06 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_deferred_mode", "mode"), &ColorPicker::set_deferred_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_deferred_mode"), &ColorPicker::is_deferred_mode);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPicker::set_edit_alpha);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPicker::is_editing_alpha);
|
2019-02-24 20:42:46 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_presets_enabled", "enabled"), &ColorPicker::set_presets_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("are_presets_enabled"), &ColorPicker::are_presets_enabled);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_presets_visible", "visible"), &ColorPicker::set_presets_visible);
|
|
|
|
ClassDB::bind_method(D_METHOD("are_presets_visible"), &ColorPicker::are_presets_visible);
|
2017-07-18 19:03:34 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("add_preset", "color"), &ColorPicker::add_preset);
|
2018-10-03 14:12:38 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("erase_preset", "color"), &ColorPicker::erase_preset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_presets"), &ColorPicker::get_presets);
|
2021-02-23 10:22:46 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_picker_shape", "picker"), &ColorPicker::set_picker_shape);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_picker_shape"), &ColorPicker::get_picker_shape);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-01-11 22:35:12 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
|
2019-05-03 17:38:45 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hsv_mode"), "set_hsv_mode", "is_hsv_mode");
|
2018-01-11 22:35:12 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "raw_mode"), "set_raw_mode", "is_raw_mode");
|
2018-05-29 12:21:06 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deferred_mode"), "set_deferred_mode", "is_deferred_mode");
|
2021-02-23 10:22:46 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle"), "set_picker_shape", "get_picker_shape");
|
2019-02-24 20:42:46 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_enabled"), "set_presets_enabled", "are_presets_enabled");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_visible"), "set_presets_visible", "are_presets_visible");
|
2018-01-11 22:35:12 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
|
2018-10-03 14:12:38 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("preset_added", PropertyInfo(Variant::COLOR, "color")));
|
|
|
|
ADD_SIGNAL(MethodInfo("preset_removed", PropertyInfo(Variant::COLOR, "color")));
|
2021-02-23 10:22:46 +00:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(SHAPE_HSV_RECTANGLE);
|
|
|
|
BIND_ENUM_CONSTANT(SHAPE_HSV_WHEEL);
|
|
|
|
BIND_ENUM_CONSTANT(SHAPE_VHS_CIRCLE);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 20:36:34 +00:00
|
|
|
ColorPicker::ColorPicker() :
|
|
|
|
BoxContainer(true) {
|
2017-03-05 15:44:50 +00:00
|
|
|
HBoxContainer *hb_edit = memnew(HBoxContainer);
|
2021-08-25 13:49:30 +00:00
|
|
|
add_child(hb_edit, false, INTERNAL_MODE_FRONT);
|
2017-06-18 14:00:36 +00:00
|
|
|
hb_edit->set_v_size_flags(SIZE_EXPAND_FILL);
|
2016-10-03 19:33:42 +00:00
|
|
|
|
2019-01-26 21:07:32 +00:00
|
|
|
hb_edit->add_child(uv_edit);
|
2021-02-23 10:22:46 +00:00
|
|
|
uv_edit->connect("gui_input", callable_mp(this, &ColorPicker::_uv_input), make_binds(uv_edit));
|
2017-01-08 22:54:19 +00:00
|
|
|
uv_edit->set_mouse_filter(MOUSE_FILTER_PASS);
|
2017-06-18 14:00:36 +00:00
|
|
|
uv_edit->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
uv_edit->set_v_size_flags(SIZE_EXPAND_FILL);
|
2020-02-21 17:28:45 +00:00
|
|
|
uv_edit->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw), make_binds(0, uv_edit));
|
2015-12-24 02:17:09 +00:00
|
|
|
|
2019-12-28 17:08:16 +00:00
|
|
|
HBoxContainer *hb_smpl = memnew(HBoxContainer);
|
2021-08-25 13:49:30 +00:00
|
|
|
add_child(hb_smpl, false, INTERNAL_MODE_FRONT);
|
2019-12-28 17:08:16 +00:00
|
|
|
|
|
|
|
hb_smpl->add_child(sample);
|
|
|
|
sample->set_h_size_flags(SIZE_EXPAND_FILL);
|
2021-05-10 14:59:46 +00:00
|
|
|
sample->connect("gui_input", callable_mp(this, &ColorPicker::_sample_input));
|
2020-02-21 17:28:45 +00:00
|
|
|
sample->connect("draw", callable_mp(this, &ColorPicker::_sample_draw));
|
2019-12-28 17:08:16 +00:00
|
|
|
|
2020-06-19 18:49:04 +00:00
|
|
|
btn_pick->set_flat(true);
|
2019-12-28 17:08:16 +00:00
|
|
|
hb_smpl->add_child(btn_pick);
|
|
|
|
btn_pick->set_toggle_mode(true);
|
2021-04-01 06:25:26 +00:00
|
|
|
btn_pick->set_tooltip(RTR("Pick a color from the editor window."));
|
2020-02-21 17:28:45 +00:00
|
|
|
btn_pick->connect("pressed", callable_mp(this, &ColorPicker::_screen_pick_pressed));
|
2019-12-28 17:08:16 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VBoxContainer *vbl = memnew(VBoxContainer);
|
2021-08-25 13:49:30 +00:00
|
|
|
add_child(vbl, false, INTERNAL_MODE_FRONT);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2021-08-25 13:49:30 +00:00
|
|
|
add_child(memnew(HSeparator), false, INTERNAL_MODE_FRONT);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VBoxContainer *vbr = memnew(VBoxContainer);
|
2021-08-25 13:49:30 +00:00
|
|
|
add_child(vbr, false, INTERNAL_MODE_FRONT);
|
2014-02-10 01:10:30 +00:00
|
|
|
vbr->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
HBoxContainer *hbc = memnew(HBoxContainer);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-05-03 17:38:45 +00:00
|
|
|
labels[i] = memnew(Label());
|
2021-07-17 21:22:52 +00:00
|
|
|
labels[i]->set_custom_minimum_size(Size2(get_theme_constant(SNAME("label_width")), 0));
|
2017-07-03 22:52:45 +00:00
|
|
|
labels[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
|
2014-02-10 01:10:30 +00:00
|
|
|
hbc->add_child(labels[i]);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
scroll[i] = memnew(HSlider);
|
2017-07-03 22:52:45 +00:00
|
|
|
scroll[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
|
2018-03-09 20:02:58 +00:00
|
|
|
scroll[i]->set_focus_mode(FOCUS_NONE);
|
2014-02-10 01:10:30 +00:00
|
|
|
hbc->add_child(scroll[i]);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
values[i] = memnew(SpinBox);
|
2014-02-10 01:10:30 +00:00
|
|
|
scroll[i]->share(values[i]);
|
|
|
|
hbc->add_child(values[i]);
|
2020-02-21 17:28:45 +00:00
|
|
|
values[i]->get_line_edit()->connect("focus_entered", callable_mp(this, &ColorPicker::_focus_enter));
|
|
|
|
values[i]->get_line_edit()->connect("focus_exited", callable_mp(this, &ColorPicker::_focus_exit));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
scroll[i]->set_min(0);
|
|
|
|
scroll[i]->set_page(0);
|
|
|
|
scroll[i]->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
2020-02-21 17:28:45 +00:00
|
|
|
scroll[i]->connect("value_changed", callable_mp(this, &ColorPicker::_value_changed));
|
2021-02-17 05:30:56 +00:00
|
|
|
scroll[i]->connect("draw", callable_mp(this, &ColorPicker::_slider_draw), make_binds(i));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
vbr->add_child(hbc);
|
|
|
|
}
|
2021-04-07 11:15:29 +00:00
|
|
|
|
2019-05-03 17:38:45 +00:00
|
|
|
labels[3]->set_text("A");
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
HBoxContainer *hhb = memnew(HBoxContainer);
|
2019-01-26 21:07:32 +00:00
|
|
|
vbr->add_child(hhb);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-05-03 17:38:45 +00:00
|
|
|
hhb->add_child(btn_hsv);
|
2021-04-01 06:25:26 +00:00
|
|
|
btn_hsv->set_text(RTR("HSV"));
|
2020-02-21 17:28:45 +00:00
|
|
|
btn_hsv->connect("toggled", callable_mp(this, &ColorPicker::set_hsv_mode));
|
2019-05-03 17:38:45 +00:00
|
|
|
|
|
|
|
hhb->add_child(btn_raw);
|
2021-04-01 06:25:26 +00:00
|
|
|
btn_raw->set_text(RTR("Raw"));
|
2020-02-21 17:28:45 +00:00
|
|
|
btn_raw->connect("toggled", callable_mp(this, &ColorPicker::set_raw_mode));
|
2019-01-26 21:07:32 +00:00
|
|
|
|
2016-01-26 22:10:56 +00:00
|
|
|
hhb->add_child(text_type);
|
2019-01-26 21:07:32 +00:00
|
|
|
text_type->set_text("#");
|
|
|
|
text_type->set_tooltip(TTR("Switch between hexadecimal and code values."));
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2020-02-21 17:28:45 +00:00
|
|
|
text_type->connect("pressed", callable_mp(this, &ColorPicker::_text_type_toggled));
|
2019-01-26 21:07:32 +00:00
|
|
|
} else {
|
|
|
|
text_type->set_flat(true);
|
|
|
|
text_type->set_mouse_filter(MOUSE_FILTER_IGNORE);
|
|
|
|
}
|
2016-01-26 22:10:56 +00:00
|
|
|
|
|
|
|
hhb->add_child(c_text);
|
2019-01-26 21:07:32 +00:00
|
|
|
c_text->set_h_size_flags(SIZE_EXPAND_FILL);
|
2021-06-16 16:43:34 +00:00
|
|
|
c_text->connect("text_submitted", callable_mp(this, &ColorPicker::_html_submitted));
|
2020-02-21 17:28:45 +00:00
|
|
|
c_text->connect("focus_entered", callable_mp(this, &ColorPicker::_focus_enter));
|
|
|
|
c_text->connect("focus_exited", callable_mp(this, &ColorPicker::_html_focus_exit));
|
2018-03-09 20:02:58 +00:00
|
|
|
|
2021-02-23 10:22:46 +00:00
|
|
|
wheel_edit->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
wheel_edit->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
hb_edit->add_child(wheel_edit);
|
|
|
|
|
2021-06-17 22:03:09 +00:00
|
|
|
wheel_mat.instantiate();
|
2021-02-23 10:22:46 +00:00
|
|
|
wheel_mat->set_shader(wheel_shader);
|
2021-06-17 22:03:09 +00:00
|
|
|
circle_mat.instantiate();
|
2021-02-23 10:22:46 +00:00
|
|
|
circle_mat->set_shader(circle_shader);
|
|
|
|
|
2022-02-08 09:14:58 +00:00
|
|
|
wheel_margin->add_theme_constant_override("margin_bottom", 8);
|
2021-02-23 10:22:46 +00:00
|
|
|
wheel_edit->add_child(wheel_margin);
|
|
|
|
|
|
|
|
wheel_margin->add_child(wheel);
|
|
|
|
wheel->set_mouse_filter(MOUSE_FILTER_PASS);
|
|
|
|
wheel->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw), make_binds(2, wheel));
|
|
|
|
|
|
|
|
wheel_margin->add_child(wheel_uv);
|
|
|
|
wheel_uv->connect("gui_input", callable_mp(this, &ColorPicker::_uv_input), make_binds(wheel_uv));
|
|
|
|
wheel_uv->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw), make_binds(0, wheel_uv));
|
|
|
|
|
|
|
|
hb_edit->add_child(w_edit);
|
|
|
|
w_edit->set_h_size_flags(SIZE_FILL);
|
|
|
|
w_edit->set_v_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
w_edit->connect("gui_input", callable_mp(this, &ColorPicker::_w_input));
|
|
|
|
w_edit->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw), make_binds(1, w_edit));
|
|
|
|
|
|
|
|
picker_type = SHAPE_HSV_RECTANGLE;
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_controls();
|
2017-03-05 15:44:50 +00:00
|
|
|
updating = false;
|
2015-12-24 02:17:09 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
set_pick_color(Color(1, 1, 1));
|
2015-12-24 02:17:09 +00:00
|
|
|
|
2021-08-25 13:49:30 +00:00
|
|
|
add_child(preset_separator, false, INTERNAL_MODE_FRONT);
|
2019-01-26 21:07:32 +00:00
|
|
|
|
2019-07-30 11:03:00 +00:00
|
|
|
preset_container->set_h_size_flags(SIZE_EXPAND_FILL);
|
2021-08-18 00:09:48 +00:00
|
|
|
preset_container->set_columns(preset_column_count);
|
2021-08-25 13:49:30 +00:00
|
|
|
add_child(preset_container, false, INTERNAL_MODE_FRONT);
|
2015-12-24 02:17:09 +00:00
|
|
|
|
2021-11-25 02:58:47 +00:00
|
|
|
btn_add_preset->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
|
2021-08-18 00:09:48 +00:00
|
|
|
btn_add_preset->set_tooltip(RTR("Add current color as a preset."));
|
|
|
|
btn_add_preset->connect("pressed", callable_mp(this, &ColorPicker::_add_preset_pressed));
|
|
|
|
preset_container->add_child(btn_add_preset);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////
|
|
|
|
|
2020-01-05 17:01:24 +00:00
|
|
|
void ColorPickerButton::_about_to_popup() {
|
|
|
|
set_pressed(true);
|
2021-05-10 14:59:46 +00:00
|
|
|
if (picker) {
|
|
|
|
picker->set_old_color(color);
|
|
|
|
}
|
2020-01-05 17:01:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ColorPickerButton::_color_changed(const Color &p_color) {
|
2018-05-15 20:12:35 +00:00
|
|
|
color = p_color;
|
2014-02-10 01:10:30 +00:00
|
|
|
update();
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("color_changed"), color);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-12 21:47:53 +00:00
|
|
|
void ColorPickerButton::_modal_closed() {
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("popup_closed"));
|
2020-09-09 18:38:29 +00:00
|
|
|
set_pressed(false);
|
2018-04-12 21:47:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void ColorPickerButton::pressed() {
|
2018-05-15 20:12:35 +00:00
|
|
|
_update_picker();
|
2020-03-20 02:32:09 +00:00
|
|
|
|
2021-08-30 15:42:33 +00:00
|
|
|
Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
|
|
|
|
|
2022-03-05 23:57:42 +00:00
|
|
|
popup->reset_size();
|
2021-08-18 00:09:48 +00:00
|
|
|
picker->_update_presets();
|
2020-03-20 02:32:09 +00:00
|
|
|
|
|
|
|
Rect2i usable_rect = popup->get_usable_parent_rect();
|
|
|
|
//let's try different positions to see which one we can use
|
|
|
|
|
|
|
|
Rect2i cp_rect(Point2i(), popup->get_size());
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (i > 1) {
|
|
|
|
cp_rect.position.y = get_screen_position().y - cp_rect.size.y;
|
|
|
|
} else {
|
2021-08-30 15:42:33 +00:00
|
|
|
cp_rect.position.y = get_screen_position().y + size.height;
|
2020-03-20 02:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i & 1) {
|
|
|
|
cp_rect.position.x = get_screen_position().x;
|
|
|
|
} else {
|
2021-08-30 15:42:33 +00:00
|
|
|
cp_rect.position.x = get_screen_position().x - MAX(0, (cp_rect.size.x - size.x));
|
2020-03-20 02:32:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (usable_rect.encloses(cp_rect)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
popup->set_position(cp_rect.position);
|
2014-02-10 01:10:30 +00:00
|
|
|
popup->popup();
|
2016-07-01 13:55:35 +00:00
|
|
|
picker->set_focus_on_line_edit();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPickerButton::_notification(int p_what) {
|
2019-04-23 01:28:38 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_DRAW: {
|
2021-07-17 21:22:52 +00:00
|
|
|
const Ref<StyleBox> normal = get_theme_stylebox(SNAME("normal"));
|
2019-09-26 23:07:41 +00:00
|
|
|
const Rect2 r = Rect2(normal->get_offset(), get_size() - normal->get_minimum_size());
|
2021-07-17 21:22:52 +00:00
|
|
|
draw_texture_rect(Control::get_theme_icon(SNAME("bg"), SNAME("ColorPickerButton")), r, true);
|
2019-04-23 01:28:38 +00:00
|
|
|
draw_rect(r, color);
|
2019-09-26 23:07:41 +00:00
|
|
|
|
|
|
|
if (color.r > 1 || color.g > 1 || color.b > 1) {
|
|
|
|
// Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
|
2021-07-17 21:22:52 +00:00
|
|
|
draw_texture(Control::get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), normal->get_offset());
|
2019-09-26 23:07:41 +00:00
|
|
|
}
|
2019-04-23 01:28:38 +00:00
|
|
|
} break;
|
2022-02-15 17:06:48 +00:00
|
|
|
|
2020-03-04 16:36:09 +00:00
|
|
|
case NOTIFICATION_WM_CLOSE_REQUEST: {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (popup) {
|
2019-04-23 01:28:38 +00:00
|
|
|
popup->hide();
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2019-04-23 01:28:38 +00:00
|
|
|
} break;
|
2019-04-18 22:50:35 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (popup && !is_visible_in_tree()) {
|
|
|
|
popup->hide();
|
|
|
|
}
|
|
|
|
} break;
|
2019-04-18 22:50:35 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ColorPickerButton::set_pick_color(const Color &p_color) {
|
2018-05-15 20:12:35 +00:00
|
|
|
color = p_color;
|
|
|
|
if (picker) {
|
|
|
|
picker->set_pick_color(p_color);
|
|
|
|
}
|
|
|
|
|
2015-03-22 15:52:47 +00:00
|
|
|
update();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-05-14 12:29:06 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color ColorPickerButton::get_pick_color() const {
|
2018-05-15 20:12:35 +00:00
|
|
|
return color;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPickerButton::set_edit_alpha(bool p_show) {
|
2018-05-15 20:12:35 +00:00
|
|
|
edit_alpha = p_show;
|
|
|
|
if (picker) {
|
|
|
|
picker->set_edit_alpha(p_show);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool ColorPickerButton::is_editing_alpha() const {
|
2018-05-15 20:12:35 +00:00
|
|
|
return edit_alpha;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
ColorPicker *ColorPickerButton::get_picker() {
|
|
|
|
_update_picker();
|
2016-06-19 05:08:22 +00:00
|
|
|
return picker;
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:06:36 +00:00
|
|
|
PopupPanel *ColorPickerButton::get_popup() {
|
|
|
|
_update_picker();
|
2017-12-01 20:58:53 +00:00
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
2018-05-15 20:12:35 +00:00
|
|
|
void ColorPickerButton::_update_picker() {
|
|
|
|
if (!picker) {
|
|
|
|
popup = memnew(PopupPanel);
|
2020-03-20 02:32:09 +00:00
|
|
|
popup->set_wrap_controls(true);
|
2018-05-15 20:12:35 +00:00
|
|
|
picker = memnew(ColorPicker);
|
2020-12-22 16:24:29 +00:00
|
|
|
picker->set_anchors_and_offsets_preset(PRESET_WIDE);
|
2018-05-15 20:12:35 +00:00
|
|
|
popup->add_child(picker);
|
2021-08-25 13:49:30 +00:00
|
|
|
add_child(popup, false, INTERNAL_MODE_FRONT);
|
2020-02-21 17:28:45 +00:00
|
|
|
picker->connect("color_changed", callable_mp(this, &ColorPickerButton::_color_changed));
|
2020-01-05 17:01:24 +00:00
|
|
|
popup->connect("about_to_popup", callable_mp(this, &ColorPickerButton::_about_to_popup));
|
2020-09-09 18:38:29 +00:00
|
|
|
popup->connect("popup_hide", callable_mp(this, &ColorPickerButton::_modal_closed));
|
2018-05-15 20:12:35 +00:00
|
|
|
picker->set_pick_color(color);
|
|
|
|
picker->set_edit_alpha(edit_alpha);
|
2020-01-05 17:01:24 +00:00
|
|
|
picker->set_display_old_color(true);
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("picker_created"));
|
2018-05-15 20:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void ColorPickerButton::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPickerButton::set_pick_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPickerButton::get_pick_color);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_picker"), &ColorPickerButton::get_picker);
|
2017-12-01 20:58:53 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_popup"), &ColorPickerButton::get_popup);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPickerButton::set_edit_alpha);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPickerButton::is_editing_alpha);
|
2020-01-05 17:01:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_about_to_popup"), &ColorPickerButton::_about_to_popup);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
|
2018-04-12 21:47:53 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("popup_closed"));
|
2019-08-30 20:25:15 +00:00
|
|
|
ADD_SIGNAL(MethodInfo("picker_created"));
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 08:18:44 +00:00
|
|
|
ColorPickerButton::ColorPickerButton(const String &p_text) :
|
|
|
|
Button(p_text) {
|
2018-09-21 05:18:40 +00:00
|
|
|
set_toggle_mode(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2021-08-18 00:09:48 +00:00
|
|
|
|
|
|
|
/////////////////
|
|
|
|
|
|
|
|
void ColorPresetButton::_notification(int p_what) {
|
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
const Rect2 r = Rect2(Point2(0, 0), get_size());
|
|
|
|
Ref<StyleBox> sb_raw = get_theme_stylebox(SNAME("preset_fg"), SNAME("ColorPresetButton"))->duplicate();
|
|
|
|
Ref<StyleBoxFlat> sb_flat = sb_raw;
|
|
|
|
Ref<StyleBoxTexture> sb_texture = sb_raw;
|
|
|
|
|
|
|
|
if (sb_flat.is_valid()) {
|
|
|
|
if (preset_color.a < 1) {
|
|
|
|
// Draw a background pattern when the color is transparent.
|
|
|
|
sb_flat->set_bg_color(Color(1, 1, 1));
|
|
|
|
sb_flat->draw(get_canvas_item(), r);
|
|
|
|
|
|
|
|
Rect2 bg_texture_rect = r.grow_side(SIDE_LEFT, -sb_flat->get_margin(SIDE_LEFT));
|
|
|
|
bg_texture_rect = bg_texture_rect.grow_side(SIDE_RIGHT, -sb_flat->get_margin(SIDE_RIGHT));
|
|
|
|
bg_texture_rect = bg_texture_rect.grow_side(SIDE_TOP, -sb_flat->get_margin(SIDE_TOP));
|
|
|
|
bg_texture_rect = bg_texture_rect.grow_side(SIDE_BOTTOM, -sb_flat->get_margin(SIDE_BOTTOM));
|
|
|
|
|
|
|
|
draw_texture_rect(get_theme_icon(SNAME("preset_bg"), SNAME("ColorPresetButton")), bg_texture_rect, true);
|
|
|
|
sb_flat->set_bg_color(preset_color);
|
|
|
|
}
|
|
|
|
sb_flat->set_bg_color(preset_color);
|
|
|
|
sb_flat->draw(get_canvas_item(), r);
|
|
|
|
} else if (sb_texture.is_valid()) {
|
|
|
|
if (preset_color.a < 1) {
|
|
|
|
// Draw a background pattern when the color is transparent.
|
|
|
|
bool use_tile_texture = (sb_texture->get_h_axis_stretch_mode() == StyleBoxTexture::AxisStretchMode::AXIS_STRETCH_MODE_TILE) || (sb_texture->get_h_axis_stretch_mode() == StyleBoxTexture::AxisStretchMode::AXIS_STRETCH_MODE_TILE_FIT);
|
|
|
|
draw_texture_rect(get_theme_icon(SNAME("preset_bg"), SNAME("ColorPresetButton")), r, use_tile_texture);
|
|
|
|
}
|
|
|
|
sb_texture->set_modulate(preset_color);
|
|
|
|
sb_texture->draw(get_canvas_item(), r);
|
|
|
|
} else {
|
|
|
|
WARN_PRINT("Unsupported StyleBox used for ColorPresetButton. Use StyleBoxFlat or StyleBoxTexture instead.");
|
|
|
|
}
|
|
|
|
if (preset_color.r > 1 || preset_color.g > 1 || preset_color.b > 1) {
|
|
|
|
// Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
|
|
|
|
draw_texture(Control::get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPresetButton")), Vector2(0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPresetButton::set_preset_color(const Color &p_color) {
|
|
|
|
preset_color = p_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
Color ColorPresetButton::get_preset_color() const {
|
|
|
|
return preset_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColorPresetButton::ColorPresetButton(Color p_color) {
|
|
|
|
preset_color = p_color;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColorPresetButton::~ColorPresetButton() {
|
|
|
|
}
|