grid_map_editor_plugin.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521
  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 "tools/editor/plugins/spatial_editor_plugin.h"
  31. #include "scene/3d/camera.h"
  32. #include "tools/editor/editor_settings.h"
  33. #include "os/keyboard.h"
  34. #include "geometry.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. Matrix3 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. Matrix3 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. Matrix3 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. Matrix3 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. Matrix3 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. Matrix3 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,AABB(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_geometry_set_flag(cursor_instance,VS::INSTANCE_FLAG_VISIBLE,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(Matrix3(),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. } if (input_action==INPUT_PAINT) {
  306. SetItem si;
  307. si.pos=Vector3(cell[0],cell[1],cell[2]);
  308. si.new_value=selected_pallete;
  309. si.new_orientation=cursor_rot;
  310. si.old_value=node->get_cell_item(cell[0],cell[1],cell[2]);
  311. si.old_orientation=node->get_cell_item_orientation(cell[0],cell[1],cell[2]);
  312. set_items.push_back(si);
  313. node->set_cell_item(cell[0],cell[1],cell[2],selected_pallete,cursor_rot);
  314. return true;
  315. } else if (input_action==INPUT_ERASE) {
  316. SetItem si;
  317. si.pos=Vector3(cell[0],cell[1],cell[2]);
  318. si.new_value=-1;
  319. si.new_orientation=0;
  320. si.old_value=node->get_cell_item(cell[0],cell[1],cell[2]);
  321. si.old_orientation=node->get_cell_item_orientation(cell[0],cell[1],cell[2]);
  322. set_items.push_back(si);
  323. node->set_cell_item(cell[0],cell[1],cell[2],-1);
  324. return true;
  325. }
  326. return false;
  327. }
  328. void GridMapEditor::_delete_selection() {
  329. if (!selection.active)
  330. return;
  331. undo_redo->create_action("GridMap Delete Selection");
  332. for(int i=selection.begin.x;i<=selection.end.x;i++) {
  333. for(int j=selection.begin.y;j<=selection.end.y;j++) {
  334. for(int k=selection.begin.z;k<=selection.end.z;k++) {
  335. undo_redo->add_do_method(node,"set_cell_item",i,j,k,GridMap::INVALID_CELL_ITEM);
  336. 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));
  337. }
  338. }
  339. }
  340. undo_redo->commit_action();
  341. selection.active=false;
  342. _validate_selection();
  343. }
  344. void GridMapEditor::_update_duplicate_indicator() {
  345. if (!selection.active || input_action!=INPUT_DUPLICATE) {
  346. Transform xf;
  347. xf.basis.set_zero();
  348. VisualServer::get_singleton()->instance_set_transform(duplicate_instance,xf);
  349. return;
  350. }
  351. Transform xf;
  352. xf.scale(Vector3(1,1,1)*(Vector3(1,1,1)+(selection.end-selection.begin))*node->get_cell_size());
  353. xf.origin=(selection.begin+(selection.current-selection.click))*node->get_cell_size();
  354. Matrix3 rot;
  355. rot.set_orthogonal_index(selection.duplicate_rot);
  356. xf.basis = rot * xf.basis;
  357. VisualServer::get_singleton()->instance_set_transform(duplicate_instance,node->get_global_transform() * xf);
  358. }
  359. struct __Item { Vector3 pos; int rot; int item ; };
  360. void GridMapEditor::_duplicate_paste() {
  361. if (!selection.active)
  362. return;
  363. int idx = options->get_popup()->get_item_index(MENU_OPTION_DUPLICATE_SELECTS);
  364. bool reselect = options->get_popup()->is_item_checked( idx );
  365. List< __Item > items;
  366. Matrix3 rot;
  367. rot.set_orthogonal_index(selection.duplicate_rot);
  368. for(int i=selection.begin.x;i<=selection.end.x;i++) {
  369. for(int j=selection.begin.y;j<=selection.end.y;j++) {
  370. for(int k=selection.begin.z;k<=selection.end.z;k++) {
  371. int itm = node->get_cell_item(i,j,k);
  372. if (itm==GridMap::INVALID_CELL_ITEM)
  373. continue;
  374. int orientation = node->get_cell_item_orientation(i,j,k);
  375. __Item item;
  376. Vector3 rel=Vector3(i,j,k)-selection.begin;
  377. rel = rot.xform(rel);
  378. Matrix3 orm;
  379. orm.set_orthogonal_index(orientation);
  380. orm = rot * orm;
  381. item.pos=selection.begin+rel;
  382. item.item=itm;
  383. item.rot=orm.get_orthogonal_index();
  384. items.push_back(item);
  385. }
  386. }
  387. }
  388. Vector3 ofs=selection.current-selection.click;
  389. if (items.size()) {
  390. undo_redo->create_action("GridMap Duplicate Selection");
  391. for(List< __Item >::Element *E=items.front();E;E=E->next()) {
  392. __Item &it=E->get();
  393. Vector3 pos = it.pos+ofs;
  394. undo_redo->add_do_method(node,"set_cell_item",pos.x,pos.y,pos.z,it.item,it.rot);
  395. 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));
  396. }
  397. undo_redo->commit_action();
  398. }
  399. if (reselect) {
  400. selection.begin+=ofs;
  401. selection.end+=ofs;
  402. selection.click=selection.begin;
  403. selection.current=selection.end;
  404. _validate_selection();
  405. }
  406. }
  407. bool GridMapEditor::forward_spatial_input_event(Camera* p_camera,const InputEvent& p_event) {
  408. if (!node) {
  409. return false;
  410. }
  411. if (edit_mode->get_selected()==0) { // regular click
  412. switch (p_event.type) {
  413. case InputEvent::MOUSE_BUTTON: {
  414. if (p_event.mouse_button.button_index==BUTTON_WHEEL_UP && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
  415. if (p_event.mouse_button.pressed)
  416. floor->set_value( floor->get_value() +1);
  417. return true; //eaten
  418. } else if (p_event.mouse_button.button_index==BUTTON_WHEEL_DOWN && (p_event.mouse_button.mod.command || p_event.mouse_button.mod.shift)) {
  419. if (p_event.mouse_button.pressed)
  420. floor->set_value( floor->get_value() -1);
  421. return true;
  422. }
  423. if (p_event.mouse_button.pressed) {
  424. if (p_event.mouse_button.button_index==BUTTON_LEFT) {
  425. if (input_action==INPUT_DUPLICATE) {
  426. //paste
  427. _duplicate_paste();
  428. input_action=INPUT_NONE;
  429. _update_duplicate_indicator();
  430. } else if (p_event.mouse_button.mod.shift) {
  431. input_action=INPUT_SELECT;
  432. } else if (p_event.mouse_button.mod.command)
  433. input_action=INPUT_COPY;
  434. else {
  435. input_action=INPUT_PAINT;
  436. set_items.clear();
  437. }
  438. } else if (p_event.mouse_button.button_index==BUTTON_RIGHT)
  439. if (input_action==INPUT_DUPLICATE) {
  440. input_action=INPUT_NONE;
  441. _update_duplicate_indicator();
  442. } else {
  443. input_action=INPUT_ERASE;
  444. set_items.clear();
  445. }
  446. else
  447. return false;
  448. return do_input_action(p_camera,Point2(p_event.mouse_button.x,p_event.mouse_button.y),true);
  449. } else {
  450. if (
  451. (p_event.mouse_button.button_index==BUTTON_RIGHT && input_action==INPUT_ERASE) ||
  452. (p_event.mouse_button.button_index==BUTTON_LEFT && input_action==INPUT_PAINT) ) {
  453. if (set_items.size()) {
  454. undo_redo->create_action("GridMap Paint");
  455. for(List<SetItem>::Element *E=set_items.front();E;E=E->next()) {
  456. const SetItem &si=E->get();
  457. undo_redo->add_do_method(node,"set_cell_item",si.pos.x,si.pos.y,si.pos.z,si.new_value,si.new_orientation);
  458. }
  459. for(List<SetItem>::Element *E=set_items.back();E;E=E->prev()) {
  460. const SetItem &si=E->get();
  461. undo_redo->add_undo_method(node,"set_cell_item",si.pos.x,si.pos.y,si.pos.z,si.old_value,si.old_orientation);
  462. }
  463. undo_redo->commit_action();
  464. }
  465. set_items.clear();
  466. input_action=INPUT_NONE;
  467. return true;
  468. }
  469. if (p_event.mouse_button.button_index==BUTTON_LEFT && input_action!=INPUT_NONE) {
  470. set_items.clear();
  471. input_action=INPUT_NONE;
  472. return true;
  473. }
  474. if (p_event.mouse_button.button_index==BUTTON_RIGHT && (input_action==INPUT_ERASE || input_action==INPUT_DUPLICATE)) {
  475. input_action=INPUT_NONE;
  476. return true;
  477. }
  478. }
  479. } break;
  480. case InputEvent::MOUSE_MOTION: {
  481. return do_input_action(p_camera,Point2(p_event.mouse_motion.x,p_event.mouse_motion.y),false);
  482. } break;
  483. }
  484. } else if (edit_mode->get_selected()==1) {
  485. //area mode, select an area
  486. switch (p_event.type) {
  487. case InputEvent::MOUSE_BUTTON: {
  488. if (p_event.mouse_button.button_index==BUTTON_LEFT && p_event.mouse_button.pressed) {
  489. Point2 point = Point2(p_event.mouse_motion.x,p_event.mouse_motion.y);
  490. Camera *camera = p_camera;
  491. Vector3 from = camera->project_ray_origin(point);
  492. Vector3 normal = camera->project_ray_normal(point);
  493. Transform local_xform = node->get_global_transform().affine_inverse();
  494. from=local_xform.xform(from);
  495. normal=local_xform.basis.xform(normal).normalized();
  496. List<int> areas;
  497. node->get_area_list(&areas);
  498. float min_d=1e10;
  499. int min_area=-1;
  500. for(List<int>::Element *E=areas.front();E;E=E->next()) {
  501. int area = E->get();
  502. AABB aabb = node->area_get_bounds(area);
  503. aabb.pos*=node->get_cell_size();
  504. aabb.size*=node->get_cell_size();
  505. Vector3 rclip,rnormal;
  506. if (!aabb.intersects_segment(from,from+normal*10000,&rclip,&rnormal))
  507. continue;
  508. float d = normal.dot(rclip);
  509. if (d<min_d) {
  510. min_d=d;
  511. min_area=area;
  512. }
  513. }
  514. selected_area=min_area;
  515. update_areas();
  516. }
  517. } break;
  518. }
  519. }
  520. return false;
  521. }
  522. struct _CGMEItemSort {
  523. String name;
  524. int id;
  525. _FORCE_INLINE_ bool operator<(const _CGMEItemSort& r_it) const { return name < r_it.name; }
  526. };
  527. void GridMapEditor::_set_display_mode(int p_mode) {
  528. if (display_mode==p_mode) {
  529. return;
  530. }
  531. if (p_mode == DISPLAY_LIST) {
  532. mode_list->set_pressed(true);
  533. mode_thumbnail->set_pressed(false);
  534. } else if (p_mode == DISPLAY_THUMBNAIL) {
  535. mode_list->set_pressed(false);
  536. mode_thumbnail->set_pressed(true);
  537. }
  538. display_mode=p_mode;
  539. update_pallete();
  540. }
  541. void GridMapEditor::update_pallete() {
  542. int selected = theme_pallete->get_current();
  543. theme_pallete->clear();
  544. if (display_mode == DISPLAY_THUMBNAIL) {
  545. theme_pallete->set_max_columns(0);
  546. theme_pallete->set_icon_mode(ItemList::ICON_MODE_TOP);
  547. } else if (display_mode == DISPLAY_LIST){
  548. theme_pallete->set_max_columns(1);
  549. theme_pallete->set_icon_mode(ItemList::ICON_MODE_LEFT);
  550. }
  551. float min_size = EDITOR_DEF("grid_map/preview_size",64);
  552. theme_pallete->set_fixed_icon_size(Size2(min_size, min_size));
  553. theme_pallete->set_fixed_column_width(min_size*3/2);
  554. theme_pallete->set_max_text_lines(2);
  555. Ref<MeshLibrary> theme = node->get_theme();
  556. if (theme.is_null()) {
  557. last_theme=NULL;
  558. return;
  559. }
  560. Vector<int> ids;
  561. ids = theme->get_item_list();
  562. List<_CGMEItemSort> il;
  563. for(int i=0;i<ids.size();i++) {
  564. _CGMEItemSort is;
  565. is.id=ids[i];
  566. is.name=theme->get_item_name(ids[i]);
  567. il.push_back(is);
  568. }
  569. il.sort();
  570. int item = 0;
  571. for(List<_CGMEItemSort>::Element *E=il.front();E;E=E->next()) {
  572. int id = E->get().id;
  573. theme_pallete->add_item("");
  574. String name=theme->get_item_name(id);
  575. Ref<Texture> preview = theme->get_item_preview(id);
  576. if (!preview.is_null()) {
  577. theme_pallete->set_item_icon(item, preview);
  578. theme_pallete->set_item_tooltip(item, name);
  579. }
  580. if (name!="") {
  581. theme_pallete->set_item_text(item,name);
  582. }
  583. theme_pallete->set_item_metadata(item, id);
  584. item++;
  585. }
  586. if (selected!=-1) {
  587. theme_pallete->select(selected);
  588. }
  589. last_theme=theme.operator->();
  590. }
  591. void GridMapEditor::_area_renamed() {
  592. TreeItem * it = area_list->get_selected();
  593. int area = it->get_metadata(0);
  594. if (area<1)
  595. return;
  596. node->area_set_name(area,it->get_text(0));
  597. }
  598. void GridMapEditor::_area_selected() {
  599. TreeItem * it = area_list->get_selected();
  600. int area = it->get_metadata(0);
  601. if (area<1)
  602. return;
  603. selected_area=area;
  604. }
  605. void GridMapEditor::update_areas() {
  606. area_list->clear();
  607. List<int> areas;
  608. node->get_area_list(&areas);
  609. TreeItem *root = area_list->create_item(NULL);
  610. area_list->set_hide_root(true);
  611. TreeItem *selected=NULL;
  612. for (List<int>::Element *E=areas.front();E;E=E->next()) {
  613. int area = E->get();
  614. TreeItem *ti = area_list->create_item(root);
  615. String name=node->area_get_name(area);
  616. ti->set_metadata(0,area);
  617. ti->set_text(0,name);
  618. ti->set_editable(0,true);
  619. if (area==selected_area)
  620. selected=ti;
  621. }
  622. if (selected)
  623. selected->select(0);
  624. }
  625. void GridMapEditor::edit(GridMap *p_gridmap) {
  626. node=p_gridmap;
  627. VS *vs = VS::get_singleton();
  628. last_mouseover=Vector3(-1,-1,-1);
  629. input_action=INPUT_NONE;
  630. selection.active=false;
  631. _update_selection_transform();
  632. _update_duplicate_indicator();
  633. spatial_editor = editor->get_editor_plugin_screen()->cast_to<SpatialEditorPlugin>();
  634. if (!node) {
  635. set_process(false);
  636. for(int i=0;i<3;i++) {
  637. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  638. }
  639. VisualServer::get_singleton()->instance_geometry_set_flag(cursor_instance, VS::INSTANCE_FLAG_VISIBLE,false);
  640. _clear_areas();
  641. return;
  642. }
  643. update_pallete();
  644. update_areas();
  645. set_process(true);
  646. Vector3 edited_floor = p_gridmap->get_meta("_editor_floor_");
  647. clip_mode=p_gridmap->has_meta("_editor_clip_")?ClipMode(p_gridmap->get_meta("_editor_clip_").operator int()):CLIP_DISABLED;
  648. for(int i=0;i<3;i++) {
  649. if (vs->mesh_get_surface_count(grid[i])>0)
  650. vs->mesh_remove_surface(grid[i],0);
  651. edit_floor[i]=edited_floor[i];
  652. }
  653. {
  654. //update grids
  655. indicator_mat = VisualServer::get_singleton()->fixed_material_create();
  656. VisualServer::get_singleton()->material_set_flag( indicator_mat, VisualServer::MATERIAL_FLAG_UNSHADED, true );
  657. VisualServer::get_singleton()->material_set_flag( indicator_mat, VisualServer::MATERIAL_FLAG_ONTOP, false );
  658. VisualServer::get_singleton()->fixed_material_set_param(indicator_mat,VisualServer::FIXED_MATERIAL_PARAM_DIFFUSE,Color(0.8,0.5,0.1));
  659. VisualServer::get_singleton()->fixed_material_set_flag( indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true );
  660. VisualServer::get_singleton()->fixed_material_set_flag( indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY, true );
  661. Vector<Vector3> grid_points[3];
  662. Vector<Color> grid_colors[3];
  663. float cell_size[3]={p_gridmap->get_cell_size(),p_gridmap->get_cell_size(),p_gridmap->get_cell_size()};
  664. for(int i=0;i<3;i++) {
  665. Vector3 axis;
  666. axis[i]=1;
  667. Vector3 axis_n1;
  668. axis_n1[(i+1)%3]=cell_size[(i+1)%3];;
  669. Vector3 axis_n2;
  670. axis_n2[(i+2)%3]=cell_size[(i+2)%3];
  671. for(int j=-GRID_CURSOR_SIZE;j<=GRID_CURSOR_SIZE;j++) {
  672. for(int k=-GRID_CURSOR_SIZE;k<=GRID_CURSOR_SIZE;k++) {
  673. Vector3 p = axis_n1*j + axis_n2 *k;
  674. float trans = Math::pow(MAX(0,1.0-(Vector2(j,k).length()/GRID_CURSOR_SIZE)),2);
  675. Vector3 pj = axis_n1*(j+1) + axis_n2 *k;
  676. float transj = Math::pow(MAX(0,1.0-(Vector2(j+1,k).length()/GRID_CURSOR_SIZE)),2);
  677. Vector3 pk = axis_n1*j + axis_n2 *(k+1);
  678. float transk = Math::pow(MAX(0,1.0-(Vector2(j,k+1).length()/GRID_CURSOR_SIZE)),2);
  679. grid_points[i].push_back(p);
  680. grid_points[i].push_back(pk);
  681. grid_colors[i].push_back(Color(1,1,1,trans));
  682. grid_colors[i].push_back(Color(1,1,1,transk));
  683. grid_points[i].push_back(p);
  684. grid_points[i].push_back(pj);
  685. grid_colors[i].push_back(Color(1,1,1,trans));
  686. grid_colors[i].push_back(Color(1,1,1,transj));
  687. }
  688. }
  689. Array d;
  690. d.resize(VS::ARRAY_MAX);
  691. d[VS::ARRAY_VERTEX]=grid_points[i];
  692. d[VS::ARRAY_COLOR]=grid_colors[i];
  693. VisualServer::get_singleton()->mesh_add_surface(grid[i],VisualServer::PRIMITIVE_LINES,d);
  694. VisualServer::get_singleton()->mesh_surface_set_material(grid[i],0,indicator_mat);
  695. }
  696. }
  697. update_grid();
  698. _update_clip();
  699. _update_areas_display();
  700. }
  701. void GridMapEditor::_update_clip() {
  702. node->set_meta("_editor_clip_",clip_mode);
  703. if (clip_mode==CLIP_DISABLED)
  704. node->set_clip(false);
  705. else
  706. node->set_clip(true,clip_mode==CLIP_ABOVE,edit_floor[edit_axis],edit_axis);
  707. }
  708. void GridMapEditor::update_grid() {
  709. grid_xform.origin.x-=1; //force update in hackish way.. what do i care
  710. //VS *vs = VS::get_singleton();
  711. grid_ofs[edit_axis]=edit_floor[edit_axis]*node->get_cell_size();
  712. edit_grid_xform.origin=grid_ofs;
  713. edit_grid_xform.basis=Matrix3();
  714. for(int i=0;i<3;i++) {
  715. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,i==edit_axis);
  716. }
  717. updating=true;
  718. floor->set_value(edit_floor[edit_axis]);
  719. updating=false;
  720. }
  721. void GridMapEditor::_notification(int p_what) {
  722. if (p_what==NOTIFICATION_ENTER_TREE) {
  723. theme_pallete->connect("item_selected", this,"_item_selected_cbk");
  724. edit_mode->connect("item_selected", this,"_edit_mode_changed");
  725. area_list->connect("item_edited", this,"_area_renamed");
  726. area_list->connect("item_selected", this,"_area_selected");
  727. for(int i=0;i<3;i++) {
  728. grid[i]=VS::get_singleton()->mesh_create();
  729. grid_instance[i]=VS::get_singleton()->instance_create2(grid[i],get_tree()->get_root()->get_world()->get_scenario());
  730. }
  731. selection_instance = VisualServer::get_singleton()->instance_create2(selection_mesh,get_tree()->get_root()->get_world()->get_scenario());
  732. duplicate_instance = VisualServer::get_singleton()->instance_create2(duplicate_mesh,get_tree()->get_root()->get_world()->get_scenario());
  733. _update_selection_transform();
  734. _update_duplicate_indicator();
  735. } else if (p_what==NOTIFICATION_EXIT_TREE) {
  736. for(int i=0;i<3;i++) {
  737. VS::get_singleton()->free(grid_instance[i]);
  738. VS::get_singleton()->free(grid[i]);
  739. grid_instance[i]=RID();
  740. grid[i]=RID();
  741. }
  742. VisualServer::get_singleton()->free(selection_instance);
  743. VisualServer::get_singleton()->free(duplicate_instance);
  744. selection_instance=RID();
  745. duplicate_instance=RID();
  746. } else if (p_what==NOTIFICATION_PROCESS) {
  747. Transform xf = node->get_global_transform();
  748. if (xf!=grid_xform) {
  749. for(int i=0;i<3;i++) {
  750. VS::get_singleton()->instance_set_transform(grid_instance[i],xf * edit_grid_xform);
  751. }
  752. grid_xform=xf;
  753. }
  754. Ref<MeshLibrary> cgmt = node->get_theme();
  755. if (cgmt.operator->()!=last_theme)
  756. update_pallete();
  757. if (lock_view) {
  758. EditorNode*editor = get_tree()->get_root()->get_child(0)->cast_to<EditorNode>();
  759. Plane p;
  760. p.normal[edit_axis]=1.0;
  761. p.d=edit_floor[edit_axis]*node->get_cell_size();
  762. p = node->get_transform().xform(p); // plane to snap
  763. SpatialEditorPlugin *sep = editor->get_editor_plugin_screen()->cast_to<SpatialEditorPlugin>();
  764. if (sep)
  765. sep->snap_cursor_to_plane(p);
  766. //editor->get_editor_plugin_screen()->call("snap_cursor_to_plane",p);
  767. }
  768. }
  769. }
  770. void GridMapEditor::_update_cursor_instance() {
  771. if (!node) {
  772. return;
  773. }
  774. if (cursor_instance.is_valid())
  775. VisualServer::get_singleton()->free(cursor_instance);
  776. cursor_instance=RID();
  777. if (selected_pallete>=0) {
  778. if (node && !node->get_theme().is_null()) {
  779. Ref<Mesh> mesh = node->get_theme()->get_item_mesh(selected_pallete);
  780. if (!mesh.is_null() && mesh->get_rid().is_valid()) {
  781. cursor_instance=VisualServer::get_singleton()->instance_create2(mesh->get_rid(),get_tree()->get_root()->get_world()->get_scenario());
  782. VisualServer::get_singleton()->instance_set_transform(cursor_instance,cursor_transform);
  783. }
  784. }
  785. }
  786. }
  787. void GridMapEditor::_item_selected_cbk(int idx) {
  788. selected_pallete=theme_pallete->get_item_metadata(idx);
  789. _update_cursor_instance();
  790. }
  791. void GridMapEditor::_clear_areas() {
  792. for(int i=0;i<areas.size();i++) {
  793. VisualServer::get_singleton()->free(areas[i].instance);
  794. VisualServer::get_singleton()->free(areas[i].mesh);
  795. }
  796. areas.clear();
  797. }
  798. void GridMapEditor::_update_areas_display() {
  799. if (!node) {
  800. return;
  801. }
  802. _clear_areas();
  803. List<int> areas;
  804. node->get_area_list(&areas);
  805. Transform global_xf = node->get_global_transform();
  806. for(List<int>::Element *E=areas.front();E;E=E->next()) {
  807. int area = E->get();
  808. Color color;
  809. if (node->area_is_exterior_portal(area))
  810. color=Color(1,1,1,0.2);
  811. else
  812. color.set_hsv(Math::fmod(area*0.37,1),Math::fmod(area*0.75,1),1.0,0.2);
  813. RID material = VisualServer::get_singleton()->fixed_material_create();
  814. VisualServer::get_singleton()->fixed_material_set_param( material, VS::FIXED_MATERIAL_PARAM_DIFFUSE,color );
  815. VisualServer::get_singleton()->fixed_material_set_param( material, VS::FIXED_MATERIAL_PARAM_EMISSION,0.5 );
  816. VisualServer::get_singleton()->fixed_material_set_flag( material, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true );
  817. RID mesh = VisualServer::get_singleton()->mesh_create();
  818. DVector<Plane> planes;
  819. for(int i=0;i<3;i++) {
  820. Vector3 axis;
  821. axis[i]=1.0;
  822. planes.push_back(Plane(axis,1));
  823. planes.push_back(Plane(-axis,0));
  824. }
  825. VisualServer::get_singleton()->mesh_add_surface_from_planes(mesh,planes);
  826. VisualServer::get_singleton()->mesh_surface_set_material(mesh,0,material,true);
  827. AreaDisplay ad;
  828. ad.mesh=mesh;
  829. ad.instance = VisualServer::get_singleton()->instance_create2(mesh,node->get_world()->get_scenario());
  830. Transform xform;
  831. AABB aabb = node->area_get_bounds(area);
  832. xform.origin=aabb.pos * node->get_cell_size();
  833. xform.basis.scale(aabb.size * node->get_cell_size());
  834. VisualServer::get_singleton()->instance_set_transform(ad.instance,global_xf * xform);
  835. this->areas.push_back(ad);
  836. }
  837. }
  838. void GridMapEditor::_edit_mode_changed(int p_what) {
  839. if (p_what==0) {
  840. theme_pallete->show();
  841. area_list->hide();
  842. } else {
  843. theme_pallete->hide();
  844. area_list->show();
  845. }
  846. }
  847. void GridMapEditor::_floor_changed(float p_value) {
  848. if (updating)
  849. return;
  850. edit_floor[edit_axis]=p_value;
  851. node->set_meta("_editor_floor_",Vector3(edit_floor[0],edit_floor[1],edit_floor[2]));
  852. update_grid();
  853. _update_clip();
  854. }
  855. void GridMapEditor::_bind_methods() {
  856. ClassDB::bind_method("_menu_option",&GridMapEditor::_menu_option);
  857. ClassDB::bind_method("_configure",&GridMapEditor::_configure);
  858. ClassDB::bind_method("_item_selected_cbk",&GridMapEditor::_item_selected_cbk);
  859. ClassDB::bind_method("_edit_mode_changed",&GridMapEditor::_edit_mode_changed);
  860. ClassDB::bind_method("_area_renamed",&GridMapEditor::_area_renamed);
  861. ClassDB::bind_method("_area_selected",&GridMapEditor::_area_selected);
  862. ClassDB::bind_method("_floor_changed",&GridMapEditor::_floor_changed);
  863. ClassDB::bind_method(_MD("_set_display_mode","mode"), &GridMapEditor::_set_display_mode);
  864. }
  865. GridMapEditor::GridMapEditor(EditorNode *p_editor) {
  866. input_action=INPUT_NONE;
  867. editor=p_editor;
  868. undo_redo=p_editor->get_undo_redo();
  869. int mw = EDITOR_DEF("grid_map/palette_min_width",230);
  870. Control *ec = memnew( Control);
  871. ec->set_custom_minimum_size(Size2(mw,0));
  872. add_child(ec);
  873. spatial_editor_hb = memnew( HBoxContainer );
  874. SpatialEditor::get_singleton()->add_control_to_menu_panel(spatial_editor_hb);
  875. options = memnew( MenuButton );
  876. spatial_editor_hb->add_child(options);
  877. spatial_editor_hb->hide();
  878. options->set_text("Grid");
  879. options->get_popup()->add_check_item("Snap View",MENU_OPTION_LOCK_VIEW);
  880. options->get_popup()->add_separator();
  881. options->get_popup()->add_item("Prev Level ("+keycode_get_string(KEY_MASK_CMD)+"Down Wheel)",MENU_OPTION_PREV_LEVEL);
  882. options->get_popup()->add_item("Next Level ("+keycode_get_string(KEY_MASK_CMD)+"Up Wheel)",MENU_OPTION_NEXT_LEVEL);
  883. options->get_popup()->add_separator();
  884. options->get_popup()->add_check_item("Clip Disabled",MENU_OPTION_CLIP_DISABLED);
  885. options->get_popup()->set_item_checked( options->get_popup()->get_item_index(MENU_OPTION_CLIP_DISABLED), true );
  886. options->get_popup()->add_check_item("Clip Above",MENU_OPTION_CLIP_ABOVE);
  887. options->get_popup()->add_check_item("Clip Below",MENU_OPTION_CLIP_BELOW);
  888. options->get_popup()->add_separator();
  889. options->get_popup()->add_check_item("Edit X Axis",MENU_OPTION_X_AXIS,KEY_Z);
  890. options->get_popup()->add_check_item("Edit Y Axis",MENU_OPTION_Y_AXIS,KEY_X);
  891. options->get_popup()->add_check_item("Edit Z Axis",MENU_OPTION_Z_AXIS,KEY_C);
  892. options->get_popup()->set_item_checked( options->get_popup()->get_item_index(MENU_OPTION_Y_AXIS), true );
  893. options->get_popup()->add_separator();
  894. options->get_popup()->add_item("Cursor Rotate X",MENU_OPTION_CURSOR_ROTATE_X,KEY_A);
  895. options->get_popup()->add_item("Cursor Rotate Y",MENU_OPTION_CURSOR_ROTATE_Y,KEY_S);
  896. options->get_popup()->add_item("Cursor Rotate Z",MENU_OPTION_CURSOR_ROTATE_Z,KEY_D);
  897. options->get_popup()->add_item("Cursor Back Rotate X",MENU_OPTION_CURSOR_BACK_ROTATE_X,KEY_MASK_SHIFT+KEY_A);
  898. options->get_popup()->add_item("Cursor Back Rotate Y",MENU_OPTION_CURSOR_BACK_ROTATE_Y,KEY_MASK_SHIFT+KEY_S);
  899. options->get_popup()->add_item("Cursor Back Rotate Z",MENU_OPTION_CURSOR_BACK_ROTATE_Z,KEY_MASK_SHIFT+KEY_D);
  900. options->get_popup()->add_item("Cursor Clear Rotation",MENU_OPTION_CURSOR_CLEAR_ROTATION,KEY_W);
  901. options->get_popup()->add_separator();
  902. options->get_popup()->add_check_item("Duplicate Selects",MENU_OPTION_DUPLICATE_SELECTS);
  903. options->get_popup()->add_separator();
  904. options->get_popup()->add_item("Create Area",MENU_OPTION_SELECTION_MAKE_AREA,KEY_CONTROL+KEY_C);
  905. options->get_popup()->add_item("Create Exterior Connector",MENU_OPTION_SELECTION_MAKE_EXTERIOR_CONNECTOR);
  906. options->get_popup()->add_item("Erase Area",MENU_OPTION_REMOVE_AREA);
  907. options->get_popup()->add_separator();
  908. options->get_popup()->add_item("Selection -> Duplicate",MENU_OPTION_SELECTION_DUPLICATE,KEY_MASK_SHIFT+KEY_INSERT);
  909. options->get_popup()->add_item("Selection -> Clear",MENU_OPTION_SELECTION_CLEAR,KEY_MASK_SHIFT+KEY_DELETE);
  910. //options->get_popup()->add_separator();
  911. //options->get_popup()->add_item("Configure",MENU_OPTION_CONFIGURE);
  912. options->get_popup()->add_separator();
  913. options->get_popup()->add_item("Settings", MENU_OPTION_GRIDMAP_SETTINGS);
  914. settings_dialog = memnew(ConfirmationDialog);
  915. settings_dialog->set_title("GridMap Settings");
  916. add_child(settings_dialog);
  917. settings_vbc = memnew(VBoxContainer);
  918. settings_vbc->set_custom_minimum_size(Size2(200, 0));
  919. settings_dialog->add_child(settings_vbc);
  920. settings_dialog->set_child_rect(settings_vbc);
  921. settings_pick_distance = memnew(SpinBox);
  922. settings_pick_distance->set_max(10000.0f);
  923. settings_pick_distance->set_min(500.0f);
  924. settings_pick_distance->set_step(1.0f);
  925. settings_pick_distance->set_value(EDITOR_DEF("grid_map/pick_distance", 5000.0));
  926. settings_vbc->add_margin_child("Pick Distance:", settings_pick_distance);
  927. clip_mode=CLIP_DISABLED;
  928. options->get_popup()->connect("item_pressed", this,"_menu_option");
  929. HBoxContainer *hb = memnew( HBoxContainer );
  930. add_child(hb);
  931. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  932. edit_mode = memnew(OptionButton);
  933. edit_mode->set_area_as_parent_rect();
  934. edit_mode->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_BEGIN,24);;
  935. edit_mode->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_END,14);;
  936. edit_mode->add_item("Tiles");
  937. edit_mode->add_item("Areas");
  938. hb->add_child(edit_mode);
  939. edit_mode->set_h_size_flags(SIZE_EXPAND_FILL);
  940. mode_thumbnail = memnew( ToolButton );
  941. mode_thumbnail->set_toggle_mode(true);
  942. mode_thumbnail->set_pressed(true);
  943. mode_thumbnail->set_icon(p_editor->get_gui_base()->get_icon("FileThumbnail","EditorIcons"));
  944. hb->add_child(mode_thumbnail);
  945. mode_thumbnail->connect("pressed", this, "_set_display_mode", varray(DISPLAY_THUMBNAIL));
  946. mode_list = memnew( ToolButton );
  947. mode_list->set_toggle_mode(true);
  948. mode_list->set_pressed(false);
  949. mode_list->set_icon(p_editor->get_gui_base()->get_icon("FileList", "EditorIcons"));
  950. hb->add_child(mode_list);
  951. mode_list->connect("pressed", this, "_set_display_mode", varray(DISPLAY_LIST));
  952. display_mode = DISPLAY_THUMBNAIL;
  953. selected_area=-1;
  954. theme_pallete = memnew( ItemList );
  955. add_child(theme_pallete);
  956. theme_pallete->set_v_size_flags(SIZE_EXPAND_FILL);
  957. area_list = memnew( Tree );
  958. add_child(area_list);
  959. area_list->set_v_size_flags(SIZE_EXPAND_FILL);
  960. area_list->hide();
  961. spatial_editor_hb->add_child(memnew(VSeparator));
  962. Label *fl = memnew(Label);
  963. fl->set_text(" Floor: ");
  964. spatial_editor_hb->add_child(fl);
  965. floor = memnew( SpinBox );
  966. floor->set_min(-32767);
  967. floor->set_max(32767);
  968. floor->set_step(1);
  969. floor->get_line_edit()->add_constant_override("minimum_spaces",16);
  970. spatial_editor_hb->add_child(floor);
  971. floor->connect("value_changed",this,"_floor_changed");
  972. edit_axis=Vector3::AXIS_Y;
  973. edit_floor[0]=-1;
  974. edit_floor[1]=-1;
  975. edit_floor[2]=-1;
  976. cursor_visible=false;
  977. selected_pallete=-1;
  978. lock_view=false;
  979. cursor_rot=0;
  980. last_mouseover=Vector3(-1,-1,-1);
  981. selection_mesh = VisualServer::get_singleton()->mesh_create();
  982. duplicate_mesh = VisualServer::get_singleton()->mesh_create();
  983. {
  984. //selection mesh create
  985. DVector<Vector3> lines;
  986. DVector<Vector3> triangles;
  987. for (int i=0;i<6;i++) {
  988. Vector3 face_points[4];
  989. for (int j=0;j<4;j++) {
  990. float v[3];
  991. v[0]=1.0;
  992. v[1]=1-2*((j>>1)&1);
  993. v[2]=v[1]*(1-2*(j&1));
  994. for (int k=0;k<3;k++) {
  995. if (i<3)
  996. face_points[j][(i+k)%3]=v[k]*(i>=3?-1:1);
  997. else
  998. face_points[3-j][(i+k)%3]=v[k]*(i>=3?-1:1);
  999. }
  1000. }
  1001. triangles.push_back(face_points[0]*0.5+Vector3(0.5,0.5,0.5));
  1002. triangles.push_back(face_points[1]*0.5+Vector3(0.5,0.5,0.5));
  1003. triangles.push_back(face_points[2]*0.5+Vector3(0.5,0.5,0.5));
  1004. triangles.push_back(face_points[2]*0.5+Vector3(0.5,0.5,0.5));
  1005. triangles.push_back(face_points[3]*0.5+Vector3(0.5,0.5,0.5));
  1006. triangles.push_back(face_points[0]*0.5+Vector3(0.5,0.5,0.5));
  1007. }
  1008. for(int i=0;i<12;i++) {
  1009. AABB base(Vector3(0,0,0),Vector3(1,1,1));
  1010. Vector3 a,b;
  1011. base.get_edge(i,a,b);
  1012. lines.push_back(a);
  1013. lines.push_back(b);
  1014. }
  1015. Array d;
  1016. d.resize(VS::ARRAY_MAX);
  1017. inner_mat = VisualServer::get_singleton()->fixed_material_create();
  1018. VisualServer::get_singleton()->fixed_material_set_param(inner_mat,VS::FIXED_MATERIAL_PARAM_DIFFUSE,Color(0.7,0.7,1.0,0.3));
  1019. VisualServer::get_singleton()->material_set_flag(inner_mat,VS::MATERIAL_FLAG_ONTOP,true);
  1020. VisualServer::get_singleton()->material_set_flag(inner_mat,VS::MATERIAL_FLAG_UNSHADED,true);
  1021. VisualServer::get_singleton()->fixed_material_set_flag( inner_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true );
  1022. d[VS::ARRAY_VERTEX]=triangles;
  1023. VisualServer::get_singleton()->mesh_add_surface(selection_mesh,VS::PRIMITIVE_TRIANGLES,d);
  1024. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh,0,inner_mat);
  1025. outer_mat = VisualServer::get_singleton()->fixed_material_create();
  1026. VisualServer::get_singleton()->fixed_material_set_param(outer_mat,VS::FIXED_MATERIAL_PARAM_DIFFUSE,Color(0.7,0.7,1.0,0.8));
  1027. VisualServer::get_singleton()->material_set_line_width(outer_mat,3.0);
  1028. VisualServer::get_singleton()->material_set_flag(outer_mat,VS::MATERIAL_FLAG_ONTOP,true);
  1029. VisualServer::get_singleton()->material_set_flag(outer_mat,VS::MATERIAL_FLAG_UNSHADED,true);
  1030. VisualServer::get_singleton()->fixed_material_set_flag( outer_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true );
  1031. d[VS::ARRAY_VERTEX]=lines;
  1032. VisualServer::get_singleton()->mesh_add_surface(selection_mesh,VS::PRIMITIVE_LINES,d);
  1033. VisualServer::get_singleton()->mesh_surface_set_material(selection_mesh,1,outer_mat);
  1034. inner_mat_dup = VisualServer::get_singleton()->fixed_material_create();
  1035. VisualServer::get_singleton()->fixed_material_set_param(inner_mat_dup,VS::FIXED_MATERIAL_PARAM_DIFFUSE,Color(1.0,0.7,0.7,0.3));
  1036. VisualServer::get_singleton()->material_set_flag(inner_mat_dup,VS::MATERIAL_FLAG_ONTOP,true);
  1037. VisualServer::get_singleton()->material_set_flag(inner_mat_dup,VS::MATERIAL_FLAG_UNSHADED,true);
  1038. VisualServer::get_singleton()->fixed_material_set_flag( inner_mat_dup, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true );
  1039. d[VS::ARRAY_VERTEX]=triangles;
  1040. VisualServer::get_singleton()->mesh_add_surface(duplicate_mesh,VS::PRIMITIVE_TRIANGLES,d);
  1041. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh,0,inner_mat_dup);
  1042. outer_mat_dup = VisualServer::get_singleton()->fixed_material_create();
  1043. VisualServer::get_singleton()->fixed_material_set_param(outer_mat_dup,VS::FIXED_MATERIAL_PARAM_DIFFUSE,Color(1.0,0.7,0.7,0.8));
  1044. VisualServer::get_singleton()->material_set_line_width(outer_mat_dup,3.0);
  1045. VisualServer::get_singleton()->material_set_flag(outer_mat_dup,VS::MATERIAL_FLAG_ONTOP,true);
  1046. VisualServer::get_singleton()->material_set_flag(outer_mat_dup,VS::MATERIAL_FLAG_UNSHADED,true);
  1047. VisualServer::get_singleton()->fixed_material_set_flag( outer_mat_dup, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA, true );
  1048. d[VS::ARRAY_VERTEX]=lines;
  1049. VisualServer::get_singleton()->mesh_add_surface(duplicate_mesh,VS::PRIMITIVE_LINES,d);
  1050. VisualServer::get_singleton()->mesh_surface_set_material(duplicate_mesh,1,outer_mat_dup);
  1051. }
  1052. selection.active=false;
  1053. updating=false;
  1054. }
  1055. GridMapEditor::~GridMapEditor() {
  1056. for(int i=0;i<3;i++) {
  1057. if (grid[i].is_valid())
  1058. VisualServer::get_singleton()->free(grid[i]);
  1059. if (grid_instance[i].is_valid())
  1060. VisualServer::get_singleton()->free(grid_instance[i]);
  1061. if (cursor_instance)
  1062. VisualServer::get_singleton()->free(cursor_instance);
  1063. }
  1064. VisualServer::get_singleton()->free(inner_mat);
  1065. VisualServer::get_singleton()->free(outer_mat);
  1066. VisualServer::get_singleton()->free(inner_mat_dup);
  1067. VisualServer::get_singleton()->free(outer_mat_dup);
  1068. VisualServer::get_singleton()->free(selection_mesh);
  1069. if (selection_instance.is_valid())
  1070. VisualServer::get_singleton()->free(selection_instance);
  1071. VisualServer::get_singleton()->free(duplicate_mesh);
  1072. if (duplicate_instance.is_valid())
  1073. VisualServer::get_singleton()->free(duplicate_instance);
  1074. _clear_areas();
  1075. }
  1076. void GridMapEditorPlugin::edit(Object *p_object) {
  1077. gridmap_editor->edit(p_object?p_object->cast_to<GridMap>():NULL);
  1078. }
  1079. bool GridMapEditorPlugin::handles(Object *p_object) const {
  1080. return p_object->is_class("GridMap");
  1081. }
  1082. void GridMapEditorPlugin::make_visible(bool p_visible) {
  1083. if (p_visible) {
  1084. gridmap_editor->show();
  1085. gridmap_editor->spatial_editor_hb->show();
  1086. gridmap_editor->set_process(true);
  1087. } else {
  1088. gridmap_editor->spatial_editor_hb->hide();
  1089. gridmap_editor->hide();
  1090. gridmap_editor->edit(NULL);
  1091. gridmap_editor->set_process(false);
  1092. }
  1093. }
  1094. GridMapEditorPlugin::GridMapEditorPlugin(EditorNode *p_node) {
  1095. editor=p_node;
  1096. gridmap_editor = memnew( GridMapEditor(editor) );
  1097. SpatialEditor::get_singleton()->get_palette_split()->add_child(gridmap_editor);
  1098. SpatialEditor::get_singleton()->get_palette_split()->move_child(gridmap_editor,0);
  1099. gridmap_editor->hide();
  1100. }
  1101. GridMapEditorPlugin::~GridMapEditorPlugin()
  1102. {
  1103. }