grid_map_editor_plugin.cpp 52 KB

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