grid_map_editor_plugin.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. /*************************************************************************/
  2. /* grid_map_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "grid_map_editor_plugin.h"
  30. #include "editor/editor_settings.h"
  31. #include "editor/plugins/spatial_editor_plugin.h"
  32. #include "scene/3d/camera.h"
  33. #include "geometry.h"
  34. #include "os/keyboard.h"
  35. void GridMapEditor::_node_removed(Node *p_node) {
  36. if (p_node == node) {
  37. node = NULL;
  38. hide();
  39. theme_pallete->hide();
  40. }
  41. }
  42. void GridMapEditor::_configure() {
  43. if (!node)
  44. return;
  45. update_grid();
  46. }
  47. void GridMapEditor::_menu_option(int p_option) {
  48. switch (p_option) {
  49. case MENU_OPTION_CONFIGURE: {
  50. } break;
  51. case MENU_OPTION_LOCK_VIEW: {
  52. int index = options->get_popup()->get_item_index(MENU_OPTION_LOCK_VIEW);
  53. lock_view = !options->get_popup()->is_item_checked(index);
  54. options->get_popup()->set_item_checked(index, lock_view);
  55. } break;
  56. case MENU_OPTION_CLIP_DISABLED:
  57. case MENU_OPTION_CLIP_ABOVE:
  58. case MENU_OPTION_CLIP_BELOW: {
  59. clip_mode = ClipMode(p_option - MENU_OPTION_CLIP_DISABLED);
  60. for (int i = 0; i < 3; i++) {
  61. int index = options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED + i);
  62. options->get_popup()->set_item_checked(index, i == clip_mode);
  63. }
  64. _update_clip();
  65. } break;
  66. case MENU_OPTION_X_AXIS:
  67. case MENU_OPTION_Y_AXIS:
  68. case MENU_OPTION_Z_AXIS: {
  69. int new_axis = p_option - MENU_OPTION_X_AXIS;
  70. for (int i = 0; i < 3; i++) {
  71. int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i);
  72. options->get_popup()->set_item_checked(idx, i == new_axis);
  73. }
  74. edit_axis = Vector3::Axis(new_axis);
  75. update_grid();
  76. _update_clip();
  77. } break;
  78. case MENU_OPTION_CURSOR_ROTATE_Y: {
  79. Basis r;
  80. if (input_action == INPUT_DUPLICATE) {
  81. r.set_orthogonal_index(selection.duplicate_rot);
  82. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  83. selection.duplicate_rot = r.get_orthogonal_index();
  84. _update_duplicate_indicator();
  85. break;
  86. }
  87. r.set_orthogonal_index(cursor_rot);
  88. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  89. cursor_rot = r.get_orthogonal_index();
  90. _update_cursor_transform();
  91. } break;
  92. case MENU_OPTION_CURSOR_ROTATE_X: {
  93. Basis r;
  94. if (input_action == INPUT_DUPLICATE) {
  95. r.set_orthogonal_index(selection.duplicate_rot);
  96. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  97. selection.duplicate_rot = r.get_orthogonal_index();
  98. _update_duplicate_indicator();
  99. break;
  100. }
  101. r.set_orthogonal_index(cursor_rot);
  102. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  103. cursor_rot = r.get_orthogonal_index();
  104. _update_cursor_transform();
  105. } break;
  106. case MENU_OPTION_CURSOR_ROTATE_Z: {
  107. Basis r;
  108. if (input_action == INPUT_DUPLICATE) {
  109. r.set_orthogonal_index(selection.duplicate_rot);
  110. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  111. selection.duplicate_rot = r.get_orthogonal_index();
  112. _update_duplicate_indicator();
  113. break;
  114. }
  115. r.set_orthogonal_index(cursor_rot);
  116. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  117. cursor_rot = r.get_orthogonal_index();
  118. _update_cursor_transform();
  119. } break;
  120. case MENU_OPTION_CURSOR_BACK_ROTATE_Y: {
  121. Basis r;
  122. r.set_orthogonal_index(cursor_rot);
  123. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  124. cursor_rot = r.get_orthogonal_index();
  125. _update_cursor_transform();
  126. } break;
  127. case MENU_OPTION_CURSOR_BACK_ROTATE_X: {
  128. Basis r;
  129. r.set_orthogonal_index(cursor_rot);
  130. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  131. cursor_rot = r.get_orthogonal_index();
  132. _update_cursor_transform();
  133. } break;
  134. case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
  135. Basis r;
  136. r.set_orthogonal_index(cursor_rot);
  137. r.rotate(Vector3(0, 0, 1), Math_PI / 2.0);
  138. cursor_rot = r.get_orthogonal_index();
  139. _update_cursor_transform();
  140. } break;
  141. case MENU_OPTION_CURSOR_CLEAR_ROTATION: {
  142. if (input_action == INPUT_DUPLICATE) {
  143. selection.duplicate_rot = 0;
  144. _update_duplicate_indicator();
  145. break;
  146. }
  147. cursor_rot = 0;
  148. _update_cursor_transform();
  149. } break;
  150. case MENU_OPTION_DUPLICATE_SELECTS: {
  151. int idx = options->get_popup()->get_item_index(MENU_OPTION_DUPLICATE_SELECTS);
  152. options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
  153. } break;
  154. case MENU_OPTION_SELECTION_MAKE_AREA:
  155. case MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR: {
  156. if (!selection.active)
  157. break;
  158. int area = node->get_unused_area_id();
  159. Error err = node->create_area(area, Rect3(selection.begin, selection.end - selection.begin + Vector3(1, 1, 1)));
  160. if (err != OK) {
  161. }
  162. if (p_option == MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR) {
  163. node->area_set_exterior_portal(area, true);
  164. }
  165. _update_areas_display();
  166. update_areas();
  167. } break;
  168. case MENU_OPTION_REMOVE_AREA: {
  169. if (selected_area < 1)
  170. return;
  171. node->erase_area(selected_area);
  172. _update_areas_display();
  173. update_areas();
  174. } break;
  175. case MENU_OPTION_SELECTION_DUPLICATE:
  176. if (!(selection.active && input_action == INPUT_NONE))
  177. return;
  178. if (last_mouseover == Vector3(-1, -1, -1)) //nono mouseovering anythin
  179. break;
  180. input_action = INPUT_DUPLICATE;
  181. selection.click = last_mouseover;
  182. selection.current = last_mouseover;
  183. selection.duplicate_rot = 0;
  184. _update_duplicate_indicator();
  185. break;
  186. case MENU_OPTION_SELECTION_CLEAR: {
  187. if (!selection.active)
  188. return;
  189. _delete_selection();
  190. } break;
  191. case MENU_OPTION_GRIDMAP_SETTINGS: {
  192. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  193. } break;
  194. }
  195. }
  196. void GridMapEditor::_update_cursor_transform() {
  197. cursor_transform = Transform();
  198. cursor_transform.origin = cursor_origin;
  199. cursor_transform.basis.set_orthogonal_index(cursor_rot);
  200. cursor_transform = node->get_transform() * cursor_transform;
  201. if (cursor_instance.is_valid()) {
  202. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  203. VisualServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
  204. }
  205. }
  206. void GridMapEditor::_update_selection_transform() {
  207. if (!selection.active) {
  208. Transform xf;
  209. xf.basis.set_zero();
  210. VisualServer::get_singleton()->instance_set_transform(selection_instance, xf);
  211. return;
  212. }
  213. Transform xf;
  214. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  215. xf.origin = selection.begin * node->get_cell_size();
  216. VisualServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  217. }
  218. void GridMapEditor::_validate_selection() {
  219. if (!selection.active)
  220. return;
  221. selection.begin = selection.click;
  222. selection.end = selection.current;
  223. if (selection.begin.x > selection.end.x)
  224. SWAP(selection.begin.x, selection.end.x);
  225. if (selection.begin.y > selection.end.y)
  226. SWAP(selection.begin.y, selection.end.y);
  227. if (selection.begin.z > selection.end.z)
  228. SWAP(selection.begin.z, selection.end.z);
  229. _update_selection_transform();
  230. }
  231. bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click) {
  232. if (!spatial_editor)
  233. return false;
  234. if (selected_pallete < 0 && input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE)
  235. return false;
  236. Ref<MeshLibrary> theme = node->get_theme();
  237. if (theme.is_null())
  238. return false;
  239. if (input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE && !theme->has_item(selected_pallete))
  240. return false;
  241. Camera *camera = p_camera;
  242. Vector3 from = camera->project_ray_origin(p_point);
  243. Vector3 normal = camera->project_ray_normal(p_point);
  244. Transform local_xform = node->get_global_transform().affine_inverse();
  245. Vector<Plane> planes = camera->get_frustum();
  246. from = local_xform.xform(from);
  247. normal = local_xform.basis.xform(normal).normalized();
  248. Plane p;
  249. p.normal[edit_axis] = 1.0;
  250. p.d = edit_floor[edit_axis] * node->get_cell_size();
  251. Vector3 inters;
  252. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters))
  253. return false;
  254. //make sure the intersection is inside the frustum planes, to avoid
  255. //painting on invisible regions
  256. for (int i = 0; i < planes.size(); i++) {
  257. Plane fp = local_xform.xform(planes[i]);
  258. if (fp.is_point_over(inters))
  259. return false;
  260. }
  261. int cell[3];
  262. float cell_size[3] = { node->get_cell_size(), node->get_cell_size(), node->get_cell_size() };
  263. last_mouseover = Vector3(-1, -1, -1);
  264. for (int i = 0; i < 3; i++) {
  265. if (i == edit_axis)
  266. cell[i] = edit_floor[i];
  267. else {
  268. cell[i] = inters[i] / node->get_cell_size();
  269. if (inters[i] < 0)
  270. cell[i] -= 1; //compensate negative
  271. grid_ofs[i] = cell[i] * cell_size[i];
  272. }
  273. /*if (cell[i]<0 || cell[i]>=grid_size[i]) {
  274. cursor_visible=false;
  275. _update_cursor_transform();
  276. return false;
  277. }*/
  278. }
  279. last_mouseover = Vector3(cell[0], cell[1], cell[2]);
  280. VS::get_singleton()->instance_set_transform(grid_instance[edit_axis], Transform(Basis(), grid_ofs));
  281. if (cursor_instance.is_valid()) {
  282. 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();
  283. cursor_visible = true;
  284. _update_cursor_transform();
  285. }
  286. if (input_action == INPUT_DUPLICATE) {
  287. selection.current = Vector3(cell[0], cell[1], cell[2]);
  288. _update_duplicate_indicator();
  289. } else if (input_action == INPUT_SELECT) {
  290. selection.current = Vector3(cell[0], cell[1], cell[2]);
  291. if (p_click)
  292. selection.click = selection.current;
  293. selection.active = true;
  294. _validate_selection();
  295. return true;
  296. } else if (input_action == INPUT_COPY) {
  297. int item = node->get_cell_item(cell[0], cell[1], cell[2]);
  298. if (item >= 0) {
  299. selected_pallete = item;
  300. theme_pallete->set_current(item);
  301. update_pallete();
  302. _update_cursor_instance();
  303. }
  304. return true;
  305. }
  306. if (input_action == INPUT_PAINT) {
  307. SetItem si;
  308. si.pos = Vector3(cell[0], cell[1], cell[2]);
  309. si.new_value = selected_pallete;
  310. si.new_orientation = cursor_rot;
  311. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  312. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  313. set_items.push_back(si);
  314. node->set_cell_item(cell[0], cell[1], cell[2], selected_pallete, cursor_rot);
  315. return true;
  316. } else if (input_action == INPUT_ERASE) {
  317. SetItem si;
  318. si.pos = Vector3(cell[0], cell[1], cell[2]);
  319. si.new_value = -1;
  320. si.new_orientation = 0;
  321. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  322. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  323. set_items.push_back(si);
  324. node->set_cell_item(cell[0], cell[1], cell[2], -1);
  325. return true;
  326. }
  327. return false;
  328. }
  329. void GridMapEditor::_delete_selection() {
  330. if (!selection.active)
  331. return;
  332. undo_redo->create_action("GridMap Delete Selection");
  333. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  334. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  335. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  336. undo_redo->add_do_method(node, "set_cell_item", i, j, k, GridMap::INVALID_CELL_ITEM);
  337. 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));
  338. }
  339. }
  340. }
  341. undo_redo->commit_action();
  342. selection.active = false;
  343. _validate_selection();
  344. }
  345. void GridMapEditor::_update_duplicate_indicator() {
  346. if (!selection.active || input_action != INPUT_DUPLICATE) {
  347. Transform xf;
  348. xf.basis.set_zero();
  349. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, xf);
  350. return;
  351. }
  352. Transform xf;
  353. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  354. xf.origin = (selection.begin + (selection.current - selection.click)) * node->get_cell_size();
  355. Basis rot;
  356. rot.set_orthogonal_index(selection.duplicate_rot);
  357. xf.basis = rot * xf.basis;
  358. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, node->get_global_transform() * xf);
  359. }
  360. struct __Item {
  361. Vector3 pos;
  362. int rot;
  363. int item;
  364. };
  365. void GridMapEditor::_duplicate_paste() {
  366. if (!selection.active)
  367. return;
  368. int idx = options->get_popup()->get_item_index(MENU_OPTION_DUPLICATE_SELECTS);
  369. bool reselect = options->get_popup()->is_item_checked(idx);
  370. List<__Item> items;
  371. Basis rot;
  372. rot.set_orthogonal_index(selection.duplicate_rot);
  373. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  374. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  375. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  376. int itm = node->get_cell_item(i, j, k);
  377. if (itm == GridMap::INVALID_CELL_ITEM)
  378. continue;
  379. int orientation = node->get_cell_item_orientation(i, j, k);
  380. __Item item;
  381. Vector3 rel = Vector3(i, j, k) - selection.begin;
  382. rel = rot.xform(rel);
  383. Basis orm;
  384. orm.set_orthogonal_index(orientation);
  385. orm = rot * orm;
  386. item.pos = selection.begin + rel;
  387. item.item = itm;
  388. item.rot = orm.get_orthogonal_index();
  389. items.push_back(item);
  390. }
  391. }
  392. }
  393. Vector3 ofs = selection.current - selection.click;
  394. if (items.size()) {
  395. undo_redo->create_action("GridMap Duplicate Selection");
  396. for (List<__Item>::Element *E = items.front(); E; E = E->next()) {
  397. __Item &it = E->get();
  398. Vector3 pos = it.pos + ofs;
  399. undo_redo->add_do_method(node, "set_cell_item", pos.x, pos.y, pos.z, it.item, it.rot);
  400. 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));
  401. }
  402. undo_redo->commit_action();
  403. }
  404. if (reselect) {
  405. selection.begin += ofs;
  406. selection.end += ofs;
  407. selection.click = selection.begin;
  408. selection.current = selection.end;
  409. _validate_selection();
  410. }
  411. }
  412. bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const InputEvent &p_event) {
  413. if (!node) {
  414. return false;
  415. }
  416. if (edit_mode->get_selected() == 0) { // regular click
  417. switch (p_event.type) {
  418. case InputEvent::MOUSE_BUTTON: {
  419. if (p_event.mouse_button.button_index == BUTTON_WHEEL_UP && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
  420. if (p_event.mouse_button.pressed)
  421. floor->set_value(floor->get_value() + 1);
  422. return true; //eaten
  423. } else if (p_event.mouse_button.button_index == BUTTON_WHEEL_DOWN && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
  424. if (p_event.mouse_button.pressed)
  425. floor->set_value(floor->get_value() - 1);
  426. return true;
  427. }
  428. if (p_event.mouse_button.pressed) {
  429. if (p_event.mouse_button.button_index == BUTTON_LEFT) {
  430. if (input_action == INPUT_DUPLICATE) {
  431. //paste
  432. _duplicate_paste();
  433. input_action = INPUT_NONE;
  434. _update_duplicate_indicator();
  435. } else if (p_event.mouse_button.mod.shift) {
  436. input_action = INPUT_SELECT;
  437. } else if (p_event.mouse_button.mod.command)
  438. input_action = INPUT_COPY;
  439. else {
  440. input_action = INPUT_PAINT;
  441. set_items.clear();
  442. }
  443. } else if (p_event.mouse_button.button_index == BUTTON_RIGHT)
  444. if (input_action == INPUT_DUPLICATE) {
  445. input_action = INPUT_NONE;
  446. _update_duplicate_indicator();
  447. } else {
  448. input_action = INPUT_ERASE;
  449. set_items.clear();
  450. }
  451. else
  452. return false;
  453. return do_input_action(p_camera, Point2(p_event.mouse_button.x, p_event.mouse_button.y), true);
  454. } else {
  455. if (
  456. (p_event.mouse_button.button_index == BUTTON_RIGHT && input_action == INPUT_ERASE) ||
  457. (p_event.mouse_button.button_index == BUTTON_LEFT && input_action == INPUT_PAINT)) {
  458. if (set_items.size()) {
  459. undo_redo->create_action("GridMap Paint");
  460. for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
  461. const SetItem &si = E->get();
  462. undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
  463. }
  464. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  465. const SetItem &si = E->get();
  466. undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
  467. }
  468. undo_redo->commit_action();
  469. }
  470. set_items.clear();
  471. input_action = INPUT_NONE;
  472. return true;
  473. }
  474. if (p_event.mouse_button.button_index == BUTTON_LEFT && input_action != INPUT_NONE) {
  475. set_items.clear();
  476. input_action = INPUT_NONE;
  477. return true;
  478. }
  479. if (p_event.mouse_button.button_index == BUTTON_RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_DUPLICATE)) {
  480. input_action = INPUT_NONE;
  481. return true;
  482. }
  483. }
  484. } break;
  485. case InputEvent::MOUSE_MOTION: {
  486. return do_input_action(p_camera, Point2(p_event.mouse_motion.x, p_event.mouse_motion.y), false);
  487. } break;
  488. }
  489. } else if (edit_mode->get_selected() == 1) {
  490. //area mode, select an area
  491. switch (p_event.type) {
  492. case InputEvent::MOUSE_BUTTON: {
  493. if (p_event.mouse_button.button_index == BUTTON_LEFT && p_event.mouse_button.pressed) {
  494. Point2 point = Point2(p_event.mouse_motion.x, p_event.mouse_motion.y);
  495. Camera *camera = p_camera;
  496. Vector3 from = camera->project_ray_origin(point);
  497. Vector3 normal = camera->project_ray_normal(point);
  498. Transform local_xform = node->get_global_transform().affine_inverse();
  499. from = local_xform.xform(from);
  500. normal = local_xform.basis.xform(normal).normalized();
  501. List<int> areas;
  502. node->get_area_list(&areas);
  503. float min_d = 1e10;
  504. int min_area = -1;
  505. for (List<int>::Element *E = areas.front(); E; E = E->next()) {
  506. int area = E->get();
  507. Rect3 aabb = node->area_get_bounds(area);
  508. aabb.pos *= node->get_cell_size();
  509. aabb.size *= node->get_cell_size();
  510. Vector3 rclip, rnormal;
  511. if (!aabb.intersects_segment(from, from + normal * 10000, &rclip, &rnormal))
  512. continue;
  513. float d = normal.dot(rclip);
  514. if (d < min_d) {
  515. min_d = d;
  516. min_area = area;
  517. }
  518. }
  519. selected_area = min_area;
  520. update_areas();
  521. }
  522. } break;
  523. }
  524. }
  525. return false;
  526. }
  527. struct _CGMEItemSort {
  528. String name;
  529. int id;
  530. _FORCE_INLINE_ bool operator<(const _CGMEItemSort &r_it) const { return name < r_it.name; }
  531. };
  532. void GridMapEditor::_set_display_mode(int p_mode) {
  533. if (display_mode == p_mode) {
  534. return;
  535. }
  536. if (p_mode == DISPLAY_LIST) {
  537. mode_list->set_pressed(true);
  538. mode_thumbnail->set_pressed(false);
  539. } else if (p_mode == DISPLAY_THUMBNAIL) {
  540. mode_list->set_pressed(false);
  541. mode_thumbnail->set_pressed(true);
  542. }
  543. display_mode = p_mode;
  544. update_pallete();
  545. }
  546. void GridMapEditor::update_pallete() {
  547. int selected = theme_pallete->get_current();
  548. theme_pallete->clear();
  549. if (display_mode == DISPLAY_THUMBNAIL) {
  550. theme_pallete->set_max_columns(0);
  551. theme_pallete->set_icon_mode(ItemList::ICON_MODE_TOP);
  552. } else if (display_mode == DISPLAY_LIST) {
  553. theme_pallete->set_max_columns(1);
  554. theme_pallete->set_icon_mode(ItemList::ICON_MODE_LEFT);
  555. }
  556. float min_size = EDITOR_DEF("editors/grid_map/preview_size", 64);
  557. theme_pallete->set_fixed_icon_size(Size2(min_size, min_size));
  558. theme_pallete->set_fixed_column_width(min_size * 3 / 2);
  559. theme_pallete->set_max_text_lines(2);
  560. Ref<MeshLibrary> theme = node->get_theme();
  561. if (theme.is_null()) {
  562. last_theme = NULL;
  563. return;
  564. }
  565. Vector<int> ids;
  566. ids = theme->get_item_list();
  567. List<_CGMEItemSort> il;
  568. for (int i = 0; i < ids.size(); i++) {
  569. _CGMEItemSort is;
  570. is.id = ids[i];
  571. is.name = theme->get_item_name(ids[i]);
  572. il.push_back(is);
  573. }
  574. il.sort();
  575. int item = 0;
  576. for (List<_CGMEItemSort>::Element *E = il.front(); E; E = E->next()) {
  577. int id = E->get().id;
  578. theme_pallete->add_item("");
  579. String name = theme->get_item_name(id);
  580. Ref<Texture> preview = theme->get_item_preview(id);
  581. if (!preview.is_null()) {
  582. theme_pallete->set_item_icon(item, preview);
  583. theme_pallete->set_item_tooltip(item, name);
  584. }
  585. if (name != "") {
  586. theme_pallete->set_item_text(item, name);
  587. }
  588. theme_pallete->set_item_metadata(item, id);
  589. item++;
  590. }
  591. if (selected != -1) {
  592. theme_pallete->select(selected);
  593. }
  594. last_theme = theme.operator->();
  595. }
  596. void GridMapEditor::_area_renamed() {
  597. TreeItem *it = area_list->get_selected();
  598. int area = it->get_metadata(0);
  599. if (area < 1)
  600. return;
  601. node->area_set_name(area, it->get_text(0));
  602. }
  603. void GridMapEditor::_area_selected() {
  604. TreeItem *it = area_list->get_selected();
  605. int area = it->get_metadata(0);
  606. if (area < 1)
  607. return;
  608. selected_area = area;
  609. }
  610. void GridMapEditor::update_areas() {
  611. area_list->clear();
  612. List<int> areas;
  613. node->get_area_list(&areas);
  614. TreeItem *root = area_list->create_item(NULL);
  615. area_list->set_hide_root(true);
  616. TreeItem *selected = NULL;
  617. for (List<int>::Element *E = areas.front(); E; E = E->next()) {
  618. int area = E->get();
  619. TreeItem *ti = area_list->create_item(root);
  620. String name = node->area_get_name(area);
  621. ti->set_metadata(0, area);
  622. ti->set_text(0, name);
  623. ti->set_editable(0, true);
  624. if (area == selected_area)
  625. selected = ti;
  626. }
  627. if (selected)
  628. selected->select(0);
  629. }
  630. void GridMapEditor::edit(GridMap *p_gridmap) {
  631. node = p_gridmap;
  632. VS *vs = VS::get_singleton();
  633. last_mouseover = Vector3(-1, -1, -1);
  634. input_action = INPUT_NONE;
  635. selection.active = false;
  636. _update_selection_transform();
  637. _update_duplicate_indicator();
  638. spatial_editor = editor->get_editor_plugin_screen()->cast_to<SpatialEditorPlugin>();
  639. if (!node) {
  640. set_process(false);
  641. for (int i = 0; i < 3; i++) {
  642. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], false);
  643. }
  644. VisualServer::get_singleton()->instance_set_visible(cursor_instance, false);
  645. _clear_areas();
  646. return;
  647. }
  648. update_pallete();
  649. update_areas();
  650. set_process(true);
  651. Vector3 edited_floor = p_gridmap->get_meta("_editor_floor_");
  652. clip_mode = p_gridmap->has_meta("_editor_clip_") ? ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()) : CLIP_DISABLED;
  653. for (int i = 0; i < 3; i++) {
  654. if (vs->mesh_get_surface_count(grid[i]) > 0)
  655. vs->mesh_remove_surface(grid[i], 0);
  656. edit_floor[i] = edited_floor[i];
  657. }
  658. {
  659. //update grids
  660. indicator_mat.instance();
  661. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  662. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  663. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  664. indicator_mat->set_albedo(Color(0.8, 0.5, 0.1));
  665. Vector<Vector3> grid_points[3];
  666. Vector<Color> grid_colors[3];
  667. float cell_size[3] = { p_gridmap->get_cell_size(), p_gridmap->get_cell_size(), p_gridmap->get_cell_size() };
  668. for (int i = 0; i < 3; i++) {
  669. Vector3 axis;
  670. axis[i] = 1;
  671. Vector3 axis_n1;
  672. axis_n1[(i + 1) % 3] = cell_size[(i + 1) % 3];
  673. Vector3 axis_n2;
  674. axis_n2[(i + 2) % 3] = cell_size[(i + 2) % 3];
  675. for (int j = -GRID_CURSOR_SIZE; j <= GRID_CURSOR_SIZE; j++) {
  676. for (int k = -GRID_CURSOR_SIZE; k <= GRID_CURSOR_SIZE; k++) {
  677. Vector3 p = axis_n1 * j + axis_n2 * k;
  678. float trans = Math::pow(MAX(0, 1.0 - (Vector2(j, k).length() / GRID_CURSOR_SIZE)), 2);
  679. Vector3 pj = axis_n1 * (j + 1) + axis_n2 * k;
  680. float transj = Math::pow(MAX(0, 1.0 - (Vector2(j + 1, k).length() / GRID_CURSOR_SIZE)), 2);
  681. Vector3 pk = axis_n1 * j + axis_n2 * (k + 1);
  682. float transk = Math::pow(MAX(0, 1.0 - (Vector2(j, k + 1).length() / GRID_CURSOR_SIZE)), 2);
  683. grid_points[i].push_back(p);
  684. grid_points[i].push_back(pk);
  685. grid_colors[i].push_back(Color(1, 1, 1, trans));
  686. grid_colors[i].push_back(Color(1, 1, 1, transk));
  687. grid_points[i].push_back(p);
  688. grid_points[i].push_back(pj);
  689. grid_colors[i].push_back(Color(1, 1, 1, trans));
  690. grid_colors[i].push_back(Color(1, 1, 1, transj));
  691. }
  692. }
  693. Array d;
  694. d.resize(VS::ARRAY_MAX);
  695. d[VS::ARRAY_VERTEX] = grid_points[i];
  696. d[VS::ARRAY_COLOR] = grid_colors[i];
  697. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  698. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  699. }
  700. }
  701. update_grid();
  702. _update_clip();
  703. _update_areas_display();
  704. }
  705. void GridMapEditor::_update_clip() {
  706. node->set_meta("_editor_clip_", clip_mode);
  707. if (clip_mode == CLIP_DISABLED)
  708. node->set_clip(false);
  709. else
  710. node->set_clip(true, clip_mode == CLIP_ABOVE, edit_floor[edit_axis], edit_axis);
  711. }
  712. void GridMapEditor::update_grid() {
  713. grid_xform.origin.x -= 1; //force update in hackish way.. what do i care
  714. //VS *vs = VS::get_singleton();
  715. grid_ofs[edit_axis] = edit_floor[edit_axis] * node->get_cell_size();
  716. edit_grid_xform.origin = grid_ofs;
  717. edit_grid_xform.basis = Basis();
  718. for (int i = 0; i < 3; i++) {
  719. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], i == edit_axis);
  720. }
  721. updating = true;
  722. floor->set_value(edit_floor[edit_axis]);
  723. updating = false;
  724. }
  725. void GridMapEditor::_notification(int p_what) {
  726. if (p_what == NOTIFICATION_ENTER_TREE) {
  727. theme_pallete->connect("item_selected", this, "_item_selected_cbk");
  728. edit_mode->connect("item_selected", this, "_edit_mode_changed");
  729. area_list->connect("item_edited", this, "_area_renamed");
  730. area_list->connect("item_selected", this, "_area_selected");
  731. for (int i = 0; i < 3; i++) {
  732. grid[i] = VS::get_singleton()->mesh_create();
  733. grid_instance[i] = VS::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  734. }
  735. selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh, get_tree()->get_root()->get_world()->get_scenario());
  736. duplicate_instance = VisualServer::get_singleton()->instance_create2(duplicate_mesh, get_tree()->get_root()->get_world()->get_scenario());
  737. _update_selection_transform();
  738. _update_duplicate_indicator();
  739. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  740. for (int i = 0; i < 3; i++) {
  741. VS::get_singleton()->free(grid_instance[i]);
  742. VS::get_singleton()->free(grid[i]);
  743. grid_instance[i] = RID();
  744. grid[i] = RID();
  745. }
  746. VisualServer::get_singleton()->free(selection_instance);
  747. VisualServer::get_singleton()->free(duplicate_instance);
  748. selection_instance = RID();
  749. duplicate_instance = RID();
  750. } else if (p_what == NOTIFICATION_PROCESS) {
  751. Transform xf = node->get_global_transform();
  752. if (xf != grid_xform) {
  753. for (int i = 0; i < 3; i++) {
  754. VS::get_singleton()->instance_set_transform(grid_instance[i], xf * edit_grid_xform);
  755. }
  756. grid_xform = xf;
  757. }
  758. Ref<MeshLibrary> cgmt = node->get_theme();
  759. if (cgmt.operator->() != last_theme)
  760. update_pallete();
  761. if (lock_view) {
  762. EditorNode *editor = get_tree()->get_root()->get_child(0)->cast_to<EditorNode>();
  763. Plane p;
  764. p.normal[edit_axis] = 1.0;
  765. p.d = edit_floor[edit_axis] * node->get_cell_size();
  766. p = node->get_transform().xform(p); // plane to snap
  767. SpatialEditorPlugin *sep = editor->get_editor_plugin_screen()->cast_to<SpatialEditorPlugin>();
  768. if (sep)
  769. sep->snap_cursor_to_plane(p);
  770. //editor->get_editor_plugin_screen()->call("snap_cursor_to_plane",p);
  771. }
  772. }
  773. }
  774. void GridMapEditor::_update_cursor_instance() {
  775. if (!node) {
  776. return;
  777. }
  778. if (cursor_instance.is_valid())
  779. VisualServer::get_singleton()->free(cursor_instance);
  780. cursor_instance = RID();
  781. if (selected_pallete >= 0) {
  782. if (node && !node->get_theme().is_null()) {
  783. Ref<Mesh> mesh = node->get_theme()->get_item_mesh(selected_pallete);
  784. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  785. cursor_instance = VisualServer::get_singleton()->instance_create2(mesh->get_rid(), get_tree()->get_root()->get_world()->get_scenario());
  786. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  787. }
  788. }
  789. }
  790. }
  791. void GridMapEditor::_item_selected_cbk(int idx) {
  792. selected_pallete = theme_pallete->get_item_metadata(idx);
  793. _update_cursor_instance();
  794. }
  795. void GridMapEditor::_clear_areas() {
  796. for (int i = 0; i < areas.size(); i++) {
  797. VisualServer::get_singleton()->free(areas[i].instance);
  798. VisualServer::get_singleton()->free(areas[i].mesh);
  799. }
  800. areas.clear();
  801. }
  802. void GridMapEditor::_update_areas_display() {
  803. if (!node) {
  804. return;
  805. }
  806. #if 0
  807. _clear_areas();
  808. List<int> areas;
  809. node->get_area_list(&areas);
  810. Transform global_xf = node->get_global_transform();
  811. for(List<int>::Element *E=areas.front();E;E=E->next()) {
  812. int area = E->get();
  813. Color color;
  814. if (node->area_is_exterior_portal(area))
  815. color=Color(1,1,1,0.2);
  816. else
  817. color.set_hsv(Math::fmod(area*0.37,1),Math::fmod(area*0.75,1),1.0,0.2);
  818. RID material = VisualServer::get_singleton()->fixed_material_create();
  819. VisualServer::get_singleton()->fixed_material_set_param( material, VS::FIXED_MATERIAL_PARAM_DIFFUSE,color );
  820. VisualServer::get_singleton()->fixed_material_set_param( material, VS::FIXED_MATERIAL_PARAM_EMISSION,0.5 );
  821. VisualServer::get_singleton()->fixed_material_set_flag( material, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true );
  822. RID mesh = VisualServer::get_singleton()->mesh_create();
  823. PoolVector<Plane> planes;
  824. for(int i=0;i<3;i++) {
  825. Vector3 axis;
  826. axis[i]=1.0;
  827. planes.push_back(Plane(axis,1));
  828. planes.push_back(Plane(-axis,0));
  829. }
  830. VisualServer::get_singleton()->mesh_add_surface_from_planes(mesh,planes);
  831. VisualServer::get_singleton()->mesh_surface_set_material(mesh,0,material,true);
  832. AreaDisplay ad;
  833. ad.mesh=mesh;
  834. ad.instance = VisualServer::get_singleton()->instance_create2(mesh,node->get_world()->get_scenario());
  835. Transform xform;
  836. Rect3 aabb = node->area_get_bounds(area);
  837. xform.origin=aabb.pos * node->get_cell_size();
  838. xform.basis.scale(aabb.size * node->get_cell_size());
  839. VisualServer::get_singleton()->instance_set_transform(ad.instance,global_xf * xform);
  840. this->areas.push_back(ad);
  841. }
  842. #endif
  843. }
  844. void GridMapEditor::_edit_mode_changed(int p_what) {
  845. if (p_what == 0) {
  846. theme_pallete->show();
  847. area_list->hide();
  848. } else {
  849. theme_pallete->hide();
  850. area_list->show();
  851. }
  852. }
  853. void GridMapEditor::_floor_changed(float p_value) {
  854. if (updating)
  855. return;
  856. edit_floor[edit_axis] = p_value;
  857. node->set_meta("_editor_floor_", Vector3(edit_floor[0], edit_floor[1], edit_floor[2]));
  858. update_grid();
  859. _update_clip();
  860. }
  861. void GridMapEditor::_bind_methods() {
  862. ClassDB::bind_method("_menu_option", &GridMapEditor::_menu_option);
  863. ClassDB::bind_method("_configure", &GridMapEditor::_configure);
  864. ClassDB::bind_method("_item_selected_cbk", &GridMapEditor::_item_selected_cbk);
  865. ClassDB::bind_method("_edit_mode_changed", &GridMapEditor::_edit_mode_changed);
  866. ClassDB::bind_method("_area_renamed", &GridMapEditor::_area_renamed);
  867. ClassDB::bind_method("_area_selected", &GridMapEditor::_area_selected);
  868. ClassDB::bind_method("_floor_changed", &GridMapEditor::_floor_changed);
  869. ClassDB::bind_method(D_METHOD("_set_display_mode", "mode"), &GridMapEditor::_set_display_mode);
  870. }
  871. GridMapEditor::GridMapEditor(EditorNode *p_editor) {
  872. input_action = INPUT_NONE;
  873. editor = p_editor;
  874. undo_redo = p_editor->get_undo_redo();
  875. int mw = EDITOR_DEF("editors/grid_map/palette_min_width", 230);
  876. Control *ec = memnew(Control);
  877. ec->set_custom_minimum_size(Size2(mw, 0));
  878. add_child(ec);
  879. spatial_editor_hb = memnew(HBoxContainer);
  880. SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  881. options = memnew(MenuButton);
  882. spatial_editor_hb->add_child(options);
  883. spatial_editor_hb->hide();
  884. options->set_text("Grid");
  885. options->get_popup()->add_check_item("Snap View", MENU_OPTION_LOCK_VIEW);
  886. options->get_popup()->add_separator();
  887. options->get_popup()->add_item("Prev Level (" + keycode_get_string(KEY_MASK_CMD) + "Down Wheel)", MENU_OPTION_PREV_LEVEL);
  888. options->get_popup()->add_item("Next Level (" + keycode_get_string(KEY_MASK_CMD) + "Up Wheel)", MENU_OPTION_NEXT_LEVEL);
  889. options->get_popup()->add_separator();
  890. options->get_popup()->add_check_item("Clip Disabled", MENU_OPTION_CLIP_DISABLED);
  891. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true);
  892. options->get_popup()->add_check_item("Clip Above", MENU_OPTION_CLIP_ABOVE);
  893. options->get_popup()->add_check_item("Clip Below", MENU_OPTION_CLIP_BELOW);
  894. options->get_popup()->add_separator();
  895. options->get_popup()->add_check_item("Edit X Axis", MENU_OPTION_X_AXIS, KEY_Z);
  896. options->get_popup()->add_check_item("Edit Y Axis", MENU_OPTION_Y_AXIS, KEY_X);
  897. options->get_popup()->add_check_item("Edit Z Axis", MENU_OPTION_Z_AXIS, KEY_C);
  898. options->get_popup()->set_item_checked(options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true);
  899. options->get_popup()->add_separator();
  900. options->get_popup()->add_item("Cursor Rotate X", MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
  901. options->get_popup()->add_item("Cursor Rotate Y", MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
  902. options->get_popup()->add_item("Cursor Rotate Z", MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
  903. options->get_popup()->add_item("Cursor Back Rotate X", MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
  904. options->get_popup()->add_item("Cursor Back Rotate Y", MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
  905. options->get_popup()->add_item("Cursor Back Rotate Z", MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
  906. options->get_popup()->add_item("Cursor Clear Rotation", MENU_OPTION_CURSOR_CLEAR_ROTATION, KEY_W);
  907. options->get_popup()->add_separator();
  908. options->get_popup()->add_check_item("Duplicate Selects", MENU_OPTION_DUPLICATE_SELECTS);
  909. options->get_popup()->add_separator();
  910. options->get_popup()->add_item("Create Area", MENU_OPTION_SELECTION_MAKE_AREA, KEY_CONTROL + KEY_C);
  911. options->get_popup()->add_item("Create Exterior Connector", MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR);
  912. options->get_popup()->add_item("Erase Area", MENU_OPTION_REMOVE_AREA);
  913. options->get_popup()->add_separator();
  914. options->get_popup()->add_item("Selection -> Duplicate", MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_SHIFT + KEY_INSERT);
  915. options->get_popup()->add_item("Selection -> Clear", MENU_OPTION_SELECTION_CLEAR, KEY_MASK_SHIFT + KEY_DELETE);
  916. //options->get_popup()->add_separator();
  917. //options->get_popup()->add_item("Configure",MENU_OPTION_CONFIGURE);
  918. options->get_popup()->add_separator();
  919. options->get_popup()->add_item("Settings", MENU_OPTION_GRIDMAP_SETTINGS);
  920. settings_dialog = memnew(ConfirmationDialog);
  921. settings_dialog->set_title("GridMap Settings");
  922. add_child(settings_dialog);
  923. settings_vbc = memnew(VBoxContainer);
  924. settings_vbc->set_custom_minimum_size(Size2(200, 0));
  925. settings_dialog->add_child(settings_vbc);
  926. settings_pick_distance = memnew(SpinBox);
  927. settings_pick_distance->set_max(10000.0f);
  928. settings_pick_distance->set_min(500.0f);
  929. settings_pick_distance->set_step(1.0f);
  930. settings_pick_distance->set_value(EDITOR_DEF("editors/grid_map/pick_distance", 5000.0));
  931. settings_vbc->add_margin_child("Pick Distance:", settings_pick_distance);
  932. clip_mode = CLIP_DISABLED;
  933. options->get_popup()->connect("id_pressed", this, "_menu_option");
  934. HBoxContainer *hb = memnew(HBoxContainer);
  935. add_child(hb);
  936. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  937. edit_mode = memnew(OptionButton);
  938. edit_mode->set_area_as_parent_rect();
  939. edit_mode->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_BEGIN, 24);
  940. edit_mode->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, 14);
  941. edit_mode->add_item("Tiles");
  942. edit_mode->add_item("Areas");
  943. hb->add_child(edit_mode);
  944. edit_mode->set_h_size_flags(SIZE_EXPAND_FILL);
  945. mode_thumbnail = memnew(ToolButton);
  946. mode_thumbnail->set_toggle_mode(true);
  947. mode_thumbnail->set_pressed(true);
  948. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
  949. hb->add_child(mode_thumbnail);
  950. mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
  951. mode_list = memnew(ToolButton);
  952. mode_list->set_toggle_mode(true);
  953. mode_list->set_pressed(false);
  954. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  955. hb->add_child(mode_list);
  956. mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
  957. EDITOR_DEF("editors/grid_map/preview_size", 64);
  958. display_mode = DISPLAY_THUMBNAIL;
  959. selected_area = -1;
  960. theme_pallete = memnew(ItemList);
  961. add_child(theme_pallete);
  962. theme_pallete->set_v_size_flags(SIZE_EXPAND_FILL);
  963. area_list = memnew(Tree);
  964. add_child(area_list);
  965. area_list->set_v_size_flags(SIZE_EXPAND_FILL);
  966. area_list->hide();
  967. spatial_editor_hb->add_child(memnew(VSeparator));
  968. Label *fl = memnew(Label);
  969. fl->set_text(" Floor: ");
  970. spatial_editor_hb->add_child(fl);
  971. floor = memnew(SpinBox);
  972. floor->set_min(-32767);
  973. floor->set_max(32767);
  974. floor->set_step(1);
  975. floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
  976. spatial_editor_hb->add_child(floor);
  977. floor->connect("value_changed", this, "_floor_changed");
  978. edit_axis = Vector3::AXIS_Y;
  979. edit_floor[0] = -1;
  980. edit_floor[1] = -1;
  981. edit_floor[2] = -1;
  982. cursor_visible = false;
  983. selected_pallete = -1;
  984. lock_view = false;
  985. cursor_rot = 0;
  986. last_mouseover = Vector3(-1, -1, -1);
  987. selection_mesh = VisualServer::get_singleton()->mesh_create();
  988. duplicate_mesh = VisualServer::get_singleton()->mesh_create();
  989. {
  990. //selection mesh create
  991. PoolVector<Vector3> lines;
  992. PoolVector<Vector3> triangles;
  993. for (int i = 0; i < 6; i++) {
  994. Vector3 face_points[4];
  995. for (int j = 0; j < 4; j++) {
  996. float v[3];
  997. v[0] = 1.0;
  998. v[1] = 1 - 2 * ((j >> 1) & 1);
  999. v[2] = v[1] * (1 - 2 * (j & 1));
  1000. for (int k = 0; k < 3; k++) {
  1001. if (i < 3)
  1002. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  1003. else
  1004. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  1005. }
  1006. }
  1007. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1008. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1009. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1010. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1011. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1012. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1013. }
  1014. for (int i = 0; i < 12; i++) {
  1015. Rect3 base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  1016. Vector3 a, b;
  1017. base.get_edge(i, a, b);
  1018. lines.push_back(a);
  1019. lines.push_back(b);
  1020. }
  1021. Array d;
  1022. d.resize(VS::ARRAY_MAX);
  1023. inner_mat.instance();
  1024. inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.3));
  1025. inner_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
  1026. inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1027. inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1028. d[VS::ARRAY_VERTEX] = triangles;
  1029. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1030. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  1031. outer_mat.instance();
  1032. outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.3));
  1033. outer_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
  1034. outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1035. outer_mat->set_line_width(3.0);
  1036. outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1037. d[VS::ARRAY_VERTEX] = lines;
  1038. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
  1039. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  1040. d[VS::ARRAY_VERTEX] = triangles;
  1041. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1042. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 0, inner_mat->get_rid());
  1043. d[VS::ARRAY_VERTEX] = lines;
  1044. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_LINES, d);
  1045. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 1, outer_mat->get_rid());
  1046. }
  1047. selection.active = false;
  1048. updating = false;
  1049. }
  1050. GridMapEditor::~GridMapEditor() {
  1051. for (int i = 0; i < 3; i++) {
  1052. if (grid[i].is_valid())
  1053. VisualServer::get_singleton()->free(grid[i]);
  1054. if (grid_instance[i].is_valid())
  1055. VisualServer::get_singleton()->free(grid_instance[i]);
  1056. if (cursor_instance.is_valid())
  1057. VisualServer::get_singleton()->free(cursor_instance);
  1058. }
  1059. VisualServer::get_singleton()->free(selection_mesh);
  1060. if (selection_instance.is_valid())
  1061. VisualServer::get_singleton()->free(selection_instance);
  1062. VisualServer::get_singleton()->free(duplicate_mesh);
  1063. if (duplicate_instance.is_valid())
  1064. VisualServer::get_singleton()->free(duplicate_instance);
  1065. _clear_areas();
  1066. }
  1067. void GridMapEditorPlugin::edit(Object *p_object) {
  1068. gridmap_editor->edit(p_object ? p_object->cast_to<GridMap>() : NULL);
  1069. }
  1070. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1071. return p_object->is_class("GridMap");
  1072. }
  1073. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1074. if (p_visible) {
  1075. gridmap_editor->show();
  1076. gridmap_editor->spatial_editor_hb->show();
  1077. gridmap_editor->set_process(true);
  1078. } else {
  1079. gridmap_editor->spatial_editor_hb->hide();
  1080. gridmap_editor->hide();
  1081. gridmap_editor->edit(NULL);
  1082. gridmap_editor->set_process(false);
  1083. }
  1084. }
  1085. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  1086. editor = p_node;
  1087. gridmap_editor = memnew(GridMapEditor(editor));
  1088. SpatialEditor::get_singleton()->get_palette_split()->add_child(gridmap_editor);
  1089. SpatialEditor::get_singleton()->get_palette_split()->move_child(gridmap_editor, 0);
  1090. gridmap_editor->hide();
  1091. }
  1092. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1093. }