diff options
Diffstat (limited to 'scene/resources/gradient.cpp')
-rw-r--r-- | scene/resources/gradient.cpp | 54 |
1 files changed, 21 insertions, 33 deletions
diff --git a/scene/resources/gradient.cpp b/scene/resources/gradient.cpp index 7bce04beaf..7b9b942142 100644 --- a/scene/resources/gradient.cpp +++ b/scene/resources/gradient.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 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 */ @@ -32,29 +32,21 @@ #include "core/core_string_names.h" -//setter and getter names for property serialization -#define COLOR_RAMP_GET_OFFSETS "get_offsets" -#define COLOR_RAMP_GET_COLORS "get_colors" -#define COLOR_RAMP_SET_OFFSETS "set_offsets" -#define COLOR_RAMP_SET_COLORS "set_colors" - Gradient::Gradient() { - //Set initial color ramp transition from black to white + //Set initial gradient transition from black to white points.resize(2); points.write[0].color = Color(0, 0, 0, 1); points.write[0].offset = 0; points.write[1].color = Color(1, 1, 1, 1); points.write[1].offset = 1; - is_sorted = true; } Gradient::~Gradient() { } void Gradient::_bind_methods() { - ClassDB::bind_method(D_METHOD("add_point", "offset", "color"), &Gradient::add_point); - ClassDB::bind_method(D_METHOD("remove_point", "offset"), &Gradient::remove_point); + ClassDB::bind_method(D_METHOD("remove_point", "point"), &Gradient::remove_point); ClassDB::bind_method(D_METHOD("set_offset", "point", "offset"), &Gradient::set_offset); ClassDB::bind_method(D_METHOD("get_offset", "point"), &Gradient::get_offset); @@ -66,14 +58,14 @@ void Gradient::_bind_methods() { ClassDB::bind_method(D_METHOD("get_point_count"), &Gradient::get_points_count); - ClassDB::bind_method(D_METHOD(COLOR_RAMP_SET_OFFSETS, "offsets"), &Gradient::set_offsets); - ClassDB::bind_method(D_METHOD(COLOR_RAMP_GET_OFFSETS), &Gradient::get_offsets); + ClassDB::bind_method(D_METHOD("set_offsets", "offsets"), &Gradient::set_offsets); + ClassDB::bind_method(D_METHOD("get_offsets"), &Gradient::get_offsets); - ClassDB::bind_method(D_METHOD(COLOR_RAMP_SET_COLORS, "colors"), &Gradient::set_colors); - ClassDB::bind_method(D_METHOD(COLOR_RAMP_GET_COLORS), &Gradient::get_colors); + ClassDB::bind_method(D_METHOD("set_colors", "colors"), &Gradient::set_colors); + ClassDB::bind_method(D_METHOD("get_colors"), &Gradient::get_colors); - ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "offsets"), COLOR_RAMP_SET_OFFSETS, COLOR_RAMP_GET_OFFSETS); - ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "colors"), COLOR_RAMP_SET_COLORS, COLOR_RAMP_GET_COLORS); + ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "offsets"), "set_offsets", "get_offsets"); + ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "colors"), "set_colors", "get_colors"); } Vector<float> Gradient::get_offsets() const { @@ -104,8 +96,9 @@ void Gradient::set_offsets(const Vector<float> &p_offsets) { } void Gradient::set_colors(const Vector<Color> &p_colors) { - if (points.size() < p_colors.size()) + if (points.size() < p_colors.size()) { is_sorted = false; + } points.resize(p_colors.size()); for (int i = 0; i < points.size(); i++) { points.write[i].color = p_colors[i]; @@ -118,7 +111,6 @@ Vector<Gradient::Point> &Gradient::get_points() { } void Gradient::add_point(float p_offset, const Color &p_color) { - Point p; p.offset = p_offset; p.color = p_color; @@ -129,9 +121,8 @@ void Gradient::add_point(float p_offset, const Color &p_color) { } void Gradient::remove_point(int p_index) { - ERR_FAIL_INDEX(p_index, points.size()); - ERR_FAIL_COND(points.size() <= 2); + ERR_FAIL_COND(points.size() <= 1); points.remove(p_index); emit_signal(CoreStringNames::get_singleton()->changed); } @@ -143,32 +134,29 @@ void Gradient::set_points(Vector<Gradient::Point> &p_points) { } void Gradient::set_offset(int pos, const float offset) { - - ERR_FAIL_COND(pos < 0); - if (points.size() <= pos) - points.resize(pos + 1); + ERR_FAIL_INDEX(pos, points.size()); + _update_sorting(); points.write[pos].offset = offset; is_sorted = false; emit_signal(CoreStringNames::get_singleton()->changed); } -float Gradient::get_offset(int pos) const { +float Gradient::get_offset(int pos) { ERR_FAIL_INDEX_V(pos, points.size(), 0.0); + _update_sorting(); return points[pos].offset; } void Gradient::set_color(int pos, const Color &color) { - ERR_FAIL_COND(pos < 0); - if (points.size() <= pos) { - points.resize(pos + 1); - is_sorted = false; - } + ERR_FAIL_INDEX(pos, points.size()); + _update_sorting(); points.write[pos].color = color; emit_signal(CoreStringNames::get_singleton()->changed); } -Color Gradient::get_color(int pos) const { +Color Gradient::get_color(int pos) { ERR_FAIL_INDEX_V(pos, points.size(), Color()); + _update_sorting(); return points[pos].color; } |