2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* color_ramp.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 14:46:12 +02: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. */
|
|
|
|
/*************************************************************************/
|
2015-05-24 20:18:52 +02:00
|
|
|
#include "color_ramp.h"
|
2016-12-09 02:52:40 +01:00
|
|
|
#include "core_string_names.h"
|
2015-05-24 20:18:52 +02:00
|
|
|
|
|
|
|
//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"
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
Gradient::Gradient() {
|
2015-05-24 20:18:52 +02:00
|
|
|
//Set initial color ramp transition from black to white
|
|
|
|
points.resize(2);
|
2017-03-05 16:44:50 +01:00
|
|
|
points[0].color = Color(0, 0, 0, 1);
|
2015-05-24 20:18:52 +02:00
|
|
|
points[0].offset = 0;
|
2017-03-05 16:44:50 +01:00
|
|
|
points[1].color = Color(1, 1, 1, 1);
|
2015-05-24 20:18:52 +02:00
|
|
|
points[1].offset = 1;
|
2015-05-26 21:17:54 +02:00
|
|
|
is_sorted = true;
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
Gradient::~Gradient() {
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
void Gradient::_bind_methods() {
|
2015-05-24 20:18:52 +02:00
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("add_point", "offset", "color"), &Gradient::add_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_point", "offset", "color"), &Gradient::remove_point);
|
2015-12-12 13:45:17 +01:00
|
|
|
|
2017-05-29 02:46:48 +02: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 13:45:17 +01:00
|
|
|
|
2017-05-29 02:46:48 +02: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 13:45:17 +01:00
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("interpolate", "offset"), &Gradient::get_color_at_offset);
|
2015-12-12 13:45:17 +01:00
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_point_count"), &Gradient::get_points_count);
|
2015-12-12 13:45:17 +01:00
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
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);
|
2015-12-12 13:45:17 +01:00
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
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);
|
2015-12-12 13:45:17 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "offsets"), COLOR_RAMP_SET_OFFSETS, COLOR_RAMP_GET_OFFSETS);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "colors"), COLOR_RAMP_SET_COLORS, COLOR_RAMP_GET_COLORS);
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
Vector<float> Gradient::get_offsets() const {
|
2015-05-24 20:18:52 +02:00
|
|
|
Vector<float> offsets;
|
|
|
|
offsets.resize(points.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2015-05-24 20:18:52 +02:00
|
|
|
offsets[i] = points[i].offset;
|
|
|
|
}
|
|
|
|
return offsets;
|
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
Vector<Color> Gradient::get_colors() const {
|
2015-05-24 20:18:52 +02:00
|
|
|
Vector<Color> colors;
|
|
|
|
colors.resize(points.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2015-05-24 20:18:52 +02:00
|
|
|
colors[i] = points[i].color;
|
|
|
|
}
|
|
|
|
return colors;
|
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
void Gradient::set_offsets(const Vector<float> &p_offsets) {
|
2015-05-26 21:17:54 +02:00
|
|
|
points.resize(p_offsets.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2015-05-26 21:17:54 +02:00
|
|
|
points[i].offset = p_offsets[i];
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
2015-05-26 21:17:54 +02:00
|
|
|
is_sorted = false;
|
2016-12-09 02:52:40 +01:00
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
void Gradient::set_colors(const Vector<Color> &p_colors) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (points.size() < p_colors.size())
|
2015-05-26 21:17:54 +02:00
|
|
|
is_sorted = false;
|
|
|
|
points.resize(p_colors.size());
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < points.size(); i++) {
|
2015-05-26 21:17:54 +02:00
|
|
|
points[i].color = p_colors[i];
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
2016-12-09 02:52:40 +01:00
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
Vector<Gradient::Point> &Gradient::get_points() {
|
2015-05-24 20:18:52 +02:00
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
void Gradient::add_point(float p_offset, const Color &p_color) {
|
2015-12-12 13:45:17 +01:00
|
|
|
|
|
|
|
Point p;
|
2017-03-05 16:44:50 +01:00
|
|
|
p.offset = p_offset;
|
|
|
|
p.color = p_color;
|
|
|
|
is_sorted = false;
|
2015-12-12 13:45:17 +01:00
|
|
|
points.push_back(p);
|
|
|
|
|
2016-12-09 02:52:40 +01:00
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
2015-12-12 13:45:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
void Gradient::remove_point(int p_index) {
|
2015-12-12 13:45:17 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_INDEX(p_index, points.size());
|
|
|
|
ERR_FAIL_COND(points.size() <= 2);
|
2015-12-12 13:45:17 +01:00
|
|
|
points.remove(p_index);
|
2016-12-09 02:52:40 +01:00
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
2015-12-12 13:45:17 +01:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
void Gradient::set_points(Vector<Gradient::Point> &p_points) {
|
2015-05-24 20:18:52 +02:00
|
|
|
points = p_points;
|
2015-05-26 21:17:54 +02:00
|
|
|
is_sorted = false;
|
2016-12-09 02:52:40 +01:00
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
void Gradient::set_offset(int pos, const float offset) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (points.size() <= pos)
|
2015-05-24 20:18:52 +02:00
|
|
|
points.resize(pos + 1);
|
|
|
|
points[pos].offset = offset;
|
2015-05-26 21:17:54 +02:00
|
|
|
is_sorted = false;
|
2016-12-09 02:52:40 +01:00
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
float Gradient::get_offset(int pos) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (points.size() > pos)
|
2015-05-24 20:18:52 +02:00
|
|
|
return points[pos].offset;
|
2017-03-05 16:44:50 +01:00
|
|
|
return 0; //TODO: Maybe throw some error instead?
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
void Gradient::set_color(int pos, const Color &color) {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (points.size() <= pos) {
|
2015-05-24 20:18:52 +02:00
|
|
|
points.resize(pos + 1);
|
2015-05-26 21:17:54 +02:00
|
|
|
is_sorted = false;
|
|
|
|
}
|
2015-05-24 20:18:52 +02:00
|
|
|
points[pos].color = color;
|
2016-12-09 02:52:40 +01:00
|
|
|
emit_signal(CoreStringNames::get_singleton()->changed);
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
Color Gradient::get_color(int pos) const {
|
2017-03-05 16:44:50 +01:00
|
|
|
if (points.size() > pos)
|
2015-05-24 20:18:52 +02:00
|
|
|
return points[pos].color;
|
2017-03-05 16:44:50 +01:00
|
|
|
return Color(0, 0, 0, 1); //TODO: Maybe throw some error instead?
|
2015-05-24 20:18:52 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 02:46:48 +02:00
|
|
|
int Gradient::get_points_count() const {
|
2015-05-24 20:18:52 +02:00
|
|
|
return points.size();
|
|
|
|
}
|