grid_map_editor_plugin.cpp 45 KB

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