grid_map_editor_plugin.cpp 49 KB

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