grid_map_editor_plugin.cpp 73 KB

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