grid_map_editor_plugin.cpp 41 KB

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