1 | /**************************************************************************/ |
2 | /* collision_shape_2d_editor_plugin.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef COLLISION_SHAPE_2D_EDITOR_PLUGIN_H |
32 | #define COLLISION_SHAPE_2D_EDITOR_PLUGIN_H |
33 | |
34 | #include "editor/editor_plugin.h" |
35 | #include "scene/2d/collision_shape_2d.h" |
36 | |
37 | class CanvasItemEditor; |
38 | |
39 | class CollisionShape2DEditor : public Control { |
40 | GDCLASS(CollisionShape2DEditor, Control); |
41 | |
42 | enum ShapeType { |
43 | CAPSULE_SHAPE, |
44 | CIRCLE_SHAPE, |
45 | CONCAVE_POLYGON_SHAPE, |
46 | CONVEX_POLYGON_SHAPE, |
47 | WORLD_BOUNDARY_SHAPE, |
48 | SEPARATION_RAY_SHAPE, |
49 | RECTANGLE_SHAPE, |
50 | SEGMENT_SHAPE |
51 | }; |
52 | |
53 | const Point2 RECT_HANDLES[8] = { |
54 | Point2(1, 0), |
55 | Point2(1, 1), |
56 | Point2(0, 1), |
57 | Point2(-1, 1), |
58 | Point2(-1, 0), |
59 | Point2(-1, -1), |
60 | Point2(0, -1), |
61 | Point2(1, -1), |
62 | }; |
63 | |
64 | CanvasItemEditor *canvas_item_editor = nullptr; |
65 | CollisionShape2D *node = nullptr; |
66 | |
67 | Vector<Point2> handles; |
68 | |
69 | int shape_type = -1; |
70 | int edit_handle = -1; |
71 | bool pressed = false; |
72 | real_t grab_threshold = 8; |
73 | Variant original; |
74 | Transform2D original_transform; |
75 | Vector2 original_point; |
76 | Point2 last_point; |
77 | |
78 | Ref<Shape2D> current_shape; |
79 | |
80 | Variant get_handle_value(int idx) const; |
81 | void set_handle(int idx, Point2 &p_point); |
82 | void commit_handle(int idx, Variant &p_org); |
83 | |
84 | void _shape_changed(); |
85 | |
86 | protected: |
87 | void _notification(int p_what); |
88 | void _node_removed(Node *p_node); |
89 | |
90 | public: |
91 | bool forward_canvas_gui_input(const Ref<InputEvent> &p_event); |
92 | void forward_canvas_draw_over_viewport(Control *p_overlay); |
93 | void edit(Node *p_node); |
94 | |
95 | CollisionShape2DEditor(); |
96 | }; |
97 | |
98 | class CollisionShape2DEditorPlugin : public EditorPlugin { |
99 | GDCLASS(CollisionShape2DEditorPlugin, EditorPlugin); |
100 | |
101 | CollisionShape2DEditor *collision_shape_2d_editor = nullptr; |
102 | |
103 | public: |
104 | virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) override { return collision_shape_2d_editor->forward_canvas_gui_input(p_event); } |
105 | virtual void forward_canvas_draw_over_viewport(Control *p_overlay) override { collision_shape_2d_editor->forward_canvas_draw_over_viewport(p_overlay); } |
106 | |
107 | virtual String get_name() const override { return "CollisionShape2D" ; } |
108 | bool has_main_screen() const override { return false; } |
109 | virtual void edit(Object *p_obj) override; |
110 | virtual bool handles(Object *p_obj) const override; |
111 | virtual void make_visible(bool visible) override; |
112 | |
113 | CollisionShape2DEditorPlugin(); |
114 | ~CollisionShape2DEditorPlugin(); |
115 | }; |
116 | |
117 | #endif // COLLISION_SHAPE_2D_EDITOR_PLUGIN_H |
118 | |