Cleaned up PolygonView.

This commit is contained in:
Relintai 2022-06-13 17:28:10 +02:00
parent 09e7eb3db2
commit a12ee8f913
3 changed files with 123 additions and 192 deletions

View File

@ -1,25 +1,17 @@
#include "polygon_view.h" #include "polygon_view.h"
#include "../../../nodes/bases/polygon_base.h"
Variant PolygonView::get_Variant() { Ref<PolygonBase> PolygonView::get_polygon() {
return Variant; return polygon;
} }
void PolygonView::set_Variant(const Variant &val) { void PolygonView::set_polygon(const Ref<PolygonBase> &val) {
Variant = val; polygon = val;
update();
} }
Variant PolygonView::get_Variant() {
return Variant;
}
void PolygonView::set_Variant(const Variant &val) {
Variant = val;
}
Vector2 PolygonView::get_draw_size() { Vector2 PolygonView::get_draw_size() {
return draw_size; return draw_size;
} }
@ -28,7 +20,6 @@ void PolygonView::set_draw_size(const Vector2 &val) {
draw_size = val; draw_size = val;
} }
Vector2 PolygonView::get_draw_offset() { Vector2 PolygonView::get_draw_offset() {
return draw_offset; return draw_offset;
} }
@ -37,57 +28,25 @@ void PolygonView::set_draw_offset(const Vector2 &val) {
draw_offset = val; draw_offset = val;
} }
bool PolygonView::get_closed() const { bool PolygonView::get_closed() const {
return closed; return closed;
} }
void PolygonView::set_closed(const bool val) {
closed = val;
}
//tool;
//var MMPolygon = preload("res://addons/mat_maker_gd/nodes/bases/polygon_base.gd");
//: MMPolygon;
Variant ;
Vector2 draw_size = Vector2(1, 1);
Vector2 draw_offset = Vector2(0, 0);
bool closed = true;
void PolygonView::set_closed(const bool c) { void PolygonView::set_closed(const bool c) {
closed = c; closed = c;
update(); update();
} }
void PolygonView::_ready() {
// polygon = MMPolygon.new();
connect("resized", self, "_on_resize");
_on_resize();
}
Vector2 PolygonView::transform_point(const Vector2 &p) { Vector2 PolygonView::transform_point(const Vector2 &p) {
return draw_offset + p * draw_size; return draw_offset + p * draw_size;
} }
Vector2 PolygonView::reverse_transform_point(const Vector2 &p) { Vector2 PolygonView::reverse_transform_point(const Vector2 &p) {
return (p - draw_offset) / draw_size; return (p - draw_offset) / draw_size;
} }
void PolygonView::set_polygon(const Variant &val) {
polygon = val;
update();
}
void PolygonView::_draw() { void PolygonView::_draw() {
if (!polygon.is_valid()) {
if (!polygon) {
return; return;
} }
@ -99,73 +58,80 @@ closed = val;
Color axes_color = Color(0.9, 0.9, 0.9, 1); Color axes_color = Color(0.9, 0.9, 0.9, 1);
Color curve_color = Color(1, 1, 1, 1); Color curve_color = Color(1, 1, 1, 1);
draw_rect(Rect2(draw_offset, draw_size), axes_color, false); draw_rect(Rect2(draw_offset, draw_size), axes_color, false);
Vector2 tp = transform_point(polygon.points[polygon.points.size()-1 if closed else 0]);
for (p in polygon.points) { PoolVector2Array points = polygon->get_points();
Variant = transform_point(p);
int index = 0;
if (closed) {
index = points.size() - 1;
}
Vector2 tp = transform_point(points[index]);
for (int i = 0; i < points.size(); ++i) {
Vector2 p = points[i];
Vector2 tnp = transform_point(p);
draw_line(tp, tnp, curve_color); draw_line(tp, tnp, curve_color);
tp = tnp; tp = tnp;
} }
} }
void PolygonView::_on_resize() { void PolygonView::_on_resize() {
float ds = min(rect_size.x, rect_size.y); float ds = MIN(get_size().x, get_size().y);
draw_size = Vector2(ds, ds); draw_size = Vector2(ds, ds);
draw_offset = 0.5*(rect_size-draw_size); draw_offset = 0.5 * (get_size() - draw_size);
update(); update();
}
} }
PolygonView::PolygonView() { PolygonView::PolygonView() {
//var MMPolygon = preload("res://addons/mat_maker_gd/nodes/bases/polygon_base.gd");
;
draw_size = Vector2(1, 1); draw_size = Vector2(1, 1);
draw_offset = Vector2(0, 0); draw_offset = Vector2(0, 0);
closed = true; closed = true;
set_size(Vector2(64, 64));
set_mouse_filter(MOUSE_FILTER_IGNORE);
} }
PolygonView::~PolygonView() { PolygonView::~PolygonView() {
} }
void PolygonView::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POSTINITIALIZE: {
// polygon = MMPolygon.new();
connect("resized", this, "_on_resize");
_on_resize();
} break;
case NOTIFICATION_DRAW: {
_draw();
} break;
default: {
} break;
}
}
static void PolygonView::_bind_methods() { void PolygonView::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_Variant"), &PolygonView::get_Variant); ClassDB::bind_method(D_METHOD("get_polygon"), &PolygonView::get_polygon);
ClassDB::bind_method(D_METHOD("set_Variant", "value"), &PolygonView::set_Variant); ClassDB::bind_method(D_METHOD("set_polygon", "value"), &PolygonView::set_polygon);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "Variant", PROPERTY_HINT_RESOURCE_TYPE, "Variant"), "set_Variant", "get_Variant"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "polygon", PROPERTY_HINT_RESOURCE_TYPE, "PolygonBase"), "set_polygon", "get_polygon");
ClassDB::bind_method(D_METHOD("get_Variant"), &PolygonView::get_Variant);
ClassDB::bind_method(D_METHOD("set_Variant", "value"), &PolygonView::set_Variant);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "Variant", PROPERTY_HINT_RESOURCE_TYPE, "Variant"), "set_Variant", "get_Variant");
ClassDB::bind_method(D_METHOD("get_draw_size"), &PolygonView::get_draw_size); ClassDB::bind_method(D_METHOD("get_draw_size"), &PolygonView::get_draw_size);
ClassDB::bind_method(D_METHOD("set_draw_size", "value"), &PolygonView::set_draw_size); ClassDB::bind_method(D_METHOD("set_draw_size", "value"), &PolygonView::set_draw_size);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "draw_size"), "set_draw_size", "get_draw_size"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "draw_size"), "set_draw_size", "get_draw_size");
ClassDB::bind_method(D_METHOD("get_draw_offset"), &PolygonView::get_draw_offset); ClassDB::bind_method(D_METHOD("get_draw_offset"), &PolygonView::get_draw_offset);
ClassDB::bind_method(D_METHOD("set_draw_offset", "value"), &PolygonView::set_draw_offset); ClassDB::bind_method(D_METHOD("set_draw_offset", "value"), &PolygonView::set_draw_offset);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "draw_offset"), "set_draw_offset", "get_draw_offset"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "draw_offset"), "set_draw_offset", "get_draw_offset");
ClassDB::bind_method(D_METHOD("get_closed"), &PolygonView::get_closed); ClassDB::bind_method(D_METHOD("get_closed"), &PolygonView::get_closed);
ClassDB::bind_method(D_METHOD("set_closed", "value"), &PolygonView::set_closed); // ClassDB::bind_method(D_METHOD("set_closed", "value"), &PolygonView::set_closed);
ClassDB::bind_method(D_METHOD("set_closed", "c"), &PolygonView::set_closed, true);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "get_closed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "closed"), "set_closed", "get_closed");
ClassDB::bind_method(D_METHOD("set_closed", "c"), &PolygonView::set_closed, true);
ClassDB::bind_method(D_METHOD("_ready"), &PolygonView::_ready);
ClassDB::bind_method(D_METHOD("transform_point", "p"), &PolygonView::transform_point); ClassDB::bind_method(D_METHOD("transform_point", "p"), &PolygonView::transform_point);
ClassDB::bind_method(D_METHOD("reverse_transform_point", "p"), &PolygonView::reverse_transform_point); ClassDB::bind_method(D_METHOD("reverse_transform_point", "p"), &PolygonView::reverse_transform_point);
ClassDB::bind_method(D_METHOD("set_polygon", "val"), &PolygonView::set_polygon);
ClassDB::bind_method(D_METHOD("_draw"), &PolygonView::_draw); //ClassDB::bind_method(D_METHOD("_draw"), &PolygonView::_draw);
ClassDB::bind_method(D_METHOD("_on_resize"), &PolygonView::_on_resize); ClassDB::bind_method(D_METHOD("_on_resize"), &PolygonView::_on_resize);
} }

View File

@ -1,31 +0,0 @@
void construct() {
//Script: res://addons/mat_maker_gd/widgets/polygon_edit/polygon_view.gd
Control *polygonview = memnew(Control);
polygonview->set_name("PolygonView");
polygonview->set_name("PolygonView");
//polygonview->set("name", PolygonView));
polygonview->set_filename("res://addons/mat_maker_gd/widgets/polygon_edit/polygon_view.tscn");
//polygonview->set("filename", "res://addons/mat_maker_gd/widgets/polygon_edit/polygon_view.tscn");
polygonview->set_margin_right(64);
//polygonview->set("margin_right", 64);
polygonview->set_margin_bottom(64);
//polygonview->set("margin_bottom", 64);
polygonview->set_rect_size(Vector2(64, 64));
//polygonview->set("rect_size", Vector2(64, 64));
polygonview->set_mouse_filter(2);
//polygonview->set("mouse_filter", 2);
//polygonview property __meta__ TYPE_DICTIONARY value: {_edit_use_anchors_:False}
}

View File

@ -1,17 +1,18 @@
#ifndef POLYGON_VIEW_H #ifndef POLYGON_VIEW_H
#define POLYGON_VIEW_H #define POLYGON_VIEW_H
#include "core/reference.h"
#include "scene/gui/control.h"
class PolygonBase;
class PolygonView : public Control { class PolygonView : public Control {
GDCLASS(PolygonView, Control); GDCLASS(PolygonView, Control);
public: public:
Ref<PolygonBase> get_polygon();
Variant get_Variant(); void set_polygon(const Ref<PolygonBase> &val);
void set_Variant(const Variant &val);
Variant get_Variant();
void set_Variant(const Variant &val);
Vector2 get_draw_size(); Vector2 get_draw_size();
void set_draw_size(const Vector2 &val); void set_draw_size(const Vector2 &val);
@ -20,13 +21,11 @@ class PolygonView : public Control {
void set_draw_offset(const Vector2 &val); void set_draw_offset(const Vector2 &val);
bool get_closed() const; bool get_closed() const;
void set_closed(const bool val);
void set_closed(const bool c = true); void set_closed(const bool c = true);
void _ready();
Vector2 transform_point(const Vector2 &p); Vector2 transform_point(const Vector2 &p);
Vector2 reverse_transform_point(const Vector2 &p); Vector2 reverse_transform_point(const Vector2 &p);
void set_polygon(const Variant &val);
void _draw(); void _draw();
void _on_resize(); void _on_resize();
@ -34,16 +33,13 @@ class PolygonView : public Control {
~PolygonView(); ~PolygonView();
protected: protected:
void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();
//tool Ref<PolygonBase> polygon;
Variant = preload("res://addons/mat_maker_gd/nodes/bases/polygon_base.gd");
//: MMPolygon
Variant ;
Vector2 draw_size = Vector2(1, 1); Vector2 draw_size = Vector2(1, 1);
Vector2 draw_offset = Vector2(0, 0); Vector2 draw_offset = Vector2(0, 0);
bool closed = true; bool closed = true;
}; };
#endif #endif