grid_map_editor_plugin.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/input/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. }
  42. void GridMapEditor::_configure() {
  43. if (!node)
  44. return;
  45. update_grid();
  46. }
  47. void GridMapEditor::_menu_option(int p_option) {
  48. switch (p_option) {
  49. case MENU_OPTION_PREV_LEVEL: {
  50. floor->set_value(floor->get_value() - 1);
  51. } break;
  52. case MENU_OPTION_NEXT_LEVEL: {
  53. floor->set_value(floor->get_value() + 1);
  54. } break;
  55. case MENU_OPTION_LOCK_VIEW: {
  56. int index = options->get_popup()->get_item_index(MENU_OPTION_LOCK_VIEW);
  57. lock_view = !options->get_popup()->is_item_checked(index);
  58. options->get_popup()->set_item_checked(index, lock_view);
  59. } break;
  60. case MENU_OPTION_CLIP_DISABLED:
  61. case MENU_OPTION_CLIP_ABOVE:
  62. case MENU_OPTION_CLIP_BELOW: {
  63. clip_mode = ClipMode(p_option - MENU_OPTION_CLIP_DISABLED);
  64. for (int i = 0; i < 3; i++) {
  65. int index = options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED + i);
  66. options->get_popup()->set_item_checked(index, i == clip_mode);
  67. }
  68. _update_clip();
  69. } break;
  70. case MENU_OPTION_X_AXIS:
  71. case MENU_OPTION_Y_AXIS:
  72. case MENU_OPTION_Z_AXIS: {
  73. int new_axis = p_option - MENU_OPTION_X_AXIS;
  74. for (int i = 0; i < 3; i++) {
  75. int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i);
  76. options->get_popup()->set_item_checked(idx, i == new_axis);
  77. }
  78. if (edit_axis != new_axis) {
  79. int item1 = options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL);
  80. int item2 = options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL);
  81. if (edit_axis == Vector3::AXIS_Y) {
  82. options->get_popup()->set_item_text(item1, TTR("Next Plane"));
  83. options->get_popup()->set_item_text(item2, TTR("Previous Plane"));
  84. spin_box_label->set_text(TTR("Plane:"));
  85. } else if (new_axis == Vector3::AXIS_Y) {
  86. options->get_popup()->set_item_text(item1, TTR("Next Floor"));
  87. options->get_popup()->set_item_text(item2, TTR("Previous Floor"));
  88. spin_box_label->set_text(TTR("Floor:"));
  89. }
  90. }
  91. edit_axis = Vector3::Axis(new_axis);
  92. update_grid();
  93. _update_clip();
  94. } break;
  95. case MENU_OPTION_CURSOR_ROTATE_Y: {
  96. Basis r;
  97. if (input_action == INPUT_PASTE) {
  98. r.set_orthogonal_index(paste_indicator.orientation);
  99. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  100. paste_indicator.orientation = r.get_orthogonal_index();
  101. _update_paste_indicator();
  102. break;
  103. }
  104. r.set_orthogonal_index(cursor_rot);
  105. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  106. cursor_rot = r.get_orthogonal_index();
  107. _update_cursor_transform();
  108. } break;
  109. case MENU_OPTION_CURSOR_ROTATE_X: {
  110. Basis r;
  111. if (input_action == INPUT_PASTE) {
  112. r.set_orthogonal_index(paste_indicator.orientation);
  113. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  114. paste_indicator.orientation = r.get_orthogonal_index();
  115. _update_paste_indicator();
  116. break;
  117. }
  118. r.set_orthogonal_index(cursor_rot);
  119. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  120. cursor_rot = r.get_orthogonal_index();
  121. _update_cursor_transform();
  122. } break;
  123. case MENU_OPTION_CURSOR_ROTATE_Z: {
  124. Basis r;
  125. if (input_action == INPUT_PASTE) {
  126. r.set_orthogonal_index(paste_indicator.orientation);
  127. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  128. paste_indicator.orientation = r.get_orthogonal_index();
  129. _update_paste_indicator();
  130. break;
  131. }
  132. r.set_orthogonal_index(cursor_rot);
  133. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  134. cursor_rot = r.get_orthogonal_index();
  135. _update_cursor_transform();
  136. } break;
  137. case MENU_OPTION_CURSOR_BACK_ROTATE_Y: {
  138. Basis r;
  139. if (input_action == INPUT_PASTE) {
  140. r.set_orthogonal_index(paste_indicator.orientation);
  141. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  142. paste_indicator.orientation = r.get_orthogonal_index();
  143. _update_paste_indicator();
  144. break;
  145. }
  146. r.set_orthogonal_index(cursor_rot);
  147. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  148. cursor_rot = r.get_orthogonal_index();
  149. _update_cursor_transform();
  150. } break;
  151. case MENU_OPTION_CURSOR_BACK_ROTATE_X: {
  152. Basis r;
  153. if (input_action == INPUT_PASTE) {
  154. r.set_orthogonal_index(paste_indicator.orientation);
  155. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  156. paste_indicator.orientation = r.get_orthogonal_index();
  157. _update_paste_indicator();
  158. break;
  159. }
  160. r.set_orthogonal_index(cursor_rot);
  161. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  162. cursor_rot = r.get_orthogonal_index();
  163. _update_cursor_transform();
  164. } break;
  165. case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
  166. Basis r;
  167. if (input_action == INPUT_PASTE) {
  168. r.set_orthogonal_index(paste_indicator.orientation);
  169. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  170. paste_indicator.orientation = r.get_orthogonal_index();
  171. _update_paste_indicator();
  172. break;
  173. }
  174. r.set_orthogonal_index(cursor_rot);
  175. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  176. cursor_rot = r.get_orthogonal_index();
  177. _update_cursor_transform();
  178. } break;
  179. case MENU_OPTION_CURSOR_CLEAR_ROTATION: {
  180. if (input_action == INPUT_PASTE) {
  181. paste_indicator.orientation = 0;
  182. _update_paste_indicator();
  183. break;
  184. }
  185. cursor_rot = 0;
  186. _update_cursor_transform();
  187. } break;
  188. case MENU_OPTION_PASTE_SELECTS: {
  189. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  190. options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
  191. } break;
  192. case MENU_OPTION_SELECTION_DUPLICATE:
  193. case MENU_OPTION_SELECTION_CUT: {
  194. if (!(selection.active && input_action == INPUT_NONE))
  195. break;
  196. _set_clipboard_data();
  197. if (p_option == MENU_OPTION_SELECTION_CUT) {
  198. _delete_selection();
  199. }
  200. input_action = INPUT_PASTE;
  201. paste_indicator.click = selection.begin;
  202. paste_indicator.current = selection.begin;
  203. paste_indicator.begin = selection.begin;
  204. paste_indicator.end = selection.end;
  205. paste_indicator.orientation = 0;
  206. _update_paste_indicator();
  207. } break;
  208. case MENU_OPTION_SELECTION_CLEAR: {
  209. if (!selection.active)
  210. break;
  211. _delete_selection();
  212. } break;
  213. case MENU_OPTION_SELECTION_FILL: {
  214. if (!selection.active)
  215. return;
  216. _fill_selection();
  217. } break;
  218. case MENU_OPTION_GRIDMAP_SETTINGS: {
  219. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE);
  220. } break;
  221. }
  222. }
  223. void GridMapEditor::_update_cursor_transform() {
  224. cursor_transform = Transform();
  225. cursor_transform.origin = cursor_origin;
  226. cursor_transform.basis.set_orthogonal_index(cursor_rot);
  227. cursor_transform.basis *= node->get_cell_scale();
  228. cursor_transform = node->get_global_transform() * cursor_transform;
  229. if (cursor_instance.is_valid()) {
  230. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  231. VisualServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
  232. }
  233. }
  234. void GridMapEditor::_update_selection_transform() {
  235. Transform xf_zero;
  236. xf_zero.basis.set_zero();
  237. if (!selection.active) {
  238. VisualServer::get_singleton()->instance_set_transform(selection_instance, xf_zero);
  239. for (int i = 0; i < 3; i++) {
  240. VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  241. }
  242. return;
  243. }
  244. Transform xf;
  245. xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  246. xf.origin = selection.begin * node->get_cell_size();
  247. VisualServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  248. for (int i = 0; i < 3; i++) {
  249. if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) {
  250. VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  251. } else {
  252. Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
  253. scale[edit_axis] = 1.0;
  254. Vector3 pos = selection.begin;
  255. pos[edit_axis] = edit_floor[edit_axis];
  256. scale *= node->get_cell_size();
  257. pos *= node->get_cell_size();
  258. Transform xf2;
  259. xf2.basis.scale(scale);
  260. xf2.origin = pos;
  261. VisualServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2);
  262. }
  263. }
  264. }
  265. void GridMapEditor::_validate_selection() {
  266. if (!selection.active)
  267. return;
  268. selection.begin = selection.click;
  269. selection.end = selection.current;
  270. if (selection.begin.x > selection.end.x)
  271. SWAP(selection.begin.x, selection.end.x);
  272. if (selection.begin.y > selection.end.y)
  273. SWAP(selection.begin.y, selection.end.y);
  274. if (selection.begin.z > selection.end.z)
  275. SWAP(selection.begin.z, selection.end.z);
  276. _update_selection_transform();
  277. }
  278. void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) {
  279. selection.active = p_active;
  280. selection.begin = p_begin;
  281. selection.end = p_end;
  282. selection.click = p_begin;
  283. selection.current = p_end;
  284. if (is_visible_in_tree()) {
  285. _update_selection_transform();
  286. }
  287. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CLEAR), !selection.active);
  288. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CUT), !selection.active);
  289. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_DUPLICATE), !selection.active);
  290. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_FILL), !selection.active);
  291. }
  292. bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click) {
  293. if (!spatial_editor)
  294. return false;
  295. if (selected_palette < 0 && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE)
  296. return false;
  297. Ref<MeshLibrary> mesh_library = node->get_mesh_library();
  298. if (mesh_library.is_null())
  299. return false;
  300. if (input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette))
  301. return false;
  302. Camera *camera = p_camera;
  303. Vector3 from = camera->project_ray_origin(p_point);
  304. Vector3 normal = camera->project_ray_normal(p_point);
  305. Transform local_xform = node->get_global_transform().affine_inverse();
  306. Vector<Plane> planes = camera->get_frustum();
  307. from = local_xform.xform(from);
  308. normal = local_xform.basis.xform(normal).normalized();
  309. Plane p;
  310. p.normal[edit_axis] = 1.0;
  311. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  312. Vector3 inters;
  313. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters))
  314. return false;
  315. // Make sure the intersection is inside the frustum planes, to avoid
  316. // Painting on invisible regions.
  317. for (int i = 0; i < planes.size(); i++) {
  318. Plane fp = local_xform.xform(planes[i]);
  319. if (fp.is_point_over(inters))
  320. return false;
  321. }
  322. int cell[3];
  323. float cell_size[3] = { node->get_cell_size().x, node->get_cell_size().y, node->get_cell_size().z };
  324. for (int i = 0; i < 3; i++) {
  325. if (i == edit_axis)
  326. cell[i] = edit_floor[i];
  327. else {
  328. cell[i] = inters[i] / node->get_cell_size()[i];
  329. if (inters[i] < 0)
  330. cell[i] -= 1; // Compensate negative.
  331. grid_ofs[i] = cell[i] * cell_size[i];
  332. }
  333. }
  334. VS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform);
  335. if (cursor_instance.is_valid()) {
  336. 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();
  337. cursor_visible = true;
  338. if (input_action == INPUT_SELECT || input_action == INPUT_PASTE) {
  339. cursor_visible = false;
  340. }
  341. _update_cursor_transform();
  342. }
  343. if (input_action == INPUT_PASTE) {
  344. paste_indicator.current = Vector3(cell[0], cell[1], cell[2]);
  345. _update_paste_indicator();
  346. } else if (input_action == INPUT_SELECT) {
  347. selection.current = Vector3(cell[0], cell[1], cell[2]);
  348. if (p_click)
  349. selection.click = selection.current;
  350. selection.active = true;
  351. _validate_selection();
  352. return true;
  353. } else if (input_action == INPUT_PICK) {
  354. int item = node->get_cell_item(cell[0], cell[1], cell[2]);
  355. if (item >= 0) {
  356. selected_palette = item;
  357. mesh_library_palette->set_current(item);
  358. update_palette();
  359. _update_cursor_instance();
  360. }
  361. return true;
  362. }
  363. if (input_action == INPUT_PAINT) {
  364. SetItem si;
  365. si.pos = Vector3(cell[0], cell[1], cell[2]);
  366. si.new_value = selected_palette;
  367. si.new_orientation = cursor_rot;
  368. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  369. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  370. set_items.push_back(si);
  371. node->set_cell_item(cell[0], cell[1], cell[2], selected_palette, cursor_rot);
  372. return true;
  373. } else if (input_action == INPUT_ERASE) {
  374. SetItem si;
  375. si.pos = Vector3(cell[0], cell[1], cell[2]);
  376. si.new_value = -1;
  377. si.new_orientation = 0;
  378. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  379. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  380. set_items.push_back(si);
  381. node->set_cell_item(cell[0], cell[1], cell[2], -1);
  382. return true;
  383. }
  384. return false;
  385. }
  386. void GridMapEditor::_delete_selection() {
  387. if (!selection.active)
  388. return;
  389. undo_redo->create_action(TTR("GridMap Delete Selection"));
  390. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  391. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  392. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  393. undo_redo->add_do_method(node, "set_cell_item", i, j, k, GridMap::INVALID_CELL_ITEM);
  394. 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));
  395. }
  396. }
  397. }
  398. undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
  399. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  400. undo_redo->commit_action();
  401. }
  402. void GridMapEditor::_fill_selection() {
  403. if (!selection.active)
  404. return;
  405. undo_redo->create_action(TTR("GridMap Fill Selection"));
  406. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  407. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  408. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  409. undo_redo->add_do_method(node, "set_cell_item", i, j, k, selected_palette, cursor_rot);
  410. 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));
  411. }
  412. }
  413. }
  414. undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
  415. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  416. undo_redo->commit_action();
  417. }
  418. void GridMapEditor::_clear_clipboard_data() {
  419. for (List<ClipboardItem>::Element *E = clipboard_items.front(); E; E = E->next()) {
  420. VisualServer::get_singleton()->free(E->get().instance);
  421. }
  422. clipboard_items.clear();
  423. }
  424. void GridMapEditor::_set_clipboard_data() {
  425. _clear_clipboard_data();
  426. Ref<MeshLibrary> meshLibrary = node->get_mesh_library();
  427. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  428. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  429. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  430. int itm = node->get_cell_item(i, j, k);
  431. if (itm == GridMap::INVALID_CELL_ITEM)
  432. continue;
  433. Ref<Mesh> mesh = meshLibrary->get_item_mesh(itm);
  434. ClipboardItem item;
  435. item.cell_item = itm;
  436. item.grid_offset = Vector3(i, j, k) - selection.begin;
  437. item.orientation = node->get_cell_item_orientation(i, j, k);
  438. item.instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
  439. clipboard_items.push_back(item);
  440. }
  441. }
  442. }
  443. }
  444. void GridMapEditor::_update_paste_indicator() {
  445. if (input_action != INPUT_PASTE) {
  446. Transform xf;
  447. xf.basis.set_zero();
  448. VisualServer::get_singleton()->instance_set_transform(paste_instance, xf);
  449. return;
  450. }
  451. Vector3 center = 0.5 * Vector3(float(node->get_center_x()), float(node->get_center_y()), float(node->get_center_z()));
  452. Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size();
  453. Transform xf;
  454. xf.scale(scale);
  455. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  456. Basis rot;
  457. rot.set_orthogonal_index(paste_indicator.orientation);
  458. xf.basis = rot * xf.basis;
  459. xf.translate((-center * node->get_cell_size()) / scale);
  460. VisualServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf);
  461. for (List<ClipboardItem>::Element *E = clipboard_items.front(); E; E = E->next()) {
  462. ClipboardItem &item = E->get();
  463. xf = Transform();
  464. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  465. xf.basis = rot * xf.basis;
  466. xf.translate(item.grid_offset * node->get_cell_size());
  467. Basis item_rot;
  468. item_rot.set_orthogonal_index(item.orientation);
  469. xf.basis = item_rot * xf.basis * node->get_cell_scale();
  470. VisualServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf);
  471. }
  472. }
  473. void GridMapEditor::_do_paste() {
  474. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  475. bool reselect = options->get_popup()->is_item_checked(idx);
  476. Basis rot;
  477. rot.set_orthogonal_index(paste_indicator.orientation);
  478. Vector3 ofs = paste_indicator.current - paste_indicator.click;
  479. undo_redo->create_action(TTR("GridMap Paste Selection"));
  480. for (List<ClipboardItem>::Element *E = clipboard_items.front(); E; E = E->next()) {
  481. ClipboardItem &item = E->get();
  482. Vector3 pos = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;
  483. Basis orm;
  484. orm.set_orthogonal_index(item.orientation);
  485. orm = rot * orm;
  486. undo_redo->add_do_method(node, "set_cell_item", pos.x, pos.y, pos.z, item.cell_item, orm.get_orthogonal_index());
  487. 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));
  488. }
  489. if (reselect) {
  490. undo_redo->add_do_method(this, "_set_selection", true, paste_indicator.begin + ofs, paste_indicator.end + ofs);
  491. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  492. }
  493. undo_redo->commit_action();
  494. _clear_clipboard_data();
  495. }
  496. bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const Ref<InputEvent> &p_event) {
  497. if (!node) {
  498. return false;
  499. }
  500. Ref<InputEventMouseButton> mb = p_event;
  501. if (mb.is_valid()) {
  502. if (mb->get_button_index() == BUTTON_WHEEL_UP && (mb->get_command() || mb->get_shift())) {
  503. if (mb->is_pressed())
  504. floor->set_value(floor->get_value() + mb->get_factor());
  505. return true; // Eaten.
  506. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && (mb->get_command() || mb->get_shift())) {
  507. if (mb->is_pressed())
  508. floor->set_value(floor->get_value() - mb->get_factor());
  509. return true;
  510. }
  511. if (mb->is_pressed()) {
  512. SpatialEditorViewport::NavigationScheme nav_scheme = (SpatialEditorViewport::NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  513. if ((nav_scheme == SpatialEditorViewport::NAVIGATION_MAYA || nav_scheme == SpatialEditorViewport::NAVIGATION_MODO) && mb->get_alt()) {
  514. input_action = INPUT_NONE;
  515. } else if (mb->get_button_index() == BUTTON_LEFT) {
  516. bool can_edit = (node && node->get_mesh_library().is_valid());
  517. if (input_action == INPUT_PASTE) {
  518. _do_paste();
  519. input_action = INPUT_NONE;
  520. _update_paste_indicator();
  521. } else if (mb->get_shift() && can_edit) {
  522. input_action = INPUT_SELECT;
  523. last_selection = selection;
  524. } else if (mb->get_command() && can_edit) {
  525. input_action = INPUT_PICK;
  526. } else {
  527. input_action = INPUT_PAINT;
  528. set_items.clear();
  529. }
  530. } else if (mb->get_button_index() == BUTTON_RIGHT) {
  531. if (input_action == INPUT_PASTE) {
  532. _clear_clipboard_data();
  533. input_action = INPUT_NONE;
  534. _update_paste_indicator();
  535. return true;
  536. } else if (selection.active) {
  537. _set_selection(false);
  538. return true;
  539. } else {
  540. input_action = INPUT_ERASE;
  541. set_items.clear();
  542. }
  543. } else {
  544. return false;
  545. }
  546. return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true);
  547. } else {
  548. if ((mb->get_button_index() == BUTTON_RIGHT && input_action == INPUT_ERASE) || (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_PAINT)) {
  549. if (set_items.size()) {
  550. undo_redo->create_action(TTR("GridMap Paint"));
  551. for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
  552. const SetItem &si = E->get();
  553. undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
  554. }
  555. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  556. const SetItem &si = E->get();
  557. undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
  558. }
  559. undo_redo->commit_action();
  560. }
  561. set_items.clear();
  562. input_action = INPUT_NONE;
  563. return set_items.size() > 0;
  564. }
  565. if (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_SELECT) {
  566. undo_redo->create_action("GridMap Selection");
  567. undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  568. undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end);
  569. undo_redo->commit_action();
  570. }
  571. if (mb->get_button_index() == BUTTON_LEFT && input_action != INPUT_NONE) {
  572. set_items.clear();
  573. input_action = INPUT_NONE;
  574. return true;
  575. }
  576. if (mb->get_button_index() == BUTTON_RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) {
  577. input_action = INPUT_NONE;
  578. return true;
  579. }
  580. }
  581. }
  582. Ref<InputEventMouseMotion> mm = p_event;
  583. if (mm.is_valid()) {
  584. return do_input_action(p_camera, mm->get_position(), false);
  585. }
  586. Ref<InputEventKey> k = p_event;
  587. if (k.is_valid()) {
  588. if (k->is_pressed()) {
  589. if (k->get_keycode() == KEY_ESCAPE) {
  590. if (input_action == INPUT_PASTE) {
  591. _clear_clipboard_data();
  592. input_action = INPUT_NONE;
  593. _update_paste_indicator();
  594. return true;
  595. } else if (selection.active) {
  596. _set_selection(false);
  597. return true;
  598. } else {
  599. selected_palette = -1;
  600. mesh_library_palette->unselect_all();
  601. update_palette();
  602. _update_cursor_instance();
  603. return true;
  604. }
  605. }
  606. if (k->get_shift() && selection.active && input_action != INPUT_PASTE) {
  607. if (k->get_keycode() == options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL))) {
  608. selection.click[edit_axis]--;
  609. _validate_selection();
  610. return true;
  611. }
  612. if (k->get_keycode() == options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL))) {
  613. selection.click[edit_axis]++;
  614. _validate_selection();
  615. return true;
  616. }
  617. }
  618. }
  619. }
  620. Ref<InputEventPanGesture> pan_gesture = p_event;
  621. if (pan_gesture.is_valid()) {
  622. if (pan_gesture->get_alt() && (pan_gesture->get_command() || pan_gesture->get_shift())) {
  623. const real_t delta = pan_gesture->get_delta().y * 0.5;
  624. accumulated_floor_delta += delta;
  625. int step = 0;
  626. if (ABS(accumulated_floor_delta) > 1.0) {
  627. step = SGN(accumulated_floor_delta);
  628. accumulated_floor_delta -= step;
  629. }
  630. if (step) {
  631. floor->set_value(floor->get_value() + step);
  632. }
  633. return true;
  634. }
  635. }
  636. accumulated_floor_delta = 0.0;
  637. return false;
  638. }
  639. struct _CGMEItemSort {
  640. String name;
  641. int id;
  642. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  643. };
  644. void GridMapEditor::_set_display_mode(int p_mode) {
  645. if (display_mode == p_mode) {
  646. return;
  647. }
  648. if (p_mode == DISPLAY_LIST) {
  649. mode_list->set_pressed(true);
  650. mode_thumbnail->set_pressed(false);
  651. } else if (p_mode == DISPLAY_THUMBNAIL) {
  652. mode_list->set_pressed(false);
  653. mode_thumbnail->set_pressed(true);
  654. }
  655. display_mode = p_mode;
  656. update_palette();
  657. }
  658. void GridMapEditor::_text_changed(const String &p_text) {
  659. update_palette();
  660. }
  661. void GridMapEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  662. const Ref<InputEventKey> k = p_ie;
  663. if (k.is_valid() && (k->get_keycode() == KEY_UP || k->get_keycode() == KEY_DOWN || k->get_keycode() == KEY_PAGEUP || k->get_keycode() == KEY_PAGEDOWN)) {
  664. // Forward the key input to the ItemList so it can be scrolled
  665. mesh_library_palette->call("_gui_input", k);
  666. search_box->accept_event();
  667. }
  668. }
  669. void GridMapEditor::_mesh_library_palette_input(const Ref<InputEvent> &p_ie) {
  670. const Ref<InputEventMouseButton> mb = p_ie;
  671. // Zoom in/out using Ctrl + mouse wheel
  672. if (mb.is_valid() && mb->is_pressed() && mb->get_command()) {
  673. if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_UP) {
  674. size_slider->set_value(size_slider->get_value() + 0.2);
  675. }
  676. if (mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  677. size_slider->set_value(size_slider->get_value() - 0.2);
  678. }
  679. }
  680. }
  681. void GridMapEditor::_icon_size_changed(float p_value) {
  682. mesh_library_palette->set_icon_scale(p_value);
  683. update_palette();
  684. }
  685. void GridMapEditor::update_palette() {
  686. int selected = mesh_library_palette->get_current();
  687. float min_size = EDITOR_DEF("editors/grid_map/preview_size", 64);
  688. min_size *= EDSCALE;
  689. mesh_library_palette->clear();
  690. if (display_mode == DISPLAY_THUMBNAIL) {
  691. mesh_library_palette->set_max_columns(0);
  692. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  693. mesh_library_palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1.5));
  694. } else if (display_mode == DISPLAY_LIST) {
  695. mesh_library_palette->set_max_columns(1);
  696. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT);
  697. mesh_library_palette->set_fixed_column_width(0);
  698. }
  699. mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size));
  700. mesh_library_palette->set_max_text_lines(2);
  701. Ref<MeshLibrary> mesh_library = node->get_mesh_library();
  702. if (mesh_library.is_null()) {
  703. last_mesh_library = NULL;
  704. search_box->set_text("");
  705. search_box->set_editable(false);
  706. info_message->show();
  707. return;
  708. }
  709. search_box->set_editable(true);
  710. info_message->hide();
  711. Vector<int> ids;
  712. ids = mesh_library->get_item_list();
  713. List<_CGMEItemSort> il;
  714. for (int i = 0; i < ids.size(); i++) {
  715. _CGMEItemSort is;
  716. is.id = ids[i];
  717. is.name = mesh_library->get_item_name(ids[i]);
  718. il.push_back(is);
  719. }
  720. il.sort();
  721. String filter = search_box->get_text().strip_edges();
  722. int item = 0;
  723. for (List<_CGMEItemSort>::Element *E = il.front(); E; E = E->next()) {
  724. int id = E->get().id;
  725. String name = mesh_library->get_item_name(id);
  726. Ref<Texture2D> preview = mesh_library->get_item_preview(id);
  727. if (name == "") {
  728. name = "#" + itos(id);
  729. }
  730. if (filter != "" && !filter.is_subsequence_ofi(name))
  731. continue;
  732. mesh_library_palette->add_item("");
  733. if (!preview.is_null()) {
  734. mesh_library_palette->set_item_icon(item, preview);
  735. mesh_library_palette->set_item_tooltip(item, name);
  736. }
  737. mesh_library_palette->set_item_text(item, name);
  738. mesh_library_palette->set_item_metadata(item, id);
  739. item++;
  740. }
  741. if (selected != -1 && mesh_library_palette->get_item_count() > 0) {
  742. mesh_library_palette->select(selected);
  743. }
  744. last_mesh_library = mesh_library.operator->();
  745. }
  746. void GridMapEditor::edit(GridMap *p_gridmap) {
  747. if (!p_gridmap && node)
  748. node->disconnect("cell_size_changed", callable_mp(this, &GridMapEditor::_draw_grids));
  749. node = p_gridmap;
  750. input_action = INPUT_NONE;
  751. selection.active = false;
  752. _update_selection_transform();
  753. _update_paste_indicator();
  754. spatial_editor = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  755. if (!node) {
  756. set_process(false);
  757. for (int i = 0; i < 3; i++) {
  758. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  759. }
  760. if (cursor_instance.is_valid()) {
  761. VisualServer::get_singleton()->instance_set_visible(cursor_instance, false);
  762. }
  763. return;
  764. }
  765. update_palette();
  766. set_process(true);
  767. clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
  768. _draw_grids(node->get_cell_size());
  769. update_grid();
  770. _update_clip();
  771. node->connect("cell_size_changed", callable_mp(this, &GridMapEditor::_draw_grids));
  772. }
  773. void GridMapEditor::_update_clip() {
  774. node->set_meta("_editor_clip_", clip_mode);
  775. if (clip_mode == CLIP_DISABLED)
  776. node->set_clip(false);
  777. else
  778. node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
  779. }
  780. void GridMapEditor::update_grid() {
  781. grid_xform.origin.x -= 1; // Force update in hackish way.
  782. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  783. edit_grid_xform.origin = grid_ofs;
  784. edit_grid_xform.basis = Basis();
  785. for (int i = 0; i < 3; i++) {
  786. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  787. }
  788. updating = true;
  789. floor->set_value(edit_floor[edit_axis]);
  790. updating = false;
  791. }
  792. void GridMapEditor::_draw_grids(const Vector3 &cell_size) {
  793. Vector3 edited_floor = node->has_meta("_editor_floor_") ? node->get_meta("_editor_floor_") : Variant();
  794. for (int i = 0; i < 3; i++) {
  795. VS::get_singleton()->mesh_clear(grid[i]);
  796. edit_floor[i] = edited_floor[i];
  797. }
  798. Vector<Vector3> grid_points[3];
  799. Vector<Color> grid_colors[3];
  800. for (int i = 0; i < 3; i++) {
  801. Vector3 axis;
  802. axis[i] = 1;
  803. Vector3 axis_n1;
  804. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  805. Vector3 axis_n2;
  806. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  807. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  808. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  809. Vector3 p = axis_n1 * j + axis_n2 * k;
  810. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  811. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  812. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  813. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  814. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  815. grid_points[i].push_back(p);
  816. grid_points[i].push_back(pk);
  817. grid_colors[i].push_back(Color(1, 1, 1, trans));
  818. grid_colors[i].push_back(Color(1, 1, 1, transk));
  819. grid_points[i].push_back(p);
  820. grid_points[i].push_back(pj);
  821. grid_colors[i].push_back(Color(1, 1, 1, trans));
  822. grid_colors[i].push_back(Color(1, 1, 1, transj));
  823. }
  824. }
  825. Array d;
  826. d.resize(VS::ARRAY_MAX);
  827. d[VS::ARRAY_VERTEX] = grid_points[i];
  828. d[VS::ARRAY_COLOR] = grid_colors[i];
  829. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  830. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  831. }
  832. }
  833. void GridMapEditor::_notification(int p_what) {
  834. switch (p_what) {
  835. case NOTIFICATION_ENTER_TREE: {
  836. get_tree()->connect("node_removed", callable_mp(this, &GridMapEditor::_node_removed));
  837. mesh_library_palette->connect("item_selected", callable_mp(this, &GridMapEditor::_item_selected_cbk));
  838. for (int i = 0; i < 3; i++) {
  839. grid[i] = VS::get_singleton()->mesh_create();
  840. grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  841. selection_level_instance[i] = VisualServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world()->get_scenario());
  842. }
  843. selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
  844. paste_instance = VisualServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world()->get_scenario());
  845. _update_selection_transform();
  846. _update_paste_indicator();
  847. } break;
  848. case NOTIFICATION_EXIT_TREE: {
  849. get_tree()->disconnect("node_removed", callable_mp(this, &GridMapEditor::_node_removed));
  850. _clear_clipboard_data();
  851. for (int i = 0; i < 3; i++) {
  852. VS::get_singleton()->free(grid_instance[i]);
  853. VS::get_singleton()->free(grid[i]);
  854. grid_instance[i] = RID();
  855. grid[i] = RID();
  856. VisualServer::get_singleton()->free(selection_level_instance[i]);
  857. }
  858. VisualServer::get_singleton()->free(selection_instance);
  859. VisualServer::get_singleton()->free(paste_instance);
  860. selection_instance = RID();
  861. paste_instance = RID();
  862. } break;
  863. case NOTIFICATION_PROCESS: {
  864. if (!node) {
  865. return;
  866. }
  867. Transform xf = node->get_global_transform();
  868. if (xf != grid_xform) {
  869. for (int i = 0; i < 3; i++) {
  870. VS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  871. }
  872. grid_xform = xf;
  873. }
  874. Ref<MeshLibrary> cgmt = node->get_mesh_library();
  875. if (cgmt.operator->() != last_mesh_library)
  876. update_palette();
  877. if (lock_view) {
  878. EditorNode *editor = Object::cast_to<EditorNode>(get_tree()->get_root()->get_child(0));
  879. Plane p;
  880. p.normal[edit_axis] = 1.0;
  881. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  882. p = node->get_transform().xform(p); // plane to snap
  883. SpatialEditorPlugin *sep = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  884. if (sep)
  885. sep->snap_cursor_to_plane(p);
  886. }
  887. } break;
  888. case NOTIFICATION_THEME_CHANGED: {
  889. options->set_icon(get_icon("GridMap", "EditorIcons"));
  890. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  891. } break;
  892. }
  893. }
  894. void GridMapEditor::_update_cursor_instance() {
  895. if (!node) {
  896. return;
  897. }
  898. if (cursor_instance.is_valid())
  899. VisualServer::get_singleton()->free(cursor_instance);
  900. cursor_instance = RID();
  901. if (selected_palette >= 0) {
  902. if (node && !node->get_mesh_library().is_null()) {
  903. Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette);
  904. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  905. cursor_instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
  906. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  907. }
  908. }
  909. }
  910. }
  911. void GridMapEditor::_item_selected_cbk(int idx) {
  912. selected_palette = mesh_library_palette->get_item_metadata(idx);
  913. _update_cursor_instance();
  914. }
  915. void GridMapEditor::_floor_changed(float p_value) {
  916. if (updating)
  917. return;
  918. edit_floor[edit_axis] = p_value;
  919. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  920. update_grid();
  921. _update_clip();
  922. _update_selection_transform();
  923. }
  924. void GridMapEditor::_floor_mouse_exited() {
  925. floor->get_line_edit()->release_focus();
  926. }
  927. void GridMapEditor::_bind_methods() {
  928. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  929. ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection);
  930. }
  931. GridMapEditor::GridMapEditor(EditorNode *p_editor) {
  932. input_action = INPUT_NONE;
  933. editor = p_editor;
  934. undo_redo = p_editor->get_undo_redo();
  935. int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230);
  936. Control *ec = memnew(Control);
  937. ec->set_custom_minimum_size(Size2(mw, 0) * EDSCALE);
  938. add_child(ec);
  939. spatial_editor_hb = memnew(HBoxContainer);
  940. spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  941. spatial_editor_hb->set_alignment(BoxContainer::ALIGN_END);
  942. SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  943. spin_box_label = memnew(Label);
  944. spin_box_label->set_text(TTR("Floor:"));
  945. spatial_editor_hb->add_child(spin_box_label);
  946. floor = memnew(SpinBox);
  947. floor->set_min(-32767);
  948. floor->set_max(32767);
  949. floor->set_step(1);
  950. floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
  951. spatial_editor_hb->add_child(floor);
  952. floor->connect("value_changed", callable_mp(this, &GridMapEditor::_floor_changed));
  953. floor->connect("mouse_exited", callable_mp(this, &GridMapEditor::_floor_mouse_exited));
  954. floor->get_line_edit()->connect("mouse_exited", callable_mp(this, &GridMapEditor::_floor_mouse_exited));
  955. spatial_editor_hb->add_child(memnew(VSeparator));
  956. options = memnew(MenuButton);
  957. spatial_editor_hb->add_child(options);
  958. spatial_editor_hb->hide();
  959. options->set_text(TTR("Grid Map"));
  960. options->get_popup()->add_check_item(TTR("Snap View"), MENU_OPTION_LOCK_VIEW);
  961. options->get_popup()->add_separator();
  962. options->get_popup()->add_item(TTR("Previous Floor"), MENU_OPTION_PREV_LEVEL, KEY_Q);
  963. options->get_popup()->add_item(TTR("Next Floor"), MENU_OPTION_NEXT_LEVEL, KEY_E);
  964. options->get_popup()->add_separator();
  965. options->get_popup()->add_radio_check_item(TTR("Clip Disabled"), MENU_OPTION_CLIP_DISABLED);
  966. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true);
  967. options->get_popup()->add_radio_check_item(TTR("Clip Above"), MENU_OPTION_CLIP_ABOVE);
  968. options->get_popup()->add_radio_check_item(TTR("Clip Below"), MENU_OPTION_CLIP_BELOW);
  969. options->get_popup()->add_separator();
  970. options->get_popup()->add_radio_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, KEY_Z);
  971. options->get_popup()->add_radio_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, KEY_X);
  972. options->get_popup()->add_radio_check_item(TTR("Edit Z Axis"), MENU_OPTION_Z_AXIS, KEY_C);
  973. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  974. options->get_popup()->add_separator();
  975. options->get_popup()->add_item(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
  976. options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
  977. options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
  978. options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
  979. options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
  980. options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
  981. options->get_popup()->add_item(TTR("Cursor Clear Rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION, KEY_W);
  982. options->get_popup()->add_separator();
  983. options->get_popup()->add_check_item(TTR("Paste Selects"), MENU_OPTION_PASTE_SELECTS);
  984. options->get_popup()->add_separator();
  985. options->get_popup()->add_item(TTR("Duplicate Selection"), MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_CTRL + KEY_C);
  986. options->get_popup()->add_item(TTR("Cut Selection"), MENU_OPTION_SELECTION_CUT, KEY_MASK_CTRL + KEY_X);
  987. options->get_popup()->add_item(TTR("Clear Selection"), MENU_OPTION_SELECTION_CLEAR, KEY_DELETE);
  988. options->get_popup()->add_item(TTR("Fill Selection"), MENU_OPTION_SELECTION_FILL, KEY_MASK_CTRL + KEY_F);
  989. options->get_popup()->add_separator();
  990. options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
  991. settings_dialog = memnew(ConfirmationDialog);
  992. settings_dialog->set_title(TTR("GridMap Settings"));
  993. add_child(settings_dialog);
  994. settings_vbc = memnew(VBoxContainer);
  995. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  996. settings_dialog->add_child(settings_vbc);
  997. settings_pick_distance = memnew(SpinBox);
  998. settings_pick_distance->set_max(10000.0f);
  999. settings_pick_distance->set_min(500.0f);
  1000. settings_pick_distance->set_step(1.0f);
  1001. settings_pick_distance->set_value(EDITOR_DEF("editors/grid_map/pick_distance", 5000.0));
  1002. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  1003. clip_mode = CLIP_DISABLED;
  1004. options->get_popup()->connect("id_pressed", callable_mp(this, &GridMapEditor::_menu_option));
  1005. HBoxContainer *hb = memnew(HBoxContainer);
  1006. add_child(hb);
  1007. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1008. search_box = memnew(LineEdit);
  1009. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1010. search_box->set_placeholder(TTR("Filter meshes"));
  1011. hb->add_child(search_box);
  1012. search_box->connect("text_changed", callable_mp(this, &GridMapEditor::_text_changed));
  1013. search_box->connect("gui_input", callable_mp(this, &GridMapEditor::_sbox_input));
  1014. mode_thumbnail = memnew(ToolButton);
  1015. mode_thumbnail->set_toggle_mode(true);
  1016. mode_thumbnail->set_pressed(true);
  1017. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
  1018. hb->add_child(mode_thumbnail);
  1019. mode_thumbnail->connect("pressed", callable_mp(this, &GridMapEditor::_set_display_mode), varray(DISPLAY_THUMBNAIL));
  1020. mode_list = memnew(ToolButton);
  1021. mode_list->set_toggle_mode(true);
  1022. mode_list->set_pressed(false);
  1023. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  1024. hb->add_child(mode_list);
  1025. mode_list->connect("pressed", callable_mp(this, &GridMapEditor::_set_display_mode), varray(DISPLAY_LIST));
  1026. size_slider = memnew(HSlider);
  1027. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  1028. size_slider->set_min(0.2f);
  1029. size_slider->set_max(4.0f);
  1030. size_slider->set_step(0.1f);
  1031. size_slider->set_value(1.0f);
  1032. size_slider->connect("value_changed", callable_mp(this, &GridMapEditor::_icon_size_changed));
  1033. add_child(size_slider);
  1034. EDITOR_DEF("editors/grid_map/preview_size", 64);
  1035. display_mode = DISPLAY_THUMBNAIL;
  1036. mesh_library_palette = memnew(ItemList);
  1037. add_child(mesh_library_palette);
  1038. mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL);
  1039. mesh_library_palette->connect("gui_input", callable_mp(this, &GridMapEditor::_mesh_library_palette_input));
  1040. info_message = memnew(Label);
  1041. info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes."));
  1042. info_message->set_valign(Label::VALIGN_CENTER);
  1043. info_message->set_align(Label::ALIGN_CENTER);
  1044. info_message->set_autowrap(true);
  1045. info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  1046. info_message->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  1047. mesh_library_palette->add_child(info_message);
  1048. edit_axis = Vector3::AXIS_Y;
  1049. edit_floor[0] = -1;
  1050. edit_floor[1] = -1;
  1051. edit_floor[2] = -1;
  1052. cursor_visible = false;
  1053. selected_palette = -1;
  1054. lock_view = false;
  1055. cursor_rot = 0;
  1056. selection_mesh = VisualServer::get_singleton()->mesh_create();
  1057. paste_mesh = VisualServer::get_singleton()->mesh_create();
  1058. {
  1059. // Selection mesh create.
  1060. Vector<Vector3> lines;
  1061. Vector<Vector3> triangles;
  1062. Vector<Vector3> square[3];
  1063. for (int i = 0; i < 6; i++) {
  1064. Vector3 face_points[4];
  1065. for (int j = 0; j < 4; j++) {
  1066. float v[3];
  1067. v[0] = 1.0;
  1068. v[1] = 1 - 2 * ((j >> 1) & 1);
  1069. v[2] = v[1] * (1 - 2 * (j & 1));
  1070. for (int k = 0; k < 3; k++) {
  1071. if (i < 3)
  1072. face_points[j][(i + k) % 3] = v[k];
  1073. else
  1074. face_points[3 - j][(i + k) % 3] = -v[k];
  1075. }
  1076. }
  1077. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1078. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1079. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1080. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1081. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1082. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1083. }
  1084. for (int i = 0; i < 12; i++) {
  1085. AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  1086. Vector3 a, b;
  1087. base.get_edge(i, a, b);
  1088. lines.push_back(a);
  1089. lines.push_back(b);
  1090. }
  1091. for (int i = 0; i < 3; i++) {
  1092. Vector3 points[4];
  1093. for (int j = 0; j < 4; j++) {
  1094. static const bool orderx[4] = { 0, 1, 1, 0 };
  1095. static const bool ordery[4] = { 0, 0, 1, 1 };
  1096. Vector3 sp;
  1097. if (orderx[j]) {
  1098. sp[(i + 1) % 3] = 1.0;
  1099. }
  1100. if (ordery[j]) {
  1101. sp[(i + 2) % 3] = 1.0;
  1102. }
  1103. points[j] = sp;
  1104. }
  1105. for (int j = 0; j < 4; j++) {
  1106. Vector3 ofs;
  1107. ofs[i] += 0.01;
  1108. square[i].push_back(points[j] - ofs);
  1109. square[i].push_back(points[(j + 1) % 4] - ofs);
  1110. square[i].push_back(points[j] + ofs);
  1111. square[i].push_back(points[(j + 1) % 4] + ofs);
  1112. }
  1113. }
  1114. Array d;
  1115. d.resize(VS::ARRAY_MAX);
  1116. inner_mat.instance();
  1117. inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2));
  1118. inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1119. inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1120. d[VS::ARRAY_VERTEX] = triangles;
  1121. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1122. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  1123. outer_mat.instance();
  1124. outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8));
  1125. outer_mat->set_on_top_of_alpha();
  1126. outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1127. outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1128. selection_floor_mat.instance();
  1129. selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
  1130. selection_floor_mat->set_on_top_of_alpha();
  1131. selection_floor_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1132. d[VS::ARRAY_VERTEX] = lines;
  1133. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
  1134. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  1135. d[VS::ARRAY_VERTEX] = triangles;
  1136. VisualServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1137. VisualServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid());
  1138. d[VS::ARRAY_VERTEX] = lines;
  1139. VisualServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, VS::PRIMITIVE_LINES, d);
  1140. VisualServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid());
  1141. for (int i = 0; i < 3; i++) {
  1142. d[VS::ARRAY_VERTEX] = square[i];
  1143. selection_level_mesh[i] = VS::get_singleton()->mesh_create();
  1144. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], VS::PRIMITIVE_LINES, d);
  1145. VisualServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
  1146. }
  1147. }
  1148. _set_selection(false);
  1149. updating = false;
  1150. accumulated_floor_delta = 0.0;
  1151. indicator_mat.instance();
  1152. indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1153. indicator_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1154. indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  1155. indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1156. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  1157. }
  1158. GridMapEditor::~GridMapEditor() {
  1159. _clear_clipboard_data();
  1160. for (int i = 0; i < 3; i++) {
  1161. if (grid[i].is_valid())
  1162. VisualServer::get_singleton()->free(grid[i]);
  1163. if (grid_instance[i].is_valid())
  1164. VisualServer::get_singleton()->free(grid_instance[i]);
  1165. if (cursor_instance.is_valid())
  1166. VisualServer::get_singleton()->free(cursor_instance);
  1167. if (selection_level_instance[i].is_valid())
  1168. VisualServer::get_singleton()->free(selection_level_instance[i]);
  1169. if (selection_level_mesh[i].is_valid())
  1170. VisualServer::get_singleton()->free(selection_level_mesh[i]);
  1171. }
  1172. VisualServer::get_singleton()->free(selection_mesh);
  1173. if (selection_instance.is_valid())
  1174. VisualServer::get_singleton()->free(selection_instance);
  1175. VisualServer::get_singleton()->free(paste_mesh);
  1176. if (paste_instance.is_valid())
  1177. VisualServer::get_singleton()->free(paste_instance);
  1178. }
  1179. void GridMapEditorPlugin::_notification(int p_what) {
  1180. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  1181. switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
  1182. case 0: { // Left.
  1183. SpatialEditor::get_singleton()->get_palette_split()->move_child(grid_map_editor, 0);
  1184. } break;
  1185. case 1: { // Right.
  1186. SpatialEditor::get_singleton()->get_palette_split()->move_child(grid_map_editor, 1);
  1187. } break;
  1188. }
  1189. }
  1190. }
  1191. void GridMapEditorPlugin::edit(Object *p_object) {
  1192. grid_map_editor->edit(Object::cast_to<GridMap>(p_object));
  1193. }
  1194. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1195. return p_object->is_class("GridMap");
  1196. }
  1197. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1198. if (p_visible) {
  1199. grid_map_editor->show();
  1200. grid_map_editor->spatial_editor_hb->show();
  1201. grid_map_editor->set_process(true);
  1202. } else {
  1203. grid_map_editor->spatial_editor_hb->hide();
  1204. grid_map_editor->hide();
  1205. grid_map_editor->edit(NULL);
  1206. grid_map_editor->set_process(false);
  1207. }
  1208. }
  1209. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  1210. editor = p_node;
  1211. EDITOR_DEF("editors/grid_map/editor_side", 1);
  1212. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/grid_map/editor_side", PROPERTY_HINT_ENUM, "Left,Right"));
  1213. grid_map_editor = memnew(GridMapEditor(editor));
  1214. switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
  1215. case 0: { // Left.
  1216. add_control_to_container(CONTAINER_SPATIAL_EDITOR_SIDE_LEFT, grid_map_editor);
  1217. } break;
  1218. case 1: { // Right.
  1219. add_control_to_container(CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT, grid_map_editor);
  1220. } break;
  1221. }
  1222. grid_map_editor->hide();
  1223. }
  1224. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1225. }