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_node.h"
  33. #include "editor/editor_scale.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/plugins/node_3d_editor_plugin.h"
  36. #include "scene/3d/camera_3d.h"
  37. #include "core/os/keyboard.h"
  38. #include "scene/main/window.h"
  39. void GridMapEditor::_node_removed(Node *p_node) {
  40. if (p_node == node) {
  41. node = nullptr;
  42. }
  43. }
  44. void GridMapEditor::_configure() {
  45. if (!node) {
  46. return;
  47. }
  48. update_grid();
  49. }
  50. void GridMapEditor::_menu_option(int p_option) {
  51. switch (p_option) {
  52. case MENU_OPTION_PREV_LEVEL: {
  53. floor->set_value(floor->get_value() - 1);
  54. } break;
  55. case MENU_OPTION_NEXT_LEVEL: {
  56. floor->set_value(floor->get_value() + 1);
  57. } break;
  58. case MENU_OPTION_CLIP_DISABLED:
  59. case MENU_OPTION_CLIP_ABOVE:
  60. case MENU_OPTION_CLIP_BELOW: {
  61. clip_mode = ClipMode(p_option - MENU_OPTION_CLIP_DISABLED);
  62. for (int i = 0; i < 3; i++) {
  63. int index = options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED + i);
  64. options->get_popup()->set_item_checked(index, i == clip_mode);
  65. }
  66. _update_clip();
  67. } break;
  68. case MENU_OPTION_X_AXIS:
  69. case MENU_OPTION_Y_AXIS:
  70. case MENU_OPTION_Z_AXIS: {
  71. int new_axis = p_option - MENU_OPTION_X_AXIS;
  72. for (int i = 0; i < 3; i++) {
  73. int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i);
  74. options->get_popup()->set_item_checked(idx, i == new_axis);
  75. }
  76. if (edit_axis != new_axis) {
  77. int item1 = options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL);
  78. int item2 = options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL);
  79. if (edit_axis == Vector3::AXIS_Y) {
  80. options->get_popup()->set_item_text(item1, TTR("Next Plane"));
  81. options->get_popup()->set_item_text(item2, TTR("Previous Plane"));
  82. spin_box_label->set_text(TTR("Plane:"));
  83. } else if (new_axis == Vector3::AXIS_Y) {
  84. options->get_popup()->set_item_text(item1, TTR("Next Floor"));
  85. options->get_popup()->set_item_text(item2, TTR("Previous Floor"));
  86. spin_box_label->set_text(TTR("Floor:"));
  87. }
  88. }
  89. edit_axis = Vector3::Axis(new_axis);
  90. update_grid();
  91. _update_clip();
  92. } break;
  93. case MENU_OPTION_CURSOR_ROTATE_Y: {
  94. Basis r;
  95. if (input_action == INPUT_PASTE) {
  96. r.set_orthogonal_index(paste_indicator.orientation);
  97. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  98. paste_indicator.orientation = r.get_orthogonal_index();
  99. _update_paste_indicator();
  100. break;
  101. }
  102. r.set_orthogonal_index(cursor_rot);
  103. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  104. cursor_rot = r.get_orthogonal_index();
  105. _update_cursor_transform();
  106. } break;
  107. case MENU_OPTION_CURSOR_ROTATE_X: {
  108. Basis r;
  109. if (input_action == INPUT_PASTE) {
  110. r.set_orthogonal_index(paste_indicator.orientation);
  111. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  112. paste_indicator.orientation = r.get_orthogonal_index();
  113. _update_paste_indicator();
  114. break;
  115. }
  116. r.set_orthogonal_index(cursor_rot);
  117. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  118. cursor_rot = r.get_orthogonal_index();
  119. _update_cursor_transform();
  120. } break;
  121. case MENU_OPTION_CURSOR_ROTATE_Z: {
  122. Basis r;
  123. if (input_action == INPUT_PASTE) {
  124. r.set_orthogonal_index(paste_indicator.orientation);
  125. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  126. paste_indicator.orientation = r.get_orthogonal_index();
  127. _update_paste_indicator();
  128. break;
  129. }
  130. r.set_orthogonal_index(cursor_rot);
  131. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  132. cursor_rot = r.get_orthogonal_index();
  133. _update_cursor_transform();
  134. } break;
  135. case MENU_OPTION_CURSOR_BACK_ROTATE_Y: {
  136. Basis r;
  137. if (input_action == INPUT_PASTE) {
  138. r.set_orthogonal_index(paste_indicator.orientation);
  139. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  140. paste_indicator.orientation = r.get_orthogonal_index();
  141. _update_paste_indicator();
  142. break;
  143. }
  144. r.set_orthogonal_index(cursor_rot);
  145. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  146. cursor_rot = r.get_orthogonal_index();
  147. _update_cursor_transform();
  148. } break;
  149. case MENU_OPTION_CURSOR_BACK_ROTATE_X: {
  150. Basis r;
  151. if (input_action == INPUT_PASTE) {
  152. r.set_orthogonal_index(paste_indicator.orientation);
  153. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  154. paste_indicator.orientation = r.get_orthogonal_index();
  155. _update_paste_indicator();
  156. break;
  157. }
  158. r.set_orthogonal_index(cursor_rot);
  159. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  160. cursor_rot = r.get_orthogonal_index();
  161. _update_cursor_transform();
  162. } break;
  163. case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
  164. Basis r;
  165. if (input_action == INPUT_PASTE) {
  166. r.set_orthogonal_index(paste_indicator.orientation);
  167. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  168. paste_indicator.orientation = r.get_orthogonal_index();
  169. _update_paste_indicator();
  170. break;
  171. }
  172. r.set_orthogonal_index(cursor_rot);
  173. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  174. cursor_rot = r.get_orthogonal_index();
  175. _update_cursor_transform();
  176. } break;
  177. case MENU_OPTION_CURSOR_CLEAR_ROTATION: {
  178. if (input_action == INPUT_PASTE) {
  179. paste_indicator.orientation = 0;
  180. _update_paste_indicator();
  181. break;
  182. }
  183. cursor_rot = 0;
  184. _update_cursor_transform();
  185. } break;
  186. case MENU_OPTION_PASTE_SELECTS: {
  187. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  188. options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
  189. } break;
  190. case MENU_OPTION_SELECTION_DUPLICATE:
  191. case MENU_OPTION_SELECTION_CUT: {
  192. if (!(selection.active && input_action == INPUT_NONE)) {
  193. break;
  194. }
  195. _set_clipboard_data();
  196. if (p_option == MENU_OPTION_SELECTION_CUT) {
  197. _delete_selection();
  198. }
  199. input_action = INPUT_PASTE;
  200. paste_indicator.click = selection.begin;
  201. paste_indicator.current = selection.begin;
  202. paste_indicator.begin = selection.begin;
  203. paste_indicator.end = selection.end;
  204. paste_indicator.orientation = 0;
  205. _update_paste_indicator();
  206. } break;
  207. case MENU_OPTION_SELECTION_CLEAR: {
  208. if (!selection.active) {
  209. break;
  210. }
  211. _delete_selection();
  212. } break;
  213. case MENU_OPTION_SELECTION_FILL: {
  214. if (!selection.active) {
  215. return;
  216. }
  217. _fill_selection();
  218. } break;
  219. case MENU_OPTION_GRIDMAP_SETTINGS: {
  220. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50) * EDSCALE);
  221. } break;
  222. }
  223. }
  224. void GridMapEditor::_update_cursor_transform() {
  225. cursor_transform = Transform3D();
  226. cursor_transform.origin = cursor_origin;
  227. cursor_transform.basis.set_orthogonal_index(cursor_rot);
  228. cursor_transform.basis *= node->get_cell_scale();
  229. cursor_transform = node->get_global_transform() * cursor_transform;
  230. if (selected_palette >= 0) {
  231. if (node && !node->get_mesh_library().is_null()) {
  232. cursor_transform *= node->get_mesh_library()->get_item_mesh_transform(selected_palette);
  233. }
  234. }
  235. if (cursor_instance.is_valid()) {
  236. RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  237. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
  238. }
  239. }
  240. void GridMapEditor::_update_selection_transform() {
  241. Transform3D xf_zero;
  242. xf_zero.basis.set_zero();
  243. if (!selection.active) {
  244. RenderingServer::get_singleton()->instance_set_transform(selection_instance, xf_zero);
  245. for (int i = 0; i < 3; i++) {
  246. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  247. }
  248. return;
  249. }
  250. Transform3D xf;
  251. xf.scale((Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  252. xf.origin = selection.begin * node->get_cell_size();
  253. RenderingServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  254. for (int i = 0; i < 3; i++) {
  255. if (i != edit_axis || (edit_floor[edit_axis] < selection.begin[edit_axis]) || (edit_floor[edit_axis] > selection.end[edit_axis] + 1)) {
  256. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf_zero);
  257. } else {
  258. Vector3 scale = (selection.end - selection.begin + Vector3(1, 1, 1));
  259. scale[edit_axis] = 1.0;
  260. Vector3 position = selection.begin;
  261. position[edit_axis] = edit_floor[edit_axis];
  262. scale *= node->get_cell_size();
  263. position *= node->get_cell_size();
  264. Transform3D xf2;
  265. xf2.basis.scale(scale);
  266. xf2.origin = position;
  267. RenderingServer::get_singleton()->instance_set_transform(selection_level_instance[i], xf2);
  268. }
  269. }
  270. }
  271. void GridMapEditor::_validate_selection() {
  272. if (!selection.active) {
  273. return;
  274. }
  275. selection.begin = selection.click;
  276. selection.end = selection.current;
  277. if (selection.begin.x > selection.end.x) {
  278. SWAP(selection.begin.x, selection.end.x);
  279. }
  280. if (selection.begin.y > selection.end.y) {
  281. SWAP(selection.begin.y, selection.end.y);
  282. }
  283. if (selection.begin.z > selection.end.z) {
  284. SWAP(selection.begin.z, selection.end.z);
  285. }
  286. _update_selection_transform();
  287. }
  288. void GridMapEditor::_set_selection(bool p_active, const Vector3 &p_begin, const Vector3 &p_end) {
  289. selection.active = p_active;
  290. selection.begin = p_begin;
  291. selection.end = p_end;
  292. selection.click = p_begin;
  293. selection.current = p_end;
  294. if (is_visible_in_tree()) {
  295. _update_selection_transform();
  296. }
  297. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CLEAR), !selection.active);
  298. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_CUT), !selection.active);
  299. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_DUPLICATE), !selection.active);
  300. options->get_popup()->set_item_disabled(options->get_popup()->get_item_index(MENU_OPTION_SELECTION_FILL), !selection.active);
  301. }
  302. bool GridMapEditor::do_input_action(Camera3D *p_camera, const Point2 &p_point, bool p_click) {
  303. if (!spatial_editor) {
  304. return false;
  305. }
  306. if (selected_palette < 0 && input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE) {
  307. return false;
  308. }
  309. Ref<MeshLibrary> mesh_library = node->get_mesh_library();
  310. if (mesh_library.is_null()) {
  311. return false;
  312. }
  313. if (input_action != INPUT_PICK && input_action != INPUT_SELECT && input_action != INPUT_PASTE && !mesh_library->has_item(selected_palette)) {
  314. return false;
  315. }
  316. Camera3D *camera = p_camera;
  317. Vector3 from = camera->project_ray_origin(p_point);
  318. Vector3 normal = camera->project_ray_normal(p_point);
  319. Transform3D local_xform = node->get_global_transform().affine_inverse();
  320. Vector<Plane> planes = camera->get_frustum();
  321. from = local_xform.xform(from);
  322. normal = local_xform.basis.xform(normal).normalized();
  323. Plane p;
  324. p.normal[edit_axis] = 1.0;
  325. p.d = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  326. Vector3 inters;
  327. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters)) {
  328. return false;
  329. }
  330. // Make sure the intersection is inside the frustum planes, to avoid
  331. // Painting on invisible regions.
  332. for (int i = 0; i < planes.size(); i++) {
  333. Plane fp = local_xform.xform(planes[i]);
  334. if (fp.is_point_over(inters)) {
  335. return false;
  336. }
  337. }
  338. int cell[3];
  339. Vector3 cell_size = node->get_cell_size();
  340. for (int i = 0; i < 3; i++) {
  341. if (i == edit_axis) {
  342. cell[i] = edit_floor[i];
  343. } else {
  344. cell[i] = inters[i] / cell_size[i];
  345. if (inters[i] < 0) {
  346. cell[i] -= 1; // Compensate negative.
  347. }
  348. grid_ofs[i] = cell[i] * cell_size[i];
  349. }
  350. }
  351. RS::get_singleton()->instance_set_transform(grid_instance[edit_axis], node->get_global_transform() * edit_grid_xform);
  352. if (cursor_instance.is_valid()) {
  353. 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();
  354. cursor_visible = true;
  355. if (input_action == INPUT_SELECT || input_action == INPUT_PASTE) {
  356. cursor_visible = false;
  357. }
  358. _update_cursor_transform();
  359. }
  360. if (input_action == INPUT_PASTE) {
  361. paste_indicator.current = Vector3i(cell[0], cell[1], cell[2]);
  362. _update_paste_indicator();
  363. } else if (input_action == INPUT_SELECT) {
  364. selection.current = Vector3i(cell[0], cell[1], cell[2]);
  365. if (p_click) {
  366. selection.click = selection.current;
  367. }
  368. selection.active = true;
  369. _validate_selection();
  370. return true;
  371. } else if (input_action == INPUT_PICK) {
  372. int item = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  373. if (item >= 0) {
  374. selected_palette = item;
  375. mesh_library_palette->set_current(item);
  376. update_palette();
  377. _update_cursor_instance();
  378. }
  379. return true;
  380. }
  381. if (input_action == INPUT_PAINT) {
  382. SetItem si;
  383. si.position = Vector3i(cell[0], cell[1], cell[2]);
  384. si.new_value = selected_palette;
  385. si.new_orientation = cursor_rot;
  386. si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  387. si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
  388. set_items.push_back(si);
  389. node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), selected_palette, cursor_rot);
  390. return true;
  391. } else if (input_action == INPUT_ERASE) {
  392. SetItem si;
  393. si.position = Vector3i(cell[0], cell[1], cell[2]);
  394. si.new_value = -1;
  395. si.new_orientation = 0;
  396. si.old_value = node->get_cell_item(Vector3i(cell[0], cell[1], cell[2]));
  397. si.old_orientation = node->get_cell_item_orientation(Vector3i(cell[0], cell[1], cell[2]));
  398. set_items.push_back(si);
  399. node->set_cell_item(Vector3i(cell[0], cell[1], cell[2]), -1);
  400. return true;
  401. }
  402. return false;
  403. }
  404. void GridMapEditor::_delete_selection() {
  405. if (!selection.active) {
  406. return;
  407. }
  408. undo_redo->create_action(TTR("GridMap Delete Selection"));
  409. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  410. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  411. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  412. Vector3i selected = Vector3i(i, j, k);
  413. undo_redo->add_do_method(node, "set_cell_item", selected, GridMap::INVALID_CELL_ITEM);
  414. undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
  415. }
  416. }
  417. }
  418. undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
  419. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  420. undo_redo->commit_action();
  421. }
  422. void GridMapEditor::_fill_selection() {
  423. if (!selection.active) {
  424. return;
  425. }
  426. undo_redo->create_action(TTR("GridMap Fill Selection"));
  427. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  428. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  429. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  430. Vector3i selected = Vector3i(i, j, k);
  431. undo_redo->add_do_method(node, "set_cell_item", selected, selected_palette, cursor_rot);
  432. undo_redo->add_undo_method(node, "set_cell_item", selected, node->get_cell_item(selected), node->get_cell_item_orientation(selected));
  433. }
  434. }
  435. }
  436. undo_redo->add_do_method(this, "_set_selection", !selection.active, selection.begin, selection.end);
  437. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  438. undo_redo->commit_action();
  439. }
  440. void GridMapEditor::_clear_clipboard_data() {
  441. for (const ClipboardItem &E : clipboard_items) {
  442. RenderingServer::get_singleton()->free(E.instance);
  443. }
  444. clipboard_items.clear();
  445. }
  446. void GridMapEditor::_set_clipboard_data() {
  447. _clear_clipboard_data();
  448. Ref<MeshLibrary> meshLibrary = node->get_mesh_library();
  449. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  450. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  451. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  452. Vector3i selected = Vector3i(i, j, k);
  453. int itm = node->get_cell_item(selected);
  454. if (itm == GridMap::INVALID_CELL_ITEM) {
  455. continue;
  456. }
  457. Ref<Mesh> mesh = meshLibrary->get_item_mesh(itm);
  458. ClipboardItem item;
  459. item.cell_item = itm;
  460. item.grid_offset = Vector3(selected) - selection.begin;
  461. item.orientation = node->get_cell_item_orientation(selected);
  462. item.instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());
  463. clipboard_items.push_back(item);
  464. }
  465. }
  466. }
  467. }
  468. void GridMapEditor::_update_paste_indicator() {
  469. if (input_action != INPUT_PASTE) {
  470. Transform3D xf;
  471. xf.basis.set_zero();
  472. RenderingServer::get_singleton()->instance_set_transform(paste_instance, xf);
  473. return;
  474. }
  475. Vector3 center = 0.5 * Vector3(real_t(node->get_center_x()), real_t(node->get_center_y()), real_t(node->get_center_z()));
  476. Vector3 scale = (Vector3(1, 1, 1) + (paste_indicator.end - paste_indicator.begin)) * node->get_cell_size();
  477. Transform3D xf;
  478. xf.scale(scale);
  479. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  480. Basis rot;
  481. rot.set_orthogonal_index(paste_indicator.orientation);
  482. xf.basis = rot * xf.basis;
  483. xf.translate((-center * node->get_cell_size()) / scale);
  484. RenderingServer::get_singleton()->instance_set_transform(paste_instance, node->get_global_transform() * xf);
  485. for (const ClipboardItem &item : clipboard_items) {
  486. xf = Transform3D();
  487. xf.origin = (paste_indicator.begin + (paste_indicator.current - paste_indicator.click) + center) * node->get_cell_size();
  488. xf.basis = rot * xf.basis;
  489. xf.translate(item.grid_offset * node->get_cell_size());
  490. Basis item_rot;
  491. item_rot.set_orthogonal_index(item.orientation);
  492. xf.basis = item_rot * xf.basis * node->get_cell_scale();
  493. RenderingServer::get_singleton()->instance_set_transform(item.instance, node->get_global_transform() * xf);
  494. }
  495. }
  496. void GridMapEditor::_do_paste() {
  497. int idx = options->get_popup()->get_item_index(MENU_OPTION_PASTE_SELECTS);
  498. bool reselect = options->get_popup()->is_item_checked(idx);
  499. Basis rot;
  500. rot.set_orthogonal_index(paste_indicator.orientation);
  501. Vector3 ofs = paste_indicator.current - paste_indicator.click;
  502. undo_redo->create_action(TTR("GridMap Paste Selection"));
  503. for (const ClipboardItem &item : clipboard_items) {
  504. Vector3 position = rot.xform(item.grid_offset) + paste_indicator.begin + ofs;
  505. Basis orm;
  506. orm.set_orthogonal_index(item.orientation);
  507. orm = rot * orm;
  508. undo_redo->add_do_method(node, "set_cell_item", position, item.cell_item, orm.get_orthogonal_index());
  509. undo_redo->add_undo_method(node, "set_cell_item", position, node->get_cell_item(position), node->get_cell_item_orientation(position));
  510. }
  511. if (reselect) {
  512. undo_redo->add_do_method(this, "_set_selection", true, paste_indicator.begin + ofs, paste_indicator.end + ofs);
  513. undo_redo->add_undo_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  514. }
  515. undo_redo->commit_action();
  516. _clear_clipboard_data();
  517. }
  518. EditorPlugin::AfterGUIInput GridMapEditor::forward_spatial_input_event(Camera3D *p_camera, const Ref<InputEvent> &p_event) {
  519. if (!node) {
  520. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  521. }
  522. Ref<InputEventMouseButton> mb = p_event;
  523. if (mb.is_valid()) {
  524. if (mb->get_button_index() == MouseButton::WHEEL_UP && (mb->is_command_pressed() || mb->is_shift_pressed())) {
  525. if (mb->is_pressed()) {
  526. floor->set_value(floor->get_value() + mb->get_factor());
  527. }
  528. return EditorPlugin::AFTER_GUI_INPUT_STOP; // Eaten.
  529. } else if (mb->get_button_index() == MouseButton::WHEEL_DOWN && (mb->is_command_pressed() || mb->is_shift_pressed())) {
  530. if (mb->is_pressed()) {
  531. floor->set_value(floor->get_value() - mb->get_factor());
  532. }
  533. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  534. }
  535. if (mb->is_pressed()) {
  536. Node3DEditorViewport::NavigationScheme nav_scheme = (Node3DEditorViewport::NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  537. if ((nav_scheme == Node3DEditorViewport::NAVIGATION_MAYA || nav_scheme == Node3DEditorViewport::NAVIGATION_MODO) && mb->is_alt_pressed()) {
  538. input_action = INPUT_NONE;
  539. } else if (mb->get_button_index() == MouseButton::LEFT) {
  540. bool can_edit = (node && node->get_mesh_library().is_valid());
  541. if (input_action == INPUT_PASTE) {
  542. _do_paste();
  543. input_action = INPUT_NONE;
  544. _update_paste_indicator();
  545. } else if (mb->is_shift_pressed() && can_edit) {
  546. input_action = INPUT_SELECT;
  547. last_selection = selection;
  548. } else if (mb->is_command_pressed() && can_edit) {
  549. input_action = INPUT_PICK;
  550. } else {
  551. input_action = INPUT_PAINT;
  552. set_items.clear();
  553. }
  554. } else if (mb->get_button_index() == MouseButton::RIGHT) {
  555. if (input_action == INPUT_PASTE) {
  556. _clear_clipboard_data();
  557. input_action = INPUT_NONE;
  558. _update_paste_indicator();
  559. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  560. } else if (selection.active) {
  561. _set_selection(false);
  562. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  563. } else {
  564. input_action = INPUT_ERASE;
  565. set_items.clear();
  566. }
  567. } else {
  568. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  569. }
  570. if (do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true)) {
  571. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  572. }
  573. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  574. } else {
  575. if ((mb->get_button_index() == MouseButton::RIGHT && input_action == INPUT_ERASE) || (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_PAINT)) {
  576. if (set_items.size()) {
  577. undo_redo->create_action(TTR("GridMap Paint"));
  578. for (const SetItem &si : set_items) {
  579. undo_redo->add_do_method(node, "set_cell_item", si.position, si.new_value, si.new_orientation);
  580. }
  581. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  582. const SetItem &si = E->get();
  583. undo_redo->add_undo_method(node, "set_cell_item", si.position, si.old_value, si.old_orientation);
  584. }
  585. undo_redo->commit_action();
  586. }
  587. set_items.clear();
  588. input_action = INPUT_NONE;
  589. if (set_items.size() > 0) {
  590. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  591. }
  592. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  593. }
  594. if (mb->get_button_index() == MouseButton::LEFT && input_action == INPUT_SELECT) {
  595. undo_redo->create_action(TTR("GridMap Selection"));
  596. undo_redo->add_do_method(this, "_set_selection", selection.active, selection.begin, selection.end);
  597. undo_redo->add_undo_method(this, "_set_selection", last_selection.active, last_selection.begin, last_selection.end);
  598. undo_redo->commit_action();
  599. }
  600. if (mb->get_button_index() == MouseButton::LEFT && input_action != INPUT_NONE) {
  601. set_items.clear();
  602. input_action = INPUT_NONE;
  603. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  604. }
  605. if (mb->get_button_index() == MouseButton::RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_PASTE)) {
  606. input_action = INPUT_NONE;
  607. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  608. }
  609. }
  610. }
  611. Ref<InputEventMouseMotion> mm = p_event;
  612. if (mm.is_valid()) {
  613. if (do_input_action(p_camera, mm->get_position(), false)) {
  614. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  615. }
  616. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  617. }
  618. Ref<InputEventKey> k = p_event;
  619. if (k.is_valid()) {
  620. if (k->is_pressed()) {
  621. if (k->get_keycode() == Key::ESCAPE) {
  622. if (input_action == INPUT_PASTE) {
  623. _clear_clipboard_data();
  624. input_action = INPUT_NONE;
  625. _update_paste_indicator();
  626. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  627. } else if (selection.active) {
  628. _set_selection(false);
  629. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  630. } else {
  631. selected_palette = -1;
  632. mesh_library_palette->deselect_all();
  633. update_palette();
  634. _update_cursor_instance();
  635. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  636. }
  637. }
  638. if (k->is_shift_pressed() && selection.active && input_action != INPUT_PASTE) {
  639. if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_PREV_LEVEL))) {
  640. selection.click[edit_axis]--;
  641. _validate_selection();
  642. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  643. }
  644. if (k->get_keycode() == (Key)options->get_popup()->get_item_accelerator(options->get_popup()->get_item_index(MENU_OPTION_NEXT_LEVEL))) {
  645. selection.click[edit_axis]++;
  646. _validate_selection();
  647. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  648. }
  649. }
  650. }
  651. }
  652. Ref<InputEventPanGesture> pan_gesture = p_event;
  653. if (pan_gesture.is_valid()) {
  654. if (pan_gesture->is_alt_pressed() && (pan_gesture->is_command_pressed() || pan_gesture->is_shift_pressed())) {
  655. const real_t delta = pan_gesture->get_delta().y * 0.5;
  656. accumulated_floor_delta += delta;
  657. int step = 0;
  658. if (ABS(accumulated_floor_delta) > 1.0) {
  659. step = SIGN(accumulated_floor_delta);
  660. accumulated_floor_delta -= step;
  661. }
  662. if (step) {
  663. floor->set_value(floor->get_value() + step);
  664. }
  665. return EditorPlugin::AFTER_GUI_INPUT_STOP;
  666. }
  667. }
  668. accumulated_floor_delta = 0.0;
  669. return EditorPlugin::AFTER_GUI_INPUT_PASS;
  670. }
  671. struct _CGMEItemSort {
  672. String name;
  673. int id = 0;
  674. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  675. };
  676. void GridMapEditor::_set_display_mode(int p_mode) {
  677. if (display_mode == p_mode) {
  678. return;
  679. }
  680. if (p_mode == DISPLAY_LIST) {
  681. mode_list->set_pressed(true);
  682. mode_thumbnail->set_pressed(false);
  683. } else if (p_mode == DISPLAY_THUMBNAIL) {
  684. mode_list->set_pressed(false);
  685. mode_thumbnail->set_pressed(true);
  686. }
  687. display_mode = p_mode;
  688. update_palette();
  689. }
  690. void GridMapEditor::_text_changed(const String &p_text) {
  691. update_palette();
  692. }
  693. void GridMapEditor::_sbox_input(const Ref<InputEvent> &p_ie) {
  694. const Ref<InputEventKey> k = p_ie;
  695. if (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::PAGEUP || k->get_keycode() == Key::PAGEDOWN)) {
  696. // Forward the key input to the ItemList so it can be scrolled
  697. mesh_library_palette->gui_input(k);
  698. search_box->accept_event();
  699. }
  700. }
  701. void GridMapEditor::_mesh_library_palette_input(const Ref<InputEvent> &p_ie) {
  702. const Ref<InputEventMouseButton> mb = p_ie;
  703. // Zoom in/out using Ctrl + mouse wheel
  704. if (mb.is_valid() && mb->is_pressed() && mb->is_command_pressed()) {
  705. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) {
  706. size_slider->set_value(size_slider->get_value() + 0.2);
  707. }
  708. if (mb->is_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
  709. size_slider->set_value(size_slider->get_value() - 0.2);
  710. }
  711. }
  712. }
  713. void GridMapEditor::_icon_size_changed(float p_value) {
  714. mesh_library_palette->set_icon_scale(p_value);
  715. update_palette();
  716. }
  717. void GridMapEditor::update_palette() {
  718. int selected = mesh_library_palette->get_current();
  719. float min_size = EDITOR_DEF("editors/grid_map/preview_size", 64);
  720. min_size *= EDSCALE;
  721. mesh_library_palette->clear();
  722. if (display_mode == DISPLAY_THUMBNAIL) {
  723. mesh_library_palette->set_max_columns(0);
  724. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_TOP);
  725. mesh_library_palette->set_fixed_column_width(min_size * MAX(size_slider->get_value(), 1.5));
  726. } else if (display_mode == DISPLAY_LIST) {
  727. mesh_library_palette->set_max_columns(1);
  728. mesh_library_palette->set_icon_mode(ItemList::ICON_MODE_LEFT);
  729. mesh_library_palette->set_fixed_column_width(0);
  730. }
  731. mesh_library_palette->set_fixed_icon_size(Size2(min_size, min_size));
  732. mesh_library_palette->set_max_text_lines(2);
  733. Ref<MeshLibrary> mesh_library = node->get_mesh_library();
  734. if (mesh_library.is_null()) {
  735. last_mesh_library = nullptr;
  736. search_box->set_text("");
  737. search_box->set_editable(false);
  738. info_message->show();
  739. return;
  740. }
  741. search_box->set_editable(true);
  742. info_message->hide();
  743. Vector<int> ids;
  744. ids = mesh_library->get_item_list();
  745. List<_CGMEItemSort> il;
  746. for (int i = 0; i < ids.size(); i++) {
  747. _CGMEItemSort is;
  748. is.id = ids[i];
  749. is.name = mesh_library->get_item_name(ids[i]);
  750. il.push_back(is);
  751. }
  752. il.sort();
  753. String filter = search_box->get_text().strip_edges();
  754. int item = 0;
  755. for (_CGMEItemSort &E : il) {
  756. int id = E.id;
  757. String name = mesh_library->get_item_name(id);
  758. Ref<Texture2D> preview = mesh_library->get_item_preview(id);
  759. if (name.is_empty()) {
  760. name = "#" + itos(id);
  761. }
  762. if (!filter.is_empty() && !filter.is_subsequence_ofn(name)) {
  763. continue;
  764. }
  765. mesh_library_palette->add_item("");
  766. if (!preview.is_null()) {
  767. mesh_library_palette->set_item_icon(item, preview);
  768. mesh_library_palette->set_item_tooltip(item, name);
  769. }
  770. mesh_library_palette->set_item_text(item, name);
  771. mesh_library_palette->set_item_metadata(item, id);
  772. item++;
  773. }
  774. if (selected != -1 && mesh_library_palette->get_item_count() > 0) {
  775. mesh_library_palette->select(selected);
  776. }
  777. last_mesh_library = mesh_library.operator->();
  778. }
  779. void GridMapEditor::edit(GridMap *p_gridmap) {
  780. if (!p_gridmap && node) {
  781. node->disconnect("cell_size_changed", callable_mp(this, &GridMapEditor::_draw_grids));
  782. }
  783. node = p_gridmap;
  784. input_action = INPUT_NONE;
  785. selection.active = false;
  786. _update_selection_transform();
  787. _update_paste_indicator();
  788. spatial_editor = Object::cast_to<Node3DEditorPlugin>(EditorNode::get_singleton()->get_editor_plugin_screen());
  789. if (!node) {
  790. set_process(false);
  791. for (int i = 0; i < 3; i++) {
  792. RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  793. }
  794. if (cursor_instance.is_valid()) {
  795. RenderingServer::get_singleton()->instance_set_visible(cursor_instance, false);
  796. }
  797. return;
  798. }
  799. update_palette();
  800. set_process(true);
  801. clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
  802. _draw_grids(node->get_cell_size());
  803. update_grid();
  804. _update_clip();
  805. node->connect("cell_size_changed", callable_mp(this, &GridMapEditor::_draw_grids));
  806. }
  807. void GridMapEditor::_update_clip() {
  808. node->set_meta("_editor_clip_", clip_mode);
  809. if (clip_mode == CLIP_DISABLED) {
  810. node->set_clip(false);
  811. } else {
  812. node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
  813. }
  814. }
  815. void GridMapEditor::update_grid() {
  816. grid_xform.origin.x -= 1; // Force update in hackish way.
  817. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size()[edit_axis];
  818. edit_grid_xform.origin = grid_ofs;
  819. edit_grid_xform.basis = Basis();
  820. for (int i = 0; i < 3; i++) {
  821. RenderingServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  822. }
  823. updating = true;
  824. floor->set_value(edit_floor[edit_axis]);
  825. updating = false;
  826. }
  827. void GridMapEditor::_draw_grids(const Vector3 &cell_size) {
  828. Vector3 edited_floor = node->has_meta("_editor_floor_") ? node->get_meta("_editor_floor_") : Variant();
  829. for (int i = 0; i < 3; i++) {
  830. RS::get_singleton()->mesh_clear(grid[i]);
  831. edit_floor[i] = edited_floor[i];
  832. }
  833. Vector<Vector3> grid_points[3];
  834. Vector<Color> grid_colors[3];
  835. for (int i = 0; i < 3; i++) {
  836. Vector3 axis;
  837. axis[i] = 1;
  838. Vector3 axis_n1;
  839. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  840. Vector3 axis_n2;
  841. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  842. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  843. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  844. Vector3 p = axis_n1 * j + axis_n2 * k;
  845. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  846. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  847. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  848. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  849. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  850. grid_points[i].push_back(p);
  851. grid_points[i].push_back(pk);
  852. grid_colors[i].push_back(Color(1, 1, 1, trans));
  853. grid_colors[i].push_back(Color(1, 1, 1, transk));
  854. grid_points[i].push_back(p);
  855. grid_points[i].push_back(pj);
  856. grid_colors[i].push_back(Color(1, 1, 1, trans));
  857. grid_colors[i].push_back(Color(1, 1, 1, transj));
  858. }
  859. }
  860. Array d;
  861. d.resize(RS::ARRAY_MAX);
  862. d[RS::ARRAY_VERTEX] = grid_points[i];
  863. d[RS::ARRAY_COLOR] = grid_colors[i];
  864. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], RenderingServer::PRIMITIVE_LINES, d);
  865. RenderingServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  866. }
  867. }
  868. void GridMapEditor::_update_theme() {
  869. options->set_icon(get_theme_icon(SNAME("GridMap"), SNAME("EditorIcons")));
  870. search_box->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  871. mode_thumbnail->set_icon(get_theme_icon(SNAME("FileThumbnail"), SNAME("EditorIcons")));
  872. mode_list->set_icon(get_theme_icon(SNAME("FileList"), SNAME("EditorIcons")));
  873. }
  874. void GridMapEditor::_notification(int p_what) {
  875. switch (p_what) {
  876. case NOTIFICATION_ENTER_TREE: {
  877. get_tree()->connect("node_removed", callable_mp(this, &GridMapEditor::_node_removed));
  878. mesh_library_palette->connect("item_selected", callable_mp(this, &GridMapEditor::_item_selected_cbk));
  879. for (int i = 0; i < 3; i++) {
  880. grid[i] = RS::get_singleton()->mesh_create();
  881. grid_instance[i] = RS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  882. RenderingServer::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  883. selection_level_instance[i] = RenderingServer::get_singleton()->instance_create2(selection_level_mesh[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  884. RenderingServer::get_singleton()->instance_set_layer_mask(selection_level_instance[i], 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  885. }
  886. selection_instance = RenderingServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  887. RenderingServer::get_singleton()->instance_set_layer_mask(selection_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  888. paste_instance = RenderingServer::get_singleton()->instance_create2(paste_mesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  889. RenderingServer::get_singleton()->instance_set_layer_mask(paste_instance, 1 << Node3DEditorViewport::MISC_TOOL_LAYER);
  890. _update_selection_transform();
  891. _update_paste_indicator();
  892. _update_theme();
  893. } break;
  894. case NOTIFICATION_EXIT_TREE: {
  895. get_tree()->disconnect("node_removed", callable_mp(this, &GridMapEditor::_node_removed));
  896. _clear_clipboard_data();
  897. for (int i = 0; i < 3; i++) {
  898. RS::get_singleton()->free(grid_instance[i]);
  899. RS::get_singleton()->free(grid[i]);
  900. grid_instance[i] = RID();
  901. grid[i] = RID();
  902. RenderingServer::get_singleton()->free(selection_level_instance[i]);
  903. }
  904. RenderingServer::get_singleton()->free(selection_instance);
  905. RenderingServer::get_singleton()->free(paste_instance);
  906. selection_instance = RID();
  907. paste_instance = RID();
  908. } break;
  909. case NOTIFICATION_PROCESS: {
  910. if (!node) {
  911. return;
  912. }
  913. Transform3D xf = node->get_global_transform();
  914. if (xf != grid_xform) {
  915. for (int i = 0; i < 3; i++) {
  916. RS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  917. }
  918. grid_xform = xf;
  919. }
  920. Ref<MeshLibrary> cgmt = node->get_mesh_library();
  921. if (cgmt.operator->() != last_mesh_library) {
  922. update_palette();
  923. }
  924. } break;
  925. case NOTIFICATION_THEME_CHANGED: {
  926. _update_theme();
  927. } break;
  928. case NOTIFICATION_APPLICATION_FOCUS_OUT: {
  929. if (input_action == INPUT_PAINT) {
  930. // Simulate mouse released event to stop drawing when editor focus exists.
  931. Ref<InputEventMouseButton> release;
  932. release.instantiate();
  933. release->set_button_index(MouseButton::LEFT);
  934. forward_spatial_input_event(nullptr, release);
  935. }
  936. } break;
  937. }
  938. }
  939. void GridMapEditor::_update_cursor_instance() {
  940. if (!node) {
  941. return;
  942. }
  943. if (cursor_instance.is_valid()) {
  944. RenderingServer::get_singleton()->free(cursor_instance);
  945. }
  946. cursor_instance = RID();
  947. if (selected_palette >= 0) {
  948. if (node && !node->get_mesh_library().is_null()) {
  949. Ref<Mesh> mesh = node->get_mesh_library()->get_item_mesh(selected_palette);
  950. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  951. cursor_instance = RenderingServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world_3d()->get_scenario());
  952. RenderingServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  953. }
  954. }
  955. }
  956. }
  957. void GridMapEditor::_item_selected_cbk(int idx) {
  958. selected_palette = mesh_library_palette->get_item_metadata(idx);
  959. _update_cursor_instance();
  960. }
  961. void GridMapEditor::_floor_changed(float p_value) {
  962. if (updating) {
  963. return;
  964. }
  965. edit_floor[edit_axis] = p_value;
  966. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  967. update_grid();
  968. _update_clip();
  969. _update_selection_transform();
  970. }
  971. void GridMapEditor::_floor_mouse_exited() {
  972. floor->get_line_edit()->release_focus();
  973. }
  974. void GridMapEditor::_bind_methods() {
  975. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  976. ClassDB::bind_method("_set_selection", &GridMapEditor::_set_selection);
  977. }
  978. GridMapEditor::GridMapEditor() {
  979. undo_redo = EditorNode::get_singleton()->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("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. switch (p_what) {
  1224. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1225. switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
  1226. case 0: { // Left.
  1227. Node3DEditor::get_singleton()->move_control_to_left_panel(grid_map_editor);
  1228. } break;
  1229. case 1: { // Right.
  1230. Node3DEditor::get_singleton()->move_control_to_right_panel(grid_map_editor);
  1231. } break;
  1232. }
  1233. } break;
  1234. }
  1235. }
  1236. void GridMapEditorPlugin::edit(Object *p_object) {
  1237. grid_map_editor->edit(Object::cast_to<GridMap>(p_object));
  1238. }
  1239. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1240. return p_object->is_class("GridMap");
  1241. }
  1242. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1243. if (p_visible) {
  1244. grid_map_editor->show();
  1245. grid_map_editor->spatial_editor_hb->show();
  1246. grid_map_editor->set_process(true);
  1247. } else {
  1248. grid_map_editor->spatial_editor_hb->hide();
  1249. grid_map_editor->hide();
  1250. grid_map_editor->edit(nullptr);
  1251. grid_map_editor->set_process(false);
  1252. }
  1253. }
  1254. GridMapEditorPlugin::GridMapEditorPlugin() {
  1255. EDITOR_DEF("editors/grid_map/editor_side", 1);
  1256. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/grid_map/editor_side", PROPERTY_HINT_ENUM, "Left,Right"));
  1257. grid_map_editor = memnew(GridMapEditor);
  1258. switch ((int)EditorSettings::get_singleton()->get("editors/grid_map/editor_side")) {
  1259. case 0: { // Left.
  1260. Node3DEditor::get_singleton()->add_control_to_left_panel(grid_map_editor);
  1261. } break;
  1262. case 1: { // Right.
  1263. Node3DEditor::get_singleton()->add_control_to_right_panel(grid_map_editor);
  1264. } break;
  1265. }
  1266. grid_map_editor->hide();
  1267. }
  1268. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1269. }