grid_map_editor_plugin.cpp 70 KB

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