grid_map_editor_plugin.cpp 55 KB

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