grid_map_editor_plugin.cpp 70 KB

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