collision_shape_2d_editor_plugin.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifndef COLLISION_SHAPE_2D_EDITOR_PLUGIN_H
  31. #define COLLISION_SHAPE_2D_EDITOR_PLUGIN_H
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_plugin.h"
  36. #include "scene/2d/collision_shape_2d.h"
  37. class CanvasItemEditor;
  38. class CollisionShape2DEditor : public Control {
  39. GDCLASS(CollisionShape2DEditor, Control);
  40. enum ShapeType {
  41. CAPSULE_SHAPE,
  42. CIRCLE_SHAPE,
  43. CONCAVE_POLYGON_SHAPE,
  44. CONVEX_POLYGON_SHAPE,
  45. LINE_SHAPE,
  46. RAY_SHAPE,
  47. RECTANGLE_SHAPE,
  48. SEGMENT_SHAPE
  49. };
  50. const Point2 RECT_HANDLES[8] = {
  51. Point2(1, 0),
  52. Point2(1, 1),
  53. Point2(0, 1),
  54. Point2(-1, 1),
  55. Point2(-1, 0),
  56. Point2(-1, -1),
  57. Point2(0, -1),
  58. Point2(1, -1),
  59. };
  60. EditorNode *editor;
  61. UndoRedo *undo_redo;
  62. CanvasItemEditor *canvas_item_editor;
  63. CollisionShape2D *node;
  64. Vector<Point2> handles;
  65. int shape_type;
  66. int edit_handle;
  67. bool pressed;
  68. real_t grab_threshold = 8;
  69. Variant original;
  70. Transform2D original_transform;
  71. Point2 last_point;
  72. Variant get_handle_value(int idx) const;
  73. void set_handle(int idx, Point2 &p_point);
  74. void commit_handle(int idx, Variant &p_org);
  75. void _get_current_shape_type();
  76. protected:
  77. void _notification(int p_what);
  78. void _node_removed(Node *p_node);
  79. static void _bind_methods();
  80. public:
  81. bool forward_canvas_gui_input(const Ref<InputEvent> &p_event);
  82. void forward_canvas_draw_over_viewport(Control *p_overlay);
  83. void edit(Node *p_node);
  84. CollisionShape2DEditor(EditorNode *p_editor);
  85. };
  86. class CollisionShape2DEditorPlugin : public EditorPlugin {
  87. GDCLASS(CollisionShape2DEditorPlugin, EditorPlugin);
  88. CollisionShape2DEditor *collision_shape_2d_editor;
  89. EditorNode *editor;
  90. public:
  91. virtual bool forward_canvas_gui_input(const Ref<InputEvent> &p_event) { return collision_shape_2d_editor->forward_canvas_gui_input(p_event); }
  92. virtual void forward_canvas_draw_over_viewport(Control *p_overlay) { collision_shape_2d_editor->forward_canvas_draw_over_viewport(p_overlay); }
  93. virtual String get_name() const { return "CollisionShape2D"; }
  94. bool has_main_screen() const { return false; }
  95. virtual void edit(Object *p_obj);
  96. virtual bool handles(Object *p_obj) const;
  97. virtual void make_visible(bool visible);
  98. CollisionShape2DEditorPlugin(EditorNode *p_editor);
  99. ~CollisionShape2DEditorPlugin();
  100. };
  101. #endif // COLLISION_SHAPE_2D_EDITOR_PLUGIN_H