grid_map_editor_plugin.cpp 52 KB

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