grid_map_editor_plugin.cpp 51 KB

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