grid_map_editor_plugin.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #ifdef TOOLS_ENABLED
  32. #include "core/input/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_command_palette.h"
  35. #include "editor/editor_main_screen.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_settings.h"
  38. #include "editor/editor_string_names.h"
  39. #include "editor/editor_undo_redo_manager.h"
  40. #include "editor/gui/editor_bottom_panel.h"
  41. #include "editor/gui/editor_zoom_widget.h"
  42. #include "editor/plugins/node_3d_editor_plugin.h"
  43. #include "editor/themes/editor_scale.h"
  44. #include "scene/3d/camera_3d.h"
  45. #include "scene/gui/dialogs.h"
  46. #include "scene/gui/label.h"
  47. #include "scene/gui/menu_button.h"
  48. #include "scene/gui/separator.h"
  49. #include "scene/main/window.h"
  50. void GridMapEditor::_configure() {
  51. if (!node) {
  52. return;
  53. }
  54. update_grid();
  55. }
  56. void GridMapEditor::_menu_option(int p_option) {
  57. switch (p_option) {
  58. case MENU_OPTION_PREV_LEVEL: {
  59. floor->set_value(floor->get_value() - 1);
  60. if (selection.active && input_action == INPUT_SELECT) {
  61. selection.current[edit_axis]--;
  62. _validate_selection();
  63. }
  64. } break;
  65. case MENU_OPTION_NEXT_LEVEL: {
  66. floor->set_value(floor->get_value() + 1);
  67. if (selection.active && input_action == INPUT_SELECT) {
  68. selection.current[edit_axis]++;
  69. _validate_selection();
  70. }
  71. } break;
  72. case MENU_OPTION_X_AXIS:
  73. case MENU_OPTION_Y_AXIS:
  74. case MENU_OPTION_Z_AXIS: {
  75. int new_axis = p_option - MENU_OPTION_X_AXIS;
  76. for (int i = 0; i < 3; i++) {
  77. int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i);
  78. options->get_popup()->set_item_checked(idx, i == new_axis);
  79. }
  80. if (edit_axis != new_axis) {
  81. if (edit_axis == Vector3::AXIS_Y) {
  82. floor->set_tooltip_text("Change Grid Plane");
  83. } else if (new_axis == Vector3::AXIS_Y) {
  84. floor->set_tooltip_text("Change Grid Floor");
  85. }
  86. }
  87. edit_axis = Vector3::Axis(new_axis);
  88. update_grid();
  89. } break;
  90. case MENU_OPTION_CURSOR_ROTATE_Y: {
  91. Basis r;
  92. if (input_action == INPUT_PASTE) {
  93. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  94. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  95. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  96. _update_paste_indicator();
  97. break;
  98. }
  99. r = node->get_basis_with_orthogonal_index(cursor_rot);
  100. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  101. cursor_rot = node->get_orthogonal_index_from_basis(r);
  102. _update_cursor_transform();
  103. } break;
  104. case MENU_OPTION_CURSOR_ROTATE_X: {
  105. Basis r;
  106. if (input_action == INPUT_PASTE) {
  107. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  108. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  109. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  110. _update_paste_indicator();
  111. break;
  112. }
  113. r = node->get_basis_with_orthogonal_index(cursor_rot);
  114. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  115. cursor_rot = node->get_orthogonal_index_from_basis(r);
  116. _update_cursor_transform();
  117. } break;
  118. case MENU_OPTION_CURSOR_ROTATE_Z: {
  119. Basis r;
  120. if (input_action == INPUT_PASTE) {
  121. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  122. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  123. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  124. _update_paste_indicator();
  125. break;
  126. }
  127. r = node->get_basis_with_orthogonal_index(cursor_rot);
  128. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  129. cursor_rot = node->get_orthogonal_index_from_basis(r);
  130. _update_cursor_transform();
  131. } break;
  132. case MENU_OPTION_CURSOR_BACK_ROTATE_Y: {
  133. Basis r;
  134. if (input_action == INPUT_PASTE) {
  135. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  136. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  137. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  138. _update_paste_indicator();
  139. break;
  140. }
  141. r = node->get_basis_with_orthogonal_index(cursor_rot);
  142. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  143. cursor_rot = node->get_orthogonal_index_from_basis(r);
  144. _update_cursor_transform();
  145. } break;
  146. case MENU_OPTION_CURSOR_BACK_ROTATE_X: {
  147. Basis r;
  148. if (input_action == INPUT_PASTE) {
  149. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  150. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  151. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  152. _update_paste_indicator();
  153. break;
  154. }
  155. r = node->get_basis_with_orthogonal_index(cursor_rot);
  156. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  157. cursor_rot = node->get_orthogonal_index_from_basis(r);
  158. _update_cursor_transform();
  159. } break;
  160. case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
  161. Basis r;
  162. if (input_action == INPUT_PASTE) {
  163. r = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  164. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  165. paste_indicator.orientation = node->get_orthogonal_index_from_basis(r);
  166. _update_paste_indicator();
  167. break;
  168. }
  169. r = node->get_basis_with_orthogonal_index(cursor_rot);
  170. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  171. cursor_rot = node->get_orthogonal_index_from_basis(r);
  172. _update_cursor_transform();
  173. } break;
  174. case MENU_OPTION_CURSOR_CLEAR_ROTATION: {
  175. if (input_action == INPUT_PASTE) {
  176. paste_indicator.orientation = 0;
  177. _update_paste_indicator();
  178. break;
  179. }
  180. cursor_rot = 0;
  181. _update_cursor_transform();
  182. } break;
  183. case MENU_OPTION_PASTE_SELECTS: {
  184. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  185. options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
  186. } break;
  187. case MENU_OPTION_SELECTION_DUPLICATE:
  188. case MENU_OPTION_SELECTION_CUT: {
  189. if (!(selection.active && input_action == INPUT_NONE)) {
  190. break;
  191. }
  192. _set_clipboard_data();
  193. if (p_option == MENU_OPTION_SELECTION_CUT) {
  194. _delete_selection();
  195. }
  196. input_action = INPUT_PASTE;
  197. paste_indicator.click = selection.begin;
  198. paste_indicator.current = selection.begin;
  199. paste_indicator.begin = selection.begin;
  200. paste_indicator.end = selection.end;
  201. paste_indicator.orientation = 0;
  202. _update_paste_indicator();
  203. } break;
  204. case MENU_OPTION_SELECTION_CLEAR: {
  205. if (!selection.active) {
  206. break;
  207. }
  208. _delete_selection();
  209. } break;
  210. case MENU_OPTION_SELECTION_FILL: {
  211. if (!selection.active) {
  212. return;
  213. }
  214. _fill_selection();
  215. } break;
  216. case MENU_OPTION_GRIDMAP_SETTINGS: {
  217. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE);
  218. } break;
  219. }
  220. }
  221. void GridMapEditor::_update_cursor_transform() {
  222. cursor_transform = Transform3D();
  223. cursor_transform.origin = cursor_origin;
  224. cursor_transform.basis *= node->get_cell_scale();
  225. cursor_transform = node->get_global_transform() * cursor_transform;
  226. if (mode_buttons_group->get_pressed_button() == paint_mode_button) {
  227. // Rotation is only applied in paint mode, we don't want the cursor box to rotate otherwise.
  228. cursor_transform.basis = node->get_basis_with_orthogonal_index(cursor_rot);
  229. if (selected_palette >= 0 && node && node->get_mesh_library().is_valid()) {
  230. cursor_transform *= node->get_mesh_library()->get_item_mesh_transform(selected_palette);
  231. }
  232. } else {
  233. Transform3D xf;
  234. xf.scale(node->get_cell_size());
  235. xf.origin.x = node->get_center_x() ? -node->get_cell_size().x / 2 : 0;
  236. xf.origin.y = node->get_center_y() ? -node->get_cell_size().y / 2 : 0;
  237. xf.origin.z = node->get_center_z() ? -node->get_cell_size().z / 2 : 0;
  238. cursor_transform *= xf;
  239. }
  240. if (cursor_instance.is_valid()) {
  241. RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  242. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
  243. }
  244. }
  245. void GridMapEditor::_update_selection_transform() {
  246. Transform3D xf_zero;
  247. xf_zero.basis.set_zero();
  248. if (!selection.active) {
  249. RenderingServer::get_singleton()->instance_set_transform(selection_instance, xf_zero);
  250. for (int i = 0; i < 3; i++) {
  251. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  252. }
  253. return;
  254. }
  255. Transform3D xf;
  256. xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  257. xf.origin = selection.begin * node->get_cell_size();
  258. RenderingServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  259. for (int i = 0; i < 3; i++) {
  260. if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) {
  261. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  262. } else {
  263. Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
  264. scale[edit_axis] = 1.0;
  265. Vector3 position = selection.begin;
  266. position[edit_axis] = edit_floor[edit_axis];
  267. scale *= node->get_cell_size();
  268. position *= node->get_cell_size();
  269. Transform3D xf2;
  270. xf2.basis.scale(scale);
  271. xf2.origin = position;
  272. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], node->get_global_transform() * xf2);
  273. }
  274. }
  275. }
  276. void GridMapEditor::_validate_selection() {
  277. if (!selection.active) {
  278. return;
  279. }
  280. selection.begin = selection.click;
  281. selection.end = selection.current;
  282. if (selection.begin.x > selection.end.x) {
  283. SWAP(selection.begin.x, selection.end.x);
  284. }
  285. if (selection.begin.y > selection.end.y) {
  286. SWAP(selection.begin.y, selection.end.y);
  287. }
  288. if (selection.begin.z > selection.end.z) {
  289. SWAP(selection.begin.z, selection.end.z);
  290. }
  291. _update_selection_transform();
  292. }
  293. void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) {
  294. selection.active = p_active;
  295. selection.begin = p_begin;
  296. selection.end = p_end;
  297. selection.click = p_begin;
  298. selection.current = p_end;
  299. if (is_visible_in_tree()) {
  300. _update_selection_transform();
  301. }
  302. }
  303. bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click) {
  304. if (!spatial_editor) {
  305. return false;
  306. }
  307. if (input_action == INPUT_TRANSFORM) {
  308. return false;
  309. }
  310. if (selected_palette < 0 && input_action != INPUT_NONE && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE) {
  311. return false;
  312. }
  313. if (mesh_library.is_null()) {
  314. return false;
  315. }
  316. if (input_action != INPUT_NONE && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette)) {
  317. return false;
  318. }
  319. Camera3D *camera = p_camera;
  320. Vector3 from = camera->project_ray_origin(p_point);
  321. Vector3 normal = camera->project_ray_normal(p_point);
  322. Transform3D local_xform = node->get_global_transform().affine_inverse();
  323. Vector<Plane> planes = camera->get_frustum();
  324. from = local_xform.xform(from);
  325. normal = local_xform.basis.xform(normal).normalized();
  326. Plane p;
  327. p.normal[edit_axis] = 1.0;
  328. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  329. Vector3 inters;
  330. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters)) {
  331. return false;
  332. }
  333. // Make sure the intersection is inside the frustum planes, to avoid
  334. // Painting on invisible regions.
  335. for (int i = 0; i < planes.size(); i++) {
  336. Plane fp = local_xform.xform(planes[i]);
  337. if (fp.is_point_over(inters)) {
  338. return false;
  339. }
  340. }
  341. int cell[3];
  342. Vector3 cell_size = node->get_cell_size();
  343. for (int i = 0; i < 3; i++) {
  344. if (i == edit_axis) {
  345. cell[i] = edit_floor[i];
  346. } else {
  347. cell[i] = inters[i] / cell_size[i];
  348. if (inters[i] < 0) {
  349. cell[i] -= 1; // Compensate negative.
  350. }
  351. grid_ofs[i] = cell[i] * cell_size[i];
  352. }
  353. }
  354. RS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform);
  355. if (cursor_instance.is_valid()) {
  356. 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();
  357. cursor_visible = true;
  358. if (input_action == INPUT_PASTE) {
  359. cursor_visible = false;
  360. }
  361. _update_cursor_transform();
  362. }
  363. if (input_action == INPUT_NONE) {
  364. return false;
  365. }
  366. if (input_action == INPUT_PASTE) {
  367. paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]);
  368. _update_paste_indicator();
  369. } else if (input_action == INPUT_SELECT) {
  370. selection.current = Vector3i(cell[0], cell[1], cell[2]);
  371. if (p_click) {
  372. selection.click = selection.current;
  373. }
  374. selection.active = true;
  375. _validate_selection();
  376. return true;
  377. } else if (input_action == INPUT_PICK) {
  378. int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  379. if (item >= 0) {
  380. selected_palette = item;
  381. // Clear the filter if picked an item that's filtered out.
  382. int index = mesh_library_palette->find_metadata(item);
  383. if (index == -1) {
  384. search_box->clear();
  385. }
  386. // This will select `selected_palette` in the ItemList when possible.
  387. update_palette();
  388. _update_cursor_instance();
  389. }
  390. return true;
  391. }
  392. if (input_action == INPUT_PAINT) {
  393. SetItem si;
  394. si.position = Vector3i(cell[0], cell[1], cell[2]);
  395. si.new_value = selected_palette;
  396. si.new_orientation = cursor_rot;
  397. si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  398. si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
  399. set_items.push_back(si);
  400. node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot);
  401. return true;
  402. } else if (input_action == INPUT_ERASE) {
  403. SetItem si;
  404. si.position = Vector3i(cell[0], cell[1], cell[2]);
  405. si.new_value = -1;
  406. si.new_orientation = 0;
  407. si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  408. si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
  409. set_items.push_back(si);
  410. node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1);
  411. return true;
  412. }
  413. return false;
  414. }
  415. void GridMapEditor::_delete_selection() {
  416. if (!selection.active) {
  417. return;
  418. }
  419. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  420. undo_redo->create_action(TTR("GridMap Delete Selection"));
  421. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  422. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  423. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  424. Vector3i selected = Vector3i(i, j, k);
  425. undo_redo->add_do_method(node, "set_cell_item", selected, GridMap::INVALID_CELL_ITEM);
  426. undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
  427. }
  428. }
  429. }
  430. undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
  431. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  432. undo_redo->commit_action();
  433. }
  434. void GridMapEditor::_fill_selection() {
  435. if (!selection.active) {
  436. return;
  437. }
  438. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  439. undo_redo->create_action(TTR("GridMap Fill Selection"));
  440. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  441. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  442. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  443. Vector3i selected = Vector3i(i, j, k);
  444. undo_redo->add_do_method(node, "set_cell_item", selected, selected_palette, cursor_rot);
  445. undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
  446. }
  447. }
  448. }
  449. undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
  450. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  451. undo_redo->commit_action();
  452. }
  453. void GridMapEditor::_clear_clipboard_data() {
  454. for (const ClipboardItem &E : clipboard_items) {
  455. RenderingServer::get_singleton()->free(E.instance);
  456. }
  457. clipboard_items.clear();
  458. }
  459. void GridMapEditor::_set_clipboard_data() {
  460. _clear_clipboard_data();
  461. Ref<MeshLibrary> meshLibrary = node->get_mesh_library();
  462. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  463. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  464. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  465. Vector3i selected = Vector3i(i, j, k);
  466. int itm = node->get_cell_item(selected);
  467. if (itm == GridMap::INVALID_CELL_ITEM) {
  468. continue;
  469. }
  470. Ref<Mesh> mesh = meshLibrary->get_item_mesh(itm);
  471. ClipboardItem item;
  472. item.cell_item = itm;
  473. item.grid_offset = Vector3(selected) - selection.begin;
  474. item.orientation = node->get_cell_item_orientation(selected);
  475. item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());
  476. clipboard_items.push_back(item);
  477. }
  478. }
  479. }
  480. }
  481. void GridMapEditor::_update_paste_indicator() {
  482. if (input_action != INPUT_PASTE) {
  483. Transform3D xf;
  484. xf.basis.set_zero();
  485. RenderingServer::get_singleton()->instance_set_transform(paste_instance, xf);
  486. return;
  487. }
  488. Vector3 center = 0.5 * Vector3(real_t(node->get_center_x()), real_t(node->get_center_y()), real_t(node->get_center_z()));
  489. Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size();
  490. Transform3D xf;
  491. xf.scale(scale);
  492. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  493. Basis rot;
  494. rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  495. xf.basis = rot * xf.basis;
  496. xf.translate_local((-center * node->get_cell_size()) / scale);
  497. RenderingServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf);
  498. for (const ClipboardItem &item : clipboard_items) {
  499. xf = Transform3D();
  500. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  501. xf.basis = rot * xf.basis;
  502. xf.translate_local(item.grid_offset * node->get_cell_size());
  503. Basis item_rot;
  504. item_rot = node->get_basis_with_orthogonal_index(item.orientation);
  505. xf.basis = item_rot * xf.basis * node->get_cell_scale();
  506. RenderingServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf);
  507. }
  508. }
  509. void GridMapEditor::_do_paste() {
  510. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  511. bool reselect = options->get_popup()->is_item_checked(idx);
  512. Basis rot;
  513. rot = node->get_basis_with_orthogonal_index(paste_indicator.orientation);
  514. Vector3 ofs = paste_indicator.current - paste_indicator.click;
  515. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  516. undo_redo->create_action(TTR("GridMap Paste Selection"));
  517. for (const ClipboardItem &item : clipboard_items) {
  518. Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;
  519. Basis orm;
  520. orm = node->get_basis_with_orthogonal_index(item.orientation);
  521. orm = rot * orm;
  522. undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, node->get_orthogonal_index_from_basis(orm));
  523. undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position));
  524. }
  525. if (reselect) {
  526. // We need to rotate the paste_indicator to find the selection begin and end:
  527. Vector3 temp_end = rot.xform(paste_indicator.end - paste_indicator.begin) + paste_indicator.begin + ofs;
  528. Vector3 temp_begin = paste_indicator.begin + ofs;
  529. // _set_selection expects that selection_begin is the corner closer to the origin:
  530. for (int i = 0; i < 3; ++i) {
  531. if (temp_begin[i] > temp_end[i]) {
  532. float p = temp_begin[i];
  533. temp_begin[i] = temp_end[i];
  534. temp_end[i] = p;
  535. }
  536. }
  537. undo_redo->add_do_method(this, "_set_selection", true, temp_begin, temp_end);
  538. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  539. }
  540. undo_redo->commit_action();
  541. _clear_clipboard_data();
  542. }
  543. void GridMapEditor::_show_viewports_transform_gizmo(bool p_value) {
  544. Dictionary new_state;
  545. new_state["transform_gizmo"] = p_value;
  546. for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
  547. Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(i);
  548. viewport->set_state(new_state);
  549. }
  550. }
  551. EditorPlugin::AfterGUIInput GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  552. if (!node) {
  553. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  554. }
  555. Ref<InputEventKey> k = p_event;
  556. if (k.is_valid() && k->is_pressed() && !k->is_echo()) {
  557. // Transform mode (toggle button):
  558. // If we are in Transform mode we pass the events to the 3D editor,
  559. // but if the Transform mode shortcut is pressed again, we go back to Selection mode.
  560. if (mode_buttons_group->get_pressed_button() == transform_mode_button) {
  561. if (transform_mode_button->get_shortcut().is_valid() && transform_mode_button->get_shortcut()->matches_event(p_event)) {
  562. select_mode_button->set_pressed(true);
  563. accept_event();
  564. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  565. }
  566. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  567. }
  568. // Tool modes and tool actions:
  569. for (BaseButton *b : viewport_shortcut_buttons) {
  570. if (b->is_disabled()) {
  571. continue;
  572. }
  573. if (b->get_shortcut().is_valid() && b->get_shortcut()->matches_event(p_event)) {
  574. if (b->is_toggle_mode()) {
  575. b->set_pressed(b->get_button_group().is_valid() || !b->is_pressed());
  576. } else {
  577. // Can't press a button without toggle mode, so just emit the signal directly.
  578. b->emit_signal(SceneStringName(pressed));
  579. }
  580. accept_event();
  581. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  582. }
  583. }
  584. // Hard key actions:
  585. if (k->get_keycode() == Key::ESCAPE) {
  586. if (input_action == INPUT_PASTE) {
  587. _clear_clipboard_data();
  588. input_action = INPUT_NONE;
  589. _update_paste_indicator();
  590. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  591. } else if (selection.active) {
  592. _set_selection(false);
  593. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  594. } else {
  595. input_action = INPUT_NONE;
  596. update_palette();
  597. _update_cursor_instance();
  598. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  599. }
  600. }
  601. // Options menu shortcuts:
  602. Ref<Shortcut> ed_shortcut = ED_GET_SHORTCUT("grid_map/previous_floor");
  603. if (ed_shortcut.is_valid() && ed_shortcut->matches_event(p_event)) {
  604. accept_event();
  605. _menu_option(MENU_OPTION_PREV_LEVEL);
  606. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  607. }
  608. ed_shortcut = ED_GET_SHORTCUT("grid_map/next_floor");
  609. if (ed_shortcut.is_valid() && ed_shortcut->matches_event(p_event)) {
  610. accept_event();
  611. _menu_option(MENU_OPTION_NEXT_LEVEL);
  612. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  613. }
  614. for (int i = 0; i < options->get_popup()->get_item_count(); ++i) {
  615. const Ref<Shortcut> &shortcut = options->get_popup()->get_item_shortcut(i);
  616. if (shortcut.is_valid() && shortcut->matches_event(p_event)) {
  617. // Consume input to avoid conflicts with other plugins.
  618. accept_event();
  619. _menu_option(options->get_popup()->get_item_id(i));
  620. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  621. }
  622. }
  623. }
  624. Ref<InputEventMouseButton> mb = p_event;
  625. if (mb.is_valid()) {
  626. if (mb->get_button_index() == MouseButton::WHEEL_UP && (mb->is_command_or_control_pressed())) {
  627. if (mb->is_pressed()) {
  628. floor->set_value(floor->get_value() + mb->get_factor());
  629. }
  630. return EditorPlugin::AFTER_GUI_INPUT_STOP; // Eaten.
  631. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && (mb->is_command_or_control_pressed())) {
  632. if (mb->is_pressed()) {
  633. floor->set_value(floor->get_value() - mb->get_factor());
  634. }
  635. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  636. }
  637. if (mb->is_pressed()) {
  638. Node3DEditorViewport::NavigationScheme nav_scheme = (Node3DEditorViewport::NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme").operator int();
  639. if ((nav_scheme == Node3DEditorViewport::NAVIGATION_MAYA || nav_scheme == Node3DEditorViewport::NAVIGATION_MODO) && mb->is_alt_pressed()) {
  640. input_action = INPUT_NONE;
  641. } else if (mb->get_button_index() == MouseButton::LEFT) {
  642. bool can_edit = (node && node->get_mesh_library().is_valid());
  643. if (input_action == INPUT_PASTE) {
  644. _do_paste();
  645. input_action = INPUT_NONE;
  646. _update_paste_indicator();
  647. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  648. } else if (mode_buttons_group->get_pressed_button() == select_mode_button && can_edit) {
  649. input_action = INPUT_SELECT;
  650. last_selection = selection;
  651. } else if (mode_buttons_group->get_pressed_button() == pick_mode_button && can_edit) {
  652. input_action = INPUT_PICK;
  653. } else if (mode_buttons_group->get_pressed_button() == paint_mode_button && can_edit) {
  654. input_action = INPUT_PAINT;
  655. set_items.clear();
  656. } else if (mode_buttons_group->get_pressed_button() == erase_mode_button && can_edit) {
  657. input_action = INPUT_ERASE;
  658. set_items.clear();
  659. }
  660. } else if (mb->get_button_index() == MouseButton::RIGHT) {
  661. if (input_action == INPUT_PASTE) {
  662. _clear_clipboard_data();
  663. input_action = INPUT_NONE;
  664. _update_paste_indicator();
  665. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  666. } else if (selection.active) {
  667. _set_selection(false);
  668. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  669. }
  670. } else {
  671. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  672. }
  673. if (do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true)) {
  674. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  675. }
  676. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  677. } else {
  678. if ((mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_ERASE) || (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_PAINT)) {
  679. if (set_items.size()) {
  680. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  681. undo_redo->create_action(TTR("GridMap Paint"));
  682. for (const SetItem &si : set_items) {
  683. undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation);
  684. }
  685. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  686. const SetItem &si = E->get();
  687. undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation);
  688. }
  689. undo_redo->commit_action();
  690. }
  691. set_items.clear();
  692. input_action = INPUT_NONE;
  693. if (set_items.size() > 0) {
  694. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  695. }
  696. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  697. }
  698. if (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_SELECT) {
  699. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  700. undo_redo->create_action(TTR("GridMap Selection"));
  701. undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  702. undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end);
  703. undo_redo->commit_action();
  704. }
  705. if (mb->get_button_index() == MouseButton::LEFT && input_action != INPUT_NONE) {
  706. set_items.clear();
  707. input_action = INPUT_NONE;
  708. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  709. }
  710. if (mb->get_button_index() == MouseButton::RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) {
  711. input_action = INPUT_NONE;
  712. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  713. }
  714. }
  715. }
  716. Ref<InputEventMouseMotion> mm = p_event;
  717. if (mm.is_valid()) {
  718. // Update the grid, to check if the grid needs to be moved to a tile cursor.
  719. update_grid();
  720. if (do_input_action(p_camera, mm->get_position(), false)) {
  721. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  722. }
  723. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  724. }
  725. Ref<InputEventPanGesture> pan_gesture = p_event;
  726. if (pan_gesture.is_valid()) {
  727. if (pan_gesture->is_alt_pressed() && pan_gesture->is_command_or_control_pressed()) {
  728. const real_t delta = pan_gesture->get_delta().y * 0.5;
  729. accumulated_floor_delta += delta;
  730. int step = 0;
  731. if (ABS(accumulated_floor_delta) > 1.0) {
  732. step = SIGN(accumulated_floor_delta);
  733. accumulated_floor_delta -= step;
  734. }
  735. if (step) {
  736. floor->set_value(floor->get_value() + step);
  737. }
  738. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  739. }
  740. }
  741. accumulated_floor_delta = 0.0;
  742. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  743. }
  744. struct _CGMEItemSort {
  745. String name;
  746. int id = 0;
  747. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  748. };
  749. void GridMapEditor::_set_display_mode(int p_mode) {
  750. if (display_mode == p_mode) {
  751. return;
  752. }
  753. if (p_mode == DISPLAY_LIST) {
  754. mode_list->set_pressed(true);
  755. mode_thumbnail->set_pressed(false);
  756. } else if (p_mode == DISPLAY_THUMBNAIL) {
  757. mode_list->set_pressed(false);
  758. mode_thumbnail->set_pressed(true);
  759. }
  760. display_mode = p_mode;
  761. update_palette();
  762. }
  763. void GridMapEditor::_text_changed(const String &p_text) {
  764. update_palette();
  765. }
  766. void GridMapEditor::_sbox_input(const Ref<InputEvent> &p_event) {
  767. // Redirect navigational key events to the item list.
  768. Ref<InputEventKey> key = p_event;
  769. if (key.is_valid()) {
  770. if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
  771. mesh_library_palette->gui_input(key);
  772. search_box->accept_event();
  773. }
  774. }
  775. }
  776. void GridMapEditor::_mesh_library_palette_input(const Ref<InputEvent> &p_ie) {
  777. const Ref<InputEventMouseButton> mb = p_ie;
  778. // Zoom in/out using Ctrl + mouse wheel
  779. if (mb.is_valid() && mb->is_pressed() && mb->is_command_or_control_pressed()) {
  780. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) {
  781. zoom_widget->set_zoom(zoom_widget->get_zoom() + 0.2);
  782. zoom_widget->emit_signal(SNAME("zoom_changed"), zoom_widget->get_zoom());
  783. }
  784. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  785. zoom_widget->set_zoom(zoom_widget->get_zoom() - 0.2);
  786. zoom_widget->emit_signal(SNAME("zoom_changed"), zoom_widget->get_zoom());
  787. }
  788. }
  789. }
  790. void GridMapEditor::_icon_size_changed(float p_value) {
  791. mesh_library_palette->set_icon_scale(p_value);
  792. update_palette();
  793. }
  794. void GridMapEditor::update_palette() {
  795. float min_size = EDITOR_GET("editors/grid_map/preview_size");
  796. min_size *= EDSCALE;
  797. mesh_library_palette->clear();
  798. if (display_mode == DISPLAY_THUMBNAIL) {
  799. mesh_library_palette->set_max_columns(0);
  800. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  801. mesh_library_palette->set_fixed_column_width(min_size * MAX(zoom_widget->get_zoom(), 1.5));
  802. } else if (display_mode == DISPLAY_LIST) {
  803. mesh_library_palette->set_max_columns(0);
  804. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT);
  805. mesh_library_palette->set_fixed_column_width(0);
  806. }
  807. mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size));
  808. mesh_library_palette->set_max_text_lines(2);
  809. if (mesh_library.is_null()) {
  810. search_box->set_text("");
  811. search_box->set_editable(false);
  812. info_message->show();
  813. return;
  814. }
  815. search_box->set_editable(true);
  816. info_message->hide();
  817. Vector<int> ids;
  818. ids = mesh_library->get_item_list();
  819. List<_CGMEItemSort> il;
  820. for (int i = 0; i < ids.size(); i++) {
  821. _CGMEItemSort is;
  822. is.id = ids[i];
  823. is.name = mesh_library->get_item_name(ids[i]);
  824. il.push_back(is);
  825. }
  826. il.sort();
  827. String filter = search_box->get_text().strip_edges();
  828. int item = 0;
  829. for (_CGMEItemSort &E : il) {
  830. int id = E.id;
  831. String name = mesh_library->get_item_name(id);
  832. Ref<Texture2D> preview = mesh_library->get_item_preview(id);
  833. if (name.is_empty()) {
  834. name = "#" + itos(id);
  835. }
  836. if (!filter.is_empty() && !filter.is_subsequence_ofn(name)) {
  837. continue;
  838. }
  839. mesh_library_palette->add_item("");
  840. if (!preview.is_null()) {
  841. mesh_library_palette->set_item_icon(item, preview);
  842. mesh_library_palette->set_item_tooltip(item, name);
  843. }
  844. mesh_library_palette->set_item_text(item, name);
  845. mesh_library_palette->set_item_metadata(item, id);
  846. if (selected_palette == id) {
  847. mesh_library_palette->select(item);
  848. }
  849. item++;
  850. }
  851. }
  852. void GridMapEditor::_update_mesh_library() {
  853. ERR_FAIL_NULL(node);
  854. Ref<MeshLibrary> new_mesh_library = node->get_mesh_library();
  855. if (new_mesh_library != mesh_library) {
  856. if (mesh_library.is_valid()) {
  857. mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette));
  858. }
  859. mesh_library = new_mesh_library;
  860. } else {
  861. return;
  862. }
  863. if (mesh_library.is_valid()) {
  864. mesh_library->connect_changed(callable_mp(this, &GridMapEditor::update_palette));
  865. }
  866. update_palette();
  867. // Make sure we select the first tile as default possible.
  868. if (mesh_library_palette->get_current() == -1 && mesh_library_palette->get_item_count() > 0) {
  869. mesh_library_palette->set_current(0);
  870. selected_palette = mesh_library_palette->get_item_metadata(0);
  871. }
  872. // Update the cursor and grid in case the library is changed or removed.
  873. _update_cursor_instance();
  874. update_grid();
  875. }
  876. void GridMapEditor::edit(GridMap *p_gridmap) {
  877. if (node) {
  878. node->disconnect(SNAME("cell_size_changed"), callable_mp(this, &GridMapEditor::_draw_grids));
  879. node->disconnect(CoreStringName(changed), callable_mp(this, &GridMapEditor::_update_mesh_library));
  880. if (mesh_library.is_valid()) {
  881. mesh_library->disconnect_changed(callable_mp(this, &GridMapEditor::update_palette));
  882. mesh_library = Ref<MeshLibrary>();
  883. }
  884. }
  885. node = p_gridmap;
  886. input_action = INPUT_NONE;
  887. selection.active = false;
  888. _update_selection_transform();
  889. _update_paste_indicator();
  890. spatial_editor = Object::cast_to<Node3DEditorPlugin>(EditorNode::get_singleton()->get_editor_main_screen()->get_selected_plugin());
  891. if (!node) {
  892. set_process(false);
  893. for (int i = 0; i < 3; i++) {
  894. RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  895. }
  896. if (cursor_instance.is_valid()) {
  897. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false);
  898. }
  899. return;
  900. }
  901. update_palette();
  902. _update_cursor_instance();
  903. set_process(true);
  904. _draw_grids(node->get_cell_size());
  905. update_grid();
  906. node->connect(SNAME("cell_size_changed"), callable_mp(this, &GridMapEditor::_draw_grids));
  907. node->connect(CoreStringName(changed), callable_mp(this, &GridMapEditor::_update_mesh_library));
  908. _update_mesh_library();
  909. }
  910. void GridMapEditor::update_grid() {
  911. grid_xform.origin.x -= 1; // Force update in hackish way.
  912. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  913. // If there's a valid tile cursor, offset the grid, otherwise move it back to the node.
  914. edit_grid_xform.origin = cursor_instance.is_valid() ? grid_ofs : Vector3();
  915. edit_grid_xform.basis = Basis();
  916. for (int i = 0; i < 3; i++) {
  917. RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  918. }
  919. updating = true;
  920. floor->set_value(edit_floor[edit_axis]);
  921. updating = false;
  922. }
  923. void GridMapEditor::_draw_grids(const Vector3 &cell_size) {
  924. Vector3 edited_floor = node->get_meta("_editor_floor_", Vector3());
  925. for (int i = 0; i < 3; i++) {
  926. RS::get_singleton()->mesh_clear(grid[i]);
  927. edit_floor[i] = edited_floor[i];
  928. }
  929. Vector<Vector3> grid_points[3];
  930. Vector<Color> grid_colors[3];
  931. for (int i = 0; i < 3; i++) {
  932. Vector3 axis;
  933. axis[i] = 1;
  934. Vector3 axis_n1;
  935. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  936. Vector3 axis_n2;
  937. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  938. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  939. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  940. Vector3 p = axis_n1 * j + axis_n2 * k;
  941. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  942. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  943. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  944. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  945. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  946. grid_points[i].push_back(p);
  947. grid_points[i].push_back(pk);
  948. grid_colors[i].push_back(Color(1, 1, 1, trans));
  949. grid_colors[i].push_back(Color(1, 1, 1, transk));
  950. grid_points[i].push_back(p);
  951. grid_points[i].push_back(pj);
  952. grid_colors[i].push_back(Color(1, 1, 1, trans));
  953. grid_colors[i].push_back(Color(1, 1, 1, transj));
  954. }
  955. }
  956. Array d;
  957. d.resize(RS::ARRAY_MAX);
  958. d[RS::ARRAY_VERTEX] = grid_points[i];
  959. d[RS::ARRAY_COLOR] = grid_colors[i];
  960. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], RenderingServer::PRIMITIVE_LINES, d);
  961. RenderingServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  962. }
  963. }
  964. void GridMapEditor::_update_theme() {
  965. transform_mode_button->set_button_icon(get_theme_icon(SNAME("ToolMove"), EditorStringName(EditorIcons)));
  966. select_mode_button->set_button_icon(get_theme_icon(SNAME("ToolSelect"), EditorStringName(EditorIcons)));
  967. erase_mode_button->set_button_icon(get_theme_icon(SNAME("Eraser"), EditorStringName(EditorIcons)));
  968. paint_mode_button->set_button_icon(get_theme_icon(SNAME("Paint"), EditorStringName(EditorIcons)));
  969. pick_mode_button->set_button_icon(get_theme_icon(SNAME("ColorPick"), EditorStringName(EditorIcons)));
  970. fill_action_button->set_button_icon(get_theme_icon(SNAME("Bucket"), EditorStringName(EditorIcons)));
  971. move_action_button->set_button_icon(get_theme_icon(SNAME("ActionCut"), EditorStringName(EditorIcons)));
  972. duplicate_action_button->set_button_icon(get_theme_icon(SNAME("ActionCopy"), EditorStringName(EditorIcons)));
  973. delete_action_button->set_button_icon(get_theme_icon(SNAME("Clear"), EditorStringName(EditorIcons)));
  974. rotate_x_button->set_button_icon(get_theme_icon(SNAME("RotateLeft"), EditorStringName(EditorIcons)));
  975. rotate_y_button->set_button_icon(get_theme_icon(SNAME("ToolRotate"), EditorStringName(EditorIcons)));
  976. rotate_z_button->set_button_icon(get_theme_icon(SNAME("RotateRight"), EditorStringName(EditorIcons)));
  977. search_box->set_right_icon(get_theme_icon(SNAME("Search"), EditorStringName(EditorIcons)));
  978. mode_thumbnail->set_button_icon(get_theme_icon(SNAME("FileThumbnail"), EditorStringName(EditorIcons)));
  979. mode_list->set_button_icon(get_theme_icon(SNAME("FileList"), EditorStringName(EditorIcons)));
  980. options->set_button_icon(get_theme_icon(SNAME("Tools"), EditorStringName(EditorIcons)));
  981. }
  982. void GridMapEditor::_notification(int p_what) {
  983. switch (p_what) {
  984. case NOTIFICATION_ENTER_TREE: {
  985. mesh_library_palette->connect(SceneStringName(item_selected), callable_mp(this, &GridMapEditor::_item_selected_cbk));
  986. for (int i = 0; i < 3; i++) {
  987. grid[i] = RS::get_singleton()->mesh_create();
  988. grid_instance[i] = RS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  989. RenderingServer::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  990. selection_level_instance[i] = RenderingServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  991. RenderingServer::get_singleton()->instance_set_layer_mask(selection_level_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  992. }
  993. cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  994. RenderingServer::get_singleton()->instance_set_layer_mask(cursor_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  995. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false);
  996. selection_instance = RenderingServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  997. RenderingServer::get_singleton()->instance_set_layer_mask(selection_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  998. paste_instance = RenderingServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  999. RenderingServer::get_singleton()->instance_set_layer_mask(paste_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  1000. _update_selection_transform();
  1001. _update_paste_indicator();
  1002. _update_theme();
  1003. } break;
  1004. case NOTIFICATION_EXIT_TREE: {
  1005. _clear_clipboard_data();
  1006. for (int i = 0; i < 3; i++) {
  1007. RS::get_singleton()->free(grid_instance[i]);
  1008. RS::get_singleton()->free(grid[i]);
  1009. grid_instance[i] = RID();
  1010. grid[i] = RID();
  1011. RenderingServer::get_singleton()->free(selection_level_instance[i]);
  1012. }
  1013. RenderingServer::get_singleton()->free(cursor_instance);
  1014. RenderingServer::get_singleton()->free(selection_instance);
  1015. RenderingServer::get_singleton()->free(paste_instance);
  1016. cursor_instance = RID();
  1017. selection_instance = RID();
  1018. paste_instance = RID();
  1019. } break;
  1020. case NOTIFICATION_PROCESS: {
  1021. if (!node) {
  1022. return;
  1023. }
  1024. Transform3D xf = node->get_global_transform();
  1025. if (xf != grid_xform) {
  1026. for (int i = 0; i < 3; i++) {
  1027. RS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  1028. }
  1029. grid_xform = xf;
  1030. }
  1031. } break;
  1032. case NOTIFICATION_THEME_CHANGED: {
  1033. _update_theme();
  1034. } break;
  1035. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  1036. if (input_action == INPUT_PAINT) {
  1037. // Simulate mouse released event to stop drawing when editor focus exists.
  1038. Ref<InputEventMouseButton> release;
  1039. release.instantiate();
  1040. release->set_button_index(MouseButton::LEFT);
  1041. forward_spatial_input_event(nullptr, release);
  1042. }
  1043. } break;
  1044. }
  1045. }
  1046. void GridMapEditor::_update_cursor_instance() {
  1047. if (!node) {
  1048. return;
  1049. }
  1050. if (cursor_instance.is_valid()) {
  1051. RenderingServer::get_singleton()->free(cursor_instance);
  1052. }
  1053. cursor_instance = RID();
  1054. if (mode_buttons_group->get_pressed_button() == paint_mode_button) {
  1055. if (selected_palette >= 0 && node && node->get_mesh_library().is_valid()) {
  1056. Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette);
  1057. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  1058. cursor_instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());
  1059. RS::ShadowCastingSetting cast_shadows = (RS::ShadowCastingSetting)node->get_mesh_library()->get_item_mesh_cast_shadow(selected_palette);
  1060. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(cursor_instance, cast_shadows);
  1061. }
  1062. }
  1063. } else if (mode_buttons_group->get_pressed_button() == select_mode_button) {
  1064. cursor_inner_mat->set_albedo(Color(default_color, 0.2));
  1065. cursor_outer_mat->set_albedo(Color(default_color, 0.8));
  1066. cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  1067. } else if (mode_buttons_group->get_pressed_button() == erase_mode_button) {
  1068. cursor_inner_mat->set_albedo(Color(erase_color, 0.2));
  1069. cursor_outer_mat->set_albedo(Color(erase_color, 0.8));
  1070. cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  1071. } else if (mode_buttons_group->get_pressed_button() == pick_mode_button) {
  1072. cursor_inner_mat->set_albedo(Color(pick_color, 0.2));
  1073. cursor_outer_mat->set_albedo(Color(pick_color, 0.8));
  1074. cursor_instance = RenderingServer::get_singleton()->instance_create2(cursor_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  1075. }
  1076. _update_cursor_transform();
  1077. }
  1078. void GridMapEditor::_on_tool_mode_changed() {
  1079. _show_viewports_transform_gizmo(mode_buttons_group->get_pressed_button() == transform_mode_button);
  1080. _update_cursor_instance();
  1081. }
  1082. void GridMapEditor::_item_selected_cbk(int idx) {
  1083. selected_palette = mesh_library_palette->get_item_metadata(idx);
  1084. _update_cursor_instance();
  1085. }
  1086. void GridMapEditor::_floor_changed(float p_value) {
  1087. if (updating) {
  1088. return;
  1089. }
  1090. edit_floor[edit_axis] = p_value;
  1091. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  1092. update_grid();
  1093. _update_selection_transform();
  1094. }
  1095. void GridMapEditor::_floor_mouse_exited() {
  1096. floor->get_line_edit()->release_focus();
  1097. }
  1098. void GridMapEditor::_bind_methods() {
  1099. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  1100. ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection);
  1101. }
  1102. GridMapEditor::GridMapEditor() {
  1103. ED_SHORTCUT("grid_map/previous_floor", TTR("Previous Floor"), Key::KEY_1, true);
  1104. ED_SHORTCUT("grid_map/next_floor", TTR("Next Floor"), Key::KEY_3, true);
  1105. ED_SHORTCUT("grid_map/edit_x_axis", TTR("Edit X Axis"), KeyModifierMask::SHIFT + Key::Z, true);
  1106. ED_SHORTCUT("grid_map/edit_y_axis", TTR("Edit Y Axis"), KeyModifierMask::SHIFT + Key::X, true);
  1107. ED_SHORTCUT("grid_map/edit_z_axis", TTR("Edit Z Axis"), KeyModifierMask::SHIFT + Key::C, true);
  1108. ED_SHORTCUT("grid_map/keep_selected", TTR("Keep Selection"));
  1109. ED_SHORTCUT("grid_map/clear_rotation", TTR("Clear Rotation"));
  1110. options = memnew(MenuButton);
  1111. options->set_theme_type_variation("FlatButton");
  1112. options->get_popup()->add_separator();
  1113. options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_x_axis"), MENU_OPTION_X_AXIS);
  1114. options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_y_axis"), MENU_OPTION_Y_AXIS);
  1115. options->get_popup()->add_radio_check_shortcut(ED_GET_SHORTCUT("grid_map/edit_z_axis"), MENU_OPTION_Z_AXIS);
  1116. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  1117. options->get_popup()->add_separator();
  1118. // TRANSLATORS: This is a toggle to select after pasting the new content.
  1119. options->get_popup()->add_shortcut(ED_GET_SHORTCUT("grid_map/clear_rotation"), MENU_OPTION_CURSOR_CLEAR_ROTATION);
  1120. options->get_popup()->add_check_shortcut(ED_GET_SHORTCUT("grid_map/keep_selected"), MENU_OPTION_PASTE_SELECTS);
  1121. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS), true);
  1122. options->get_popup()->add_separator();
  1123. options->get_popup()->add_item(TTR("Settings..."), MENU_OPTION_GRIDMAP_SETTINGS);
  1124. settings_dialog = memnew(ConfirmationDialog);
  1125. settings_dialog->set_title(TTR("GridMap Settings"));
  1126. add_child(settings_dialog);
  1127. settings_vbc = memnew(VBoxContainer);
  1128. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  1129. settings_dialog->add_child(settings_vbc);
  1130. settings_pick_distance = memnew(SpinBox);
  1131. settings_pick_distance->set_max(10000.0f);
  1132. settings_pick_distance->set_min(500.0f);
  1133. settings_pick_distance->set_step(1.0f);
  1134. settings_pick_distance->set_value(EDITOR_GET("editors/grid_map/pick_distance"));
  1135. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  1136. options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &GridMapEditor::_menu_option));
  1137. toolbar = memnew(HBoxContainer);
  1138. add_child(toolbar);
  1139. toolbar->set_h_size_flags(SIZE_EXPAND_FILL);
  1140. HBoxContainer *mode_buttons = memnew(HBoxContainer);
  1141. toolbar->add_child(mode_buttons);
  1142. mode_buttons_group.instantiate();
  1143. transform_mode_button = memnew(Button);
  1144. transform_mode_button->set_theme_type_variation("FlatButton");
  1145. transform_mode_button->set_toggle_mode(true);
  1146. transform_mode_button->set_button_group(mode_buttons_group);
  1147. transform_mode_button->set_shortcut(ED_SHORTCUT("grid_map/transform_tool", TTR("Transform"), Key::T, true));
  1148. transform_mode_button->connect(SceneStringName(toggled),
  1149. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1150. mode_buttons->add_child(transform_mode_button);
  1151. viewport_shortcut_buttons.push_back(transform_mode_button);
  1152. VSeparator *vsep = memnew(VSeparator);
  1153. mode_buttons->add_child(vsep);
  1154. select_mode_button = memnew(Button);
  1155. select_mode_button->set_theme_type_variation("FlatButton");
  1156. select_mode_button->set_toggle_mode(true);
  1157. select_mode_button->set_button_group(mode_buttons_group);
  1158. select_mode_button->set_shortcut(ED_SHORTCUT("grid_map/selection_tool", TTR("Selection"), Key::Q, true));
  1159. select_mode_button->connect(SceneStringName(toggled),
  1160. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1161. mode_buttons->add_child(select_mode_button);
  1162. viewport_shortcut_buttons.push_back(select_mode_button);
  1163. erase_mode_button = memnew(Button);
  1164. erase_mode_button->set_theme_type_variation("FlatButton");
  1165. erase_mode_button->set_toggle_mode(true);
  1166. erase_mode_button->set_button_group(mode_buttons_group);
  1167. erase_mode_button->set_shortcut(ED_SHORTCUT("grid_map/erase_tool", TTR("Erase"), Key::W, true));
  1168. mode_buttons->add_child(erase_mode_button);
  1169. erase_mode_button->connect(SceneStringName(toggled),
  1170. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1171. viewport_shortcut_buttons.push_back(erase_mode_button);
  1172. paint_mode_button = memnew(Button);
  1173. paint_mode_button->set_theme_type_variation("FlatButton");
  1174. paint_mode_button->set_toggle_mode(true);
  1175. paint_mode_button->set_button_group(mode_buttons_group);
  1176. paint_mode_button->set_shortcut(ED_SHORTCUT("grid_map/paint_tool", TTR("Paint"), Key::E, true));
  1177. paint_mode_button->connect(SceneStringName(toggled),
  1178. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1179. mode_buttons->add_child(paint_mode_button);
  1180. viewport_shortcut_buttons.push_back(paint_mode_button);
  1181. pick_mode_button = memnew(Button);
  1182. pick_mode_button->set_theme_type_variation("FlatButton");
  1183. pick_mode_button->set_toggle_mode(true);
  1184. pick_mode_button->set_button_group(mode_buttons_group);
  1185. pick_mode_button->set_shortcut(ED_SHORTCUT("grid_map/pick_tool", TTR("Pick"), Key::R, true));
  1186. pick_mode_button->connect(SceneStringName(toggled),
  1187. callable_mp(this, &GridMapEditor::_on_tool_mode_changed).unbind(1));
  1188. mode_buttons->add_child(pick_mode_button);
  1189. viewport_shortcut_buttons.push_back(pick_mode_button);
  1190. vsep = memnew(VSeparator);
  1191. toolbar->add_child(vsep);
  1192. HBoxContainer *action_buttons = memnew(HBoxContainer);
  1193. toolbar->add_child(action_buttons);
  1194. fill_action_button = memnew(Button);
  1195. fill_action_button->set_theme_type_variation("FlatButton");
  1196. fill_action_button->set_shortcut(ED_SHORTCUT("grid_map/fill_tool", TTR("Fill"), Key::Z, true));
  1197. fill_action_button->connect(SceneStringName(pressed),
  1198. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_FILL));
  1199. action_buttons->add_child(fill_action_button);
  1200. viewport_shortcut_buttons.push_back(fill_action_button);
  1201. move_action_button = memnew(Button);
  1202. move_action_button->set_theme_type_variation("FlatButton");
  1203. move_action_button->set_shortcut(ED_SHORTCUT("grid_map/move_tool", TTR("Move"), Key::X, true));
  1204. move_action_button->connect(SceneStringName(pressed),
  1205. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_CUT));
  1206. action_buttons->add_child(move_action_button);
  1207. viewport_shortcut_buttons.push_back(move_action_button);
  1208. duplicate_action_button = memnew(Button);
  1209. duplicate_action_button->set_theme_type_variation("FlatButton");
  1210. duplicate_action_button->set_shortcut(ED_SHORTCUT("grid_map/duplicate_tool", TTR("Duplicate"), Key::C, true));
  1211. duplicate_action_button->connect(SceneStringName(pressed),
  1212. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_DUPLICATE));
  1213. action_buttons->add_child(duplicate_action_button);
  1214. viewport_shortcut_buttons.push_back(duplicate_action_button);
  1215. delete_action_button = memnew(Button);
  1216. delete_action_button->set_theme_type_variation("FlatButton");
  1217. delete_action_button->set_shortcut(ED_SHORTCUT("grid_map/delete_tool", TTR("Delete"), Key::V, true));
  1218. delete_action_button->connect(SceneStringName(pressed),
  1219. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_SELECTION_CLEAR));
  1220. action_buttons->add_child(delete_action_button);
  1221. viewport_shortcut_buttons.push_back(delete_action_button);
  1222. vsep = memnew(VSeparator);
  1223. toolbar->add_child(vsep);
  1224. HBoxContainer *rotation_buttons = memnew(HBoxContainer);
  1225. toolbar->add_child(rotation_buttons);
  1226. rotate_x_button = memnew(Button);
  1227. rotate_x_button->set_theme_type_variation("FlatButton");
  1228. rotate_x_button->set_shortcut(ED_SHORTCUT("grid_map/cursor_rotate_x", TTR("Cursor Rotate X"), Key::A, true));
  1229. rotate_x_button->connect(SceneStringName(pressed),
  1230. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_CURSOR_ROTATE_X));
  1231. rotation_buttons->add_child(rotate_x_button);
  1232. viewport_shortcut_buttons.push_back(rotate_x_button);
  1233. rotate_y_button = memnew(Button);
  1234. rotate_y_button->set_theme_type_variation("FlatButton");
  1235. rotate_y_button->set_shortcut(ED_SHORTCUT("grid_map/cursor_rotate_y", TTR("Cursor Rotate Y"), Key::S, true));
  1236. rotate_y_button->connect(SceneStringName(pressed),
  1237. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_CURSOR_ROTATE_Y));
  1238. rotation_buttons->add_child(rotate_y_button);
  1239. viewport_shortcut_buttons.push_back(rotate_y_button);
  1240. rotate_z_button = memnew(Button);
  1241. rotate_z_button->set_theme_type_variation("FlatButton");
  1242. rotate_z_button->set_shortcut(ED_SHORTCUT("grid_map/cursor_rotate_z", TTR("Cursor Rotate Z"), Key::D, true));
  1243. rotate_z_button->connect(SceneStringName(pressed),
  1244. callable_mp(this, &GridMapEditor::_menu_option).bind(MENU_OPTION_CURSOR_ROTATE_Z));
  1245. rotation_buttons->add_child(rotate_z_button);
  1246. viewport_shortcut_buttons.push_back(rotate_z_button);
  1247. // Wide empty separation control. (like BoxContainer::add_spacer())
  1248. Control *c = memnew(Control);
  1249. c->set_mouse_filter(MOUSE_FILTER_PASS);
  1250. c->set_h_size_flags(SIZE_EXPAND_FILL);
  1251. toolbar->add_child(c);
  1252. floor = memnew(SpinBox);
  1253. floor->set_min(-32767);
  1254. floor->set_max(32767);
  1255. floor->set_step(1);
  1256. floor->set_tooltip_text(
  1257. vformat(TTR("Change Grid Floor:\nPrevious Plane (%s)\nNext Plane (%s)"),
  1258. ED_GET_SHORTCUT("grid_map/previous_floor")->get_as_text(),
  1259. ED_GET_SHORTCUT("grid_map/next_floor")->get_as_text()));
  1260. toolbar->add_child(floor);
  1261. floor->get_line_edit()->add_theme_constant_override("minimum_character_width", 2);
  1262. floor->get_line_edit()->set_context_menu_enabled(false);
  1263. floor->connect(SceneStringName(value_changed), callable_mp(this, &GridMapEditor::_floor_changed));
  1264. floor->connect(SceneStringName(mouse_exited), callable_mp(this, &GridMapEditor::_floor_mouse_exited));
  1265. floor->get_line_edit()->connect(SceneStringName(mouse_exited), callable_mp(this, &GridMapEditor::_floor_mouse_exited));
  1266. search_box = memnew(LineEdit);
  1267. search_box->add_theme_constant_override("minimum_character_width", 10);
  1268. search_box->set_placeholder(TTR("Filter Meshes"));
  1269. search_box->set_clear_button_enabled(true);
  1270. toolbar->add_child(search_box);
  1271. search_box->connect(SceneStringName(text_changed), callable_mp(this, &GridMapEditor::_text_changed));
  1272. search_box->connect(SceneStringName(gui_input), callable_mp(this, &GridMapEditor::_sbox_input));
  1273. zoom_widget = memnew(EditorZoomWidget);
  1274. toolbar->add_child(zoom_widget);
  1275. zoom_widget->setup_zoom_limits(0.2, 4);
  1276. zoom_widget->set_zoom(1.0);
  1277. zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);
  1278. zoom_widget->connect("zoom_changed", callable_mp(this, &GridMapEditor::_icon_size_changed));
  1279. zoom_widget->set_shortcut_context(this);
  1280. mode_thumbnail = memnew(Button);
  1281. mode_thumbnail->set_theme_type_variation("FlatButton");
  1282. mode_thumbnail->set_toggle_mode(true);
  1283. mode_thumbnail->set_pressed(true);
  1284. toolbar->add_child(mode_thumbnail);
  1285. mode_thumbnail->connect(SceneStringName(pressed), callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_THUMBNAIL));
  1286. mode_list = memnew(Button);
  1287. mode_list->set_theme_type_variation("FlatButton");
  1288. mode_list->set_toggle_mode(true);
  1289. mode_list->set_pressed(false);
  1290. toolbar->add_child(mode_list);
  1291. mode_list->connect(SceneStringName(pressed), callable_mp(this, &GridMapEditor::_set_display_mode).bind(DISPLAY_LIST));
  1292. toolbar->add_child(options);
  1293. mesh_library_palette = memnew(ItemList);
  1294. mesh_library_palette->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1295. add_child(mesh_library_palette);
  1296. mesh_library_palette->set_v_size_flags(SIZE_EXPAND_FILL);
  1297. mesh_library_palette->connect(SceneStringName(gui_input), callable_mp(this, &GridMapEditor::_mesh_library_palette_input));
  1298. info_message = memnew(Label);
  1299. info_message->set_text(TTR("Give a MeshLibrary resource to this GridMap to use its meshes."));
  1300. info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1301. info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1302. info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
  1303. info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  1304. info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  1305. mesh_library_palette->add_child(info_message);
  1306. edit_axis = Vector3::AXIS_Y;
  1307. edit_floor[0] = -1;
  1308. edit_floor[1] = -1;
  1309. edit_floor[2] = -1;
  1310. cursor_mesh = RenderingServer::get_singleton()->mesh_create();
  1311. selection_mesh = RenderingServer::get_singleton()->mesh_create();
  1312. paste_mesh = RenderingServer::get_singleton()->mesh_create();
  1313. {
  1314. // Selection mesh create.
  1315. Vector<Vector3> lines;
  1316. Vector<Vector3> triangles;
  1317. Vector<Vector3> square[3];
  1318. for (int i = 0; i < 6; i++) {
  1319. Vector3 face_points[4];
  1320. for (int j = 0; j < 4; j++) {
  1321. float v[3];
  1322. v[0] = 1.0;
  1323. v[1] = 1 - 2 * ((j >> 1) & 1);
  1324. v[2] = v[1] * (1 - 2 * (j & 1));
  1325. for (int k = 0; k < 3; k++) {
  1326. if (i < 3) {
  1327. face_points[j][(i + k) % 3] = v[k];
  1328. } else {
  1329. face_points[3 - j][(i + k) % 3] = -v[k];
  1330. }
  1331. }
  1332. }
  1333. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1334. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1335. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1336. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1337. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1338. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1339. }
  1340. for (int i = 0; i < 12; i++) {
  1341. AABB base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  1342. Vector3 a, b;
  1343. base.get_edge(i, a, b);
  1344. lines.push_back(a);
  1345. lines.push_back(b);
  1346. }
  1347. for (int i = 0; i < 3; i++) {
  1348. Vector3 points[4];
  1349. for (int j = 0; j < 4; j++) {
  1350. static const bool orderx[4] = { false, true, true, false };
  1351. static const bool ordery[4] = { false, false, true, true };
  1352. Vector3 sp;
  1353. if (orderx[j]) {
  1354. sp[(i + 1) % 3] = 1.0;
  1355. }
  1356. if (ordery[j]) {
  1357. sp[(i + 2) % 3] = 1.0;
  1358. }
  1359. points[j] = sp;
  1360. }
  1361. for (int j = 0; j < 4; j++) {
  1362. Vector3 ofs;
  1363. ofs[i] += 0.01;
  1364. square[i].push_back(points[j] - ofs);
  1365. square[i].push_back(points[(j + 1) % 4] - ofs);
  1366. square[i].push_back(points[j] + ofs);
  1367. square[i].push_back(points[(j + 1) % 4] + ofs);
  1368. }
  1369. }
  1370. Array d;
  1371. d.resize(RS::ARRAY_MAX);
  1372. default_color = Color(0.0, 0.565, 1.0); // blue 0.7, 0.7, 1.0
  1373. erase_color = Color(1.0, 0.2, 0.2); // red
  1374. pick_color = Color(1, 0.7, 0); // orange/yellow
  1375. cursor_inner_mat.instantiate();
  1376. cursor_inner_mat->set_albedo(Color(default_color, 0.2));
  1377. cursor_inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1378. cursor_inner_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1379. cursor_inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1380. cursor_outer_mat.instantiate();
  1381. cursor_outer_mat->set_albedo(Color(default_color, 0.8));
  1382. cursor_outer_mat->set_on_top_of_alpha();
  1383. cursor_outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1384. cursor_outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1385. cursor_outer_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1386. inner_mat.instantiate();
  1387. inner_mat->set_albedo(Color(default_color, 0.2));
  1388. inner_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1389. inner_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1390. inner_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1391. outer_mat.instantiate();
  1392. outer_mat->set_albedo(Color(default_color, 0.8));
  1393. outer_mat->set_on_top_of_alpha();
  1394. outer_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1395. outer_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1396. outer_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1397. selection_floor_mat.instantiate();
  1398. selection_floor_mat->set_albedo(Color(0.80, 0.80, 1.0, 1));
  1399. selection_floor_mat->set_on_top_of_alpha();
  1400. selection_floor_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1401. selection_floor_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1402. d[RS::ARRAY_VERTEX] = triangles;
  1403. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(cursor_mesh, RS::PRIMITIVE_TRIANGLES, d);
  1404. RenderingServer::get_singleton()->mesh_surface_set_material(cursor_mesh, 0, cursor_inner_mat->get_rid());
  1405. d[RS::ARRAY_VERTEX] = lines;
  1406. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(cursor_mesh, RS::PRIMITIVE_LINES, d);
  1407. RenderingServer::get_singleton()->mesh_surface_set_material(cursor_mesh, 1, cursor_outer_mat->get_rid());
  1408. d[RS::ARRAY_VERTEX] = triangles;
  1409. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_TRIANGLES, d);
  1410. RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  1411. d[RS::ARRAY_VERTEX] = lines;
  1412. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, RS::PRIMITIVE_LINES, d);
  1413. RenderingServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  1414. d[RS::ARRAY_VERTEX] = triangles;
  1415. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_TRIANGLES, d);
  1416. RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 0, inner_mat->get_rid());
  1417. d[RS::ARRAY_VERTEX] = lines;
  1418. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(paste_mesh, RS::PRIMITIVE_LINES, d);
  1419. RenderingServer::get_singleton()->mesh_surface_set_material(paste_mesh, 1, outer_mat->get_rid());
  1420. for (int i = 0; i < 3; i++) {
  1421. d[RS::ARRAY_VERTEX] = square[i];
  1422. selection_level_mesh[i] = RS::get_singleton()->mesh_create();
  1423. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(selection_level_mesh[i], RS::PRIMITIVE_LINES, d);
  1424. RenderingServer::get_singleton()->mesh_surface_set_material(selection_level_mesh[i], 0, selection_floor_mat->get_rid());
  1425. }
  1426. }
  1427. _set_selection(false);
  1428. indicator_mat.instantiate();
  1429. indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1430. indicator_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1431. indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  1432. indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1433. indicator_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  1434. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  1435. }
  1436. GridMapEditor::~GridMapEditor() {
  1437. ERR_FAIL_NULL(RenderingServer::get_singleton());
  1438. _clear_clipboard_data();
  1439. for (int i = 0; i < 3; i++) {
  1440. if (grid[i].is_valid()) {
  1441. RenderingServer::get_singleton()->free(grid[i]);
  1442. }
  1443. if (grid_instance[i].is_valid()) {
  1444. RenderingServer::get_singleton()->free(grid_instance[i]);
  1445. }
  1446. if (selection_level_instance[i].is_valid()) {
  1447. RenderingServer::get_singleton()->free(selection_level_instance[i]);
  1448. }
  1449. if (selection_level_mesh[i].is_valid()) {
  1450. RenderingServer::get_singleton()->free(selection_level_mesh[i]);
  1451. }
  1452. }
  1453. RenderingServer::get_singleton()->free(cursor_mesh);
  1454. if (cursor_instance.is_valid()) {
  1455. RenderingServer::get_singleton()->free(cursor_instance);
  1456. }
  1457. RenderingServer::get_singleton()->free(selection_mesh);
  1458. if (selection_instance.is_valid()) {
  1459. RenderingServer::get_singleton()->free(selection_instance);
  1460. }
  1461. RenderingServer::get_singleton()->free(paste_mesh);
  1462. if (paste_instance.is_valid()) {
  1463. RenderingServer::get_singleton()->free(paste_instance);
  1464. }
  1465. }
  1466. void GridMapEditorPlugin::edit(Object *p_object) {
  1467. grid_map_editor->edit(Object::cast_to<GridMap>(p_object));
  1468. }
  1469. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1470. return p_object->is_class("GridMap");
  1471. }
  1472. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1473. if (p_visible) {
  1474. BaseButton *button = grid_map_editor->mode_buttons_group->get_pressed_button();
  1475. if (button == nullptr) {
  1476. grid_map_editor->select_mode_button->set_pressed(true);
  1477. }
  1478. grid_map_editor->_on_tool_mode_changed();
  1479. panel_button->show();
  1480. EditorNode::get_bottom_panel()->make_item_visible(grid_map_editor);
  1481. grid_map_editor->set_process(true);
  1482. } else {
  1483. grid_map_editor->_show_viewports_transform_gizmo(true);
  1484. panel_button->hide();
  1485. if (grid_map_editor->is_visible_in_tree()) {
  1486. EditorNode::get_bottom_panel()->hide_bottom_panel();
  1487. }
  1488. grid_map_editor->set_process(false);
  1489. }
  1490. }
  1491. GridMapEditorPlugin::GridMapEditorPlugin() {
  1492. grid_map_editor = memnew(GridMapEditor);
  1493. grid_map_editor->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1494. grid_map_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  1495. grid_map_editor->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  1496. grid_map_editor->hide();
  1497. panel_button = EditorNode::get_bottom_panel()->add_item(TTR("GridMap"), grid_map_editor, ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_grid_map_bottom_panel", TTR("Toggle GridMap Bottom Panel")));
  1498. panel_button->hide();
  1499. }
  1500. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1501. }
  1502. #endif // TOOLS_ENABLED