grid_map_editor_plugin.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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. }
  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_scancode() == 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_scancode() == 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_scancode() == 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. Ref<InputEventKey> k = p_ie;
  663. if (k.is_valid() && (k->get_scancode() == KEY_UP || k->get_scancode() == KEY_DOWN || k->get_scancode() == KEY_PAGEUP || k->get_scancode() == KEY_PAGEDOWN)) {
  664. mesh_library_palette->call("_gui_input", k);
  665. search_box->accept_event();
  666. }
  667. }
  668. void GridMapEditor::_icon_size_changed(float p_value) {
  669. mesh_library_palette->set_icon_scale(p_value);
  670. update_palette();
  671. }
  672. void GridMapEditor::update_palette() {
  673. int selected = mesh_library_palette->get_current();
  674. float min_size = EDITOR_DEF("editors/grid_map/preview_size", 64);
  675. min_size *= EDSCALE;
  676. mesh_library_palette->clear();
  677. if (display_mode == DISPLAY_THUMBNAIL) {
  678. mesh_library_palette->set_max_columns(0);
  679. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  680. mesh_library_palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1.5));
  681. } else if (display_mode == DISPLAY_LIST) {
  682. mesh_library_palette->set_max_columns(1);
  683. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT);
  684. mesh_library_palette->set_fixed_column_width(0);
  685. }
  686. mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size));
  687. mesh_library_palette->set_max_text_lines(2);
  688. Ref<MeshLibrary> mesh_library = node->get_mesh_library();
  689. if (mesh_library.is_null()) {
  690. last_mesh_library = NULL;
  691. search_box->set_text("");
  692. search_box->set_editable(false);
  693. info_message->show();
  694. return;
  695. }
  696. search_box->set_editable(true);
  697. info_message->hide();
  698. Vector<int> ids;
  699. ids = mesh_library->get_item_list();
  700. List<_CGMEItemSort> il;
  701. for (int i = 0; i < ids.size(); i++) {
  702. _CGMEItemSort is;
  703. is.id = ids[i];
  704. is.name = mesh_library->get_item_name(ids[i]);
  705. il.push_back(is);
  706. }
  707. il.sort();
  708. String filter = search_box->get_text().strip_edges();
  709. int item = 0;
  710. for (List<_CGMEItemSort>::Element *E = il.front(); E; E = E->next()) {
  711. int id = E->get().id;
  712. String name = mesh_library->get_item_name(id);
  713. Ref<Texture> preview = mesh_library->get_item_preview(id);
  714. if (name == "") {
  715. name = "#" + itos(id);
  716. }
  717. if (filter != "" && !filter.is_subsequence_ofi(name))
  718. continue;
  719. mesh_library_palette->add_item("");
  720. if (!preview.is_null()) {
  721. mesh_library_palette->set_item_icon(item, preview);
  722. mesh_library_palette->set_item_tooltip(item, name);
  723. }
  724. mesh_library_palette->set_item_text(item, name);
  725. mesh_library_palette->set_item_metadata(item, id);
  726. item++;
  727. }
  728. if (selected != -1 && mesh_library_palette->get_item_count() > 0) {
  729. mesh_library_palette->select(selected);
  730. }
  731. last_mesh_library = mesh_library.operator->();
  732. }
  733. void GridMapEditor::edit(GridMap *p_gridmap) {
  734. if (!p_gridmap && node)
  735. node->disconnect("cell_size_changed", this, "_draw_grids");
  736. node = p_gridmap;
  737. input_action = INPUT_NONE;
  738. selection.active = false;
  739. _update_selection_transform();
  740. _update_paste_indicator();
  741. spatial_editor = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  742. if (!node) {
  743. set_process(false);
  744. for (int i = 0; i < 3; i++) {
  745. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  746. }
  747. if (cursor_instance.is_valid()) {
  748. VisualServer::get_singleton()->instance_set_visible(cursor_instance, false);
  749. }
  750. return;
  751. }
  752. update_palette();
  753. set_process(true);
  754. clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
  755. _draw_grids(node->get_cell_size());
  756. update_grid();
  757. _update_clip();
  758. node->connect("cell_size_changed", this, "_draw_grids");
  759. }
  760. void GridMapEditor::_update_clip() {
  761. node->set_meta("_editor_clip_", clip_mode);
  762. if (clip_mode == CLIP_DISABLED)
  763. node->set_clip(false);
  764. else
  765. node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
  766. }
  767. void GridMapEditor::update_grid() {
  768. grid_xform.origin.x -= 1; // Force update in hackish way.
  769. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  770. edit_grid_xform.origin = grid_ofs;
  771. edit_grid_xform.basis = Basis();
  772. for (int i = 0; i < 3; i++) {
  773. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  774. }
  775. updating = true;
  776. floor->set_value(edit_floor[edit_axis]);
  777. updating = false;
  778. }
  779. void GridMapEditor::_draw_grids(const Vector3 &cell_size) {
  780. Vector3 edited_floor = node->has_meta("_editor_floor_") ? node->get_meta("_editor_floor_") : Variant();
  781. for (int i = 0; i < 3; i++) {
  782. if (VS::get_singleton()->mesh_get_surface_count(grid[i]) > 0)
  783. VS::get_singleton()->mesh_remove_surface(grid[i], 0);
  784. edit_floor[i] = edited_floor[i];
  785. }
  786. Vector<Vector3> grid_points[3];
  787. Vector<Color> grid_colors[3];
  788. for (int i = 0; i < 3; i++) {
  789. Vector3 axis;
  790. axis[i] = 1;
  791. Vector3 axis_n1;
  792. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  793. Vector3 axis_n2;
  794. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  795. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  796. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  797. Vector3 p = axis_n1 * j + axis_n2 * k;
  798. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  799. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  800. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  801. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  802. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  803. grid_points[i].push_back(p);
  804. grid_points[i].push_back(pk);
  805. grid_colors[i].push_back(Color(1, 1, 1, trans));
  806. grid_colors[i].push_back(Color(1, 1, 1, transk));
  807. grid_points[i].push_back(p);
  808. grid_points[i].push_back(pj);
  809. grid_colors[i].push_back(Color(1, 1, 1, trans));
  810. grid_colors[i].push_back(Color(1, 1, 1, transj));
  811. }
  812. }
  813. Array d;
  814. d.resize(VS::ARRAY_MAX);
  815. d[VS::ARRAY_VERTEX] = grid_points[i];
  816. d[VS::ARRAY_COLOR] = grid_colors[i];
  817. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  818. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  819. }
  820. }
  821. void GridMapEditor::_notification(int p_what) {
  822. switch (p_what) {
  823. case NOTIFICATION_ENTER_TREE: {
  824. get_tree()->connect("node_removed", this, "_node_removed");
  825. mesh_library_palette->connect("item_selected", this, "_item_selected_cbk");
  826. for (int i = 0; i < 3; i++) {
  827. grid[i] = VS::get_singleton()->mesh_create();
  828. grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  829. selection_level_instance[i] = VisualServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world()->get_scenario());
  830. }
  831. selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
  832. paste_instance = VisualServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world()->get_scenario());
  833. _update_selection_transform();
  834. _update_paste_indicator();
  835. } break;
  836. case NOTIFICATION_EXIT_TREE: {
  837. get_tree()->disconnect("node_removed", this, "_node_removed");
  838. _clear_clipboard_data();
  839. for (int i = 0; i < 3; i++) {
  840. VS::get_singleton()->free(grid_instance[i]);
  841. VS::get_singleton()->free(grid[i]);
  842. grid_instance[i] = RID();
  843. grid[i] = RID();
  844. VisualServer::get_singleton()->free(selection_level_instance[i]);
  845. }
  846. VisualServer::get_singleton()->free(selection_instance);
  847. VisualServer::get_singleton()->free(paste_instance);
  848. selection_instance = RID();
  849. paste_instance = RID();
  850. } break;
  851. case NOTIFICATION_PROCESS: {
  852. if (!node) {
  853. return;
  854. }
  855. Transform xf = node->get_global_transform();
  856. if (xf != grid_xform) {
  857. for (int i = 0; i < 3; i++) {
  858. VS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  859. }
  860. grid_xform = xf;
  861. }
  862. Ref<MeshLibrary> cgmt = node->get_mesh_library();
  863. if (cgmt.operator->() != last_mesh_library)
  864. update_palette();
  865. if (lock_view) {
  866. EditorNode *editor = Object::cast_to<EditorNode>(get_tree()->get_root()->get_child(0));
  867. Plane p;
  868. p.normal[edit_axis] = 1.0;
  869. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  870. p = node->get_transform().xform(p); // plane to snap
  871. SpatialEditorPlugin *sep = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  872. if (sep)
  873. sep->snap_cursor_to_plane(p);
  874. }
  875. } break;
  876. case NOTIFICATION_THEME_CHANGED: {
  877. options->set_icon(get_icon("GridMap", "EditorIcons"));
  878. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  879. } break;
  880. }
  881. }
  882. void GridMapEditor::_update_cursor_instance() {
  883. if (!node) {
  884. return;
  885. }
  886. if (cursor_instance.is_valid())
  887. VisualServer::get_singleton()->free(cursor_instance);
  888. cursor_instance = RID();
  889. if (selected_palette >= 0) {
  890. if (node && !node->get_mesh_library().is_null()) {
  891. Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette);
  892. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  893. cursor_instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
  894. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  895. }
  896. }
  897. }
  898. }
  899. void GridMapEditor::_item_selected_cbk(int idx) {
  900. selected_palette = mesh_library_palette->get_item_metadata(idx);
  901. _update_cursor_instance();
  902. }
  903. void GridMapEditor::_floor_changed(float p_value) {
  904. if (updating)
  905. return;
  906. edit_floor[edit_axis] = p_value;
  907. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  908. update_grid();
  909. _update_clip();
  910. _update_selection_transform();
  911. }
  912. void GridMapEditor::_floor_mouse_exited() {
  913. floor->get_line_edit()->release_focus();
  914. }
  915. void GridMapEditor::_bind_methods() {
  916. ClassDB::bind_method("_text_changed", &GridMapEditor::_text_changed);
  917. ClassDB::bind_method("_sbox_input", &GridMapEditor::_sbox_input);
  918. ClassDB::bind_method("_icon_size_changed", &GridMapEditor::_icon_size_changed);
  919. ClassDB::bind_method("_menu_option", &GridMapEditor::_menu_option);
  920. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  921. ClassDB::bind_method("_item_selected_cbk", &GridMapEditor::_item_selected_cbk);
  922. ClassDB::bind_method("_floor_changed", &GridMapEditor::_floor_changed);
  923. ClassDB::bind_method("_floor_mouse_exited", &GridMapEditor::_floor_mouse_exited);
  924. ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection);
  925. ClassDB::bind_method("_node_removed", &GridMapEditor::_node_removed);
  926. ClassDB::bind_method(D_METHOD("_set_display_mode", "mode"), &GridMapEditor::_set_display_mode);
  927. ClassDB::bind_method("_draw_grids", &GridMapEditor::_draw_grids);
  928. }
  929. GridMapEditor::GridMapEditor(EditorNode *p_editor) {
  930. input_action = INPUT_NONE;
  931. editor = p_editor;
  932. undo_redo = p_editor->get_undo_redo();
  933. int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230);
  934. Control *ec = memnew(Control);
  935. ec->set_custom_minimum_size(Size2(mw, 0) * EDSCALE);
  936. add_child(ec);
  937. spatial_editor_hb = memnew(HBoxContainer);
  938. spatial_editor_hb->set_h_size_flags(SIZE_EXPAND_FILL);
  939. spatial_editor_hb->set_alignment(BoxContainer::ALIGN_END);
  940. SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  941. spin_box_label = memnew(Label);
  942. spin_box_label->set_text(TTR("Floor:"));
  943. spatial_editor_hb->add_child(spin_box_label);
  944. floor = memnew(SpinBox);
  945. floor->set_min(-32767);
  946. floor->set_max(32767);
  947. floor->set_step(1);
  948. floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
  949. spatial_editor_hb->add_child(floor);
  950. floor->connect("value_changed", this, "_floor_changed");
  951. floor->connect("mouse_exited", this, "_floor_mouse_exited");
  952. floor->get_line_edit()->connect("mouse_exited", this, "_floor_mouse_exited");
  953. spatial_editor_hb->add_child(memnew(VSeparator));
  954. options = memnew(MenuButton);
  955. spatial_editor_hb->add_child(options);
  956. spatial_editor_hb->hide();
  957. options->set_text(TTR("Grid Map"));
  958. options->get_popup()->add_check_item(TTR("Snap View"), MENU_OPTION_LOCK_VIEW);
  959. options->get_popup()->add_separator();
  960. options->get_popup()->add_item(TTR("Previous Floor"), MENU_OPTION_PREV_LEVEL, KEY_Q);
  961. options->get_popup()->add_item(TTR("Next Floor"), MENU_OPTION_NEXT_LEVEL, KEY_E);
  962. options->get_popup()->add_separator();
  963. options->get_popup()->add_radio_check_item(TTR("Clip Disabled"), MENU_OPTION_CLIP_DISABLED);
  964. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true);
  965. options->get_popup()->add_radio_check_item(TTR("Clip Above"), MENU_OPTION_CLIP_ABOVE);
  966. options->get_popup()->add_radio_check_item(TTR("Clip Below"), MENU_OPTION_CLIP_BELOW);
  967. options->get_popup()->add_separator();
  968. options->get_popup()->add_radio_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, KEY_Z);
  969. options->get_popup()->add_radio_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, KEY_X);
  970. options->get_popup()->add_radio_check_item(TTR("Edit Z Axis"), MENU_OPTION_Z_AXIS, KEY_C);
  971. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  972. options->get_popup()->add_separator();
  973. options->get_popup()->add_item(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
  974. options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
  975. options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
  976. options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
  977. options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
  978. options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
  979. options->get_popup()->add_item(TTR("Cursor Clear Rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION, KEY_W);
  980. options->get_popup()->add_separator();
  981. options->get_popup()->add_check_item(TTR("Paste Selects"), MENU_OPTION_PASTE_SELECTS);
  982. options->get_popup()->add_separator();
  983. options->get_popup()->add_item(TTR("Duplicate Selection"), MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_CTRL + KEY_C);
  984. options->get_popup()->add_item(TTR("Cut Selection"), MENU_OPTION_SELECTION_CUT, KEY_MASK_CTRL + KEY_X);
  985. options->get_popup()->add_item(TTR("Clear Selection"), MENU_OPTION_SELECTION_CLEAR, KEY_DELETE);
  986. options->get_popup()->add_item(TTR("Fill Selection"), MENU_OPTION_SELECTION_FILL, KEY_MASK_CTRL + KEY_F);
  987. options->get_popup()->add_separator();
  988. options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
  989. settings_dialog = memnew(ConfirmationDialog);
  990. settings_dialog->set_title(TTR("GridMap Settings"));
  991. add_child(settings_dialog);
  992. settings_vbc = memnew(VBoxContainer);
  993. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  994. settings_dialog->add_child(settings_vbc);
  995. settings_pick_distance = memnew(SpinBox);
  996. settings_pick_distance->set_max(10000.0f);
  997. settings_pick_distance->set_min(500.0f);
  998. settings_pick_distance->set_step(1.0f);
  999. settings_pick_distance->set_value(EDITOR_DEF("editors/grid_map/pick_distance", 5000.0));
  1000. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  1001. clip_mode = CLIP_DISABLED;
  1002. options->get_popup()->connect("id_pressed", this, "_menu_option");
  1003. HBoxContainer *hb = memnew(HBoxContainer);
  1004. add_child(hb);
  1005. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  1006. search_box = memnew(LineEdit);
  1007. search_box->set_h_size_flags(SIZE_EXPAND_FILL);
  1008. search_box->set_placeholder(TTR("Filter meshes"));
  1009. hb->add_child(search_box);
  1010. search_box->connect("text_changed", this, "_text_changed");
  1011. search_box->connect("gui_input", this, "_sbox_input");
  1012. mode_thumbnail = memnew(ToolButton);
  1013. mode_thumbnail->set_toggle_mode(true);
  1014. mode_thumbnail->set_pressed(true);
  1015. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
  1016. hb->add_child(mode_thumbnail);
  1017. mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
  1018. mode_list = memnew(ToolButton);
  1019. mode_list->set_toggle_mode(true);
  1020. mode_list->set_pressed(false);
  1021. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  1022. hb->add_child(mode_list);
  1023. mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
  1024. size_slider = memnew(HSlider);
  1025. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  1026. size_slider->set_min(0.1f);
  1027. size_slider->set_max(4.0f);
  1028. size_slider->set_step(0.1f);
  1029. size_slider->set_value(1.0f);
  1030. size_slider->connect("value_changed", this, "_icon_size_changed");
  1031. add_child(size_slider);
  1032. EDITOR_DEF("editors/grid_map/preview_size", 64);
  1033. display_mode = DISPLAY_THUMBNAIL;
  1034. mesh_library_palette = memnew(ItemList);
  1035. add_child(mesh_library_palette);
  1036. mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL);
  1037. info_message = memnew(Label);
  1038. info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes."));
  1039. info_message->set_valign(Label::VALIGN_CENTER);
  1040. info_message->set_align(Label::ALIGN_CENTER);
  1041. info_message->set_autowrap(true);
  1042. info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  1043. info_message->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  1044. mesh_library_palette->add_child(info_message);
  1045. edit_axis = Vector3::AXIS_Y;
  1046. edit_floor[0] = -1;
  1047. edit_floor[1] = -1;
  1048. edit_floor[2] = -1;
  1049. cursor_visible = false;
  1050. selected_palette = -1;
  1051. lock_view = false;
  1052. cursor_rot = 0;
  1053. selection_mesh = VisualServer::get_singleton()->mesh_create();
  1054. paste_mesh = VisualServer::get_singleton()->mesh_create();
  1055. {
  1056. // Selection mesh create.
  1057. PoolVector<Vector3> lines;
  1058. PoolVector<Vector3> triangles;
  1059. PoolVector<Vector3> square[3];
  1060. for (int i = 0; i < 6; i++) {
  1061. Vector3 face_points[4];
  1062. for (int j = 0; j < 4; j++) {
  1063. float v[3];
  1064. v[0] = 1.0;
  1065. v[1] = 1 - 2 * ((j >> 1) & 1);
  1066. v[2] = v[1] * (1 - 2 * (j & 1));
  1067. for (int k = 0; k < 3; k++) {
  1068. if (i < 3)
  1069. face_points[j][(i + k) % 3] = v[k];
  1070. else
  1071. face_points[3 - j][(i + k) % 3] = -v[k];
  1072. }
  1073. }
  1074. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1075. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1076. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1077. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1078. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1079. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1080. }
  1081. for (int i = 0; i < 12; i++) {
  1082. AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  1083. Vector3 a, b;
  1084. base.get_edge(i, a, b);
  1085. lines.push_back(a);
  1086. lines.push_back(b);
  1087. }
  1088. for (int i = 0; i < 3; i++) {
  1089. Vector3 points[4];
  1090. for (int j = 0; j < 4; j++) {
  1091. static const bool orderx[4] = { 0, 1, 1, 0 };
  1092. static const bool ordery[4] = { 0, 0, 1, 1 };
  1093. Vector3 sp;
  1094. if (orderx[j]) {
  1095. sp[(i + 1) % 3] = 1.0;
  1096. }
  1097. if (ordery[j]) {
  1098. sp[(i + 2) % 3] = 1.0;
  1099. }
  1100. points[j] = sp;
  1101. }
  1102. for (int j = 0; j < 4; j++) {
  1103. Vector3 ofs;
  1104. ofs[i] += 0.01;
  1105. square[i].push_back(points[j] - ofs);
  1106. square[i].push_back(points[(j + 1) % 4] - ofs);
  1107. square[i].push_back(points[j] + ofs);
  1108. square[i].push_back(points[(j + 1) % 4] + ofs);
  1109. }
  1110. }
  1111. Array d;
  1112. d.resize(VS::ARRAY_MAX);
  1113. inner_mat.instance();
  1114. inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.2));
  1115. inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1116. inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1117. d[VS::ARRAY_VERTEX] = triangles;
  1118. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1119. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  1120. outer_mat.instance();
  1121. outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.8));
  1122. outer_mat->set_on_top_of_alpha();
  1123. outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1124. outer_mat->set_line_width(3.0);
  1125. outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1126. selection_floor_mat.instance();
  1127. selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
  1128. selection_floor_mat->set_on_top_of_alpha();
  1129. selection_floor_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1130. selection_floor_mat->set_line_width(3.0);
  1131. d[VS::ARRAY_VERTEX] = lines;
  1132. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
  1133. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  1134. d[VS::ARRAY_VERTEX] = triangles;
  1135. VisualServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1136. VisualServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid());
  1137. d[VS::ARRAY_VERTEX] = lines;
  1138. VisualServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, VS::PRIMITIVE_LINES, d);
  1139. VisualServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid());
  1140. for (int i = 0; i < 3; i++) {
  1141. d[VS::ARRAY_VERTEX] = square[i];
  1142. selection_level_mesh[i] = VS::get_singleton()->mesh_create();
  1143. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], VS::PRIMITIVE_LINES, d);
  1144. VisualServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
  1145. }
  1146. }
  1147. _set_selection(false);
  1148. updating = false;
  1149. accumulated_floor_delta = 0.0;
  1150. indicator_mat.instance();
  1151. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1152. indicator_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1153. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  1154. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1155. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  1156. }
  1157. GridMapEditor::~GridMapEditor() {
  1158. _clear_clipboard_data();
  1159. for (int i = 0; i < 3; i++) {
  1160. if (grid[i].is_valid())
  1161. VisualServer::get_singleton()->free(grid[i]);
  1162. if (grid_instance[i].is_valid())
  1163. VisualServer::get_singleton()->free(grid_instance[i]);
  1164. if (cursor_instance.is_valid())
  1165. VisualServer::get_singleton()->free(cursor_instance);
  1166. if (selection_level_instance[i].is_valid())
  1167. VisualServer::get_singleton()->free(selection_level_instance[i]);
  1168. if (selection_level_mesh[i].is_valid())
  1169. VisualServer::get_singleton()->free(selection_level_mesh[i]);
  1170. }
  1171. VisualServer::get_singleton()->free(selection_mesh);
  1172. if (selection_instance.is_valid())
  1173. VisualServer::get_singleton()->free(selection_instance);
  1174. VisualServer::get_singleton()->free(paste_mesh);
  1175. if (paste_instance.is_valid())
  1176. VisualServer::get_singleton()->free(paste_instance);
  1177. }
  1178. void GridMapEditorPlugin::_notification(int p_what) {
  1179. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  1180. switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
  1181. case 0: { // Left.
  1182. SpatialEditor::get_singleton()->get_palette_split()->move_child(grid_map_editor, 0);
  1183. } break;
  1184. case 1: { // Right.
  1185. SpatialEditor::get_singleton()->get_palette_split()->move_child(grid_map_editor, 1);
  1186. } break;
  1187. }
  1188. }
  1189. }
  1190. void GridMapEditorPlugin::edit(Object *p_object) {
  1191. grid_map_editor->edit(Object::cast_to<GridMap>(p_object));
  1192. }
  1193. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1194. return p_object->is_class("GridMap");
  1195. }
  1196. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1197. if (p_visible) {
  1198. grid_map_editor->show();
  1199. grid_map_editor->spatial_editor_hb->show();
  1200. grid_map_editor->set_process(true);
  1201. } else {
  1202. grid_map_editor->spatial_editor_hb->hide();
  1203. grid_map_editor->hide();
  1204. grid_map_editor->edit(NULL);
  1205. grid_map_editor->set_process(false);
  1206. }
  1207. }
  1208. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  1209. editor = p_node;
  1210. EDITOR_DEF("editors/grid_map/editor_side", 1);
  1211. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/grid_map/editor_side", PROPERTY_HINT_ENUM, "Left,Right"));
  1212. grid_map_editor = memnew(GridMapEditor(editor));
  1213. switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
  1214. case 0: { // Left.
  1215. add_control_to_container(CONTAINER_SPATIAL_EDITOR_SIDE_LEFT, grid_map_editor);
  1216. } break;
  1217. case 1: { // Right.
  1218. add_control_to_container(CONTAINER_SPATIAL_EDITOR_SIDE_RIGHT, grid_map_editor);
  1219. } break;
  1220. }
  1221. grid_map_editor->hide();
  1222. }
  1223. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1224. }