diff options
Diffstat (limited to 'scene/gui/gradient_edit.cpp')
-rw-r--r-- | scene/gui/gradient_edit.cpp | 59 |
1 files changed, 26 insertions, 33 deletions
diff --git a/scene/gui/gradient_edit.cpp b/scene/gui/gradient_edit.cpp index 88107f754c..ecd4ad17ea 100644 --- a/scene/gui/gradient_edit.cpp +++ b/scene/gui/gradient_edit.cpp @@ -73,8 +73,9 @@ int GradientEdit::_get_point_from_pos(int x) { } void GradientEdit::_show_color_picker() { - if (grabbed == -1) + if (grabbed == -1) { return; + } picker->set_pick_color(points[grabbed].color); Size2 minsize = popup->get_contents_minimum_size(); bool show_above = false; @@ -93,11 +94,9 @@ GradientEdit::~GradientEdit() { } void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) { - Ref<InputEventKey> k = p_event; if (k.is_valid() && k->is_pressed() && k->get_keycode() == KEY_DELETE && grabbed != -1) { - points.remove(grabbed); grabbed = -1; grabbing = false; @@ -129,7 +128,6 @@ void GradientEdit::_gui_input(const Ref<InputEvent> &p_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); @@ -154,7 +152,6 @@ void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) { //select 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 - SPACING; @@ -182,12 +179,12 @@ void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) { int pos = -1; for (int i = 0; i < points.size(); i++) { - if (points[i].offset < newPoint.offset) + if (points[i].offset < newPoint.offset) { pos = i; + } } if (pos == -1) { - prev.color = Color(0, 0, 0); prev.offset = 0; if (points.size()) { @@ -197,7 +194,6 @@ void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) { next.offset = 1.0; } } else { - if (pos == points.size() - 1) { next.color = Color(1, 1, 1); next.offset = 1.0; @@ -207,7 +203,7 @@ void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) { prev = points[pos]; } - newPoint.color = prev.color.linear_interpolate(next.color, (newPoint.offset - prev.offset) / (next.offset - prev.offset)); + newPoint.color = prev.color.lerp(next.color, (newPoint.offset - prev.offset) / (next.offset - prev.offset)); points.push_back(newPoint); points.sort(); @@ -222,7 +218,6 @@ void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) { } if (mb.is_valid() && mb->get_button_index() == 1 && !mb->is_pressed()) { - if (grabbing) { grabbing = false; emit_signal("ramp_changed"); @@ -233,7 +228,6 @@ void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) { Ref<InputEventMouseMotion> mm = p_event; if (mm.is_valid() && grabbing) { - int total_w = get_size().width - get_size().height - SPACING; int x = mm->get_position().x; @@ -256,24 +250,25 @@ void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) { if (temp_ofs < smallest_ofs) { smallest_ofs = temp_ofs; nearest_point = i; - if (found) + if (found) { break; + } found = true; } } } if (found) { - if (points[nearest_point].offset < newofs) + if (points[nearest_point].offset < newofs) { newofs = points[nearest_point].offset + 0.00001; - else + } 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; break; @@ -300,19 +295,18 @@ void GradientEdit::_gui_input(const Ref<InputEvent> &p_event) { } void GradientEdit::_notification(int p_what) { - if (p_what == NOTIFICATION_ENTER_TREE) { if (!picker->is_connected("color_changed", callable_mp(this, &GradientEdit::_color_changed))) { picker->connect("color_changed", callable_mp(this, &GradientEdit::_color_changed)); } } if (p_what == NOTIFICATION_DRAW) { - int w = get_size().x; int h = get_size().y; - if (w == 0 || h == 0) + 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 - SPACING; @@ -322,20 +316,21 @@ void GradientEdit::_notification(int p_what) { //Draw color ramp Gradient::Point prev; prev.offset = 0; - if (points.size() == 0) + if (points.size() == 0) { prev.color = Color(0, 0, 0); //Draw black rectangle if we have no points - else + } 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) + if (points.size() == 0) { next.color = Color(0, 0, 0); //Draw black rectangle if we have no points - else + } else { next.color = points[i].color; //Extend color of last point to the end. + } next.offset = 1; } else { next = points[i + 1]; @@ -362,7 +357,6 @@ void GradientEdit::_notification(int p_what) { //Draw point markers for (int i = 0; i < points.size(); i++) { - Color col = points[i].color.contrasted(); col.a = 0.9; @@ -397,7 +391,6 @@ void GradientEdit::_notification(int p_what) { //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)); @@ -406,7 +399,6 @@ void GradientEdit::_notification(int p_what) { } if (p_what == NOTIFICATION_VISIBILITY_CHANGED) { - if (!is_visible()) { grabbing = false; } @@ -435,21 +427,19 @@ void GradientEdit::_draw_checker(int x, int y, int w, int h) { } Size2 GradientEdit::get_minimum_size() const { - return Vector2(0, 16); } void GradientEdit::_color_changed(const Color &p_color) { - - if (grabbed == -1) + if (grabbed == -1) { return; + } points.write[grabbed].color = p_color; update(); emit_signal("ramp_changed"); } void GradientEdit::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++) { @@ -465,21 +455,24 @@ void GradientEdit::set_ramp(const Vector<float> &p_offsets, const Vector<Color> Vector<float> GradientEdit::get_offsets() const { Vector<float> ret; - for (int i = 0; i < points.size(); i++) + for (int i = 0; i < points.size(); i++) { ret.push_back(points[i].offset); + } return ret; } Vector<Color> GradientEdit::get_colors() const { Vector<Color> ret; - for (int i = 0; i < points.size(); i++) + for (int i = 0; i < points.size(); i++) { ret.push_back(points[i].color); + } return ret; } void GradientEdit::set_points(Vector<Gradient::Point> &p_points) { - if (points.size() != p_points.size()) + if (points.size() != p_points.size()) { grabbed = -1; + } points.clear(); points = p_points; } |