grid_map_editor_plugin.cpp 51 KB

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