1 /*************************************************************************/
2 /*  grid_map_editor_plugin.cpp                                           */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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 #include "grid_map_editor_plugin.h"
32 #include "core/os/input.h"
33 #include "editor/editor_scale.h"
34 #include "editor/editor_settings.h"
35 #include "editor/plugins/spatial_editor_plugin.h"
36 #include "scene/3d/camera.h"
37 
38 #include "core/math/geometry.h"
39 #include "core/os/keyboard.h"
40 
_node_removed(Node * p_node)41 void GridMapEditor::_node_removed(Node *p_node) {
42 
43 	if (p_node == node)
44 		node = NULL;
45 }
46 
_configure()47 void GridMapEditor::_configure() {
48 
49 	if (!node)
50 		return;
51 
52 	update_grid();
53 }
54 
_menu_option(int p_option)55 void GridMapEditor::_menu_option(int p_option) {
56 
57 	switch (p_option) {
58 
59 		case MENU_OPTION_PREV_LEVEL: {
60 			floor->set_value(floor->get_value() - 1);
61 		} break;
62 
63 		case MENU_OPTION_NEXT_LEVEL: {
64 			floor->set_value(floor->get_value() + 1);
65 		} break;
66 
67 		case MENU_OPTION_LOCK_VIEW: {
68 
69 			int index = options->get_popup()->get_item_index(MENU_OPTION_LOCK_VIEW);
70 			lock_view = !options->get_popup()->is_item_checked(index);
71 
72 			options->get_popup()->set_item_checked(index, lock_view);
73 		} break;
74 		case MENU_OPTION_CLIP_DISABLED:
75 		case MENU_OPTION_CLIP_ABOVE:
76 		case MENU_OPTION_CLIP_BELOW: {
77 
78 			clip_mode = ClipMode(p_option - MENU_OPTION_CLIP_DISABLED);
79 			for (int i = 0; i < 3; i++) {
80 
81 				int index = options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED + i);
82 				options->get_popup()->set_item_checked(index, i == clip_mode);
83 			}
84 
85 			_update_clip();
86 		} break;
87 		case MENU_OPTION_X_AXIS:
88 		case MENU_OPTION_Y_AXIS:
89 		case MENU_OPTION_Z_AXIS: {
90 
91 			int new_axis = p_option - MENU_OPTION_X_AXIS;
92 			for (int i = 0; i < 3; i++) {
93 				int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i);
94 				options->get_popup()->set_item_checked(idx, i == new_axis);
95 			}
96 
97 			if (edit_axis != new_axis) {
98 				int item1 = options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL);
99 				int item2 = options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL);
100 				if (edit_axis == Vector3::AXIS_Y) {
101 					options->get_popup()->set_item_text(item1, TTR("Next Plane"));
102 					options->get_popup()->set_item_text(item2, TTR("Previous Plane"));
103 					spin_box_label->set_text(TTR("Plane:"));
104 				} else if (new_axis == Vector3::AXIS_Y) {
105 					options->get_popup()->set_item_text(item1, TTR("Next Floor"));
106 					options->get_popup()->set_item_text(item2, TTR("Previous Floor"));
107 					spin_box_label->set_text(TTR("Floor:"));
108 				}
109 			}
110 			edit_axis = Vector3::Axis(new_axis);
111 			update_grid();
112 			_update_clip();
113 
114 		} break;
115 		case MENU_OPTION_CURSOR_ROTATE_Y: {
116 
117 			Basis r;
118 			if (input_action == INPUT_PASTE) {
119 
120 				r.set_orthogonal_index(paste_indicator.orientation);
121 				r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
122 				paste_indicator.orientation = r.get_orthogonal_index();
123 				_update_paste_indicator();
124 				break;
125 			}
126 
127 			r.set_orthogonal_index(cursor_rot);
128 			r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
129 			cursor_rot = r.get_orthogonal_index();
130 			_update_cursor_transform();
131 		} break;
132 		case MENU_OPTION_CURSOR_ROTATE_X: {
133 
134 			Basis r;
135 			if (input_action == INPUT_PASTE) {
136 
137 				r.set_orthogonal_index(paste_indicator.orientation);
138 				r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
139 				paste_indicator.orientation = r.get_orthogonal_index();
140 				_update_paste_indicator();
141 				break;
142 			}
143 
144 			r.set_orthogonal_index(cursor_rot);
145 			r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
146 			cursor_rot = r.get_orthogonal_index();
147 			_update_cursor_transform();
148 		} break;
149 		case MENU_OPTION_CURSOR_ROTATE_Z: {
150 
151 			Basis r;
152 			if (input_action == INPUT_PASTE) {
153 
154 				r.set_orthogonal_index(paste_indicator.orientation);
155 				r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
156 				paste_indicator.orientation = r.get_orthogonal_index();
157 				_update_paste_indicator();
158 				break;
159 			}
160 
161 			r.set_orthogonal_index(cursor_rot);
162 			r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
163 			cursor_rot = r.get_orthogonal_index();
164 			_update_cursor_transform();
165 		} break;
166 		case MENU_OPTION_CURSOR_BACK_ROTATE_Y: {
167 
168 			Basis r;
169 			if (input_action == INPUT_PASTE) {
170 
171 				r.set_orthogonal_index(paste_indicator.orientation);
172 				r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
173 				paste_indicator.orientation = r.get_orthogonal_index();
174 				_update_paste_indicator();
175 				break;
176 			}
177 
178 			r.set_orthogonal_index(cursor_rot);
179 			r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
180 			cursor_rot = r.get_orthogonal_index();
181 			_update_cursor_transform();
182 		} break;
183 		case MENU_OPTION_CURSOR_BACK_ROTATE_X: {
184 
185 			Basis r;
186 			if (input_action == INPUT_PASTE) {
187 
188 				r.set_orthogonal_index(paste_indicator.orientation);
189 				r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
190 				paste_indicator.orientation = r.get_orthogonal_index();
191 				_update_paste_indicator();
192 				break;
193 			}
194 
195 			r.set_orthogonal_index(cursor_rot);
196 			r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
197 			cursor_rot = r.get_orthogonal_index();
198 			_update_cursor_transform();
199 		} break;
200 		case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
201 
202 			Basis r;
203 			if (input_action == INPUT_PASTE) {
204 
205 				r.set_orthogonal_index(paste_indicator.orientation);
206 				r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
207 				paste_indicator.orientation = r.get_orthogonal_index();
208 				_update_paste_indicator();
209 				break;
210 			}
211 
212 			r.set_orthogonal_index(cursor_rot);
213 			r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
214 			cursor_rot = r.get_orthogonal_index();
215 			_update_cursor_transform();
216 		} break;
217 		case MENU_OPTION_CURSOR_CLEAR_ROTATION: {
218 
219 			if (input_action == INPUT_PASTE) {
220 
221 				paste_indicator.orientation = 0;
222 				_update_paste_indicator();
223 				break;
224 			}
225 
226 			cursor_rot = 0;
227 			_update_cursor_transform();
228 		} break;
229 
230 		case MENU_OPTION_PASTE_SELECTS: {
231 			int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
232 			options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
233 		} break;
234 
235 		case MENU_OPTION_SELECTION_DUPLICATE:
236 		case MENU_OPTION_SELECTION_CUT: {
237 			if (!(selection.active && input_action == INPUT_NONE))
238 				break;
239 
240 			_set_clipboard_data();
241 
242 			if (p_option == MENU_OPTION_SELECTION_CUT) {
243 				_delete_selection();
244 			}
245 
246 			input_action = INPUT_PASTE;
247 			paste_indicator.click = selection.begin;
248 			paste_indicator.current = selection.begin;
249 			paste_indicator.begin = selection.begin;
250 			paste_indicator.end = selection.end;
251 			paste_indicator.orientation = 0;
252 			_update_paste_indicator();
253 		} break;
254 		case MENU_OPTION_SELECTION_CLEAR: {
255 			if (!selection.active)
256 				break;
257 
258 			_delete_selection();
259 
260 		} break;
261 		case MENU_OPTION_SELECTION_FILL: {
262 			if (!selection.active)
263 				return;
264 
265 			_fill_selection();
266 
267 		} break;
268 		case MENU_OPTION_GRIDMAP_SETTINGS: {
269 			settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE);
270 		} break;
271 	}
272 }
273 
_update_cursor_transform()274 void GridMapEditor::_update_cursor_transform() {
275 
276 	cursor_transform = Transform();
277 	cursor_transform.origin = cursor_origin;
278 	cursor_transform.basis.set_orthogonal_index(cursor_rot);
279 	cursor_transform.basis *= node->get_cell_scale();
280 	cursor_transform = node->get_global_transform() * cursor_transform;
281 
282 	if (cursor_instance.is_valid()) {
283 		VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
284 		VisualServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
285 	}
286 }
287 
_update_selection_transform()288 void GridMapEditor::_update_selection_transform() {
289 	Transform xf_zero;
290 	xf_zero.basis.set_zero();
291 
292 	if (!selection.active) {
293 
294 		VisualServer::get_singleton()->instance_set_transform(selection_instance, xf_zero);
295 		for (int i = 0; i < 3; i++) {
296 			VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
297 		}
298 		return;
299 	}
300 
301 	Transform xf;
302 	xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
303 	xf.origin = selection.begin * node->get_cell_size();
304 
305 	VisualServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
306 
307 	for (int i = 0; i < 3; i++) {
308 		if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) {
309 			VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
310 		} else {
311 
312 			Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
313 			scale[edit_axis] = 1.0;
314 			Vector3 pos = selection.begin;
315 			pos[edit_axis] = edit_floor[edit_axis];
316 
317 			scale *= node->get_cell_size();
318 			pos *= node->get_cell_size();
319 
320 			Transform xf2;
321 			xf2.basis.scale(scale);
322 			xf2.origin = pos;
323 
324 			VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2);
325 		}
326 	}
327 }
328 
_validate_selection()329 void GridMapEditor::_validate_selection() {
330 
331 	if (!selection.active)
332 		return;
333 	selection.begin = selection.click;
334 	selection.end = selection.current;
335 
336 	if (selection.begin.x > selection.end.x)
337 		SWAP(selection.begin.x, selection.end.x);
338 	if (selection.begin.y > selection.end.y)
339 		SWAP(selection.begin.y, selection.end.y);
340 	if (selection.begin.z > selection.end.z)
341 		SWAP(selection.begin.z, selection.end.z);
342 
343 	_update_selection_transform();
344 }
345 
_set_selection(bool p_active,const Vector3 & p_begin,const Vector3 & p_end)346 void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) {
347 
348 	selection.active = p_active;
349 	selection.begin = p_begin;
350 	selection.end = p_end;
351 	selection.click = p_begin;
352 	selection.current = p_end;
353 
354 	if (is_visible_in_tree()) {
355 		_update_selection_transform();
356 	}
357 
358 	options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CLEAR), !selection.active);
359 	options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CUT), !selection.active);
360 	options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_DUPLICATE), !selection.active);
361 	options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_FILL), !selection.active);
362 }
363 
do_input_action(Camera * p_camera,const Point2 & p_point,bool p_click)364 bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click) {
365 
366 	if (!spatial_editor)
367 		return false;
368 
369 	if (selected_palette < 0 && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE)
370 		return false;
371 	Ref<MeshLibrary> mesh_library = node->get_mesh_library();
372 	if (mesh_library.is_null())
373 		return false;
374 	if (input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette))
375 		return false;
376 
377 	Camera *camera = p_camera;
378 	Vector3 from = camera->project_ray_origin(p_point);
379 	Vector3 normal = camera->project_ray_normal(p_point);
380 	Transform local_xform = node->get_global_transform().affine_inverse();
381 	Vector<Plane> planes = camera->get_frustum();
382 	from = local_xform.xform(from);
383 	normal = local_xform.basis.xform(normal).normalized();
384 
385 	Plane p;
386 	p.normal[edit_axis] = 1.0;
387 	p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
388 
389 	Vector3 inters;
390 	if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters))
391 		return false;
392 
393 	// Make sure the intersection is inside the frustum planes, to avoid
394 	// Painting on invisible regions.
395 	for (int i = 0; i < planes.size(); i++) {
396 
397 		Plane fp = local_xform.xform(planes[i]);
398 		if (fp.is_point_over(inters))
399 			return false;
400 	}
401 
402 	int cell[3];
403 	float cell_size[3] = { node->get_cell_size().x, node->get_cell_size().y, node->get_cell_size().z };
404 
405 	for (int i = 0; i < 3; i++) {
406 
407 		if (i == edit_axis)
408 			cell[i] = edit_floor[i];
409 		else {
410 
411 			cell[i] = inters[i] / node->get_cell_size()[i];
412 			if (inters[i] < 0)
413 				cell[i] -= 1; // Compensate negative.
414 			grid_ofs[i] = cell[i] * cell_size[i];
415 		}
416 	}
417 
418 	VS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform);
419 
420 	if (cursor_instance.is_valid()) {
421 
422 		cursor_origin = (Vector3(cell[0], cell[1], cell[2]) + Vector3(0.5 * node->get_center_x(), 0.5 * node->get_center_y(), 0.5 * node->get_center_z())) * node->get_cell_size();
423 		cursor_visible = true;
424 
425 		if (input_action == INPUT_SELECT || input_action == INPUT_PASTE) {
426 			cursor_visible = false;
427 		}
428 
429 		_update_cursor_transform();
430 	}
431 
432 	if (input_action == INPUT_PASTE) {
433 
434 		paste_indicator.current = Vector3(cell[0], cell[1], cell[2]);
435 		_update_paste_indicator();
436 
437 	} else if (input_action == INPUT_SELECT) {
438 
439 		selection.current = Vector3(cell[0], cell[1], cell[2]);
440 		if (p_click)
441 			selection.click = selection.current;
442 		selection.active = true;
443 		_validate_selection();
444 
445 		return true;
446 	} else if (input_action == INPUT_PICK) {
447 
448 		int item = node->get_cell_item(cell[0], cell[1], cell[2]);
449 		if (item >= 0) {
450 			selected_palette = item;
451 			mesh_library_palette->set_current(item);
452 			update_palette();
453 			_update_cursor_instance();
454 		}
455 		return true;
456 	}
457 	if (input_action == INPUT_PAINT) {
458 		SetItem si;
459 		si.pos = Vector3(cell[0], cell[1], cell[2]);
460 		si.new_value = selected_palette;
461 		si.new_orientation = cursor_rot;
462 		si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
463 		si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
464 		set_items.push_back(si);
465 		node->set_cell_item(cell[0], cell[1], cell[2], selected_palette, cursor_rot);
466 		return true;
467 	} else if (input_action == INPUT_ERASE) {
468 		SetItem si;
469 		si.pos = Vector3(cell[0], cell[1], cell[2]);
470 		si.new_value = -1;
471 		si.new_orientation = 0;
472 		si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
473 		si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
474 		set_items.push_back(si);
475 		node->set_cell_item(cell[0], cell[1], cell[2], -1);
476 		return true;
477 	}
478 
479 	return false;
480 }
481 
_delete_selection()482 void GridMapEditor::_delete_selection() {
483 
484 	if (!selection.active)
485 		return;
486 
487 	undo_redo->create_action(TTR("GridMap Delete Selection"));
488 	for (int i = selection.begin.x; i <= selection.end.x; i++) {
489 
490 		for (int j = selection.begin.y; j <= selection.end.y; j++) {
491 
492 			for (int k = selection.begin.z; k <= selection.end.z; k++) {
493 
494 				undo_redo->add_do_method(node, "set_cell_item", i, j, k, GridMap::INVALID_CELL_ITEM);
495 				undo_redo->add_undo_method(node, "set_cell_item", i, j, k, node->get_cell_item(i, j, k), node->get_cell_item_orientation(i, j, k));
496 			}
497 		}
498 	}
499 	undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
500 	undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
501 	undo_redo->commit_action();
502 }
503 
_fill_selection()504 void GridMapEditor::_fill_selection() {
505 
506 	if (!selection.active)
507 		return;
508 
509 	undo_redo->create_action(TTR("GridMap Fill Selection"));
510 	for (int i = selection.begin.x; i <= selection.end.x; i++) {
511 
512 		for (int j = selection.begin.y; j <= selection.end.y; j++) {
513 
514 			for (int k = selection.begin.z; k <= selection.end.z; k++) {
515 
516 				undo_redo->add_do_method(node, "set_cell_item", i, j, k, selected_palette, cursor_rot);
517 				undo_redo->add_undo_method(node, "set_cell_item", i, j, k, node->get_cell_item(i, j, k), node->get_cell_item_orientation(i, j, k));
518 			}
519 		}
520 	}
521 	undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
522 	undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
523 	undo_redo->commit_action();
524 }
525 
_clear_clipboard_data()526 void GridMapEditor::_clear_clipboard_data() {
527 
528 	for (List<ClipboardItem>::Element *E = clipboard_items.front(); E; E = E->next()) {
529 
530 		VisualServer::get_singleton()->free(E->get().instance);
531 	}
532 
533 	clipboard_items.clear();
534 }
535 
_set_clipboard_data()536 void GridMapEditor::_set_clipboard_data() {
537 
538 	_clear_clipboard_data();
539 
540 	Ref<MeshLibrary> meshLibrary = node->get_mesh_library();
541 
542 	for (int i = selection.begin.x; i <= selection.end.x; i++) {
543 
544 		for (int j = selection.begin.y; j <= selection.end.y; j++) {
545 
546 			for (int k = selection.begin.z; k <= selection.end.z; k++) {
547 
548 				int itm = node->get_cell_item(i, j, k);
549 				if (itm == GridMap::INVALID_CELL_ITEM)
550 					continue;
551 
552 				Ref<Mesh> mesh = meshLibrary->get_item_mesh(itm);
553 
554 				ClipboardItem item;
555 				item.cell_item = itm;
556 				item.grid_offset = Vector3(i, j, k) - selection.begin;
557 				item.orientation = node->get_cell_item_orientation(i, j, k);
558 				item.instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
559 
560 				clipboard_items.push_back(item);
561 			}
562 		}
563 	}
564 }
565 
_update_paste_indicator()566 void GridMapEditor::_update_paste_indicator() {
567 
568 	if (input_action != INPUT_PASTE) {
569 
570 		Transform xf;
571 		xf.basis.set_zero();
572 		VisualServer::get_singleton()->instance_set_transform(paste_instance, xf);
573 		return;
574 	}
575 
576 	Vector3 center = 0.5 * Vector3(float(node->get_center_x()), float(node->get_center_y()), float(node->get_center_z()));
577 	Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size();
578 	Transform xf;
579 	xf.scale(scale);
580 	xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
581 	Basis rot;
582 	rot.set_orthogonal_index(paste_indicator.orientation);
583 	xf.basis = rot * xf.basis;
584 	xf.translate((-center * node->get_cell_size()) / scale);
585 
586 	VisualServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf);
587 
588 	for (List<ClipboardItem>::Element *E = clipboard_items.front(); E; E = E->next()) {
589 
590 		ClipboardItem &item = E->get();
591 
592 		xf = Transform();
593 		xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
594 		xf.basis = rot * xf.basis;
595 		xf.translate(item.grid_offset * node->get_cell_size());
596 
597 		Basis item_rot;
598 		item_rot.set_orthogonal_index(item.orientation);
599 		xf.basis = item_rot * xf.basis * node->get_cell_scale();
600 
601 		VisualServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf);
602 	}
603 }
604 
_do_paste()605 void GridMapEditor::_do_paste() {
606 
607 	int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
608 	bool reselect = options->get_popup()->is_item_checked(idx);
609 
610 	Basis rot;
611 	rot.set_orthogonal_index(paste_indicator.orientation);
612 
613 	Vector3 ofs = paste_indicator.current - paste_indicator.click;
614 	undo_redo->create_action(TTR("GridMap Paste Selection"));
615 
616 	for (List<ClipboardItem>::Element *E = clipboard_items.front(); E; E = E->next()) {
617 
618 		ClipboardItem &item = E->get();
619 
620 		Vector3 pos = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;
621 
622 		Basis orm;
623 		orm.set_orthogonal_index(item.orientation);
624 		orm = rot * orm;
625 
626 		undo_redo->add_do_method(node, "set_cell_item", pos.x, pos.y, pos.z, item.cell_item, orm.get_orthogonal_index());
627 		undo_redo->add_undo_method(node, "set_cell_item", pos.x, pos.y, pos.z, node->get_cell_item(pos.x, pos.y, pos.z), node->get_cell_item_orientation(pos.x, pos.y, pos.z));
628 	}
629 
630 	if (reselect) {
631 
632 		undo_redo->add_do_method(this, "_set_selection", true, paste_indicator.begin + ofs, paste_indicator.end + ofs);
633 		undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
634 	}
635 
636 	undo_redo->commit_action();
637 
638 	_clear_clipboard_data();
639 }
640 
forward_spatial_input_event(Camera * p_camera,const Ref<InputEvent> & p_event)641 bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const Ref<InputEvent> &p_event) {
642 	if (!node) {
643 		return false;
644 	}
645 
646 	Ref<InputEventMouseButton> mb = p_event;
647 
648 	if (mb.is_valid()) {
649 
650 		if (mb->get_button_index() == BUTTON_WHEEL_UP && (mb->get_command() || mb->get_shift())) {
651 			if (mb->is_pressed())
652 				floor->set_value(floor->get_value() + mb->get_factor());
653 
654 			return true; // Eaten.
655 		} else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && (mb->get_command() || mb->get_shift())) {
656 			if (mb->is_pressed())
657 				floor->set_value(floor->get_value() - mb->get_factor());
658 			return true;
659 		}
660 
661 		if (mb->is_pressed()) {
662 			SpatialEditorViewport::NavigationScheme nav_scheme = (SpatialEditorViewport::NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
663 			if ((nav_scheme == SpatialEditorViewport::NAVIGATION_MAYA || nav_scheme == SpatialEditorViewport::NAVIGATION_MODO) && mb->get_alt()) {
664 				input_action = INPUT_NONE;
665 			} else if (mb->get_button_index() == BUTTON_LEFT) {
666 
667 				bool can_edit = (node && node->get_mesh_library().is_valid());
668 				if (input_action == INPUT_PASTE) {
669 					_do_paste();
670 					input_action = INPUT_NONE;
671 					_update_paste_indicator();
672 				} else if (mb->get_shift() && can_edit) {
673 					input_action = INPUT_SELECT;
674 					last_selection = selection;
675 				} else if (mb->get_command() && can_edit) {
676 					input_action = INPUT_PICK;
677 				} else {
678 					input_action = INPUT_PAINT;
679 					set_items.clear();
680 				}
681 			} else if (mb->get_button_index() == BUTTON_RIGHT) {
682 				if (input_action == INPUT_PASTE) {
683 					_clear_clipboard_data();
684 					input_action = INPUT_NONE;
685 					_update_paste_indicator();
686 					return true;
687 				} else if (selection.active) {
688 					_set_selection(false);
689 					return true;
690 				} else {
691 					input_action = INPUT_ERASE;
692 					set_items.clear();
693 				}
694 			} else {
695 				return false;
696 			}
697 
698 			return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true);
699 		} else {
700 
701 			if ((mb->get_button_index() == BUTTON_RIGHT && input_action == INPUT_ERASE) || (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_PAINT)) {
702 
703 				if (set_items.size()) {
704 					undo_redo->create_action(TTR("GridMap Paint"));
705 					for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
706 
707 						const SetItem &si = E->get();
708 						undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
709 					}
710 					for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
711 
712 						const SetItem &si = E->get();
713 						undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
714 					}
715 
716 					undo_redo->commit_action();
717 				}
718 				set_items.clear();
719 				input_action = INPUT_NONE;
720 				return set_items.size() > 0;
721 			}
722 
723 			if (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_SELECT) {
724 
725 				undo_redo->create_action("GridMap Selection");
726 				undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end);
727 				undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end);
728 				undo_redo->commit_action();
729 			}
730 
731 			if (mb->get_button_index() == BUTTON_LEFT && input_action != INPUT_NONE) {
732 
733 				set_items.clear();
734 				input_action = INPUT_NONE;
735 				return true;
736 			}
737 			if (mb->get_button_index() == BUTTON_RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) {
738 				input_action = INPUT_NONE;
739 				return true;
740 			}
741 		}
742 	}
743 
744 	Ref<InputEventMouseMotion> mm = p_event;
745 
746 	if (mm.is_valid()) {
747 
748 		return do_input_action(p_camera, mm->get_position(), false);
749 	}
750 
751 	Ref<InputEventKey> k = p_event;
752 
753 	if (k.is_valid()) {
754 		if (k->is_pressed()) {
755 			if (k->get_scancode() == KEY_ESCAPE) {
756 
757 				if (input_action == INPUT_PASTE) {
758 					_clear_clipboard_data();
759 					input_action = INPUT_NONE;
760 					_update_paste_indicator();
761 					return true;
762 				} else if (selection.active) {
763 					_set_selection(false);
764 					return true;
765 				} else {
766 					selected_palette = -1;
767 					mesh_library_palette->unselect_all();
768 					update_palette();
769 					_update_cursor_instance();
770 					return true;
771 				}
772 			}
773 
774 			if (k->get_shift() && selection.active && input_action != INPUT_PASTE) {
775 
776 				if (k->get_scancode() == options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL))) {
777 					selection.click[edit_axis]--;
778 					_validate_selection();
779 					return true;
780 				}
781 				if (k->get_scancode() == options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL))) {
782 					selection.click[edit_axis]++;
783 					_validate_selection();
784 					return true;
785 				}
786 			}
787 		}
788 	}
789 
790 	Ref<InputEventPanGesture> pan_gesture = p_event;
791 	if (pan_gesture.is_valid()) {
792 
793 		if (pan_gesture->get_alt() && (pan_gesture->get_command() || pan_gesture->get_shift())) {
794 			const real_t delta = pan_gesture->get_delta().y * 0.5;
795 			accumulated_floor_delta += delta;
796 			int step = 0;
797 			if (ABS(accumulated_floor_delta) > 1.0) {
798 				step = SGN(accumulated_floor_delta);
799 				accumulated_floor_delta -= step;
800 			}
801 			if (step) {
802 				floor->set_value(floor->get_value() + step);
803 			}
804 			return true;
805 		}
806 	}
807 	accumulated_floor_delta = 0.0;
808 
809 	return false;
810 }
811 
812 struct _CGMEItemSort {
813 
814 	String name;
815 	int id;
operator <_CGMEItemSort816 	_FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
817 };
818 
_set_display_mode(int p_mode)819 void GridMapEditor::_set_display_mode(int p_mode) {
820 	if (display_mode == p_mode) {
821 		return;
822 	}
823 
824 	if (p_mode == DISPLAY_LIST) {
825 		mode_list->set_pressed(true);
826 		mode_thumbnail->set_pressed(false);
827 	} else if (p_mode == DISPLAY_THUMBNAIL) {
828 		mode_list->set_pressed(false);
829 		mode_thumbnail->set_pressed(true);
830 	}
831 
832 	display_mode = p_mode;
833 
834 	update_palette();
835 }
836 
_text_changed(const String & p_text)837 void GridMapEditor::_text_changed(const String &p_text) {
838 	update_palette();
839 }
840 
_sbox_input(const Ref<InputEvent> & p_ie)841 void GridMapEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
842 
843 	const Ref<InputEventKey> k = p_ie;
844 
845 	if (k.is_valid() && (k->get_scancode() == KEY_UP || k->get_scancode() == KEY_DOWN || k->get_scancode() == KEY_PAGEUP || k->get_scancode() == KEY_PAGEDOWN)) {
846 
847 		// Forward the key input to the ItemList so it can be scrolled
848 		mesh_library_palette->call("_gui_input", k);
849 		search_box->accept_event();
850 	}
851 }
852 
_mesh_library_palette_input(const Ref<InputEvent> & p_ie)853 void GridMapEditor::_mesh_library_palette_input(const Ref<InputEvent> &p_ie) {
854 
855 	const Ref<InputEventMouseButton> mb = p_ie;
856 
857 	// Zoom in/out using Ctrl + mouse wheel
858 	if (mb.is_valid() && mb->is_pressed() && mb->get_command()) {
859 
860 		if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_UP) {
861 			size_slider->set_value(size_slider->get_value() + 0.2);
862 		}
863 
864 		if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_DOWN) {
865 			size_slider->set_value(size_slider->get_value() - 0.2);
866 		}
867 	}
868 }
869 
_icon_size_changed(float p_value)870 void GridMapEditor::_icon_size_changed(float p_value) {
871 	mesh_library_palette->set_icon_scale(p_value);
872 	update_palette();
873 }
874 
update_palette()875 void GridMapEditor::update_palette() {
876 	int selected = mesh_library_palette->get_current();
877 
878 	float min_size = EDITOR_DEF("editors/grid_map/preview_size", 64);
879 	min_size *= EDSCALE;
880 
881 	mesh_library_palette->clear();
882 	if (display_mode == DISPLAY_THUMBNAIL) {
883 		mesh_library_palette->set_max_columns(0);
884 		mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
885 		mesh_library_palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1.5));
886 	} else if (display_mode == DISPLAY_LIST) {
887 		mesh_library_palette->set_max_columns(1);
888 		mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT);
889 		mesh_library_palette->set_fixed_column_width(0);
890 	}
891 
892 	mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size));
893 	mesh_library_palette->set_max_text_lines(2);
894 
895 	Ref<MeshLibrary> mesh_library = node->get_mesh_library();
896 
897 	if (mesh_library.is_null()) {
898 		last_mesh_library = NULL;
899 		search_box->set_text("");
900 		search_box->set_editable(false);
901 		info_message->show();
902 		return;
903 	}
904 
905 	search_box->set_editable(true);
906 	info_message->hide();
907 
908 	Vector<int> ids;
909 	ids = mesh_library->get_item_list();
910 
911 	List<_CGMEItemSort> il;
912 	for (int i = 0; i < ids.size(); i++) {
913 
914 		_CGMEItemSort is;
915 		is.id = ids[i];
916 		is.name = mesh_library->get_item_name(ids[i]);
917 		il.push_back(is);
918 	}
919 	il.sort();
920 
921 	String filter = search_box->get_text().strip_edges();
922 
923 	int item = 0;
924 
925 	for (List<_CGMEItemSort>::Element *E = il.front(); E; E = E->next()) {
926 		int id = E->get().id;
927 		String name = mesh_library->get_item_name(id);
928 		Ref<Texture> preview = mesh_library->get_item_preview(id);
929 
930 		if (name == "") {
931 			name = "#" + itos(id);
932 		}
933 
934 		if (filter != "" && !filter.is_subsequence_ofi(name))
935 			continue;
936 
937 		mesh_library_palette->add_item("");
938 		if (!preview.is_null()) {
939 			mesh_library_palette->set_item_icon(item, preview);
940 			mesh_library_palette->set_item_tooltip(item, name);
941 		}
942 		mesh_library_palette->set_item_text(item, name);
943 		mesh_library_palette->set_item_metadata(item, id);
944 
945 		item++;
946 	}
947 
948 	if (selected != -1 && mesh_library_palette->get_item_count() > 0) {
949 		mesh_library_palette->select(selected);
950 	}
951 
952 	last_mesh_library = mesh_library.operator->();
953 }
954 
edit(GridMap * p_gridmap)955 void GridMapEditor::edit(GridMap *p_gridmap) {
956 	if (!p_gridmap && node)
957 		node->disconnect("cell_size_changed", this, "_draw_grids");
958 
959 	node = p_gridmap;
960 
961 	input_action = INPUT_NONE;
962 	selection.active = false;
963 	_update_selection_transform();
964 	_update_paste_indicator();
965 
966 	spatial_editor = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
967 
968 	if (!node) {
969 		set_process(false);
970 		for (int i = 0; i < 3; i++) {
971 			VisualServer::get_singleton()->instance_set_visible(grid_instance[i], false);
972 		}
973 
974 		if (cursor_instance.is_valid()) {
975 			VisualServer::get_singleton()->instance_set_visible(cursor_instance, false);
976 		}
977 
978 		return;
979 	}
980 
981 	update_palette();
982 
983 	set_process(true);
984 
985 	clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
986 
987 	_draw_grids(node->get_cell_size());
988 	update_grid();
989 	_update_clip();
990 
991 	node->connect("cell_size_changed", this, "_draw_grids");
992 }
993 
_update_clip()994 void GridMapEditor::_update_clip() {
995 
996 	node->set_meta("_editor_clip_", clip_mode);
997 	if (clip_mode == CLIP_DISABLED)
998 		node->set_clip(false);
999 	else
1000 		node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
1001 }
1002 
update_grid()1003 void GridMapEditor::update_grid() {
1004 
1005 	grid_xform.origin.x -= 1; // Force update in hackish way.
1006 
1007 	grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
1008 
1009 	edit_grid_xform.origin = grid_ofs;
1010 	edit_grid_xform.basis = Basis();
1011 
1012 	for (int i = 0; i < 3; i++) {
1013 		VisualServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
1014 	}
1015 
1016 	updating = true;
1017 	floor->set_value(edit_floor[edit_axis]);
1018 	updating = false;
1019 }
1020 
_draw_grids(const Vector3 & cell_size)1021 void GridMapEditor::_draw_grids(const Vector3 &cell_size) {
1022 	Vector3 edited_floor = node->has_meta("_editor_floor_") ? node->get_meta("_editor_floor_") : Variant();
1023 
1024 	for (int i = 0; i < 3; i++) {
1025 		if (VS::get_singleton()->mesh_get_surface_count(grid[i]) > 0)
1026 			VS::get_singleton()->mesh_remove_surface(grid[i], 0);
1027 		edit_floor[i] = edited_floor[i];
1028 	}
1029 
1030 	Vector<Vector3> grid_points[3];
1031 	Vector<Color> grid_colors[3];
1032 
1033 	for (int i = 0; i < 3; i++) {
1034 
1035 		Vector3 axis;
1036 		axis[i] = 1;
1037 		Vector3 axis_n1;
1038 		axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
1039 		Vector3 axis_n2;
1040 		axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
1041 
1042 		for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
1043 
1044 			for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
1045 
1046 				Vector3 p = axis_n1 * j + axis_n2 * k;
1047 				float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
1048 
1049 				Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
1050 				float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
1051 
1052 				Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
1053 				float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
1054 
1055 				grid_points[i].push_back(p);
1056 				grid_points[i].push_back(pk);
1057 				grid_colors[i].push_back(Color(1, 1, 1, trans));
1058 				grid_colors[i].push_back(Color(1, 1, 1, transk));
1059 
1060 				grid_points[i].push_back(p);
1061 				grid_points[i].push_back(pj);
1062 				grid_colors[i].push_back(Color(1, 1, 1, trans));
1063 				grid_colors[i].push_back(Color(1, 1, 1, transj));
1064 			}
1065 		}
1066 
1067 		Array d;
1068 		d.resize(VS::ARRAY_MAX);
1069 		d[VS::ARRAY_VERTEX] = grid_points[i];
1070 		d[VS::ARRAY_COLOR] = grid_colors[i];
1071 		VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
1072 		VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
1073 	}
1074 }
1075 
_notification(int p_what)1076 void GridMapEditor::_notification(int p_what) {
1077 
1078 	switch (p_what) {
1079 
1080 		case NOTIFICATION_ENTER_TREE: {
1081 			get_tree()->connect("node_removed", this, "_node_removed");
1082 			mesh_library_palette->connect("item_selected", this, "_item_selected_cbk");
1083 			for (int i = 0; i < 3; i++) {
1084 
1085 				grid[i] = VS::get_singleton()->mesh_create();
1086 				grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
1087 				selection_level_instance[i] = VisualServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world()->get_scenario());
1088 			}
1089 
1090 			selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
1091 			paste_instance = VisualServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world()->get_scenario());
1092 
1093 			_update_selection_transform();
1094 			_update_paste_indicator();
1095 		} break;
1096 
1097 		case NOTIFICATION_EXIT_TREE: {
1098 			get_tree()->disconnect("node_removed", this, "_node_removed");
1099 			_clear_clipboard_data();
1100 
1101 			for (int i = 0; i < 3; i++) {
1102 
1103 				VS::get_singleton()->free(grid_instance[i]);
1104 				VS::get_singleton()->free(grid[i]);
1105 				grid_instance[i] = RID();
1106 				grid[i] = RID();
1107 				VisualServer::get_singleton()->free(selection_level_instance[i]);
1108 			}
1109 
1110 			VisualServer::get_singleton()->free(selection_instance);
1111 			VisualServer::get_singleton()->free(paste_instance);
1112 			selection_instance = RID();
1113 			paste_instance = RID();
1114 		} break;
1115 
1116 		case NOTIFICATION_PROCESS: {
1117 			if (!node) {
1118 				return;
1119 			}
1120 
1121 			Transform xf = node->get_global_transform();
1122 
1123 			if (xf != grid_xform) {
1124 				for (int i = 0; i < 3; i++) {
1125 
1126 					VS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
1127 				}
1128 				grid_xform = xf;
1129 			}
1130 			Ref<MeshLibrary> cgmt = node->get_mesh_library();
1131 			if (cgmt.operator->() != last_mesh_library)
1132 				update_palette();
1133 
1134 			if (lock_view) {
1135 
1136 				EditorNode *editor = Object::cast_to<EditorNode>(get_tree()->get_root()->get_child(0));
1137 
1138 				Plane p;
1139 				p.normal[edit_axis] = 1.0;
1140 				p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
1141 				p = node->get_transform().xform(p); // plane to snap
1142 
1143 				SpatialEditorPlugin *sep = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
1144 				if (sep)
1145 					sep->snap_cursor_to_plane(p);
1146 			}
1147 		} break;
1148 
1149 		case NOTIFICATION_THEME_CHANGED: {
1150 			options->set_icon(get_icon("GridMap", "EditorIcons"));
1151 			search_box->set_right_icon(get_icon("Search", "EditorIcons"));
1152 		} break;
1153 	}
1154 }
1155 
_update_cursor_instance()1156 void GridMapEditor::_update_cursor_instance() {
1157 	if (!node) {
1158 		return;
1159 	}
1160 
1161 	if (cursor_instance.is_valid())
1162 		VisualServer::get_singleton()->free(cursor_instance);
1163 	cursor_instance = RID();
1164 
1165 	if (selected_palette >= 0) {
1166 
1167 		if (node && !node->get_mesh_library().is_null()) {
1168 			Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette);
1169 			if (!mesh.is_null() && mesh->get_rid().is_valid()) {
1170 
1171 				cursor_instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
1172 				VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
1173 			}
1174 		}
1175 	}
1176 }
1177 
_item_selected_cbk(int idx)1178 void GridMapEditor::_item_selected_cbk(int idx) {
1179 	selected_palette = mesh_library_palette->get_item_metadata(idx);
1180 
1181 	_update_cursor_instance();
1182 }
1183 
_floor_changed(float p_value)1184 void GridMapEditor::_floor_changed(float p_value) {
1185 
1186 	if (updating)
1187 		return;
1188 
1189 	edit_floor[edit_axis] = p_value;
1190 	node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
1191 	update_grid();
1192 	_update_clip();
1193 	_update_selection_transform();
1194 }
1195 
_floor_mouse_exited()1196 void GridMapEditor::_floor_mouse_exited() {
1197 	floor->get_line_edit()->release_focus();
1198 }
1199 
_bind_methods()1200 void GridMapEditor::_bind_methods() {
1201 
1202 	ClassDB::bind_method("_text_changed", &GridMapEditor::_text_changed);
1203 	ClassDB::bind_method("_sbox_input", &GridMapEditor::_sbox_input);
1204 	ClassDB::bind_method("_mesh_library_palette_input", &GridMapEditor::_mesh_library_palette_input);
1205 	ClassDB::bind_method("_icon_size_changed", &GridMapEditor::_icon_size_changed);
1206 	ClassDB::bind_method("_menu_option", &GridMapEditor::_menu_option);
1207 	ClassDB::bind_method("_configure", &GridMapEditor::_configure);
1208 	ClassDB::bind_method("_item_selected_cbk", &GridMapEditor::_item_selected_cbk);
1209 	ClassDB::bind_method("_floor_changed", &GridMapEditor::_floor_changed);
1210 	ClassDB::bind_method("_floor_mouse_exited", &GridMapEditor::_floor_mouse_exited);
1211 	ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection);
1212 	ClassDB::bind_method("_node_removed", &GridMapEditor::_node_removed);
1213 
1214 	ClassDB::bind_method(D_METHOD("_set_display_mode", "mode"), &GridMapEditor::_set_display_mode);
1215 	ClassDB::bind_method("_draw_grids", &GridMapEditor::_draw_grids);
1216 }
1217 
GridMapEditor(EditorNode * p_editor)1218 GridMapEditor::GridMapEditor(EditorNode *p_editor) {
1219 
1220 	input_action = INPUT_NONE;
1221 	editor = p_editor;
1222 	undo_redo = p_editor->get_undo_redo();
1223 
1224 	int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230);
1225 	Control *ec = memnew(Control);
1226 	ec->set_custom_minimum_size(Size2(mw, 0) * EDSCALE);
1227 	add_child(ec);
1228 
1229 	spatial_editor_hb = memnew(HBoxContainer);
1230 	spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL);
1231 	spatial_editor_hb->set_alignment(BoxContainer::ALIGN_END);
1232 	SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
1233 
1234 	spin_box_label = memnew(Label);
1235 	spin_box_label->set_text(TTR("Floor:"));
1236 	spatial_editor_hb->add_child(spin_box_label);
1237 
1238 	floor = memnew(SpinBox);
1239 	floor->set_min(-32767);
1240 	floor->set_max(32767);
1241 	floor->set_step(1);
1242 	floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
1243 
1244 	spatial_editor_hb->add_child(floor);
1245 	floor->connect("value_changed", this, "_floor_changed");
1246 	floor->connect("mouse_exited", this, "_floor_mouse_exited");
1247 	floor->get_line_edit()->connect("mouse_exited", this, "_floor_mouse_exited");
1248 
1249 	spatial_editor_hb->add_child(memnew(VSeparator));
1250 
1251 	options = memnew(MenuButton);
1252 	spatial_editor_hb->add_child(options);
1253 	spatial_editor_hb->hide();
1254 
1255 	options->set_text(TTR("Grid Map"));
1256 	options->get_popup()->add_check_item(TTR("Snap View"), MENU_OPTION_LOCK_VIEW);
1257 	options->get_popup()->add_separator();
1258 	options->get_popup()->add_item(TTR("Previous Floor"), MENU_OPTION_PREV_LEVEL, KEY_Q);
1259 	options->get_popup()->add_item(TTR("Next Floor"), MENU_OPTION_NEXT_LEVEL, KEY_E);
1260 	options->get_popup()->add_separator();
1261 	options->get_popup()->add_radio_check_item(TTR("Clip Disabled"), MENU_OPTION_CLIP_DISABLED);
1262 	options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true);
1263 	options->get_popup()->add_radio_check_item(TTR("Clip Above"), MENU_OPTION_CLIP_ABOVE);
1264 	options->get_popup()->add_radio_check_item(TTR("Clip Below"), MENU_OPTION_CLIP_BELOW);
1265 	options->get_popup()->add_separator();
1266 	options->get_popup()->add_radio_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, KEY_Z);
1267 	options->get_popup()->add_radio_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, KEY_X);
1268 	options->get_popup()->add_radio_check_item(TTR("Edit Z Axis"), MENU_OPTION_Z_AXIS, KEY_C);
1269 	options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
1270 	options->get_popup()->add_separator();
1271 	options->get_popup()->add_item(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
1272 	options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
1273 	options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
1274 	options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
1275 	options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
1276 	options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
1277 	options->get_popup()->add_item(TTR("Cursor Clear Rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION, KEY_W);
1278 	options->get_popup()->add_separator();
1279 	options->get_popup()->add_check_item(TTR("Paste Selects"), MENU_OPTION_PASTE_SELECTS);
1280 	options->get_popup()->add_separator();
1281 	options->get_popup()->add_item(TTR("Duplicate Selection"), MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_CTRL + KEY_C);
1282 	options->get_popup()->add_item(TTR("Cut Selection"), MENU_OPTION_SELECTION_CUT, KEY_MASK_CTRL + KEY_X);
1283 	options->get_popup()->add_item(TTR("Clear Selection"), MENU_OPTION_SELECTION_CLEAR, KEY_DELETE);
1284 	options->get_popup()->add_item(TTR("Fill Selection"), MENU_OPTION_SELECTION_FILL, KEY_MASK_CTRL + KEY_F);
1285 
1286 	options->get_popup()->add_separator();
1287 	options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
1288 
1289 	settings_dialog = memnew(ConfirmationDialog);
1290 	settings_dialog->set_title(TTR("GridMap Settings"));
1291 	add_child(settings_dialog);
1292 	settings_vbc = memnew(VBoxContainer);
1293 	settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
1294 	settings_dialog->add_child(settings_vbc);
1295 
1296 	settings_pick_distance = memnew(SpinBox);
1297 	settings_pick_distance->set_max(10000.0f);
1298 	settings_pick_distance->set_min(500.0f);
1299 	settings_pick_distance->set_step(1.0f);
1300 	settings_pick_distance->set_value(EDITOR_DEF("editors/grid_map/pick_distance", 5000.0));
1301 	settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
1302 
1303 	clip_mode = CLIP_DISABLED;
1304 	options->get_popup()->connect("id_pressed", this, "_menu_option");
1305 
1306 	HBoxContainer *hb = memnew(HBoxContainer);
1307 	add_child(hb);
1308 	hb->set_h_size_flags(SIZE_EXPAND_FILL);
1309 
1310 	search_box = memnew(LineEdit);
1311 	search_box->set_h_size_flags(SIZE_EXPAND_FILL);
1312 	search_box->set_placeholder(TTR("Filter meshes"));
1313 	hb->add_child(search_box);
1314 	search_box->connect("text_changed", this, "_text_changed");
1315 	search_box->connect("gui_input", this, "_sbox_input");
1316 
1317 	mode_thumbnail = memnew(ToolButton);
1318 	mode_thumbnail->set_toggle_mode(true);
1319 	mode_thumbnail->set_pressed(true);
1320 	mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
1321 	hb->add_child(mode_thumbnail);
1322 	mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
1323 
1324 	mode_list = memnew(ToolButton);
1325 	mode_list->set_toggle_mode(true);
1326 	mode_list->set_pressed(false);
1327 	mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
1328 	hb->add_child(mode_list);
1329 	mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
1330 
1331 	size_slider = memnew(HSlider);
1332 	size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
1333 	size_slider->set_min(0.2f);
1334 	size_slider->set_max(4.0f);
1335 	size_slider->set_step(0.1f);
1336 	size_slider->set_value(1.0f);
1337 	size_slider->connect("value_changed", this, "_icon_size_changed");
1338 	add_child(size_slider);
1339 
1340 	EDITOR_DEF("editors/grid_map/preview_size", 64);
1341 
1342 	display_mode = DISPLAY_THUMBNAIL;
1343 
1344 	mesh_library_palette = memnew(ItemList);
1345 	add_child(mesh_library_palette);
1346 	mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL);
1347 	mesh_library_palette->connect("gui_input", this, "_mesh_library_palette_input");
1348 
1349 	info_message = memnew(Label);
1350 	info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes."));
1351 	info_message->set_valign(Label::VALIGN_CENTER);
1352 	info_message->set_align(Label::ALIGN_CENTER);
1353 	info_message->set_autowrap(true);
1354 	info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
1355 	info_message->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
1356 	mesh_library_palette->add_child(info_message);
1357 
1358 	edit_axis = Vector3::AXIS_Y;
1359 	edit_floor[0] = -1;
1360 	edit_floor[1] = -1;
1361 	edit_floor[2] = -1;
1362 
1363 	cursor_visible = false;
1364 	selected_palette = -1;
1365 	lock_view = false;
1366 	cursor_rot = 0;
1367 
1368 	selection_mesh = VisualServer::get_singleton()->mesh_create();
1369 	paste_mesh = VisualServer::get_singleton()->mesh_create();
1370 
1371 	{
1372 		// Selection mesh create.
1373 
1374 		PoolVector<Vector3> lines;
1375 		PoolVector<Vector3> triangles;
1376 		PoolVector<Vector3> square[3];
1377 
1378 		for (int i = 0; i < 6; i++) {
1379 
1380 			Vector3 face_points[4];
1381 
1382 			for (int j = 0; j < 4; j++) {
1383 
1384 				float v[3];
1385 				v[0] = 1.0;
1386 				v[1] = 1 - 2 * ((j >> 1) & 1);
1387 				v[2] = v[1] * (1 - 2 * (j & 1));
1388 
1389 				for (int k = 0; k < 3; k++) {
1390 
1391 					if (i < 3)
1392 						face_points[j][(i + k) % 3] = v[k];
1393 					else
1394 						face_points[3 - j][(i + k) % 3] = -v[k];
1395 				}
1396 			}
1397 
1398 			triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
1399 			triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
1400 			triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
1401 
1402 			triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
1403 			triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
1404 			triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
1405 		}
1406 
1407 		for (int i = 0; i < 12; i++) {
1408 
1409 			AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
1410 			Vector3 a, b;
1411 			base.get_edge(i, a, b);
1412 			lines.push_back(a);
1413 			lines.push_back(b);
1414 		}
1415 
1416 		for (int i = 0; i < 3; i++) {
1417 			Vector3 points[4];
1418 			for (int j = 0; j < 4; j++) {
1419 
1420 				static const bool orderx[4] = { 0, 1, 1, 0 };
1421 				static const bool ordery[4] = { 0, 0, 1, 1 };
1422 
1423 				Vector3 sp;
1424 				if (orderx[j]) {
1425 					sp[(i + 1) % 3] = 1.0;
1426 				}
1427 				if (ordery[j]) {
1428 					sp[(i + 2) % 3] = 1.0;
1429 				}
1430 
1431 				points[j] = sp;
1432 			}
1433 
1434 			for (int j = 0; j < 4; j++) {
1435 
1436 				Vector3 ofs;
1437 				ofs[i] += 0.01;
1438 				square[i].push_back(points[j] - ofs);
1439 				square[i].push_back(points[(j + 1) % 4] - ofs);
1440 				square[i].push_back(points[j] + ofs);
1441 				square[i].push_back(points[(j + 1) % 4] + ofs);
1442 			}
1443 		}
1444 
1445 		Array d;
1446 		d.resize(VS::ARRAY_MAX);
1447 
1448 		inner_mat.instance();
1449 		inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2));
1450 		inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
1451 		inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
1452 
1453 		d[VS::ARRAY_VERTEX] = triangles;
1454 		VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
1455 		VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
1456 
1457 		outer_mat.instance();
1458 		outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8));
1459 		outer_mat->set_on_top_of_alpha();
1460 		outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
1461 		outer_mat->set_line_width(3.0);
1462 		outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
1463 
1464 		selection_floor_mat.instance();
1465 		selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
1466 		selection_floor_mat->set_on_top_of_alpha();
1467 		selection_floor_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
1468 		selection_floor_mat->set_line_width(3.0);
1469 
1470 		d[VS::ARRAY_VERTEX] = lines;
1471 		VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
1472 		VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
1473 
1474 		d[VS::ARRAY_VERTEX] = triangles;
1475 		VisualServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, VS::PRIMITIVE_TRIANGLES, d);
1476 		VisualServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid());
1477 
1478 		d[VS::ARRAY_VERTEX] = lines;
1479 		VisualServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, VS::PRIMITIVE_LINES, d);
1480 		VisualServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid());
1481 
1482 		for (int i = 0; i < 3; i++) {
1483 			d[VS::ARRAY_VERTEX] = square[i];
1484 			selection_level_mesh[i] = VS::get_singleton()->mesh_create();
1485 			VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], VS::PRIMITIVE_LINES, d);
1486 			VisualServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
1487 		}
1488 	}
1489 
1490 	_set_selection(false);
1491 	updating = false;
1492 	accumulated_floor_delta = 0.0;
1493 
1494 	indicator_mat.instance();
1495 	indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
1496 	indicator_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
1497 	indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
1498 	indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
1499 	indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
1500 }
1501 
~GridMapEditor()1502 GridMapEditor::~GridMapEditor() {
1503 
1504 	_clear_clipboard_data();
1505 
1506 	for (int i = 0; i < 3; i++) {
1507 
1508 		if (grid[i].is_valid())
1509 			VisualServer::get_singleton()->free(grid[i]);
1510 		if (grid_instance[i].is_valid())
1511 			VisualServer::get_singleton()->free(grid_instance[i]);
1512 		if (cursor_instance.is_valid())
1513 			VisualServer::get_singleton()->free(cursor_instance);
1514 		if (selection_level_instance[i].is_valid())
1515 			VisualServer::get_singleton()->free(selection_level_instance[i]);
1516 		if (selection_level_mesh[i].is_valid())
1517 			VisualServer::get_singleton()->free(selection_level_mesh[i]);
1518 	}
1519 
1520 	VisualServer::get_singleton()->free(selection_mesh);
1521 	if (selection_instance.is_valid())
1522 		VisualServer::get_singleton()->free(selection_instance);
1523 
1524 	VisualServer::get_singleton()->free(paste_mesh);
1525 	if (paste_instance.is_valid())
1526 		VisualServer::get_singleton()->free(paste_instance);
1527 }
1528 
_notification(int p_what)1529 void GridMapEditorPlugin::_notification(int p_what) {
1530 
1531 	if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
1532 
1533 		switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
1534 			case 0: { // Left.
1535 				SpatialEditor::get_singleton()->get_palette_split()->move_child(grid_map_editor, 0);
1536 			} break;
1537 			case 1: { // Right.
1538 				SpatialEditor::get_singleton()->get_palette_split()->move_child(grid_map_editor, 1);
1539 			} break;
1540 		}
1541 	}
1542 }
1543 
edit(Object * p_object)1544 void GridMapEditorPlugin::edit(Object *p_object) {
1545 
1546 	grid_map_editor->edit(Object::cast_to<GridMap>(p_object));
1547 }
1548 
handles(Object * p_object) const1549 bool GridMapEditorPlugin::handles(Object *p_object) const {
1550 
1551 	return p_object->is_class("GridMap");
1552 }
1553 
make_visible(bool p_visible)1554 void GridMapEditorPlugin::make_visible(bool p_visible) {
1555 
1556 	if (p_visible) {
1557 		grid_map_editor->show();
1558 		grid_map_editor->spatial_editor_hb->show();
1559 		grid_map_editor->set_process(true);
1560 	} else {
1561 
1562 		grid_map_editor->spatial_editor_hb->hide();
1563 		grid_map_editor->hide();
1564 		grid_map_editor->edit(NULL);
1565 		grid_map_editor->set_process(false);
1566 	}
1567 }
1568 
GridMapEditorPlugin(EditorNode * p_node)1569 GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
1570 
1571 	editor = p_node;
1572 
1573 	EDITOR_DEF("editors/grid_map/editor_side", 1);
1574 	EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/grid_map/editor_side", PROPERTY_HINT_ENUM, "Left,Right"));
1575 
1576 	grid_map_editor = memnew(GridMapEditor(editor));
1577 	switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
1578 		case 0: { // Left.
1579 			add_control_to_container(CONTAINER_SPATIAL_EDITOR_SIDE_LEFT, grid_map_editor);
1580 		} break;
1581 		case 1: { // Right.
1582 			add_control_to_container(CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT, grid_map_editor);
1583 		} break;
1584 	}
1585 	grid_map_editor->hide();
1586 }
1587 
~GridMapEditorPlugin()1588 GridMapEditorPlugin::~GridMapEditorPlugin() {
1589 }
1590