pandemonium_engine/modules/material_maker/nodes/bases/curve_base.cpp

206 lines
4.6 KiB
C++
Raw Normal View History

#include "curve_base.h"
PoolRealArray CurveBase::get_points_array() {
2022-06-12 00:54:08 +02:00
return points;
}
void CurveBase::set_points_array(const PoolRealArray &val) {
2022-06-12 00:54:08 +02:00
points = val;
}
2022-06-12 00:54:08 +02:00
void CurveBase::init_points_01() {
if (points.size() == 0) {
points.push_back(0.0);
points.push_back(0.0);
points.push_back(0.0);
points.push_back(1.0);
points.push_back(1.0);
points.push_back(1.0);
points.push_back(1.0);
points.push_back(0.0);
2022-06-12 00:54:08 +02:00
}
}
2022-06-12 00:54:08 +02:00
void CurveBase::init_points_11() {
if (points.size() == 0) {
points.push_back(0.0);
points.push_back(1.0);
points.push_back(0.0);
points.push_back(0.0);
points.push_back(1.0);
points.push_back(1.0);
points.push_back(0.0);
points.push_back(0.0);
2022-06-12 00:54:08 +02:00
}
}
2022-06-12 00:54:08 +02:00
String CurveBase::to_string() {
PoolStringArray rv;
Vector<CurveBase::Point> ps = get_points();
for (int i = 0; i < ps.size(); ++i) {
Point p = ps[i];
rv.append("(" + String::num(p.p.x) + "," + String::num(p.p.y) + "," + String::num(p.ls) + "," + String::num(p.rs) + ")");
2022-06-12 00:54:08 +02:00
}
2022-06-12 00:54:08 +02:00
return rv.join(",");
}
2022-06-12 00:54:08 +02:00
void CurveBase::clear() {
points.resize(0);
2022-06-12 00:54:08 +02:00
curve_changed();
}
void CurveBase::add_point(const float x, const float y, float ls, float rs) {
2022-06-12 00:54:08 +02:00
int indx = points.size() / 4;
for (int i = 0; i < indx; ++i) {
2022-06-12 00:54:08 +02:00
int ii = i * 4;
2022-06-12 00:54:08 +02:00
if (x < points[ii]) {
if (ls == Math_INF) {
ls = 0;
2022-06-12 00:54:08 +02:00
}
if (rs == Math_INF) {
rs = 0;
2022-06-12 00:54:08 +02:00
}
2022-06-12 00:54:08 +02:00
points.insert(ii, x);
points.insert(ii + 1, y);
points.insert(ii + 2, ls);
points.insert(ii + 3, rs);
curve_changed();
return;
}
}
2022-06-12 00:54:08 +02:00
points.append(x);
points.append(y);
points.append(ls);
points.append(rs);
curve_changed();
}
2022-06-12 00:54:08 +02:00
bool CurveBase::remove_point(const int i) {
int index = i * 4;
2022-06-12 00:54:08 +02:00
if (index <= 0 || index >= points.size() - 1) {
return false;
} else {
points.remove(index);
points.remove(index);
points.remove(index);
points.remove(index);
curve_changed();
}
2022-06-12 00:54:08 +02:00
return true;
}
PoolRealArray CurveBase::get_point_arr(const int i) {
int indx = i * 4;
PoolRealArray arr;
arr.push_back(points[indx + 0]);
arr.push_back(points[indx + 1]);
arr.push_back(points[indx + 2]);
arr.push_back(points[indx + 3]);
return arr;
}
2022-06-12 00:54:08 +02:00
int CurveBase::get_point_count() {
return points.size() / 4;
}
2022-06-12 00:54:08 +02:00
void CurveBase::set_point(const int i, const Point &v) {
int indx = i * 4;
points.set(indx + 0, v.p.x);
points.set(indx + 1, v.p.y);
points.set(indx + 2, v.ls);
points.set(indx + 3, v.rs);
2022-06-12 00:54:08 +02:00
curve_changed();
}
CurveBase::Point CurveBase::get_point(const int i) {
2022-06-12 00:54:08 +02:00
int indx = i * 4;
return Point(points[indx + 0], points[indx + 1], points[indx + 2], points[indx + 3]);
}
Vector<CurveBase::Point> CurveBase::get_points() {
Vector<CurveBase::Point> arr;
2022-06-12 00:54:08 +02:00
int c = get_point_count();
2022-06-12 00:54:08 +02:00
for (int i = 0; i < c; ++i) { //i in range(c)
arr.push_back(get_point(i));
2022-06-12 00:54:08 +02:00
}
2022-06-12 00:54:08 +02:00
return arr;
}
void CurveBase::set_points(const Vector<CurveBase::Point> &arr, const bool notify) {
2022-06-12 00:54:08 +02:00
points.resize(0);
for (int i = 0; i < arr.size(); ++i) {
Point p = arr[i];
2022-06-12 00:54:08 +02:00
points.append(p.p.x);
points.append(p.p.y);
points.append(p.ls);
points.append(p.rs);
}
2022-06-12 00:54:08 +02:00
if (notify) {
curve_changed();
}
}
2022-06-12 00:54:08 +02:00
void CurveBase::curve_changed() {
_curve_changed();
}
2022-06-12 00:54:08 +02:00
void CurveBase::_curve_changed() {
emit_changed();
}
2022-06-12 00:54:08 +02:00
CurveBase::CurveBase() {
}
2022-06-12 00:54:08 +02:00
CurveBase::~CurveBase() {
}
void CurveBase::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_points_array"), &CurveBase::get_points_array);
ClassDB::bind_method(D_METHOD("set_points_array", "value"), &CurveBase::set_points_array);
ADD_PROPERTY(PropertyInfo(Variant::POOL_REAL_ARRAY, "points_array"), "set_points_array", "get_points_array");
2022-06-12 00:54:08 +02:00
ClassDB::bind_method(D_METHOD("init_points_01"), &CurveBase::init_points_01);
ClassDB::bind_method(D_METHOD("init_points_11"), &CurveBase::init_points_11);
2022-06-12 00:54:08 +02:00
ClassDB::bind_method(D_METHOD("to_string"), &CurveBase::to_string);
2022-06-12 00:54:08 +02:00
ClassDB::bind_method(D_METHOD("clear"), &CurveBase::clear);
ClassDB::bind_method(D_METHOD("add_point", "x", "y", "ls", "rs"), &CurveBase::add_point, Math_INF, Math_INF);
2022-06-12 00:54:08 +02:00
ClassDB::bind_method(D_METHOD("remove_point", "i"), &CurveBase::remove_point);
ClassDB::bind_method(D_METHOD("get_point_arr", "i"), &CurveBase::get_point_arr);
2022-06-12 00:54:08 +02:00
ClassDB::bind_method(D_METHOD("get_point_count"), &CurveBase::get_point_count);
//ClassDB::bind_method(D_METHOD("set_point", "i", "v"), &CurveBase::set_point);
//ClassDB::bind_method(D_METHOD("get_point", "i"), &CurveBase::get_point);
2022-06-12 14:45:01 +02:00
//ClassDB::bind_method(D_METHOD("get_points"), &CurveBase::get_points);
//ClassDB::bind_method(D_METHOD("set_points", "arr", "notify"), &CurveBase::set_points, true);
BIND_VMETHOD(MethodInfo("_curve_changed"));
2022-06-12 00:54:08 +02:00
ClassDB::bind_method(D_METHOD("curve_changed"), &CurveBase::curve_changed);
ClassDB::bind_method(D_METHOD("_curve_changed"), &CurveBase::_curve_changed);
}