grid_map_editor_plugin.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /**************************************************************************/
  2. /* grid_map_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. #pragma once
  31. #include "../grid_map.h"
  32. #include "editor/plugins/editor_plugin.h"
  33. #include "scene/gui/box_container.h"
  34. #include "scene/gui/item_list.h"
  35. #include "scene/gui/slider.h"
  36. #include "scene/gui/spin_box.h"
  37. class ConfirmationDialog;
  38. class MenuButton;
  39. class Node3DEditorPlugin;
  40. class ButtonGroup;
  41. class EditorZoomWidget;
  42. class BaseButton;
  43. class GridMapEditor : public VBoxContainer {
  44. GDCLASS(GridMapEditor, VBoxContainer);
  45. static constexpr int32_t GRID_CURSOR_SIZE = 50;
  46. enum InputAction {
  47. INPUT_NONE,
  48. INPUT_TRANSFORM,
  49. INPUT_PAINT,
  50. INPUT_ERASE,
  51. INPUT_PICK,
  52. INPUT_SELECT,
  53. INPUT_PASTE,
  54. };
  55. enum DisplayMode {
  56. DISPLAY_THUMBNAIL,
  57. DISPLAY_LIST
  58. };
  59. InputAction input_action = INPUT_NONE;
  60. Panel *panel = nullptr;
  61. MenuButton *options = nullptr;
  62. SpinBox *floor = nullptr;
  63. double accumulated_floor_delta = 0.0;
  64. HBoxContainer *toolbar = nullptr;
  65. TightLocalVector<BaseButton *> viewport_shortcut_buttons;
  66. Ref<ButtonGroup> mode_buttons_group;
  67. // mode
  68. Button *transform_mode_button = nullptr;
  69. Button *select_mode_button = nullptr;
  70. Button *erase_mode_button = nullptr;
  71. Button *paint_mode_button = nullptr;
  72. Button *pick_mode_button = nullptr;
  73. // action
  74. Button *fill_action_button = nullptr;
  75. Button *move_action_button = nullptr;
  76. Button *duplicate_action_button = nullptr;
  77. Button *delete_action_button = nullptr;
  78. // rotation
  79. Button *rotate_x_button = nullptr;
  80. Button *rotate_y_button = nullptr;
  81. Button *rotate_z_button = nullptr;
  82. EditorZoomWidget *zoom_widget = nullptr;
  83. Button *mode_thumbnail = nullptr;
  84. Button *mode_list = nullptr;
  85. LineEdit *search_box = nullptr;
  86. HSlider *size_slider = nullptr;
  87. ConfirmationDialog *settings_dialog = nullptr;
  88. VBoxContainer *settings_vbc = nullptr;
  89. SpinBox *settings_pick_distance = nullptr;
  90. Label *spin_box_label = nullptr;
  91. struct SetItem {
  92. Vector3i position;
  93. int new_value = 0;
  94. int new_orientation = 0;
  95. int old_value = 0;
  96. int old_orientation = 0;
  97. };
  98. LocalVector<SetItem> set_items;
  99. GridMap *node = nullptr;
  100. Ref<MeshLibrary> mesh_library = nullptr;
  101. Transform3D grid_xform;
  102. Transform3D edit_grid_xform;
  103. Vector3::Axis edit_axis;
  104. int edit_floor[3];
  105. Vector3 grid_ofs;
  106. RID grid[3];
  107. RID grid_instance[3];
  108. RID cursor_mesh;
  109. RID cursor_instance;
  110. RID selection_mesh;
  111. RID selection_instance;
  112. RID selection_level_mesh[3];
  113. RID selection_level_instance[3];
  114. RID paste_mesh;
  115. RID paste_instance;
  116. struct ClipboardItem {
  117. int cell_item = 0;
  118. Vector3 grid_offset;
  119. int orientation = 0;
  120. RID instance;
  121. };
  122. LocalVector<ClipboardItem> clipboard_items;
  123. bool clipboard_is_move = false;
  124. Color default_color;
  125. Color erase_color;
  126. Color pick_color;
  127. Ref<StandardMaterial3D> indicator_mat;
  128. Ref<StandardMaterial3D> cursor_inner_mat;
  129. Ref<StandardMaterial3D> cursor_outer_mat;
  130. Ref<StandardMaterial3D> inner_mat;
  131. Ref<StandardMaterial3D> outer_mat;
  132. Ref<StandardMaterial3D> selection_floor_mat;
  133. bool updating = false;
  134. struct Selection {
  135. Vector3 click;
  136. Vector3 current;
  137. Vector3 begin;
  138. Vector3 end;
  139. bool active = false;
  140. } selection;
  141. Selection last_selection;
  142. struct PasteIndicator {
  143. Vector3 click;
  144. Vector3 current;
  145. Vector3 begin;
  146. Vector3 end;
  147. Vector3 distance_from_cursor;
  148. int orientation = 0;
  149. };
  150. PasteIndicator paste_indicator;
  151. bool cursor_visible = false;
  152. Transform3D cursor_transform;
  153. Vector3 cursor_origin;
  154. Vector3i cursor_gridpos;
  155. int display_mode = DISPLAY_THUMBNAIL;
  156. int selected_palette = -1;
  157. int cursor_rot = 0;
  158. enum Menu {
  159. MENU_OPTION_NEXT_LEVEL,
  160. MENU_OPTION_PREV_LEVEL,
  161. MENU_OPTION_LOCK_VIEW,
  162. MENU_OPTION_X_AXIS,
  163. MENU_OPTION_Y_AXIS,
  164. MENU_OPTION_Z_AXIS,
  165. MENU_OPTION_CURSOR_ROTATE_Y,
  166. MENU_OPTION_CURSOR_ROTATE_X,
  167. MENU_OPTION_CURSOR_ROTATE_Z,
  168. MENU_OPTION_CURSOR_BACK_ROTATE_Y,
  169. MENU_OPTION_CURSOR_BACK_ROTATE_X,
  170. MENU_OPTION_CURSOR_BACK_ROTATE_Z,
  171. MENU_OPTION_CURSOR_CLEAR_ROTATION,
  172. MENU_OPTION_PASTE_SELECTS,
  173. MENU_OPTION_SELECTION_DUPLICATE,
  174. MENU_OPTION_SELECTION_MOVE,
  175. MENU_OPTION_SELECTION_CLEAR,
  176. MENU_OPTION_SELECTION_FILL,
  177. MENU_OPTION_GRIDMAP_SETTINGS
  178. };
  179. Node3DEditorPlugin *spatial_editor = nullptr;
  180. struct AreaDisplay {
  181. RID mesh;
  182. RID instance;
  183. };
  184. ItemList *mesh_library_palette = nullptr;
  185. Label *info_message = nullptr;
  186. void update_grid(); // Change which and where the grid is displayed
  187. void _draw_grids(const Vector3 &cell_size);
  188. void _configure();
  189. void _menu_option(int);
  190. void update_palette();
  191. void _update_mesh_library();
  192. void _set_display_mode(int p_mode);
  193. void _item_selected_cbk(int idx);
  194. void _update_cursor_transform();
  195. void _update_cursor_instance();
  196. void _on_tool_mode_changed();
  197. void _update_theme();
  198. void _text_changed(const String &p_text);
  199. void _sbox_input(const Ref<InputEvent> &p_event);
  200. void _mesh_library_palette_input(const Ref<InputEvent> &p_ie);
  201. void _icon_size_changed(float p_value);
  202. void _clear_clipboard_data();
  203. void _set_clipboard_data();
  204. void _update_paste_indicator();
  205. void _do_paste();
  206. void _cancel_pending_move();
  207. void _show_viewports_transform_gizmo(bool p_value);
  208. void _update_selection_transform();
  209. void _validate_selection();
  210. void _set_selection(bool p_active, const Vector3 &p_begin = Vector3(), const Vector3 &p_end = Vector3());
  211. AABB _get_selection() const;
  212. bool _has_selection() const;
  213. Array _get_selected_cells() const;
  214. void _floor_changed(float p_value);
  215. void _floor_mouse_exited();
  216. void _delete_selection();
  217. void _delete_selection_with_undo();
  218. void _fill_selection();
  219. void _setup_paste_mode();
  220. bool do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click);
  221. friend class GridMapEditorPlugin;
  222. protected:
  223. void _notification(int p_what);
  224. static void _bind_methods();
  225. public:
  226. EditorPlugin::AfterGUIInput forward_spatial_input_event(Camera3D *p_camera, const Ref<InputEvent> &p_event);
  227. void edit(GridMap *p_gridmap);
  228. GridMapEditor();
  229. ~GridMapEditor();
  230. };
  231. class GridMapEditorPlugin : public EditorPlugin {
  232. GDCLASS(GridMapEditorPlugin, EditorPlugin);
  233. GridMapEditor *grid_map_editor = nullptr;
  234. Button *panel_button = nullptr;
  235. protected:
  236. void _notification(int p_what);
  237. static void _bind_methods();
  238. public:
  239. virtual EditorPlugin::AfterGUIInput forward_3d_gui_input(Camera3D *p_camera, const Ref<InputEvent> &p_event) override { return grid_map_editor->forward_spatial_input_event(p_camera, p_event); }
  240. virtual String get_plugin_name() const override { return "GridMap"; }
  241. bool has_main_screen() const override { return false; }
  242. virtual void edit(Object *p_object) override;
  243. virtual bool handles(Object *p_object) const override;
  244. virtual void make_visible(bool p_visible) override;
  245. GridMap *get_current_grid_map() const;
  246. void set_selection(const Vector3i &p_begin, const Vector3i &p_end);
  247. void clear_selection();
  248. AABB get_selection() const;
  249. bool has_selection() const;
  250. Array get_selected_cells() const;
  251. void set_selected_palette_item(int p_item) const;
  252. int get_selected_palette_item() const;
  253. };