grid_map_editor_plugin.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "editor/editor_scale.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/plugins/spatial_editor_plugin.h"
  34. #include "os/input.h"
  35. #include "scene/3d/camera.h"
  36. #include "geometry.h"
  37. #include "os/keyboard.h"
  38. void GridMapEditor::_node_removed(Node *p_node) {
  39. if (p_node == node) {
  40. node = NULL;
  41. hide();
  42. theme_pallete->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 xf;
  239. xf.basis.scale(scale);
  240. xf.origin = pos;
  241. VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf);
  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_pallete < 0 && input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE)
  262. return false;
  263. Ref<MeshLibrary> theme = node->get_theme();
  264. if (theme.is_null())
  265. return false;
  266. if (input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE && !theme->has_item(selected_pallete))
  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_pallete = item;
  327. theme_pallete->set_current(item);
  328. update_pallete();
  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_pallete;
  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_pallete, 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_pallete, 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. return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true);
  495. } else {
  496. if (
  497. (mb->get_button_index() == BUTTON_RIGHT && input_action == INPUT_ERASE) ||
  498. (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_PAINT)) {
  499. if (set_items.size()) {
  500. undo_redo->create_action("GridMap Paint");
  501. for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
  502. const SetItem &si = E->get();
  503. undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
  504. }
  505. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  506. const SetItem &si = E->get();
  507. undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
  508. }
  509. undo_redo->commit_action();
  510. }
  511. set_items.clear();
  512. input_action = INPUT_NONE;
  513. return true;
  514. }
  515. if (mb->get_button_index() == BUTTON_LEFT && input_action != INPUT_NONE) {
  516. set_items.clear();
  517. input_action = INPUT_NONE;
  518. return true;
  519. }
  520. if (mb->get_button_index() == BUTTON_RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_DUPLICATE)) {
  521. input_action = INPUT_NONE;
  522. return true;
  523. }
  524. }
  525. }
  526. Ref<InputEventMouseMotion> mm = p_event;
  527. if (mm.is_valid()) {
  528. return do_input_action(p_camera, mm->get_position(), false);
  529. }
  530. Ref<InputEventPanGesture> pan_gesture = p_event;
  531. if (pan_gesture.is_valid()) {
  532. if (pan_gesture->get_alt() && (pan_gesture->get_command() || pan_gesture->get_shift())) {
  533. const real_t delta = pan_gesture->get_delta().y * 0.5;
  534. accumulated_floor_delta += delta;
  535. int step = 0;
  536. if (ABS(accumulated_floor_delta) > 1.0) {
  537. step = SGN(accumulated_floor_delta);
  538. accumulated_floor_delta -= step;
  539. }
  540. if (step) {
  541. floor->set_value(floor->get_value() + step);
  542. }
  543. return true;
  544. }
  545. }
  546. accumulated_floor_delta = 0.0;
  547. return false;
  548. }
  549. struct _CGMEItemSort {
  550. String name;
  551. int id;
  552. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  553. };
  554. void GridMapEditor::_set_display_mode(int p_mode) {
  555. if (display_mode == p_mode) {
  556. return;
  557. }
  558. if (p_mode == DISPLAY_LIST) {
  559. mode_list->set_pressed(true);
  560. mode_thumbnail->set_pressed(false);
  561. } else if (p_mode == DISPLAY_THUMBNAIL) {
  562. mode_list->set_pressed(false);
  563. mode_thumbnail->set_pressed(true);
  564. }
  565. display_mode = p_mode;
  566. update_pallete();
  567. }
  568. void GridMapEditor::update_pallete() {
  569. int selected = theme_pallete->get_current();
  570. theme_pallete->clear();
  571. if (display_mode == DISPLAY_THUMBNAIL) {
  572. theme_pallete->set_max_columns(0);
  573. theme_pallete->set_icon_mode(ItemList::ICON_MODE_TOP);
  574. } else if (display_mode == DISPLAY_LIST) {
  575. theme_pallete->set_max_columns(1);
  576. theme_pallete->set_icon_mode(ItemList::ICON_MODE_LEFT);
  577. }
  578. float min_size = EDITOR_DEF("editors/grid_map/preview_size", 64);
  579. theme_pallete->set_fixed_icon_size(Size2(min_size, min_size));
  580. theme_pallete->set_fixed_column_width(min_size * 3 / 2);
  581. theme_pallete->set_max_text_lines(2);
  582. Ref<MeshLibrary> theme = node->get_theme();
  583. if (theme.is_null()) {
  584. last_theme = NULL;
  585. return;
  586. }
  587. Vector<int> ids;
  588. ids = theme->get_item_list();
  589. List<_CGMEItemSort> il;
  590. for (int i = 0; i < ids.size(); i++) {
  591. _CGMEItemSort is;
  592. is.id = ids[i];
  593. is.name = theme->get_item_name(ids[i]);
  594. il.push_back(is);
  595. }
  596. il.sort();
  597. int item = 0;
  598. for (List<_CGMEItemSort>::Element *E = il.front(); E; E = E->next()) {
  599. int id = E->get().id;
  600. theme_pallete->add_item("");
  601. String name = theme->get_item_name(id);
  602. Ref<Texture> preview = theme->get_item_preview(id);
  603. if (!preview.is_null()) {
  604. theme_pallete->set_item_icon(item, preview);
  605. theme_pallete->set_item_tooltip(item, name);
  606. }
  607. if (name != "") {
  608. theme_pallete->set_item_text(item, name);
  609. }
  610. theme_pallete->set_item_metadata(item, id);
  611. item++;
  612. }
  613. if (selected != -1) {
  614. theme_pallete->select(selected);
  615. }
  616. last_theme = theme.operator->();
  617. }
  618. void GridMapEditor::edit(GridMap *p_gridmap) {
  619. node = p_gridmap;
  620. VS *vs = VS::get_singleton();
  621. last_mouseover = Vector3(-1, -1, -1);
  622. input_action = INPUT_NONE;
  623. selection.active = false;
  624. _update_selection_transform();
  625. _update_duplicate_indicator();
  626. spatial_editor = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  627. if (!node) {
  628. set_process(false);
  629. for (int i = 0; i < 3; i++) {
  630. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  631. }
  632. VisualServer::get_singleton()->instance_set_visible(cursor_instance, false);
  633. return;
  634. }
  635. update_pallete();
  636. set_process(true);
  637. Vector3 edited_floor = p_gridmap->has_meta("_editor_floor_") ? p_gridmap->get_meta("_editor_floor_") : Variant();
  638. clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
  639. for (int i = 0; i < 3; i++) {
  640. if (vs->mesh_get_surface_count(grid[i]) > 0)
  641. vs->mesh_remove_surface(grid[i], 0);
  642. edit_floor[i] = edited_floor[i];
  643. }
  644. {
  645. //update grids
  646. indicator_mat.instance();
  647. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  648. indicator_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  649. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  650. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  651. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  652. Vector<Vector3> grid_points[3];
  653. Vector<Color> grid_colors[3];
  654. float cell_size[3] = { p_gridmap->get_cell_size().x, p_gridmap->get_cell_size().y, p_gridmap->get_cell_size().z };
  655. for (int i = 0; i < 3; i++) {
  656. Vector3 axis;
  657. axis[i] = 1;
  658. Vector3 axis_n1;
  659. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  660. Vector3 axis_n2;
  661. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  662. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  663. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  664. Vector3 p = axis_n1 * j + axis_n2 * k;
  665. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  666. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  667. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  668. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  669. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  670. grid_points[i].push_back(p);
  671. grid_points[i].push_back(pk);
  672. grid_colors[i].push_back(Color(1, 1, 1, trans));
  673. grid_colors[i].push_back(Color(1, 1, 1, transk));
  674. grid_points[i].push_back(p);
  675. grid_points[i].push_back(pj);
  676. grid_colors[i].push_back(Color(1, 1, 1, trans));
  677. grid_colors[i].push_back(Color(1, 1, 1, transj));
  678. }
  679. }
  680. Array d;
  681. d.resize(VS::ARRAY_MAX);
  682. d[VS::ARRAY_VERTEX] = grid_points[i];
  683. d[VS::ARRAY_COLOR] = grid_colors[i];
  684. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  685. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  686. }
  687. }
  688. update_grid();
  689. _update_clip();
  690. }
  691. void GridMapEditor::_update_clip() {
  692. node->set_meta("_editor_clip_", clip_mode);
  693. if (clip_mode == CLIP_DISABLED)
  694. node->set_clip(false);
  695. else
  696. node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
  697. }
  698. void GridMapEditor::update_grid() {
  699. grid_xform.origin.x -= 1; //force update in hackish way.. what do i care
  700. //VS *vs = VS::get_singleton();
  701. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  702. edit_grid_xform.origin = grid_ofs;
  703. edit_grid_xform.basis = Basis();
  704. for (int i = 0; i < 3; i++) {
  705. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  706. }
  707. updating = true;
  708. floor->set_value(edit_floor[edit_axis]);
  709. updating = false;
  710. }
  711. void GridMapEditor::_notification(int p_what) {
  712. switch (p_what) {
  713. case NOTIFICATION_ENTER_TREE: {
  714. theme_pallete->connect("item_selected", this, "_item_selected_cbk");
  715. for (int i = 0; i < 3; i++) {
  716. grid[i] = VS::get_singleton()->mesh_create();
  717. grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  718. selection_level_instance[i] = VisualServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world()->get_scenario());
  719. }
  720. selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
  721. duplicate_instance = VisualServer::get_singleton()->instance_create2(duplicate_mesh, get_tree()->get_root()->get_world()->get_scenario());
  722. _update_selection_transform();
  723. _update_duplicate_indicator();
  724. } break;
  725. case NOTIFICATION_EXIT_TREE: {
  726. for (int i = 0; i < 3; i++) {
  727. VS::get_singleton()->free(grid_instance[i]);
  728. VS::get_singleton()->free(grid[i]);
  729. grid_instance[i] = RID();
  730. grid[i] = RID();
  731. VisualServer::get_singleton()->free(selection_level_instance[i]);
  732. }
  733. VisualServer::get_singleton()->free(selection_instance);
  734. VisualServer::get_singleton()->free(duplicate_instance);
  735. selection_instance = RID();
  736. duplicate_instance = RID();
  737. } break;
  738. case NOTIFICATION_PROCESS: {
  739. if (!node) {
  740. return;
  741. }
  742. Transform xf = node->get_global_transform();
  743. if (xf != grid_xform) {
  744. for (int i = 0; i < 3; i++) {
  745. VS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  746. }
  747. grid_xform = xf;
  748. }
  749. Ref<MeshLibrary> cgmt = node->get_theme();
  750. if (cgmt.operator->() != last_theme)
  751. update_pallete();
  752. if (lock_view) {
  753. EditorNode *editor = Object::cast_to<EditorNode>(get_tree()->get_root()->get_child(0));
  754. Plane p;
  755. p.normal[edit_axis] = 1.0;
  756. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  757. p = node->get_transform().xform(p); // plane to snap
  758. SpatialEditorPlugin *sep = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  759. if (sep)
  760. sep->snap_cursor_to_plane(p);
  761. //editor->get_editor_plugin_screen()->call("snap_cursor_to_plane",p);
  762. }
  763. } break;
  764. case NOTIFICATION_THEME_CHANGED: {
  765. options->set_icon(get_icon("GridMap", "EditorIcons"));
  766. } break;
  767. }
  768. }
  769. void GridMapEditor::_update_cursor_instance() {
  770. if (!node) {
  771. return;
  772. }
  773. if (cursor_instance.is_valid())
  774. VisualServer::get_singleton()->free(cursor_instance);
  775. cursor_instance = RID();
  776. if (selected_pallete >= 0) {
  777. if (node && !node->get_theme().is_null()) {
  778. Ref<Mesh> mesh = node->get_theme()->get_item_mesh(selected_pallete);
  779. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  780. cursor_instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
  781. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  782. }
  783. }
  784. }
  785. }
  786. void GridMapEditor::_item_selected_cbk(int idx) {
  787. selected_pallete = theme_pallete->get_item_metadata(idx);
  788. _update_cursor_instance();
  789. }
  790. void GridMapEditor::_floor_changed(float p_value) {
  791. if (updating)
  792. return;
  793. edit_floor[edit_axis] = p_value;
  794. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  795. update_grid();
  796. _update_clip();
  797. _update_selection_transform();
  798. }
  799. void GridMapEditor::_bind_methods() {
  800. ClassDB::bind_method("_menu_option", &GridMapEditor::_menu_option);
  801. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  802. ClassDB::bind_method("_item_selected_cbk", &GridMapEditor::_item_selected_cbk);
  803. ClassDB::bind_method("_floor_changed", &GridMapEditor::_floor_changed);
  804. ClassDB::bind_method(D_METHOD("_set_display_mode", "mode"), &GridMapEditor::_set_display_mode);
  805. }
  806. GridMapEditor::GridMapEditor(EditorNode *p_editor) {
  807. input_action = INPUT_NONE;
  808. editor = p_editor;
  809. undo_redo = p_editor->get_undo_redo();
  810. int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230);
  811. Control *ec = memnew(Control);
  812. ec->set_custom_minimum_size(Size2(mw, 0) * EDSCALE);
  813. add_child(ec);
  814. spatial_editor_hb = memnew(HBoxContainer);
  815. spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  816. spatial_editor_hb->set_alignment(BoxContainer::ALIGN_END);
  817. SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  818. spin_box_label = memnew(Label);
  819. spin_box_label->set_text(TTR("Floor:"));
  820. spatial_editor_hb->add_child(spin_box_label);
  821. floor = memnew(SpinBox);
  822. floor->set_min(-32767);
  823. floor->set_max(32767);
  824. floor->set_step(1);
  825. floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
  826. spatial_editor_hb->add_child(floor);
  827. floor->connect("value_changed", this, "_floor_changed");
  828. spatial_editor_hb->add_child(memnew(VSeparator));
  829. options = memnew(MenuButton);
  830. spatial_editor_hb->add_child(options);
  831. spatial_editor_hb->hide();
  832. options->set_text(TTR("Grid Map"));
  833. options->get_popup()->add_check_item(TTR("Snap View"), MENU_OPTION_LOCK_VIEW);
  834. options->get_popup()->add_separator();
  835. options->get_popup()->add_item(TTR("Previous Floor"), MENU_OPTION_PREV_LEVEL, KEY_Q);
  836. options->get_popup()->add_item(TTR("Next Floor"), MENU_OPTION_NEXT_LEVEL, KEY_E);
  837. options->get_popup()->add_separator();
  838. options->get_popup()->add_radio_check_item(TTR("Clip Disabled"), MENU_OPTION_CLIP_DISABLED);
  839. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true);
  840. options->get_popup()->add_radio_check_item(TTR("Clip Above"), MENU_OPTION_CLIP_ABOVE);
  841. options->get_popup()->add_radio_check_item(TTR("Clip Below"), MENU_OPTION_CLIP_BELOW);
  842. options->get_popup()->add_separator();
  843. options->get_popup()->add_radio_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, KEY_Z);
  844. options->get_popup()->add_radio_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, KEY_X);
  845. options->get_popup()->add_radio_check_item(TTR("Edit Z Axis"), MENU_OPTION_Z_AXIS, KEY_C);
  846. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  847. options->get_popup()->add_separator();
  848. options->get_popup()->add_item(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
  849. options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
  850. options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
  851. options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
  852. options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
  853. options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
  854. options->get_popup()->add_item(TTR("Cursor Clear Rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION, KEY_W);
  855. options->get_popup()->add_separator();
  856. options->get_popup()->add_check_item("Duplicate Selects", MENU_OPTION_DUPLICATE_SELECTS);
  857. options->get_popup()->add_separator();
  858. options->get_popup()->add_item(TTR("Create Area"), MENU_OPTION_SELECTION_MAKE_AREA, KEY_CONTROL + KEY_C);
  859. options->get_popup()->add_item(TTR("Create Exterior Connector"), MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR);
  860. options->get_popup()->add_item(TTR("Erase Area"), MENU_OPTION_REMOVE_AREA);
  861. options->get_popup()->add_separator();
  862. options->get_popup()->add_item(TTR("Duplicate Selection"), MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_SHIFT + KEY_C);
  863. options->get_popup()->add_item(TTR("Clear Selection"), MENU_OPTION_SELECTION_CLEAR, KEY_MASK_SHIFT + KEY_X);
  864. options->get_popup()->add_item(TTR("Fill Selection"), MENU_OPTION_SELECTION_FILL, KEY_MASK_SHIFT + KEY_F);
  865. options->get_popup()->add_separator();
  866. options->get_popup()->add_item(TTR("Settings"), MENU_OPTION_GRIDMAP_SETTINGS);
  867. settings_dialog = memnew(ConfirmationDialog);
  868. settings_dialog->set_title(TTR("GridMap Settings"));
  869. add_child(settings_dialog);
  870. settings_vbc = memnew(VBoxContainer);
  871. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  872. settings_dialog->add_child(settings_vbc);
  873. settings_pick_distance = memnew(SpinBox);
  874. settings_pick_distance->set_max(10000.0f);
  875. settings_pick_distance->set_min(500.0f);
  876. settings_pick_distance->set_step(1.0f);
  877. settings_pick_distance->set_value(EDITOR_DEF("editors/grid_map/pick_distance", 5000.0));
  878. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  879. clip_mode = CLIP_DISABLED;
  880. options->get_popup()->connect("id_pressed", this, "_menu_option");
  881. HBoxContainer *hb = memnew(HBoxContainer);
  882. add_child(hb);
  883. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  884. mode_thumbnail = memnew(ToolButton);
  885. mode_thumbnail->set_toggle_mode(true);
  886. mode_thumbnail->set_pressed(true);
  887. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
  888. hb->add_child(mode_thumbnail);
  889. mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
  890. mode_list = memnew(ToolButton);
  891. mode_list->set_toggle_mode(true);
  892. mode_list->set_pressed(false);
  893. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  894. hb->add_child(mode_list);
  895. mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
  896. EDITOR_DEF("editors/grid_map/preview_size", 64);
  897. display_mode = DISPLAY_THUMBNAIL;
  898. theme_pallete = memnew(ItemList);
  899. add_child(theme_pallete);
  900. theme_pallete->set_v_size_flags(SIZE_EXPAND_FILL);
  901. edit_axis = Vector3::AXIS_Y;
  902. edit_floor[0] = -1;
  903. edit_floor[1] = -1;
  904. edit_floor[2] = -1;
  905. cursor_visible = false;
  906. selected_pallete = -1;
  907. lock_view = false;
  908. cursor_rot = 0;
  909. last_mouseover = Vector3(-1, -1, -1);
  910. selection_mesh = VisualServer::get_singleton()->mesh_create();
  911. duplicate_mesh = VisualServer::get_singleton()->mesh_create();
  912. {
  913. //selection mesh create
  914. PoolVector<Vector3> lines;
  915. PoolVector<Vector3> triangles;
  916. PoolVector<Vector3> square[3];
  917. for (int i = 0; i < 6; i++) {
  918. Vector3 face_points[4];
  919. for (int j = 0; j < 4; j++) {
  920. float v[3];
  921. v[0] = 1.0;
  922. v[1] = 1 - 2 * ((j >> 1) & 1);
  923. v[2] = v[1] * (1 - 2 * (j & 1));
  924. for (int k = 0; k < 3; k++) {
  925. if (i < 3)
  926. face_points[j][(i + k) % 3] = v[k];
  927. else
  928. face_points[3 - j][(i + k) % 3] = -v[k];
  929. }
  930. }
  931. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  932. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  933. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  934. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  935. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  936. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  937. }
  938. for (int i = 0; i < 12; i++) {
  939. AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  940. Vector3 a, b;
  941. base.get_edge(i, a, b);
  942. lines.push_back(a);
  943. lines.push_back(b);
  944. }
  945. for (int i = 0; i < 3; i++) {
  946. Vector3 points[4];
  947. for (int j = 0; j < 4; j++) {
  948. static const bool orderx[4] = { 0, 1, 1, 0 };
  949. static const bool ordery[4] = { 0, 0, 1, 1 };
  950. Vector3 sp;
  951. if (orderx[j]) {
  952. sp[(i + 1) % 3] = 1.0;
  953. }
  954. if (ordery[j]) {
  955. sp[(i + 2) % 3] = 1.0;
  956. }
  957. points[j] = sp;
  958. }
  959. for (int j = 0; j < 4; j++) {
  960. Vector3 ofs;
  961. ofs[i] += 0.01;
  962. square[i].push_back(points[j] - ofs);
  963. square[i].push_back(points[(j + 1) % 4] - ofs);
  964. square[i].push_back(points[j] + ofs);
  965. square[i].push_back(points[(j + 1) % 4] + ofs);
  966. }
  967. }
  968. Array d;
  969. d.resize(VS::ARRAY_MAX);
  970. inner_mat.instance();
  971. inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2));
  972. //inner_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
  973. inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  974. inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  975. d[VS::ARRAY_VERTEX] = triangles;
  976. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
  977. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  978. outer_mat.instance();
  979. outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8));
  980. outer_mat->set_on_top_of_alpha();
  981. outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  982. outer_mat->set_line_width(3.0);
  983. outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  984. selection_floor_mat.instance();
  985. selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
  986. selection_floor_mat->set_on_top_of_alpha();
  987. selection_floor_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  988. selection_floor_mat->set_line_width(3.0);
  989. //selection_floor_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  990. d[VS::ARRAY_VERTEX] = lines;
  991. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
  992. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  993. d[VS::ARRAY_VERTEX] = triangles;
  994. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_TRIANGLES, d);
  995. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 0, inner_mat->get_rid());
  996. d[VS::ARRAY_VERTEX] = lines;
  997. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_LINES, d);
  998. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 1, outer_mat->get_rid());
  999. for (int i = 0; i < 3; i++) {
  1000. d[VS::ARRAY_VERTEX] = square[i];
  1001. selection_level_mesh[i] = VS::get_singleton()->mesh_create();
  1002. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], VS::PRIMITIVE_LINES, d);
  1003. VisualServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
  1004. }
  1005. }
  1006. selection.active = false;
  1007. updating = false;
  1008. accumulated_floor_delta = 0.0;
  1009. }
  1010. GridMapEditor::~GridMapEditor() {
  1011. for (int i = 0; i < 3; i++) {
  1012. if (grid[i].is_valid())
  1013. VisualServer::get_singleton()->free(grid[i]);
  1014. if (grid_instance[i].is_valid())
  1015. VisualServer::get_singleton()->free(grid_instance[i]);
  1016. if (cursor_instance.is_valid())
  1017. VisualServer::get_singleton()->free(cursor_instance);
  1018. if (selection_level_instance[i].is_valid())
  1019. VisualServer::get_singleton()->free(selection_level_instance[i]);
  1020. if (selection_level_mesh[i].is_valid())
  1021. VisualServer::get_singleton()->free(selection_level_mesh[i]);
  1022. }
  1023. VisualServer::get_singleton()->free(selection_mesh);
  1024. if (selection_instance.is_valid())
  1025. VisualServer::get_singleton()->free(selection_instance);
  1026. VisualServer::get_singleton()->free(duplicate_mesh);
  1027. if (duplicate_instance.is_valid())
  1028. VisualServer::get_singleton()->free(duplicate_instance);
  1029. }
  1030. void GridMapEditorPlugin::edit(Object *p_object) {
  1031. gridmap_editor->edit(Object::cast_to<GridMap>(p_object));
  1032. }
  1033. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1034. return p_object->is_class("GridMap");
  1035. }
  1036. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1037. if (p_visible) {
  1038. gridmap_editor->show();
  1039. gridmap_editor->spatial_editor_hb->show();
  1040. gridmap_editor->set_process(true);
  1041. } else {
  1042. gridmap_editor->spatial_editor_hb->hide();
  1043. gridmap_editor->hide();
  1044. gridmap_editor->edit(NULL);
  1045. gridmap_editor->set_process(false);
  1046. }
  1047. }
  1048. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  1049. editor = p_node;
  1050. gridmap_editor = memnew(GridMapEditor(editor));
  1051. SpatialEditor::get_singleton()->get_palette_split()->add_child(gridmap_editor);
  1052. // TODO: make this configurable, so the user can choose were to put this, it makes more sense
  1053. // on the right, but some people might find it strange.
  1054. SpatialEditor::get_singleton()->get_palette_split()->move_child(gridmap_editor, 1);
  1055. gridmap_editor->hide();
  1056. }
  1057. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1058. }