grid_map_editor_plugin.cpp 52 KB

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