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