2016-06-18 20:46:12 +08:00
|
|
|
/**************************************************************************/
|
2019-02-13 00:18:13 +08:00
|
|
|
/* gradient.cpp */
|
2016-06-18 20:46:12 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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-05 07:50:27 +08:00
|
|
|
|
2019-02-13 00:18:13 +08:00
|
|
|
#include "gradient.h"
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
Gradient::Gradient() {
|
2020-10-26 00:32:44 +08:00
|
|
|
//Set initial gradient transition from black to white
|
2015-05-25 02:18:52 +08:00
|
|
|
points.resize(2);
|
2018-07-25 09:11:03 +08:00
|
|
|
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;
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
Gradient::~Gradient() {
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
void Gradient::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("add_point", "offset", "color"), &Gradient::add_point);
|
2020-12-29 00:57:55 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("remove_point", "point"), &Gradient::remove_point);
|
2015-12-12 20:45:17 +08:00
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_offset", "point", "offset"), &Gradient::set_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_offset", "point"), &Gradient::get_offset);
|
2015-12-12 20:45:17 +08:00
|
|
|
|
2021-11-09 02:11:36 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("reverse"), &Gradient::reverse);
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_color", "point", "color"), &Gradient::set_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_color", "point"), &Gradient::get_color);
|
2015-12-12 20:45:17 +08:00
|
|
|
|
2022-07-25 00:47:57 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("sample", "offset"), &Gradient::get_color_at_offset);
|
2015-12-12 20:45:17 +08:00
|
|
|
|
2023-01-02 22:52:05 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_count"), &Gradient::get_point_count);
|
2015-12-12 20:45:17 +08:00
|
|
|
|
2020-10-26 00:32:44 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_offsets", "offsets"), &Gradient::set_offsets);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_offsets"), &Gradient::get_offsets);
|
2015-12-12 20:45:17 +08:00
|
|
|
|
2020-10-26 00:32:44 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_colors", "colors"), &Gradient::set_colors);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_colors"), &Gradient::get_colors);
|
2015-12-12 20:45:17 +08:00
|
|
|
|
2021-11-09 02:11:36 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_interpolation_mode", "interpolation_mode"), &Gradient::set_interpolation_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_interpolation_mode"), &Gradient::get_interpolation_mode);
|
|
|
|
|
2023-04-24 09:22:45 +08:00
|
|
|
ClassDB::bind_method(D_METHOD("set_interpolation_color_space", "interpolation_color_space"), &Gradient::set_interpolation_color_space);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_interpolation_color_space"), &Gradient::get_interpolation_color_space);
|
|
|
|
|
|
|
|
ADD_GROUP("Interpolation", "interpolation_");
|
2021-11-09 02:11:36 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "interpolation_mode", PROPERTY_HINT_ENUM, "Linear,Constant,Cubic"), "set_interpolation_mode", "get_interpolation_mode");
|
2023-04-24 09:22:45 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "interpolation_color_space", PROPERTY_HINT_ENUM, "sRGB,Linear sRGB,Oklab"), "set_interpolation_color_space", "get_interpolation_color_space");
|
2021-11-09 02:11:36 +08:00
|
|
|
|
2022-04-01 10:16:53 +08:00
|
|
|
ADD_GROUP("Raw Data", "");
|
2020-10-26 00:32:44 +08:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_FLOAT32_ARRAY, "offsets"), "set_offsets", "get_offsets");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_COLOR_ARRAY, "colors"), "set_colors", "get_colors");
|
2021-11-09 02:11:36 +08:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_LINEAR);
|
|
|
|
BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CONSTANT);
|
|
|
|
BIND_ENUM_CONSTANT(GRADIENT_INTERPOLATE_CUBIC);
|
2023-04-24 09:22:45 +08:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(GRADIENT_COLOR_SPACE_SRGB);
|
|
|
|
BIND_ENUM_CONSTANT(GRADIENT_COLOR_SPACE_LINEAR_SRGB);
|
|
|
|
BIND_ENUM_CONSTANT(GRADIENT_COLOR_SPACE_OKLAB);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Gradient::_validate_property(PropertyInfo &p_property) const {
|
|
|
|
if (p_property.name == "interpolation_color_space" && interpolation_mode == GRADIENT_INTERPOLATE_CONSTANT) {
|
|
|
|
p_property.usage = PROPERTY_USAGE_NO_EDITOR;
|
|
|
|
}
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
Vector<float> Gradient::get_offsets() const {
|
2015-05-25 02:18:52 +08:00
|
|
|
Vector<float> offsets;
|
|
|
|
offsets.resize(points.size());
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2018-07-25 09:11:03 +08:00
|
|
|
offsets.write[i] = points[i].offset;
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
return offsets;
|
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
Vector<Color> Gradient::get_colors() const {
|
2015-05-25 02:18:52 +08:00
|
|
|
Vector<Color> colors;
|
|
|
|
colors.resize(points.size());
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2018-07-25 09:11:03 +08:00
|
|
|
colors.write[i] = points[i].color;
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
2021-11-09 02:11:36 +08:00
|
|
|
void Gradient::set_interpolation_mode(Gradient::InterpolationMode p_interp_mode) {
|
2023-06-02 21:40:13 +08:00
|
|
|
if (p_interp_mode == interpolation_mode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-09 02:11:36 +08:00
|
|
|
interpolation_mode = p_interp_mode;
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2023-04-24 09:22:45 +08:00
|
|
|
notify_property_list_changed();
|
2021-11-09 02:11:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Gradient::InterpolationMode Gradient::get_interpolation_mode() {
|
|
|
|
return interpolation_mode;
|
|
|
|
}
|
|
|
|
|
2023-04-24 09:22:45 +08:00
|
|
|
void Gradient::set_interpolation_color_space(Gradient::ColorSpace p_color_space) {
|
2023-06-02 21:40:13 +08:00
|
|
|
if (p_color_space == interpolation_color_space) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-24 09:22:45 +08:00
|
|
|
interpolation_color_space = p_color_space;
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2023-04-24 09:22:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Gradient::ColorSpace Gradient::get_interpolation_color_space() {
|
|
|
|
return interpolation_color_space;
|
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
void Gradient::set_offsets(const Vector<float> &p_offsets) {
|
2015-05-27 03:17:54 +08:00
|
|
|
points.resize(p_offsets.size());
|
2015-05-25 02:18:52 +08:00
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2018-07-25 09:11:03 +08:00
|
|
|
points.write[i].offset = p_offsets[i];
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
2015-05-27 03:17:54 +08:00
|
|
|
is_sorted = false;
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
void Gradient::set_colors(const Vector<Color> &p_colors) {
|
2020-05-14 22:41:43 +08:00
|
|
|
if (points.size() < p_colors.size()) {
|
2015-05-27 03:17:54 +08:00
|
|
|
is_sorted = false;
|
2020-05-14 22:41:43 +08:00
|
|
|
}
|
2015-05-27 03:17:54 +08:00
|
|
|
points.resize(p_colors.size());
|
2015-05-25 02:18:52 +08:00
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2018-07-25 09:11:03 +08:00
|
|
|
points.write[i].color = p_colors[i];
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
Vector<Gradient::Point> &Gradient::get_points() {
|
2015-05-25 02:18:52 +08:00
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
void Gradient::add_point(float p_offset, const Color &p_color) {
|
2015-12-12 20:45:17 +08:00
|
|
|
Point p;
|
|
|
|
p.offset = p_offset;
|
|
|
|
p.color = p_color;
|
|
|
|
is_sorted = false;
|
|
|
|
points.push_back(p);
|
|
|
|
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2015-12-12 20:45:17 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
void Gradient::remove_point(int p_index) {
|
2015-12-12 20:45:17 +08:00
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
2020-02-05 18:20:15 +08:00
|
|
|
ERR_FAIL_COND(points.size() <= 1);
|
2021-07-04 06:17:03 +08:00
|
|
|
points.remove_at(p_index);
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2015-12-12 20:45:17 +08:00
|
|
|
}
|
|
|
|
|
2021-11-09 02:11:36 +08:00
|
|
|
void Gradient::reverse() {
|
|
|
|
for (int i = 0; i < points.size(); i++) {
|
|
|
|
points.write[i].offset = 1.0 - points[i].offset;
|
|
|
|
}
|
|
|
|
|
2023-03-23 02:32:00 +08:00
|
|
|
is_sorted = false;
|
2021-11-09 02:11:36 +08:00
|
|
|
_update_sorting();
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2021-11-09 02:11:36 +08:00
|
|
|
}
|
|
|
|
|
2022-04-28 17:35:39 +08:00
|
|
|
void Gradient::set_points(const Vector<Gradient::Point> &p_points) {
|
2015-05-25 02:18:52 +08:00
|
|
|
points = p_points;
|
2015-05-27 03:17:54 +08:00
|
|
|
is_sorted = false;
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
void Gradient::set_offset(int pos, const float offset) {
|
2020-08-10 16:23:12 +08:00
|
|
|
ERR_FAIL_INDEX(pos, points.size());
|
|
|
|
_update_sorting();
|
2018-07-25 09:11:03 +08:00
|
|
|
points.write[pos].offset = offset;
|
2015-05-27 03:17:54 +08:00
|
|
|
is_sorted = false;
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2020-08-10 16:23:12 +08:00
|
|
|
float Gradient::get_offset(int pos) {
|
2019-06-21 17:34:32 +08:00
|
|
|
ERR_FAIL_INDEX_V(pos, points.size(), 0.0);
|
2020-08-10 16:23:12 +08:00
|
|
|
_update_sorting();
|
2019-06-21 17:34:32 +08:00
|
|
|
return points[pos].offset;
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2017-05-29 08:46:48 +08:00
|
|
|
void Gradient::set_color(int pos, const Color &color) {
|
2020-08-10 16:23:12 +08:00
|
|
|
ERR_FAIL_INDEX(pos, points.size());
|
|
|
|
_update_sorting();
|
2018-07-25 09:11:03 +08:00
|
|
|
points.write[pos].color = color;
|
2023-06-03 02:39:14 +08:00
|
|
|
emit_changed();
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2020-08-10 16:23:12 +08:00
|
|
|
Color Gradient::get_color(int pos) {
|
2019-06-21 17:34:32 +08:00
|
|
|
ERR_FAIL_INDEX_V(pos, points.size(), Color());
|
2020-08-10 16:23:12 +08:00
|
|
|
_update_sorting();
|
2019-06-21 17:34:32 +08:00
|
|
|
return points[pos].color;
|
2015-05-25 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2023-01-02 22:52:05 +08:00
|
|
|
int Gradient::get_point_count() const {
|
2015-05-25 02:18:52 +08:00
|
|
|
return points.size();
|
|
|
|
}
|