grid_map_editor_plugin.cpp 50 KB

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