From 23c5fa86877ba90be8e6d4c06adc3dca18a8a784 Mon Sep 17 00:00:00 2001 From: Karroffel Date: Tue, 13 Jun 2017 23:47:34 +0200 Subject: renamed occurances of ColorRamp with Gradient ColorRamp got renamed to Gradient recently, reduz missed some occurances though. --- scene/2d/line_2d.cpp | 2 +- scene/2d/particles_2d.cpp | 48 ++--- scene/2d/particles_2d.h | 6 +- scene/gui/color_ramp_edit.cpp | 452 ------------------------------------------ scene/gui/color_ramp_edit.h | 80 -------- scene/gui/gradient_edit.cpp | 452 ++++++++++++++++++++++++++++++++++++++++++ scene/gui/gradient_edit.h | 80 ++++++++ 7 files changed, 560 insertions(+), 560 deletions(-) delete mode 100644 scene/gui/color_ramp_edit.cpp delete mode 100644 scene/gui/color_ramp_edit.h create mode 100644 scene/gui/gradient_edit.cpp create mode 100644 scene/gui/gradient_edit.h (limited to 'scene') diff --git a/scene/2d/line_2d.cpp b/scene/2d/line_2d.cpp index 80969d11b3..1a57d24342 100644 --- a/scene/2d/line_2d.cpp +++ b/scene/2d/line_2d.cpp @@ -310,7 +310,7 @@ void Line2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "points"), "set_points", "get_points"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "width"), "set_width", "get_width"); ADD_PROPERTY(PropertyInfo(Variant::COLOR, "default_color"), "set_default_color", "get_default_color"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "ColorRamp"), "set_gradient", "get_gradient"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "gradient", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_gradient", "get_gradient"); ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture"); ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "texture_mode", PROPERTY_HINT_ENUM, "None,Tile"), "set_texture_mode", "get_texture_mode"); ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "joint_mode", PROPERTY_HINT_ENUM, "Sharp,Bevel,Round"), "set_joint_mode", "get_joint_mode"); diff --git a/scene/2d/particles_2d.cpp b/scene/2d/particles_2d.cpp index 27d0d07c0f..21d64c5d64 100644 --- a/scene/2d/particles_2d.cpp +++ b/scene/2d/particles_2d.cpp @@ -505,8 +505,8 @@ void Particles2D::_notification(int p_what) { Color color; - if (color_ramp.is_valid()) { - color = color_ramp->get_color_at_offset(ptime); + if (gradient.is_valid()) { + color = gradient->get_color_at_offset(ptime); } else { color = default_color; } @@ -774,14 +774,14 @@ Color Particles2D::get_color() const { return default_color; } -void Particles2D::set_color_ramp(const Ref &p_color_ramp) { +void Particles2D::set_gradient(const Ref &p_gradient) { - color_ramp = p_color_ramp; + gradient = p_gradient; } -Ref Particles2D::get_color_ramp() const { +Ref Particles2D::get_gradient() const { - return color_ramp; + return gradient; } void Particles2D::set_emissor_offset(const Point2 &p_offset) { @@ -809,19 +809,19 @@ void Particles2D::set_color_phases(int p_phases) { //Create color ramp if we have 2 or more phases. //Otherwise first phase phase will be assigned to default color. - if (p_phases > 1 && color_ramp.is_null()) { - color_ramp = Ref(memnew(Gradient())); + if (p_phases > 1 && gradient.is_null()) { + gradient = Ref(memnew(Gradient())); } - if (color_ramp.is_valid()) { - color_ramp->get_points().resize(p_phases); + if (gradient.is_valid()) { + gradient->get_points().resize(p_phases); } } //Deprecated. int Particles2D::get_color_phases() const { - if (color_ramp.is_valid()) { - return color_ramp->get_points_count(); + if (gradient.is_valid()) { + return gradient->get_points_count(); } return 0; } @@ -830,9 +830,9 @@ int Particles2D::get_color_phases() const { void Particles2D::set_color_phase_color(int p_phase, const Color &p_color) { ERR_FAIL_INDEX(p_phase, MAX_COLOR_PHASES); - if (color_ramp.is_valid()) { - if (color_ramp->get_points_count() > p_phase) - color_ramp->set_color(p_phase, p_color); + if (gradient.is_valid()) { + if (gradient->get_points_count() > p_phase) + gradient->set_color(p_phase, p_color); } else { if (p_phase == 0) default_color = p_color; @@ -843,8 +843,8 @@ void Particles2D::set_color_phase_color(int p_phase, const Color &p_color) { Color Particles2D::get_color_phase_color(int p_phase) const { ERR_FAIL_INDEX_V(p_phase, MAX_COLOR_PHASES, Color()); - if (color_ramp.is_valid()) { - return color_ramp->get_color(p_phase); + if (gradient.is_valid()) { + return gradient->get_color(p_phase); } return Color(0, 0, 0, 1); } @@ -853,8 +853,8 @@ Color Particles2D::get_color_phase_color(int p_phase) const { void Particles2D::set_color_phase_pos(int p_phase, float p_pos) { ERR_FAIL_INDEX(p_phase, MAX_COLOR_PHASES); ERR_FAIL_COND(p_pos < 0.0 || p_pos > 1.0); - if (color_ramp.is_valid() && color_ramp->get_points_count() > p_phase) { - return color_ramp->set_offset(p_phase, p_pos); + if (gradient.is_valid() && gradient->get_points_count() > p_phase) { + return gradient->set_offset(p_phase, p_pos); } } @@ -862,8 +862,8 @@ void Particles2D::set_color_phase_pos(int p_phase, float p_pos) { float Particles2D::get_color_phase_pos(int p_phase) const { ERR_FAIL_INDEX_V(p_phase, MAX_COLOR_PHASES, 0); - if (color_ramp.is_valid()) { - return color_ramp->get_offset(p_phase); + if (gradient.is_valid()) { + return gradient->get_offset(p_phase); } return 0; } @@ -996,8 +996,8 @@ void Particles2D::_bind_methods() { ClassDB::bind_method(D_METHOD("set_color", "color"), &Particles2D::set_color); ClassDB::bind_method(D_METHOD("get_color"), &Particles2D::get_color); - ClassDB::bind_method(D_METHOD("set_color_ramp:ColorRamp", "color_ramp"), &Particles2D::set_color_ramp); - ClassDB::bind_method(D_METHOD("get_color_ramp:ColorRamp"), &Particles2D::get_color_ramp); + ClassDB::bind_method(D_METHOD("set_gradient:Gradient", "gradient"), &Particles2D::set_gradient); + ClassDB::bind_method(D_METHOD("get_gradient:Gradient"), &Particles2D::get_gradient); ClassDB::bind_method(D_METHOD("set_emissor_offset", "offset"), &Particles2D::set_emissor_offset); ClassDB::bind_method(D_METHOD("get_emissor_offset"), &Particles2D::get_emissor_offset); @@ -1078,7 +1078,7 @@ void Particles2D::_bind_methods() { } ADD_PROPERTYNO(PropertyInfo(Variant::COLOR, "color/color"), "set_color", "get_color"); - ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "color/color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "ColorRamp"), "set_color_ramp", "get_color_ramp"); + ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "color/color_ramp", PROPERTY_HINT_RESOURCE_TYPE, "Gradient"), "set_gradient", "get_gradient"); ADD_PROPERTYNZ(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "emission_points", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_emission_points", "get_emission_points"); diff --git a/scene/2d/particles_2d.h b/scene/2d/particles_2d.h index 5769fdd251..856beaa836 100644 --- a/scene/2d/particles_2d.h +++ b/scene/2d/particles_2d.h @@ -169,7 +169,7 @@ private: //If no color ramp is set then default color is used. Created as simple alternative to color_ramp. Color default_color; - Ref color_ramp; + Ref gradient; void _process_particles(float p_delta); friend class ParticleAttractor2D; @@ -241,8 +241,8 @@ public: void set_color(const Color &p_color); Color get_color() const; - void set_color_ramp(const Ref &p_texture); - Ref get_color_ramp() const; + void set_gradient(const Ref &p_texture); + Ref get_gradient() const; void set_emissor_offset(const Point2 &p_offset); Point2 get_emissor_offset() const; diff --git a/scene/gui/color_ramp_edit.cpp b/scene/gui/color_ramp_edit.cpp deleted file mode 100644 index 33e4ce0403..0000000000 --- a/scene/gui/color_ramp_edit.cpp +++ /dev/null @@ -1,452 +0,0 @@ -/*************************************************************************/ -/* color_ramp_edit.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* 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_ramp_edit.h" -#include "os/keyboard.h" - -ColorRampEdit::ColorRampEdit() { - grabbed = -1; - grabbing = false; - set_focus_mode(FOCUS_ALL); - - popup = memnew(PopupPanel); - picker = memnew(ColorPicker); - popup->add_child(picker); - - add_child(popup); - - checker = Ref(memnew(ImageTexture)); - Ref img = memnew(Image(checker_bg_png)); - checker->create_from_image(img, ImageTexture::FLAG_REPEAT); -} - -int ColorRampEdit::_get_point_from_pos(int x) { - int result = -1; - int total_w = get_size().width - get_size().height - 3; - for (int i = 0; i < points.size(); i++) { - //Check if we clicked at point - if (ABS(x - points[i].offset * total_w + 1) < (POINT_WIDTH / 2 + 1)) { - result = i; - } - } - return result; -} - -void ColorRampEdit::_show_color_picker() { - if (grabbed == -1) - return; - Size2 ms = Size2(350, picker->get_combined_minimum_size().height + 10); - picker->set_pick_color(points[grabbed].color); - popup->set_position(get_global_position() - Vector2(ms.width - get_size().width, ms.height)); - popup->set_size(ms); - popup->popup(); -} - -ColorRampEdit::~ColorRampEdit() { -} - -void ColorRampEdit::_gui_input(const Ref &p_event) { - - Ref k = p_event; - - if (k.is_valid() && k->is_pressed() && k->get_scancode() == KEY_DELETE && grabbed != -1) { - - points.remove(grabbed); - grabbed = -1; - grabbing = false; - update(); - emit_signal("ramp_changed"); - accept_event(); - } - - Ref mb = p_event; - //Show color picker on double click. - if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_doubleclick() && mb->is_pressed()) { - grabbed = _get_point_from_pos(mb->get_position().x); - _show_color_picker(); - accept_event(); - } - - //Delete point on right click - if (mb.is_valid() && mb->get_button_index() == 2 && mb->is_pressed()) { - grabbed = _get_point_from_pos(mb->get_position().x); - if (grabbed != -1) { - points.remove(grabbed); - grabbed = -1; - grabbing = false; - update(); - emit_signal("ramp_changed"); - accept_event(); - } - } - - //Hold alt key to duplicate selected color - if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed() && mb->get_alt()) { - - int x = mb->get_position().x; - grabbed = _get_point_from_pos(x); - - if (grabbed != -1) { - int total_w = get_size().width - get_size().height - 3; - Gradient::Point newPoint = points[grabbed]; - newPoint.offset = CLAMP(x / float(total_w), 0, 1); - - points.push_back(newPoint); - points.sort(); - for (int i = 0; i < points.size(); ++i) { - if (points[i].offset == newPoint.offset) { - grabbed = i; - break; - } - } - - emit_signal("ramp_changed"); - update(); - } - } - - if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) { - - update(); - int x = mb->get_position().x; - int total_w = get_size().width - get_size().height - 3; - - //Check if color selector was clicked. - if (x > total_w + 3) { - _show_color_picker(); - return; - } - - grabbing = true; - - grabbed = _get_point_from_pos(x); - //grab or select - if (grabbed != -1) { - return; - } - - //insert - Gradient::Point newPoint; - newPoint.offset = CLAMP(x / float(total_w), 0, 1); - - Gradient::Point prev; - Gradient::Point next; - - int pos = -1; - for (int i = 0; i < points.size(); i++) { - if (points[i].offset < newPoint.offset) - pos = i; - } - - 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]; - } - - newPoint.color = prev.color.linear_interpolate(next.color, (newPoint.offset - prev.offset) / (next.offset - prev.offset)); - - points.push_back(newPoint); - points.sort(); - for (int i = 0; i < points.size(); i++) { - if (points[i].offset == newPoint.offset) { - grabbed = i; - break; - } - } - - emit_signal("ramp_changed"); - } - - if (mb.is_valid() && mb->get_button_index() == 1 && !mb->is_pressed()) { - - if (grabbing) { - grabbing = false; - emit_signal("ramp_changed"); - } - update(); - } - - Ref mm = p_event; - - if (mm.is_valid() && grabbing) { - - int total_w = get_size().width - get_size().height - 3; - - int x = mm->get_position().x; - - float newofs = CLAMP(x / float(total_w), 0, 1); - - //Snap to nearest point if holding shift - if (mm->get_shift()) { - float snap_treshhold = 0.03; - float smallest_ofs = snap_treshhold; - bool founded = false; - int nearest_point; - 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; - if (founded) - break; - founded = true; - } - } - } - if (founded) { - if (points[nearest_point].offset < newofs) - newofs = points[nearest_point].offset + 0.00001; - else - newofs = points[nearest_point].offset - 0.00001; - newofs = CLAMP(newofs, 0, 1); - } - } - - bool valid = true; - for (int i = 0; i < points.size(); i++) { - - if (points[i].offset == newofs && i != grabbed) { - valid = false; - } - } - - if (!valid) - return; - - points[grabbed].offset = newofs; - - points.sort(); - for (int i = 0; i < points.size(); i++) { - if (points[i].offset == newofs) { - grabbed = i; - break; - } - } - - emit_signal("ramp_changed"); - - update(); - } -} - -void ColorRampEdit::_notification(int p_what) { - - if (p_what == NOTIFICATION_ENTER_TREE) { - if (!picker->is_connected("color_changed", this, "_color_changed")) { - picker->connect("color_changed", this, "_color_changed"); - } - } - if (p_what == NOTIFICATION_DRAW) { - - int w = get_size().x; - int h = get_size().y; - - 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 - - int total_w = get_size().width - get_size().height - 3; - - //Draw checker pattern for ramp - _draw_checker(0, 0, total_w, h); - - //Draw color ramp - Gradient::Point prev; - prev.offset = 0; - if (points.size() == 0) - prev.color = Color(0, 0, 0); //Draw black rectangle if we have no points - else - prev.color = points[0].color; //Extend color of first point to the beginning. - - for (int i = -1; i < points.size(); i++) { - - Gradient::Point next; - //If there is no next point - if (i + 1 == points.size()) { - if (points.size() == 0) - next.color = Color(0, 0, 0); //Draw black rectangle if we have no points - else - next.color = points[i].color; //Extend color of last point to the end. - next.offset = 1; - } else { - next = points[i + 1]; - } - - if (prev.offset == next.offset) { - prev = next; - continue; - } - - Vector points; - Vector colors; - points.push_back(Vector2(prev.offset * total_w, h)); - points.push_back(Vector2(prev.offset * total_w, 0)); - points.push_back(Vector2(next.offset * total_w, 0)); - points.push_back(Vector2(next.offset * total_w, h)); - colors.push_back(prev.color); - colors.push_back(prev.color); - colors.push_back(next.color); - colors.push_back(next.color); - draw_primitive(points, colors, Vector()); - prev = next; - } - - //Draw point markers - for (int i = 0; i < points.size(); i++) { - - Color col = i == grabbed ? Color(1, 0.0, 0.0, 0.9) : points[i].color.contrasted(); - col.a = 0.9; - - draw_line(Vector2(points[i].offset * total_w, 0), Vector2(points[i].offset * total_w, h / 2), col); - draw_rect(Rect2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2, POINT_WIDTH, h / 2), Color(0.6, 0.6, 0.6, i == grabbed ? 0.9 : 0.4)); - draw_line(Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2), Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h - 1), col); - draw_line(Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h / 2), Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h - 1), col); - draw_line(Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2), Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h / 2), col); - draw_line(Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h - 1), Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h - 1), col); - } - - //Draw "button" for color selector - _draw_checker(total_w + 3, 0, h, h); - if (grabbed != -1) { - //Draw with selection color - draw_rect(Rect2(total_w + 3, 0, h, h), points[grabbed].color); - } else { - //if no color selected draw grey color with 'X' on top. - draw_rect(Rect2(total_w + 3, 0, h, h), Color(0.5, 0.5, 0.5, 1)); - draw_line(Vector2(total_w + 3, 0), Vector2(total_w + 3 + h, h), Color(1, 1, 1, 0.6)); - draw_line(Vector2(total_w + 3, h), Vector2(total_w + 3 + h, 0), Color(1, 1, 1, 0.6)); - } - - //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)); - } - } -} - -void ColorRampEdit::_draw_checker(int x, int y, int w, int h) { - //Draw it with polygon to insert UVs for scale - Vector backPoints; - backPoints.push_back(Vector2(x, y)); - backPoints.push_back(Vector2(x, y + h)); - backPoints.push_back(Vector2(x + w, y + h)); - backPoints.push_back(Vector2(x + w, y)); - Vector colorPoints; - colorPoints.push_back(Color(1, 1, 1, 1)); - colorPoints.push_back(Color(1, 1, 1, 1)); - colorPoints.push_back(Color(1, 1, 1, 1)); - colorPoints.push_back(Color(1, 1, 1, 1)); - Vector uvPoints; - //Draw checker pattern pixel-perfect and scale it by 2. - uvPoints.push_back(Vector2(x, y)); - uvPoints.push_back(Vector2(x, y + h * .5f / checker->get_height())); - uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y + h * .5f / checker->get_height())); - uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y)); - draw_polygon(backPoints, colorPoints, uvPoints, checker); -} - -Size2 ColorRampEdit::get_minimum_size() const { - - return Vector2(0, 16); -} - -void ColorRampEdit::_color_changed(const Color &p_color) { - - if (grabbed == -1) - return; - points[grabbed].color = p_color; - update(); - emit_signal("ramp_changed"); -} - -void ColorRampEdit::set_ramp(const Vector &p_offsets, const Vector &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(); - update(); -} - -Vector ColorRampEdit::get_offsets() const { - Vector ret; - for (int i = 0; i < points.size(); i++) - ret.push_back(points[i].offset); - return ret; -} - -Vector ColorRampEdit::get_colors() const { - Vector ret; - for (int i = 0; i < points.size(); i++) - ret.push_back(points[i].color); - return ret; -} - -void ColorRampEdit::set_points(Vector &p_points) { - if (points.size() != p_points.size()) - grabbed = -1; - points.clear(); - points = p_points; -} - -Vector &ColorRampEdit::get_points() { - return points; -} - -void ColorRampEdit::_bind_methods() { - ClassDB::bind_method(D_METHOD("_gui_input"), &ColorRampEdit::_gui_input); - ClassDB::bind_method(D_METHOD("_color_changed"), &ColorRampEdit::_color_changed); - ADD_SIGNAL(MethodInfo("ramp_changed")); -} diff --git a/scene/gui/color_ramp_edit.h b/scene/gui/color_ramp_edit.h deleted file mode 100644 index 0fe447c43a..0000000000 --- a/scene/gui/color_ramp_edit.h +++ /dev/null @@ -1,80 +0,0 @@ -/*************************************************************************/ -/* color_ramp_edit.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* http://www.godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ -#ifndef SCENE_GUI_COLOR_RAMP_EDIT_H_ -#define SCENE_GUI_COLOR_RAMP_EDIT_H_ - -#include "scene/gui/color_picker.h" -#include "scene/gui/popup.h" -#include "scene/resources/color_ramp.h" -#include "scene/resources/default_theme/theme_data.h" - -#define POINT_WIDTH 8 - -class ColorRampEdit : public Control { - - GDCLASS(ColorRampEdit, Control); - - PopupPanel *popup; - ColorPicker *picker; - - Ref checker; - - bool grabbing; - int grabbed; - Vector points; - - void _draw_checker(int x, int y, int w, int h); - void _color_changed(const Color &p_color); - int _get_point_from_pos(int x); - void _show_color_picker(); - -protected: - void _gui_input(const Ref &p_event); - void _notification(int p_what); - static void _bind_methods(); - -public: - void set_ramp(const Vector &p_offsets, const Vector &p_colors); - Vector get_offsets() const; - Vector get_colors() const; - void set_points(Vector &p_points); - Vector &get_points(); - virtual Size2 get_minimum_size() const; - - ColorRampEdit(); - virtual ~ColorRampEdit(); -}; - -/*class ColorRampEditPanel : public Panel -{ - GDCLASS(ColorRampEditPanel, Panel ); -};*/ - -#endif /* SCENE_GUI_COLOR_RAMP_EDIT_H_ */ diff --git a/scene/gui/gradient_edit.cpp b/scene/gui/gradient_edit.cpp new file mode 100644 index 0000000000..58bce57580 --- /dev/null +++ b/scene/gui/gradient_edit.cpp @@ -0,0 +1,452 @@ +/*************************************************************************/ +/* color_ramp_edit.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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 "gradient_edit.h" +#include "os/keyboard.h" + +GradientEdit::GradientEdit() { + grabbed = -1; + grabbing = false; + set_focus_mode(FOCUS_ALL); + + popup = memnew(PopupPanel); + picker = memnew(ColorPicker); + popup->add_child(picker); + + add_child(popup); + + checker = Ref(memnew(ImageTexture)); + Ref img = memnew(Image(checker_bg_png)); + checker->create_from_image(img, ImageTexture::FLAG_REPEAT); +} + +int GradientEdit::_get_point_from_pos(int x) { + int result = -1; + int total_w = get_size().width - get_size().height - 3; + for (int i = 0; i < points.size(); i++) { + //Check if we clicked at point + if (ABS(x - points[i].offset * total_w + 1) < (POINT_WIDTH / 2 + 1)) { + result = i; + } + } + return result; +} + +void GradientEdit::_show_color_picker() { + if (grabbed == -1) + return; + Size2 ms = Size2(350, picker->get_combined_minimum_size().height + 10); + picker->set_pick_color(points[grabbed].color); + popup->set_position(get_global_position() - Vector2(ms.width - get_size().width, ms.height)); + popup->set_size(ms); + popup->popup(); +} + +GradientEdit::~GradientEdit() { +} + +void GradientEdit::_gui_input(const Ref &p_event) { + + Ref k = p_event; + + if (k.is_valid() && k->is_pressed() && k->get_scancode() == KEY_DELETE && grabbed != -1) { + + points.remove(grabbed); + grabbed = -1; + grabbing = false; + update(); + emit_signal("ramp_changed"); + accept_event(); + } + + Ref mb = p_event; + //Show color picker on double click. + if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_doubleclick() && mb->is_pressed()) { + grabbed = _get_point_from_pos(mb->get_position().x); + _show_color_picker(); + accept_event(); + } + + //Delete point on right click + if (mb.is_valid() && mb->get_button_index() == 2 && mb->is_pressed()) { + grabbed = _get_point_from_pos(mb->get_position().x); + if (grabbed != -1) { + points.remove(grabbed); + grabbed = -1; + grabbing = false; + update(); + emit_signal("ramp_changed"); + accept_event(); + } + } + + //Hold alt key to duplicate selected color + if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed() && mb->get_alt()) { + + int x = mb->get_position().x; + grabbed = _get_point_from_pos(x); + + if (grabbed != -1) { + int total_w = get_size().width - get_size().height - 3; + Gradient::Point newPoint = points[grabbed]; + newPoint.offset = CLAMP(x / float(total_w), 0, 1); + + points.push_back(newPoint); + points.sort(); + for (int i = 0; i < points.size(); ++i) { + if (points[i].offset == newPoint.offset) { + grabbed = i; + break; + } + } + + emit_signal("ramp_changed"); + update(); + } + } + + if (mb.is_valid() && mb->get_button_index() == 1 && mb->is_pressed()) { + + update(); + int x = mb->get_position().x; + int total_w = get_size().width - get_size().height - 3; + + //Check if color selector was clicked. + if (x > total_w + 3) { + _show_color_picker(); + return; + } + + grabbing = true; + + grabbed = _get_point_from_pos(x); + //grab or select + if (grabbed != -1) { + return; + } + + //insert + Gradient::Point newPoint; + newPoint.offset = CLAMP(x / float(total_w), 0, 1); + + Gradient::Point prev; + Gradient::Point next; + + int pos = -1; + for (int i = 0; i < points.size(); i++) { + if (points[i].offset < newPoint.offset) + pos = i; + } + + 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]; + } + + newPoint.color = prev.color.linear_interpolate(next.color, (newPoint.offset - prev.offset) / (next.offset - prev.offset)); + + points.push_back(newPoint); + points.sort(); + for (int i = 0; i < points.size(); i++) { + if (points[i].offset == newPoint.offset) { + grabbed = i; + break; + } + } + + emit_signal("ramp_changed"); + } + + if (mb.is_valid() && mb->get_button_index() == 1 && !mb->is_pressed()) { + + if (grabbing) { + grabbing = false; + emit_signal("ramp_changed"); + } + update(); + } + + Ref mm = p_event; + + if (mm.is_valid() && grabbing) { + + int total_w = get_size().width - get_size().height - 3; + + int x = mm->get_position().x; + + float newofs = CLAMP(x / float(total_w), 0, 1); + + //Snap to nearest point if holding shift + if (mm->get_shift()) { + float snap_treshhold = 0.03; + float smallest_ofs = snap_treshhold; + bool founded = false; + int nearest_point; + 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; + if (founded) + break; + founded = true; + } + } + } + if (founded) { + if (points[nearest_point].offset < newofs) + newofs = points[nearest_point].offset + 0.00001; + else + newofs = points[nearest_point].offset - 0.00001; + newofs = CLAMP(newofs, 0, 1); + } + } + + bool valid = true; + for (int i = 0; i < points.size(); i++) { + + if (points[i].offset == newofs && i != grabbed) { + valid = false; + } + } + + if (!valid) + return; + + points[grabbed].offset = newofs; + + points.sort(); + for (int i = 0; i < points.size(); i++) { + if (points[i].offset == newofs) { + grabbed = i; + break; + } + } + + emit_signal("ramp_changed"); + + update(); + } +} + +void GradientEdit::_notification(int p_what) { + + if (p_what == NOTIFICATION_ENTER_TREE) { + if (!picker->is_connected("color_changed", this, "_color_changed")) { + picker->connect("color_changed", this, "_color_changed"); + } + } + if (p_what == NOTIFICATION_DRAW) { + + int w = get_size().x; + int h = get_size().y; + + 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 + + int total_w = get_size().width - get_size().height - 3; + + //Draw checker pattern for ramp + _draw_checker(0, 0, total_w, h); + + //Draw color ramp + Gradient::Point prev; + prev.offset = 0; + if (points.size() == 0) + prev.color = Color(0, 0, 0); //Draw black rectangle if we have no points + else + prev.color = points[0].color; //Extend color of first point to the beginning. + + for (int i = -1; i < points.size(); i++) { + + Gradient::Point next; + //If there is no next point + if (i + 1 == points.size()) { + if (points.size() == 0) + next.color = Color(0, 0, 0); //Draw black rectangle if we have no points + else + next.color = points[i].color; //Extend color of last point to the end. + next.offset = 1; + } else { + next = points[i + 1]; + } + + if (prev.offset == next.offset) { + prev = next; + continue; + } + + Vector points; + Vector colors; + points.push_back(Vector2(prev.offset * total_w, h)); + points.push_back(Vector2(prev.offset * total_w, 0)); + points.push_back(Vector2(next.offset * total_w, 0)); + points.push_back(Vector2(next.offset * total_w, h)); + colors.push_back(prev.color); + colors.push_back(prev.color); + colors.push_back(next.color); + colors.push_back(next.color); + draw_primitive(points, colors, Vector()); + prev = next; + } + + //Draw point markers + for (int i = 0; i < points.size(); i++) { + + Color col = i == grabbed ? Color(1, 0.0, 0.0, 0.9) : points[i].color.contrasted(); + col.a = 0.9; + + draw_line(Vector2(points[i].offset * total_w, 0), Vector2(points[i].offset * total_w, h / 2), col); + draw_rect(Rect2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2, POINT_WIDTH, h / 2), Color(0.6, 0.6, 0.6, i == grabbed ? 0.9 : 0.4)); + draw_line(Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2), Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h - 1), col); + draw_line(Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h / 2), Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h - 1), col); + draw_line(Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h / 2), Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h / 2), col); + draw_line(Vector2(points[i].offset * total_w - POINT_WIDTH / 2, h - 1), Vector2(points[i].offset * total_w + POINT_WIDTH / 2, h - 1), col); + } + + //Draw "button" for color selector + _draw_checker(total_w + 3, 0, h, h); + if (grabbed != -1) { + //Draw with selection color + draw_rect(Rect2(total_w + 3, 0, h, h), points[grabbed].color); + } else { + //if no color selected draw grey color with 'X' on top. + draw_rect(Rect2(total_w + 3, 0, h, h), Color(0.5, 0.5, 0.5, 1)); + draw_line(Vector2(total_w + 3, 0), Vector2(total_w + 3 + h, h), Color(1, 1, 1, 0.6)); + draw_line(Vector2(total_w + 3, h), Vector2(total_w + 3 + h, 0), Color(1, 1, 1, 0.6)); + } + + //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)); + } + } +} + +void GradientEdit::_draw_checker(int x, int y, int w, int h) { + //Draw it with polygon to insert UVs for scale + Vector backPoints; + backPoints.push_back(Vector2(x, y)); + backPoints.push_back(Vector2(x, y + h)); + backPoints.push_back(Vector2(x + w, y + h)); + backPoints.push_back(Vector2(x + w, y)); + Vector colorPoints; + colorPoints.push_back(Color(1, 1, 1, 1)); + colorPoints.push_back(Color(1, 1, 1, 1)); + colorPoints.push_back(Color(1, 1, 1, 1)); + colorPoints.push_back(Color(1, 1, 1, 1)); + Vector uvPoints; + //Draw checker pattern pixel-perfect and scale it by 2. + uvPoints.push_back(Vector2(x, y)); + uvPoints.push_back(Vector2(x, y + h * .5f / checker->get_height())); + uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y + h * .5f / checker->get_height())); + uvPoints.push_back(Vector2(x + w * .5f / checker->get_width(), y)); + draw_polygon(backPoints, colorPoints, uvPoints, checker); +} + +Size2 GradientEdit::get_minimum_size() const { + + return Vector2(0, 16); +} + +void GradientEdit::_color_changed(const Color &p_color) { + + if (grabbed == -1) + return; + points[grabbed].color = p_color; + update(); + emit_signal("ramp_changed"); +} + +void GradientEdit::set_ramp(const Vector &p_offsets, const Vector &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(); + update(); +} + +Vector GradientEdit::get_offsets() const { + Vector ret; + for (int i = 0; i < points.size(); i++) + ret.push_back(points[i].offset); + return ret; +} + +Vector GradientEdit::get_colors() const { + Vector ret; + for (int i = 0; i < points.size(); i++) + ret.push_back(points[i].color); + return ret; +} + +void GradientEdit::set_points(Vector &p_points) { + if (points.size() != p_points.size()) + grabbed = -1; + points.clear(); + points = p_points; +} + +Vector &GradientEdit::get_points() { + return points; +} + +void GradientEdit::_bind_methods() { + ClassDB::bind_method(D_METHOD("_gui_input"), &GradientEdit::_gui_input); + ClassDB::bind_method(D_METHOD("_color_changed"), &GradientEdit::_color_changed); + ADD_SIGNAL(MethodInfo("ramp_changed")); +} diff --git a/scene/gui/gradient_edit.h b/scene/gui/gradient_edit.h new file mode 100644 index 0000000000..6c4ae6fd15 --- /dev/null +++ b/scene/gui/gradient_edit.h @@ -0,0 +1,80 @@ +/*************************************************************************/ +/* color_ramp_edit.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ +#ifndef SCENE_GUI_COLOR_RAMP_EDIT_H_ +#define SCENE_GUI_COLOR_RAMP_EDIT_H_ + +#include "scene/gui/color_picker.h" +#include "scene/gui/popup.h" +#include "scene/resources/color_ramp.h" +#include "scene/resources/default_theme/theme_data.h" + +#define POINT_WIDTH 8 + +class GradientEdit : public Control { + + GDCLASS(GradientEdit, Control); + + PopupPanel *popup; + ColorPicker *picker; + + Ref checker; + + bool grabbing; + int grabbed; + Vector points; + + void _draw_checker(int x, int y, int w, int h); + void _color_changed(const Color &p_color); + int _get_point_from_pos(int x); + void _show_color_picker(); + +protected: + void _gui_input(const Ref &p_event); + void _notification(int p_what); + static void _bind_methods(); + +public: + void set_ramp(const Vector &p_offsets, const Vector &p_colors); + Vector get_offsets() const; + Vector get_colors() const; + void set_points(Vector &p_points); + Vector &get_points(); + virtual Size2 get_minimum_size() const; + + GradientEdit(); + virtual ~GradientEdit(); +}; + +/*class ColorRampEditPanel : public Panel +{ + GDCLASS(ColorRampEditPanel, Panel ); +};*/ + +#endif /* SCENE_GUI_COLOR_RAMP_EDIT_H_ */ -- cgit v1.2.3