2
0

grid_map_editor_plugin.cpp 40 KB

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