grid_map_editor_plugin.cpp 51 KB

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