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

163 lines
5.4 KiB
C++
Raw Normal View History

2023-12-18 00:02:58 +01:00
/*************************************************************************/
/* polygon_base.cpp */
/*************************************************************************/
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* Copyright (c) 2007-2022 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. */
/*************************************************************************/
#include "polygon_base.h"
2022-06-12 14:24:27 +02:00
#include "core/math/geometry.h"
2022-06-12 14:24:27 +02:00
void PolygonBase::clear() {
points.resize(0);
_polygon_changed();
}
2022-06-12 14:24:27 +02:00
void PolygonBase::add_point(const float x, const float y, const bool closed) {
Vector2 p = Vector2(x, y);
int points_count = points.size();
2022-06-12 14:24:27 +02:00
if (points_count < 3) {
points.append(p);
_polygon_changed();
return;
}
2022-06-12 14:24:27 +02:00
Vector2 segs[2];
segs[0] = points[0];
segs[1] = points[points_count - 1];
2022-06-12 14:24:27 +02:00
float min_length = (p - Geometry::get_closest_point_to_segment_2d(p, segs)).length();
int insert_point = 0;
2022-06-12 14:24:27 +02:00
for (int i = 0; i < points_count - 1; ++i) {
segs[0] = points[i];
segs[1] = points[i + 1];
2022-06-12 14:24:27 +02:00
float length = (p - Geometry::get_closest_point_to_segment_2d(p, segs)).length();
2022-06-12 14:24:27 +02:00
if (length < min_length) {
min_length = length;
insert_point = i + 1;
}
}
2022-06-12 14:24:27 +02:00
if (!closed && insert_point == 0 && (points[0] - p).length() > (points[points_count - 1] - p).length()) {
insert_point = points_count;
}
2022-06-12 14:24:27 +02:00
points.insert(insert_point, p);
_polygon_changed();
}
2022-06-12 14:24:27 +02:00
bool PolygonBase::remove_point(const int index) {
int s = points.size();
2022-06-12 14:24:27 +02:00
if (s < 4 || index < 0 || index >= s) {
return false;
} else {
points.remove(index);
_polygon_changed();
}
2022-06-12 14:24:27 +02:00
return true;
}
2022-06-12 14:24:27 +02:00
int PolygonBase::get_point_count() {
return points.size();
}
2022-06-12 14:24:27 +02:00
Vector2 PolygonBase::get_point(const int i) {
return points[i];
}
2022-06-12 14:24:27 +02:00
void PolygonBase::set_point(const int i, const Vector2 &v) {
points[i] = v;
_polygon_changed();
}
2022-06-12 14:45:01 +02:00
PoolVector2Array PolygonBase::get_points() {
return points;
}
2022-06-12 14:24:27 +02:00
void PolygonBase::set_points(const PoolVector2Array &v) {
points = v;
_polygon_changed();
}
2022-06-12 14:24:27 +02:00
void PolygonBase::polygon_changed() {
_polygon_changed();
}
2022-06-12 14:24:27 +02:00
void PolygonBase::_polygon_changed() {
emit_changed();
}
2022-06-14 18:44:45 +02:00
String PolygonBase::_to_string() {
2022-06-12 14:24:27 +02:00
String rv;
2022-06-12 14:24:27 +02:00
PoolVector2Array ps = get_points();
2022-06-12 14:24:27 +02:00
for (int i = 0; i < ps.size(); ++i) {
Vector2 p = ps[i];
2022-06-12 14:24:27 +02:00
rv += "(" + String::num(p.x) + "," + String::num(p.y) + ")" + ",";
}
2022-06-12 14:24:27 +02:00
return rv;
}
2022-06-12 14:24:27 +02:00
PolygonBase::PolygonBase() {
points.push_back(Vector2(0.2, 0.2));
points.push_back(Vector2(0.7, 0.4));
points.push_back(Vector2(0.4, 0.7));
}
2022-06-12 14:24:27 +02:00
PolygonBase::~PolygonBase() {
}
2022-06-12 14:24:27 +02:00
void PolygonBase::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_points"), &PolygonBase::get_points);
ClassDB::bind_method(D_METHOD("set_points", "value"), &PolygonBase::set_points);
ADD_PROPERTY(PropertyInfo(Variant::POOL_VECTOR2_ARRAY, "points"), "set_points", "get_points");
2022-06-12 14:24:27 +02:00
ClassDB::bind_method(D_METHOD("clear"), &PolygonBase::clear);
ClassDB::bind_method(D_METHOD("add_point", "x", "y", "closed"), &PolygonBase::add_point, true);
ClassDB::bind_method(D_METHOD("remove_point", "index"), &PolygonBase::remove_point);
ClassDB::bind_method(D_METHOD("get_point_count"), &PolygonBase::get_point_count);
2022-06-12 14:24:27 +02:00
ClassDB::bind_method(D_METHOD("get_point", "i"), &PolygonBase::get_point);
ClassDB::bind_method(D_METHOD("set_point", "i", "v"), &PolygonBase::set_point);
2022-06-12 14:24:27 +02:00
BIND_VMETHOD(MethodInfo("_polygon_changed"));
ClassDB::bind_method(D_METHOD("polygon_changed"), &PolygonBase::polygon_changed);
ClassDB::bind_method(D_METHOD("_polygon_changed"), &PolygonBase::_polygon_changed);
2022-06-14 18:44:45 +02:00
ClassDB::bind_method(D_METHOD("_to_string"), &PolygonBase::_to_string);
2022-06-12 14:24:27 +02:00
}