2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2022-08-31 20:45:04 +00:00
|
|
|
/* gradient_editor.cpp */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +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). */
|
2016-06-18 12:46:12 +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
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
#include "gradient_editor.h"
|
2019-06-12 20:35:35 +00:00
|
|
|
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/keyboard.h"
|
2022-08-31 20:45:04 +00:00
|
|
|
#include "editor/editor_node.h"
|
|
|
|
#include "editor/editor_scale.h"
|
|
|
|
#include "editor/editor_undo_redo_manager.h"
|
|
|
|
|
|
|
|
void GradientEditor::set_gradient(const Ref<Gradient> &p_gradient) {
|
|
|
|
gradient = p_gradient;
|
|
|
|
connect("ramp_changed", callable_mp(this, &GradientEditor::_ramp_changed));
|
|
|
|
gradient->connect("changed", callable_mp(this, &GradientEditor::_gradient_changed));
|
|
|
|
set_points(gradient->get_points());
|
|
|
|
set_interpolation_mode(gradient->get_interpolation_mode());
|
|
|
|
}
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
void GradientEditor::reverse_gradient() {
|
|
|
|
gradient->reverse();
|
|
|
|
set_points(gradient->get_points());
|
|
|
|
emit_signal(SNAME("ramp_changed"));
|
|
|
|
queue_redraw();
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
int GradientEditor::_get_point_from_pos(int x) {
|
2015-05-24 18:18:52 +00:00
|
|
|
int result = -1;
|
2021-10-02 20:07:42 +00:00
|
|
|
int total_w = get_size().width - get_size().height - draw_spacing;
|
2018-09-07 16:49:10 +00:00
|
|
|
float min_distance = 1e20;
|
2015-05-24 18:18:52 +00:00
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2021-11-08 18:11:36 +00:00
|
|
|
// Check if we clicked at point.
|
2018-09-07 16:49:10 +00:00
|
|
|
float distance = ABS(x - points[i].offset * total_w);
|
2021-10-02 20:07:42 +00:00
|
|
|
float min = (draw_point_width / 2 * 1.7); //make it easier to grab
|
2018-09-07 16:49:10 +00:00
|
|
|
if (distance <= min && distance < min_distance) {
|
2015-05-24 18:18:52 +00:00
|
|
|
result = i;
|
2018-09-07 16:49:10 +00:00
|
|
|
min_distance = distance;
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
void GradientEditor::_show_color_picker() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (grabbed == -1) {
|
2015-05-24 18:18:52 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-01-14 14:07:57 +00:00
|
|
|
picker->set_pick_color(points[grabbed].color);
|
2020-03-12 12:37:40 +00:00
|
|
|
Size2 minsize = popup->get_contents_minimum_size();
|
2018-09-07 16:49:10 +00:00
|
|
|
bool show_above = false;
|
|
|
|
if (get_global_position().y + get_size().y + minsize.y > get_viewport_rect().size.y) {
|
|
|
|
show_above = true;
|
|
|
|
}
|
|
|
|
if (show_above) {
|
2020-03-12 12:37:40 +00:00
|
|
|
popup->set_position(get_screen_position() - Vector2(0, minsize.y));
|
2018-09-07 16:49:10 +00:00
|
|
|
} else {
|
2020-03-12 12:37:40 +00:00
|
|
|
popup->set_position(get_screen_position() + Vector2(0, get_size().y));
|
2018-09-07 16:49:10 +00:00
|
|
|
}
|
2015-05-24 18:18:52 +00:00
|
|
|
popup->popup();
|
|
|
|
}
|
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
void GradientEditor::_gradient_changed() {
|
|
|
|
if (editing) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
editing = true;
|
2022-09-29 09:53:28 +00:00
|
|
|
Vector<Gradient::Point> grad_points = gradient->get_points();
|
|
|
|
set_points(grad_points);
|
2022-08-31 20:45:04 +00:00
|
|
|
set_interpolation_mode(gradient->get_interpolation_mode());
|
|
|
|
queue_redraw();
|
|
|
|
editing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientEditor::_ramp_changed() {
|
|
|
|
editing = true;
|
|
|
|
Ref<EditorUndoRedoManager> undo_redo = EditorNode::get_undo_redo();
|
|
|
|
undo_redo->create_action(TTR("Gradient Edited"), UndoRedo::MERGE_ENDS);
|
|
|
|
undo_redo->add_do_method(gradient.ptr(), "set_offsets", get_offsets());
|
|
|
|
undo_redo->add_do_method(gradient.ptr(), "set_colors", get_colors());
|
|
|
|
undo_redo->add_do_method(gradient.ptr(), "set_interpolation_mode", get_interpolation_mode());
|
|
|
|
undo_redo->add_undo_method(gradient.ptr(), "set_offsets", gradient->get_offsets());
|
|
|
|
undo_redo->add_undo_method(gradient.ptr(), "set_colors", gradient->get_colors());
|
|
|
|
undo_redo->add_undo_method(gradient.ptr(), "set_interpolation_mode", gradient->get_interpolation_mode());
|
|
|
|
undo_redo->commit_action();
|
|
|
|
editing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientEditor::_color_changed(const Color &p_color) {
|
|
|
|
if (grabbed == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
points.write[grabbed].color = p_color;
|
|
|
|
queue_redraw();
|
|
|
|
emit_signal(SNAME("ramp_changed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientEditor::set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors) {
|
|
|
|
ERR_FAIL_COND(p_offsets.size() != p_colors.size());
|
|
|
|
points.clear();
|
|
|
|
for (int i = 0; i < p_offsets.size(); i++) {
|
|
|
|
Gradient::Point p;
|
|
|
|
p.offset = p_offsets[i];
|
|
|
|
p.color = p_colors[i];
|
|
|
|
points.push_back(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
points.sort();
|
|
|
|
queue_redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<float> GradientEditor::get_offsets() const {
|
|
|
|
Vector<float> ret;
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
ret.push_back(points[i].offset);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Color> GradientEditor::get_colors() const {
|
|
|
|
Vector<Color> ret;
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
ret.push_back(points[i].color);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientEditor::set_points(Vector<Gradient::Point> &p_points) {
|
|
|
|
if (points.size() != p_points.size()) {
|
|
|
|
grabbed = -1;
|
|
|
|
}
|
|
|
|
points.clear();
|
|
|
|
points = p_points;
|
|
|
|
points.sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Gradient::Point> &GradientEditor::get_points() {
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientEditor::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) {
|
|
|
|
interpolation_mode = p_interp_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Gradient::InterpolationMode GradientEditor::get_interpolation_mode() {
|
|
|
|
return interpolation_mode;
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
ColorPicker *GradientEditor::get_picker() {
|
|
|
|
return picker;
|
|
|
|
}
|
|
|
|
|
|
|
|
PopupPanel *GradientEditor::get_popup() {
|
|
|
|
return popup;
|
|
|
|
}
|
|
|
|
|
|
|
|
Size2 GradientEditor::get_minimum_size() const {
|
|
|
|
return Size2(0, 60) * EDSCALE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GradientEditor::gui_input(const Ref<InputEvent> &p_event) {
|
2021-04-05 06:52:21 +00:00
|
|
|
ERR_FAIL_COND(p_event.is_null());
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventKey> k = p_event;
|
|
|
|
|
2021-08-13 21:31:57 +00:00
|
|
|
if (k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && grabbed != -1) {
|
2021-07-03 22:17:03 +00:00
|
|
|
points.remove_at(grabbed);
|
2015-05-24 18:18:52 +00:00
|
|
|
grabbed = -1;
|
2015-06-11 03:18:13 +00:00
|
|
|
grabbing = false;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("ramp_changed"));
|
2015-05-24 18:18:52 +00:00
|
|
|
accept_event();
|
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseButton> mb = p_event;
|
2021-11-08 18:11:36 +00:00
|
|
|
// Show color picker on double click.
|
2021-08-13 21:31:57 +00:00
|
|
|
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_double_click() && mb->is_pressed()) {
|
2017-06-03 08:54:24 +00:00
|
|
|
grabbed = _get_point_from_pos(mb->get_position().x);
|
2015-05-24 18:18:52 +00:00
|
|
|
_show_color_picker();
|
|
|
|
accept_event();
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:11:36 +00:00
|
|
|
// Delete point on right click.
|
2021-08-13 21:31:57 +00:00
|
|
|
if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
|
2017-06-03 08:54:24 +00:00
|
|
|
grabbed = _get_point_from_pos(mb->get_position().x);
|
2015-05-24 18:18:52 +00:00
|
|
|
if (grabbed != -1) {
|
2021-07-03 22:17:03 +00:00
|
|
|
points.remove_at(grabbed);
|
2015-05-24 18:18:52 +00:00
|
|
|
grabbed = -1;
|
2015-06-11 03:18:13 +00:00
|
|
|
grabbing = false;
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("ramp_changed"));
|
2015-05-24 18:18:52 +00:00
|
|
|
accept_event();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:11:36 +00:00
|
|
|
// Hold alt key to duplicate selected color.
|
2021-08-13 21:31:57 +00:00
|
|
|
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed() && mb->is_alt_pressed()) {
|
2017-06-03 08:54:24 +00:00
|
|
|
int x = mb->get_position().x;
|
2015-06-11 02:36:11 +00:00
|
|
|
grabbed = _get_point_from_pos(x);
|
|
|
|
|
|
|
|
if (grabbed != -1) {
|
2021-10-02 20:07:42 +00:00
|
|
|
int total_w = get_size().width - get_size().height - draw_spacing;
|
2021-11-08 18:11:36 +00:00
|
|
|
Gradient::Point new_point = points[grabbed];
|
|
|
|
new_point.offset = CLAMP(x / float(total_w), 0, 1);
|
2015-06-11 02:36:11 +00:00
|
|
|
|
2021-11-08 18:11:36 +00:00
|
|
|
points.push_back(new_point);
|
2015-06-11 02:36:11 +00:00
|
|
|
points.sort();
|
|
|
|
for (int i = 0; i < points.size(); ++i) {
|
2021-11-08 18:11:36 +00:00
|
|
|
if (points[i].offset == new_point.offset) {
|
2015-06-11 02:36:11 +00:00
|
|
|
grabbed = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("ramp_changed"));
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2015-06-11 02:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:11:36 +00:00
|
|
|
// Select.
|
2021-08-13 21:31:57 +00:00
|
|
|
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2017-06-03 08:54:24 +00:00
|
|
|
int x = mb->get_position().x;
|
2021-10-02 20:07:42 +00:00
|
|
|
int total_w = get_size().width - get_size().height - draw_spacing;
|
2015-05-24 18:18:52 +00:00
|
|
|
|
|
|
|
//Check if color selector was clicked.
|
2021-10-02 20:07:42 +00:00
|
|
|
if (x > total_w + draw_spacing) {
|
2015-05-24 18:18:52 +00:00
|
|
|
_show_color_picker();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
grabbing = true;
|
|
|
|
|
|
|
|
grabbed = _get_point_from_pos(x);
|
|
|
|
//grab or select
|
|
|
|
if (grabbed != -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:11:36 +00:00
|
|
|
// Insert point.
|
|
|
|
Gradient::Point new_point;
|
|
|
|
new_point.offset = CLAMP(x / float(total_w), 0, 1);
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2017-05-29 00:46:48 +00:00
|
|
|
Gradient::Point prev;
|
|
|
|
Gradient::Point next;
|
2015-05-24 18:18:52 +00:00
|
|
|
|
|
|
|
int pos = -1;
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2021-11-08 18:11:36 +00:00
|
|
|
if (points[i].offset < new_point.offset) {
|
2015-05-24 18:18:52 +00:00
|
|
|
pos = i;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pos == -1) {
|
|
|
|
prev.color = Color(0, 0, 0);
|
|
|
|
prev.offset = 0;
|
|
|
|
if (points.size()) {
|
|
|
|
next = points[0];
|
|
|
|
} else {
|
|
|
|
next.color = Color(1, 1, 1);
|
|
|
|
next.offset = 1.0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (pos == points.size() - 1) {
|
|
|
|
next.color = Color(1, 1, 1);
|
|
|
|
next.offset = 1.0;
|
|
|
|
} else {
|
|
|
|
next = points[pos + 1];
|
|
|
|
}
|
|
|
|
prev = points[pos];
|
|
|
|
}
|
|
|
|
|
2021-11-08 18:11:36 +00:00
|
|
|
new_point.color = prev.color.lerp(next.color, (new_point.offset - prev.offset) / (next.offset - prev.offset));
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2021-11-08 18:11:36 +00:00
|
|
|
points.push_back(new_point);
|
2015-05-24 18:18:52 +00:00
|
|
|
points.sort();
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2021-11-08 18:11:36 +00:00
|
|
|
if (points[i].offset == new_point.offset) {
|
2015-05-24 18:18:52 +00:00
|
|
|
grabbed = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("ramp_changed"));
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 21:31:57 +00:00
|
|
|
if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
|
2015-05-24 18:18:52 +00:00
|
|
|
if (grabbing) {
|
|
|
|
grabbing = false;
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("ramp_changed"));
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 15:38:03 +00:00
|
|
|
Ref<InputEventMouseMotion> mm = p_event;
|
|
|
|
|
|
|
|
if (mm.is_valid() && grabbing) {
|
2021-10-02 20:07:42 +00:00
|
|
|
int total_w = get_size().width - get_size().height - draw_spacing;
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2017-06-03 08:54:24 +00:00
|
|
|
int x = mm->get_position().x;
|
2017-05-20 15:38:03 +00:00
|
|
|
|
2015-05-24 18:18:52 +00:00
|
|
|
float newofs = CLAMP(x / float(total_w), 0, 1);
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2019-09-02 11:35:42 +00:00
|
|
|
// Snap to "round" coordinates if holding Ctrl.
|
2021-11-08 18:11:36 +00:00
|
|
|
// Be more precise if holding Shift as well.
|
2021-04-24 20:33:50 +00:00
|
|
|
if (mm->is_ctrl_pressed()) {
|
|
|
|
newofs = Math::snapped(newofs, mm->is_shift_pressed() ? 0.025 : 0.1);
|
|
|
|
} else if (mm->is_shift_pressed()) {
|
2019-09-02 11:35:42 +00:00
|
|
|
// Snap to nearest point if holding just Shift
|
|
|
|
const float snap_threshold = 0.03;
|
2018-10-22 02:08:16 +00:00
|
|
|
float smallest_ofs = snap_threshold;
|
2018-10-04 16:54:20 +00:00
|
|
|
bool found = false;
|
|
|
|
int nearest_point = 0;
|
2015-06-11 02:36:11 +00:00
|
|
|
for (int i = 0; i < points.size(); ++i) {
|
|
|
|
if (i != grabbed) {
|
|
|
|
float temp_ofs = ABS(points[i].offset - newofs);
|
|
|
|
if (temp_ofs < smallest_ofs) {
|
|
|
|
smallest_ofs = temp_ofs;
|
|
|
|
nearest_point = i;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (found) {
|
2015-06-11 02:36:11 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-10-04 16:54:20 +00:00
|
|
|
found = true;
|
2015-06-11 02:36:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 16:54:20 +00:00
|
|
|
if (found) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (points[nearest_point].offset < newofs) {
|
2015-06-11 02:36:11 +00:00
|
|
|
newofs = points[nearest_point].offset + 0.00001;
|
2020-05-14 14:41:43 +00:00
|
|
|
} else {
|
2015-06-11 02:36:11 +00:00
|
|
|
newofs = points[nearest_point].offset - 0.00001;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2015-06-11 02:36:11 +00:00
|
|
|
newofs = CLAMP(newofs, 0, 1);
|
|
|
|
}
|
|
|
|
}
|
2015-05-24 18:18:52 +00:00
|
|
|
|
|
|
|
bool valid = true;
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
if (points[i].offset == newofs && i != grabbed) {
|
|
|
|
valid = false;
|
2019-11-19 21:05:03 +00:00
|
|
|
break;
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 21:05:03 +00:00
|
|
|
if (!valid || grabbed == -1) {
|
2015-05-24 18:18:52 +00:00
|
|
|
return;
|
2019-11-19 21:05:03 +00:00
|
|
|
}
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[grabbed].offset = newofs;
|
2015-05-24 18:18:52 +00:00
|
|
|
|
|
|
|
points.sort();
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
if (points[i].offset == newofs) {
|
|
|
|
grabbed = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:22:52 +00:00
|
|
|
emit_signal(SNAME("ramp_changed"));
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-08-13 21:21:24 +00:00
|
|
|
queue_redraw();
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
void GradientEditor::_notification(int p_what) {
|
2022-02-15 17:06:48 +00:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
2022-08-31 20:45:04 +00:00
|
|
|
if (!picker->is_connected("color_changed", callable_mp(this, &GradientEditor::_color_changed))) {
|
|
|
|
picker->connect("color_changed", callable_mp(this, &GradientEditor::_color_changed));
|
2022-02-15 17:06:48 +00:00
|
|
|
}
|
2022-08-29 09:04:31 +00:00
|
|
|
[[fallthrough]];
|
|
|
|
}
|
2022-02-15 17:06:48 +00:00
|
|
|
case NOTIFICATION_THEME_CHANGED: {
|
|
|
|
draw_spacing = BASE_SPACING * get_theme_default_base_scale();
|
|
|
|
draw_point_width = BASE_POINT_WIDTH * get_theme_default_base_scale();
|
|
|
|
} break;
|
2021-10-02 20:07:42 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
case NOTIFICATION_DRAW: {
|
|
|
|
int w = get_size().x;
|
|
|
|
int h = get_size().y;
|
2021-10-02 20:07:42 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
if (w == 0 || h == 0) {
|
|
|
|
return; // Safety check. We have division by 'h'. And in any case there is nothing to draw with such size.
|
|
|
|
}
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
int total_w = get_size().width - get_size().height - draw_spacing;
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
// Draw checker pattern for ramp.
|
|
|
|
draw_texture_rect(get_theme_icon(SNAME("GuiMiniCheckerboard"), SNAME("EditorIcons")), Rect2(0, 0, total_w, h), true);
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
// Draw color ramp.
|
|
|
|
gradient_cache->set_points(points);
|
|
|
|
gradient_cache->set_interpolation_mode(interpolation_mode);
|
|
|
|
preview_texture->set_gradient(gradient_cache);
|
|
|
|
draw_texture_rect(preview_texture, Rect2(0, 0, total_w, h));
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
// Draw point markers.
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
Color col = points[i].color.inverted();
|
|
|
|
col.a = 0.9;
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
draw_line(Vector2(points[i].offset * total_w, 0), Vector2(points[i].offset * total_w, h / 2), col);
|
|
|
|
Rect2 rect = Rect2(points[i].offset * total_w - draw_point_width / 2, h / 2, draw_point_width, h / 2);
|
|
|
|
draw_rect(rect, points[i].color, true);
|
|
|
|
draw_rect(rect, col, false);
|
|
|
|
if (grabbed == i) {
|
|
|
|
rect = rect.grow(-1);
|
|
|
|
if (has_focus()) {
|
|
|
|
draw_rect(rect, Color(1, 0, 0, 0.9), false);
|
|
|
|
} else {
|
|
|
|
draw_rect(rect, Color(0.6, 0, 0, 0.9), false);
|
|
|
|
}
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
rect = rect.grow(-1);
|
|
|
|
draw_rect(rect, col, false);
|
2018-09-07 16:49:10 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
// Draw "button" for color selector.
|
|
|
|
draw_texture_rect(get_theme_icon(SNAME("GuiMiniCheckerboard"), SNAME("EditorIcons")), Rect2(total_w + draw_spacing, 0, h, h), true);
|
|
|
|
if (grabbed != -1) {
|
|
|
|
// Draw with selection color.
|
|
|
|
draw_rect(Rect2(total_w + draw_spacing, 0, h, h), points[grabbed].color);
|
|
|
|
} else {
|
|
|
|
// If no color selected draw grey color with 'X' on top.
|
|
|
|
draw_rect(Rect2(total_w + draw_spacing, 0, h, h), Color(0.5, 0.5, 0.5, 1));
|
|
|
|
draw_line(Vector2(total_w + draw_spacing, 0), Vector2(total_w + draw_spacing + h, h), Color(1, 1, 1, 0.6));
|
|
|
|
draw_line(Vector2(total_w + draw_spacing, h), Vector2(total_w + draw_spacing + h, 0), Color(1, 1, 1, 0.6));
|
|
|
|
}
|
2015-05-24 18:18:52 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
// Draw borders around color ramp if in focus.
|
|
|
|
if (has_focus()) {
|
|
|
|
draw_line(Vector2(-1, -1), Vector2(total_w + 1, -1), Color(1, 1, 1, 0.6));
|
|
|
|
draw_line(Vector2(total_w + 1, -1), Vector2(total_w + 1, h + 1), Color(1, 1, 1, 0.6));
|
|
|
|
draw_line(Vector2(total_w + 1, h + 1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
|
|
|
|
draw_line(Vector2(-1, -1), Vector2(-1, h + 1), Color(1, 1, 1, 0.6));
|
|
|
|
}
|
|
|
|
} break;
|
2018-02-12 21:22:45 +00:00
|
|
|
|
2022-02-15 17:06:48 +00:00
|
|
|
case NOTIFICATION_VISIBILITY_CHANGED: {
|
|
|
|
if (!is_visible()) {
|
|
|
|
grabbing = false;
|
|
|
|
}
|
|
|
|
} break;
|
2018-02-12 21:22:45 +00:00
|
|
|
}
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
void GradientEditor::_bind_methods() {
|
|
|
|
ADD_SIGNAL(MethodInfo("ramp_changed"));
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
GradientEditor::GradientEditor() {
|
|
|
|
set_focus_mode(FOCUS_ALL);
|
2021-11-08 18:11:36 +00:00
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
popup = memnew(PopupPanel);
|
|
|
|
picker = memnew(ColorPicker);
|
|
|
|
popup->add_child(picker);
|
|
|
|
popup->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(GradientEditor::get_picker()));
|
2021-11-08 18:11:36 +00:00
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
gradient_cache.instantiate();
|
|
|
|
preview_texture.instantiate();
|
2021-12-20 14:34:29 +00:00
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
preview_texture->set_width(1024);
|
|
|
|
add_child(popup, false, INTERNAL_MODE_FRONT);
|
2022-07-01 03:43:46 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 20:45:04 +00:00
|
|
|
GradientEditor::~GradientEditor() {
|
2015-05-24 18:18:52 +00:00
|
|
|
}
|