Path: blob/master/editor/scene/2d/abstract_polygon_2d_editor.h
9903 views
/**************************************************************************/1/* abstract_polygon_2d_editor.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "editor/plugins/editor_plugin.h"33#include "scene/2d/node_2d.h"34#include "scene/gui/box_container.h"3536class Button;37class CanvasItemEditor;38class ConfirmationDialog;3940class AbstractPolygon2DEditor : public HBoxContainer {41GDCLASS(AbstractPolygon2DEditor, HBoxContainer);4243Button *button_create = nullptr;44Button *button_edit = nullptr;45Button *button_delete = nullptr;46Button *button_center = nullptr;4748struct Vertex {49Vertex() {}50Vertex(int p_vertex) :51vertex(p_vertex) {}52Vertex(int p_polygon, int p_vertex) :53polygon(p_polygon),54vertex(p_vertex) {}5556bool operator==(const Vertex &p_vertex) const;57bool operator!=(const Vertex &p_vertex) const;5859bool valid() const;6061int polygon = -1;62int vertex = -1;63};6465struct PosVertex : public Vertex {66PosVertex() {}67PosVertex(const Vertex &p_vertex, const Vector2 &p_pos) :68Vertex(p_vertex.polygon, p_vertex.vertex),69pos(p_pos) {}70PosVertex(int p_polygon, int p_vertex, const Vector2 &p_pos) :71Vertex(p_polygon, p_vertex),72pos(p_pos) {}7374Vector2 pos;75};7677PosVertex edited_point;78Vertex hover_point; // point under mouse cursor79Vertex selected_point; // currently selected80PosVertex edge_point; // adding an edge point?81Vector2 original_mouse_pos;8283Vector<Vector2> pre_move_edit;84Vector<Vector2> wip;85bool wip_active = false;86bool wip_destructive = false;8788Vector<Vector<Vector2>> pre_center_move_edit;89bool center_drag = false;90Vector2 center_drag_origin;91bool edit_origin_and_center = false;9293bool _polygon_editing_enabled = false;9495CanvasItemEditor *canvas_item_editor = nullptr;96Panel *panel = nullptr;97ConfirmationDialog *create_resource = nullptr;9899protected:100enum {101MODE_CREATE,102MODE_EDIT,103MODE_DELETE,104MODE_CONT,105CENTER_POLY,106};107108int mode = MODE_EDIT;109110virtual void _menu_option(int p_option);111void _wip_changed();112void _wip_close();113void _wip_cancel();114115void _notification(int p_what);116void _node_removed(Node *p_node);117118bool _commit_drag();119120void remove_point(const Vertex &p_vertex);121Vertex get_active_point() const;122PosVertex closest_point(const Vector2 &p_pos) const;123PosVertex closest_edge_point(const Vector2 &p_pos) const;124125bool _is_empty() const;126127virtual Node2D *_get_node() const = 0;128virtual void _set_node(Node *p_polygon) = 0;129130virtual bool _is_line() const;131virtual bool _has_uv() const;132virtual int _get_polygon_count() const;133virtual Vector2 _get_offset(int p_idx) const;134virtual Variant _get_polygon(int p_idx) const;135virtual void _set_polygon(int p_idx, const Variant &p_polygon) const;136137virtual void _action_add_polygon(const Variant &p_polygon);138virtual void _action_remove_polygon(int p_idx);139virtual void _action_set_polygon(int p_idx, const Variant &p_polygon);140virtual void _action_set_polygon(int p_idx, const Variant &p_previous, const Variant &p_polygon);141virtual void _commit_action();142143virtual Vector2 _get_geometric_center() const;144145virtual bool _has_resource() const;146virtual void _create_resource();147148public:149void disable_polygon_editing(bool p_disable, const String &p_reason);150151bool forward_gui_input(const Ref<InputEvent> &p_event);152void forward_canvas_draw_over_viewport(Control *p_overlay);153void set_edit_origin_and_center(bool p_enabled);154155void edit(Node *p_polygon);156AbstractPolygon2DEditor(bool p_wip_destructive = true);157};158159class AbstractPolygon2DEditorPlugin : public EditorPlugin {160GDCLASS(AbstractPolygon2DEditorPlugin, EditorPlugin);161162AbstractPolygon2DEditor *polygon_editor = nullptr;163String klass;164165public:166virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return polygon_editor->forward_gui_input(p_event); }167virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { polygon_editor->forward_canvas_draw_over_viewport(p_overlay); }168169bool has_main_screen() const override { return false; }170virtual String get_plugin_name() const override { return klass; }171virtual void edit(Object *p_object) override;172virtual bool handles(Object *p_object) const override;173virtual void make_visible(bool p_visible) override;174175AbstractPolygon2DEditorPlugin(AbstractPolygon2DEditor *p_polygon_editor, const String &p_class);176};177178179