grid_map_editor_plugin.cpp 45 KB

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