grid_map_editor_plugin.cpp 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  1. /**************************************************************************/
  2. /* grid_map_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "grid_map_editor_plugin.h"
  31. #ifdef TOOLS_ENABLED
  32. #include "core/input/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_command_palette.h"
  35. #include "editor/editor_main_screen.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/gui/editor_bottom_panel.h"
  41. #include "editor/gui/editor_zoom_widget.h"
  42. #include "editor/plugins/node_3d_editor_plugin.h"
  43. #include "editor/themes/editor_scale.h"
  44. #include "scene/3d/camera_3d.h"
  45. #include "scene/gui/dialogs.h"
  46. #include "scene/gui/label.h"
  47. #include "scene/gui/menu_button.h"
  48. #include "scene/gui/separator.h"
  49. #include "scene/main/window.h"
  50. void GridMapEditor::_configure() {
  51. if (!node) {
  52. return;
  53. }
  54. update_grid();
  55. }
  56. void GridMapEditor::_menu_option(int p_option) {
  57. switch (p_option) {
  58. case MENU_OPTION_PREV_LEVEL: {
  59. floor->set_value(floor->get_value() - 1);
  60. if (selection.active && input_action == INPUT_SELECT) {
  61. selection.current[edit_axis]--;
  62. _validate_selection();
  63. }
  64. } break;
  65. case MENU_OPTION_NEXT_LEVEL: {
  66. floor->set_value(floor->get_value() + 1);
  67. if (selection.active && input_action == INPUT_SELECT) {
  68. selection.current[edit_axis]++;
  69. _validate_selection();
  70. }
  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. if (edit_axis == Vector3::AXIS_Y) {
  82. floor->set_tooltip_text("Change Grid Plane");
  83. } else if (new_axis == Vector3::AXIS_Y) {
  84. floor->set_tooltip_text("Change Grid Floor");
  85. }
  86. }
  87. edit_axis = Vector3::Axis(new_axis);
  88. update_grid();
  89. } break;
  90. case MENU_OPTION_CURSOR_ROTATE_Y: {
  91. Basis r;
  92. if (input_action == INPUT_PASTE) {
  93. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  94. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  95. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  96. _update_paste_indicator();
  97. break;
  98. }
  99. r = node->get_basis_with_orthogonal_index(cursor_rot);
  100. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  101. cursor_rot = node->get_orthogonal_index_from_basis(r);
  102. _update_cursor_transform();
  103. } break;
  104. case MENU_OPTION_CURSOR_ROTATE_X: {
  105. Basis r;
  106. if (input_action == INPUT_PASTE) {
  107. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  108. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  109. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  110. _update_paste_indicator();
  111. break;
  112. }
  113. r = node->get_basis_with_orthogonal_index(cursor_rot);
  114. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  115. cursor_rot = node->get_orthogonal_index_from_basis(r);
  116. _update_cursor_transform();
  117. } break;
  118. case MENU_OPTION_CURSOR_ROTATE_Z: {
  119. Basis r;
  120. if (input_action == INPUT_PASTE) {
  121. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  122. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  123. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  124. _update_paste_indicator();
  125. break;
  126. }
  127. r = node->get_basis_with_orthogonal_index(cursor_rot);
  128. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  129. cursor_rot = node->get_orthogonal_index_from_basis(r);
  130. _update_cursor_transform();
  131. } break;
  132. case MENU_OPTION_CURSOR_BACK_ROTATE_Y: {
  133. Basis r;
  134. if (input_action == INPUT_PASTE) {
  135. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  136. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  137. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  138. _update_paste_indicator();
  139. break;
  140. }
  141. r = node->get_basis_with_orthogonal_index(cursor_rot);
  142. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  143. cursor_rot = node->get_orthogonal_index_from_basis(r);
  144. _update_cursor_transform();
  145. } break;
  146. case MENU_OPTION_CURSOR_BACK_ROTATE_X: {
  147. Basis r;
  148. if (input_action == INPUT_PASTE) {
  149. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  150. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  151. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  152. _update_paste_indicator();
  153. break;
  154. }
  155. r = node->get_basis_with_orthogonal_index(cursor_rot);
  156. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  157. cursor_rot = node->get_orthogonal_index_from_basis(r);
  158. _update_cursor_transform();
  159. } break;
  160. case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
  161. Basis r;
  162. if (input_action == INPUT_PASTE) {
  163. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  164. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  165. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  166. _update_paste_indicator();
  167. break;
  168. }
  169. r = node->get_basis_with_orthogonal_index(cursor_rot);
  170. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  171. cursor_rot = node->get_orthogonal_index_from_basis(r);
  172. _update_cursor_transform();
  173. } break;
  174. case MENU_OPTION_CURSOR_CLEAR_ROTATION: {
  175. if (input_action == INPUT_PASTE) {
  176. paste_indicator.orientation = 0;
  177. _update_paste_indicator();
  178. break;
  179. }
  180. cursor_rot = 0;
  181. _update_cursor_transform();
  182. } break;
  183. case MENU_OPTION_PASTE_SELECTS: {
  184. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  185. options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
  186. } break;
  187. case MENU_OPTION_SELECTION_DUPLICATE:
  188. case MENU_OPTION_SELECTION_CUT: {
  189. if (!(selection.active && input_action == INPUT_NONE)) {
  190. break;
  191. }
  192. _set_clipboard_data();
  193. if (p_option == MENU_OPTION_SELECTION_CUT) {
  194. _delete_selection();
  195. }
  196. input_action = INPUT_PASTE;
  197. paste_indicator.click = selection.begin;
  198. paste_indicator.current = selection.begin;
  199. paste_indicator.begin = selection.begin;
  200. paste_indicator.end = selection.end;
  201. paste_indicator.orientation = 0;
  202. _update_paste_indicator();
  203. } break;
  204. case MENU_OPTION_SELECTION_CLEAR: {
  205. if (!selection.active) {
  206. break;
  207. }
  208. _delete_selection();
  209. } break;
  210. case MENU_OPTION_SELECTION_FILL: {
  211. if (!selection.active) {
  212. return;
  213. }
  214. _fill_selection();
  215. } break;
  216. case MENU_OPTION_GRIDMAP_SETTINGS: {
  217. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE);
  218. } break;
  219. }
  220. }
  221. void GridMapEditor::_update_cursor_transform() {
  222. cursor_transform = Transform3D();
  223. cursor_transform.origin = cursor_origin;
  224. cursor_transform.basis *= node->get_cell_scale();
  225. cursor_transform = node->get_global_transform() * cursor_transform;
  226. if (mode_buttons_group->get_pressed_button() == paint_mode_button) {
  227. // Rotation is only applied in paint mode, we don't want the cursor box to rotate otherwise.
  228. cursor_transform.basis = node->get_basis_with_orthogonal_index(cursor_rot);
  229. if (selected_palette >= 0 && node && node->get_mesh_library().is_valid()) {
  230. cursor_transform *= node->get_mesh_library()->get_item_mesh_transform(selected_palette);
  231. }
  232. } else {
  233. Transform3D xf;
  234. xf.scale(node->get_cell_size());
  235. xf.origin.x = node->get_center_x() ? -node->get_cell_size().x / 2 : 0;
  236. xf.origin.y = node->get_center_y() ? -node->get_cell_size().y / 2 : 0;
  237. xf.origin.z = node->get_center_z() ? -node->get_cell_size().z / 2 : 0;
  238. cursor_transform *= xf;
  239. }
  240. if (cursor_instance.is_valid()) {
  241. RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  242. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
  243. }
  244. }
  245. void GridMapEditor::_update_selection_transform() {
  246. Transform3D xf_zero;
  247. xf_zero.basis.set_zero();
  248. if (!selection.active) {
  249. RenderingServer::get_singleton()->instance_set_transform(selection_instance, xf_zero);
  250. for (int i = 0; i < 3; i++) {
  251. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  252. }
  253. return;
  254. }
  255. Transform3D xf;
  256. xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  257. xf.origin = selection.begin * node->get_cell_size();
  258. RenderingServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  259. for (int i = 0; i < 3; i++) {
  260. if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) {
  261. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  262. } else {
  263. Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
  264. scale[edit_axis] = 1.0;
  265. Vector3 position = selection.begin;
  266. position[edit_axis] = edit_floor[edit_axis];
  267. scale *= node->get_cell_size();
  268. position *= node->get_cell_size();
  269. Transform3D xf2;
  270. xf2.basis.scale(scale);
  271. xf2.origin = position;
  272. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], node->get_global_transform() * xf2);
  273. }
  274. }
  275. }
  276. void GridMapEditor::_validate_selection() {
  277. if (!selection.active) {
  278. return;
  279. }
  280. selection.begin = selection.click;
  281. selection.end = selection.current;
  282. if (selection.begin.x > selection.end.x) {
  283. SWAP(selection.begin.x, selection.end.x);
  284. }
  285. if (selection.begin.y > selection.end.y) {
  286. SWAP(selection.begin.y, selection.end.y);
  287. }
  288. if (selection.begin.z > selection.end.z) {
  289. SWAP(selection.begin.z, selection.end.z);
  290. }
  291. _update_selection_transform();
  292. }
  293. void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) {
  294. selection.active = p_active;
  295. selection.begin = p_begin;
  296. selection.end = p_end;
  297. selection.click = p_begin;
  298. selection.current = p_end;
  299. if (is_visible_in_tree()) {
  300. _update_selection_transform();
  301. }
  302. }
  303. bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click) {
  304. if (!spatial_editor) {
  305. return false;
  306. }
  307. if (input_action == INPUT_TRANSFORM) {
  308. return false;
  309. }
  310. if (selected_palette < 0 && input_action != INPUT_NONE && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE) {
  311. return false;
  312. }
  313. if (mesh_library.is_null()) {
  314. return false;
  315. }
  316. if (input_action != INPUT_NONE && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette)) {
  317. return false;
  318. }
  319. Camera3D *camera = p_camera;
  320. Vector3 from = camera->project_ray_origin(p_point);
  321. Vector3 normal = camera->project_ray_normal(p_point);
  322. Transform3D local_xform = node->get_global_transform().affine_inverse();
  323. Vector<Plane> planes = camera->get_frustum();
  324. from = local_xform.xform(from);
  325. normal = local_xform.basis.xform(normal).normalized();
  326. Plane p;
  327. p.normal[edit_axis] = 1.0;
  328. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  329. Vector3 inters;
  330. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters)) {
  331. return false;
  332. }
  333. // Make sure the intersection is inside the frustum planes, to avoid
  334. // Painting on invisible regions.
  335. for (int i = 0; i < planes.size(); i++) {
  336. Plane fp = local_xform.xform(planes[i]);
  337. if (fp.is_point_over(inters)) {
  338. return false;
  339. }
  340. }
  341. int cell[3];
  342. Vector3 cell_size = node->get_cell_size();
  343. for (int i = 0; i < 3; i++) {
  344. if (i == edit_axis) {
  345. cell[i] = edit_floor[i];
  346. } else {
  347. cell[i] = inters[i] / cell_size[i];
  348. if (inters[i] < 0) {
  349. cell[i] -= 1; // Compensate negative.
  350. }
  351. grid_ofs[i] = cell[i] * cell_size[i];
  352. }
  353. }
  354. RS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform);
  355. if (cursor_instance.is_valid()) {
  356. 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();
  357. cursor_visible = true;
  358. if (input_action == INPUT_PASTE) {
  359. cursor_visible = false;
  360. }
  361. _update_cursor_transform();
  362. }
  363. if (input_action == INPUT_NONE) {
  364. return false;
  365. }
  366. if (input_action == INPUT_PASTE) {
  367. paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]);
  368. _update_paste_indicator();
  369. } else if (input_action == INPUT_SELECT) {
  370. selection.current = Vector3i(cell[0], cell[1], cell[2]);
  371. if (p_click) {
  372. selection.click = selection.current;
  373. }
  374. selection.active = true;
  375. _validate_selection();
  376. return true;
  377. } else if (input_action == INPUT_PICK) {
  378. int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  379. if (item >= 0) {
  380. selected_palette = item;
  381. // Clear the filter if picked an item that's filtered out.
  382. int index = mesh_library_palette->find_metadata(item);
  383. if (index == -1) {
  384. search_box->clear();
  385. }
  386. // This will select `selected_palette` in the ItemList when possible.
  387. update_palette();
  388. _update_cursor_instance();
  389. }
  390. return true;
  391. }
  392. if (input_action == INPUT_PAINT) {
  393. SetItem si;
  394. si.position = Vector3i(cell[0], cell[1], cell[2]);
  395. si.new_value = selected_palette;
  396. si.new_orientation = cursor_rot;
  397. si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  398. si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
  399. set_items.push_back(si);
  400. node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot);
  401. return true;
  402. } else if (input_action == INPUT_ERASE) {
  403. SetItem si;
  404. si.position = Vector3i(cell[0], cell[1], cell[2]);
  405. si.new_value = -1;
  406. si.new_orientation = 0;
  407. si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  408. si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
  409. set_items.push_back(si);
  410. node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1);
  411. return true;
  412. }
  413. return false;
  414. }
  415. void GridMapEditor::_delete_selection() {
  416. if (!selection.active) {
  417. return;
  418. }
  419. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  420. undo_redo->create_action(TTR("GridMap Delete 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, GridMap::INVALID_CELL_ITEM);
  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::_fill_selection() {
  435. if (!selection.active) {
  436. return;
  437. }
  438. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  439. undo_redo->create_action(TTR("GridMap Fill Selection"));
  440. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  441. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  442. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  443. Vector3i selected = Vector3i(i, j, k);
  444. undo_redo->add_do_method(node, "set_cell_item", selected, selected_palette, cursor_rot);
  445. undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
  446. }
  447. }
  448. }
  449. undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
  450. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  451. undo_redo->commit_action();
  452. }
  453. void GridMapEditor::_clear_clipboard_data() {
  454. for (const ClipboardItem &E : clipboard_items) {
  455. RenderingServer::get_singleton()->free(E.instance);
  456. }
  457. clipboard_items.clear();
  458. }
  459. void GridMapEditor::_set_clipboard_data() {
  460. _clear_clipboard_data();
  461. Ref<MeshLibrary> meshLibrary = node->get_mesh_library();
  462. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  463. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  464. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  465. Vector3i selected = Vector3i(i, j, k);
  466. int itm = node->get_cell_item(selected);
  467. if (itm == GridMap::INVALID_CELL_ITEM) {
  468. continue;
  469. }
  470. Ref<Mesh> mesh = meshLibrary->get_item_mesh(itm);
  471. ClipboardItem item;
  472. item.cell_item = itm;
  473. item.grid_offset = Vector3(selected) - selection.begin;
  474. item.orientation = node->get_cell_item_orientation(selected);
  475. item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());
  476. clipboard_items.push_back(item);
  477. }
  478. }
  479. }
  480. }
  481. void GridMapEditor::_update_paste_indicator() {
  482. if (input_action != INPUT_PASTE) {
  483. Transform3D xf;
  484. xf.basis.set_zero();
  485. RenderingServer::get_singleton()->instance_set_transform(paste_instance, xf);
  486. return;
  487. }
  488. Vector3 center = 0.5 * Vector3(real_t(node->get_center_x()), real_t(node->get_center_y()), real_t(node->get_center_z()));
  489. Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size();
  490. Transform3D xf;
  491. xf.scale(scale);
  492. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  493. Basis rot;
  494. rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  495. xf.basis = rot * xf.basis;
  496. xf.translate_local((-center * node->get_cell_size()) / scale);
  497. RenderingServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf);
  498. for (const ClipboardItem &item : clipboard_items) {
  499. xf = Transform3D();
  500. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  501. xf.basis = rot * xf.basis;
  502. xf.translate_local(item.grid_offset * node->get_cell_size());
  503. Basis item_rot;
  504. item_rot = node->get_basis_with_orthogonal_index(item.orientation);
  505. xf.basis = item_rot * xf.basis * node->get_cell_scale();
  506. RenderingServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf);
  507. }
  508. }
  509. void GridMapEditor::_do_paste() {
  510. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  511. bool reselect = options->get_popup()->is_item_checked(idx);
  512. Basis rot;
  513. rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  514. Vector3 ofs = paste_indicator.current - paste_indicator.click;
  515. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  516. undo_redo->create_action(TTR("GridMap Paste Selection"));
  517. for (const ClipboardItem &item : clipboard_items) {
  518. Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;
  519. Basis orm;
  520. orm = node->get_basis_with_orthogonal_index(item.orientation);
  521. orm = rot * orm;
  522. undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, node->get_orthogonal_index_from_basis(orm));
  523. undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position));
  524. }
  525. if (reselect) {
  526. // We need to rotate the paste_indicator to find the selection begin and end:
  527. Vector3 temp_end = rot.xform(paste_indicator.end - paste_indicator.begin) + paste_indicator.begin + ofs;
  528. Vector3 temp_begin = paste_indicator.begin + ofs;
  529. // _set_selection expects that selection_begin is the corner closer to the origin:
  530. for (int i = 0; i < 3; ++i) {
  531. if (temp_begin[i] > temp_end[i]) {
  532. float p = temp_begin[i];
  533. temp_begin[i] = temp_end[i];
  534. temp_end[i] = p;
  535. }
  536. }
  537. undo_redo->add_do_method(this, "_set_selection", true, temp_begin, temp_end);
  538. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  539. }
  540. undo_redo->commit_action();
  541. _clear_clipboard_data();
  542. }
  543. void GridMapEditor::_show_viewports_transform_gizmo(bool p_value) {
  544. Dictionary new_state;
  545. new_state["transform_gizmo"] = p_value;
  546. for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
  547. Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(i);
  548. viewport->set_state(new_state);
  549. }
  550. }
  551. EditorPlugin::AfterGUIInput GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  552. if (!node) {
  553. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  554. }
  555. Ref<InputEventKey> k = p_event;
  556. if (k.is_valid() && k->is_pressed() && !k->is_echo()) {
  557. // Transform mode (toggle button):
  558. // If we are in Transform mode we pass the events to the 3D editor,
  559. // but if the Transform mode shortcut is pressed again, we go back to Selection mode.
  560. if (mode_buttons_group->get_pressed_button() == transform_mode_button) {
  561. if (transform_mode_button->get_shortcut().is_valid() && transform_mode_button->get_shortcut()->matches_event(p_event)) {
  562. select_mode_button->set_pressed(true);
  563. accept_event();
  564. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  565. }
  566. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  567. }
  568. // Tool modes and tool actions:
  569. for (BaseButton *b : viewport_shortcut_buttons) {
  570. if (b->is_disabled()) {
  571. continue;
  572. }
  573. if (b->get_shortcut().is_valid() && b->get_shortcut()->matches_event(p_event)) {
  574. if (b->is_toggle_mode()) {
  575. b->set_pressed(b->get_button_group().is_valid() || !b->is_pressed());
  576. } else {
  577. // Can't press a button without toggle mode, so just emit the signal directly.
  578. b->emit_signal(SceneStringName(pressed));
  579. }
  580. accept_event();
  581. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  582. }
  583. }
  584. // Hard key actions:
  585. if (k->get_keycode() == Key::ESCAPE) {
  586. if (input_action == INPUT_PASTE) {
  587. _clear_clipboard_data();
  588. input_action = INPUT_NONE;
  589. _update_paste_indicator();
  590. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  591. } else if (selection.active) {
  592. _set_selection(false);
  593. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  594. } else {
  595. input_action = INPUT_NONE;
  596. update_palette();
  597. _update_cursor_instance();
  598. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  599. }
  600. }
  601. // Options menu shortcuts:
  602. Ref<Shortcut> ed_shortcut = ED_GET_SHORTCUT("grid_map/previous_floor");
  603. if (ed_shortcut.is_valid() && ed_shortcut->matches_event(p_event)) {
  604. accept_event();
  605. _menu_option(MENU_OPTION_PREV_LEVEL);
  606. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  607. }
  608. ed_shortcut = ED_GET_SHORTCUT("grid_map/next_floor");
  609. if (ed_shortcut.is_valid() && ed_shortcut->matches_event(p_event)) {
  610. accept_event();
  611. _menu_option(MENU_OPTION_NEXT_LEVEL);
  612. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  613. }
  614. for (int i = 0; i < options->get_popup()->get_item_count(); ++i) {
  615. const Ref<Shortcut> &shortcut = options->get_popup()->get_item_shortcut(i);
  616. if (shortcut.is_valid() && shortcut->matches_event(p_event)) {
  617. // Consume input to avoid conflicts with other plugins.
  618. accept_event();
  619. _menu_option(options->get_popup()->get_item_id(i));
  620. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  621. }
  622. }
  623. }
  624. Ref<InputEventMouseButton> mb = p_event;
  625. if (mb.is_valid()) {
  626. if (mb->get_button_index() == MouseButton::WHEEL_UP && (mb->is_command_or_control_pressed())) {
  627. if (mb->is_pressed()) {
  628. floor->set_value(floor->get_value() + mb->get_factor());
  629. }
  630. return EditorPlugin::AFTER_GUI_INPUT_STOP; // Eaten.
  631. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && (mb->is_command_or_control_pressed())) {
  632. if (mb->is_pressed()) {
  633. floor->set_value(floor->get_value() - mb->get_factor());
  634. }
  635. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  636. }
  637. if (mb->is_pressed()) {
  638. Node3DEditorViewport::NavigationScheme nav_scheme = (Node3DEditorViewport::NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme").operator int();
  639. if ((nav_scheme == Node3DEditorViewport::NAVIGATION_MAYA || nav_scheme == Node3DEditorViewport::NAVIGATION_MODO) && mb->is_alt_pressed()) {
  640. input_action = INPUT_NONE;
  641. } else if (mb->get_button_index() == MouseButton::LEFT) {
  642. bool can_edit = (node && node->get_mesh_library().is_valid());
  643. if (input_action == INPUT_PASTE) {
  644. _do_paste();
  645. input_action = INPUT_NONE;
  646. _update_paste_indicator();
  647. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  648. } else if (mode_buttons_group->get_pressed_button() == select_mode_button && can_edit) {
  649. input_action = INPUT_SELECT;
  650. last_selection = selection;
  651. } else if (mode_buttons_group->get_pressed_button() == pick_mode_button && can_edit) {
  652. input_action = INPUT_PICK;
  653. } else if (mode_buttons_group->get_pressed_button() == paint_mode_button && can_edit) {
  654. input_action = INPUT_PAINT;
  655. set_items.clear();
  656. } else if (mode_buttons_group->get_pressed_button() == erase_mode_button && can_edit) {
  657. input_action = INPUT_ERASE;
  658. set_items.clear();
  659. }
  660. } else if (mb->get_button_index() == MouseButton::RIGHT) {
  661. if (input_action == INPUT_PASTE) {
  662. _clear_clipboard_data();
  663. input_action = INPUT_NONE;
  664. _update_paste_indicator();
  665. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  666. } else if (selection.active) {
  667. _set_selection(false);
  668. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  669. }
  670. } else {
  671. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  672. }
  673. if (do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true)) {
  674. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  675. }
  676. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  677. } else {
  678. if ((mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_ERASE) || (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_PAINT)) {
  679. if (set_items.size()) {
  680. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  681. undo_redo->create_action(TTR("GridMap Paint"));
  682. for (const SetItem &si : set_items) {
  683. undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation);
  684. }
  685. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  686. const SetItem &si = E->get();
  687. undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation);
  688. }
  689. undo_redo->commit_action();
  690. }
  691. set_items.clear();
  692. input_action = INPUT_NONE;
  693. if (set_items.size() > 0) {
  694. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  695. }
  696. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  697. }
  698. if (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_SELECT) {
  699. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  700. undo_redo->create_action(TTR("GridMap Selection"));
  701. undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  702. undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end);
  703. undo_redo->commit_action();
  704. }
  705. if (mb->get_button_index() == MouseButton::LEFT && input_action != INPUT_NONE) {
  706. set_items.clear();
  707. input_action = INPUT_NONE;
  708. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  709. }
  710. if (mb->get_button_index() == MouseButton::RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) {
  711. input_action = INPUT_NONE;
  712. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  713. }
  714. }
  715. }
  716. Ref<InputEventMouseMotion> mm = p_event;
  717. if (mm.is_valid()) {
  718. // Update the grid, to check if the grid needs to be moved to a tile cursor.
  719. update_grid();
  720. if (do_input_action(p_camera, mm->get_position(), false)) {
  721. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  722. }
  723. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  724. }
  725. Ref<InputEventPanGesture> pan_gesture = p_event;
  726. if (pan_gesture.is_valid()) {
  727. if (pan_gesture->is_alt_pressed() && pan_gesture->is_command_or_control_pressed()) {
  728. const real_t delta = pan_gesture->get_delta().y * 0.5;
  729. accumulated_floor_delta += delta;
  730. int step = 0;
  731. if (ABS(accumulated_floor_delta) > 1.0) {
  732. step = SIGN(accumulated_floor_delta);
  733. accumulated_floor_delta -= step;
  734. }
  735. if (step) {
  736. floor->set_value(floor->get_value() + step);
  737. }
  738. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  739. }
  740. }
  741. accumulated_floor_delta = 0.0;
  742. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  743. }
  744. struct _CGMEItemSort {
  745. String name;
  746. int id = 0;
  747. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  748. };
  749. void GridMapEditor::_set_display_mode(int p_mode) {
  750. if (display_mode == p_mode) {
  751. return;
  752. }
  753. if (p_mode == DISPLAY_LIST) {
  754. mode_list->set_pressed(true);
  755. mode_thumbnail->set_pressed(false);
  756. } else if (p_mode == DISPLAY_THUMBNAIL) {
  757. mode_list->set_pressed(false);
  758. mode_thumbnail->set_pressed(true);
  759. }
  760. display_mode = p_mode;
  761. update_palette();
  762. }
  763. void GridMapEditor::_text_changed(const String &p_text) {
  764. update_palette();
  765. }
  766. void GridMapEditor::_sbox_input(const Ref<InputEvent> &p_event) {
  767. // Redirect navigational key events to the item list.
  768. Ref<InputEventKey> key = p_event;
  769. if (key.is_valid()) {
  770. if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
  771. mesh_library_palette->gui_input(key);
  772. search_box->accept_event();
  773. }
  774. }
  775. }
  776. void GridMapEditor::_mesh_library_palette_input(const Ref<InputEvent> &p_ie) {
  777. const Ref<InputEventMouseButton> mb = p_ie;
  778. // Zoom in/out using Ctrl + mouse wheel
  779. if (mb.is_valid() && mb->is_pressed() && mb->is_command_or_control_pressed()) {
  780. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) {
  781. zoom_widget->set_zoom(zoom_widget->get_zoom() + 0.2);
  782. zoom_widget->emit_signal(SNAME("zoom_changed"), zoom_widget->get_zoom());
  783. }
  784. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  785. zoom_widget->set_zoom(zoom_widget->get_zoom() - 0.2);
  786. zoom_widget->emit_signal(SNAME("zoom_changed"), zoom_widget->get_zoom());
  787. }
  788. }
  789. }
  790. void GridMapEditor::_icon_size_changed(float p_value) {
  791. mesh_library_palette->set_icon_scale(p_value);
  792. update_palette();
  793. }
  794. void GridMapEditor::update_palette() {
  795. float min_size = EDITOR_GET("editors/grid_map/preview_size");
  796. min_size *= EDSCALE;
  797. mesh_library_palette->clear();
  798. if (display_mode == DISPLAY_THUMBNAIL) {
  799. mesh_library_palette->set_max_columns(0);
  800. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  801. mesh_library_palette->set_fixed_column_width(min_size * MAX(zoom_widget->get_zoom(), 1.5));
  802. } else if (display_mode == DISPLAY_LIST) {
  803. mesh_library_palette->set_max_columns(0);
  804. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT);
  805. mesh_library_palette->set_fixed_column_width(0);
  806. }
  807. mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size));
  808. mesh_library_palette->set_max_text_lines(2);
  809. if (mesh_library.is_null()) {
  810. search_box->set_text("");
  811. search_box->set_editable(false);
  812. info_message->show();
  813. return;
  814. }
  815. search_box->set_editable(true);
  816. info_message->hide();
  817. Vector<int> ids;
  818. ids = mesh_library->get_item_list();
  819. List<_CGMEItemSort> il;
  820. for (int i = 0; i < ids.size(); i++) {
  821. _CGMEItemSort is;
  822. is.id = ids[i];
  823. is.name = mesh_library->get_item_name(ids[i]);
  824. il.push_back(is);
  825. }
  826. il.sort();
  827. String filter = search_box->get_text().strip_edges();
  828. int item = 0;
  829. for (_CGMEItemSort &E : il) {
  830. int id = E.id;
  831. String name = mesh_library->get_item_name(id);
  832. Ref<Texture2D> preview = mesh_library->get_item_preview(id);
  833. if (name.is_empty()) {
  834. name = "#" + itos(id);
  835. }
  836. if (!filter.is_empty() && !filter.is_subsequence_ofn(name)) {
  837. continue;
  838. }
  839. mesh_library_palette->add_item("");
  840. if (!preview.is_null()) {
  841. mesh_library_palette->set_item_icon(item, preview);
  842. mesh_library_palette->set_item_tooltip(item, name);
  843. }
  844. mesh_library_palette->set_item_text(item, name);
  845. mesh_library_palette->set_item_metadata(item, id);
  846. if (selected_palette == id) {
  847. mesh_library_palette->select(item);
  848. }
  849. item++;
  850. }
  851. }
  852. void GridMapEditor::_update_mesh_library() {
  853. ERR_FAIL_NULL(node);
  854. Ref<MeshLibrary> new_mesh_library = node->get_mesh_library();
  855. if (new_mesh_library != mesh_library) {
  856. if (mesh_library.is_valid()) {
  857. mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette));
  858. }
  859. mesh_library = new_mesh_library;
  860. } else {
  861. return;
  862. }
  863. if (mesh_library.is_valid()) {
  864. mesh_library->connect_changed(callable_mp(this, &GridMapEditor::update_palette));
  865. }
  866. update_palette();
  867. // Make sure we select the first tile as default possible.
  868. if (mesh_library_palette->get_current() == -1 && mesh_library_palette->get_item_count() > 0) {
  869. mesh_library_palette->set_current(0);
  870. selected_palette = mesh_library_palette->get_item_metadata(0);
  871. }
  872. // Update the cursor and grid in case the library is changed or removed.
  873. _update_cursor_instance();
  874. update_grid();
  875. }
  876. void GridMapEditor::edit(GridMap *p_gridmap) {
  877. if (node) {
  878. node->disconnect(SNAME("cell_size_changed"), callable_mp(this, &GridMapEditor::_draw_grids));
  879. node->disconnect(CoreStringName(changed), callable_mp(this, &GridMapEditor::_update_mesh_library));
  880. if (mesh_library.is_valid()) {
  881. mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette));
  882. mesh_library = Ref<MeshLibrary>();
  883. }
  884. }
  885. node = p_gridmap;
  886. input_action = INPUT_NONE;
  887. selection.active = false;
  888. _update_selection_transform();
  889. _update_paste_indicator();
  890. spatial_editor = Object::cast_to<Node3DEditorPlugin>(EditorNode::get_singleton()->get_editor_main_screen()->get_selected_plugin());
  891. if (!node) {
  892. set_process(false);
  893. for (int i = 0; i < 3; i++) {
  894. RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  895. }
  896. if (cursor_instance.is_valid()) {
  897. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false);
  898. }
  899. return;
  900. }
  901. update_palette();
  902. _update_cursor_instance();
  903. set_process(true);
  904. _draw_grids(node->get_cell_size());
  905. update_grid();
  906. node->connect(SNAME("cell_size_changed"), callable_mp(this, &GridMapEditor::_draw_grids));
  907. node->connect(CoreStringName(changed), callable_mp(this, &GridMapEditor::_update_mesh_library));
  908. _update_mesh_library();
  909. }
  910. void GridMapEditor::update_grid() {
  911. grid_xform.origin.x -= 1; // Force update in hackish way.
  912. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  913. // If there's a valid tile cursor, offset the grid, otherwise move it back to the node.
  914. edit_grid_xform.origin = cursor_instance.is_valid() ? grid_ofs : Vector3();
  915. edit_grid_xform.basis = Basis();
  916. for (int i = 0; i < 3; i++) {
  917. RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  918. }
  919. updating = true;
  920. floor->set_value(edit_floor[edit_axis]);
  921. updating = false;
  922. }
  923. void GridMapEditor::_draw_grids(const Vector3 &cell_size) {
  924. Vector3 edited_floor = node->get_meta("_editor_floor_", Vector3());
  925. for (int i = 0; i < 3; i++) {
  926. RS::get_singleton()->mesh_clear(grid[i]);
  927. edit_floor[i] = edited_floor[i];
  928. }
  929. Vector<Vector3> grid_points[3];
  930. Vector<Color> grid_colors[3];
  931. for (int i = 0; i < 3; i++) {
  932. Vector3 axis;
  933. axis[i] = 1;
  934. Vector3 axis_n1;
  935. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  936. Vector3 axis_n2;
  937. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  938. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  939. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  940. Vector3 p = axis_n1 * j + axis_n2 * k;
  941. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  942. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  943. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  944. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  945. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  946. grid_points[i].push_back(p);
  947. grid_points[i].push_back(pk);
  948. grid_colors[i].push_back(Color(1, 1, 1, trans));
  949. grid_colors[i].push_back(Color(1, 1, 1, transk));
  950. grid_points[i].push_back(p);
  951. grid_points[i].push_back(pj);
  952. grid_colors[i].push_back(Color(1, 1, 1, trans));
  953. grid_colors[i].push_back(Color(1, 1, 1, transj));
  954. }
  955. }
  956. Array d;
  957. d.resize(RS::ARRAY_MAX);
  958. d[RS::ARRAY_VERTEX] = grid_points[i];
  959. d[RS::ARRAY_COLOR] = grid_colors[i];
  960. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], RenderingServer::PRIMITIVE_LINES, d);
  961. RenderingServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  962. }
  963. }
  964. void GridMapEditor::_update_theme() {
  965. transform_mode_button->set_button_icon(get_theme_icon(SNAME("ToolMove"), EditorStringName(EditorIcons)));
  966. select_mode_button->set_button_icon(get_theme_icon(SNAME("ToolSelect"), EditorStringName(EditorIcons)));
  967. erase_mode_button->set_button_icon(get_theme_icon(SNAME("Eraser"), EditorStringName(EditorIcons)));
  968. paint_mode_button->set_button_icon(get_theme_icon(SNAME("Paint"), EditorStringName(EditorIcons)));
  969. pick_mode_button->set_button_icon(get_theme_icon(SNAME("ColorPick"), EditorStringName(EditorIcons)));
  970. fill_action_button->set_button_icon(get_theme_icon(SNAME("Bucket"), EditorStringName(EditorIcons)));
  971. move_action_button->set_button_icon(get_theme_icon(SNAME("ActionCut"), EditorStringName(EditorIcons)));
  972. duplicate_action_button->set_button_icon(get_theme_icon(SNAME("ActionCopy"), EditorStringName(EditorIcons)));
  973. delete_action_button->set_button_icon(get_theme_icon(SNAME("Clear"), EditorStringName(EditorIcons)));
  974. rotate_x_button->set_button_icon(get_theme_icon(SNAME("RotateLeft"), EditorStringName(EditorIcons)));
  975. rotate_y_button->set_button_icon(get_theme_icon(SNAME("ToolRotate"), EditorStringName(EditorIcons)));
  976. rotate_z_button->set_button_icon(get_theme_icon(SNAME("RotateRight"), EditorStringName(EditorIcons)));
  977. search_box->set_right_icon(get_theme_icon(SNAME("Search"), EditorStringName(EditorIcons)));
  978. mode_thumbnail->set_button_icon(get_theme_icon(SNAME("FileThumbnail"), EditorStringName(EditorIcons)));
  979. mode_list->set_button_icon(get_theme_icon(SNAME("FileList"), EditorStringName(EditorIcons)));
  980. options->set_button_icon(get_theme_icon(SNAME("Tools"), EditorStringName(EditorIcons)));
  981. }
  982. void GridMapEditor::_notification(int p_what) {
  983. switch (p_what) {
  984. case NOTIFICATION_ENTER_TREE: {
  985. mesh_library_palette->connect(SceneStringName(item_selected), callable_mp(this, &GridMapEditor::_item_selected_cbk));
  986. for (int i = 0; i < 3; i++) {
  987. grid[i] = RS::get_singleton()->mesh_create();
  988. grid_instance[i] = RS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  989. RenderingServer::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  990. selection_level_instance[i] = RenderingServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  991. RenderingServer::get_singleton()->instance_set_layer_mask(selection_level_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  992. }
  993. cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  994. RenderingServer::get_singleton()->instance_set_layer_mask(cursor_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  995. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false);
  996. selection_instance = RenderingServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  997. RenderingServer::get_singleton()->instance_set_layer_mask(selection_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  998. paste_instance = RenderingServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  999. RenderingServer::get_singleton()->instance_set_layer_mask(paste_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  1000. _update_selection_transform();
  1001. _update_paste_indicator();
  1002. _update_theme();
  1003. } break;
  1004. case NOTIFICATION_EXIT_TREE: {
  1005. _clear_clipboard_data();
  1006. for (int i = 0; i < 3; i++) {
  1007. RS::get_singleton()->free(grid_instance[i]);
  1008. RS::get_singleton()->free(grid[i]);
  1009. grid_instance[i] = RID();
  1010. grid[i] = RID();
  1011. RenderingServer::get_singleton()->free(selection_level_instance[i]);
  1012. }
  1013. RenderingServer::get_singleton()->free(cursor_instance);
  1014. RenderingServer::get_singleton()->free(selection_instance);
  1015. RenderingServer::get_singleton()->free(paste_instance);
  1016. cursor_instance = RID();
  1017. selection_instance = RID();
  1018. paste_instance = RID();
  1019. } break;
  1020. case NOTIFICATION_PROCESS: {
  1021. if (!node) {
  1022. return;
  1023. }
  1024. Transform3D xf = node->get_global_transform();
  1025. if (xf != grid_xform) {
  1026. for (int i = 0; i < 3; i++) {
  1027. RS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  1028. }
  1029. grid_xform = xf;
  1030. }
  1031. } break;
  1032. case NOTIFICATION_THEME_CHANGED: {
  1033. _update_theme();
  1034. } break;
  1035. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  1036. if (input_action == INPUT_PAINT) {
  1037. // Simulate mouse released event to stop drawing when editor focus exists.
  1038. Ref<InputEventMouseButton> release;
  1039. release.instantiate();
  1040. release->set_button_index(MouseButton::LEFT);
  1041. forward_spatial_input_event(nullptr, release);
  1042. }
  1043. } break;
  1044. }
  1045. }
  1046. void GridMapEditor::_update_cursor_instance() {
  1047. if (!node) {
  1048. return;
  1049. }
  1050. if (cursor_instance.is_valid()) {
  1051. RenderingServer::get_singleton()->free(cursor_instance);
  1052. }
  1053. cursor_instance = RID();
  1054. if (mode_buttons_group->get_pressed_button() == paint_mode_button) {
  1055. if (selected_palette >= 0 && node && node->get_mesh_library().is_valid()) {
  1056. Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette);
  1057. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  1058. cursor_instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());
  1059. }
  1060. }
  1061. } else if (mode_buttons_group->get_pressed_button() == select_mode_button) {
  1062. cursor_inner_mat->set_albedo(Color(default_color, 0.2));
  1063. cursor_outer_mat->set_albedo(Color(default_color, 0.8));
  1064. cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  1065. } else if (mode_buttons_group->get_pressed_button() == erase_mode_button) {
  1066. cursor_inner_mat->set_albedo(Color(erase_color, 0.2));
  1067. cursor_outer_mat->set_albedo(Color(erase_color, 0.8));
  1068. cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  1069. } else if (mode_buttons_group->get_pressed_button() == pick_mode_button) {
  1070. cursor_inner_mat->set_albedo(Color(pick_color, 0.2));
  1071. cursor_outer_mat->set_albedo(Color(pick_color, 0.8));
  1072. cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  1073. }
  1074. _update_cursor_transform();
  1075. }
  1076. void GridMapEditor::_on_tool_mode_changed() {
  1077. _show_viewports_transform_gizmo(mode_buttons_group->get_pressed_button() == transform_mode_button);
  1078. _update_cursor_instance();
  1079. }
  1080. void GridMapEditor::_item_selected_cbk(int idx) {
  1081. selected_palette = mesh_library_palette->get_item_metadata(idx);
  1082. _update_cursor_instance();
  1083. }
  1084. void GridMapEditor::_floor_changed(float p_value) {
  1085. if (updating) {
  1086. return;
  1087. }
  1088. edit_floor[edit_axis] = p_value;
  1089. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  1090. update_grid();
  1091. _update_selection_transform();
  1092. }
  1093. void GridMapEditor::_floor_mouse_exited() {
  1094. floor->get_line_edit()->release_focus();
  1095. }
  1096. void GridMapEditor::_bind_methods() {
  1097. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  1098. ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection);
  1099. }
  1100. GridMapEditor::GridMapEditor() {
  1101. ED_SHORTCUT("grid_map/previous_floor", TTR("Previous Floor"), Key::KEY_1, true);
  1102. ED_SHORTCUT("grid_map/next_floor", TTR("Next Floor"), Key::KEY_3, true);
  1103. ED_SHORTCUT("grid_map/edit_x_axis", TTR("Edit X Axis"), KeyModifierMask::SHIFT + Key::Z, true);
  1104. ED_SHORTCUT("grid_map/edit_y_axis", TTR("Edit Y Axis"), KeyModifierMask::SHIFT + Key::X, true);
  1105. ED_SHORTCUT("grid_map/edit_z_axis", TTR("Edit Z Axis"), KeyModifierMask::SHIFT + Key::C, true);
  1106. ED_SHORTCUT("grid_map/keep_selected", TTR("Keep Selection"));
  1107. ED_SHORTCUT("grid_map/clear_rotation", TTR("Clear Rotation"));
  1108. options = memnew(MenuButton);
  1109. options->set_theme_type_variation("FlatButton");
  1110. options->get_popup()->add_separator();
  1111. options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_x_axis"), MENU_OPTION_X_AXIS);
  1112. options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_y_axis"), MENU_OPTION_Y_AXIS);
  1113. options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_z_axis"), MENU_OPTION_Z_AXIS);
  1114. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  1115. options->get_popup()->add_separator();
  1116. // TRANSLATORS: This is a toggle to select after pasting the new content.
  1117. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/clear_rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION);
  1118. options->get_popup()->add_check_shortcut(ED_GET_SHORTCUT("grid_map/keep_selected"), MENU_OPTION_PASTE_SELECTS);
  1119. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS), true);
  1120. options->get_popup()->add_separator();
  1121. options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
  1122. settings_dialog = memnew(ConfirmationDialog);
  1123. settings_dialog->set_title(TTR("GridMap Settings"));
  1124. add_child(settings_dialog);
  1125. settings_vbc = memnew(VBoxContainer);
  1126. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  1127. settings_dialog->add_child(settings_vbc);
  1128. settings_pick_distance = memnew(SpinBox);
  1129. settings_pick_distance->set_max(10000.0f);
  1130. settings_pick_distance->set_min(500.0f);
  1131. settings_pick_distance->set_step(1.0f);
  1132. settings_pick_distance->set_value(EDITOR_GET("editors/grid_map/pick_distance"));
  1133. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  1134. options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &GridMapEditor::_menu_option));
  1135. toolbar = memnew(HBoxContainer);
  1136. add_child(toolbar);
  1137. toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
  1138. HBoxContainer *mode_buttons = memnew(HBoxContainer);
  1139. toolbar->add_child(mode_buttons);
  1140. mode_buttons_group.instantiate();
  1141. transform_mode_button = memnew(Button);
  1142. transform_mode_button->set_theme_type_variation("FlatButton");
  1143. transform_mode_button->set_toggle_mode(true);
  1144. transform_mode_button->set_button_group(mode_buttons_group);
  1145. transform_mode_button->set_shortcut(ED_SHORTCUT("grid_map/transform_tool", TTR("Transform"), Key::T, true));
  1146. transform_mode_button->connect(SceneStringName(toggled),
  1147. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1148. mode_buttons->add_child(transform_mode_button);
  1149. viewport_shortcut_buttons.push_back(transform_mode_button);
  1150. VSeparator *vsep = memnew(VSeparator);
  1151. mode_buttons->add_child(vsep);
  1152. select_mode_button = memnew(Button);
  1153. select_mode_button->set_theme_type_variation("FlatButton");
  1154. select_mode_button->set_toggle_mode(true);
  1155. select_mode_button->set_button_group(mode_buttons_group);
  1156. select_mode_button->set_shortcut(ED_SHORTCUT("grid_map/selection_tool", TTR("Selection"), Key::Q, true));
  1157. select_mode_button->connect(SceneStringName(toggled),
  1158. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1159. mode_buttons->add_child(select_mode_button);
  1160. viewport_shortcut_buttons.push_back(select_mode_button);
  1161. erase_mode_button = memnew(Button);
  1162. erase_mode_button->set_theme_type_variation("FlatButton");
  1163. erase_mode_button->set_toggle_mode(true);
  1164. erase_mode_button->set_button_group(mode_buttons_group);
  1165. erase_mode_button->set_shortcut(ED_SHORTCUT("grid_map/erase_tool", TTR("Erase"), Key::W, true));
  1166. mode_buttons->add_child(erase_mode_button);
  1167. erase_mode_button->connect(SceneStringName(toggled),
  1168. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1169. viewport_shortcut_buttons.push_back(erase_mode_button);
  1170. paint_mode_button = memnew(Button);
  1171. paint_mode_button->set_theme_type_variation("FlatButton");
  1172. paint_mode_button->set_toggle_mode(true);
  1173. paint_mode_button->set_button_group(mode_buttons_group);
  1174. paint_mode_button->set_shortcut(ED_SHORTCUT("grid_map/paint_tool", TTR("Paint"), Key::E, true));
  1175. paint_mode_button->connect(SceneStringName(toggled),
  1176. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1177. mode_buttons->add_child(paint_mode_button);
  1178. viewport_shortcut_buttons.push_back(paint_mode_button);
  1179. pick_mode_button = memnew(Button);
  1180. pick_mode_button->set_theme_type_variation("FlatButton");
  1181. pick_mode_button->set_toggle_mode(true);
  1182. pick_mode_button->set_button_group(mode_buttons_group);
  1183. pick_mode_button->set_shortcut(ED_SHORTCUT("grid_map/pick_tool", TTR("Pick"), Key::R, true));
  1184. pick_mode_button->connect(SceneStringName(toggled),
  1185. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1186. mode_buttons->add_child(pick_mode_button);
  1187. viewport_shortcut_buttons.push_back(pick_mode_button);
  1188. vsep = memnew(VSeparator);
  1189. toolbar->add_child(vsep);
  1190. HBoxContainer *action_buttons = memnew(HBoxContainer);
  1191. toolbar->add_child(action_buttons);
  1192. fill_action_button = memnew(Button);
  1193. fill_action_button->set_theme_type_variation("FlatButton");
  1194. fill_action_button->set_shortcut(ED_SHORTCUT("grid_map/fill_tool", TTR("Fill"), Key::Z, true));
  1195. fill_action_button->connect(SceneStringName(pressed),
  1196. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_FILL));
  1197. action_buttons->add_child(fill_action_button);
  1198. viewport_shortcut_buttons.push_back(fill_action_button);
  1199. move_action_button = memnew(Button);
  1200. move_action_button->set_theme_type_variation("FlatButton");
  1201. move_action_button->set_shortcut(ED_SHORTCUT("grid_map/move_tool", TTR("Move"), Key::X, true));
  1202. move_action_button->connect(SceneStringName(pressed),
  1203. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_CUT));
  1204. action_buttons->add_child(move_action_button);
  1205. viewport_shortcut_buttons.push_back(move_action_button);
  1206. duplicate_action_button = memnew(Button);
  1207. duplicate_action_button->set_theme_type_variation("FlatButton");
  1208. duplicate_action_button->set_shortcut(ED_SHORTCUT("grid_map/duplicate_tool", TTR("Duplicate"), Key::C, true));
  1209. duplicate_action_button->connect(SceneStringName(pressed),
  1210. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_DUPLICATE));
  1211. action_buttons->add_child(duplicate_action_button);
  1212. viewport_shortcut_buttons.push_back(duplicate_action_button);
  1213. delete_action_button = memnew(Button);
  1214. delete_action_button->set_theme_type_variation("FlatButton");
  1215. delete_action_button->set_shortcut(ED_SHORTCUT("grid_map/delete_tool", TTR("Delete"), Key::V, true));
  1216. delete_action_button->connect(SceneStringName(pressed),
  1217. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_CLEAR));
  1218. action_buttons->add_child(delete_action_button);
  1219. viewport_shortcut_buttons.push_back(delete_action_button);
  1220. vsep = memnew(VSeparator);
  1221. toolbar->add_child(vsep);
  1222. HBoxContainer *rotation_buttons = memnew(HBoxContainer);
  1223. toolbar->add_child(rotation_buttons);
  1224. rotate_x_button = memnew(Button);
  1225. rotate_x_button->set_theme_type_variation("FlatButton");
  1226. rotate_x_button->set_shortcut(ED_SHORTCUT("grid_map/cursor_rotate_x", TTR("Cursor Rotate X"), Key::A, true));
  1227. rotate_x_button->connect(SceneStringName(pressed),
  1228. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_CURSOR_ROTATE_X));
  1229. rotation_buttons->add_child(rotate_x_button);
  1230. viewport_shortcut_buttons.push_back(rotate_x_button);
  1231. rotate_y_button = memnew(Button);
  1232. rotate_y_button->set_theme_type_variation("FlatButton");
  1233. rotate_y_button->set_shortcut(ED_SHORTCUT("grid_map/cursor_rotate_y", TTR("Cursor Rotate Y"), Key::S, true));
  1234. rotate_y_button->connect(SceneStringName(pressed),
  1235. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_CURSOR_ROTATE_Y));
  1236. rotation_buttons->add_child(rotate_y_button);
  1237. viewport_shortcut_buttons.push_back(rotate_y_button);
  1238. rotate_z_button = memnew(Button);
  1239. rotate_z_button->set_theme_type_variation("FlatButton");
  1240. rotate_z_button->set_shortcut(ED_SHORTCUT("grid_map/cursor_rotate_z", TTR("Cursor Rotate Z"), Key::D, true));
  1241. rotate_z_button->connect(SceneStringName(pressed),
  1242. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_CURSOR_ROTATE_Z));
  1243. rotation_buttons->add_child(rotate_z_button);
  1244. viewport_shortcut_buttons.push_back(rotate_z_button);
  1245. // Wide empty separation control. (like BoxContainer::add_spacer())
  1246. Control *c = memnew(Control);
  1247. c->set_mouse_filter(MOUSE_FILTER_PASS);
  1248. c->set_h_size_flags(SIZE_EXPAND_FILL);
  1249. toolbar->add_child(c);
  1250. floor = memnew(SpinBox);
  1251. floor->set_min(-32767);
  1252. floor->set_max(32767);
  1253. floor->set_step(1);
  1254. floor->set_tooltip_text(
  1255. vformat(TTR("Change Grid Floor:\nPrevious Plane (%s)\nNext Plane (%s)"),
  1256. ED_GET_SHORTCUT("grid_map/previous_floor")->get_as_text(),
  1257. ED_GET_SHORTCUT("grid_map/next_floor")->get_as_text()));
  1258. toolbar->add_child(floor);
  1259. floor->get_line_edit()->add_theme_constant_override("minimum_character_width", 2);
  1260. floor->get_line_edit()->set_context_menu_enabled(false);
  1261. floor->connect(SceneStringName(value_changed), callable_mp(this, &GridMapEditor::_floor_changed));
  1262. floor->connect(SceneStringName(mouse_exited), callable_mp(this, &GridMapEditor::_floor_mouse_exited));
  1263. floor->get_line_edit()->connect(SceneStringName(mouse_exited), callable_mp(this, &GridMapEditor::_floor_mouse_exited));
  1264. search_box = memnew(LineEdit);
  1265. search_box->add_theme_constant_override("minimum_character_width", 10);
  1266. search_box->set_placeholder(TTR("Filter Meshes"));
  1267. search_box->set_clear_button_enabled(true);
  1268. toolbar->add_child(search_box);
  1269. search_box->connect(SceneStringName(text_changed), callable_mp(this, &GridMapEditor::_text_changed));
  1270. search_box->connect(SceneStringName(gui_input), callable_mp(this, &GridMapEditor::_sbox_input));
  1271. zoom_widget = memnew(EditorZoomWidget);
  1272. toolbar->add_child(zoom_widget);
  1273. zoom_widget->setup_zoom_limits(0.2, 4);
  1274. zoom_widget->set_zoom(1.0);
  1275. zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);
  1276. zoom_widget->connect("zoom_changed", callable_mp(this, &GridMapEditor::_icon_size_changed));
  1277. zoom_widget->set_shortcut_context(this);
  1278. mode_thumbnail = memnew(Button);
  1279. mode_thumbnail->set_theme_type_variation("FlatButton");
  1280. mode_thumbnail->set_toggle_mode(true);
  1281. mode_thumbnail->set_pressed(true);
  1282. toolbar->add_child(mode_thumbnail);
  1283. mode_thumbnail->connect(SceneStringName(pressed), callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_THUMBNAIL));
  1284. mode_list = memnew(Button);
  1285. mode_list->set_theme_type_variation("FlatButton");
  1286. mode_list->set_toggle_mode(true);
  1287. mode_list->set_pressed(false);
  1288. toolbar->add_child(mode_list);
  1289. mode_list->connect(SceneStringName(pressed), callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_LIST));
  1290. toolbar->add_child(options);
  1291. mesh_library_palette = memnew(ItemList);
  1292. mesh_library_palette->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1293. add_child(mesh_library_palette);
  1294. mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL);
  1295. mesh_library_palette->connect(SceneStringName(gui_input), callable_mp(this, &GridMapEditor::_mesh_library_palette_input));
  1296. info_message = memnew(Label);
  1297. info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes."));
  1298. info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1299. info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1300. info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  1301. info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  1302. info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  1303. mesh_library_palette->add_child(info_message);
  1304. edit_axis = Vector3::AXIS_Y;
  1305. edit_floor[0] = -1;
  1306. edit_floor[1] = -1;
  1307. edit_floor[2] = -1;
  1308. cursor_mesh = RenderingServer::get_singleton()->mesh_create();
  1309. selection_mesh = RenderingServer::get_singleton()->mesh_create();
  1310. paste_mesh = RenderingServer::get_singleton()->mesh_create();
  1311. {
  1312. // Selection mesh create.
  1313. Vector<Vector3> lines;
  1314. Vector<Vector3> triangles;
  1315. Vector<Vector3> square[3];
  1316. for (int i = 0; i < 6; i++) {
  1317. Vector3 face_points[4];
  1318. for (int j = 0; j < 4; j++) {
  1319. float v[3];
  1320. v[0] = 1.0;
  1321. v[1] = 1 - 2 * ((j >> 1) & 1);
  1322. v[2] = v[1] * (1 - 2 * (j & 1));
  1323. for (int k = 0; k < 3; k++) {
  1324. if (i < 3) {
  1325. face_points[j][(i + k) % 3] = v[k];
  1326. } else {
  1327. face_points[3 - j][(i + k) % 3] = -v[k];
  1328. }
  1329. }
  1330. }
  1331. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1332. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1333. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1334. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1335. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1336. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1337. }
  1338. for (int i = 0; i < 12; i++) {
  1339. AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  1340. Vector3 a, b;
  1341. base.get_edge(i, a, b);
  1342. lines.push_back(a);
  1343. lines.push_back(b);
  1344. }
  1345. for (int i = 0; i < 3; i++) {
  1346. Vector3 points[4];
  1347. for (int j = 0; j < 4; j++) {
  1348. static const bool orderx[4] = { false, true, true, false };
  1349. static const bool ordery[4] = { false, false, true, true };
  1350. Vector3 sp;
  1351. if (orderx[j]) {
  1352. sp[(i + 1) % 3] = 1.0;
  1353. }
  1354. if (ordery[j]) {
  1355. sp[(i + 2) % 3] = 1.0;
  1356. }
  1357. points[j] = sp;
  1358. }
  1359. for (int j = 0; j < 4; j++) {
  1360. Vector3 ofs;
  1361. ofs[i] += 0.01;
  1362. square[i].push_back(points[j] - ofs);
  1363. square[i].push_back(points[(j + 1) % 4] - ofs);
  1364. square[i].push_back(points[j] + ofs);
  1365. square[i].push_back(points[(j + 1) % 4] + ofs);
  1366. }
  1367. }
  1368. Array d;
  1369. d.resize(RS::ARRAY_MAX);
  1370. default_color = Color(0.0, 0.565, 1.0); // blue 0.7, 0.7, 1.0
  1371. erase_color = Color(1.0, 0.2, 0.2); // red
  1372. pick_color = Color(1, 0.7, 0); // orange/yellow
  1373. cursor_inner_mat.instantiate();
  1374. cursor_inner_mat->set_albedo(Color(default_color, 0.2));
  1375. cursor_inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1376. cursor_inner_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1377. cursor_inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1378. cursor_outer_mat.instantiate();
  1379. cursor_outer_mat->set_albedo(Color(default_color, 0.8));
  1380. cursor_outer_mat->set_on_top_of_alpha();
  1381. cursor_outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1382. cursor_outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1383. cursor_outer_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1384. inner_mat.instantiate();
  1385. inner_mat->set_albedo(Color(default_color, 0.2));
  1386. inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1387. inner_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1388. inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1389. outer_mat.instantiate();
  1390. outer_mat->set_albedo(Color(default_color, 0.8));
  1391. outer_mat->set_on_top_of_alpha();
  1392. outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1393. outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1394. outer_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1395. selection_floor_mat.instantiate();
  1396. selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
  1397. selection_floor_mat->set_on_top_of_alpha();
  1398. selection_floor_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1399. selection_floor_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1400. d[RS::ARRAY_VERTEX] = triangles;
  1401. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(cursor_mesh, RS::PRIMITIVE_TRIANGLES, d);
  1402. RenderingServer::get_singleton()->mesh_surface_set_material(cursor_mesh, 0, cursor_inner_mat->get_rid());
  1403. d[RS::ARRAY_VERTEX] = lines;
  1404. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(cursor_mesh, RS::PRIMITIVE_LINES, d);
  1405. RenderingServer::get_singleton()->mesh_surface_set_material(cursor_mesh, 1, cursor_outer_mat->get_rid());
  1406. d[RS::ARRAY_VERTEX] = triangles;
  1407. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_TRIANGLES, d);
  1408. RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  1409. d[RS::ARRAY_VERTEX] = lines;
  1410. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_LINES, d);
  1411. RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  1412. d[RS::ARRAY_VERTEX] = triangles;
  1413. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_TRIANGLES, d);
  1414. RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid());
  1415. d[RS::ARRAY_VERTEX] = lines;
  1416. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_LINES, d);
  1417. RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid());
  1418. for (int i = 0; i < 3; i++) {
  1419. d[RS::ARRAY_VERTEX] = square[i];
  1420. selection_level_mesh[i] = RS::get_singleton()->mesh_create();
  1421. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], RS::PRIMITIVE_LINES, d);
  1422. RenderingServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
  1423. }
  1424. }
  1425. _set_selection(false);
  1426. indicator_mat.instantiate();
  1427. indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1428. indicator_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1429. indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  1430. indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1431. indicator_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1432. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  1433. }
  1434. GridMapEditor::~GridMapEditor() {
  1435. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1436. _clear_clipboard_data();
  1437. for (int i = 0; i < 3; i++) {
  1438. if (grid[i].is_valid()) {
  1439. RenderingServer::get_singleton()->free(grid[i]);
  1440. }
  1441. if (grid_instance[i].is_valid()) {
  1442. RenderingServer::get_singleton()->free(grid_instance[i]);
  1443. }
  1444. if (selection_level_instance[i].is_valid()) {
  1445. RenderingServer::get_singleton()->free(selection_level_instance[i]);
  1446. }
  1447. if (selection_level_mesh[i].is_valid()) {
  1448. RenderingServer::get_singleton()->free(selection_level_mesh[i]);
  1449. }
  1450. }
  1451. RenderingServer::get_singleton()->free(cursor_mesh);
  1452. if (cursor_instance.is_valid()) {
  1453. RenderingServer::get_singleton()->free(cursor_instance);
  1454. }
  1455. RenderingServer::get_singleton()->free(selection_mesh);
  1456. if (selection_instance.is_valid()) {
  1457. RenderingServer::get_singleton()->free(selection_instance);
  1458. }
  1459. RenderingServer::get_singleton()->free(paste_mesh);
  1460. if (paste_instance.is_valid()) {
  1461. RenderingServer::get_singleton()->free(paste_instance);
  1462. }
  1463. }
  1464. void GridMapEditorPlugin::edit(Object *p_object) {
  1465. grid_map_editor->edit(Object::cast_to<GridMap>(p_object));
  1466. }
  1467. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1468. return p_object->is_class("GridMap");
  1469. }
  1470. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1471. if (p_visible) {
  1472. BaseButton *button = grid_map_editor->mode_buttons_group->get_pressed_button();
  1473. if (button == nullptr) {
  1474. grid_map_editor->select_mode_button->set_pressed(true);
  1475. }
  1476. grid_map_editor->_on_tool_mode_changed();
  1477. panel_button->show();
  1478. EditorNode::get_bottom_panel()->make_item_visible(grid_map_editor);
  1479. grid_map_editor->set_process(true);
  1480. } else {
  1481. grid_map_editor->_show_viewports_transform_gizmo(true);
  1482. panel_button->hide();
  1483. if (grid_map_editor->is_visible_in_tree()) {
  1484. EditorNode::get_bottom_panel()->hide_bottom_panel();
  1485. }
  1486. grid_map_editor->set_process(false);
  1487. }
  1488. }
  1489. GridMapEditorPlugin::GridMapEditorPlugin() {
  1490. grid_map_editor = memnew(GridMapEditor);
  1491. grid_map_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1492. grid_map_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1493. grid_map_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  1494. grid_map_editor->hide();
  1495. panel_button = EditorNode::get_bottom_panel()->add_item(TTR("GridMap"), grid_map_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_grid_map_bottom_panel", TTR("Toggle GridMap Bottom Panel")));
  1496. panel_button->hide();
  1497. }
  1498. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1499. }
  1500. #endif // TOOLS_ENABLED