2017-09-06 16:41:14 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* abstract_polygon_2d_editor.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 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. */
|
|
|
|
/**************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2017-09-06 16:41:14 +00:00
|
|
|
#ifndef ABSTRACT_POLYGON_2D_EDITOR_H
|
|
|
|
#define ABSTRACT_POLYGON_2D_EDITOR_H
|
|
|
|
|
2024-04-21 08:11:35 +00:00
|
|
|
#include "editor/plugins/editor_plugin.h"
|
2017-09-06 16:41:14 +00:00
|
|
|
#include "scene/2d/polygon_2d.h"
|
2022-02-12 01:46:22 +00:00
|
|
|
#include "scene/gui/box_container.h"
|
2017-09-06 16:41:14 +00:00
|
|
|
|
2023-04-07 16:59:49 +00:00
|
|
|
class Button;
|
2017-09-06 16:41:14 +00:00
|
|
|
class CanvasItemEditor;
|
2022-11-11 19:12:48 +00:00
|
|
|
class ConfirmationDialog;
|
2017-09-06 16:41:14 +00:00
|
|
|
|
|
|
|
class AbstractPolygon2DEditor : public HBoxContainer {
|
|
|
|
GDCLASS(AbstractPolygon2DEditor, HBoxContainer);
|
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
Button *button_create = nullptr;
|
|
|
|
Button *button_edit = nullptr;
|
|
|
|
Button *button_delete = nullptr;
|
2017-09-06 16:41:14 +00:00
|
|
|
|
2017-10-16 10:40:13 +00:00
|
|
|
struct Vertex {
|
2020-05-12 15:01:17 +00:00
|
|
|
Vertex() {}
|
|
|
|
Vertex(int p_vertex) :
|
|
|
|
vertex(p_vertex) {}
|
|
|
|
Vertex(int p_polygon, int p_vertex) :
|
|
|
|
polygon(p_polygon),
|
|
|
|
vertex(p_vertex) {}
|
2017-10-16 10:40:13 +00:00
|
|
|
|
|
|
|
bool operator==(const Vertex &p_vertex) const;
|
|
|
|
bool operator!=(const Vertex &p_vertex) const;
|
|
|
|
|
|
|
|
bool valid() const;
|
|
|
|
|
2020-05-12 15:01:17 +00:00
|
|
|
int polygon = -1;
|
|
|
|
int vertex = -1;
|
2017-10-16 10:40:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PosVertex : public Vertex {
|
2020-05-12 15:01:17 +00:00
|
|
|
PosVertex() {}
|
|
|
|
PosVertex(const Vertex &p_vertex, const Vector2 &p_pos) :
|
|
|
|
Vertex(p_vertex.polygon, p_vertex.vertex),
|
|
|
|
pos(p_pos) {}
|
|
|
|
PosVertex(int p_polygon, int p_vertex, const Vector2 &p_pos) :
|
|
|
|
Vertex(p_polygon, p_vertex),
|
|
|
|
pos(p_pos) {}
|
2017-10-16 10:40:13 +00:00
|
|
|
|
|
|
|
Vector2 pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
PosVertex edited_point;
|
|
|
|
Vertex hover_point; // point under mouse cursor
|
|
|
|
Vertex selected_point; // currently selected
|
|
|
|
PosVertex edge_point; // adding an edge point?
|
2024-01-14 03:49:20 +00:00
|
|
|
Vector2 original_mouse_pos;
|
2017-10-16 10:40:13 +00:00
|
|
|
|
2017-09-06 16:41:14 +00:00
|
|
|
Vector<Vector2> pre_move_edit;
|
|
|
|
Vector<Vector2> wip;
|
2022-05-02 14:28:25 +00:00
|
|
|
bool wip_active = false;
|
|
|
|
bool wip_destructive = false;
|
2017-09-06 16:41:14 +00:00
|
|
|
|
2022-05-02 14:28:25 +00:00
|
|
|
bool _polygon_editing_enabled = false;
|
2019-02-16 18:46:09 +00:00
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
CanvasItemEditor *canvas_item_editor = nullptr;
|
|
|
|
Panel *panel = nullptr;
|
|
|
|
ConfirmationDialog *create_resource = nullptr;
|
2017-09-06 16:41:14 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
enum {
|
|
|
|
MODE_CREATE,
|
|
|
|
MODE_EDIT,
|
2017-11-04 13:46:49 +00:00
|
|
|
MODE_DELETE,
|
2017-09-06 16:41:14 +00:00
|
|
|
MODE_CONT,
|
|
|
|
};
|
|
|
|
|
2022-05-02 14:28:25 +00:00
|
|
|
int mode = MODE_EDIT;
|
2017-09-06 16:41:14 +00:00
|
|
|
|
|
|
|
virtual void _menu_option(int p_option);
|
2017-11-04 13:46:49 +00:00
|
|
|
void _wip_changed();
|
2017-09-06 16:41:14 +00:00
|
|
|
void _wip_close();
|
2018-11-13 21:50:54 +00:00
|
|
|
void _wip_cancel();
|
2017-09-06 16:41:14 +00:00
|
|
|
|
|
|
|
void _notification(int p_what);
|
|
|
|
void _node_removed(Node *p_node);
|
|
|
|
|
2017-10-16 10:40:13 +00:00
|
|
|
void remove_point(const Vertex &p_vertex);
|
|
|
|
Vertex get_active_point() const;
|
|
|
|
PosVertex closest_point(const Vector2 &p_pos) const;
|
|
|
|
PosVertex closest_edge_point(const Vector2 &p_pos) const;
|
|
|
|
|
2017-09-06 16:41:14 +00:00
|
|
|
bool _is_empty() const;
|
|
|
|
|
|
|
|
virtual Node2D *_get_node() const = 0;
|
|
|
|
virtual void _set_node(Node *p_polygon) = 0;
|
|
|
|
|
2017-11-04 13:46:49 +00:00
|
|
|
virtual bool _is_line() const;
|
2018-11-24 04:38:26 +00:00
|
|
|
virtual bool _has_uv() const;
|
2017-09-06 16:41:14 +00:00
|
|
|
virtual int _get_polygon_count() const;
|
|
|
|
virtual Vector2 _get_offset(int p_idx) const;
|
|
|
|
virtual Variant _get_polygon(int p_idx) const;
|
|
|
|
virtual void _set_polygon(int p_idx, const Variant &p_polygon) const;
|
|
|
|
|
|
|
|
virtual void _action_add_polygon(const Variant &p_polygon);
|
|
|
|
virtual void _action_remove_polygon(int p_idx);
|
|
|
|
virtual void _action_set_polygon(int p_idx, const Variant &p_polygon);
|
|
|
|
virtual void _action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon);
|
2018-11-24 04:38:26 +00:00
|
|
|
virtual void _commit_action();
|
2017-09-06 16:41:14 +00:00
|
|
|
|
|
|
|
virtual bool _has_resource() const;
|
|
|
|
virtual void _create_resource();
|
|
|
|
|
|
|
|
public:
|
2024-02-15 16:25:58 +00:00
|
|
|
void disable_polygon_editing(bool p_disable, const String &p_reason);
|
2019-02-16 18:46:09 +00:00
|
|
|
|
2017-09-06 16:41:14 +00:00
|
|
|
bool forward_gui_input(const Ref<InputEvent> &p_event);
|
2018-09-18 18:00:07 +00:00
|
|
|
void forward_canvas_draw_over_viewport(Control *p_overlay);
|
2017-10-15 20:33:25 +00:00
|
|
|
|
2017-09-06 16:41:14 +00:00
|
|
|
void edit(Node *p_polygon);
|
2022-01-27 09:36:51 +00:00
|
|
|
AbstractPolygon2DEditor(bool p_wip_destructive = true);
|
2017-09-06 16:41:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AbstractPolygon2DEditorPlugin : public EditorPlugin {
|
|
|
|
GDCLASS(AbstractPolygon2DEditorPlugin, EditorPlugin);
|
|
|
|
|
2022-04-04 13:06:57 +00:00
|
|
|
AbstractPolygon2DEditor *polygon_editor = nullptr;
|
2017-09-06 16:41:14 +00:00
|
|
|
String klass;
|
|
|
|
|
|
|
|
public:
|
2020-07-10 10:34:39 +00:00
|
|
|
virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return polygon_editor->forward_gui_input(p_event); }
|
|
|
|
virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { polygon_editor->forward_canvas_draw_over_viewport(p_overlay); }
|
|
|
|
|
|
|
|
bool has_main_screen() const override { return false; }
|
|
|
|
virtual String get_name() const override { return klass; }
|
|
|
|
virtual void edit(Object *p_object) override;
|
|
|
|
virtual bool handles(Object *p_object) const override;
|
|
|
|
virtual void make_visible(bool p_visible) override;
|
2017-09-06 16:41:14 +00:00
|
|
|
|
2024-02-15 16:25:58 +00:00
|
|
|
AbstractPolygon2DEditorPlugin(AbstractPolygon2DEditor *p_polygon_editor, const String &p_class);
|
2017-09-06 16:41:14 +00:00
|
|
|
~AbstractPolygon2DEditorPlugin();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // ABSTRACT_POLYGON_2D_EDITOR_H
|