grid_map_editor_plugin.cpp 51 KB

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