grid_map_editor_plugin.cpp 71 KB

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