2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* color_picker.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "color_picker.h"
|
|
|
|
|
2015-12-24 03:17:09 +01:00
|
|
|
#include "os/input.h"
|
|
|
|
#include "os/keyboard.h"
|
2017-03-05 16:44:50 +01:00
|
|
|
#include "os/os.h"
|
|
|
|
#include "scene/gui/separator.h"
|
|
|
|
#include "scene/main/viewport.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: {
|
2016-10-20 00:43:49 +02:00
|
|
|
//sample->set_texture(get_icon("color_sample"));
|
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: {
|
|
|
|
btn_pick->set_icon(get_icon("screen_picker", "ColorPicker"));
|
2016-02-02 01:32:47 +01:00
|
|
|
|
|
|
|
_update_color();
|
2015-11-21 06:51:42 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 15:55:35 +02:00
|
|
|
void ColorPicker::set_focus_on_line_edit() {
|
|
|
|
|
|
|
|
c_text->grab_focus();
|
|
|
|
c_text->select();
|
|
|
|
}
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void ColorPicker::_update_controls() {
|
|
|
|
|
|
|
|
if (edit_alpha) {
|
|
|
|
values[3]->show();
|
|
|
|
scroll[3]->show();
|
|
|
|
labels[3]->show();
|
|
|
|
} else {
|
|
|
|
values[3]->hide();
|
|
|
|
scroll[3]->hide();
|
|
|
|
labels[3]->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPicker::set_pick_color(const Color &p_color) {
|
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;
|
|
|
|
|
2017-01-05 23:41:36 +01:00
|
|
|
return; //it crashes, so returning
|
2015-12-25 21:59:23 +01:00
|
|
|
uv_edit->get_child(0)->cast_to<Control>()->update();
|
|
|
|
w_edit->get_child(0)->cast_to<Control>()->update();
|
2014-02-10 02:10:30 +01:00
|
|
|
_update_color();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-14 15:07:57 +01:00
|
|
|
set_pick_color(color);
|
2015-12-24 03:17:09 +01:00
|
|
|
|
2016-07-03 03:08:25 +02:00
|
|
|
_update_text_value();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
if (updating)
|
|
|
|
return;
|
|
|
|
|
|
|
|
color = Color::html(p_html);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::_update_color() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
updating = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2015-11-21 06:51:42 +01:00
|
|
|
scroll[i]->set_max(255);
|
|
|
|
scroll[i]->set_step(0.01);
|
2016-07-03 03:08:25 +02:00
|
|
|
if (raw_mode_enabled) {
|
|
|
|
if (i == 3)
|
|
|
|
scroll[i]->set_max(1);
|
2017-01-04 05:16:14 +01:00
|
|
|
scroll[i]->set_value(color.components[i]);
|
2016-07-03 03:08:25 +02:00
|
|
|
} else {
|
2017-01-04 05:16:14 +01:00
|
|
|
scroll[i]->set_value(color.components[i] * 255);
|
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-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() {
|
|
|
|
Size2 size = bt_add_preset->get_size();
|
|
|
|
preset->set_custom_minimum_size(Size2(size.width * presets.size(), size.height));
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<uint8_t> img;
|
2017-03-05 16:44:50 +01:00
|
|
|
img.resize(size.x * presets.size() * size.y * 3);
|
2016-10-03 21:33:42 +02:00
|
|
|
|
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
PoolVector<uint8_t>::Write w = img.write();
|
|
|
|
for (int y = 0; y < size.y; y++) {
|
|
|
|
for (int x = 0; x < size.x * presets.size(); x++) {
|
|
|
|
int ofs = (y * (size.x * presets.size()) + x) * 3;
|
|
|
|
w[ofs + 0] = uint8_t(CLAMP(presets[(int)x / size.x].r * 255.0, 0, 255));
|
|
|
|
w[ofs + 1] = uint8_t(CLAMP(presets[(int)x / size.x].g * 255.0, 0, 255));
|
|
|
|
w[ofs + 2] = uint8_t(CLAMP(presets[(int)x / size.x].b * 255.0, 0, 255));
|
2016-10-03 21:33:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Image i(size.x * presets.size(), size.y, false, Image::FORMAT_RGB8, img);
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2015-12-24 03:17:09 +01:00
|
|
|
Ref<ImageTexture> t;
|
|
|
|
t.instance();
|
|
|
|
t->create_from_image(i);
|
|
|
|
preset->set_texture(t);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPicker::_text_type_toggled() {
|
2016-01-26 23:10:56 +01:00
|
|
|
if (!get_tree()->is_editor_hint())
|
|
|
|
return;
|
|
|
|
text_is_constructor = !text_is_constructor;
|
|
|
|
if (text_is_constructor) {
|
|
|
|
text_type->set_text("");
|
|
|
|
text_type->set_icon(get_icon("Script", "EditorIcons"));
|
|
|
|
} else {
|
|
|
|
text_type->set_text("#");
|
|
|
|
text_type->set_icon(NULL);
|
|
|
|
}
|
|
|
|
_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) {
|
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);
|
|
|
|
}
|
|
|
|
_update_presets();
|
2017-03-05 16:44:50 +01:00
|
|
|
if (presets.size() == 10)
|
2015-12-24 03:17:09 +01:00
|
|
|
bt_add_preset->hide();
|
|
|
|
}
|
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
void ColorPicker::set_raw_mode(bool p_enabled) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (raw_mode_enabled == p_enabled)
|
2015-11-21 06:51:42 +01:00
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
raw_mode_enabled = p_enabled;
|
|
|
|
if (btn_mode->is_pressed() != p_enabled)
|
2015-11-21 06:51:42 +01:00
|
|
|
btn_mode->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
|
|
|
}
|
|
|
|
|
2016-07-03 03:08:25 +02:00
|
|
|
void ColorPicker::_update_text_value() {
|
|
|
|
if (text_is_constructor) {
|
2017-03-05 16:44:50 +01:00
|
|
|
String t = "Color(" + String::num(color.r) + "," + String::num(color.g) + "," + String::num(color.b);
|
|
|
|
if (edit_alpha && color.a < 1)
|
|
|
|
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);
|
|
|
|
} else {
|
2017-03-05 16:44:50 +01:00
|
|
|
c_text->set_text(color.to_html(edit_alpha && color.a < 1));
|
2016-07-03 03:08:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
void ColorPicker::_sample_draw() {
|
2017-03-05 16:44:50 +01:00
|
|
|
sample->draw_rect(Rect2(Point2(), Size2(256, 20)), color);
|
2015-11-21 06:51:42 +01:00
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPicker::_hsv_draw(int p_wich, Control *c) {
|
2015-12-25 21:59:23 +01:00
|
|
|
if (!c)
|
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_wich == 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-03-05 16:44:50 +01:00
|
|
|
colors.push_back(Color(1, 1, 1));
|
|
|
|
colors.push_back(Color(1, 1, 1));
|
2016-10-20 00:43:49 +02:00
|
|
|
colors.push_back(Color());
|
|
|
|
colors.push_back(Color());
|
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-03-05 16:44:50 +01:00
|
|
|
col.set_hsv(color.get_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-03-05 16:44:50 +01:00
|
|
|
col.set_hsv(color.get_h(), 1, 0);
|
2016-10-20 00:43:49 +02:00
|
|
|
colors2.push_back(col);
|
|
|
|
col.a = 0;
|
|
|
|
colors2.push_back(col);
|
2017-03-05 16:44:50 +01:00
|
|
|
c->draw_polygon(points, colors);
|
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);
|
|
|
|
} else if (p_wich == 1) {
|
|
|
|
Ref<Texture> hue = get_icon("color_hue", "ColorPicker");
|
|
|
|
c->draw_texture_rect(hue, Rect2(Point2(), c->get_size()));
|
|
|
|
int y = c->get_size().y - c->get_size().y * h;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 03:17:09 +01:00
|
|
|
void ColorPicker::_uv_input(const InputEvent &ev) {
|
2015-11-21 06:51:42 +01:00
|
|
|
if (ev.type == InputEvent::MOUSE_BUTTON) {
|
|
|
|
const InputEventMouseButton &bev = ev.mouse_button;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (bev.pressed && bev.button_index == BUTTON_LEFT) {
|
2015-11-21 06:51:42 +01:00
|
|
|
changing_color = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
float x = CLAMP((float)bev.x, 0, 256);
|
|
|
|
float y = CLAMP((float)bev.y, 0, 256);
|
|
|
|
s = x / 256;
|
|
|
|
v = 1.0 - y / 256.0;
|
|
|
|
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();
|
|
|
|
emit_signal("color_changed", color);
|
2015-11-21 06:51:42 +01:00
|
|
|
} else {
|
|
|
|
changing_color = false;
|
|
|
|
}
|
|
|
|
} else if (ev.type == InputEvent::MOUSE_MOTION) {
|
|
|
|
const InputEventMouse &bev = ev.mouse_motion;
|
|
|
|
if (!changing_color)
|
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
float x = CLAMP((float)bev.x, 0, 256);
|
|
|
|
float y = CLAMP((float)bev.y, 0, 256);
|
|
|
|
s = x / 256;
|
|
|
|
v = 1.0 - y / 256.0;
|
|
|
|
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();
|
|
|
|
emit_signal("color_changed", color);
|
2015-11-21 06:51:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 03:17:09 +01:00
|
|
|
void ColorPicker::_w_input(const InputEvent &ev) {
|
2015-11-21 06:51:42 +01:00
|
|
|
if (ev.type == InputEvent::MOUSE_BUTTON) {
|
|
|
|
const InputEventMouseButton &bev = ev.mouse_button;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (bev.pressed && bev.button_index == BUTTON_LEFT) {
|
2015-11-21 06:51:42 +01:00
|
|
|
changing_color = true;
|
2017-03-05 16:44:50 +01:00
|
|
|
h = 1 - ((float)bev.y) / 256.0;
|
2016-03-09 00:00:52 +01:00
|
|
|
|
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();
|
|
|
|
emit_signal("color_changed", color);
|
2015-11-21 06:51:42 +01:00
|
|
|
} else if (ev.type == InputEvent::MOUSE_MOTION) {
|
|
|
|
const InputEventMouse &bev = ev.mouse_motion;
|
|
|
|
if (!changing_color)
|
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
float y = CLAMP((float)bev.y, 0, 256);
|
|
|
|
h = 1.0 - y / 256.0;
|
|
|
|
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();
|
|
|
|
emit_signal("color_changed", color);
|
2015-11-21 06:51:42 +01:00
|
|
|
}
|
2015-12-24 03:17:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPicker::_preset_input(const InputEvent &ev) {
|
|
|
|
if (ev.type == InputEvent::MOUSE_BUTTON) {
|
|
|
|
const InputEventMouseButton &bev = ev.mouse_button;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (bev.pressed && bev.button_index == BUTTON_LEFT) {
|
|
|
|
int index = bev.x / (preset->get_size().x / presets.size());
|
2017-01-14 15:07:57 +01:00
|
|
|
set_pick_color(presets[index]);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (bev.pressed && bev.button_index == BUTTON_RIGHT) {
|
|
|
|
int index = bev.x / (preset->get_size().x / presets.size());
|
2015-12-24 03:17:09 +01:00
|
|
|
presets.erase(presets[index]);
|
|
|
|
_update_presets();
|
|
|
|
bt_add_preset->show();
|
|
|
|
}
|
|
|
|
_update_color();
|
|
|
|
emit_signal("color_changed", color);
|
|
|
|
} else if (ev.type == InputEvent::MOUSE_MOTION) {
|
2015-12-25 15:08:02 +01:00
|
|
|
const InputEventMouse &mev = ev.mouse_motion;
|
2016-03-01 00:08:33 +01:00
|
|
|
int index = mev.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;
|
2017-03-05 16:44:50 +01:00
|
|
|
preset->set_tooltip("Color: #" + presets[index].to_html(presets[index].a < 1) + "\n"
|
|
|
|
"LMB: Set color\n"
|
|
|
|
"RMB: Remove preset");
|
2015-12-24 03:17:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void ColorPicker::_screen_input(const InputEvent &ev) {
|
|
|
|
if (ev.type == InputEvent::MOUSE_BUTTON) {
|
2015-12-25 15:08:02 +01:00
|
|
|
const InputEventMouseButton &bev = ev.mouse_button;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (bev.button_index == BUTTON_LEFT && !bev.pressed) {
|
2015-12-25 15:08:02 +01:00
|
|
|
emit_signal("color_changed", color);
|
|
|
|
screen->hide();
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (ev.type == InputEvent::MOUSE_MOTION) {
|
2015-12-25 15:08:02 +01:00
|
|
|
const InputEventMouse &mev = ev.mouse_motion;
|
2017-03-05 16:44:50 +01:00
|
|
|
Viewport *r = get_tree()->get_root();
|
|
|
|
if (!r->get_visible_rect().has_point(Point2(mev.global_x, mev.global_y)))
|
2015-12-25 15:08:02 +01:00
|
|
|
return;
|
2017-03-05 16:44:50 +01:00
|
|
|
Image img = r->get_screen_capture();
|
2016-07-26 15:04:54 +02:00
|
|
|
if (!img.empty()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
last_capture = img;
|
2015-12-25 15:08:02 +01:00
|
|
|
r->queue_screen_capture();
|
2016-07-26 15:04:54 +02:00
|
|
|
}
|
2016-10-03 21:33:42 +02:00
|
|
|
if (!last_capture.empty()) {
|
2017-03-05 16:44:50 +01:00
|
|
|
int pw = last_capture.get_format() == Image::FORMAT_RGBA8 ? 4 : 3;
|
|
|
|
int ofs = (mev.global_y * last_capture.get_width() + mev.global_x) * pw;
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2017-01-07 22:25:37 +01:00
|
|
|
PoolVector<uint8_t>::Read r = last_capture.get_data().read();
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Color c(r[ofs + 0] / 255.0, r[ofs + 1] / 255.0, r[ofs + 2] / 255.0);
|
2016-10-03 21:33:42 +02: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);
|
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);
|
2015-12-25 15:08:02 +01:00
|
|
|
screen->set_area_as_parent_rect();
|
2017-03-05 16:44:50 +01:00
|
|
|
screen->connect("gui_input", this, "_screen_input");
|
2015-12-25 15:08:02 +01:00
|
|
|
}
|
|
|
|
screen->raise();
|
2016-07-23 01:35:12 +02:00
|
|
|
screen->show_modal();
|
2015-12-25 15:08:02 +01:00
|
|
|
r->queue_screen_capture();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_raw_mode", "mode"), &ColorPicker::set_raw_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_raw_mode"), &ColorPicker::is_raw_mode);
|
|
|
|
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);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("add_preset"), &ColorPicker::add_preset);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_value_changed"), &ColorPicker::_value_changed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_html_entered"), &ColorPicker::_html_entered);
|
|
|
|
ClassDB::bind_method(D_METHOD("_text_type_toggled"), &ColorPicker::_text_type_toggled);
|
2017-02-13 12:47:24 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_add_preset_pressed"), &ColorPicker::_add_preset_pressed);
|
|
|
|
ClassDB::bind_method(D_METHOD("_screen_pick_pressed"), &ColorPicker::_screen_pick_pressed);
|
2017-03-05 16:44:50 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("_sample_draw"), &ColorPicker::_sample_draw);
|
|
|
|
ClassDB::bind_method(D_METHOD("_hsv_draw"), &ColorPicker::_hsv_draw);
|
|
|
|
ClassDB::bind_method(D_METHOD("_uv_input"), &ColorPicker::_uv_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("_w_input"), &ColorPicker::_w_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("_preset_input"), &ColorPicker::_preset_input);
|
|
|
|
ClassDB::bind_method(D_METHOD("_screen_input"), &ColorPicker::_screen_input);
|
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")));
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +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;
|
2017-03-05 16:44:50 +01:00
|
|
|
raw_mode_enabled = false;
|
|
|
|
changing_color = false;
|
|
|
|
screen = NULL;
|
2015-11-21 06:51:42 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *hb_smpl = memnew(HBoxContainer);
|
|
|
|
btn_pick = memnew(ToolButton);
|
|
|
|
btn_pick->connect("pressed", this, "_screen_pick_pressed");
|
2015-12-25 15:08:02 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
sample = memnew(TextureRect);
|
2015-11-21 06:51:42 +01:00
|
|
|
sample->set_h_size_flags(SIZE_EXPAND_FILL);
|
2017-03-05 16:44:50 +01:00
|
|
|
sample->connect("draw", this, "_sample_draw");
|
2015-11-21 06:51:42 +01:00
|
|
|
|
|
|
|
hb_smpl->add_child(sample);
|
|
|
|
hb_smpl->add_child(btn_pick);
|
|
|
|
add_child(hb_smpl);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *hb_edit = memnew(HBoxContainer);
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uv_edit = memnew(Control);
|
2017-01-08 23:54:19 +01:00
|
|
|
|
2017-01-08 20:28:12 +01:00
|
|
|
uv_edit->connect("gui_input", this, "_uv_input");
|
2017-01-08 23:54:19 +01:00
|
|
|
uv_edit->set_mouse_filter(MOUSE_FILTER_PASS);
|
2017-03-05 16:44:50 +01:00
|
|
|
uv_edit->set_custom_minimum_size(Size2(256, 256));
|
|
|
|
Vector<Variant> args = Vector<Variant>();
|
2015-12-25 21:59:23 +01:00
|
|
|
args.push_back(0);
|
2016-10-20 00:43:49 +02:00
|
|
|
args.push_back(uv_edit);
|
2017-03-05 16:44:50 +01:00
|
|
|
uv_edit->connect("draw", this, "_hsv_draw", args);
|
2015-12-24 03:17:09 +01:00
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
add_child(hb_edit);
|
2016-10-03 21:33:42 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
w_edit = memnew(Control);
|
2017-01-08 23:54:19 +01:00
|
|
|
//w_edit->set_ignore_mouse(false);
|
2017-03-05 16:44:50 +01:00
|
|
|
w_edit->set_custom_minimum_size(Size2(30, 256));
|
2017-01-08 20:28:12 +01:00
|
|
|
w_edit->connect("gui_input", this, "_w_input");
|
2015-12-25 21:59:23 +01:00
|
|
|
args.clear();
|
|
|
|
args.push_back(1);
|
2016-10-20 00:43:49 +02:00
|
|
|
args.push_back(w_edit);
|
2017-03-05 16:44:50 +01:00
|
|
|
w_edit->connect("draw", this, "_hsv_draw", args);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2015-11-21 06:51:42 +01:00
|
|
|
hb_edit->add_child(uv_edit);
|
2017-03-05 16:44:50 +01:00
|
|
|
hb_edit->add_child(memnew(VSeparator));
|
2015-11-21 06:51:42 +01:00
|
|
|
hb_edit->add_child(w_edit);
|
2015-12-24 03:17:09 +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
|
|
|
const char *lt[4] = { "R", "G", "B", "A" };
|
2014-02-10 02:10:30 +01:00
|
|
|
|
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
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
labels[i] = memnew(Label(lt[i]));
|
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);
|
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]);
|
|
|
|
|
|
|
|
scroll[i]->set_min(0);
|
|
|
|
scroll[i]->set_page(0);
|
|
|
|
scroll[i]->set_h_size_flags(SIZE_EXPAND_FILL);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
scroll[i]->connect("value_changed", this, "_value_changed");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
vbr->add_child(hbc);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *hhb = memnew(HBoxContainer);
|
2016-03-09 00:00:52 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
btn_mode = memnew(CheckButton);
|
2015-11-21 06:51:42 +01:00
|
|
|
btn_mode->set_text("RAW Mode");
|
|
|
|
btn_mode->connect("toggled", this, "set_raw_mode");
|
|
|
|
hhb->add_child(btn_mode);
|
2014-02-10 02:10:30 +01:00
|
|
|
vbr->add_child(hhb);
|
2017-03-05 16:44:50 +01:00
|
|
|
text_type = memnew(Button);
|
2016-01-26 23:10:56 +01:00
|
|
|
text_type->set_flat(true);
|
|
|
|
text_type->connect("pressed", this, "_text_type_toggled");
|
|
|
|
hhb->add_child(text_type);
|
|
|
|
|
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);
|
2017-03-05 16:44:50 +01:00
|
|
|
c_text->connect("text_entered", this, "_html_entered");
|
2016-01-26 23:10:56 +01:00
|
|
|
text_type->set_text("#");
|
|
|
|
c_text->set_h_size_flags(SIZE_EXPAND_FILL);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_update_controls();
|
2016-02-02 01:32:47 +01:00
|
|
|
//_update_color();
|
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
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
HBoxContainer *bbc = memnew(HBoxContainer);
|
2015-12-24 03:17:09 +01:00
|
|
|
add_child(bbc);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
preset = memnew(TextureRect);
|
2015-12-24 03:17:09 +01:00
|
|
|
bbc->add_child(preset);
|
2017-01-08 23:54:19 +01:00
|
|
|
//preset->set_ignore_mouse(false);
|
2017-01-08 20:28:12 +01:00
|
|
|
preset->connect("gui_input", this, "_preset_input");
|
2015-12-24 03:17:09 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bt_add_preset = memnew(Button);
|
2015-12-24 03:17:09 +01:00
|
|
|
bt_add_preset->set_icon(get_icon("add_preset"));
|
|
|
|
bt_add_preset->set_tooltip("Add current color as a preset");
|
2015-12-25 15:08:02 +01:00
|
|
|
bt_add_preset->connect("pressed", this, "_add_preset_pressed");
|
2015-12-24 03:17:09 +01:00
|
|
|
bbc->add_child(bt_add_preset);
|
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
|
|
|
|
|
|
|
update();
|
2017-03-05 16:44:50 +01:00
|
|
|
emit_signal("color_changed", p_color);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPickerButton::pressed() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Size2 ms = Size2(300, picker->get_combined_minimum_size().height + 10);
|
2017-03-29 17:29:38 +02:00
|
|
|
popup->set_position(get_global_position() - Size2(0, ms.height));
|
2014-02-10 02:10:30 +01:00
|
|
|
popup->set_size(ms);
|
|
|
|
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) {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_what == NOTIFICATION_DRAW) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Ref<StyleBox> normal = get_stylebox("normal");
|
|
|
|
draw_rect(Rect2(normal->get_offset(), get_size() - normal->get_minimum_size()), picker->get_pick_color());
|
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
|
|
|
|
2017-01-14 15:07:57 +01:00
|
|
|
picker->set_pick_color(p_color);
|
2015-03-22 16:52:47 +01:00
|
|
|
update();
|
2017-03-05 16:44:50 +01:00
|
|
|
emit_signal("color_changed", p_color);
|
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
|
|
|
|
2017-01-14 15:07:57 +01:00
|
|
|
return picker->get_pick_color();
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ColorPickerButton::set_edit_alpha(bool p_show) {
|
|
|
|
|
|
|
|
picker->set_edit_alpha(p_show);
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool ColorPickerButton::is_editing_alpha() const {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
return picker->is_editing_alpha();
|
|
|
|
}
|
|
|
|
|
2016-06-19 07:08:22 +02:00
|
|
|
ColorPicker *ColorPickerButton::get_picker() {
|
|
|
|
return picker;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_picker:ColorPicker"), &ColorPickerButton::get_picker);
|
|
|
|
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);
|
|
|
|
ClassDB::bind_method(D_METHOD("_color_changed"), &ColorPickerButton::_color_changed);
|
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")));
|
|
|
|
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() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
popup = memnew(PopupPanel);
|
|
|
|
picker = memnew(ColorPicker);
|
2014-02-10 02:10:30 +01:00
|
|
|
popup->add_child(picker);
|
2017-01-10 05:49:55 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
picker->connect("color_changed", this, "_color_changed");
|
2014-02-10 02:10:30 +01:00
|
|
|
add_child(popup);
|
|
|
|
}
|