grid_map_editor_plugin.cpp 53 KB

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