grid_map_editor_plugin.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  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) 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. #include "grid_map_editor_plugin.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "core/core_string_names.h"
  33. #include "core/input/input.h"
  34. #include "core/os/keyboard.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_scale.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/plugins/node_3d_editor_plugin.h"
  41. #include "scene/3d/camera_3d.h"
  42. #include "scene/gui/dialogs.h"
  43. #include "scene/gui/label.h"
  44. #include "scene/gui/menu_button.h"
  45. #include "scene/gui/separator.h"
  46. #include "scene/main/window.h"
  47. void GridMapEditor::_configure() {
  48. if (!node) {
  49. return;
  50. }
  51. update_grid();
  52. }
  53. void GridMapEditor::_menu_option(int p_option) {
  54. switch (p_option) {
  55. case MENU_OPTION_PREV_LEVEL: {
  56. floor->set_value(floor->get_value() - 1);
  57. } break;
  58. case MENU_OPTION_NEXT_LEVEL: {
  59. floor->set_value(floor->get_value() + 1);
  60. } break;
  61. case MENU_OPTION_X_AXIS:
  62. case MENU_OPTION_Y_AXIS:
  63. case MENU_OPTION_Z_AXIS: {
  64. int new_axis = p_option - MENU_OPTION_X_AXIS;
  65. for (int i = 0; i < 3; i++) {
  66. int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i);
  67. options->get_popup()->set_item_checked(idx, i == new_axis);
  68. }
  69. if (edit_axis != new_axis) {
  70. int item1 = options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL);
  71. int item2 = options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL);
  72. if (edit_axis == Vector3::AXIS_Y) {
  73. options->get_popup()->set_item_text(item1, TTR("Next Plane"));
  74. options->get_popup()->set_item_text(item2, TTR("Previous Plane"));
  75. spin_box_label->set_text(TTR("Plane:"));
  76. } else if (new_axis == Vector3::AXIS_Y) {
  77. options->get_popup()->set_item_text(item1, TTR("Next Floor"));
  78. options->get_popup()->set_item_text(item2, TTR("Previous Floor"));
  79. spin_box_label->set_text(TTR("Floor:"));
  80. }
  81. }
  82. edit_axis = Vector3::Axis(new_axis);
  83. update_grid();
  84. } break;
  85. case MENU_OPTION_CURSOR_ROTATE_Y: {
  86. Basis r;
  87. if (input_action == INPUT_PASTE) {
  88. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  89. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  90. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  91. _update_paste_indicator();
  92. break;
  93. }
  94. r = node->get_basis_with_orthogonal_index(cursor_rot);
  95. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  96. cursor_rot = node->get_orthogonal_index_from_basis(r);
  97. _update_cursor_transform();
  98. } break;
  99. case MENU_OPTION_CURSOR_ROTATE_X: {
  100. Basis r;
  101. if (input_action == INPUT_PASTE) {
  102. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  103. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  104. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  105. _update_paste_indicator();
  106. break;
  107. }
  108. r = node->get_basis_with_orthogonal_index(cursor_rot);
  109. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  110. cursor_rot = node->get_orthogonal_index_from_basis(r);
  111. _update_cursor_transform();
  112. } break;
  113. case MENU_OPTION_CURSOR_ROTATE_Z: {
  114. Basis r;
  115. if (input_action == INPUT_PASTE) {
  116. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  117. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  118. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  119. _update_paste_indicator();
  120. break;
  121. }
  122. r = node->get_basis_with_orthogonal_index(cursor_rot);
  123. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  124. cursor_rot = node->get_orthogonal_index_from_basis(r);
  125. _update_cursor_transform();
  126. } break;
  127. case MENU_OPTION_CURSOR_BACK_ROTATE_Y: {
  128. Basis r;
  129. if (input_action == INPUT_PASTE) {
  130. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  131. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  132. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  133. _update_paste_indicator();
  134. break;
  135. }
  136. r = node->get_basis_with_orthogonal_index(cursor_rot);
  137. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  138. cursor_rot = node->get_orthogonal_index_from_basis(r);
  139. _update_cursor_transform();
  140. } break;
  141. case MENU_OPTION_CURSOR_BACK_ROTATE_X: {
  142. Basis r;
  143. if (input_action == INPUT_PASTE) {
  144. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  145. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  146. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  147. _update_paste_indicator();
  148. break;
  149. }
  150. r = node->get_basis_with_orthogonal_index(cursor_rot);
  151. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  152. cursor_rot = node->get_orthogonal_index_from_basis(r);
  153. _update_cursor_transform();
  154. } break;
  155. case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
  156. Basis r;
  157. if (input_action == INPUT_PASTE) {
  158. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  159. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  160. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  161. _update_paste_indicator();
  162. break;
  163. }
  164. r = node->get_basis_with_orthogonal_index(cursor_rot);
  165. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  166. cursor_rot = node->get_orthogonal_index_from_basis(r);
  167. _update_cursor_transform();
  168. } break;
  169. case MENU_OPTION_CURSOR_CLEAR_ROTATION: {
  170. if (input_action == INPUT_PASTE) {
  171. paste_indicator.orientation = 0;
  172. _update_paste_indicator();
  173. break;
  174. }
  175. cursor_rot = 0;
  176. _update_cursor_transform();
  177. } break;
  178. case MENU_OPTION_PASTE_SELECTS: {
  179. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  180. options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
  181. } break;
  182. case MENU_OPTION_SELECTION_DUPLICATE:
  183. case MENU_OPTION_SELECTION_CUT: {
  184. if (!(selection.active && input_action == INPUT_NONE)) {
  185. break;
  186. }
  187. _set_clipboard_data();
  188. if (p_option == MENU_OPTION_SELECTION_CUT) {
  189. _delete_selection();
  190. }
  191. input_action = INPUT_PASTE;
  192. paste_indicator.click = selection.begin;
  193. paste_indicator.current = selection.begin;
  194. paste_indicator.begin = selection.begin;
  195. paste_indicator.end = selection.end;
  196. paste_indicator.orientation = 0;
  197. _update_paste_indicator();
  198. } break;
  199. case MENU_OPTION_SELECTION_CLEAR: {
  200. if (!selection.active) {
  201. break;
  202. }
  203. _delete_selection();
  204. } break;
  205. case MENU_OPTION_SELECTION_FILL: {
  206. if (!selection.active) {
  207. return;
  208. }
  209. _fill_selection();
  210. } break;
  211. case MENU_OPTION_GRIDMAP_SETTINGS: {
  212. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE);
  213. } break;
  214. }
  215. }
  216. void GridMapEditor::_update_cursor_transform() {
  217. cursor_transform = Transform3D();
  218. cursor_transform.origin = cursor_origin;
  219. cursor_transform.basis = node->get_basis_with_orthogonal_index(cursor_rot);
  220. cursor_transform.basis *= node->get_cell_scale();
  221. cursor_transform = node->get_global_transform() * cursor_transform;
  222. if (selected_palette >= 0) {
  223. if (node && !node->get_mesh_library().is_null()) {
  224. cursor_transform *= node->get_mesh_library()->get_item_mesh_transform(selected_palette);
  225. }
  226. }
  227. if (cursor_instance.is_valid()) {
  228. RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  229. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
  230. }
  231. }
  232. void GridMapEditor::_update_selection_transform() {
  233. Transform3D xf_zero;
  234. xf_zero.basis.set_zero();
  235. if (!selection.active) {
  236. RenderingServer::get_singleton()->instance_set_transform(selection_instance, xf_zero);
  237. for (int i = 0; i < 3; i++) {
  238. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  239. }
  240. return;
  241. }
  242. Transform3D xf;
  243. xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  244. xf.origin = selection.begin * node->get_cell_size();
  245. RenderingServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  246. for (int i = 0; i < 3; i++) {
  247. if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) {
  248. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  249. } else {
  250. Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
  251. scale[edit_axis] = 1.0;
  252. Vector3 position = selection.begin;
  253. position[edit_axis] = edit_floor[edit_axis];
  254. scale *= node->get_cell_size();
  255. position *= node->get_cell_size();
  256. Transform3D xf2;
  257. xf2.basis.scale(scale);
  258. xf2.origin = position;
  259. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2);
  260. }
  261. }
  262. }
  263. void GridMapEditor::_validate_selection() {
  264. if (!selection.active) {
  265. return;
  266. }
  267. selection.begin = selection.click;
  268. selection.end = selection.current;
  269. if (selection.begin.x > selection.end.x) {
  270. SWAP(selection.begin.x, selection.end.x);
  271. }
  272. if (selection.begin.y > selection.end.y) {
  273. SWAP(selection.begin.y, selection.end.y);
  274. }
  275. if (selection.begin.z > selection.end.z) {
  276. SWAP(selection.begin.z, selection.end.z);
  277. }
  278. _update_selection_transform();
  279. }
  280. void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) {
  281. selection.active = p_active;
  282. selection.begin = p_begin;
  283. selection.end = p_end;
  284. selection.click = p_begin;
  285. selection.current = p_end;
  286. if (is_visible_in_tree()) {
  287. _update_selection_transform();
  288. }
  289. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CLEAR), !selection.active);
  290. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CUT), !selection.active);
  291. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_DUPLICATE), !selection.active);
  292. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_FILL), !selection.active);
  293. }
  294. bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click) {
  295. if (!spatial_editor) {
  296. return false;
  297. }
  298. if (selected_palette < 0 && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE) {
  299. return false;
  300. }
  301. if (mesh_library.is_null()) {
  302. return false;
  303. }
  304. if (input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette)) {
  305. return false;
  306. }
  307. Camera3D *camera = p_camera;
  308. Vector3 from = camera->project_ray_origin(p_point);
  309. Vector3 normal = camera->project_ray_normal(p_point);
  310. Transform3D local_xform = node->get_global_transform().affine_inverse();
  311. Vector<Plane> planes = camera->get_frustum();
  312. from = local_xform.xform(from);
  313. normal = local_xform.basis.xform(normal).normalized();
  314. Plane p;
  315. p.normal[edit_axis] = 1.0;
  316. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  317. Vector3 inters;
  318. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters)) {
  319. return false;
  320. }
  321. // Make sure the intersection is inside the frustum planes, to avoid
  322. // Painting on invisible regions.
  323. for (int i = 0; i < planes.size(); i++) {
  324. Plane fp = local_xform.xform(planes[i]);
  325. if (fp.is_point_over(inters)) {
  326. return false;
  327. }
  328. }
  329. int cell[3];
  330. Vector3 cell_size = node->get_cell_size();
  331. for (int i = 0; i < 3; i++) {
  332. if (i == edit_axis) {
  333. cell[i] = edit_floor[i];
  334. } else {
  335. cell[i] = inters[i] / cell_size[i];
  336. if (inters[i] < 0) {
  337. cell[i] -= 1; // Compensate negative.
  338. }
  339. grid_ofs[i] = cell[i] * cell_size[i];
  340. }
  341. }
  342. RS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform);
  343. if (cursor_instance.is_valid()) {
  344. 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();
  345. cursor_visible = true;
  346. if (input_action == INPUT_SELECT || input_action == INPUT_PASTE) {
  347. cursor_visible = false;
  348. }
  349. _update_cursor_transform();
  350. }
  351. if (input_action == INPUT_PASTE) {
  352. paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]);
  353. _update_paste_indicator();
  354. } else if (input_action == INPUT_SELECT) {
  355. selection.current = Vector3i(cell[0], cell[1], cell[2]);
  356. if (p_click) {
  357. selection.click = selection.current;
  358. }
  359. selection.active = true;
  360. _validate_selection();
  361. return true;
  362. } else if (input_action == INPUT_PICK) {
  363. int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  364. if (item >= 0) {
  365. selected_palette = item;
  366. // Clear the filter if picked an item that's filtered out.
  367. int index = mesh_library_palette->find_metadata(item);
  368. if (index == -1) {
  369. search_box->clear();
  370. }
  371. // This will select `selected_palette` in the ItemList when possible.
  372. update_palette();
  373. _update_cursor_instance();
  374. }
  375. return true;
  376. }
  377. if (input_action == INPUT_PAINT) {
  378. SetItem si;
  379. si.position = Vector3i(cell[0], cell[1], cell[2]);
  380. si.new_value = selected_palette;
  381. si.new_orientation = cursor_rot;
  382. si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  383. si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
  384. set_items.push_back(si);
  385. node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot);
  386. return true;
  387. } else if (input_action == INPUT_ERASE) {
  388. SetItem si;
  389. si.position = Vector3i(cell[0], cell[1], cell[2]);
  390. si.new_value = -1;
  391. si.new_orientation = 0;
  392. si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  393. si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
  394. set_items.push_back(si);
  395. node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1);
  396. return true;
  397. }
  398. return false;
  399. }
  400. void GridMapEditor::_delete_selection() {
  401. if (!selection.active) {
  402. return;
  403. }
  404. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  405. undo_redo->create_action(TTR("GridMap Delete Selection"));
  406. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  407. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  408. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  409. Vector3i selected = Vector3i(i, j, k);
  410. undo_redo->add_do_method(node, "set_cell_item", selected, GridMap::INVALID_CELL_ITEM);
  411. undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
  412. }
  413. }
  414. }
  415. undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
  416. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  417. undo_redo->commit_action();
  418. }
  419. void GridMapEditor::_fill_selection() {
  420. if (!selection.active) {
  421. return;
  422. }
  423. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  424. undo_redo->create_action(TTR("GridMap Fill Selection"));
  425. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  426. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  427. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  428. Vector3i selected = Vector3i(i, j, k);
  429. undo_redo->add_do_method(node, "set_cell_item", selected, selected_palette, cursor_rot);
  430. undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
  431. }
  432. }
  433. }
  434. undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
  435. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  436. undo_redo->commit_action();
  437. }
  438. void GridMapEditor::_clear_clipboard_data() {
  439. for (const ClipboardItem &E : clipboard_items) {
  440. RenderingServer::get_singleton()->free(E.instance);
  441. }
  442. clipboard_items.clear();
  443. }
  444. void GridMapEditor::_set_clipboard_data() {
  445. _clear_clipboard_data();
  446. Ref<MeshLibrary> meshLibrary = node->get_mesh_library();
  447. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  448. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  449. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  450. Vector3i selected = Vector3i(i, j, k);
  451. int itm = node->get_cell_item(selected);
  452. if (itm == GridMap::INVALID_CELL_ITEM) {
  453. continue;
  454. }
  455. Ref<Mesh> mesh = meshLibrary->get_item_mesh(itm);
  456. ClipboardItem item;
  457. item.cell_item = itm;
  458. item.grid_offset = Vector3(selected) - selection.begin;
  459. item.orientation = node->get_cell_item_orientation(selected);
  460. item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());
  461. clipboard_items.push_back(item);
  462. }
  463. }
  464. }
  465. }
  466. void GridMapEditor::_update_paste_indicator() {
  467. if (input_action != INPUT_PASTE) {
  468. Transform3D xf;
  469. xf.basis.set_zero();
  470. RenderingServer::get_singleton()->instance_set_transform(paste_instance, xf);
  471. return;
  472. }
  473. Vector3 center = 0.5 * Vector3(real_t(node->get_center_x()), real_t(node->get_center_y()), real_t(node->get_center_z()));
  474. Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size();
  475. Transform3D xf;
  476. xf.scale(scale);
  477. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  478. Basis rot;
  479. rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  480. xf.basis = rot * xf.basis;
  481. xf.translate_local((-center * node->get_cell_size()) / scale);
  482. RenderingServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf);
  483. for (const ClipboardItem &item : clipboard_items) {
  484. xf = Transform3D();
  485. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  486. xf.basis = rot * xf.basis;
  487. xf.translate_local(item.grid_offset * node->get_cell_size());
  488. Basis item_rot;
  489. item_rot = node->get_basis_with_orthogonal_index(item.orientation);
  490. xf.basis = item_rot * xf.basis * node->get_cell_scale();
  491. RenderingServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf);
  492. }
  493. }
  494. void GridMapEditor::_do_paste() {
  495. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  496. bool reselect = options->get_popup()->is_item_checked(idx);
  497. Basis rot;
  498. rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  499. Vector3 ofs = paste_indicator.current - paste_indicator.click;
  500. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  501. undo_redo->create_action(TTR("GridMap Paste Selection"));
  502. for (const ClipboardItem &item : clipboard_items) {
  503. Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;
  504. Basis orm;
  505. orm = node->get_basis_with_orthogonal_index(item.orientation);
  506. orm = rot * orm;
  507. undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, node->get_orthogonal_index_from_basis(orm));
  508. undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position));
  509. }
  510. if (reselect) {
  511. undo_redo->add_do_method(this, "_set_selection", true, paste_indicator.begin + ofs, paste_indicator.end + ofs);
  512. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  513. }
  514. undo_redo->commit_action();
  515. _clear_clipboard_data();
  516. }
  517. EditorPlugin::AfterGUIInput GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  518. if (!node) {
  519. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  520. }
  521. Ref<InputEventMouseButton> mb = p_event;
  522. if (mb.is_valid()) {
  523. if (mb->get_button_index() == MouseButton::WHEEL_UP && (mb->is_command_or_control_pressed())) {
  524. if (mb->is_pressed()) {
  525. floor->set_value(floor->get_value() + mb->get_factor());
  526. }
  527. return EditorPlugin::AFTER_GUI_INPUT_STOP; // Eaten.
  528. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && (mb->is_command_or_control_pressed())) {
  529. if (mb->is_pressed()) {
  530. floor->set_value(floor->get_value() - mb->get_factor());
  531. }
  532. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  533. }
  534. if (mb->is_pressed()) {
  535. Node3DEditorViewport::NavigationScheme nav_scheme = (Node3DEditorViewport::NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme").operator int();
  536. if ((nav_scheme == Node3DEditorViewport::NAVIGATION_MAYA || nav_scheme == Node3DEditorViewport::NAVIGATION_MODO) && mb->is_alt_pressed()) {
  537. input_action = INPUT_NONE;
  538. } else if (mb->get_button_index() == MouseButton::LEFT) {
  539. bool can_edit = (node && node->get_mesh_library().is_valid());
  540. if (input_action == INPUT_PASTE) {
  541. _do_paste();
  542. input_action = INPUT_NONE;
  543. _update_paste_indicator();
  544. } else if (mb->is_shift_pressed() && can_edit) {
  545. input_action = INPUT_SELECT;
  546. last_selection = selection;
  547. } else if (mb->is_command_or_control_pressed() && can_edit) {
  548. input_action = INPUT_PICK;
  549. } else {
  550. input_action = INPUT_PAINT;
  551. set_items.clear();
  552. }
  553. } else if (mb->get_button_index() == MouseButton::RIGHT) {
  554. if (input_action == INPUT_PASTE) {
  555. _clear_clipboard_data();
  556. input_action = INPUT_NONE;
  557. _update_paste_indicator();
  558. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  559. } else if (selection.active) {
  560. _set_selection(false);
  561. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  562. } else {
  563. input_action = INPUT_ERASE;
  564. set_items.clear();
  565. }
  566. } else {
  567. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  568. }
  569. if (do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true)) {
  570. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  571. }
  572. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  573. } else {
  574. if ((mb->get_button_index() == MouseButton::RIGHT && input_action == INPUT_ERASE) || (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_PAINT)) {
  575. if (set_items.size()) {
  576. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  577. undo_redo->create_action(TTR("GridMap Paint"));
  578. for (const SetItem &si : set_items) {
  579. undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation);
  580. }
  581. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  582. const SetItem &si = E->get();
  583. undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation);
  584. }
  585. undo_redo->commit_action();
  586. }
  587. set_items.clear();
  588. input_action = INPUT_NONE;
  589. if (set_items.size() > 0) {
  590. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  591. }
  592. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  593. }
  594. if (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_SELECT) {
  595. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  596. undo_redo->create_action(TTR("GridMap Selection"));
  597. undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  598. undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end);
  599. undo_redo->commit_action();
  600. }
  601. if (mb->get_button_index() == MouseButton::LEFT && input_action != INPUT_NONE) {
  602. set_items.clear();
  603. input_action = INPUT_NONE;
  604. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  605. }
  606. if (mb->get_button_index() == MouseButton::RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) {
  607. input_action = INPUT_NONE;
  608. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  609. }
  610. }
  611. }
  612. Ref<InputEventMouseMotion> mm = p_event;
  613. if (mm.is_valid()) {
  614. // Update the grid, to check if the grid needs to be moved to a tile cursor.
  615. update_grid();
  616. if (do_input_action(p_camera, mm->get_position(), false)) {
  617. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  618. }
  619. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  620. }
  621. Ref<InputEventKey> k = p_event;
  622. if (k.is_valid()) {
  623. if (k->is_pressed()) {
  624. if (k->get_keycode() == Key::ESCAPE) {
  625. if (input_action == INPUT_PASTE) {
  626. _clear_clipboard_data();
  627. input_action = INPUT_NONE;
  628. _update_paste_indicator();
  629. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  630. } else if (selection.active) {
  631. _set_selection(false);
  632. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  633. } else {
  634. selected_palette = -1;
  635. mesh_library_palette->deselect_all();
  636. update_palette();
  637. _update_cursor_instance();
  638. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  639. }
  640. }
  641. // Consume input to avoid conflicts with other plugins.
  642. if (k.is_valid() && k->is_pressed() && !k->is_echo()) {
  643. for (int i = 0; i < options->get_popup()->get_item_count(); ++i) {
  644. const Ref<Shortcut> &shortcut = options->get_popup()->get_item_shortcut(i);
  645. if (shortcut.is_valid() && shortcut->matches_event(p_event)) {
  646. accept_event();
  647. _menu_option(options->get_popup()->get_item_id(i));
  648. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  649. }
  650. }
  651. }
  652. if (k->is_shift_pressed() && selection.active && input_action != INPUT_PASTE) {
  653. if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL))) {
  654. selection.click[edit_axis]--;
  655. _validate_selection();
  656. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  657. }
  658. if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL))) {
  659. selection.click[edit_axis]++;
  660. _validate_selection();
  661. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  662. }
  663. }
  664. }
  665. }
  666. Ref<InputEventPanGesture> pan_gesture = p_event;
  667. if (pan_gesture.is_valid()) {
  668. if (pan_gesture->is_alt_pressed() && pan_gesture->is_command_or_control_pressed()) {
  669. const real_t delta = pan_gesture->get_delta().y * 0.5;
  670. accumulated_floor_delta += delta;
  671. int step = 0;
  672. if (ABS(accumulated_floor_delta) > 1.0) {
  673. step = SIGN(accumulated_floor_delta);
  674. accumulated_floor_delta -= step;
  675. }
  676. if (step) {
  677. floor->set_value(floor->get_value() + step);
  678. }
  679. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  680. }
  681. }
  682. accumulated_floor_delta = 0.0;
  683. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  684. }
  685. struct _CGMEItemSort {
  686. String name;
  687. int id = 0;
  688. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  689. };
  690. void GridMapEditor::_set_display_mode(int p_mode) {
  691. if (display_mode == p_mode) {
  692. return;
  693. }
  694. if (p_mode == DISPLAY_LIST) {
  695. mode_list->set_pressed(true);
  696. mode_thumbnail->set_pressed(false);
  697. } else if (p_mode == DISPLAY_THUMBNAIL) {
  698. mode_list->set_pressed(false);
  699. mode_thumbnail->set_pressed(true);
  700. }
  701. display_mode = p_mode;
  702. update_palette();
  703. }
  704. void GridMapEditor::_text_changed(const String &p_text) {
  705. update_palette();
  706. }
  707. void GridMapEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  708. const Ref<InputEventKey> k = p_ie;
  709. if (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::PAGEUP || k->get_keycode() == Key::PAGEDOWN)) {
  710. // Forward the key input to the ItemList so it can be scrolled
  711. mesh_library_palette->gui_input(k);
  712. search_box->accept_event();
  713. }
  714. }
  715. void GridMapEditor::_mesh_library_palette_input(const Ref<InputEvent> &p_ie) {
  716. const Ref<InputEventMouseButton> mb = p_ie;
  717. // Zoom in/out using Ctrl + mouse wheel
  718. if (mb.is_valid() && mb->is_pressed() && mb->is_command_or_control_pressed()) {
  719. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) {
  720. size_slider->set_value(size_slider->get_value() + 0.2);
  721. }
  722. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  723. size_slider->set_value(size_slider->get_value() - 0.2);
  724. }
  725. }
  726. }
  727. void GridMapEditor::_icon_size_changed(float p_value) {
  728. mesh_library_palette->set_icon_scale(p_value);
  729. update_palette();
  730. }
  731. void GridMapEditor::update_palette() {
  732. float min_size = EDITOR_GET("editors/grid_map/preview_size");
  733. min_size *= EDSCALE;
  734. mesh_library_palette->clear();
  735. if (display_mode == DISPLAY_THUMBNAIL) {
  736. mesh_library_palette->set_max_columns(0);
  737. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  738. mesh_library_palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1.5));
  739. } else if (display_mode == DISPLAY_LIST) {
  740. mesh_library_palette->set_max_columns(1);
  741. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT);
  742. mesh_library_palette->set_fixed_column_width(0);
  743. }
  744. mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size));
  745. mesh_library_palette->set_max_text_lines(2);
  746. if (mesh_library.is_null()) {
  747. search_box->set_text("");
  748. search_box->set_editable(false);
  749. info_message->show();
  750. return;
  751. }
  752. search_box->set_editable(true);
  753. info_message->hide();
  754. Vector<int> ids;
  755. ids = mesh_library->get_item_list();
  756. List<_CGMEItemSort> il;
  757. for (int i = 0; i < ids.size(); i++) {
  758. _CGMEItemSort is;
  759. is.id = ids[i];
  760. is.name = mesh_library->get_item_name(ids[i]);
  761. il.push_back(is);
  762. }
  763. il.sort();
  764. String filter = search_box->get_text().strip_edges();
  765. int item = 0;
  766. for (_CGMEItemSort &E : il) {
  767. int id = E.id;
  768. String name = mesh_library->get_item_name(id);
  769. Ref<Texture2D> preview = mesh_library->get_item_preview(id);
  770. if (name.is_empty()) {
  771. name = "#" + itos(id);
  772. }
  773. if (!filter.is_empty() && !filter.is_subsequence_ofn(name)) {
  774. continue;
  775. }
  776. mesh_library_palette->add_item("");
  777. if (!preview.is_null()) {
  778. mesh_library_palette->set_item_icon(item, preview);
  779. mesh_library_palette->set_item_tooltip(item, name);
  780. }
  781. mesh_library_palette->set_item_text(item, name);
  782. mesh_library_palette->set_item_metadata(item, id);
  783. if (selected_palette == id) {
  784. mesh_library_palette->select(item);
  785. }
  786. item++;
  787. }
  788. }
  789. void GridMapEditor::_update_mesh_library() {
  790. ERR_FAIL_NULL(node);
  791. Ref<MeshLibrary> new_mesh_library = node->get_mesh_library();
  792. if (new_mesh_library != mesh_library) {
  793. if (mesh_library.is_valid()) {
  794. mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette));
  795. }
  796. mesh_library = new_mesh_library;
  797. } else {
  798. return;
  799. }
  800. if (mesh_library.is_valid()) {
  801. mesh_library->connect_changed(callable_mp(this, &GridMapEditor::update_palette));
  802. }
  803. update_palette();
  804. // Update the cursor and grid in case the library is changed or removed.
  805. _update_cursor_instance();
  806. update_grid();
  807. }
  808. void GridMapEditor::edit(GridMap *p_gridmap) {
  809. if (node) {
  810. node->disconnect(SNAME("cell_size_changed"), callable_mp(this, &GridMapEditor::_draw_grids));
  811. node->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GridMapEditor::_update_mesh_library));
  812. if (mesh_library.is_valid()) {
  813. mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette));
  814. mesh_library = Ref<MeshLibrary>();
  815. }
  816. }
  817. node = p_gridmap;
  818. input_action = INPUT_NONE;
  819. selection.active = false;
  820. _update_selection_transform();
  821. _update_paste_indicator();
  822. spatial_editor = Object::cast_to<Node3DEditorPlugin>(EditorNode::get_singleton()->get_editor_plugin_screen());
  823. if (!node) {
  824. set_process(false);
  825. for (int i = 0; i < 3; i++) {
  826. RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  827. }
  828. if (cursor_instance.is_valid()) {
  829. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false);
  830. }
  831. return;
  832. }
  833. update_palette();
  834. _update_cursor_instance();
  835. set_process(true);
  836. _draw_grids(node->get_cell_size());
  837. update_grid();
  838. node->connect(SNAME("cell_size_changed"), callable_mp(this, &GridMapEditor::_draw_grids));
  839. node->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GridMapEditor::_update_mesh_library));
  840. _update_mesh_library();
  841. }
  842. void GridMapEditor::update_grid() {
  843. grid_xform.origin.x -= 1; // Force update in hackish way.
  844. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  845. // If there's a valid tile cursor, offset the grid, otherwise move it back to the node.
  846. edit_grid_xform.origin = cursor_instance.is_valid() ? grid_ofs : Vector3();
  847. edit_grid_xform.basis = Basis();
  848. for (int i = 0; i < 3; i++) {
  849. RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  850. }
  851. updating = true;
  852. floor->set_value(edit_floor[edit_axis]);
  853. updating = false;
  854. }
  855. void GridMapEditor::_draw_grids(const Vector3 &cell_size) {
  856. Vector3 edited_floor = node->get_meta("_editor_floor_", Vector3());
  857. for (int i = 0; i < 3; i++) {
  858. RS::get_singleton()->mesh_clear(grid[i]);
  859. edit_floor[i] = edited_floor[i];
  860. }
  861. Vector<Vector3> grid_points[3];
  862. Vector<Color> grid_colors[3];
  863. for (int i = 0; i < 3; i++) {
  864. Vector3 axis;
  865. axis[i] = 1;
  866. Vector3 axis_n1;
  867. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  868. Vector3 axis_n2;
  869. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  870. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  871. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  872. Vector3 p = axis_n1 * j + axis_n2 * k;
  873. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  874. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  875. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  876. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  877. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  878. grid_points[i].push_back(p);
  879. grid_points[i].push_back(pk);
  880. grid_colors[i].push_back(Color(1, 1, 1, trans));
  881. grid_colors[i].push_back(Color(1, 1, 1, transk));
  882. grid_points[i].push_back(p);
  883. grid_points[i].push_back(pj);
  884. grid_colors[i].push_back(Color(1, 1, 1, trans));
  885. grid_colors[i].push_back(Color(1, 1, 1, transj));
  886. }
  887. }
  888. Array d;
  889. d.resize(RS::ARRAY_MAX);
  890. d[RS::ARRAY_VERTEX] = grid_points[i];
  891. d[RS::ARRAY_COLOR] = grid_colors[i];
  892. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], RenderingServer::PRIMITIVE_LINES, d);
  893. RenderingServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  894. }
  895. }
  896. void GridMapEditor::_update_theme() {
  897. options->set_icon(get_theme_icon(SNAME("GridMap"), EditorStringName(EditorIcons)));
  898. search_box->set_right_icon(get_theme_icon(SNAME("Search"), EditorStringName(EditorIcons)));
  899. mode_thumbnail->set_icon(get_theme_icon(SNAME("FileThumbnail"), EditorStringName(EditorIcons)));
  900. mode_list->set_icon(get_theme_icon(SNAME("FileList"), EditorStringName(EditorIcons)));
  901. }
  902. void GridMapEditor::_notification(int p_what) {
  903. switch (p_what) {
  904. case NOTIFICATION_ENTER_TREE: {
  905. mesh_library_palette->connect("item_selected", callable_mp(this, &GridMapEditor::_item_selected_cbk));
  906. for (int i = 0; i < 3; i++) {
  907. grid[i] = RS::get_singleton()->mesh_create();
  908. grid_instance[i] = RS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  909. RenderingServer::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  910. selection_level_instance[i] = RenderingServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  911. RenderingServer::get_singleton()->instance_set_layer_mask(selection_level_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  912. }
  913. selection_instance = RenderingServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  914. RenderingServer::get_singleton()->instance_set_layer_mask(selection_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  915. paste_instance = RenderingServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  916. RenderingServer::get_singleton()->instance_set_layer_mask(paste_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  917. _update_selection_transform();
  918. _update_paste_indicator();
  919. _update_theme();
  920. } break;
  921. case NOTIFICATION_EXIT_TREE: {
  922. _clear_clipboard_data();
  923. for (int i = 0; i < 3; i++) {
  924. RS::get_singleton()->free(grid_instance[i]);
  925. RS::get_singleton()->free(grid[i]);
  926. grid_instance[i] = RID();
  927. grid[i] = RID();
  928. RenderingServer::get_singleton()->free(selection_level_instance[i]);
  929. }
  930. RenderingServer::get_singleton()->free(selection_instance);
  931. RenderingServer::get_singleton()->free(paste_instance);
  932. selection_instance = RID();
  933. paste_instance = RID();
  934. } break;
  935. case NOTIFICATION_PROCESS: {
  936. if (!node) {
  937. return;
  938. }
  939. Transform3D xf = node->get_global_transform();
  940. if (xf != grid_xform) {
  941. for (int i = 0; i < 3; i++) {
  942. RS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  943. }
  944. grid_xform = xf;
  945. }
  946. } break;
  947. case NOTIFICATION_THEME_CHANGED: {
  948. _update_theme();
  949. } break;
  950. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  951. if (input_action == INPUT_PAINT) {
  952. // Simulate mouse released event to stop drawing when editor focus exists.
  953. Ref<InputEventMouseButton> release;
  954. release.instantiate();
  955. release->set_button_index(MouseButton::LEFT);
  956. forward_spatial_input_event(nullptr, release);
  957. }
  958. } break;
  959. }
  960. }
  961. void GridMapEditor::_update_cursor_instance() {
  962. if (!node) {
  963. return;
  964. }
  965. if (cursor_instance.is_valid()) {
  966. RenderingServer::get_singleton()->free(cursor_instance);
  967. }
  968. cursor_instance = RID();
  969. if (selected_palette >= 0) {
  970. if (node && !node->get_mesh_library().is_null()) {
  971. Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette);
  972. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  973. cursor_instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());
  974. RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  975. }
  976. }
  977. }
  978. }
  979. void GridMapEditor::_item_selected_cbk(int idx) {
  980. selected_palette = mesh_library_palette->get_item_metadata(idx);
  981. _update_cursor_instance();
  982. }
  983. void GridMapEditor::_floor_changed(float p_value) {
  984. if (updating) {
  985. return;
  986. }
  987. edit_floor[edit_axis] = p_value;
  988. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  989. update_grid();
  990. _update_selection_transform();
  991. }
  992. void GridMapEditor::_floor_mouse_exited() {
  993. floor->get_line_edit()->release_focus();
  994. }
  995. void GridMapEditor::_bind_methods() {
  996. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  997. ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection);
  998. }
  999. GridMapEditor::GridMapEditor() {
  1000. ED_SHORTCUT("grid_map/previous_floor", TTR("Previous Floor"), Key::Q, true);
  1001. ED_SHORTCUT("grid_map/next_floor", TTR("Next Floor"), Key::E, true);
  1002. ED_SHORTCUT("grid_map/edit_x_axis", TTR("Edit X Axis"), Key::Z, true);
  1003. ED_SHORTCUT("grid_map/edit_y_axis", TTR("Edit Y Axis"), Key::X, true);
  1004. ED_SHORTCUT("grid_map/edit_z_axis", TTR("Edit Z Axis"), Key::C, true);
  1005. ED_SHORTCUT("grid_map/cursor_rotate_x", TTR("Cursor Rotate X"), Key::A, true);
  1006. ED_SHORTCUT("grid_map/cursor_rotate_y", TTR("Cursor Rotate Y"), Key::S, true);
  1007. ED_SHORTCUT("grid_map/cursor_rotate_z", TTR("Cursor Rotate Z"), Key::D, true);
  1008. ED_SHORTCUT("grid_map/cursor_back_rotate_x", TTR("Cursor Back Rotate X"), KeyModifierMask::SHIFT + Key::A, true);
  1009. ED_SHORTCUT("grid_map/cursor_back_rotate_y", TTR("Cursor Back Rotate Y"), KeyModifierMask::SHIFT + Key::S, true);
  1010. ED_SHORTCUT("grid_map/cursor_back_rotate_z", TTR("Cursor Back Rotate Z"), KeyModifierMask::SHIFT + Key::D, true);
  1011. ED_SHORTCUT("grid_map/cursor_clear_rotation", TTR("Cursor Clear Rotation"), Key::W, true);
  1012. ED_SHORTCUT("grid_map/paste_selects", TTR("Paste Selects"));
  1013. ED_SHORTCUT("grid_map/duplicate_selection", TTR("Duplicate Selection"), KeyModifierMask::CTRL + Key::C);
  1014. ED_SHORTCUT("grid_map/cut_selection", TTR("Cut Selection"), KeyModifierMask::CTRL + Key::X);
  1015. ED_SHORTCUT("grid_map/clear_selection", TTR("Clear Selection"), Key::KEY_DELETE);
  1016. ED_SHORTCUT("grid_map/fill_selection", TTR("Fill Selection"), KeyModifierMask::CTRL + Key::F);
  1017. int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230);
  1018. Control *ec = memnew(Control);
  1019. ec->set_custom_minimum_size(Size2(mw, 0) * EDSCALE);
  1020. add_child(ec);
  1021. spatial_editor_hb = memnew(HBoxContainer);
  1022. spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1023. spatial_editor_hb->set_alignment(BoxContainer::ALIGNMENT_END);
  1024. Node3DEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  1025. spin_box_label = memnew(Label);
  1026. spin_box_label->set_text(TTR("Floor:"));
  1027. spatial_editor_hb->add_child(spin_box_label);
  1028. floor = memnew(SpinBox);
  1029. floor->set_min(-32767);
  1030. floor->set_max(32767);
  1031. floor->set_step(1);
  1032. floor->get_line_edit()->add_theme_constant_override("minimum_character_width", 16);
  1033. spatial_editor_hb->add_child(floor);
  1034. floor->connect("value_changed", callable_mp(this, &GridMapEditor::_floor_changed));
  1035. floor->connect("mouse_exited", callable_mp(this, &GridMapEditor::_floor_mouse_exited));
  1036. floor->get_line_edit()->connect("mouse_exited", callable_mp(this, &GridMapEditor::_floor_mouse_exited));
  1037. spatial_editor_hb->add_child(memnew(VSeparator));
  1038. options = memnew(MenuButton);
  1039. spatial_editor_hb->add_child(options);
  1040. spatial_editor_hb->hide();
  1041. options->set_text(TTR("Grid Map"));
  1042. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/previous_floor"), MENU_OPTION_PREV_LEVEL);
  1043. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/next_floor"), MENU_OPTION_NEXT_LEVEL);
  1044. options->get_popup()->add_separator();
  1045. options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_x_axis"), MENU_OPTION_X_AXIS);
  1046. options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_y_axis"), MENU_OPTION_Y_AXIS);
  1047. options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_z_axis"), MENU_OPTION_Z_AXIS);
  1048. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  1049. options->get_popup()->add_separator();
  1050. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_rotate_x"), MENU_OPTION_CURSOR_ROTATE_X);
  1051. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_rotate_y"), MENU_OPTION_CURSOR_ROTATE_Y);
  1052. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_rotate_z"), MENU_OPTION_CURSOR_ROTATE_Z);
  1053. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_back_rotate_x"), MENU_OPTION_CURSOR_BACK_ROTATE_X);
  1054. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_back_rotate_y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y);
  1055. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_back_rotate_z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z);
  1056. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cursor_clear_rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION);
  1057. options->get_popup()->add_separator();
  1058. // TRANSLATORS: This is a toggle to select after pasting the new content.
  1059. options->get_popup()->add_check_shortcut(ED_GET_SHORTCUT("grid_map/paste_selects"), MENU_OPTION_PASTE_SELECTS);
  1060. options->get_popup()->add_separator();
  1061. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/duplicate_selection"), MENU_OPTION_SELECTION_DUPLICATE);
  1062. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/cut_selection"), MENU_OPTION_SELECTION_CUT);
  1063. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/clear_selection"), MENU_OPTION_SELECTION_CLEAR);
  1064. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/fill_selection"), MENU_OPTION_SELECTION_FILL);
  1065. options->get_popup()->add_separator();
  1066. options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
  1067. settings_dialog = memnew(ConfirmationDialog);
  1068. settings_dialog->set_title(TTR("GridMap Settings"));
  1069. add_child(settings_dialog);
  1070. settings_vbc = memnew(VBoxContainer);
  1071. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  1072. settings_dialog->add_child(settings_vbc);
  1073. settings_pick_distance = memnew(SpinBox);
  1074. settings_pick_distance->set_max(10000.0f);
  1075. settings_pick_distance->set_min(500.0f);
  1076. settings_pick_distance->set_step(1.0f);
  1077. settings_pick_distance->set_value(EDITOR_GET("editors/grid_map/pick_distance"));
  1078. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  1079. options->get_popup()->connect("id_pressed", callable_mp(this, &GridMapEditor::_menu_option));
  1080. HBoxContainer *hb = memnew(HBoxContainer);
  1081. add_child(hb);
  1082. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1083. search_box = memnew(LineEdit);
  1084. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1085. search_box->set_placeholder(TTR("Filter Meshes"));
  1086. search_box->set_clear_button_enabled(true);
  1087. hb->add_child(search_box);
  1088. search_box->connect("text_changed", callable_mp(this, &GridMapEditor::_text_changed));
  1089. search_box->connect("gui_input", callable_mp(this, &GridMapEditor::_sbox_input));
  1090. mode_thumbnail = memnew(Button);
  1091. mode_thumbnail->set_theme_type_variation("FlatButton");
  1092. mode_thumbnail->set_toggle_mode(true);
  1093. mode_thumbnail->set_pressed(true);
  1094. hb->add_child(mode_thumbnail);
  1095. mode_thumbnail->connect("pressed", callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_THUMBNAIL));
  1096. mode_list = memnew(Button);
  1097. mode_list->set_theme_type_variation("FlatButton");
  1098. mode_list->set_toggle_mode(true);
  1099. mode_list->set_pressed(false);
  1100. hb->add_child(mode_list);
  1101. mode_list->connect("pressed", callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_LIST));
  1102. size_slider = memnew(HSlider);
  1103. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  1104. size_slider->set_min(0.2f);
  1105. size_slider->set_max(4.0f);
  1106. size_slider->set_step(0.1f);
  1107. size_slider->set_value(1.0f);
  1108. size_slider->connect("value_changed", callable_mp(this, &GridMapEditor::_icon_size_changed));
  1109. add_child(size_slider);
  1110. EDITOR_DEF("editors/grid_map/preview_size", 64);
  1111. mesh_library_palette = memnew(ItemList);
  1112. add_child(mesh_library_palette);
  1113. mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL);
  1114. mesh_library_palette->connect("gui_input", callable_mp(this, &GridMapEditor::_mesh_library_palette_input));
  1115. info_message = memnew(Label);
  1116. info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes."));
  1117. info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1118. info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1119. info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  1120. info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  1121. info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  1122. mesh_library_palette->add_child(info_message);
  1123. edit_axis = Vector3::AXIS_Y;
  1124. edit_floor[0] = -1;
  1125. edit_floor[1] = -1;
  1126. edit_floor[2] = -1;
  1127. selection_mesh = RenderingServer::get_singleton()->mesh_create();
  1128. paste_mesh = RenderingServer::get_singleton()->mesh_create();
  1129. {
  1130. // Selection mesh create.
  1131. Vector<Vector3> lines;
  1132. Vector<Vector3> triangles;
  1133. Vector<Vector3> square[3];
  1134. for (int i = 0; i < 6; i++) {
  1135. Vector3 face_points[4];
  1136. for (int j = 0; j < 4; j++) {
  1137. float v[3];
  1138. v[0] = 1.0;
  1139. v[1] = 1 - 2 * ((j >> 1) & 1);
  1140. v[2] = v[1] * (1 - 2 * (j & 1));
  1141. for (int k = 0; k < 3; k++) {
  1142. if (i < 3) {
  1143. face_points[j][(i + k) % 3] = v[k];
  1144. } else {
  1145. face_points[3 - j][(i + k) % 3] = -v[k];
  1146. }
  1147. }
  1148. }
  1149. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1150. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1151. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1152. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1153. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1154. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1155. }
  1156. for (int i = 0; i < 12; i++) {
  1157. AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  1158. Vector3 a, b;
  1159. base.get_edge(i, a, b);
  1160. lines.push_back(a);
  1161. lines.push_back(b);
  1162. }
  1163. for (int i = 0; i < 3; i++) {
  1164. Vector3 points[4];
  1165. for (int j = 0; j < 4; j++) {
  1166. static const bool orderx[4] = { false, true, true, false };
  1167. static const bool ordery[4] = { false, false, true, true };
  1168. Vector3 sp;
  1169. if (orderx[j]) {
  1170. sp[(i + 1) % 3] = 1.0;
  1171. }
  1172. if (ordery[j]) {
  1173. sp[(i + 2) % 3] = 1.0;
  1174. }
  1175. points[j] = sp;
  1176. }
  1177. for (int j = 0; j < 4; j++) {
  1178. Vector3 ofs;
  1179. ofs[i] += 0.01;
  1180. square[i].push_back(points[j] - ofs);
  1181. square[i].push_back(points[(j + 1) % 4] - ofs);
  1182. square[i].push_back(points[j] + ofs);
  1183. square[i].push_back(points[(j + 1) % 4] + ofs);
  1184. }
  1185. }
  1186. Array d;
  1187. d.resize(RS::ARRAY_MAX);
  1188. inner_mat.instantiate();
  1189. inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2));
  1190. inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1191. inner_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1192. inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1193. d[RS::ARRAY_VERTEX] = triangles;
  1194. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_TRIANGLES, d);
  1195. RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  1196. outer_mat.instantiate();
  1197. outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8));
  1198. outer_mat->set_on_top_of_alpha();
  1199. outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1200. outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1201. outer_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1202. selection_floor_mat.instantiate();
  1203. selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
  1204. selection_floor_mat->set_on_top_of_alpha();
  1205. selection_floor_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1206. selection_floor_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1207. d[RS::ARRAY_VERTEX] = lines;
  1208. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_LINES, d);
  1209. RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  1210. d[RS::ARRAY_VERTEX] = triangles;
  1211. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_TRIANGLES, d);
  1212. RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid());
  1213. d[RS::ARRAY_VERTEX] = lines;
  1214. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_LINES, d);
  1215. RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid());
  1216. for (int i = 0; i < 3; i++) {
  1217. d[RS::ARRAY_VERTEX] = square[i];
  1218. selection_level_mesh[i] = RS::get_singleton()->mesh_create();
  1219. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], RS::PRIMITIVE_LINES, d);
  1220. RenderingServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
  1221. }
  1222. }
  1223. _set_selection(false);
  1224. indicator_mat.instantiate();
  1225. indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1226. indicator_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1227. indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  1228. indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1229. indicator_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1230. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  1231. }
  1232. GridMapEditor::~GridMapEditor() {
  1233. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1234. _clear_clipboard_data();
  1235. for (int i = 0; i < 3; i++) {
  1236. if (grid[i].is_valid()) {
  1237. RenderingServer::get_singleton()->free(grid[i]);
  1238. }
  1239. if (grid_instance[i].is_valid()) {
  1240. RenderingServer::get_singleton()->free(grid_instance[i]);
  1241. }
  1242. if (cursor_instance.is_valid()) {
  1243. RenderingServer::get_singleton()->free(cursor_instance);
  1244. }
  1245. if (selection_level_instance[i].is_valid()) {
  1246. RenderingServer::get_singleton()->free(selection_level_instance[i]);
  1247. }
  1248. if (selection_level_mesh[i].is_valid()) {
  1249. RenderingServer::get_singleton()->free(selection_level_mesh[i]);
  1250. }
  1251. }
  1252. RenderingServer::get_singleton()->free(selection_mesh);
  1253. if (selection_instance.is_valid()) {
  1254. RenderingServer::get_singleton()->free(selection_instance);
  1255. }
  1256. RenderingServer::get_singleton()->free(paste_mesh);
  1257. if (paste_instance.is_valid()) {
  1258. RenderingServer::get_singleton()->free(paste_instance);
  1259. }
  1260. }
  1261. void GridMapEditorPlugin::_notification(int p_what) {
  1262. switch (p_what) {
  1263. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1264. switch ((int)EDITOR_GET("editors/grid_map/editor_side")) {
  1265. case 0: { // Left.
  1266. Node3DEditor::get_singleton()->move_control_to_left_panel(grid_map_editor);
  1267. } break;
  1268. case 1: { // Right.
  1269. Node3DEditor::get_singleton()->move_control_to_right_panel(grid_map_editor);
  1270. } break;
  1271. }
  1272. } break;
  1273. }
  1274. }
  1275. void GridMapEditorPlugin::edit(Object *p_object) {
  1276. grid_map_editor->edit(Object::cast_to<GridMap>(p_object));
  1277. }
  1278. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1279. return p_object->is_class("GridMap");
  1280. }
  1281. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1282. if (p_visible) {
  1283. grid_map_editor->show();
  1284. grid_map_editor->spatial_editor_hb->show();
  1285. grid_map_editor->set_process(true);
  1286. } else {
  1287. grid_map_editor->spatial_editor_hb->hide();
  1288. grid_map_editor->hide();
  1289. grid_map_editor->set_process(false);
  1290. }
  1291. }
  1292. GridMapEditorPlugin::GridMapEditorPlugin() {
  1293. EDITOR_DEF("editors/grid_map/editor_side", 1);
  1294. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/grid_map/editor_side", PROPERTY_HINT_ENUM, "Left,Right"));
  1295. grid_map_editor = memnew(GridMapEditor);
  1296. switch ((int)EDITOR_GET("editors/grid_map/editor_side")) {
  1297. case 0: { // Left.
  1298. Node3DEditor::get_singleton()->add_control_to_left_panel(grid_map_editor);
  1299. } break;
  1300. case 1: { // Right.
  1301. Node3DEditor::get_singleton()->add_control_to_right_panel(grid_map_editor);
  1302. } break;
  1303. }
  1304. grid_map_editor->hide();
  1305. }
  1306. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1307. }
  1308. #endif // TOOLS_ENABLED