grid_map_editor_plugin.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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. /* Copyright (c) 2014-2017 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 "editor/editor_settings.h"
  32. #include "editor/plugins/spatial_editor_plugin.h"
  33. #include "scene/3d/camera.h"
  34. #include "geometry.h"
  35. #include "os/keyboard.h"
  36. void GridMapEditor::_node_removed(Node *p_node) {
  37. if (p_node == node) {
  38. node = NULL;
  39. hide();
  40. theme_pallete->hide();
  41. }
  42. }
  43. void GridMapEditor::_configure() {
  44. if (!node)
  45. return;
  46. update_grid();
  47. }
  48. void GridMapEditor::_menu_option(int p_option) {
  49. switch (p_option) {
  50. case MENU_OPTION_CONFIGURE: {
  51. } break;
  52. case MENU_OPTION_LOCK_VIEW: {
  53. int index = options->get_popup()->get_item_index(MENU_OPTION_LOCK_VIEW);
  54. lock_view = !options->get_popup()->is_item_checked(index);
  55. options->get_popup()->set_item_checked(index, lock_view);
  56. } break;
  57. case MENU_OPTION_CLIP_DISABLED:
  58. case MENU_OPTION_CLIP_ABOVE:
  59. case MENU_OPTION_CLIP_BELOW: {
  60. clip_mode = ClipMode(p_option - MENU_OPTION_CLIP_DISABLED);
  61. for (int i = 0; i < 3; i++) {
  62. int index = options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED + i);
  63. options->get_popup()->set_item_checked(index, i == clip_mode);
  64. }
  65. _update_clip();
  66. } break;
  67. case MENU_OPTION_X_AXIS:
  68. case MENU_OPTION_Y_AXIS:
  69. case MENU_OPTION_Z_AXIS: {
  70. int new_axis = p_option - MENU_OPTION_X_AXIS;
  71. for (int i = 0; i < 3; i++) {
  72. int idx = options->get_popup()->get_item_index(MENU_OPTION_X_AXIS + i);
  73. options->get_popup()->set_item_checked(idx, i == new_axis);
  74. }
  75. edit_axis = Vector3::Axis(new_axis);
  76. update_grid();
  77. _update_clip();
  78. } break;
  79. case MENU_OPTION_CURSOR_ROTATE_Y: {
  80. Basis r;
  81. if (input_action == INPUT_DUPLICATE) {
  82. r.set_orthogonal_index(selection.duplicate_rot);
  83. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  84. selection.duplicate_rot = r.get_orthogonal_index();
  85. _update_duplicate_indicator();
  86. break;
  87. }
  88. r.set_orthogonal_index(cursor_rot);
  89. r.rotate(Vector3(0, 1, 0), -Math_PI / 2.0);
  90. cursor_rot = r.get_orthogonal_index();
  91. _update_cursor_transform();
  92. } break;
  93. case MENU_OPTION_CURSOR_ROTATE_X: {
  94. Basis r;
  95. if (input_action == INPUT_DUPLICATE) {
  96. r.set_orthogonal_index(selection.duplicate_rot);
  97. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  98. selection.duplicate_rot = r.get_orthogonal_index();
  99. _update_duplicate_indicator();
  100. break;
  101. }
  102. r.set_orthogonal_index(cursor_rot);
  103. r.rotate(Vector3(1, 0, 0), -Math_PI / 2.0);
  104. cursor_rot = r.get_orthogonal_index();
  105. _update_cursor_transform();
  106. } break;
  107. case MENU_OPTION_CURSOR_ROTATE_Z: {
  108. Basis r;
  109. if (input_action == INPUT_DUPLICATE) {
  110. r.set_orthogonal_index(selection.duplicate_rot);
  111. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  112. selection.duplicate_rot = r.get_orthogonal_index();
  113. _update_duplicate_indicator();
  114. break;
  115. }
  116. r.set_orthogonal_index(cursor_rot);
  117. r.rotate(Vector3(0, 0, 1), -Math_PI / 2.0);
  118. cursor_rot = r.get_orthogonal_index();
  119. _update_cursor_transform();
  120. } break;
  121. case MENU_OPTION_CURSOR_BACK_ROTATE_Y: {
  122. Basis r;
  123. r.set_orthogonal_index(cursor_rot);
  124. r.rotate(Vector3(0, 1, 0), Math_PI / 2.0);
  125. cursor_rot = r.get_orthogonal_index();
  126. _update_cursor_transform();
  127. } break;
  128. case MENU_OPTION_CURSOR_BACK_ROTATE_X: {
  129. Basis r;
  130. r.set_orthogonal_index(cursor_rot);
  131. r.rotate(Vector3(1, 0, 0), Math_PI / 2.0);
  132. cursor_rot = r.get_orthogonal_index();
  133. _update_cursor_transform();
  134. } break;
  135. case MENU_OPTION_CURSOR_BACK_ROTATE_Z: {
  136. Basis r;
  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_CLEAR_ROTATION: {
  143. if (input_action == INPUT_DUPLICATE) {
  144. selection.duplicate_rot = 0;
  145. _update_duplicate_indicator();
  146. break;
  147. }
  148. cursor_rot = 0;
  149. _update_cursor_transform();
  150. } break;
  151. case MENU_OPTION_DUPLICATE_SELECTS: {
  152. int idx = options->get_popup()->get_item_index(MENU_OPTION_DUPLICATE_SELECTS);
  153. options->get_popup()->set_item_checked(idx, !options->get_popup()->is_item_checked(idx));
  154. } break;
  155. case MENU_OPTION_SELECTION_MAKE_AREA:
  156. case MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR: {
  157. if (!selection.active)
  158. break;
  159. int area = node->get_unused_area_id();
  160. Error err = node->create_area(area, Rect3(selection.begin, selection.end - selection.begin + Vector3(1, 1, 1)));
  161. if (err != OK) {
  162. }
  163. if (p_option == MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR) {
  164. node->area_set_exterior_portal(area, true);
  165. }
  166. _update_areas_display();
  167. update_areas();
  168. } break;
  169. case MENU_OPTION_REMOVE_AREA: {
  170. if (selected_area < 1)
  171. return;
  172. node->erase_area(selected_area);
  173. _update_areas_display();
  174. update_areas();
  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. input_action = INPUT_DUPLICATE;
  182. selection.click = last_mouseover;
  183. selection.current = last_mouseover;
  184. selection.duplicate_rot = 0;
  185. _update_duplicate_indicator();
  186. break;
  187. case MENU_OPTION_SELECTION_CLEAR: {
  188. if (!selection.active)
  189. return;
  190. _delete_selection();
  191. } break;
  192. case MENU_OPTION_GRIDMAP_SETTINGS: {
  193. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  194. } break;
  195. }
  196. }
  197. void GridMapEditor::_update_cursor_transform() {
  198. cursor_transform = Transform();
  199. cursor_transform.origin = cursor_origin;
  200. cursor_transform.basis.set_orthogonal_index(cursor_rot);
  201. cursor_transform = node->get_transform() * cursor_transform;
  202. if (cursor_instance.is_valid()) {
  203. VisualServer::get_singleton()->instance_set_transform(cursor_instance, cursor_transform);
  204. VisualServer::get_singleton()->instance_set_visible(cursor_instance, cursor_visible);
  205. }
  206. }
  207. void GridMapEditor::_update_selection_transform() {
  208. if (!selection.active) {
  209. Transform xf;
  210. xf.basis.set_zero();
  211. VisualServer::get_singleton()->instance_set_transform(selection_instance, xf);
  212. return;
  213. }
  214. Transform xf;
  215. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  216. xf.origin = selection.begin * node->get_cell_size();
  217. VisualServer::get_singleton()->instance_set_transform(selection_instance, node->get_global_transform() * xf);
  218. }
  219. void GridMapEditor::_validate_selection() {
  220. if (!selection.active)
  221. return;
  222. selection.begin = selection.click;
  223. selection.end = selection.current;
  224. if (selection.begin.x > selection.end.x)
  225. SWAP(selection.begin.x, selection.end.x);
  226. if (selection.begin.y > selection.end.y)
  227. SWAP(selection.begin.y, selection.end.y);
  228. if (selection.begin.z > selection.end.z)
  229. SWAP(selection.begin.z, selection.end.z);
  230. _update_selection_transform();
  231. }
  232. bool GridMapEditor::do_input_action(Camera *p_camera, const Point2 &p_point, bool p_click) {
  233. if (!spatial_editor)
  234. return false;
  235. if (selected_pallete < 0 && input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE)
  236. return false;
  237. Ref<MeshLibrary> theme = node->get_theme();
  238. if (theme.is_null())
  239. return false;
  240. if (input_action != INPUT_COPY && input_action != INPUT_SELECT && input_action != INPUT_DUPLICATE && !theme->has_item(selected_pallete))
  241. return false;
  242. Camera *camera = p_camera;
  243. Vector3 from = camera->project_ray_origin(p_point);
  244. Vector3 normal = camera->project_ray_normal(p_point);
  245. Transform local_xform = node->get_global_transform().affine_inverse();
  246. Vector<Plane> planes = camera->get_frustum();
  247. from = local_xform.xform(from);
  248. normal = local_xform.basis.xform(normal).normalized();
  249. Plane p;
  250. p.normal[edit_axis] = 1.0;
  251. p.d = edit_floor[edit_axis] * node->get_cell_size();
  252. Vector3 inters;
  253. if (!p.intersects_segment(from, from + normal * settings_pick_distance->get_value(), &inters))
  254. return false;
  255. //make sure the intersection is inside the frustum planes, to avoid
  256. //painting on invisible regions
  257. for (int i = 0; i < planes.size(); i++) {
  258. Plane fp = local_xform.xform(planes[i]);
  259. if (fp.is_point_over(inters))
  260. return false;
  261. }
  262. int cell[3];
  263. float cell_size[3] = { node->get_cell_size(), node->get_cell_size(), node->get_cell_size() };
  264. last_mouseover = Vector3(-1, -1, -1);
  265. for (int i = 0; i < 3; i++) {
  266. if (i == edit_axis)
  267. cell[i] = edit_floor[i];
  268. else {
  269. cell[i] = inters[i] / node->get_cell_size();
  270. if (inters[i] < 0)
  271. cell[i] -= 1; //compensate negative
  272. grid_ofs[i] = cell[i] * cell_size[i];
  273. }
  274. /*if (cell[i]<0 || cell[i]>=grid_size[i]) {
  275. cursor_visible=false;
  276. _update_cursor_transform();
  277. return false;
  278. }*/
  279. }
  280. last_mouseover = Vector3(cell[0], cell[1], cell[2]);
  281. VS::get_singleton()->instance_set_transform(grid_instance[edit_axis], Transform(Basis(), grid_ofs));
  282. if (cursor_instance.is_valid()) {
  283. 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();
  284. cursor_visible = true;
  285. _update_cursor_transform();
  286. }
  287. if (input_action == INPUT_DUPLICATE) {
  288. selection.current = Vector3(cell[0], cell[1], cell[2]);
  289. _update_duplicate_indicator();
  290. } else if (input_action == INPUT_SELECT) {
  291. selection.current = Vector3(cell[0], cell[1], cell[2]);
  292. if (p_click)
  293. selection.click = selection.current;
  294. selection.active = true;
  295. _validate_selection();
  296. return true;
  297. } else if (input_action == INPUT_COPY) {
  298. int item = node->get_cell_item(cell[0], cell[1], cell[2]);
  299. if (item >= 0) {
  300. selected_pallete = item;
  301. theme_pallete->set_current(item);
  302. update_pallete();
  303. _update_cursor_instance();
  304. }
  305. return true;
  306. }
  307. if (input_action == INPUT_PAINT) {
  308. SetItem si;
  309. si.pos = Vector3(cell[0], cell[1], cell[2]);
  310. si.new_value = selected_pallete;
  311. si.new_orientation = cursor_rot;
  312. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  313. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  314. set_items.push_back(si);
  315. node->set_cell_item(cell[0], cell[1], cell[2], selected_pallete, cursor_rot);
  316. return true;
  317. } else if (input_action == INPUT_ERASE) {
  318. SetItem si;
  319. si.pos = Vector3(cell[0], cell[1], cell[2]);
  320. si.new_value = -1;
  321. si.new_orientation = 0;
  322. si.old_value = node->get_cell_item(cell[0], cell[1], cell[2]);
  323. si.old_orientation = node->get_cell_item_orientation(cell[0], cell[1], cell[2]);
  324. set_items.push_back(si);
  325. node->set_cell_item(cell[0], cell[1], cell[2], -1);
  326. return true;
  327. }
  328. return false;
  329. }
  330. void GridMapEditor::_delete_selection() {
  331. if (!selection.active)
  332. return;
  333. undo_redo->create_action(TTR("GridMap Delete Selection"));
  334. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  335. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  336. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  337. undo_redo->add_do_method(node, "set_cell_item", i, j, k, GridMap::INVALID_CELL_ITEM);
  338. 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));
  339. }
  340. }
  341. }
  342. undo_redo->commit_action();
  343. selection.active = false;
  344. _validate_selection();
  345. }
  346. void GridMapEditor::_update_duplicate_indicator() {
  347. if (!selection.active || input_action != INPUT_DUPLICATE) {
  348. Transform xf;
  349. xf.basis.set_zero();
  350. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, xf);
  351. return;
  352. }
  353. Transform xf;
  354. xf.scale(Vector3(1, 1, 1) * (Vector3(1, 1, 1) + (selection.end - selection.begin)) * node->get_cell_size());
  355. xf.origin = (selection.begin + (selection.current - selection.click)) * node->get_cell_size();
  356. Basis rot;
  357. rot.set_orthogonal_index(selection.duplicate_rot);
  358. xf.basis = rot * xf.basis;
  359. VisualServer::get_singleton()->instance_set_transform(duplicate_instance, node->get_global_transform() * xf);
  360. }
  361. struct __Item {
  362. Vector3 pos;
  363. int rot;
  364. int item;
  365. };
  366. void GridMapEditor::_duplicate_paste() {
  367. if (!selection.active)
  368. return;
  369. int idx = options->get_popup()->get_item_index(MENU_OPTION_DUPLICATE_SELECTS);
  370. bool reselect = options->get_popup()->is_item_checked(idx);
  371. List<__Item> items;
  372. Basis rot;
  373. rot.set_orthogonal_index(selection.duplicate_rot);
  374. for (int i = selection.begin.x; i <= selection.end.x; i++) {
  375. for (int j = selection.begin.y; j <= selection.end.y; j++) {
  376. for (int k = selection.begin.z; k <= selection.end.z; k++) {
  377. int itm = node->get_cell_item(i, j, k);
  378. if (itm == GridMap::INVALID_CELL_ITEM)
  379. continue;
  380. int orientation = node->get_cell_item_orientation(i, j, k);
  381. __Item item;
  382. Vector3 rel = Vector3(i, j, k) - selection.begin;
  383. rel = rot.xform(rel);
  384. Basis orm;
  385. orm.set_orthogonal_index(orientation);
  386. orm = rot * orm;
  387. item.pos = selection.begin + rel;
  388. item.item = itm;
  389. item.rot = orm.get_orthogonal_index();
  390. items.push_back(item);
  391. }
  392. }
  393. }
  394. Vector3 ofs = selection.current - selection.click;
  395. if (items.size()) {
  396. undo_redo->create_action(TTR("GridMap Duplicate Selection"));
  397. for (List<__Item>::Element *E = items.front(); E; E = E->next()) {
  398. __Item &it = E->get();
  399. Vector3 pos = it.pos + ofs;
  400. undo_redo->add_do_method(node, "set_cell_item", pos.x, pos.y, pos.z, it.item, it.rot);
  401. 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));
  402. }
  403. undo_redo->commit_action();
  404. }
  405. if (reselect) {
  406. selection.begin += ofs;
  407. selection.end += ofs;
  408. selection.click = selection.begin;
  409. selection.current = selection.end;
  410. _validate_selection();
  411. }
  412. }
  413. bool GridMapEditor::forward_spatial_input_event(Camera *p_camera, const Ref<InputEvent> &p_event) {
  414. if (!node) {
  415. return false;
  416. }
  417. if (edit_mode->get_selected() == 0) { // regular click
  418. Ref<InputEventMouseButton> mb = p_event;
  419. if (mb.is_valid()) {
  420. if (mb->get_button_index() == BUTTON_WHEEL_UP && (mb->get_command() || mb->get_shift())) {
  421. if (mb->is_pressed())
  422. floor->set_value(floor->get_value() + mb->get_factor());
  423. return true; //eaten
  424. } else if (mb->get_button_index() == BUTTON_WHEEL_DOWN && (mb->get_command() || mb->get_shift())) {
  425. if (mb->is_pressed())
  426. floor->set_value(floor->get_value() - mb->get_factor());
  427. return true;
  428. }
  429. if (mb->is_pressed()) {
  430. if (mb->get_button_index() == BUTTON_LEFT) {
  431. if (input_action == INPUT_DUPLICATE) {
  432. //paste
  433. _duplicate_paste();
  434. input_action = INPUT_NONE;
  435. _update_duplicate_indicator();
  436. } else if (mb->get_shift()) {
  437. input_action = INPUT_SELECT;
  438. } else if (mb->get_command())
  439. input_action = INPUT_COPY;
  440. else {
  441. input_action = INPUT_PAINT;
  442. set_items.clear();
  443. }
  444. } else if (mb->get_button_index() == BUTTON_RIGHT)
  445. if (input_action == INPUT_DUPLICATE) {
  446. input_action = INPUT_NONE;
  447. _update_duplicate_indicator();
  448. } else {
  449. input_action = INPUT_ERASE;
  450. set_items.clear();
  451. }
  452. else
  453. return false;
  454. return do_input_action(p_camera, Point2(mb->get_position().x, mb->get_position().y), true);
  455. } else {
  456. if (
  457. (mb->get_button_index() == BUTTON_RIGHT && input_action == INPUT_ERASE) ||
  458. (mb->get_button_index() == BUTTON_LEFT && input_action == INPUT_PAINT)) {
  459. if (set_items.size()) {
  460. undo_redo->create_action(TTR("GridMap Paint"));
  461. for (List<SetItem>::Element *E = set_items.front(); E; E = E->next()) {
  462. const SetItem &si = E->get();
  463. undo_redo->add_do_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.new_value, si.new_orientation);
  464. }
  465. for (List<SetItem>::Element *E = set_items.back(); E; E = E->prev()) {
  466. const SetItem &si = E->get();
  467. undo_redo->add_undo_method(node, "set_cell_item", si.pos.x, si.pos.y, si.pos.z, si.old_value, si.old_orientation);
  468. }
  469. undo_redo->commit_action();
  470. }
  471. set_items.clear();
  472. input_action = INPUT_NONE;
  473. return true;
  474. }
  475. if (mb->get_button_index() == BUTTON_LEFT && input_action != INPUT_NONE) {
  476. set_items.clear();
  477. input_action = INPUT_NONE;
  478. return true;
  479. }
  480. if (mb->get_button_index() == BUTTON_RIGHT && (input_action == INPUT_ERASE || input_action == INPUT_DUPLICATE)) {
  481. input_action = INPUT_NONE;
  482. return true;
  483. }
  484. }
  485. }
  486. Ref<InputEventMouseMotion> mm = p_event;
  487. if (mm.is_valid()) {
  488. return do_input_action(p_camera, mm->get_position(), false);
  489. }
  490. } else if (edit_mode->get_selected() == 1) {
  491. //area mode, select an area
  492. Ref<InputEventMouseButton> mb = p_event;
  493. if (mb.is_valid()) {
  494. if (mb->get_button_index() == BUTTON_LEFT && mb->is_pressed()) {
  495. Point2 point = mb->get_position();
  496. Camera *camera = p_camera;
  497. Vector3 from = camera->project_ray_origin(point);
  498. Vector3 normal = camera->project_ray_normal(point);
  499. Transform local_xform = node->get_global_transform().affine_inverse();
  500. from = local_xform.xform(from);
  501. normal = local_xform.basis.xform(normal).normalized();
  502. List<int> areas;
  503. node->get_area_list(&areas);
  504. float min_d = 1e10;
  505. int min_area = -1;
  506. for (List<int>::Element *E = areas.front(); E; E = E->next()) {
  507. int area = E->get();
  508. Rect3 aabb = node->area_get_bounds(area);
  509. aabb.position *= node->get_cell_size();
  510. aabb.size *= node->get_cell_size();
  511. Vector3 rclip, rnormal;
  512. if (!aabb.intersects_segment(from, from + normal * 10000, &rclip, &rnormal))
  513. continue;
  514. float d = normal.dot(rclip);
  515. if (d < min_d) {
  516. min_d = d;
  517. min_area = area;
  518. }
  519. }
  520. selected_area = min_area;
  521. update_areas();
  522. }
  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 = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  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 = Object::cast_to<EditorNode>(get_tree()->get_root()->get_child(0));
  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 = Object::cast_to<SpatialEditorPlugin>(editor->get_editor_plugin_screen());
  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(TTR("Snap View"), MENU_OPTION_LOCK_VIEW);
  886. options->get_popup()->add_separator();
  887. options->get_popup()->add_item(vformat(TTR("Prev Level (%sDown Wheel)"), keycode_get_string(KEY_MASK_CMD)), MENU_OPTION_PREV_LEVEL);
  888. options->get_popup()->add_item(vformat(TTR("Next Level (%sUp Wheel)"), keycode_get_string(KEY_MASK_CMD)), MENU_OPTION_NEXT_LEVEL);
  889. options->get_popup()->add_separator();
  890. options->get_popup()->add_check_item(TTR("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(TTR("Clip Above"), MENU_OPTION_CLIP_ABOVE);
  893. options->get_popup()->add_check_item(TTR("Clip Below"), MENU_OPTION_CLIP_BELOW);
  894. options->get_popup()->add_separator();
  895. options->get_popup()->add_check_item(TTR("Edit X Axis"), MENU_OPTION_X_AXIS, KEY_Z);
  896. options->get_popup()->add_check_item(TTR("Edit Y Axis"), MENU_OPTION_Y_AXIS, KEY_X);
  897. options->get_popup()->add_check_item(TTR("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(TTR("Cursor Rotate X"), MENU_OPTION_CURSOR_ROTATE_X, KEY_A);
  901. options->get_popup()->add_item(TTR("Cursor Rotate Y"), MENU_OPTION_CURSOR_ROTATE_Y, KEY_S);
  902. options->get_popup()->add_item(TTR("Cursor Rotate Z"), MENU_OPTION_CURSOR_ROTATE_Z, KEY_D);
  903. options->get_popup()->add_item(TTR("Cursor Back Rotate X"), MENU_OPTION_CURSOR_BACK_ROTATE_X, KEY_MASK_SHIFT + KEY_A);
  904. options->get_popup()->add_item(TTR("Cursor Back Rotate Y"), MENU_OPTION_CURSOR_BACK_ROTATE_Y, KEY_MASK_SHIFT + KEY_S);
  905. options->get_popup()->add_item(TTR("Cursor Back Rotate Z"), MENU_OPTION_CURSOR_BACK_ROTATE_Z, KEY_MASK_SHIFT + KEY_D);
  906. options->get_popup()->add_item(TTR("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(TTR("Create Area"), MENU_OPTION_SELECTION_MAKE_AREA, KEY_CONTROL + KEY_C);
  911. options->get_popup()->add_item(TTR("Create Exterior Connector"), MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR);
  912. options->get_popup()->add_item(TTR("Erase Area"), MENU_OPTION_REMOVE_AREA);
  913. options->get_popup()->add_separator();
  914. options->get_popup()->add_item(TTR("Selection -> Duplicate"), MENU_OPTION_SELECTION_DUPLICATE, KEY_MASK_SHIFT + KEY_INSERT);
  915. options->get_popup()->add_item(TTR("Selection -> Clear"), MENU_OPTION_SELECTION_CLEAR, KEY_MASK_SHIFT + KEY_DELETE);
  916. options->get_popup()->add_separator();
  917. options->get_popup()->add_item(TTR("Settings"), MENU_OPTION_GRIDMAP_SETTINGS);
  918. settings_dialog = memnew(ConfirmationDialog);
  919. settings_dialog->set_title(TTR("GridMap Settings"));
  920. add_child(settings_dialog);
  921. settings_vbc = memnew(VBoxContainer);
  922. settings_vbc->set_custom_minimum_size(Size2(200, 0));
  923. settings_dialog->add_child(settings_vbc);
  924. settings_pick_distance = memnew(SpinBox);
  925. settings_pick_distance->set_max(10000.0f);
  926. settings_pick_distance->set_min(500.0f);
  927. settings_pick_distance->set_step(1.0f);
  928. settings_pick_distance->set_value(EDITOR_DEF("editors/grid_map/pick_distance", 5000.0));
  929. settings_vbc->add_margin_child(TTR("Pick Distance:"), settings_pick_distance);
  930. clip_mode = CLIP_DISABLED;
  931. options->get_popup()->connect("id_pressed", this, "_menu_option");
  932. HBoxContainer *hb = memnew(HBoxContainer);
  933. add_child(hb);
  934. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  935. edit_mode = memnew(OptionButton);
  936. edit_mode->set_area_as_parent_rect();
  937. edit_mode->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_BEGIN, 24);
  938. edit_mode->set_margin(MARGIN_RIGHT, -14);
  939. edit_mode->add_item(TTR("Tiles"));
  940. edit_mode->add_item(TTR("Areas"));
  941. hb->add_child(edit_mode);
  942. edit_mode->set_h_size_flags(SIZE_EXPAND_FILL);
  943. mode_thumbnail = memnew(ToolButton);
  944. mode_thumbnail->set_toggle_mode(true);
  945. mode_thumbnail->set_pressed(true);
  946. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail", "EditorIcons"));
  947. hb->add_child(mode_thumbnail);
  948. mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
  949. mode_list = memnew(ToolButton);
  950. mode_list->set_toggle_mode(true);
  951. mode_list->set_pressed(false);
  952. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  953. hb->add_child(mode_list);
  954. mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
  955. EDITOR_DEF("editors/grid_map/preview_size", 64);
  956. display_mode = DISPLAY_THUMBNAIL;
  957. selected_area = -1;
  958. theme_pallete = memnew(ItemList);
  959. add_child(theme_pallete);
  960. theme_pallete->set_v_size_flags(SIZE_EXPAND_FILL);
  961. area_list = memnew(Tree);
  962. add_child(area_list);
  963. area_list->set_v_size_flags(SIZE_EXPAND_FILL);
  964. area_list->hide();
  965. spatial_editor_hb->add_child(memnew(VSeparator));
  966. Label *fl = memnew(Label);
  967. fl->set_text(" Floor: ");
  968. spatial_editor_hb->add_child(fl);
  969. floor = memnew(SpinBox);
  970. floor->set_min(-32767);
  971. floor->set_max(32767);
  972. floor->set_step(1);
  973. floor->get_line_edit()->add_constant_override("minimum_spaces", 16);
  974. spatial_editor_hb->add_child(floor);
  975. floor->connect("value_changed", this, "_floor_changed");
  976. edit_axis = Vector3::AXIS_Y;
  977. edit_floor[0] = -1;
  978. edit_floor[1] = -1;
  979. edit_floor[2] = -1;
  980. cursor_visible = false;
  981. selected_pallete = -1;
  982. lock_view = false;
  983. cursor_rot = 0;
  984. last_mouseover = Vector3(-1, -1, -1);
  985. selection_mesh = VisualServer::get_singleton()->mesh_create();
  986. duplicate_mesh = VisualServer::get_singleton()->mesh_create();
  987. {
  988. //selection mesh create
  989. PoolVector<Vector3> lines;
  990. PoolVector<Vector3> triangles;
  991. for (int i = 0; i < 6; i++) {
  992. Vector3 face_points[4];
  993. for (int j = 0; j < 4; j++) {
  994. float v[3];
  995. v[0] = 1.0;
  996. v[1] = 1 - 2 * ((j >> 1) & 1);
  997. v[2] = v[1] * (1 - 2 * (j & 1));
  998. for (int k = 0; k < 3; k++) {
  999. if (i < 3)
  1000. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  1001. else
  1002. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  1003. }
  1004. }
  1005. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1006. triangles.push_back(face_points[1] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1007. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1008. triangles.push_back(face_points[2] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1009. triangles.push_back(face_points[3] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1010. triangles.push_back(face_points[0] * 0.5 + Vector3(0.5, 0.5, 0.5));
  1011. }
  1012. for (int i = 0; i < 12; i++) {
  1013. Rect3 base(Vector3(0, 0, 0), Vector3(1, 1, 1));
  1014. Vector3 a, b;
  1015. base.get_edge(i, a, b);
  1016. lines.push_back(a);
  1017. lines.push_back(b);
  1018. }
  1019. Array d;
  1020. d.resize(VS::ARRAY_MAX);
  1021. inner_mat.instance();
  1022. inner_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.3));
  1023. inner_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
  1024. inner_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1025. inner_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1026. d[VS::ARRAY_VERTEX] = triangles;
  1027. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1028. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 0, inner_mat->get_rid());
  1029. outer_mat.instance();
  1030. outer_mat->set_albedo(Color(0.7, 0.7, 1.0, 0.3));
  1031. outer_mat->set_flag(SpatialMaterial::FLAG_ONTOP, true);
  1032. outer_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1033. outer_mat->set_line_width(3.0);
  1034. outer_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1035. d[VS::ARRAY_VERTEX] = lines;
  1036. VisualServer::get_singleton()->mesh_add_surface_from_arrays(selection_mesh, VS::PRIMITIVE_LINES, d);
  1037. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh, 1, outer_mat->get_rid());
  1038. d[VS::ARRAY_VERTEX] = triangles;
  1039. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_TRIANGLES, d);
  1040. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 0, inner_mat->get_rid());
  1041. d[VS::ARRAY_VERTEX] = lines;
  1042. VisualServer::get_singleton()->mesh_add_surface_from_arrays(duplicate_mesh, VS::PRIMITIVE_LINES, d);
  1043. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh, 1, outer_mat->get_rid());
  1044. }
  1045. selection.active = false;
  1046. updating = false;
  1047. }
  1048. GridMapEditor::~GridMapEditor() {
  1049. for (int i = 0; i < 3; i++) {
  1050. if (grid[i].is_valid())
  1051. VisualServer::get_singleton()->free(grid[i]);
  1052. if (grid_instance[i].is_valid())
  1053. VisualServer::get_singleton()->free(grid_instance[i]);
  1054. if (cursor_instance.is_valid())
  1055. VisualServer::get_singleton()->free(cursor_instance);
  1056. }
  1057. VisualServer::get_singleton()->free(selection_mesh);
  1058. if (selection_instance.is_valid())
  1059. VisualServer::get_singleton()->free(selection_instance);
  1060. VisualServer::get_singleton()->free(duplicate_mesh);
  1061. if (duplicate_instance.is_valid())
  1062. VisualServer::get_singleton()->free(duplicate_instance);
  1063. _clear_areas();
  1064. }
  1065. void GridMapEditorPlugin::edit(Object *p_object) {
  1066. gridmap_editor->edit(Object::cast_to<GridMap>(p_object));
  1067. }
  1068. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1069. return p_object->is_class("GridMap");
  1070. }
  1071. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1072. if (p_visible) {
  1073. gridmap_editor->show();
  1074. gridmap_editor->spatial_editor_hb->show();
  1075. gridmap_editor->set_process(true);
  1076. } else {
  1077. gridmap_editor->spatial_editor_hb->hide();
  1078. gridmap_editor->hide();
  1079. gridmap_editor->edit(NULL);
  1080. gridmap_editor->set_process(false);
  1081. }
  1082. }
  1083. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  1084. editor = p_node;
  1085. gridmap_editor = memnew(GridMapEditor(editor));
  1086. SpatialEditor::get_singleton()->get_palette_split()->add_child(gridmap_editor);
  1087. SpatialEditor::get_singleton()->get_palette_split()->move_child(gridmap_editor, 0);
  1088. gridmap_editor->hide();
  1089. }
  1090. GridMapEditorPlugin::~GridMapEditorPlugin() {
  1091. }