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