Cleaned up PolygonEditor.

This commit is contained in:
Relintai 2022-06-13 18:58:08 +02:00
parent 5eafb2fcd5
commit efd6c7b333
4 changed files with 98 additions and 143 deletions

View File

@ -20,13 +20,14 @@ public:
PolygonControlPoint();
~PolygonControlPoint();
const Vector2 OFFSET = Vector2(3, 3);
protected:
void _notification(int p_what);
static void _bind_methods();
bool moving = false;
const Vector2 OFFSET = Vector2(3, 3);
};
#endif

View File

@ -1,111 +1,103 @@
#include "polygon_editor.h"
#include "../../../nodes/bases/polygon_base.h"
#include "polygon_control_point.h"
//tool;
signal value_changed(value);
void PolygonEditor::_ready() {
update_controls();
void PolygonEditor::set_polygon(const Ref<PolygonBase> &p) {
polygon = p;
update();
update_controls();
}
void PolygonEditor::update_controls() {
for (int i = 0; i < get_child_count(); ++i) {
Node *c = get_child(i);
c->queue_delete();
}
void PolygonEditor::set_polygon(const Variant &p) {
polygon = p;
update();
update_controls();
if (!polygon.is_valid()) {
return;
}
PoolVector2Array points = polygon->get_points();
for (int i = 0; i < points.size(); ++i) {
Vector2 p = points[i];
PolygonControlPoint *control_point = memnew(PolygonControlPoint);
add_child(control_point);
control_point->initialize(p);
control_point->set_position(transform_point(p) - control_point->OFFSET);
control_point->connect("moved", this, "_on_ControlPoint_moved");
control_point->connect("removed", this, "_on_ControlPoint_removed");
}
emit_signal("value_changed", polygon);
}
void PolygonEditor::_on_ControlPoint_moved(const int index) {
PolygonControlPoint *control_point = Object::cast_to<PolygonControlPoint>(get_child(index));
void PolygonEditor::update_controls() {
PoolVector2Array points = polygon->get_points();
for (c in get_children()) {
c.queue_free();
points.set(index, reverse_transform_point(control_point->get_position() + control_point->OFFSET));
polygon->set_points(points);
update();
emit_signal("value_changed", polygon);
}
if (!polygon) {
return;
void PolygonEditor::_on_ControlPoint_removed(const int index) {
if (polygon->remove_point(index)) {
update();
update_controls();
}
}
void PolygonEditor::_on_PolygonEditor_gui_input(const Ref<InputEvent> &event) {
if (!polygon.is_valid()) {
return;
}
for (i in polygon.points.size()) {
Variant = polygon.points[i];
//var control_point = preload("res://addons/mat_maker_gd/widgets/polygon_edit/control_point.tscn").instance();
add_child(control_point);
control_point.initialize(p);
control_point.rect_position = transform_point(p)-control_point.OFFSET;
control_point.connect("moved", self, "_on_ControlPoint_moved");
control_point.connect("removed", self, "_on_ControlPoint_removed");
Ref<InputEventMouseButton> iemb = event;
if (iemb.is_valid()) {
if (iemb->get_button_index() == BUTTON_LEFT && iemb->is_doubleclick()) {
Vector2 new_point_position = reverse_transform_point(get_local_mouse_position());
polygon->add_point(new_point_position.x, new_point_position.y, closed);
update_controls();
}
}
}
emit_signal("value_changed", polygon);
void PolygonEditor::_on_resize() {
PolygonView::_on_resize();
update_controls();
}
void PolygonEditor::_on_ControlPoint_moved(const Variant &index) {
Variant = get_child(index);
polygon.points[index] = reverse_transform_point(control_point.rect_position+control_point.OFFSET);
update();
emit_signal("value_changed", polygon);
PolygonEditor::PolygonEditor() {
}
void PolygonEditor::_on_ControlPoint_removed(const Variant &index) {
if (polygon.remove_point(index)) {
update();
update_controls();
PolygonEditor::~PolygonEditor() {
}
void PolygonEditor::_notification(int p_what) {
if (p_what == NOTIFICATION_POSTINITIALIZE) {
update_controls();
}
}
void PolygonEditor::_bind_methods() {
ADD_SIGNAL(MethodInfo("value_changed", PropertyInfo(Variant::OBJECT, "value", PROPERTY_HINT_RESOURCE_TYPE, "PolygonBase")));
void PolygonEditor::_on_PolygonEditor_gui_input(const Variant &event) {
ClassDB::bind_method(D_METHOD("set_polygon", "p"), &PolygonEditor::set_polygon);
ClassDB::bind_method(D_METHOD("update_controls"), &PolygonEditor::update_controls);
if (!polygon) {
return;
ClassDB::bind_method(D_METHOD("_on_ControlPoint_moved", "index"), &PolygonEditor::_on_ControlPoint_moved);
ClassDB::bind_method(D_METHOD("_on_ControlPoint_removed", "index"), &PolygonEditor::_on_ControlPoint_removed);
ClassDB::bind_method(D_METHOD("_on_PolygonEditor_gui_input", "event"), &PolygonEditor::_on_PolygonEditor_gui_input);
ClassDB::bind_method(D_METHOD("_on_resize"), &PolygonEditor::_on_resize);
}
if (event is InputEventMouseButton) {
if (event.button_index == BUTTON_LEFT && event.doubleclick) {
Variant = reverse_transform_point(get_local_mouse_position());
polygon.add_point(new_point_position.x, new_point_position.y, closed);
update_controls();
}
}
}
void PolygonEditor::_on_resize() {
._on_resize();
update_controls();
}
}
PolygonEditor::PolygonEditor() {
}
PolygonEditor::~PolygonEditor() {
}
static void PolygonEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_ready"), &PolygonEditor::_ready);
ClassDB::bind_method(D_METHOD("set_polygon", "p"), &PolygonEditor::set_polygon);
ClassDB::bind_method(D_METHOD("update_controls"), &PolygonEditor::update_controls);
ClassDB::bind_method(D_METHOD("_on_ControlPoint_moved", "index"), &PolygonEditor::_on_ControlPoint_moved);
ClassDB::bind_method(D_METHOD("_on_ControlPoint_removed", "index"), &PolygonEditor::_on_ControlPoint_removed);
ClassDB::bind_method(D_METHOD("_on_PolygonEditor_gui_input", "event"), &PolygonEditor::_on_PolygonEditor_gui_input);
ClassDB::bind_method(D_METHOD("_on_resize"), &PolygonEditor::_on_resize);
}

View File

@ -1,43 +0,0 @@
void construct() {
//Script: res://addons/mat_maker_gd/widgets/polygon_edit/polygon_editor.gd
Control *polygoneditor = memnew(Control);
polygoneditor->set_name("PolygonEditor");
polygoneditor->set_name("PolygonEditor");
//polygoneditor->set("name", PolygonEditor));
polygoneditor->set_filename("res://addons/mat_maker_gd/widgets/polygon_edit/polygon_editor.tscn");
//polygoneditor->set("filename", "res://addons/mat_maker_gd/widgets/polygon_edit/polygon_editor.tscn");
polygoneditor->set_anchor_right(1);
//polygoneditor->set("anchor_right", 1);
polygoneditor->set_anchor_bottom(1);
//polygoneditor->set("anchor_bottom", 1);
polygoneditor->set_margin_left(10);
//polygoneditor->set("margin_left", 10);
polygoneditor->set_margin_top(10);
//polygoneditor->set("margin_top", 10);
polygoneditor->set_margin_right(-10);
//polygoneditor->set("margin_right", -10);
polygoneditor->set_margin_bottom(-10);
//polygoneditor->set("margin_bottom", -10);
polygoneditor->set_rect_position(Vector2(10, 10));
//polygoneditor->set("rect_position", Vector2(10, 10));
polygoneditor->set_rect_global_position(Vector2(10, 10));
//polygoneditor->set("rect_global_position", Vector2(10, 10));
//polygoneditor property __meta__ TYPE_DICTIONARY value: {_edit_use_anchors_:False}
}

View File

@ -1,29 +1,34 @@
#ifndef POLYGON_EDITOR_H
#define POLYGON_EDITOR_H
#include "core/os/input_event.h"
#include "core/reference.h"
class PolygonEditor : public "res://addons/mat_maker_gd/widgets/polygon_edit/polygon_view.gd" {
GDCLASS(PolygonEditor, "res://addons/mat_maker_gd/widgets/polygon_edit/polygon_view.gd");
#include "polygon_view.h"
public:
class PolygonBase;
void _ready();
void set_polygon(const Variant &p);
void update_controls();
void _on_ControlPoint_moved(const Variant &index);
void _on_ControlPoint_removed(const Variant &index);
void _on_PolygonEditor_gui_input(const Variant &event);
void _on_resize();
class PolygonEditor : public PolygonView {
GDCLASS(PolygonEditor, PolygonView);
PolygonEditor();
~PolygonEditor();
public:
void set_polygon(const Ref<PolygonBase> &p);
void update_controls();
protected:
static void _bind_methods();
void _on_ControlPoint_moved(const int index);
void _on_ControlPoint_removed(const int index);
void _on_PolygonEditor_gui_input(const Ref<InputEvent> &event);
void _on_resize();
//tool
signal value_changed(value);
PolygonEditor();
~PolygonEditor();
protected:
void _notification(int p_what);
static void _bind_methods();
Ref<PolygonBase> polygon;
};
#endif