grid_map_editor_plugin.cpp 43 KB

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