grid_map_editor_plugin.h 8.4 KB

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