grid_map_editor_plugin.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. if (input_action == INPUT_PASTE) {
  517. _do_paste();
  518. input_action = INPUT_NONE;
  519. _update_paste_indicator();
  520. } else if (mb->get_shift()) {
  521. input_action = INPUT_SELECT;
  522. last_selection = selection;
  523. } else if (mb->get_command()) {
  524. input_action = INPUT_PICK;
  525. } else {
  526. input_action = INPUT_PAINT;
  527. set_items.clear();
  528. }
  529. } else if (mb->get_button_index() == BUTTON_RIGHT) {
  530. if (input_action == INPUT_PASTE) {
  531. _clear_clipboard_data();
  532. input_action = INPUT_NONE;
  533. _update_paste_indicator();
  534. return true;
  535. } else if (selection.active) {
  536. _set_selection(false);
  537. return true;
  538. } else {
  539. input_action = INPUT_ERASE;
  540. set_items.clear();
  541. }
  542. } else {
  543. return false;
  544. }
  545. return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true);
  546. } else {
  547. if ((mb->get_button_index() == BUTTON_RIGHT && input_action == INPUT_ERASE) || (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_PAINT)) {
  548. if (set_items.size()) {
  549. undo_redo->create_action(TTR("GridMap Paint"));
  550. for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
  551. const SetItem &si = E->get();
  552. undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
  553. }
  554. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  555. const SetItem &si = E->get();
  556. undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
  557. }
  558. undo_redo->commit_action();
  559. }
  560. set_items.clear();
  561. input_action = INPUT_NONE;
  562. return set_items.size() > 0;
  563. }
  564. if (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_SELECT) {
  565. undo_redo->create_action("GridMap Selection");
  566. undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  567. undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end);
  568. undo_redo->commit_action();
  569. }
  570. if (mb->get_button_index() == BUTTON_LEFT && input_action != INPUT_NONE) {
  571. set_items.clear();
  572. input_action = INPUT_NONE;
  573. return true;
  574. }
  575. if (mb->get_button_index() == BUTTON_RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) {
  576. input_action = INPUT_NONE;
  577. return true;
  578. }
  579. }
  580. }
  581. Ref<InputEventMouseMotion> mm = p_event;
  582. if (mm.is_valid()) {
  583. return do_input_action(p_camera, mm->get_position(), false);
  584. }
  585. Ref<InputEventKey> k = p_event;
  586. if (k.is_valid()) {
  587. if (k->is_pressed()) {
  588. if (k->get_scancode() == KEY_ESCAPE) {
  589. if (input_action == INPUT_PASTE) {
  590. _clear_clipboard_data();
  591. input_action = INPUT_NONE;
  592. _update_paste_indicator();
  593. return true;
  594. } else if (selection.active) {
  595. _set_selection(false);
  596. return true;
  597. } else {
  598. selected_palette = -1;
  599. mesh_library_palette->unselect_all();
  600. update_palette();
  601. _update_cursor_instance();
  602. return true;
  603. }
  604. }
  605. if (k->get_shift() && selection.active && input_action != INPUT_PASTE) {
  606. if (k->get_scancode() == options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL))) {
  607. selection.click[edit_axis]--;
  608. _validate_selection();
  609. return true;
  610. }
  611. if (k->get_scancode() == options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL))) {
  612. selection.click[edit_axis]++;
  613. _validate_selection();
  614. return true;
  615. }
  616. }
  617. }
  618. }
  619. Ref<InputEventPanGesture> pan_gesture = p_event;
  620. if (pan_gesture.is_valid()) {
  621. if (pan_gesture->get_alt() && (pan_gesture->get_command() || pan_gesture->get_shift())) {
  622. const real_t delta = pan_gesture->get_delta().y * 0.5;
  623. accumulated_floor_delta += delta;
  624. int step = 0;
  625. if (ABS(accumulated_floor_delta) > 1.0) {
  626. step = SGN(accumulated_floor_delta);
  627. accumulated_floor_delta -= step;
  628. }
  629. if (step) {
  630. floor->set_value(floor->get_value() + step);
  631. }
  632. return true;
  633. }
  634. }
  635. accumulated_floor_delta = 0.0;
  636. return false;
  637. }
  638. struct _CGMEItemSort {
  639. String name;
  640. int id;
  641. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  642. };
  643. void GridMapEditor::_set_display_mode(int p_mode) {
  644. if (display_mode == p_mode) {
  645. return;
  646. }
  647. if (p_mode == DISPLAY_LIST) {
  648. mode_list->set_pressed(true);
  649. mode_thumbnail->set_pressed(false);
  650. } else if (p_mode == DISPLAY_THUMBNAIL) {
  651. mode_list->set_pressed(false);
  652. mode_thumbnail->set_pressed(true);
  653. }
  654. display_mode = p_mode;
  655. update_palette();
  656. }
  657. void GridMapEditor::_text_changed(const String &p_text) {
  658. update_palette();
  659. }
  660. void GridMapEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  661. Ref<InputEventKey> k = p_ie;
  662. if (k.is_valid() && (k->get_scancode() == KEY_UP || k->get_scancode() == KEY_DOWN || k->get_scancode() == KEY_PAGEUP || k->get_scancode() == KEY_PAGEDOWN)) {
  663. mesh_library_palette->call("_gui_input", k);
  664. search_box->accept_event();
  665. }
  666. }
  667. void GridMapEditor::_icon_size_changed(float p_value) {
  668. mesh_library_palette->set_icon_scale(p_value);
  669. update_palette();
  670. }
  671. void GridMapEditor::update_palette() {
  672. int selected = mesh_library_palette->get_current();
  673. float min_size = EDITOR_DEF("editors/grid_map/preview_size", 64);
  674. min_size *= EDSCALE;
  675. mesh_library_palette->clear();
  676. if (display_mode == DISPLAY_THUMBNAIL) {
  677. mesh_library_palette->set_max_columns(0);
  678. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  679. mesh_library_palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1.5));
  680. } else if (display_mode == DISPLAY_LIST) {
  681. mesh_library_palette->set_max_columns(1);
  682. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT);
  683. mesh_library_palette->set_fixed_column_width(0);
  684. }
  685. mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size));
  686. mesh_library_palette->set_max_text_lines(2);
  687. Ref<MeshLibrary> mesh_library = node->get_mesh_library();
  688. if (mesh_library.is_null()) {
  689. last_mesh_library = NULL;
  690. search_box->set_text("");
  691. search_box->set_editable(false);
  692. info_message->show();
  693. return;
  694. }
  695. search_box->set_editable(true);
  696. info_message->hide();
  697. Vector<int> ids;
  698. ids = mesh_library->get_item_list();
  699. List<_CGMEItemSort> il;
  700. for (int i = 0; i < ids.size(); i++) {
  701. _CGMEItemSort is;
  702. is.id = ids[i];
  703. is.name = mesh_library->get_item_name(ids[i]);
  704. il.push_back(is);
  705. }
  706. il.sort();
  707. String filter = search_box->get_text().strip_edges();
  708. int item = 0;
  709. for (List<_CGMEItemSort>::Element *E = il.front(); E; E = E->next()) {
  710. int id = E->get().id;
  711. String name = mesh_library->get_item_name(id);
  712. Ref<Texture> preview = mesh_library->get_item_preview(id);
  713. if (name == "") {
  714. name = "#" + itos(id);
  715. }
  716. if (filter != "" && !filter.is_subsequence_ofi(name))
  717. continue;
  718. mesh_library_palette->add_item("");
  719. if (!preview.is_null()) {
  720. mesh_library_palette->set_item_icon(item, preview);
  721. mesh_library_palette->set_item_tooltip(item, name);
  722. }
  723. mesh_library_palette->set_item_text(item, name);
  724. mesh_library_palette->set_item_metadata(item, id);
  725. item++;
  726. }
  727. if (selected != -1 && mesh_library_palette->get_item_count() > 0) {
  728. mesh_library_palette->select(selected);
  729. }
  730. last_mesh_library = mesh_library.operator->();
  731. }
  732. void GridMapEditor::edit(GridMap *p_gridmap) {
  733. if (!p_gridmap && node)
  734. node->disconnect("cell_size_changed", this, "_draw_grids");
  735. node = p_gridmap;
  736. input_action = INPUT_NONE;
  737. selection.active = false;
  738. _update_selection_transform();
  739. _update_paste_indicator();
  740. spatial_editor = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  741. if (!node) {
  742. set_process(false);
  743. for (int i = 0; i < 3; i++) {
  744. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  745. }
  746. if (cursor_instance.is_valid()) {
  747. VisualServer::get_singleton()->instance_set_visible(cursor_instance, false);
  748. }
  749. return;
  750. }
  751. update_palette();
  752. set_process(true);
  753. clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
  754. _draw_grids(node->get_cell_size());
  755. update_grid();
  756. _update_clip();
  757. node->connect("cell_size_changed", this, "_draw_grids");
  758. }
  759. void GridMapEditor::_update_clip() {
  760. node->set_meta("_editor_clip_", clip_mode);
  761. if (clip_mode == CLIP_DISABLED)
  762. node->set_clip(false);
  763. else
  764. node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
  765. }
  766. void GridMapEditor::update_grid() {
  767. grid_xform.origin.x -= 1; // Force update in hackish way.
  768. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  769. edit_grid_xform.origin = grid_ofs;
  770. edit_grid_xform.basis = Basis();
  771. for (int i = 0; i < 3; i++) {
  772. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  773. }
  774. updating = true;
  775. floor->set_value(edit_floor[edit_axis]);
  776. updating = false;
  777. }
  778. void GridMapEditor::_draw_grids(const Vector3 &cell_size) {
  779. Vector3 edited_floor = node->has_meta("_editor_floor_") ? node->get_meta("_editor_floor_") : Variant();
  780. for (int i = 0; i < 3; i++) {
  781. if (VS::get_singleton()->mesh_get_surface_count(grid[i]) > 0)
  782. VS::get_singleton()->mesh_remove_surface(grid[i], 0);
  783. edit_floor[i] = edited_floor[i];
  784. }
  785. Vector<Vector3> grid_points[3];
  786. Vector<Color> grid_colors[3];
  787. for (int i = 0; i < 3; i++) {
  788. Vector3 axis;
  789. axis[i] = 1;
  790. Vector3 axis_n1;
  791. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  792. Vector3 axis_n2;
  793. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  794. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  795. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  796. Vector3 p = axis_n1 * j + axis_n2 * k;
  797. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  798. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  799. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  800. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  801. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  802. grid_points[i].push_back(p);
  803. grid_points[i].push_back(pk);
  804. grid_colors[i].push_back(Color(1, 1, 1, trans));
  805. grid_colors[i].push_back(Color(1, 1, 1, transk));
  806. grid_points[i].push_back(p);
  807. grid_points[i].push_back(pj);
  808. grid_colors[i].push_back(Color(1, 1, 1, trans));
  809. grid_colors[i].push_back(Color(1, 1, 1, transj));
  810. }
  811. }
  812. Array d;
  813. d.resize(VS::ARRAY_MAX);
  814. d[VS::ARRAY_VERTEX] = grid_points[i];
  815. d[VS::ARRAY_COLOR] = grid_colors[i];
  816. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  817. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  818. }
  819. }
  820. void GridMapEditor::_notification(int p_what) {
  821. switch (p_what) {
  822. case NOTIFICATION_ENTER_TREE: {
  823. get_tree()->connect("node_removed", this, "_node_removed");
  824. mesh_library_palette->connect("item_selected", this, "_item_selected_cbk");
  825. for (int i = 0; i < 3; i++) {
  826. grid[i] = VS::get_singleton()->mesh_create();
  827. grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  828. selection_level_instance[i] = VisualServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world()->get_scenario());
  829. }
  830. selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
  831. paste_instance = VisualServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world()->get_scenario());
  832. _update_selection_transform();
  833. _update_paste_indicator();
  834. } break;
  835. case NOTIFICATION_EXIT_TREE: {
  836. get_tree()->disconnect("node_removed", this, "_node_removed");
  837. _clear_clipboard_data();
  838. for (int i = 0; i < 3; i++) {
  839. VS::get_singleton()->free(grid_instance[i]);
  840. VS::get_singleton()->free(grid[i]);
  841. grid_instance[i] = RID();
  842. grid[i] = RID();
  843. VisualServer::get_singleton()->free(selection_level_instance[i]);
  844. }
  845. VisualServer::get_singleton()->free(selection_instance);
  846. VisualServer::get_singleton()->free(paste_instance);
  847. selection_instance = RID();
  848. paste_instance = RID();
  849. } break;
  850. case NOTIFICATION_PROCESS: {
  851. if (!node) {
  852. return;
  853. }
  854. Transform xf = node->get_global_transform();
  855. if (xf != grid_xform) {
  856. for (int i = 0; i < 3; i++) {
  857. VS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  858. }
  859. grid_xform = xf;
  860. }
  861. Ref<MeshLibrary> cgmt = node->get_mesh_library();
  862. if (cgmt.operator->() != last_mesh_library)
  863. update_palette();
  864. if (lock_view) {
  865. EditorNode *editor = Object::cast_to<EditorNode>(get_tree()->get_root()->get_child(0));
  866. Plane p;
  867. p.normal[edit_axis] = 1.0;
  868. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  869. p = node->get_transform().xform(p); // plane to snap
  870. SpatialEditorPlugin *sep = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  871. if (sep)
  872. sep->snap_cursor_to_plane(p);
  873. }
  874. } break;
  875. case NOTIFICATION_THEME_CHANGED: {
  876. options->set_icon(get_icon("GridMap", "EditorIcons"));
  877. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  878. } break;
  879. }
  880. }
  881. void GridMapEditor::_update_cursor_instance() {
  882. if (!node) {
  883. return;
  884. }
  885. if (cursor_instance.is_valid())
  886. VisualServer::get_singleton()->free(cursor_instance);
  887. cursor_instance = RID();
  888. if (selected_palette >= 0) {
  889. if (node && !node->get_mesh_library().is_null()) {
  890. Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette);
  891. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  892. cursor_instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
  893. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  894. }
  895. }
  896. }
  897. }
  898. void GridMapEditor::_item_selected_cbk(int idx) {
  899. selected_palette = mesh_library_palette->get_item_metadata(idx);
  900. _update_cursor_instance();
  901. }
  902. void GridMapEditor::_floor_changed(float p_value) {
  903. if (updating)
  904. return;
  905. edit_floor[edit_axis] = p_value;
  906. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  907. update_grid();
  908. _update_clip();
  909. _update_selection_transform();
  910. }
  911. void GridMapEditor::_floor_mouse_exited() {
  912. floor->get_line_edit()->release_focus();
  913. }
  914. void GridMapEditor::_bind_methods() {
  915. ClassDB::bind_method("_text_changed", &GridMapEditor::_text_changed);
  916. ClassDB::bind_method("_sbox_input", &GridMapEditor::_sbox_input);
  917. ClassDB::bind_method("_icon_size_changed", &GridMapEditor::_icon_size_changed);
  918. ClassDB::bind_method("_menu_option", &GridMapEditor::_menu_option);
  919. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  920. ClassDB::bind_method("_item_selected_cbk", &GridMapEditor::_item_selected_cbk);
  921. ClassDB::bind_method("_floor_changed", &GridMapEditor::_floor_changed);
  922. ClassDB::bind_method("_floor_mouse_exited", &GridMapEditor::_floor_mouse_exited);
  923. ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection);
  924. ClassDB::bind_method("_node_removed", &GridMapEditor::_node_removed);
  925. ClassDB::bind_method(D_METHOD("_set_display_mode", "mode"), &GridMapEditor::_set_display_mode);
  926. ClassDB::bind_method("_draw_grids", &GridMapEditor::_draw_grids);
  927. }
  928. GridMapEditor::GridMapEditor(EditorNode *p_editor) {
  929. input_action = INPUT_NONE;
  930. editor = p_editor;
  931. undo_redo = p_editor->get_undo_redo();
  932. int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230);
  933. Control *ec = memnew(Control);
  934. ec->set_custom_minimum_size(Size2(mw, 0) * EDSCALE);
  935. add_child(ec);
  936. spatial_editor_hb = memnew(HBoxContainer);
  937. spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  938. spatial_editor_hb->set_alignment(BoxContainer::ALIGN_END);
  939. SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  940. spin_box_label = memnew(Label);
  941. spin_box_label->set_text(TTR("Floor:"));
  942. spatial_editor_hb->add_child(spin_box_label);
  943. floor = memnew(SpinBox);
  944. floor->set_min(-32767);
  945. floor->set_max(32767);
  946. floor->set_step(1);
  947. floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
  948. spatial_editor_hb->add_child(floor);
  949. floor->connect("value_changed", this, "_floor_changed");
  950. floor->connect("mouse_exited", this, "_floor_mouse_exited");
  951. floor->get_line_edit()->connect("mouse_exited", this, "_floor_mouse_exited");
  952. spatial_editor_hb->add_child(memnew(VSeparator));
  953. options = memnew(MenuButton);
  954. spatial_editor_hb->add_child(options);
  955. spatial_editor_hb->hide();
  956. options->set_text(TTR("Grid Map"));
  957. options->get_popup()->add_check_item(TTR("Snap View"), MENU_OPTION_LOCK_VIEW);
  958. options->get_popup()->add_separator();
  959. options->get_popup()->add_item(TTR("Previous Floor"), MENU_OPTION_PREV_LEVEL, KEY_Q);
  960. options->get_popup()->add_item(TTR("Next Floor"), MENU_OPTION_NEXT_LEVEL, KEY_E);
  961. options->get_popup()->add_separator();
  962. options->get_popup()->add_radio_check_item(TTR("Clip Disabled"), MENU_OPTION_CLIP_DISABLED);
  963. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true);
  964. options->get_popup()->add_radio_check_item(TTR("Clip Above"), MENU_OPTION_CLIP_ABOVE);
  965. options->get_popup()->add_radio_check_item(TTR("Clip Below"), MENU_OPTION_CLIP_BELOW);
  966. options->get_popup()->add_separator();
  967. options->get_popup()->add_radio_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, KEY_Z);
  968. options->get_popup()->add_radio_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, KEY_X);
  969. options->get_popup()->add_radio_check_item(TTR("Edit Z Axis"), MENU_OPTION_Z_AXIS, KEY_C);
  970. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  971. options->get_popup()->add_separator();
  972. options->get_popup()->add_item(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
  973. options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
  974. options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
  975. options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
  976. options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
  977. options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
  978. options->get_popup()->add_item(TTR("Cursor Clear Rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION, KEY_W);
  979. options->get_popup()->add_separator();
  980. options->get_popup()->add_check_item("Paste Selects", MENU_OPTION_PASTE_SELECTS);
  981. options->get_popup()->add_separator();
  982. options->get_popup()->add_item(TTR("Duplicate Selection"), MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_CTRL + KEY_C);
  983. options->get_popup()->add_item(TTR("Cut Selection"), MENU_OPTION_SELECTION_CUT, KEY_MASK_CTRL + KEY_X);
  984. options->get_popup()->add_item(TTR("Clear Selection"), MENU_OPTION_SELECTION_CLEAR, KEY_DELETE);
  985. options->get_popup()->add_item(TTR("Fill Selection"), MENU_OPTION_SELECTION_FILL, KEY_MASK_CTRL + KEY_F);
  986. options->get_popup()->add_separator();
  987. options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
  988. settings_dialog = memnew(ConfirmationDialog);
  989. settings_dialog->set_title(TTR("GridMap Settings"));
  990. add_child(settings_dialog);
  991. settings_vbc = memnew(VBoxContainer);
  992. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  993. settings_dialog->add_child(settings_vbc);
  994. settings_pick_distance = memnew(SpinBox);
  995. settings_pick_distance->set_max(10000.0f);
  996. settings_pick_distance->set_min(500.0f);
  997. settings_pick_distance->set_step(1.0f);
  998. settings_pick_distance->set_value(EDITOR_DEF("editors/grid_map/pick_distance", 5000.0));
  999. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  1000. clip_mode = CLIP_DISABLED;
  1001. options->get_popup()->connect("id_pressed", this, "_menu_option");
  1002. HBoxContainer *hb = memnew(HBoxContainer);
  1003. add_child(hb);
  1004. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1005. search_box = memnew(LineEdit);
  1006. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1007. search_box->set_placeholder(TTR("Filter meshes"));
  1008. hb->add_child(search_box);
  1009. search_box->connect("text_changed", this, "_text_changed");
  1010. search_box->connect("gui_input", this, "_sbox_input");
  1011. mode_thumbnail = memnew(ToolButton);
  1012. mode_thumbnail->set_toggle_mode(true);
  1013. mode_thumbnail->set_pressed(true);
  1014. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
  1015. hb->add_child(mode_thumbnail);
  1016. mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
  1017. mode_list = memnew(ToolButton);
  1018. mode_list->set_toggle_mode(true);
  1019. mode_list->set_pressed(false);
  1020. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  1021. hb->add_child(mode_list);
  1022. mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
  1023. size_slider = memnew(HSlider);
  1024. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  1025. size_slider->set_min(0.1f);
  1026. size_slider->set_max(4.0f);
  1027. size_slider->set_step(0.1f);
  1028. size_slider->set_value(1.0f);
  1029. size_slider->connect("value_changed", this, "_icon_size_changed");
  1030. add_child(size_slider);
  1031. EDITOR_DEF("editors/grid_map/preview_size", 64);
  1032. display_mode = DISPLAY_THUMBNAIL;
  1033. mesh_library_palette = memnew(ItemList);
  1034. add_child(mesh_library_palette);
  1035. mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL);
  1036. info_message = memnew(Label);
  1037. info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes."));
  1038. info_message->set_valign(Label::VALIGN_CENTER);
  1039. info_message->set_align(Label::ALIGN_CENTER);
  1040. info_message->set_autowrap(true);
  1041. info_message->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  1042. mesh_library_palette->add_child(info_message);
  1043. edit_axis = Vector3::AXIS_Y;
  1044. edit_floor[0] = -1;
  1045. edit_floor[1] = -1;
  1046. edit_floor[2] = -1;
  1047. cursor_visible = false;
  1048. selected_palette = -1;
  1049. lock_view = false;
  1050. cursor_rot = 0;
  1051. selection_mesh = VisualServer::get_singleton()->mesh_create();
  1052. paste_mesh = VisualServer::get_singleton()->mesh_create();
  1053. {
  1054. // Selection mesh create.
  1055. PoolVector<Vector3> lines;
  1056. PoolVector<Vector3> triangles;
  1057. PoolVector<Vector3> square[3];
  1058. for (int i = 0; i < 6; i++) {
  1059. Vector3 face_points[4];
  1060. for (int j = 0; j < 4; j++) {
  1061. float v[3];
  1062. v[0] = 1.0;
  1063. v[1] = 1 - 2 * ((j >> 1) & 1);
  1064. v[2] = v[1] * (1 - 2 * (j & 1));
  1065. for (int k = 0; k < 3; k++) {
  1066. if (i < 3)
  1067. face_points[j][(i + k) % 3] = v[k];
  1068. else
  1069. face_points[3 - j][(i + k) % 3] = -v[k];
  1070. }
  1071. }
  1072. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1073. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1074. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1075. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1076. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1077. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1078. }
  1079. for (int i = 0; i < 12; i++) {
  1080. AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  1081. Vector3 a, b;
  1082. base.get_edge(i, a, b);
  1083. lines.push_back(a);
  1084. lines.push_back(b);
  1085. }
  1086. for (int i = 0; i < 3; i++) {
  1087. Vector3 points[4];
  1088. for (int j = 0; j < 4; j++) {
  1089. static const bool orderx[4] = { 0, 1, 1, 0 };
  1090. static const bool ordery[4] = { 0, 0, 1, 1 };
  1091. Vector3 sp;
  1092. if (orderx[j]) {
  1093. sp[(i + 1) % 3] = 1.0;
  1094. }
  1095. if (ordery[j]) {
  1096. sp[(i + 2) % 3] = 1.0;
  1097. }
  1098. points[j] = sp;
  1099. }
  1100. for (int j = 0; j < 4; j++) {
  1101. Vector3 ofs;
  1102. ofs[i] += 0.01;
  1103. square[i].push_back(points[j] - ofs);
  1104. square[i].push_back(points[(j + 1) % 4] - ofs);
  1105. square[i].push_back(points[j] + ofs);
  1106. square[i].push_back(points[(j + 1) % 4] + ofs);
  1107. }
  1108. }
  1109. Array d;
  1110. d.resize(VS::ARRAY_MAX);
  1111. inner_mat.instance();
  1112. inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2));
  1113. inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1114. inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1115. d[VS::ARRAY_VERTEX] = triangles;
  1116. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1117. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  1118. outer_mat.instance();
  1119. outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8));
  1120. outer_mat->set_on_top_of_alpha();
  1121. outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1122. outer_mat->set_line_width(3.0);
  1123. outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1124. selection_floor_mat.instance();
  1125. selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
  1126. selection_floor_mat->set_on_top_of_alpha();
  1127. selection_floor_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1128. selection_floor_mat->set_line_width(3.0);
  1129. d[VS::ARRAY_VERTEX] = lines;
  1130. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
  1131. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  1132. d[VS::ARRAY_VERTEX] = triangles;
  1133. VisualServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1134. VisualServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid());
  1135. d[VS::ARRAY_VERTEX] = lines;
  1136. VisualServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, VS::PRIMITIVE_LINES, d);
  1137. VisualServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid());
  1138. for (int i = 0; i < 3; i++) {
  1139. d[VS::ARRAY_VERTEX] = square[i];
  1140. selection_level_mesh[i] = VS::get_singleton()->mesh_create();
  1141. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], VS::PRIMITIVE_LINES, d);
  1142. VisualServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
  1143. }
  1144. }
  1145. _set_selection(false);
  1146. updating = false;
  1147. accumulated_floor_delta = 0.0;
  1148. indicator_mat.instance();
  1149. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1150. indicator_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1151. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  1152. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1153. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  1154. }
  1155. GridMapEditor::~GridMapEditor() {
  1156. _clear_clipboard_data();
  1157. for (int i = 0; i < 3; i++) {
  1158. if (grid[i].is_valid())
  1159. VisualServer::get_singleton()->free(grid[i]);
  1160. if (grid_instance[i].is_valid())
  1161. VisualServer::get_singleton()->free(grid_instance[i]);
  1162. if (cursor_instance.is_valid())
  1163. VisualServer::get_singleton()->free(cursor_instance);
  1164. if (selection_level_instance[i].is_valid())
  1165. VisualServer::get_singleton()->free(selection_level_instance[i]);
  1166. if (selection_level_mesh[i].is_valid())
  1167. VisualServer::get_singleton()->free(selection_level_mesh[i]);
  1168. }
  1169. VisualServer::get_singleton()->free(selection_mesh);
  1170. if (selection_instance.is_valid())
  1171. VisualServer::get_singleton()->free(selection_instance);
  1172. VisualServer::get_singleton()->free(paste_mesh);
  1173. if (paste_instance.is_valid())
  1174. VisualServer::get_singleton()->free(paste_instance);
  1175. }
  1176. void GridMapEditorPlugin::_notification(int p_what) {
  1177. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  1178. switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
  1179. case 0: { // Left.
  1180. SpatialEditor::get_singleton()->get_palette_split()->move_child(grid_map_editor, 0);
  1181. } break;
  1182. case 1: { // Right.
  1183. SpatialEditor::get_singleton()->get_palette_split()->move_child(grid_map_editor, 1);
  1184. } break;
  1185. }
  1186. }
  1187. }
  1188. void GridMapEditorPlugin::edit(Object *p_object) {
  1189. grid_map_editor->edit(Object::cast_to<GridMap>(p_object));
  1190. }
  1191. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1192. return p_object->is_class("GridMap");
  1193. }
  1194. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1195. if (p_visible) {
  1196. grid_map_editor->show();
  1197. grid_map_editor->spatial_editor_hb->show();
  1198. grid_map_editor->set_process(true);
  1199. } else {
  1200. grid_map_editor->spatial_editor_hb->hide();
  1201. grid_map_editor->hide();
  1202. grid_map_editor->edit(NULL);
  1203. grid_map_editor->set_process(false);
  1204. }
  1205. }
  1206. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  1207. editor = p_node;
  1208. EDITOR_DEF("editors/grid_map/editor_side", 1);
  1209. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/grid_map/editor_side", PROPERTY_HINT_ENUM, "Left,Right"));
  1210. grid_map_editor = memnew(GridMapEditor(editor));
  1211. switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
  1212. case 0: { // Left.
  1213. add_control_to_container(CONTAINER_SPATIAL_EDITOR_SIDE_LEFT, grid_map_editor);
  1214. } break;
  1215. case 1: { // Right.
  1216. add_control_to_container(CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT, grid_map_editor);
  1217. } break;
  1218. }
  1219. grid_map_editor->hide();
  1220. }
  1221. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1222. }