grid_map_editor_plugin.cpp 40 KB

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