grid_map_editor_plugin.cpp 51 KB

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