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