grid_map_editor_plugin.cpp 55 KB

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