2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* color_picker.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
2020-01-01 11:16:22 +01:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 02:10:30 +01: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-05 00:50:27 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
#include "color_picker.h"
|
|
|
|
|
2020-03-04 17:36:09 +01:00
|
|
|
#include "core/input/input_filter.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/os/keyboard.h"
|
|
|
|
#include "core/os/os.h"
|
2018-09-24 16:28:18 +02:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
2019-10-09 16:11:04 +02:00
|
|
|
#include "editor/editor_scale.h"
|
|
|
|
#include "editor/editor_settings.h"
|
2018-09-24 16:28:18 +02:00
|
|
|
#endif
|
2020-03-04 02:51:12 +01:00
|
|
|
#include "scene/main/window.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
void ColorPicker::_notification(int p_what) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
switch (p_what) {
|
2014-02-10 02:10:30 +01:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
2017-12-25 17:03:53 +01:00
|
|
|
|
2020-03-12 13:37:40 +01:00
|
|
|
btn_pick->set_icon(get_theme_icon("screen_picker", "ColorPicker"));
|
|
|
|
bt_add_preset->set_icon(get_theme_icon("add_preset"));
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_controls();
|
|
|
|
} break;
|
2015-11-21 06:51:42 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2017-12-25 17:03:53 +01:00
|
|
|
|
2020-03-12 13:37:40 +01:00
|
|
|
btn_pick->set_icon(get_theme_icon("screen_picker", "ColorPicker"));
|
|
|
|
bt_add_preset->set_icon(get_theme_icon("add_preset"));
|
2016-02-02 01:32:47 +01:00
|
|
|
|
|
|
|
_update_color();
|
2018-09-24 16:28:18 +02:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedColorArray saved_presets = EditorSettings::get_singleton()->get_project_metadata("color_picker", "presets", PackedColorArray());
|
2018-09-24 16:28:18 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < saved_presets.size(); i++) {
|
|
|
|
add_preset(saved_presets[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-18 22:16:54 +02:00
|
|
|
} break;
|
2017-07-04 00:52:45 +02:00
|
|
|
case NOTIFICATION_PARENTED: {
|
2017-12-25 17:03:53 +01:00
|
|
|
|
2017-07-04 00:52:45 +02:00
|
|
|
for (int i = 0; i < 4; i++)
|
2020-03-12 13:37:40 +01:00
|
|
|
set_margin((Margin)i, get_theme_constant("margin"));
|
2017-07-04 00:52:45 +02:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
2017-08-24 22:58:51 +02:00
|
|
|
|
|
|
|
Popup *p = Object::cast_to<Popup>(get_parent());
|
|
|
|
if (p)
|
2020-03-12 13:37:40 +01:00
|
|
|
p->set_size(Size2(get_combined_minimum_size().width + get_theme_constant("margin") * 2, get_combined_minimum_size().height + get_theme_constant("margin") * 2));
|
2017-07-04 00:52:45 +02:00
|
|
|
} break;
|
2020-03-04 17:36:09 +01:00
|
|
|
case NOTIFICATION_WM_CLOSE_REQUEST: {
|
2017-12-25 17:03:53 +01:00
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
if (screen != nullptr && screen->is_visible())
|
2019-01-28 04:16:40 +01:00
|
|
|
screen->hide();
|
2017-06-18 22:16:54 +02:00
|
|
|
} break;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:55:35 +02:00
|
|
|
void ColorPicker::set_focus_on_line_edit() {
|
|
|
|
|
2018-03-09 21:02:58 +01:00
|
|
|
c_text->call_deferred("grab_focus");
|
2016-07-01 15:55:35 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void ColorPicker::_update_controls() {
|
|
|
|
|
2019-05-03 19:38:45 +02:00
|
|
|
const char *rgb[3] = { "R", "G", "B" };
|
|
|
|
const char *hsv[3] = { "H", "S", "V" };
|
|
|
|
|
|
|
|
if (hsv_mode_enabled) {
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
labels[i]->set_text(hsv[i]);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
labels[i]->set_text(rgb[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
if (edit_alpha) {
|
|
|
|
values[3]->show();
|
|
|
|
scroll[3]->show();
|
|
|
|
labels[3]->show();
|
|
|
|
} else {
|
|
|
|
values[3]->hide();
|
|
|
|
scroll[3]->hide();
|
|
|
|
labels[3]->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:52:54 +02:00
|
|
|
void ColorPicker::_set_pick_color(const Color &p_color, bool p_update_sliders) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
color = p_color;
|
2016-02-06 03:35:15 +01:00
|
|
|
if (color != last_hsv) {
|
2017-03-05 16:44:50 +01:00
|
|
|
h = color.get_h();
|
|
|
|
s = color.get_s();
|
|
|
|
v = color.get_v();
|
2016-02-06 03:35:15 +01:00
|
|
|
last_hsv = color;
|
|
|
|
}
|
2016-02-02 01:32:47 +01:00
|
|
|
|
|
|
|
if (!is_inside_tree())
|
|
|
|
return;
|
|
|
|
|
2019-05-03 19:38:45 +02:00
|
|
|
_update_color(p_update_sliders);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 14:52:54 +02:00
|
|
|
void ColorPicker::set_pick_color(const Color &p_color) {
|
|
|
|
|
|
|
|
_set_pick_color(p_color, true); //because setters can't have more arguments
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void ColorPicker::set_edit_alpha(bool p_show) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
edit_alpha = p_show;
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_controls();
|
2016-02-02 01:32:47 +01:00
|
|
|
|
|
|
|
if (!is_inside_tree())
|
|
|
|
return;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_color();
|
2015-11-21 06:51:42 +01:00
|
|
|
sample->update();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ColorPicker::is_editing_alpha() const {
|
|
|
|
|
|
|
|
return edit_alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::_value_changed(double) {
|
|
|
|
|
|
|
|
if (updating)
|
|
|
|
return;
|
|
|
|
|
2019-05-03 19:38:45 +02:00
|
|
|
if (hsv_mode_enabled) {
|
|
|
|
color.set_hsv(scroll[0]->get_value() / 360.0,
|
|
|
|
scroll[1]->get_value() / 100.0,
|
|
|
|
scroll[2]->get_value() / 100.0,
|
2019-06-28 16:25:00 +02:00
|
|
|
scroll[3]->get_value() / 255.0);
|
2019-05-03 19:38:45 +02: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 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 14:52:54 +02:00
|
|
|
_set_pick_color(color, false);
|
2017-03-05 16:44:50 +01:00
|
|
|
emit_signal("color_changed", color);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPicker::_html_entered(const String &p_html) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-01-26 22:07:32 +01:00
|
|
|
if (updating || text_is_constructor || !c_text->is_visible())
|
2014-02-10 02:10:30 +01:00
|
|
|
return;
|
|
|
|
|
2018-10-05 08:15:41 +02:00
|
|
|
float last_alpha = color.a;
|
2014-02-10 02:10:30 +01:00
|
|
|
color = Color::html(p_html);
|
2018-10-05 08:15:41 +02:00
|
|
|
if (!is_editing_alpha())
|
|
|
|
color.a = last_alpha;
|
2016-02-02 01:32:47 +01:00
|
|
|
|
|
|
|
if (!is_inside_tree())
|
|
|
|
return;
|
|
|
|
|
2017-01-14 15:07:57 +01:00
|
|
|
set_pick_color(color);
|
2017-03-05 16:44:50 +01:00
|
|
|
emit_signal("color_changed", color);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 19:38:45 +02:00
|
|
|
void ColorPicker::_update_color(bool p_update_sliders) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
updating = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-05-03 19:38:45 +02:00
|
|
|
if (p_update_sliders) {
|
|
|
|
|
|
|
|
if (hsv_mode_enabled) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2019-06-28 16:25:00 +02:00
|
|
|
scroll[i]->set_step(1.0);
|
2019-05-03 19:38:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-28 16:25:00 +02:00
|
|
|
scroll[0]->set_max(359);
|
2019-05-03 19:38:45 +02: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 16:25:00 +02:00
|
|
|
scroll[3]->set_max(255);
|
|
|
|
scroll[3]->set_value(color.components[3] * 255.0);
|
2016-07-03 03:08:25 +02:00
|
|
|
} else {
|
2019-05-03 19:38:45 +02:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (raw_mode_enabled) {
|
|
|
|
scroll[i]->set_step(0.01);
|
|
|
|
scroll[i]->set_max(100);
|
|
|
|
if (i == 3)
|
|
|
|
scroll[i]->set_max(1);
|
|
|
|
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 03:08:25 +02:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 03:08:25 +02:00
|
|
|
_update_text_value();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
sample->update();
|
2017-06-15 05:22:33 +02:00
|
|
|
uv_edit->update();
|
|
|
|
w_edit->update();
|
2017-03-05 16:44:50 +01:00
|
|
|
updating = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPicker::_update_presets() {
|
2020-03-20 03:32:09 +01:00
|
|
|
return;
|
|
|
|
//presets should be shown using buttons or something else, this method is not a good idea
|
|
|
|
|
2019-07-30 13:03:00 +02:00
|
|
|
presets_per_row = 10;
|
2017-03-05 16:44:50 +01:00
|
|
|
Size2 size = bt_add_preset->get_size();
|
2019-07-30 13:03:00 +02:00
|
|
|
Size2 preset_size = Size2(MIN(size.width * presets.size(), presets_per_row * size.width), size.height * (Math::ceil((float)presets.size() / presets_per_row)));
|
2017-09-27 21:44:48 +02:00
|
|
|
preset->set_custom_minimum_size(preset_size);
|
2019-07-30 13:03:00 +02:00
|
|
|
preset_container->set_custom_minimum_size(preset_size);
|
|
|
|
preset->draw_rect(Rect2(Point2(), preset_size), Color(1, 1, 1, 0));
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2017-09-27 21:44:48 +02:00
|
|
|
for (int i = 0; i < presets.size(); i++) {
|
2019-07-30 13:03:00 +02:00
|
|
|
int x = (i % presets_per_row) * size.width;
|
|
|
|
int y = (Math::floor((float)i / presets_per_row)) * size.height;
|
|
|
|
preset->draw_rect(Rect2(Point2(x, y), size), presets[i]);
|
2017-09-27 21:44:48 +02:00
|
|
|
}
|
2019-07-30 13:03:00 +02:00
|
|
|
_notification(NOTIFICATION_VISIBILITY_CHANGED);
|
2015-12-24 03:17:09 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPicker::_text_type_toggled() {
|
2019-01-26 22:07:32 +01:00
|
|
|
|
2016-01-26 23:10:56 +01:00
|
|
|
text_is_constructor = !text_is_constructor;
|
|
|
|
if (text_is_constructor) {
|
|
|
|
text_type->set_text("");
|
2020-03-12 13:37:40 +01:00
|
|
|
text_type->set_icon(get_theme_icon("Script", "EditorIcons"));
|
2019-01-26 22:07:32 +01:00
|
|
|
|
|
|
|
c_text->set_editable(false);
|
2016-01-26 23:10:56 +01:00
|
|
|
} else {
|
|
|
|
text_type->set_text("#");
|
2020-04-02 01:20:12 +02:00
|
|
|
text_type->set_icon(nullptr);
|
2019-01-26 22:07:32 +01:00
|
|
|
|
|
|
|
c_text->set_editable(true);
|
2016-01-26 23:10:56 +01:00
|
|
|
}
|
|
|
|
_update_color();
|
|
|
|
}
|
|
|
|
|
2017-01-14 15:07:57 +01:00
|
|
|
Color ColorPicker::get_pick_color() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPicker::add_preset(const Color &p_color) {
|
2018-01-04 01:00:11 +01:00
|
|
|
|
2015-12-24 03:17:09 +01:00
|
|
|
if (presets.find(p_color)) {
|
|
|
|
presets.move_to_back(presets.find(p_color));
|
|
|
|
} else {
|
|
|
|
presets.push_back(p_color);
|
|
|
|
}
|
2017-09-27 21:44:48 +02:00
|
|
|
preset->update();
|
2018-12-12 21:55:29 +01:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedColorArray arr_to_save = get_presets();
|
2018-12-12 21:55:29 +01:00
|
|
|
EditorSettings::get_singleton()->set_project_metadata("color_picker", "presets", arr_to_save);
|
|
|
|
}
|
|
|
|
#endif
|
2015-12-24 03:17:09 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 16:12:38 +02:00
|
|
|
void ColorPicker::erase_preset(const Color &p_color) {
|
|
|
|
|
|
|
|
if (presets.find(p_color)) {
|
|
|
|
presets.erase(presets.find(p_color));
|
|
|
|
preset->update();
|
2018-12-12 21:55:29 +01:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedColorArray arr_to_save = get_presets();
|
2018-12-12 21:55:29 +01:00
|
|
|
EditorSettings::get_singleton()->set_project_metadata("color_picker", "presets", arr_to_save);
|
|
|
|
}
|
|
|
|
#endif
|
2018-10-03 16:12:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedColorArray ColorPicker::get_presets() const {
|
2018-10-03 16:12:38 +02:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
PackedColorArray arr;
|
2018-10-03 16:12:38 +02:00
|
|
|
arr.resize(presets.size());
|
|
|
|
for (int i = 0; i < presets.size(); i++) {
|
|
|
|
arr.set(i, presets[i]);
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
2019-05-03 19:38:45 +02:00
|
|
|
void ColorPicker::set_hsv_mode(bool p_enabled) {
|
|
|
|
|
|
|
|
if (hsv_mode_enabled == p_enabled || raw_mode_enabled)
|
|
|
|
return;
|
|
|
|
hsv_mode_enabled = p_enabled;
|
|
|
|
if (btn_hsv->is_pressed() != p_enabled)
|
|
|
|
btn_hsv->set_pressed(p_enabled);
|
|
|
|
|
|
|
|
if (!is_inside_tree())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_update_controls();
|
|
|
|
_update_color();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColorPicker::is_hsv_mode() const {
|
|
|
|
|
|
|
|
return hsv_mode_enabled;
|
|
|
|
}
|
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
void ColorPicker::set_raw_mode(bool p_enabled) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-05-03 19:38:45 +02:00
|
|
|
if (raw_mode_enabled == p_enabled || hsv_mode_enabled)
|
2015-11-21 06:51:42 +01:00
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
raw_mode_enabled = p_enabled;
|
2019-05-03 19:38:45 +02:00
|
|
|
if (btn_raw->is_pressed() != p_enabled)
|
|
|
|
btn_raw->set_pressed(p_enabled);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2016-02-02 01:32:47 +01:00
|
|
|
if (!is_inside_tree())
|
|
|
|
return;
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_controls();
|
|
|
|
_update_color();
|
|
|
|
}
|
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
bool ColorPicker::is_raw_mode() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
return raw_mode_enabled;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-05-29 14:21:06 +02: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 03:08:25 +02:00
|
|
|
void ColorPicker::_update_text_value() {
|
2018-03-09 21:02:58 +01:00
|
|
|
bool visible = true;
|
2016-07-03 03:08:25 +02:00
|
|
|
if (text_is_constructor) {
|
2019-01-26 22:07:32 +01:00
|
|
|
String t = "Color(" + String::num(color.r) + ", " + String::num(color.g) + ", " + String::num(color.b);
|
2017-03-05 16:44:50 +01:00
|
|
|
if (edit_alpha && color.a < 1)
|
2019-01-26 22:07:32 +01:00
|
|
|
t += ", " + String::num(color.a) + ")";
|
2016-07-03 03:08:25 +02:00
|
|
|
else
|
2017-03-05 16:44:50 +01:00
|
|
|
t += ")";
|
2016-07-03 03:08:25 +02:00
|
|
|
c_text->set_text(t);
|
|
|
|
}
|
2019-01-26 22:07:32 +01: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 21:02:58 +01:00
|
|
|
c_text->set_visible(visible);
|
2016-07-03 03:08:25 +02:00
|
|
|
}
|
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
void ColorPicker::_sample_draw() {
|
2019-09-27 01:07:41 +02:00
|
|
|
const Rect2 r = Rect2(Point2(), Size2(uv_edit->get_size().width, sample->get_size().height * 0.95));
|
|
|
|
|
2017-09-27 21:44:48 +02:00
|
|
|
if (color.a < 1.0) {
|
2020-03-12 13:37:40 +01:00
|
|
|
sample->draw_texture_rect(get_theme_icon("preset_bg", "ColorPicker"), r, true);
|
2017-09-27 21:44:48 +02:00
|
|
|
}
|
2019-09-27 01:07:41 +02:00
|
|
|
|
2017-09-27 21:44:48 +02:00
|
|
|
sample->draw_rect(r, color);
|
2019-09-27 01:07:41 +02: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
|
2020-03-12 13:37:40 +01:00
|
|
|
sample->draw_texture(get_theme_icon("overbright_indicator", "ColorPicker"), Point2());
|
2019-09-27 01:07:41 +02:00
|
|
|
}
|
2015-11-21 06:51:42 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-07-04 00:52:45 +02:00
|
|
|
void ColorPicker::_hsv_draw(int p_which, Control *c) {
|
2015-12-25 21:59:23 +01:00
|
|
|
if (!c)
|
|
|
|
return;
|
2017-07-04 00:52:45 +02:00
|
|
|
if (p_which == 0) {
|
2016-10-20 00:43:49 +02:00
|
|
|
Vector<Point2> points;
|
|
|
|
points.push_back(Vector2());
|
2017-03-05 16:44:50 +01:00
|
|
|
points.push_back(Vector2(c->get_size().x, 0));
|
2016-10-20 00:43:49 +02:00
|
|
|
points.push_back(c->get_size());
|
2017-03-05 16:44:50 +01:00
|
|
|
points.push_back(Vector2(0, c->get_size().y));
|
2016-10-20 00:43:49 +02:00
|
|
|
Vector<Color> colors;
|
2017-06-15 05:22:33 +02:00
|
|
|
colors.push_back(Color(1, 1, 1, 1));
|
|
|
|
colors.push_back(Color(1, 1, 1, 1));
|
|
|
|
colors.push_back(Color(0, 0, 0, 1));
|
|
|
|
colors.push_back(Color(0, 0, 0, 1));
|
2017-03-05 16:44:50 +01:00
|
|
|
c->draw_polygon(points, colors);
|
2016-10-20 00:43:49 +02:00
|
|
|
Vector<Color> colors2;
|
|
|
|
Color col = color;
|
2017-06-18 16:00:36 +02:00
|
|
|
col.set_hsv(h, 1, 1);
|
2016-10-20 00:43:49 +02:00
|
|
|
col.a = 0;
|
|
|
|
colors2.push_back(col);
|
|
|
|
col.a = 1;
|
|
|
|
colors2.push_back(col);
|
2017-06-18 16:00:36 +02:00
|
|
|
col.set_hsv(h, 1, 0);
|
2016-10-20 00:43:49 +02:00
|
|
|
colors2.push_back(col);
|
|
|
|
col.a = 0;
|
|
|
|
colors2.push_back(col);
|
2017-06-15 05:22:33 +02:00
|
|
|
c->draw_polygon(points, colors2);
|
2016-07-03 03:08:25 +02:00
|
|
|
int x = CLAMP(c->get_size().x * s, 0, c->get_size().x);
|
2017-03-05 16:44:50 +01:00
|
|
|
int y = CLAMP(c->get_size().y - c->get_size().y * v, 0, c->get_size().y);
|
2016-10-20 00:43:49 +02:00
|
|
|
col = color;
|
2017-03-05 16:44:50 +01:00
|
|
|
col.a = 1;
|
|
|
|
c->draw_line(Point2(x, 0), Point2(x, c->get_size().y), col.inverted());
|
|
|
|
c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
|
|
|
|
c->draw_line(Point2(x, y), Point2(x, y), Color(1, 1, 1), 2);
|
2017-07-04 00:52:45 +02:00
|
|
|
} else if (p_which == 1) {
|
2020-03-12 13:37:40 +01:00
|
|
|
Ref<Texture2D> hue = get_theme_icon("color_hue", "ColorPicker");
|
2017-03-05 16:44:50 +01:00
|
|
|
c->draw_texture_rect(hue, Rect2(Point2(), c->get_size()));
|
2017-06-15 05:22:33 +02:00
|
|
|
int y = c->get_size().y - c->get_size().y * (1.0 - h);
|
2017-03-05 16:44:50 +01:00
|
|
|
Color col = Color();
|
|
|
|
col.set_hsv(h, 1, 1);
|
|
|
|
c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
|
2015-12-25 21:59:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
void ColorPicker::_uv_input(const Ref<InputEvent> &p_event) {
|
2017-05-20 17:38:03 +02:00
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
Ref<InputEventMouseButton> bev = p_event;
|
2017-05-20 17:38:03 +02:00
|
|
|
|
|
|
|
if (bev.is_valid()) {
|
|
|
|
if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
|
2015-11-21 06:51:42 +01:00
|
|
|
changing_color = true;
|
2017-06-18 16:00:36 +02:00
|
|
|
float x = CLAMP((float)bev->get_position().x, 0, uv_edit->get_size().width);
|
|
|
|
float y = CLAMP((float)bev->get_position().y, 0, uv_edit->get_size().height);
|
|
|
|
s = x / uv_edit->get_size().width;
|
|
|
|
v = 1.0 - y / uv_edit->get_size().height;
|
2017-03-05 16:44:50 +01:00
|
|
|
color.set_hsv(h, s, v, color.a);
|
2016-02-06 03:35:15 +01:00
|
|
|
last_hsv = color;
|
2017-01-14 15:07:57 +01:00
|
|
|
set_pick_color(color);
|
2015-12-24 03:17:09 +01:00
|
|
|
_update_color();
|
2018-05-29 14:21:06 +02:00
|
|
|
if (!deferred_mode_enabled)
|
|
|
|
emit_signal("color_changed", color);
|
|
|
|
} else if (deferred_mode_enabled && !bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
|
2015-12-24 03:17:09 +01:00
|
|
|
emit_signal("color_changed", color);
|
2018-05-29 14:21:06 +02:00
|
|
|
changing_color = false;
|
2015-11-21 06:51:42 +01:00
|
|
|
} else {
|
|
|
|
changing_color = false;
|
|
|
|
}
|
2017-05-20 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
Ref<InputEventMouseMotion> mev = p_event;
|
2017-05-20 17:38:03 +02:00
|
|
|
|
|
|
|
if (mev.is_valid()) {
|
2015-11-21 06:51:42 +01:00
|
|
|
if (!changing_color)
|
|
|
|
return;
|
2017-06-18 16:00:36 +02:00
|
|
|
float x = CLAMP((float)mev->get_position().x, 0, uv_edit->get_size().width);
|
|
|
|
float y = CLAMP((float)mev->get_position().y, 0, uv_edit->get_size().height);
|
|
|
|
s = x / uv_edit->get_size().width;
|
|
|
|
v = 1.0 - y / uv_edit->get_size().height;
|
2017-03-05 16:44:50 +01:00
|
|
|
color.set_hsv(h, s, v, color.a);
|
2016-02-06 03:35:15 +01:00
|
|
|
last_hsv = color;
|
2017-01-14 15:07:57 +01:00
|
|
|
set_pick_color(color);
|
2015-12-24 03:17:09 +01:00
|
|
|
_update_color();
|
2018-05-29 14:21:06 +02:00
|
|
|
if (!deferred_mode_enabled)
|
|
|
|
emit_signal("color_changed", color);
|
2015-11-21 06:51:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
|
2017-05-20 17:38:03 +02:00
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
Ref<InputEventMouseButton> bev = p_event;
|
2017-05-20 17:38:03 +02:00
|
|
|
|
|
|
|
if (bev.is_valid()) {
|
|
|
|
|
|
|
|
if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
|
2015-11-21 06:51:42 +01:00
|
|
|
changing_color = true;
|
2017-06-18 16:00:36 +02:00
|
|
|
float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
|
|
|
|
h = y / w_edit->get_size().height;
|
2015-11-21 06:51:42 +01:00
|
|
|
} else {
|
|
|
|
changing_color = false;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
color.set_hsv(h, s, v, color.a);
|
2016-02-06 03:35:15 +01:00
|
|
|
last_hsv = color;
|
2017-01-14 15:07:57 +01:00
|
|
|
set_pick_color(color);
|
2015-12-24 03:17:09 +01:00
|
|
|
_update_color();
|
2018-05-29 14:21:06 +02:00
|
|
|
if (!deferred_mode_enabled)
|
|
|
|
emit_signal("color_changed", color);
|
|
|
|
else if (!bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT)
|
|
|
|
emit_signal("color_changed", color);
|
2017-05-20 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
Ref<InputEventMouseMotion> mev = p_event;
|
2017-05-20 17:38:03 +02:00
|
|
|
|
|
|
|
if (mev.is_valid()) {
|
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
if (!changing_color)
|
|
|
|
return;
|
2017-06-18 16:00:36 +02:00
|
|
|
float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
|
|
|
|
h = y / w_edit->get_size().height;
|
2017-03-05 16:44:50 +01:00
|
|
|
color.set_hsv(h, s, v, color.a);
|
2016-02-06 03:35:15 +01:00
|
|
|
last_hsv = color;
|
2017-01-14 15:07:57 +01:00
|
|
|
set_pick_color(color);
|
2015-12-24 03:17:09 +01:00
|
|
|
_update_color();
|
2018-05-29 14:21:06 +02:00
|
|
|
if (!deferred_mode_enabled)
|
|
|
|
emit_signal("color_changed", color);
|
2015-11-21 06:51:42 +01:00
|
|
|
}
|
2015-12-24 03:17:09 +01:00
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
void ColorPicker::_preset_input(const Ref<InputEvent> &p_event) {
|
2017-05-20 17:38:03 +02:00
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
Ref<InputEventMouseButton> bev = p_event;
|
2017-05-20 17:38:03 +02:00
|
|
|
|
|
|
|
if (bev.is_valid()) {
|
2019-07-30 13:03:00 +02:00
|
|
|
int index = 0;
|
2017-05-20 17:38:03 +02:00
|
|
|
if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
|
2019-07-30 13:03:00 +02:00
|
|
|
for (int i = 0; i < presets.size(); i++) {
|
|
|
|
int x = (i % presets_per_row) * bt_add_preset->get_size().x;
|
|
|
|
int y = (Math::floor((float)i / presets_per_row)) * bt_add_preset->get_size().y;
|
|
|
|
if (bev->get_position().x > x && bev->get_position().x < x + preset->get_size().x && bev->get_position().y > y && bev->get_position().y < y + preset->get_size().y) {
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
}
|
2017-01-14 15:07:57 +01:00
|
|
|
set_pick_color(presets[index]);
|
2018-12-12 21:55:29 +01:00
|
|
|
_update_color();
|
|
|
|
emit_signal("color_changed", color);
|
2019-02-24 21:42:46 +01:00
|
|
|
} else if (bev->is_pressed() && bev->get_button_index() == BUTTON_RIGHT && presets_enabled) {
|
2019-07-30 13:03:00 +02:00
|
|
|
index = bev->get_position().x / (preset->get_size().x / presets.size());
|
2018-10-03 16:12:38 +02:00
|
|
|
Color clicked_preset = presets[index];
|
2018-12-12 21:55:29 +01:00
|
|
|
erase_preset(clicked_preset);
|
2018-10-03 16:12:38 +02:00
|
|
|
emit_signal("preset_removed", clicked_preset);
|
2015-12-24 03:17:09 +01:00
|
|
|
bt_add_preset->show();
|
|
|
|
}
|
2017-05-20 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
Ref<InputEventMouseMotion> mev = p_event;
|
2017-05-20 17:38:03 +02:00
|
|
|
|
|
|
|
if (mev.is_valid()) {
|
|
|
|
|
2017-06-03 10:54:24 +02:00
|
|
|
int index = mev->get_position().x * presets.size();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (preset->get_size().x != 0) {
|
2016-03-01 00:08:33 +01:00
|
|
|
index /= preset->get_size().x;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (index < 0 || index >= presets.size())
|
2015-12-24 03:17:09 +01:00
|
|
|
return;
|
2020-01-04 06:33:52 +01:00
|
|
|
preset->set_tooltip(vformat(RTR("Color: #%s\nLMB: Set color\nRMB: Remove preset"), presets[index].to_html(presets[index].a < 1)));
|
2015-12-24 03:17:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
void ColorPicker::_screen_input(const Ref<InputEvent> &p_event) {
|
2017-05-20 17:38:03 +02:00
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
Ref<InputEventMouseButton> bev = p_event;
|
2019-01-28 04:16:40 +01:00
|
|
|
if (bev.is_valid() && bev->get_button_index() == BUTTON_LEFT && !bev->is_pressed()) {
|
|
|
|
emit_signal("color_changed", color);
|
|
|
|
screen->hide();
|
2017-05-20 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
2017-08-11 21:10:05 +02:00
|
|
|
Ref<InputEventMouseMotion> mev = p_event;
|
2017-05-20 17:38:03 +02:00
|
|
|
if (mev.is_valid()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
Viewport *r = get_tree()->get_root();
|
2017-06-03 10:54:24 +02:00
|
|
|
if (!r->get_visible_rect().has_point(Point2(mev->get_global_position().x, mev->get_global_position().y)))
|
2015-12-25 15:08:02 +01:00
|
|
|
return;
|
2019-01-28 04:16:40 +01:00
|
|
|
|
2017-06-18 22:16:54 +02:00
|
|
|
Ref<Image> img = r->get_texture()->get_data();
|
|
|
|
if (img.is_valid() && !img->empty()) {
|
2020-02-17 22:06:54 +01:00
|
|
|
|
2017-06-18 22:16:54 +02: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 22:06:54 +01:00
|
|
|
|
2017-01-14 15:07:57 +01:00
|
|
|
set_pick_color(c);
|
2016-10-03 21:33:42 +02:00
|
|
|
}
|
2015-12-25 15:08:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 03:17:09 +01:00
|
|
|
void ColorPicker::_add_preset_pressed() {
|
|
|
|
add_preset(color);
|
2018-10-03 16:12:38 +02:00
|
|
|
emit_signal("preset_added", color);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPicker::_screen_pick_pressed() {
|
|
|
|
Viewport *r = get_tree()->get_root();
|
2015-12-25 15:08:02 +01:00
|
|
|
if (!screen) {
|
2017-03-05 16:44:50 +01:00
|
|
|
screen = memnew(Control);
|
2015-12-25 15:08:02 +01:00
|
|
|
r->add_child(screen);
|
2016-07-23 01:35:12 +02:00
|
|
|
screen->set_as_toplevel(true);
|
2017-09-22 00:12:33 +02:00
|
|
|
screen->set_anchors_and_margins_preset(Control::PRESET_WIDE);
|
2017-06-18 22:16:54 +02:00
|
|
|
screen->set_default_cursor_shape(CURSOR_POINTING_HAND);
|
2020-02-21 18:28:45 +01:00
|
|
|
screen->connect("gui_input", callable_mp(this, &ColorPicker::_screen_input));
|
2019-01-28 04:16:40 +01:00
|
|
|
// It immediately toggles off in the first press otherwise.
|
2020-02-19 20:27:19 +01:00
|
|
|
screen->call_deferred("connect", "hide", Callable(btn_pick, "set_pressed"), varray(false));
|
2015-12-25 15:08:02 +01:00
|
|
|
}
|
|
|
|
screen->raise();
|
2020-03-14 17:06:39 +01:00
|
|
|
#ifndef _MSC_VER
|
|
|
|
#warning show modal no longer works, needs to be converted to a popup
|
|
|
|
#endif
|
|
|
|
//screen->show_modal();
|
2015-12-25 15:08:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 21:02:58 +01:00
|
|
|
void ColorPicker::_focus_enter() {
|
2020-03-24 12:41:09 +01:00
|
|
|
bool has_ctext_focus = c_text->has_focus();
|
|
|
|
if (has_ctext_focus) {
|
2018-03-09 21:02:58 +01:00
|
|
|
c_text->select_all();
|
2020-03-24 12:41:09 +01:00
|
|
|
} else {
|
|
|
|
c_text->select(0, 0);
|
2018-03-09 21:02:58 +01:00
|
|
|
}
|
2020-03-24 12:41:09 +01:00
|
|
|
|
2018-03-09 21:02:58 +01:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2020-03-24 12:41:09 +01:00
|
|
|
if (values[i]->get_line_edit()->has_focus() && !has_ctext_focus) {
|
2018-03-09 21:02:58 +01:00
|
|
|
values[i]->get_line_edit()->select_all();
|
2020-03-24 12:41:09 +01:00
|
|
|
} else {
|
|
|
|
values[i]->get_line_edit()->select(0, 0);
|
2018-03-09 21:02:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::_focus_exit() {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2020-03-24 12:41:09 +01:00
|
|
|
if (!values[i]->get_line_edit()->get_menu()->is_visible())
|
|
|
|
values[i]->get_line_edit()->select(0, 0);
|
2018-03-09 21:02:58 +01:00
|
|
|
}
|
|
|
|
c_text->select(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::_html_focus_exit() {
|
2020-03-24 12:41:09 +01:00
|
|
|
if (c_text->get_menu()->is_visible())
|
|
|
|
return;
|
2018-03-09 21:02:58 +01:00
|
|
|
_html_entered(c_text->get_text());
|
|
|
|
_focus_exit();
|
|
|
|
}
|
|
|
|
|
2019-02-24 21:42:46 +01:00
|
|
|
void ColorPicker::set_presets_enabled(bool p_enabled) {
|
|
|
|
presets_enabled = p_enabled;
|
|
|
|
if (!p_enabled) {
|
|
|
|
bt_add_preset->set_disabled(true);
|
|
|
|
bt_add_preset->set_focus_mode(FOCUS_NONE);
|
|
|
|
} else {
|
|
|
|
bt_add_preset->set_disabled(false);
|
|
|
|
bt_add_preset->set_focus_mode(FOCUS_ALL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-02-15 06:15:32 +01:00
|
|
|
preset_container2->set_visible(p_visible);
|
2019-02-24 21:42:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ColorPicker::are_presets_visible() const {
|
|
|
|
return presets_visible;
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void ColorPicker::_bind_methods() {
|
|
|
|
|
2017-03-05 16:44:50 +01: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);
|
2020-02-21 18:28:45 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_hsv_mode"), &ColorPicker::set_hsv_mode);
|
2019-05-03 19:38:45 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("is_hsv_mode"), &ColorPicker::is_hsv_mode);
|
2020-02-21 18:28:45 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_raw_mode"), &ColorPicker::set_raw_mode);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_raw_mode"), &ColorPicker::is_raw_mode);
|
2018-05-29 14:21:06 +02: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 16:44:50 +01: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 21:42:46 +01: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 21:03:34 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("add_preset", "color"), &ColorPicker::add_preset);
|
2018-10-03 16:12:38 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("erase_preset", "color"), &ColorPicker::erase_preset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_presets"), &ColorPicker::get_presets);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-01-11 23:35:12 +01: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 19:38:45 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hsv_mode"), "set_hsv_mode", "is_hsv_mode");
|
2018-01-11 23:35:12 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "raw_mode"), "set_raw_mode", "is_raw_mode");
|
2018-05-29 14:21:06 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deferred_mode"), "set_deferred_mode", "is_deferred_mode");
|
2019-02-24 21:42:46 +01: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 23:35:12 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
|
2018-10-03 16:12:38 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("preset_added", PropertyInfo(Variant::COLOR, "color")));
|
|
|
|
ADD_SIGNAL(MethodInfo("preset_removed", PropertyInfo(Variant::COLOR, "color")));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-12-06 21:36:34 +01:00
|
|
|
ColorPicker::ColorPicker() :
|
|
|
|
BoxContainer(true) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
updating = true;
|
|
|
|
edit_alpha = true;
|
2016-01-26 23:10:56 +01:00
|
|
|
text_is_constructor = false;
|
2019-05-03 19:38:45 +02:00
|
|
|
hsv_mode_enabled = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
raw_mode_enabled = false;
|
2018-05-29 14:21:06 +02:00
|
|
|
deferred_mode_enabled = false;
|
2017-03-05 16:44:50 +01:00
|
|
|
changing_color = false;
|
2019-02-24 21:42:46 +01:00
|
|
|
presets_enabled = true;
|
|
|
|
presets_visible = true;
|
2020-04-02 01:20:12 +02:00
|
|
|
screen = nullptr;
|
2015-11-21 06:51:42 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *hb_edit = memnew(HBoxContainer);
|
2019-01-26 22:07:32 +01:00
|
|
|
add_child(hb_edit);
|
2017-06-18 16:00:36 +02:00
|
|
|
hb_edit->set_v_size_flags(SIZE_EXPAND_FILL);
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uv_edit = memnew(Control);
|
2019-01-26 22:07:32 +01:00
|
|
|
hb_edit->add_child(uv_edit);
|
2020-02-21 18:28:45 +01:00
|
|
|
uv_edit->connect("gui_input", callable_mp(this, &ColorPicker::_uv_input));
|
2017-01-08 23:54:19 +01:00
|
|
|
uv_edit->set_mouse_filter(MOUSE_FILTER_PASS);
|
2017-06-18 16:00:36 +02:00
|
|
|
uv_edit->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
uv_edit->set_v_size_flags(SIZE_EXPAND_FILL);
|
2020-03-12 13:37:40 +01:00
|
|
|
uv_edit->set_custom_minimum_size(Size2(get_theme_constant("sv_width"), get_theme_constant("sv_height")));
|
2020-02-21 18:28:45 +01:00
|
|
|
uv_edit->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw), make_binds(0, uv_edit));
|
2015-12-24 03:17:09 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
w_edit = memnew(Control);
|
2019-01-26 22:07:32 +01:00
|
|
|
hb_edit->add_child(w_edit);
|
2020-03-12 13:37:40 +01:00
|
|
|
w_edit->set_custom_minimum_size(Size2(get_theme_constant("h_width"), 0));
|
2017-06-18 16:00:36 +02:00
|
|
|
w_edit->set_h_size_flags(SIZE_FILL);
|
|
|
|
w_edit->set_v_size_flags(SIZE_EXPAND_FILL);
|
2020-02-21 18:28:45 +01:00
|
|
|
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));
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2019-12-28 18:08:16 +01:00
|
|
|
HBoxContainer *hb_smpl = memnew(HBoxContainer);
|
|
|
|
add_child(hb_smpl);
|
|
|
|
|
|
|
|
sample = memnew(TextureRect);
|
|
|
|
hb_smpl->add_child(sample);
|
|
|
|
sample->set_h_size_flags(SIZE_EXPAND_FILL);
|
2020-02-21 18:28:45 +01:00
|
|
|
sample->connect("draw", callable_mp(this, &ColorPicker::_sample_draw));
|
2019-12-28 18:08:16 +01:00
|
|
|
|
|
|
|
btn_pick = memnew(ToolButton);
|
|
|
|
hb_smpl->add_child(btn_pick);
|
|
|
|
btn_pick->set_toggle_mode(true);
|
|
|
|
btn_pick->set_tooltip(TTR("Pick a color from the editor window."));
|
2020-02-21 18:28:45 +01:00
|
|
|
btn_pick->connect("pressed", callable_mp(this, &ColorPicker::_screen_pick_pressed));
|
2019-12-28 18:08:16 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VBoxContainer *vbl = memnew(VBoxContainer);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(vbl);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
add_child(memnew(HSeparator));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
VBoxContainer *vbr = memnew(VBoxContainer);
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(vbr);
|
|
|
|
vbr->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *hbc = memnew(HBoxContainer);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2019-05-03 19:38:45 +02:00
|
|
|
labels[i] = memnew(Label());
|
2020-03-12 13:37:40 +01:00
|
|
|
labels[i]->set_custom_minimum_size(Size2(get_theme_constant("label_width"), 0));
|
2017-07-04 00:52:45 +02:00
|
|
|
labels[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
|
2014-02-10 02:10:30 +01:00
|
|
|
hbc->add_child(labels[i]);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
scroll[i] = memnew(HSlider);
|
2017-07-04 00:52:45 +02:00
|
|
|
scroll[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
|
2018-03-09 21:02:58 +01:00
|
|
|
scroll[i]->set_focus_mode(FOCUS_NONE);
|
2014-02-10 02:10:30 +01:00
|
|
|
hbc->add_child(scroll[i]);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
values[i] = memnew(SpinBox);
|
2014-02-10 02:10:30 +01:00
|
|
|
scroll[i]->share(values[i]);
|
|
|
|
hbc->add_child(values[i]);
|
2020-02-21 18:28:45 +01: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 02:10:30 +01:00
|
|
|
|
|
|
|
scroll[i]->set_min(0);
|
|
|
|
scroll[i]->set_page(0);
|
|
|
|
scroll[i]->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
2020-02-21 18:28:45 +01:00
|
|
|
scroll[i]->connect("value_changed", callable_mp(this, &ColorPicker::_value_changed));
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
vbr->add_child(hbc);
|
|
|
|
}
|
2019-05-03 19:38:45 +02:00
|
|
|
labels[3]->set_text("A");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *hhb = memnew(HBoxContainer);
|
2019-01-26 22:07:32 +01:00
|
|
|
vbr->add_child(hhb);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2019-05-03 19:38:45 +02:00
|
|
|
btn_hsv = memnew(CheckButton);
|
|
|
|
hhb->add_child(btn_hsv);
|
|
|
|
btn_hsv->set_text(TTR("HSV"));
|
2020-02-21 18:28:45 +01:00
|
|
|
btn_hsv->connect("toggled", callable_mp(this, &ColorPicker::set_hsv_mode));
|
2019-05-03 19:38:45 +02:00
|
|
|
|
|
|
|
btn_raw = memnew(CheckButton);
|
|
|
|
hhb->add_child(btn_raw);
|
|
|
|
btn_raw->set_text(TTR("Raw"));
|
2020-02-21 18:28:45 +01:00
|
|
|
btn_raw->connect("toggled", callable_mp(this, &ColorPicker::set_raw_mode));
|
2019-01-26 22:07:32 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
text_type = memnew(Button);
|
2016-01-26 23:10:56 +01:00
|
|
|
hhb->add_child(text_type);
|
2019-01-26 22:07:32 +01:00
|
|
|
text_type->set_text("#");
|
|
|
|
text_type->set_tooltip(TTR("Switch between hexadecimal and code values."));
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
text_type->set_custom_minimum_size(Size2(28 * EDSCALE, 0)); // Adjust for the width of the "Script" icon.
|
|
|
|
#endif
|
2020-02-21 18:28:45 +01:00
|
|
|
text_type->connect("pressed", callable_mp(this, &ColorPicker::_text_type_toggled));
|
2019-01-26 22:07:32 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
text_type->set_flat(true);
|
|
|
|
text_type->set_mouse_filter(MOUSE_FILTER_IGNORE);
|
|
|
|
}
|
2016-01-26 23:10:56 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
c_text = memnew(LineEdit);
|
2016-01-26 23:10:56 +01:00
|
|
|
hhb->add_child(c_text);
|
2019-01-26 22:07:32 +01:00
|
|
|
c_text->set_h_size_flags(SIZE_EXPAND_FILL);
|
2020-02-21 18:28:45 +01:00
|
|
|
c_text->connect("text_entered", callable_mp(this, &ColorPicker::_html_entered));
|
|
|
|
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 21:02:58 +01:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_controls();
|
2017-03-05 16:44:50 +01:00
|
|
|
updating = false;
|
2015-12-24 03:17:09 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
set_pick_color(Color(1, 1, 1));
|
2015-12-24 03:17:09 +01:00
|
|
|
|
2019-02-24 21:42:46 +01:00
|
|
|
preset_separator = memnew(HSeparator);
|
|
|
|
add_child(preset_separator);
|
2019-01-26 22:07:32 +01:00
|
|
|
|
2019-02-24 21:42:46 +01:00
|
|
|
preset_container = memnew(HBoxContainer);
|
2019-07-30 13:03:00 +02:00
|
|
|
preset_container->set_h_size_flags(SIZE_EXPAND_FILL);
|
2019-02-24 21:42:46 +01:00
|
|
|
add_child(preset_container);
|
2015-12-24 03:17:09 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
preset = memnew(TextureRect);
|
2019-02-24 21:42:46 +01:00
|
|
|
preset_container->add_child(preset);
|
2020-02-21 18:28:45 +01:00
|
|
|
preset->connect("gui_input", callable_mp(this, &ColorPicker::_preset_input));
|
|
|
|
preset->connect("draw", callable_mp(this, &ColorPicker::_update_presets));
|
2015-12-24 03:17:09 +01:00
|
|
|
|
2019-07-30 13:03:00 +02:00
|
|
|
preset_container2 = memnew(HBoxContainer);
|
|
|
|
preset_container2->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
add_child(preset_container2);
|
2017-03-05 16:44:50 +01:00
|
|
|
bt_add_preset = memnew(Button);
|
2019-07-30 13:03:00 +02:00
|
|
|
preset_container2->add_child(bt_add_preset);
|
2019-02-10 17:17:29 +01:00
|
|
|
bt_add_preset->set_tooltip(TTR("Add current color as a preset."));
|
2020-02-21 18:28:45 +01:00
|
|
|
bt_add_preset->connect("pressed", callable_mp(this, &ColorPicker::_add_preset_pressed));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPickerButton::_color_changed(const Color &p_color) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-05-15 22:12:35 +02:00
|
|
|
color = p_color;
|
2014-02-10 02:10:30 +01:00
|
|
|
update();
|
2018-05-15 22:12:35 +02:00
|
|
|
emit_signal("color_changed", color);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-04-12 23:47:53 +02:00
|
|
|
void ColorPickerButton::_modal_closed() {
|
|
|
|
|
|
|
|
emit_signal("popup_closed");
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void ColorPickerButton::pressed() {
|
|
|
|
|
2018-05-15 22:12:35 +02:00
|
|
|
_update_picker();
|
2020-03-20 03:32:09 +01:00
|
|
|
|
|
|
|
popup->set_as_minsize();
|
|
|
|
|
|
|
|
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 {
|
|
|
|
cp_rect.position.y = get_screen_position().y + get_size().height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i & 1) {
|
|
|
|
cp_rect.position.x = get_screen_position().x;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
cp_rect.position.x = get_screen_position().x - MAX(0, (cp_rect.size.x - get_size().x));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usable_rect.encloses(cp_rect)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
popup->set_position(cp_rect.position);
|
2014-02-10 02:10:30 +01:00
|
|
|
popup->popup();
|
2016-07-01 15:55:35 +02:00
|
|
|
picker->set_focus_on_line_edit();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPickerButton::_notification(int p_what) {
|
|
|
|
|
2019-04-23 03:28:38 +02:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_DRAW: {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-03-12 13:37:40 +01:00
|
|
|
const Ref<StyleBox> normal = get_theme_stylebox("normal");
|
2019-09-27 01:07:41 +02:00
|
|
|
const Rect2 r = Rect2(normal->get_offset(), get_size() - normal->get_minimum_size());
|
2020-03-12 13:37:40 +01:00
|
|
|
draw_texture_rect(Control::get_theme_icon("bg", "ColorPickerButton"), r, true);
|
2019-04-23 03:28:38 +02:00
|
|
|
draw_rect(r, color);
|
2019-09-27 01:07:41 +02: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
|
2020-03-12 13:37:40 +01:00
|
|
|
draw_texture(Control::get_theme_icon("overbright_indicator", "ColorPicker"), normal->get_offset());
|
2019-09-27 01:07:41 +02:00
|
|
|
}
|
2019-04-23 03:28:38 +02:00
|
|
|
} break;
|
2020-03-04 17:36:09 +01:00
|
|
|
case NOTIFICATION_WM_CLOSE_REQUEST: {
|
2017-06-18 22:16:54 +02:00
|
|
|
|
2019-04-23 03:28:38 +02:00
|
|
|
if (popup)
|
|
|
|
popup->hide();
|
|
|
|
} break;
|
2017-06-18 22:16:54 +02:00
|
|
|
}
|
2019-04-19 00:50:35 +02:00
|
|
|
|
|
|
|
if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
|
|
|
|
if (popup && !is_visible_in_tree()) {
|
|
|
|
popup->hide();
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPickerButton::set_pick_color(const Color &p_color) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-05-15 22:12:35 +02:00
|
|
|
color = p_color;
|
|
|
|
if (picker) {
|
|
|
|
picker->set_pick_color(p_color);
|
|
|
|
}
|
|
|
|
|
2015-03-22 16:52:47 +01:00
|
|
|
update();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
Color ColorPickerButton::get_pick_color() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-05-15 22:12:35 +02:00
|
|
|
return color;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPickerButton::set_edit_alpha(bool p_show) {
|
|
|
|
|
2018-05-15 22:12:35 +02:00
|
|
|
edit_alpha = p_show;
|
|
|
|
if (picker) {
|
|
|
|
picker->set_edit_alpha(p_show);
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool ColorPickerButton::is_editing_alpha() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2018-05-15 22:12:35 +02:00
|
|
|
return edit_alpha;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 22:12:35 +02:00
|
|
|
ColorPicker *ColorPickerButton::get_picker() {
|
2017-12-25 17:03:53 +01:00
|
|
|
|
2018-05-15 22:12:35 +02:00
|
|
|
_update_picker();
|
2016-06-19 07:08:22 +02:00
|
|
|
return picker;
|
|
|
|
}
|
|
|
|
|
2018-07-10 15:06:36 +02:00
|
|
|
PopupPanel *ColorPickerButton::get_popup() {
|
2017-12-25 17:03:53 +01:00
|
|
|
|
2018-07-10 15:06:36 +02:00
|
|
|
_update_picker();
|
2017-12-01 21:58:53 +01:00
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
2018-05-15 22:12:35 +02:00
|
|
|
void ColorPickerButton::_update_picker() {
|
|
|
|
if (!picker) {
|
|
|
|
popup = memnew(PopupPanel);
|
2020-03-20 03:32:09 +01:00
|
|
|
popup->set_wrap_controls(true);
|
2018-05-15 22:12:35 +02:00
|
|
|
picker = memnew(ColorPicker);
|
2020-03-20 03:32:09 +01:00
|
|
|
picker->set_anchors_and_margins_preset(PRESET_WIDE);
|
2018-05-15 22:12:35 +02:00
|
|
|
popup->add_child(picker);
|
|
|
|
add_child(popup);
|
2020-02-21 18:28:45 +01:00
|
|
|
picker->connect("color_changed", callable_mp(this, &ColorPickerButton::_color_changed));
|
|
|
|
popup->connect("modal_closed", callable_mp(this, &ColorPickerButton::_modal_closed));
|
2020-03-12 13:37:40 +01:00
|
|
|
popup->connect("about_to_popup", callable_mp((BaseButton *)this, &BaseButton::set_pressed), varray(true));
|
2020-02-21 23:26:13 +01:00
|
|
|
popup->connect("popup_hide", callable_mp((BaseButton *)this, &BaseButton::set_pressed), varray(false));
|
2018-05-15 22:12:35 +02:00
|
|
|
picker->set_pick_color(color);
|
|
|
|
picker->set_edit_alpha(edit_alpha);
|
2019-08-30 22:25:15 +02:00
|
|
|
emit_signal("picker_created");
|
2018-05-15 22:12:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPickerButton::_bind_methods() {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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 13:19:41 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_picker"), &ColorPickerButton::get_picker);
|
2017-12-01 21:58:53 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_popup"), &ColorPickerButton::get_popup);
|
2017-03-05 16:44:50 +01: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);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
|
2018-04-12 23:47:53 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("popup_closed"));
|
2019-08-30 22:25:15 +02:00
|
|
|
ADD_SIGNAL(MethodInfo("picker_created"));
|
2017-03-05 16:44:50 +01: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 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ColorPickerButton::ColorPickerButton() {
|
|
|
|
|
2019-01-26 22:07:32 +01:00
|
|
|
// Initialization is now done deferred,
|
|
|
|
// this improves performance in the inspector as the color picker
|
|
|
|
// can be expensive to initialize.
|
2020-04-02 01:20:12 +02:00
|
|
|
picker = nullptr;
|
|
|
|
popup = nullptr;
|
2018-05-15 22:12:35 +02:00
|
|
|
edit_alpha = true;
|
2018-09-21 07:18:40 +02:00
|
|
|
|
|
|
|
set_toggle_mode(true);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|