grid_map_editor_plugin.cpp 52 KB

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