spatial_editor_plugin.cpp 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185
  1. /*************************************************************************/
  2. /* spatial_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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 "spatial_editor_plugin.h"
  30. #include "print_string.h"
  31. #include "os/keyboard.h"
  32. #include "scene/3d/visual_instance.h"
  33. #include "scene/3d/camera.h"
  34. #include "camera_matrix.h"
  35. #include "sort.h"
  36. #include "tools/editor/editor_node.h"
  37. #include "tools/editor/editor_settings.h"
  38. #include "scene/resources/surface_tool.h"
  39. #include "tools/editor/spatial_editor_gizmos.h"
  40. #include "globals.h"
  41. #include "tools/editor/plugins/animation_player_editor_plugin.h"
  42. #include "tools/editor/animation_editor.h"
  43. #define DISTANCE_DEFAULT 4
  44. #define GIZMO_ARROW_SIZE 0.3
  45. #define GIZMO_RING_HALF_WIDTH 0.1
  46. //#define GIZMO_SCALE_DEFAULT 0.28
  47. #define GIZMO_SCALE_DEFAULT 0.15
  48. void SpatialEditorViewport::_update_camera() {
  49. if (orthogonal) {
  50. //camera->set_orthogonal(size.width*cursor.distance,get_znear(),get_zfar());
  51. camera->set_orthogonal(2 * cursor.distance, 0.1, 8192);
  52. }
  53. else
  54. camera->set_perspective(get_fov(), get_znear(), get_zfar());
  55. Transform camera_transform;
  56. camera_transform.translate(cursor.pos);
  57. camera_transform.basis.rotate(Vector3(0, 1, 0), cursor.y_rot);
  58. camera_transform.basis.rotate(Vector3(1, 0, 0), cursor.x_rot);
  59. if (orthogonal)
  60. camera_transform.translate(0, 0, 4096);
  61. else
  62. camera_transform.translate(0, 0, cursor.distance);
  63. if (camera->get_global_transform() != camera_transform) {
  64. camera->set_global_transform(camera_transform);
  65. update_transform_gizmo_view();
  66. }
  67. }
  68. String SpatialEditorGizmo::get_handle_name(int p_idx) const {
  69. if (get_script_instance() && get_script_instance()->has_method("get_handle_name"))
  70. return get_script_instance()->call("get_handle_name", p_idx);
  71. return "";
  72. }
  73. Variant SpatialEditorGizmo::get_handle_value(int p_idx) const{
  74. if (get_script_instance() && get_script_instance()->has_method("get_handle_value"))
  75. return get_script_instance()->call("get_handle_value", p_idx);
  76. return Variant();
  77. }
  78. void SpatialEditorGizmo::set_handle(int p_idx,Camera *p_camera, const Point2& p_point) {
  79. if (get_script_instance() && get_script_instance()->has_method("set_handle"))
  80. get_script_instance()->call("set_handle", p_idx, p_camera, p_point);
  81. }
  82. void SpatialEditorGizmo::commit_handle(int p_idx,const Variant& p_restore,bool p_cancel){
  83. if (get_script_instance() && get_script_instance()->has_method("commit_handle"))
  84. get_script_instance()->call("commit_handle", p_idx, p_restore, p_cancel);
  85. }
  86. bool SpatialEditorGizmo::intersect_frustum(const Camera *p_camera,const Vector<Plane> &p_frustum) {
  87. return false;
  88. }
  89. bool SpatialEditorGizmo::intersect_ray(const Camera *p_camera, const Point2 &p_point, Vector3& r_pos, Vector3& r_normal,int *r_gizmo_handle,bool p_sec_first) {
  90. return false;
  91. }
  92. SpatialEditorGizmo::SpatialEditorGizmo(){
  93. selected=false;
  94. }
  95. int SpatialEditorViewport::get_selected_count() const {
  96. Map<Node*,Object*> &selection = editor_selection->get_selection();
  97. int count=0;
  98. for(Map<Node*,Object*>::Element *E=selection.front();E;E=E->next()) {
  99. Spatial *sp = E->key()->cast_to<Spatial>();
  100. if (!sp)
  101. continue;
  102. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  103. if (!se)
  104. continue;
  105. count++;
  106. }
  107. return count;
  108. }
  109. float SpatialEditorViewport::get_znear() const {
  110. float val = spatial_editor->get_znear();
  111. if (val<0.001)
  112. val=0.001;
  113. return val;
  114. }
  115. float SpatialEditorViewport::get_zfar() const{
  116. float val = spatial_editor->get_zfar();
  117. if (val<0.001)
  118. val=0.001;
  119. return val;
  120. }
  121. float SpatialEditorViewport::get_fov() const{
  122. float val = spatial_editor->get_fov();
  123. if (val<0.001)
  124. val=0.001;
  125. if (val>89)
  126. val=89;
  127. return val;
  128. }
  129. Transform SpatialEditorViewport::_get_camera_transform() const {
  130. return camera->get_global_transform();
  131. }
  132. Vector3 SpatialEditorViewport::_get_camera_pos() const {
  133. return _get_camera_transform().origin;
  134. }
  135. Point2 SpatialEditorViewport::_point_to_screen(const Vector3& p_point) {
  136. return camera->unproject_position(p_point);
  137. }
  138. Vector3 SpatialEditorViewport::_get_ray_pos(const Vector2& p_pos) const {
  139. return camera->project_ray_origin(p_pos);
  140. }
  141. Vector3 SpatialEditorViewport::_get_camera_normal() const {
  142. return -_get_camera_transform().basis.get_axis(2);
  143. }
  144. Vector3 SpatialEditorViewport::_get_ray(const Vector2& p_pos) {
  145. return camera->project_ray_normal(p_pos);
  146. }
  147. /*
  148. void SpatialEditorViewport::_clear_id(Spatial *p_node) {
  149. editor_selection->remove_node(p_node);
  150. }
  151. */
  152. void SpatialEditorViewport::_clear_selected() {
  153. editor_selection->clear();
  154. }
  155. void SpatialEditorViewport::_select_clicked(bool p_append,bool p_single) {
  156. if (!clicked)
  157. return;
  158. Object *obj = ObjectDB::get_instance(clicked);
  159. if (!obj)
  160. return;
  161. Spatial *sp = obj->cast_to<Spatial>();
  162. if (!sp)
  163. return;
  164. _select(sp, clicked_wants_append,true);
  165. }
  166. void SpatialEditorViewport::_select(Spatial *p_node, bool p_append,bool p_single) {
  167. if (!p_append) {
  168. // should not modify the selection..
  169. editor_selection->clear();
  170. editor_selection->add_node(p_node);
  171. } else {
  172. if (editor_selection->is_selected(p_node) && p_single) {
  173. //erase
  174. editor_selection->remove_node(p_node);
  175. } else {
  176. editor_selection->add_node(p_node);
  177. }
  178. }
  179. }
  180. ObjectID SpatialEditorViewport::_select_ray(const Point2& p_pos, bool p_append,bool &r_includes_current,int *r_gizmo_handle,bool p_alt_select) {
  181. if (r_gizmo_handle)
  182. *r_gizmo_handle=-1;
  183. Vector3 ray=_get_ray(p_pos);
  184. Vector3 pos=_get_ray_pos(p_pos);
  185. Vector<RID> instances=VisualServer::get_singleton()->instances_cull_ray(pos,ray,get_tree()->get_root()->get_world()->get_scenario() );
  186. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  187. //uint32_t closest=0;
  188. // float closest_dist=0;
  189. r_includes_current=false;
  190. List<_RayResult> results;
  191. for (int i=0;i<instances.size();i++) {
  192. uint32_t id=VisualServer::get_singleton()->instance_get_object_instance_ID(instances[i]);
  193. Object *obj=ObjectDB::get_instance(id);
  194. if (!obj)
  195. continue;
  196. Spatial *spat=obj->cast_to<Spatial>();
  197. if (!spat)
  198. continue;
  199. Ref<SpatialEditorGizmo> seg = spat->get_gizmo();
  200. if (!seg.is_valid())
  201. continue;
  202. if (found_gizmos.has(seg))
  203. continue;
  204. found_gizmos.insert(seg);
  205. Vector3 point;
  206. Vector3 normal;
  207. int handle=-1;
  208. bool inters = seg->intersect_ray(camera,p_pos,point,normal,NULL,p_alt_select);
  209. if (!inters)
  210. continue;
  211. float dist = pos.distance_to(point);
  212. if (dist<0)
  213. continue;
  214. if (editor_selection->is_selected(spat))
  215. r_includes_current=true;
  216. _RayResult res;
  217. res.item=spat;
  218. res.depth=dist;
  219. res.handle=handle;
  220. results.push_back(res);
  221. }
  222. if (results.empty())
  223. return 0;
  224. results.sort();
  225. Spatial *s=NULL;
  226. if (!r_includes_current || results.size()==1 || (r_gizmo_handle && results.front()->get().handle>=0)) {
  227. //return the nearest one
  228. s = results.front()->get().item;
  229. if (r_gizmo_handle)
  230. *r_gizmo_handle=results.front()->get().handle;
  231. } else {
  232. //returns the next one from a curent selection
  233. List<_RayResult>::Element *E=results.front();
  234. List<_RayResult>::Element *S=NULL;
  235. while(true) {
  236. //very strange loop algorithm that complies with object selection standards (tm).
  237. if (S==E) {
  238. //went all around and anothing was found
  239. //since can't rotate the selection
  240. //just return the first one
  241. s=results.front()->get().item;
  242. break;
  243. }
  244. if (!S && editor_selection->is_selected(E->get().item)) {
  245. //found an item currently in the selection,
  246. //so start from this one
  247. S=E;
  248. }
  249. if (S && !editor_selection->is_selected(E->get().item)) {
  250. // free item after a selected item, this one is desired.
  251. s=E->get().item;
  252. break;
  253. }
  254. E=E->next();
  255. if (!E) {
  256. if (!S) {
  257. //did a loop but nothing was selected, select first
  258. s=results.front()->get().item;
  259. break;
  260. }
  261. E=results.front();
  262. }
  263. }
  264. }
  265. if (!s)
  266. return 0;
  267. return s->get_instance_ID();
  268. }
  269. void SpatialEditorViewport::_find_items_at_pos(const Point2& p_pos,bool &r_includes_current,Vector<_RayResult> &results,bool p_alt_select) {
  270. Vector3 ray=_get_ray(p_pos);
  271. Vector3 pos=_get_ray_pos(p_pos);
  272. Vector<RID> instances=VisualServer::get_singleton()->instances_cull_ray(pos,ray,get_tree()->get_root()->get_world()->get_scenario() );
  273. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  274. r_includes_current=false;
  275. for (int i=0;i<instances.size();i++) {
  276. uint32_t id=VisualServer::get_singleton()->instance_get_object_instance_ID(instances[i]);
  277. Object *obj=ObjectDB::get_instance(id);
  278. if (!obj)
  279. continue;
  280. Spatial *spat=obj->cast_to<Spatial>();
  281. if (!spat)
  282. continue;
  283. Ref<SpatialEditorGizmo> seg = spat->get_gizmo();
  284. if (!seg.is_valid())
  285. continue;
  286. if (found_gizmos.has(seg))
  287. continue;
  288. found_gizmos.insert(seg);
  289. Vector3 point;
  290. Vector3 normal;
  291. int handle=-1;
  292. bool inters = seg->intersect_ray(camera,p_pos,point,normal,NULL,p_alt_select);
  293. if (!inters)
  294. continue;
  295. float dist = pos.distance_to(point);
  296. if (dist<0)
  297. continue;
  298. if (editor_selection->is_selected(spat))
  299. r_includes_current=true;
  300. _RayResult res;
  301. res.item=spat;
  302. res.depth=dist;
  303. res.handle=handle;
  304. results.push_back(res);
  305. }
  306. if (results.empty())
  307. return;
  308. results.sort();
  309. }
  310. Vector3 SpatialEditorViewport::_get_screen_to_space(const Vector3& p_pos) {
  311. CameraMatrix cm;
  312. cm.set_perspective(get_fov(),get_size().get_aspect(),get_znear(),get_zfar());
  313. float screen_w,screen_h;
  314. cm.get_viewport_size(screen_w,screen_h);
  315. Transform camera_transform;
  316. camera_transform.translate( cursor.pos );
  317. camera_transform.basis.rotate(Vector3(0,1,0),cursor.y_rot);
  318. camera_transform.basis.rotate(Vector3(1,0,0),cursor.x_rot);
  319. camera_transform.translate(0,0,cursor.distance);
  320. return camera_transform.xform(Vector3( ((p_pos.x/get_size().width)*2.0-1.0)*screen_w, ((1.0-(p_pos.y/get_size().height))*2.0-1.0)*screen_h,-get_znear()));
  321. }
  322. void SpatialEditorViewport::_select_region() {
  323. if (cursor.region_begin==cursor.region_end)
  324. return; //nothing really
  325. Vector3 box[4]={
  326. Vector3(
  327. MIN( cursor.region_begin.x, cursor.region_end.x),
  328. MIN( cursor.region_begin.y, cursor.region_end.y),
  329. 0
  330. ),
  331. Vector3(
  332. MAX( cursor.region_begin.x, cursor.region_end.x),
  333. MIN( cursor.region_begin.y, cursor.region_end.y),
  334. 0
  335. ),
  336. Vector3(
  337. MAX( cursor.region_begin.x, cursor.region_end.x),
  338. MAX( cursor.region_begin.y, cursor.region_end.y),
  339. 0
  340. ),
  341. Vector3(
  342. MIN( cursor.region_begin.x, cursor.region_end.x),
  343. MAX( cursor.region_begin.y, cursor.region_end.y),
  344. 0
  345. )
  346. };
  347. Vector<Plane> frustum;
  348. Vector3 cam_pos=_get_camera_pos();
  349. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  350. for(int i=0;i<4;i++) {
  351. Vector3 a=_get_screen_to_space(box[i]);
  352. Vector3 b=_get_screen_to_space(box[(i+1)%4]);
  353. frustum.push_back( Plane(a,b,cam_pos) );
  354. }
  355. Plane near( cam_pos, -_get_camera_normal() );
  356. near.d-=get_znear();
  357. frustum.push_back( near );
  358. Plane far=-near;
  359. far.d+=500.0;
  360. frustum.push_back( far );
  361. Vector<RID> instances=VisualServer::get_singleton()->instances_cull_convex(frustum,get_tree()->get_root()->get_world()->get_scenario());
  362. for (int i=0;i<instances.size();i++) {
  363. uint32_t id=VisualServer::get_singleton()->instance_get_object_instance_ID(instances[i]);
  364. Object *obj=ObjectDB::get_instance(id);
  365. if (!obj)
  366. continue;
  367. Spatial *sp = obj->cast_to<Spatial>();
  368. if (!sp)
  369. continue;
  370. Ref<SpatialEditorGizmo> seg = sp->get_gizmo();
  371. if (!seg.is_valid())
  372. continue;
  373. if (found_gizmos.has(seg))
  374. continue;
  375. if (seg->intersect_frustum(camera,frustum))
  376. _select(sp,true,false);
  377. }
  378. }
  379. void SpatialEditorViewport::_update_name() {
  380. String ortho = orthogonal?TTR("Orthogonal"):TTR("Perspective");
  381. if (name!="")
  382. view_menu->set_text("[ "+name+" "+ortho+" ]");
  383. else
  384. view_menu->set_text("[ "+ortho+" ]");
  385. }
  386. void SpatialEditorViewport::_compute_edit(const Point2& p_point) {
  387. _edit.click_ray=_get_ray( Vector2( p_point.x, p_point.y ) );
  388. _edit.click_ray_pos=_get_ray_pos( Vector2( p_point.x, p_point.y ) );
  389. _edit.plane=TRANSFORM_VIEW;
  390. spatial_editor->update_transform_gizmo();
  391. _edit.center=spatial_editor->get_gizmo_transform().origin;
  392. List<Node*> &selection = editor_selection->get_selected_node_list();
  393. // Vector3 center;
  394. // int nc=0;
  395. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  396. Spatial *sp = E->get()->cast_to<Spatial>();
  397. if (!sp)
  398. continue;
  399. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  400. if (!se)
  401. continue;
  402. se->original=se->sp->get_global_transform();
  403. // center+=se->original.origin;
  404. // nc++;
  405. }
  406. // if (nc)
  407. // _edit.center=center/float(nc);
  408. }
  409. static int _get_key_modifier(const String& p_property) {
  410. switch(EditorSettings::get_singleton()->get(p_property).operator int()) {
  411. case 0: return 0;
  412. case 1: return KEY_SHIFT;
  413. case 2: return KEY_ALT;
  414. case 3: return KEY_META;
  415. case 4: return KEY_CONTROL;
  416. }
  417. return 0;
  418. }
  419. SpatialEditorViewport::NavigationScheme SpatialEditorViewport::_get_navigation_schema(const String& p_property) {
  420. switch(EditorSettings::get_singleton()->get(p_property).operator int()) {
  421. case 0: return NAVIGATION_GODOT;
  422. case 1: return NAVIGATION_MAYA;
  423. case 2: return NAVIGATION_MODO;
  424. }
  425. return NAVIGATION_GODOT;
  426. }
  427. SpatialEditorViewport::NavigationZoomStyle SpatialEditorViewport::_get_navigation_zoom_style(const String& p_property) {
  428. switch(EditorSettings::get_singleton()->get(p_property).operator int()) {
  429. case 0: return NAVIGATION_ZOOM_VERTICAL;
  430. case 1: return NAVIGATION_ZOOM_HORIZONTAL;
  431. }
  432. return NAVIGATION_ZOOM_VERTICAL;
  433. }
  434. bool SpatialEditorViewport::_gizmo_select(const Vector2& p_screenpos,bool p_hilite_only) {
  435. if (!spatial_editor->is_gizmo_visible())
  436. return false;
  437. if (get_selected_count()==0) {
  438. if (p_hilite_only)
  439. spatial_editor->select_gizmo_hilight_axis(-1);
  440. return false;
  441. }
  442. Vector3 ray_pos=_get_ray_pos( Vector2( p_screenpos.x, p_screenpos.y ) );
  443. Vector3 ray=_get_ray( Vector2( p_screenpos.x, p_screenpos.y ) );
  444. Transform gt = spatial_editor->get_gizmo_transform();
  445. float gs=gizmo_scale;
  446. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_MOVE) {
  447. int col_axis=-1;
  448. float col_d=1e20;
  449. for(int i=0;i<3;i++) {
  450. Vector3 grabber_pos = gt.origin+gt.basis.get_axis(i)*gs;
  451. float grabber_radius = gs*GIZMO_ARROW_SIZE;
  452. Vector3 r;
  453. if (Geometry::segment_intersects_sphere(ray_pos,ray_pos+ray*10000.0,grabber_pos,grabber_radius,&r)) {
  454. float d = r.distance_to(ray_pos);
  455. if (d<col_d) {
  456. col_d=d;
  457. col_axis=i;
  458. }
  459. }
  460. }
  461. if (col_axis!=-1) {
  462. if (p_hilite_only) {
  463. spatial_editor->select_gizmo_hilight_axis(col_axis);
  464. } else {
  465. //handle rotate
  466. _edit.mode=TRANSFORM_TRANSLATE;
  467. _compute_edit(Point2(p_screenpos.x,p_screenpos.y));
  468. _edit.plane=TransformPlane(TRANSFORM_X_AXIS+col_axis);
  469. }
  470. return true;
  471. }
  472. }
  473. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_ROTATE) {
  474. int col_axis=-1;
  475. float col_d=1e20;
  476. for(int i=0;i<3;i++) {
  477. Plane plane(gt.origin,gt.basis.get_axis(i).normalized());
  478. Vector3 r;
  479. if (!plane.intersects_ray(ray_pos,ray,&r))
  480. continue;
  481. float dist = r.distance_to(gt.origin);
  482. if (dist > gs*(1-GIZMO_RING_HALF_WIDTH) && dist < gs *(1+GIZMO_RING_HALF_WIDTH)) {
  483. float d = ray_pos.distance_to(r);
  484. if (d<col_d) {
  485. col_d=d;
  486. col_axis=i;
  487. }
  488. }
  489. }
  490. if (col_axis!=-1) {
  491. if (p_hilite_only) {
  492. spatial_editor->select_gizmo_hilight_axis(col_axis+3);
  493. } else {
  494. //handle rotate
  495. _edit.mode=TRANSFORM_ROTATE;
  496. _compute_edit(Point2(p_screenpos.x,p_screenpos.y));
  497. _edit.plane=TransformPlane(TRANSFORM_X_AXIS+col_axis);
  498. }
  499. return true;
  500. }
  501. }
  502. if (p_hilite_only)
  503. spatial_editor->select_gizmo_hilight_axis(-1);
  504. return false;
  505. }
  506. void SpatialEditorViewport::_smouseenter() {
  507. if (!surface->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field()))
  508. surface->grab_focus();
  509. }
  510. void SpatialEditorViewport::_list_select(InputEventMouseButton b) {
  511. _find_items_at_pos(Vector2( b.x, b.y ),clicked_includes_current,selection_results,b.mod.shift);
  512. Node *scene=editor->get_edited_scene();
  513. for(int i=0;i<selection_results.size();i++) {
  514. Spatial *item=selection_results[i].item;
  515. if (item!=scene && item->get_owner()!=scene && !scene->is_editable_instance(item->get_owner())) {
  516. //invalid result
  517. selection_results.remove(i);
  518. i--;
  519. }
  520. }
  521. clicked_wants_append=b.mod.shift;
  522. if (selection_results.size() == 1) {
  523. clicked=selection_results[0].item->get_instance_ID();
  524. selection_results.clear();
  525. if (clicked) {
  526. _select_clicked(clicked_wants_append,true);
  527. clicked=0;
  528. }
  529. } else if (!selection_results.empty()) {
  530. NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
  531. StringName root_name = root_path.get_name(root_path.get_name_count()-1);
  532. for (int i = 0; i < selection_results.size(); i++) {
  533. Spatial *spat=selection_results[i].item;
  534. Ref<Texture> icon;
  535. if (spat->has_meta("_editor_icon"))
  536. icon=spat->get_meta("_editor_icon");
  537. else
  538. icon=get_icon( has_icon(spat->get_type(),"EditorIcons")?spat->get_type():String("Object"),"EditorIcons");
  539. String node_path="/"+root_name+"/"+root_path.rel_path_to(spat->get_path());
  540. selection_menu->add_item(spat->get_name());
  541. selection_menu->set_item_icon(i, icon );
  542. selection_menu->set_item_metadata(i, node_path);
  543. selection_menu->set_item_tooltip(i,String(spat->get_name())+
  544. "\nType: "+spat->get_type()+"\nPath: "+node_path);
  545. }
  546. selection_menu->set_global_pos(Vector2( b.global_x, b.global_y ));
  547. selection_menu->popup();
  548. selection_menu->call_deferred("grab_click_focus");
  549. selection_menu->set_invalidate_click_until_motion();
  550. }
  551. }
  552. void SpatialEditorViewport::_sinput(const InputEvent &p_event) {
  553. if (previewing)
  554. return; //do NONE
  555. {
  556. EditorNode *en = editor;
  557. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  558. if (!over_plugin_list->empty()) {
  559. bool discard = over_plugin_list->forward_spatial_input_event(camera,p_event);
  560. if (discard)
  561. return;
  562. }
  563. }
  564. switch(p_event.type) {
  565. case InputEvent::MOUSE_BUTTON: {
  566. const InputEventMouseButton &b=p_event.mouse_button;
  567. switch(b.button_index) {
  568. case BUTTON_WHEEL_UP: {
  569. cursor.distance/=1.08;
  570. if (cursor.distance<0.001)
  571. cursor.distance=0.001;
  572. } break;
  573. case BUTTON_WHEEL_DOWN: {
  574. if (cursor.distance<0.001)
  575. cursor.distance=0.001;
  576. cursor.distance*=1.08;
  577. } break;
  578. case BUTTON_RIGHT: {
  579. NavigationScheme nav_scheme = _get_navigation_schema("3d_editor/navigation_scheme");
  580. if (b.pressed && _edit.gizmo.is_valid()) {
  581. //restore
  582. _edit.gizmo->commit_handle(_edit.gizmo_handle,_edit.gizmo_initial_value,true);
  583. _edit.gizmo=Ref<SpatialEditorGizmo>();
  584. }
  585. if (_edit.mode==TRANSFORM_NONE && b.pressed) {
  586. Plane cursor_plane(cursor.cursor_pos,_get_camera_normal());
  587. Vector3 ray_origin = _get_ray_pos(Vector2(b.x,b.y));
  588. Vector3 ray_dir = _get_ray(Vector2(b.x,b.y));
  589. //gizmo modify
  590. if (b.mod.control) {
  591. Vector<RID> instances=VisualServer::get_singleton()->instances_cull_ray(ray_origin,ray_dir,get_tree()->get_root()->get_world()->get_scenario() );
  592. Plane p(ray_origin,_get_camera_normal());
  593. float min_d=1e10;
  594. bool found=false;
  595. for (int i=0;i<instances.size();i++) {
  596. uint32_t id=VisualServer::get_singleton()->instance_get_object_instance_ID(instances[i]);
  597. Object *obj=ObjectDB::get_instance(id);
  598. if (!obj)
  599. continue;
  600. VisualInstance *vi=obj->cast_to<VisualInstance>();
  601. if (!vi)
  602. continue;
  603. //optimize by checking AABB (although should pre sort by distance)
  604. AABB aabb = vi->get_global_transform().xform(vi->get_aabb());
  605. if (p.distance_to(aabb.get_support(-ray_dir))>min_d)
  606. continue;
  607. DVector<Face3> faces = vi->get_faces(VisualInstance::FACES_SOLID);
  608. int c = faces.size();
  609. if (c>0) {
  610. DVector<Face3>::Read r = faces.read();
  611. for(int j=0;j<c;j++) {
  612. Vector3 inters;
  613. if (r[j].intersects_ray(ray_origin,ray_dir,&inters)) {
  614. float d = p.distance_to(inters);
  615. if (d<0)
  616. continue;
  617. if (d<min_d) {
  618. min_d=d;
  619. found=true;
  620. }
  621. }
  622. }
  623. }
  624. }
  625. if (found) {
  626. //cursor.cursor_pos=ray_origin+ray_dir*min_d;
  627. //VisualServer::get_singleton()->instance_set_transform(cursor_instance,Transform(Matrix3(),cursor.cursor_pos));
  628. }
  629. } else {
  630. Vector3 new_pos;
  631. if (cursor_plane.intersects_ray(ray_origin,ray_dir,&new_pos)) {
  632. //cursor.cursor_pos=new_pos;
  633. //VisualServer::get_singleton()->instance_set_transform(cursor_instance,Transform(Matrix3(),cursor.cursor_pos));
  634. }
  635. }
  636. if (b.mod.alt) {
  637. if (nav_scheme == NAVIGATION_MAYA)
  638. break;
  639. _list_select(b);
  640. return;
  641. }
  642. }
  643. if (_edit.mode!=TRANSFORM_NONE && b.pressed) {
  644. //cancel motion
  645. _edit.mode=TRANSFORM_NONE;
  646. //_validate_selection();
  647. List<Node*> &selection = editor_selection->get_selected_node_list();
  648. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  649. Spatial *sp = E->get()->cast_to<Spatial>();
  650. if (!sp)
  651. continue;
  652. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  653. if (!se)
  654. continue;
  655. sp->set_global_transform( se->original );
  656. }
  657. surface->update();
  658. //VisualServer::get_singleton()->poly_clear(indicators);
  659. set_message(TTR("Transform Aborted."),3);
  660. }
  661. } break;
  662. case BUTTON_MIDDLE: {
  663. if (b.pressed && _edit.mode!=TRANSFORM_NONE) {
  664. switch(_edit.plane ) {
  665. case TRANSFORM_VIEW: {
  666. _edit.plane=TRANSFORM_X_AXIS;
  667. set_message(TTR("X-Axis Transform."),2);
  668. name="";
  669. _update_name();
  670. } break;
  671. case TRANSFORM_X_AXIS: {
  672. _edit.plane=TRANSFORM_Y_AXIS;
  673. set_message(TTR("Y-Axis Transform."),2);
  674. } break;
  675. case TRANSFORM_Y_AXIS: {
  676. _edit.plane=TRANSFORM_Z_AXIS;
  677. set_message(TTR("Z-Axis Transform."),2);
  678. } break;
  679. case TRANSFORM_Z_AXIS: {
  680. _edit.plane=TRANSFORM_VIEW;
  681. set_message(TTR("View Plane Transform."),2);
  682. } break;
  683. }
  684. }
  685. } break;
  686. case BUTTON_LEFT: {
  687. if (b.pressed) {
  688. NavigationScheme nav_scheme = _get_navigation_schema("3d_editor/navigation_scheme");
  689. if ( (nav_scheme==NAVIGATION_MAYA || nav_scheme==NAVIGATION_MODO) && b.mod.alt) {
  690. break;
  691. }
  692. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_LIST_SELECT) {
  693. _list_select(b);
  694. break;
  695. }
  696. _edit.mouse_pos=Point2(b.x,b.y);
  697. _edit.snap=false;
  698. _edit.mode=TRANSFORM_NONE;
  699. //gizmo has priority over everything
  700. bool can_select_gizmos=true;
  701. {
  702. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  703. can_select_gizmos = view_menu->get_popup()->is_item_checked( idx );
  704. }
  705. if (can_select_gizmos && spatial_editor->get_selected()) {
  706. Ref<SpatialEditorGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  707. if (seg.is_valid()) {
  708. int handle=-1;
  709. Vector3 point;
  710. Vector3 normal;
  711. bool inters = seg->intersect_ray(camera,_edit.mouse_pos,point,normal,&handle,b.mod.shift);
  712. if (inters && handle!=-1) {
  713. _edit.gizmo=seg;
  714. _edit.gizmo_handle=handle;
  715. //_edit.gizmo_initial_pos=seg->get_handle_pos(gizmo_handle);
  716. _edit.gizmo_initial_value=seg->get_handle_value(handle);
  717. break;
  718. }
  719. }
  720. }
  721. if (_gizmo_select(_edit.mouse_pos))
  722. break;
  723. clicked=0;
  724. clicked_includes_current=false;
  725. if ((spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT && b.mod.control) || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_ROTATE) {
  726. /* HANDLE ROTATION */
  727. if (get_selected_count()==0)
  728. break; //bye
  729. //handle rotate
  730. _edit.mode=TRANSFORM_ROTATE;
  731. _compute_edit(Point2(b.x,b.y));
  732. break;
  733. }
  734. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_MOVE) {
  735. if (get_selected_count()==0)
  736. break; //bye
  737. //handle rotate
  738. _edit.mode=TRANSFORM_TRANSLATE;
  739. _compute_edit(Point2(b.x,b.y));
  740. break;
  741. }
  742. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SCALE) {
  743. if (get_selected_count()==0)
  744. break; //bye
  745. //handle rotate
  746. _edit.mode=TRANSFORM_SCALE;
  747. _compute_edit(Point2(b.x,b.y));
  748. break;
  749. }
  750. // todo scale
  751. int gizmo_handle=-1;
  752. clicked=_select_ray(Vector2( b.x, b.y ),b.mod.shift,clicked_includes_current,&gizmo_handle,b.mod.shift);
  753. //clicking is always deferred to either move or release
  754. clicked_wants_append=b.mod.shift;
  755. if (!clicked) {
  756. if (!clicked_wants_append)
  757. _clear_selected();
  758. //default to regionselect
  759. cursor.region_select=true;
  760. cursor.region_begin=Point2(b.x,b.y);
  761. cursor.region_end=Point2(b.x,b.y);
  762. }
  763. if (clicked && gizmo_handle>=0) {
  764. Object *obj=ObjectDB::get_instance(clicked);
  765. if (obj) {
  766. Spatial *spa = obj->cast_to<Spatial>();
  767. if (spa) {
  768. Ref<SpatialEditorGizmo> seg=spa->get_gizmo();
  769. if (seg.is_valid()) {
  770. _edit.gizmo=seg;
  771. _edit.gizmo_handle=gizmo_handle;
  772. //_edit.gizmo_initial_pos=seg->get_handle_pos(gizmo_handle);
  773. _edit.gizmo_initial_value=seg->get_handle_value(gizmo_handle);
  774. //print_line("GIZMO: "+itos(gizmo_handle)+" FROMPOS: "+_edit.orig_gizmo_pos);
  775. break;
  776. }
  777. }
  778. }
  779. //_compute_edit(Point2(b.x,b.y)); //in case a motion happens..
  780. }
  781. surface->update();
  782. } else {
  783. if (_edit.gizmo.is_valid()) {
  784. _edit.gizmo->commit_handle(_edit.gizmo_handle,_edit.gizmo_initial_value,false);
  785. _edit.gizmo=Ref<SpatialEditorGizmo>();
  786. break;
  787. }
  788. if (clicked) {
  789. _select_clicked(clicked_wants_append,true);
  790. //clickd processing was deferred
  791. clicked=0;
  792. }
  793. if (cursor.region_select) {
  794. _select_region();
  795. cursor.region_select=false;
  796. surface->update();
  797. }
  798. if (_edit.mode!=TRANSFORM_NONE) {
  799. static const char* _transform_name[4]={"None","Rotate","Translate","Scale"};
  800. undo_redo->create_action(_transform_name[_edit.mode]);
  801. List<Node*> &selection = editor_selection->get_selected_node_list();
  802. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  803. Spatial *sp = E->get()->cast_to<Spatial>();
  804. if (!sp)
  805. continue;
  806. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  807. if (!se)
  808. continue;
  809. undo_redo->add_do_method(sp,"set_global_transform",sp->get_global_transform());
  810. undo_redo->add_undo_method(sp,"set_global_transform",se->original);
  811. }
  812. undo_redo->commit_action();
  813. _edit.mode=TRANSFORM_NONE;
  814. //VisualServer::get_singleton()->poly_clear(indicators);
  815. set_message("");
  816. }
  817. surface->update();
  818. }
  819. } break;
  820. }
  821. } break;
  822. case InputEvent::MOUSE_MOTION: {
  823. const InputEventMouseMotion &m=p_event.mouse_motion;
  824. _edit.mouse_pos=Point2(p_event.mouse_motion.x,p_event.mouse_motion.y);
  825. if (spatial_editor->get_selected()) {
  826. Ref<SpatialEditorGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  827. if (seg.is_valid()) {
  828. int selected_handle=-1;
  829. int handle=-1;
  830. Vector3 point;
  831. Vector3 normal;
  832. bool inters = seg->intersect_ray(camera,_edit.mouse_pos,point,normal,&handle,false);
  833. if (inters && handle!=-1) {
  834. selected_handle=handle;
  835. }
  836. if (selected_handle!=spatial_editor->get_over_gizmo_handle()) {
  837. spatial_editor->set_over_gizmo_handle(selected_handle);
  838. spatial_editor->get_selected()->update_gizmo();
  839. if (selected_handle!=-1)
  840. spatial_editor->select_gizmo_hilight_axis(-1);
  841. }
  842. }
  843. }
  844. if (spatial_editor->get_over_gizmo_handle()==-1 && !(m.button_mask&1) && !_edit.gizmo.is_valid()) {
  845. _gizmo_select(_edit.mouse_pos,true);
  846. }
  847. NavigationScheme nav_scheme = _get_navigation_schema("3d_editor/navigation_scheme");
  848. NavigationMode nav_mode = NAVIGATION_NONE;
  849. if (_edit.gizmo.is_valid()) {
  850. _edit.gizmo->set_handle(_edit.gizmo_handle,camera,Vector2(m.x,m.y));
  851. Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle);
  852. String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle);
  853. set_message(n+": "+String(v));
  854. } else if (m.button_mask&1) {
  855. if (nav_scheme == NAVIGATION_MAYA && m.mod.alt) {
  856. nav_mode = NAVIGATION_ORBIT;
  857. } else if (nav_scheme == NAVIGATION_MODO && m.mod.alt && m.mod.shift) {
  858. nav_mode = NAVIGATION_PAN;
  859. } else if (nav_scheme == NAVIGATION_MODO && m.mod.alt && m.mod.control) {
  860. nav_mode = NAVIGATION_ZOOM;
  861. } else if (nav_scheme == NAVIGATION_MODO && m.mod.alt) {
  862. nav_mode = NAVIGATION_ORBIT;
  863. } else {
  864. if (clicked) {
  865. if (!clicked_includes_current) {
  866. _select_clicked(clicked_wants_append,true);
  867. //clickd processing was deferred
  868. }
  869. _compute_edit(_edit.mouse_pos);
  870. clicked=0;
  871. _edit.mode=TRANSFORM_TRANSLATE;
  872. }
  873. if (cursor.region_select && nav_mode == NAVIGATION_NONE) {
  874. cursor.region_end=Point2(m.x,m.y);
  875. surface->update();
  876. return;
  877. }
  878. if (_edit.mode==TRANSFORM_NONE && nav_mode == NAVIGATION_NONE)
  879. break;
  880. Vector3 ray_pos=_get_ray_pos( Vector2( m.x, m.y ) );
  881. Vector3 ray=_get_ray( Vector2( m.x, m.y ) );
  882. switch(_edit.mode) {
  883. case TRANSFORM_SCALE: {
  884. Plane plane=Plane(_edit.center,_get_camera_normal());
  885. Vector3 intersection;
  886. if (!plane.intersects_ray(ray_pos,ray,&intersection))
  887. break;
  888. Vector3 click;
  889. if (!plane.intersects_ray(_edit.click_ray_pos,_edit.click_ray,&click))
  890. break;
  891. float center_click_dist = click.distance_to(_edit.center);
  892. float center_inters_dist = intersection.distance_to(_edit.center);
  893. if (center_click_dist==0)
  894. break;
  895. float scale = (center_inters_dist / center_click_dist)*100.0;
  896. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  897. scale = Math::stepify(scale,spatial_editor->get_scale_snap());
  898. }
  899. set_message(vformat(TTR("Scaling to %s%%."),String::num(scale,1)));
  900. scale/=100.0;
  901. Transform r;
  902. r.basis.scale(Vector3(scale,scale,scale));
  903. List<Node*> &selection = editor_selection->get_selected_node_list();
  904. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  905. Spatial *sp = E->get()->cast_to<Spatial>();
  906. if (!sp)
  907. continue;
  908. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  909. if (!se)
  910. continue;
  911. Transform original=se->original;
  912. Transform base=Transform( Matrix3(), _edit.center);
  913. Transform t=base * (r * (base.inverse() * original));
  914. sp->set_global_transform(t);
  915. }
  916. surface->update();
  917. } break;
  918. case TRANSFORM_TRANSLATE: {
  919. Vector3 motion_mask;
  920. Plane plane;
  921. switch( _edit.plane ) {
  922. case TRANSFORM_VIEW:
  923. motion_mask=Vector3(0,0,0);
  924. plane=Plane(_edit.center,_get_camera_normal());
  925. break;
  926. case TRANSFORM_X_AXIS:
  927. motion_mask=spatial_editor->get_gizmo_transform().basis.get_axis(0);
  928. plane=Plane(_edit.center,motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  929. break;
  930. case TRANSFORM_Y_AXIS:
  931. motion_mask=spatial_editor->get_gizmo_transform().basis.get_axis(1);
  932. plane=Plane(_edit.center,motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  933. break;
  934. case TRANSFORM_Z_AXIS:
  935. motion_mask=spatial_editor->get_gizmo_transform().basis.get_axis(2);
  936. plane=Plane(_edit.center,motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  937. break;
  938. }
  939. Vector3 intersection;
  940. if (!plane.intersects_ray(ray_pos,ray,&intersection))
  941. break;
  942. Vector3 click;
  943. if (!plane.intersects_ray(_edit.click_ray_pos,_edit.click_ray,&click))
  944. break;
  945. //_validate_selection();
  946. Vector3 motion = intersection-click;
  947. if (motion_mask!=Vector3()) {
  948. motion=motion_mask.dot(motion)*motion_mask;
  949. }
  950. float snap=0;
  951. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  952. snap = spatial_editor->get_translate_snap();
  953. motion.snap(snap);
  954. }
  955. //set_message("Translating: "+motion);
  956. List<Node*> &selection = editor_selection->get_selected_node_list();
  957. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  958. Spatial *sp = E->get()->cast_to<Spatial>();
  959. if (!sp) {
  960. continue;
  961. }
  962. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  963. if (!se) {
  964. continue;
  965. }
  966. Transform t=se->original;
  967. t.origin+=motion;
  968. sp->set_global_transform(t);
  969. }
  970. } break;
  971. case TRANSFORM_ROTATE: {
  972. Plane plane;
  973. switch( _edit.plane ) {
  974. case TRANSFORM_VIEW:
  975. plane=Plane(_edit.center,_get_camera_normal());
  976. break;
  977. case TRANSFORM_X_AXIS:
  978. plane=Plane(_edit.center,spatial_editor->get_gizmo_transform().basis.get_axis(0));
  979. break;
  980. case TRANSFORM_Y_AXIS:
  981. plane=Plane(_edit.center,spatial_editor->get_gizmo_transform().basis.get_axis(1));
  982. break;
  983. case TRANSFORM_Z_AXIS:
  984. plane=Plane(_edit.center,spatial_editor->get_gizmo_transform().basis.get_axis(2));
  985. break;
  986. }
  987. Vector3 intersection;
  988. if (!plane.intersects_ray(ray_pos,ray,&intersection))
  989. break;
  990. Vector3 click;
  991. if (!plane.intersects_ray(_edit.click_ray_pos,_edit.click_ray,&click))
  992. break;
  993. Vector3 y_axis=(click-_edit.center).normalized();
  994. Vector3 x_axis=plane.normal.cross(y_axis).normalized();
  995. float angle=Math::atan2( x_axis.dot(intersection-_edit.center), y_axis.dot(intersection-_edit.center) );
  996. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  997. float snap = spatial_editor->get_rotate_snap();
  998. if (snap) {
  999. angle=Math::rad2deg(angle)+snap*0.5; //else it wont reach +180
  1000. angle-=Math::fmod(angle,snap);
  1001. set_message(vformat(TTR("Rotating %s degrees."),rtos(angle)));
  1002. angle=Math::deg2rad(angle);
  1003. } else
  1004. set_message(vformat(TTR("Rotating %s degrees."),rtos(Math::rad2deg(angle))));
  1005. } else {
  1006. set_message(vformat(TTR("Rotating %s degrees."),rtos(Math::rad2deg(angle))));
  1007. }
  1008. Transform r;
  1009. r.basis.rotate(plane.normal,-angle);
  1010. List<Node*> &selection = editor_selection->get_selected_node_list();
  1011. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1012. Spatial *sp = E->get()->cast_to<Spatial>();
  1013. if (!sp)
  1014. continue;
  1015. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1016. if (!se)
  1017. continue;
  1018. Transform original=se->original;
  1019. Transform base=Transform( Matrix3(), _edit.center);
  1020. Transform t=base * (r * (base.inverse() * original));
  1021. sp->set_global_transform(t);
  1022. }
  1023. surface->update();
  1024. /*
  1025. VisualServer::get_singleton()->poly_clear(indicators);
  1026. Vector<Vector3> points;
  1027. Vector<Vector3> empty;
  1028. Vector<Color> colors;
  1029. points.push_back(intersection);
  1030. points.push_back(_edit.original.origin);
  1031. colors.push_back( Color(255,155,100) );
  1032. colors.push_back( Color(255,155,100) );
  1033. VisualServer::get_singleton()->poly_add_primitive(indicators,points,empty,colors,empty);
  1034. */
  1035. } break;
  1036. default:{}
  1037. }
  1038. }
  1039. } else if (m.button_mask&2) {
  1040. if (nav_scheme == NAVIGATION_MAYA && m.mod.alt) {
  1041. nav_mode = NAVIGATION_ZOOM;
  1042. }
  1043. } else if (m.button_mask&4) {
  1044. if (nav_scheme == NAVIGATION_GODOT) {
  1045. int mod = 0;
  1046. if (m.mod.shift)
  1047. mod=KEY_SHIFT;
  1048. if (m.mod.alt)
  1049. mod=KEY_ALT;
  1050. if (m.mod.control)
  1051. mod=KEY_CONTROL;
  1052. if (m.mod.meta)
  1053. mod=KEY_META;
  1054. if (mod == _get_key_modifier("3d_editor/pan_modifier"))
  1055. nav_mode = NAVIGATION_PAN;
  1056. else if (mod == _get_key_modifier("3d_editor/zoom_modifier"))
  1057. nav_mode = NAVIGATION_ZOOM;
  1058. else if (mod == _get_key_modifier("3d_editor/orbit_modifier"))
  1059. nav_mode = NAVIGATION_ORBIT;
  1060. } else if (nav_scheme == NAVIGATION_MAYA) {
  1061. if (m.mod.alt)
  1062. nav_mode = NAVIGATION_PAN;
  1063. }
  1064. } else if (EditorSettings::get_singleton()->get("3d_editor/emulate_3_button_mouse")) {
  1065. // Handle trackpad (no external mouse) use case
  1066. int mod = 0;
  1067. if (m.mod.shift)
  1068. mod=KEY_SHIFT;
  1069. if (m.mod.alt)
  1070. mod=KEY_ALT;
  1071. if (m.mod.control)
  1072. mod=KEY_CONTROL;
  1073. if (m.mod.meta)
  1074. mod=KEY_META;
  1075. if(mod){
  1076. if (mod == _get_key_modifier("3d_editor/pan_modifier"))
  1077. nav_mode = NAVIGATION_PAN;
  1078. else if (mod == _get_key_modifier("3d_editor/zoom_modifier"))
  1079. nav_mode = NAVIGATION_ZOOM;
  1080. else if (mod == _get_key_modifier("3d_editor/orbit_modifier"))
  1081. nav_mode = NAVIGATION_ORBIT;
  1082. }
  1083. }
  1084. switch(nav_mode) {
  1085. case NAVIGATION_PAN:{
  1086. real_t pan_speed = 1/150.0;
  1087. int pan_speed_modifier = 10;
  1088. if (nav_scheme==NAVIGATION_MAYA && m.mod.shift)
  1089. pan_speed *= pan_speed_modifier;
  1090. Transform camera_transform;
  1091. camera_transform.translate(cursor.pos);
  1092. camera_transform.basis.rotate(Vector3(0,1,0),cursor.y_rot);
  1093. camera_transform.basis.rotate(Vector3(1,0,0),cursor.x_rot);
  1094. Vector3 translation(-m.relative_x*pan_speed,m.relative_y*pan_speed,0);
  1095. translation*=cursor.distance/DISTANCE_DEFAULT;
  1096. camera_transform.translate(translation);
  1097. cursor.pos=camera_transform.origin;
  1098. } break;
  1099. case NAVIGATION_ZOOM: {
  1100. real_t zoom_speed = 1/80.0;
  1101. int zoom_speed_modifier = 10;
  1102. if (nav_scheme==NAVIGATION_MAYA && m.mod.shift)
  1103. zoom_speed *= zoom_speed_modifier;
  1104. NavigationZoomStyle zoom_style = _get_navigation_zoom_style("3d_editor/zoom_style");
  1105. if (zoom_style == NAVIGATION_ZOOM_HORIZONTAL) {
  1106. if ( m.relative_x > 0)
  1107. cursor.distance*=1-m.relative_x*zoom_speed;
  1108. else if (m.relative_x < 0)
  1109. cursor.distance/=1+m.relative_x*zoom_speed;
  1110. }
  1111. else {
  1112. if ( m.relative_y > 0)
  1113. cursor.distance*=1+m.relative_y*zoom_speed;
  1114. else if (m.relative_y < 0)
  1115. cursor.distance/=1-m.relative_y*zoom_speed;
  1116. }
  1117. } break;
  1118. case NAVIGATION_ORBIT: {
  1119. cursor.x_rot+=m.relative_y/80.0;
  1120. cursor.y_rot+=m.relative_x/80.0;
  1121. if (cursor.x_rot>Math_PI/2.0)
  1122. cursor.x_rot=Math_PI/2.0;
  1123. if (cursor.x_rot<-Math_PI/2.0)
  1124. cursor.x_rot=-Math_PI/2.0;
  1125. name="";
  1126. _update_name();
  1127. } break;
  1128. default: {}
  1129. }
  1130. } break;
  1131. case InputEvent::KEY: {
  1132. const InputEventKey &k = p_event.key;
  1133. if (!k.pressed)
  1134. break;
  1135. if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  1136. if (_edit.mode != TRANSFORM_NONE) {
  1137. _edit.snap=true;
  1138. }
  1139. }
  1140. if (ED_IS_SHORTCUT("spatial_editor/bottom_view", p_event)) {
  1141. cursor.y_rot = 0;
  1142. cursor.x_rot = -Math_PI/2.0;
  1143. set_message(TTR("Bottom View."),2);
  1144. name = TTR("Bottom");
  1145. _update_name();
  1146. }
  1147. if (ED_IS_SHORTCUT("spatial_editor/top_view", p_event)) {
  1148. cursor.y_rot = 0;
  1149. cursor.x_rot = Math_PI/2.0;
  1150. set_message(TTR("Top View."),2);
  1151. name = TTR("Top");
  1152. _update_name();
  1153. }
  1154. if (ED_IS_SHORTCUT("spatial_editor/rear_view", p_event)) {
  1155. cursor.x_rot = 0;
  1156. cursor.y_rot = Math_PI;
  1157. set_message(TTR("Rear View."),2);
  1158. name = TTR("Rear");
  1159. _update_name();
  1160. }
  1161. if (ED_IS_SHORTCUT("spatial_editor/front_view", p_event)) {
  1162. cursor.x_rot = 0;
  1163. cursor.y_rot=0;
  1164. set_message(TTR("Front View."),2);
  1165. name=TTR("Front");
  1166. _update_name();
  1167. }
  1168. if (ED_IS_SHORTCUT("spatial_editor/left_view", p_event)) {
  1169. cursor.x_rot=0;
  1170. cursor.y_rot = Math_PI/2.0;
  1171. set_message(TTR("Left View."),2);
  1172. name = TTR("Left");
  1173. _update_name();
  1174. }
  1175. if (ED_IS_SHORTCUT("spatial_editor/right_view", p_event)) {
  1176. cursor.x_rot=0;
  1177. cursor.y_rot = -Math_PI/2.0;
  1178. set_message(TTR("Right View."),2);
  1179. name = TTR("Right");
  1180. _update_name();
  1181. }
  1182. if (ED_IS_SHORTCUT("spatial_editor/switch_perspective_orthogonal", p_event)) {
  1183. _menu_option(orthogonal?VIEW_PERSPECTIVE:VIEW_ORTHOGONAL);
  1184. _update_name();
  1185. }
  1186. if (ED_IS_SHORTCUT("spatial_editor/insert_anim_key", p_event)) {
  1187. if (!get_selected_count() || _edit.mode!=TRANSFORM_NONE)
  1188. break;
  1189. if (!AnimationPlayerEditor::singleton->get_key_editor()->has_keying()) {
  1190. set_message(TTR("Keying is disabled (no key inserted)."));
  1191. break;
  1192. }
  1193. List<Node*> &selection = editor_selection->get_selected_node_list();
  1194. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1195. Spatial *sp = E->get()->cast_to<Spatial>();
  1196. if (!sp)
  1197. continue;
  1198. emit_signal("transform_key_request",sp,"",sp->get_transform());
  1199. }
  1200. set_message(TTR("Animation Key Inserted."));
  1201. }
  1202. if (k.scancode == KEY_SPACE) {
  1203. if (!k.pressed) emit_signal("toggle_maximize_view", this);
  1204. }
  1205. } break;
  1206. }
  1207. }
  1208. void SpatialEditorViewport::set_message(String p_message,float p_time) {
  1209. message=p_message;
  1210. message_time=p_time;
  1211. }
  1212. void SpatialEditorViewport::_notification(int p_what) {
  1213. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  1214. bool visible=is_visible();
  1215. set_process(visible);
  1216. if (visible)
  1217. _update_camera();
  1218. call_deferred("update_transform_gizmo_view");
  1219. }
  1220. if (p_what==NOTIFICATION_RESIZED) {
  1221. call_deferred("update_transform_gizmo_view");
  1222. }
  1223. if (p_what==NOTIFICATION_PROCESS) {
  1224. //force editr camera
  1225. /*
  1226. current_camera=get_root_node()->get_current_camera();
  1227. if (current_camera!=camera) {
  1228. }
  1229. */
  1230. _update_camera();
  1231. Map<Node*,Object*> &selection = editor_selection->get_selection();
  1232. bool changed=false;
  1233. bool exist=false;
  1234. for(Map<Node*,Object*>::Element *E=selection.front();E;E=E->next()) {
  1235. Spatial *sp = E->key()->cast_to<Spatial>();
  1236. if (!sp)
  1237. continue;
  1238. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1239. if (!se)
  1240. continue;
  1241. VisualInstance *vi=sp->cast_to<VisualInstance>();
  1242. if (se->aabb.has_no_surface()) {
  1243. se->aabb=vi?vi->get_aabb():AABB( Vector3(-0.2,-0.2,-0.2),Vector3(0.4,0.4,0.4));
  1244. }
  1245. Transform t=sp->get_global_transform();
  1246. t.translate(se->aabb.pos);
  1247. t.basis.scale( se->aabb.size );
  1248. exist=true;
  1249. if (se->last_xform==t)
  1250. continue;
  1251. changed=true;
  1252. se->last_xform=t;
  1253. VisualServer::get_singleton()->instance_set_transform(se->sbox_instance,t);
  1254. }
  1255. if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
  1256. spatial_editor->update_transform_gizmo();
  1257. }
  1258. if (message_time>0) {
  1259. if (message!=last_message) {
  1260. surface->update();
  1261. last_message=message;
  1262. }
  1263. message_time-=get_fixed_process_delta_time();
  1264. if (message_time<0)
  1265. surface->update();
  1266. }
  1267. }
  1268. if (p_what==NOTIFICATION_ENTER_TREE) {
  1269. surface->connect("draw",this,"_draw");
  1270. surface->connect("input_event",this,"_sinput");
  1271. surface->connect("mouse_enter",this,"_smouseenter");
  1272. preview_camera->set_icon(get_icon("Camera","EditorIcons"));
  1273. _init_gizmo_instance(index);
  1274. }
  1275. if (p_what==NOTIFICATION_EXIT_TREE) {
  1276. _finish_gizmo_instances();
  1277. }
  1278. if (p_what==NOTIFICATION_MOUSE_ENTER) {
  1279. }
  1280. if (p_what==NOTIFICATION_DRAW) {
  1281. }
  1282. }
  1283. void SpatialEditorViewport::_draw() {
  1284. if (surface->has_focus()) {
  1285. Size2 size = surface->get_size();
  1286. Rect2 r =Rect2(Point2(),size);
  1287. get_stylebox("EditorFocus","EditorStyles")->draw(surface->get_canvas_item(),r);
  1288. }
  1289. RID ci=surface->get_canvas_item();
  1290. if (cursor.region_select) {
  1291. VisualServer::get_singleton()->canvas_item_add_rect(ci,Rect2(cursor.region_begin,cursor.region_end-cursor.region_begin),Color(0.7,0.7,1.0,0.3));
  1292. }
  1293. if (message_time>0) {
  1294. Ref<Font> font = get_font("font","Label");
  1295. Point2 msgpos=Point2(5,get_size().y-20);
  1296. font->draw(ci,msgpos+Point2(1,1),message,Color(0,0,0,0.8));
  1297. font->draw(ci,msgpos+Point2(-1,-1),message,Color(0,0,0,0.8));
  1298. font->draw(ci,msgpos,message,Color(1,1,1,1));
  1299. }
  1300. if (_edit.mode==TRANSFORM_ROTATE) {
  1301. Point2 center = _point_to_screen(_edit.center);
  1302. VisualServer::get_singleton()->canvas_item_add_line(ci,_edit.mouse_pos, center, Color(0.4,0.7,1.0,0.8));
  1303. }
  1304. if (previewing) {
  1305. Size2 ss = Size2( Globals::get_singleton()->get("display/width"), Globals::get_singleton()->get("display/height") );
  1306. float aspect = ss.get_aspect();
  1307. Size2 s = get_size();
  1308. Rect2 draw_rect;
  1309. switch(previewing->get_keep_aspect_mode()) {
  1310. case Camera::KEEP_WIDTH: {
  1311. draw_rect.size = Size2(s.width,s.width/aspect);
  1312. draw_rect.pos.x=0;
  1313. draw_rect.pos.y=(s.height-draw_rect.size.y)*0.5;
  1314. } break;
  1315. case Camera::KEEP_HEIGHT: {
  1316. draw_rect.size = Size2(s.height*aspect,s.height);
  1317. draw_rect.pos.y=0;
  1318. draw_rect.pos.x=(s.width-draw_rect.size.x)*0.5;
  1319. } break;
  1320. }
  1321. draw_rect = Rect2(Vector2(),s).clip(draw_rect);
  1322. surface->draw_line(draw_rect.pos,draw_rect.pos+Vector2(draw_rect.size.x,0),Color(0.6,0.6,0.1,0.5),2.0);
  1323. surface->draw_line(draw_rect.pos+Vector2(draw_rect.size.x,0),draw_rect.pos+draw_rect.size,Color(0.6,0.6,0.1,0.5),2.0);
  1324. surface->draw_line(draw_rect.pos+draw_rect.size,draw_rect.pos+Vector2(0,draw_rect.size.y),Color(0.6,0.6,0.1,0.5),2.0);
  1325. surface->draw_line(draw_rect.pos,draw_rect.pos+Vector2(0,draw_rect.size.y),Color(0.6,0.6,0.1,0.5),2.0);
  1326. }
  1327. }
  1328. void SpatialEditorViewport::_menu_option(int p_option) {
  1329. switch(p_option) {
  1330. case VIEW_TOP: {
  1331. cursor.x_rot=Math_PI/2.0;
  1332. cursor.y_rot=0;
  1333. name=TTR("Top");
  1334. _update_name();
  1335. } break;
  1336. case VIEW_BOTTOM: {
  1337. cursor.x_rot=-Math_PI/2.0;
  1338. cursor.y_rot=0;
  1339. name=TTR("Bottom");
  1340. _update_name();
  1341. } break;
  1342. case VIEW_LEFT: {
  1343. cursor.y_rot=Math_PI/2.0;
  1344. cursor.x_rot=0;
  1345. name=TTR("Left");
  1346. _update_name();
  1347. } break;
  1348. case VIEW_RIGHT: {
  1349. cursor.y_rot=-Math_PI/2.0;
  1350. cursor.x_rot=0;
  1351. name=TTR("Right");
  1352. _update_name();
  1353. } break;
  1354. case VIEW_FRONT: {
  1355. cursor.y_rot=0;
  1356. cursor.x_rot=0;
  1357. name=TTR("Front");
  1358. _update_name();
  1359. } break;
  1360. case VIEW_REAR: {
  1361. cursor.y_rot=Math_PI;
  1362. cursor.x_rot=0;
  1363. name=TTR("Rear");
  1364. _update_name();
  1365. } break;
  1366. case VIEW_CENTER_TO_SELECTION: {
  1367. focus_selection();
  1368. } break;
  1369. case VIEW_ALIGN_SELECTION_WITH_VIEW: {
  1370. if (!get_selected_count())
  1371. break;
  1372. Transform camera_transform = camera->get_global_transform();
  1373. List<Node*> &selection = editor_selection->get_selected_node_list();
  1374. undo_redo->create_action(TTR("Align with view"));
  1375. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1376. Spatial *sp = E->get()->cast_to<Spatial>();
  1377. if (!sp)
  1378. continue;
  1379. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1380. if (!se)
  1381. continue;
  1382. Transform xform = camera_transform;
  1383. xform.scale_basis(sp->get_scale());
  1384. undo_redo->add_do_method(sp,"set_global_transform",xform);
  1385. undo_redo->add_undo_method(sp,"set_global_transform",sp->get_global_transform());
  1386. }
  1387. undo_redo->commit_action();
  1388. } break;
  1389. case VIEW_ENVIRONMENT: {
  1390. int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT);
  1391. bool current = view_menu->get_popup()->is_item_checked( idx );
  1392. current=!current;
  1393. if (current) {
  1394. camera->set_environment(RES());
  1395. } else {
  1396. camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  1397. }
  1398. view_menu->get_popup()->set_item_checked( idx, current );
  1399. } break;
  1400. case VIEW_PERSPECTIVE: {
  1401. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true );
  1402. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false );
  1403. orthogonal=false;
  1404. call_deferred("update_transform_gizmo_view");
  1405. _update_name();
  1406. } break;
  1407. case VIEW_ORTHOGONAL: {
  1408. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false );
  1409. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true );
  1410. orthogonal=true;
  1411. call_deferred("update_transform_gizmo_view");
  1412. _update_name();
  1413. } break;
  1414. case VIEW_AUDIO_LISTENER: {
  1415. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  1416. bool current = view_menu->get_popup()->is_item_checked( idx );
  1417. current=!current;
  1418. viewport->set_as_audio_listener(current);
  1419. view_menu->get_popup()->set_item_checked( idx, current );
  1420. } break;
  1421. case VIEW_GIZMOS: {
  1422. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  1423. bool current = view_menu->get_popup()->is_item_checked( idx );
  1424. current=!current;
  1425. if (current)
  1426. camera->set_visible_layers( ((1<<20)-1)|(1<<(GIZMO_BASE_LAYER+index))|(1<<GIZMO_EDIT_LAYER)|(1<<GIZMO_GRID_LAYER) );
  1427. else
  1428. camera->set_visible_layers( ((1<<20)-1)|(1<<(GIZMO_BASE_LAYER+index))|(1<<GIZMO_GRID_LAYER) );
  1429. view_menu->get_popup()->set_item_checked( idx, current );
  1430. } break;
  1431. }
  1432. }
  1433. void SpatialEditorViewport::_preview_exited_scene() {
  1434. preview_camera->set_pressed(false);
  1435. _toggle_camera_preview(false);
  1436. view_menu->show();
  1437. }
  1438. void SpatialEditorViewport::_init_gizmo_instance(int p_idx) {
  1439. uint32_t layer=1<<(GIZMO_BASE_LAYER+p_idx);//|(1<<GIZMO_GRID_LAYER);
  1440. for(int i=0;i<3;i++) {
  1441. move_gizmo_instance[i]=VS::get_singleton()->instance_create();
  1442. VS::get_singleton()->instance_set_base(move_gizmo_instance[i],spatial_editor->get_move_gizmo(i)->get_rid());
  1443. VS::get_singleton()->instance_set_scenario(move_gizmo_instance[i],get_tree()->get_root()->get_world()->get_scenario());
  1444. VS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  1445. //VS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i],VS::INSTANCE_FLAG_DEPH_SCALE,true);
  1446. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  1447. VS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i],layer);
  1448. rotate_gizmo_instance[i]=VS::get_singleton()->instance_create();
  1449. VS::get_singleton()->instance_set_base(rotate_gizmo_instance[i],spatial_editor->get_rotate_gizmo(i)->get_rid());
  1450. VS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i],get_tree()->get_root()->get_world()->get_scenario());
  1451. VS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  1452. //VS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i],VS::INSTANCE_FLAG_DEPH_SCALE,true);
  1453. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  1454. VS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i],layer);
  1455. }
  1456. }
  1457. void SpatialEditorViewport::_finish_gizmo_instances() {
  1458. for(int i=0;i<3;i++) {
  1459. VS::get_singleton()->free(move_gizmo_instance[i]);
  1460. VS::get_singleton()->free(rotate_gizmo_instance[i]);
  1461. }
  1462. }
  1463. void SpatialEditorViewport::_toggle_camera_preview(bool p_activate) {
  1464. ERR_FAIL_COND(p_activate && !preview);
  1465. ERR_FAIL_COND(!p_activate && !previewing);
  1466. if (!p_activate) {
  1467. previewing->disconnect("exit_tree",this,"_preview_exited_scene");
  1468. previewing=NULL;
  1469. VS::get_singleton()->viewport_attach_camera( viewport->get_viewport(), camera->get_camera() ); //restore
  1470. if (!preview)
  1471. preview_camera->hide();
  1472. view_menu->show();
  1473. surface->update();
  1474. } else {
  1475. previewing=preview;
  1476. previewing->connect("exit_tree",this,"_preview_exited_scene");
  1477. VS::get_singleton()->viewport_attach_camera( viewport->get_viewport(), preview->get_camera() ); //replace
  1478. view_menu->hide();
  1479. surface->update();
  1480. }
  1481. }
  1482. void SpatialEditorViewport::_selection_result_pressed(int p_result) {
  1483. if (selection_results.size() <= p_result)
  1484. return;
  1485. clicked=selection_results[p_result].item->get_instance_ID();
  1486. if (clicked) {
  1487. _select_clicked(clicked_wants_append,true);
  1488. clicked=0;
  1489. }
  1490. }
  1491. void SpatialEditorViewport::_selection_menu_hide() {
  1492. selection_results.clear();
  1493. selection_menu->clear();
  1494. selection_menu->set_size(Vector2(0, 0));
  1495. }
  1496. void SpatialEditorViewport::set_can_preview(Camera* p_preview) {
  1497. preview=p_preview;
  1498. if (!preview_camera->is_pressed()) {
  1499. if (p_preview) {
  1500. preview_camera->show();
  1501. } else {
  1502. preview_camera->hide();
  1503. }
  1504. }
  1505. }
  1506. void SpatialEditorViewport::update_transform_gizmo_view() {
  1507. if (!is_visible())
  1508. return;
  1509. Transform xform = spatial_editor->get_gizmo_transform();
  1510. Transform camera_xform = camera->get_transform();
  1511. Vector3 camz = -camera_xform.get_basis().get_axis(2).normalized();
  1512. Vector3 camy = -camera_xform.get_basis().get_axis(1).normalized();
  1513. Plane p(camera_xform.origin,camz);
  1514. float gizmo_d = Math::abs( p.distance_to(xform.origin ));
  1515. float d0 = camera->unproject_position(camera_xform.origin+camz*gizmo_d).y;
  1516. float d1 = camera->unproject_position(camera_xform.origin+camz*gizmo_d+camy).y;
  1517. float dd = Math::abs(d0-d1);
  1518. if (dd==0)
  1519. dd=0.0001;
  1520. float gsize = EditorSettings::get_singleton()->get("3d_editor/manipulator_gizmo_size");
  1521. gizmo_scale=(gsize/Math::abs(dd));
  1522. Vector3 scale = Vector3(1,1,1) * gizmo_scale;
  1523. xform.basis.scale(scale);
  1524. //xform.basis.scale(GIZMO_SCALE_DEFAULT*Vector3(1,1,1));
  1525. for(int i=0;i<3;i++) {
  1526. VisualServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], xform );
  1527. VisualServer::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,spatial_editor->is_gizmo_visible()&& (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_MOVE) );
  1528. VisualServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], xform );
  1529. VisualServer::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_ROTATE) );
  1530. }
  1531. }
  1532. void SpatialEditorViewport::set_state(const Dictionary& p_state) {
  1533. cursor.pos=p_state["pos"];
  1534. cursor.x_rot=p_state["x_rot"];
  1535. cursor.y_rot=p_state["y_rot"];
  1536. cursor.distance=p_state["distance"];
  1537. bool env = p_state["use_environment"];
  1538. bool orth = p_state["use_orthogonal"];
  1539. if (orth)
  1540. _menu_option(VIEW_ORTHOGONAL);
  1541. else
  1542. _menu_option(VIEW_PERSPECTIVE);
  1543. if (env != camera->get_environment().is_valid())
  1544. _menu_option(VIEW_ENVIRONMENT);
  1545. if (p_state.has("listener")) {
  1546. bool listener = p_state["listener"];
  1547. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  1548. viewport->set_as_audio_listener(listener);
  1549. view_menu->get_popup()->set_item_checked( idx, listener );
  1550. }
  1551. if (p_state.has("previewing")) {
  1552. Node *pv = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["previewing"]);
  1553. if (pv && pv->cast_to<Camera>()) {
  1554. previewing=pv->cast_to<Camera>();
  1555. previewing->connect("exit_tree",this,"_preview_exited_scene");
  1556. VS::get_singleton()->viewport_attach_camera( viewport->get_viewport(), previewing->get_camera() ); //replace
  1557. view_menu->hide();
  1558. surface->update();
  1559. preview_camera->set_pressed(true);
  1560. preview_camera->show();
  1561. }
  1562. }
  1563. }
  1564. Dictionary SpatialEditorViewport::get_state() const {
  1565. Dictionary d;
  1566. d["pos"]=cursor.pos;
  1567. d["x_rot"]=cursor.x_rot;
  1568. d["y_rot"]=cursor.y_rot;
  1569. d["distance"]=cursor.distance;
  1570. d["use_environment"]=camera->get_environment().is_valid();
  1571. d["use_orthogonal"]=camera->get_projection()==Camera::PROJECTION_ORTHOGONAL;
  1572. d["listener"]=viewport->is_audio_listener();
  1573. if (previewing) {
  1574. d["previewing"]=EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing);
  1575. }
  1576. return d;
  1577. }
  1578. void SpatialEditorViewport::_bind_methods(){
  1579. ObjectTypeDB::bind_method(_MD("_draw"),&SpatialEditorViewport::_draw);
  1580. ObjectTypeDB::bind_method(_MD("_smouseenter"),&SpatialEditorViewport::_smouseenter);
  1581. ObjectTypeDB::bind_method(_MD("_sinput"),&SpatialEditorViewport::_sinput);
  1582. ObjectTypeDB::bind_method(_MD("_menu_option"),&SpatialEditorViewport::_menu_option);
  1583. ObjectTypeDB::bind_method(_MD("_toggle_camera_preview"),&SpatialEditorViewport::_toggle_camera_preview);
  1584. ObjectTypeDB::bind_method(_MD("_preview_exited_scene"),&SpatialEditorViewport::_preview_exited_scene);
  1585. ObjectTypeDB::bind_method(_MD("update_transform_gizmo_view"),&SpatialEditorViewport::update_transform_gizmo_view);
  1586. ObjectTypeDB::bind_method(_MD("_selection_result_pressed"),&SpatialEditorViewport::_selection_result_pressed);
  1587. ObjectTypeDB::bind_method(_MD("_selection_menu_hide"),&SpatialEditorViewport::_selection_menu_hide);
  1588. ADD_SIGNAL( MethodInfo("toggle_maximize_view", PropertyInfo(Variant::OBJECT, "viewport")) );
  1589. }
  1590. void SpatialEditorViewport::reset() {
  1591. orthogonal=false;
  1592. message_time=0;
  1593. message="";
  1594. last_message="";
  1595. name=TTR("Top");
  1596. cursor.x_rot=0;
  1597. cursor.y_rot=0;
  1598. cursor.distance=4;
  1599. cursor.region_select=false;
  1600. _update_name();
  1601. }
  1602. void SpatialEditorViewport::focus_selection() {
  1603. if (!get_selected_count())
  1604. return;
  1605. Vector3 center;
  1606. int count=0;
  1607. List<Node*> &selection = editor_selection->get_selected_node_list();
  1608. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1609. Spatial *sp = E->get()->cast_to<Spatial>();
  1610. if (!sp)
  1611. continue;
  1612. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1613. if (!se)
  1614. continue;
  1615. center+=sp->get_global_transform().origin;
  1616. count++;
  1617. }
  1618. if( count != 0 ) {
  1619. center/=float(count);
  1620. }
  1621. cursor.pos=center;
  1622. }
  1623. SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index) {
  1624. _edit.mode=TRANSFORM_NONE;
  1625. _edit.plane=TRANSFORM_VIEW;
  1626. _edit.edited_gizmo=0;
  1627. _edit.snap=1;
  1628. _edit.gizmo_handle=0;
  1629. index=p_index;
  1630. editor=p_editor;
  1631. editor_selection=editor->get_editor_selection();;
  1632. undo_redo=editor->get_undo_redo();
  1633. clicked=0;
  1634. clicked_includes_current=false;
  1635. orthogonal=false;
  1636. message_time=0;
  1637. spatial_editor=p_spatial_editor;
  1638. Control *c=memnew(Control);
  1639. add_child(c);
  1640. c->set_area_as_parent_rect();
  1641. viewport = memnew( Viewport );
  1642. viewport->set_disable_input(true);
  1643. c->add_child(viewport);
  1644. surface = memnew( Control );
  1645. add_child(surface);
  1646. surface->set_area_as_parent_rect();
  1647. camera = memnew(Camera);
  1648. camera->set_disable_gizmo(true);
  1649. camera->set_visible_layers( ((1<<20)-1)|(1<<(GIZMO_BASE_LAYER+p_index))|(1<<GIZMO_EDIT_LAYER)|(1<<GIZMO_GRID_LAYER) );
  1650. //camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  1651. viewport->add_child(camera);
  1652. camera->make_current();
  1653. surface->set_focus_mode(FOCUS_ALL);
  1654. view_menu = memnew( MenuButton );
  1655. surface->add_child(view_menu);
  1656. view_menu->set_pos( Point2(4,4));
  1657. view_menu->set_self_opacity(0.5);
  1658. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/top_view"), VIEW_TOP);
  1659. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/bottom_view"), VIEW_BOTTOM);
  1660. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/left_view"), VIEW_LEFT);
  1661. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/right_view"), VIEW_RIGHT);
  1662. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/front_view"), VIEW_FRONT);
  1663. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/rear_view"), VIEW_REAR);
  1664. view_menu->get_popup()->add_separator();
  1665. view_menu->get_popup()->add_check_item(TTR("Perspective") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_PERSPECTIVE);
  1666. view_menu->get_popup()->add_check_item(TTR("Orthogonal") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_ORTHOGONAL);
  1667. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE),true);
  1668. view_menu->get_popup()->add_separator();
  1669. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("Environment")), VIEW_ENVIRONMENT);
  1670. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT),true);
  1671. view_menu->get_popup()->add_separator();
  1672. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener", TTR("Audio Listener")), VIEW_AUDIO_LISTENER);
  1673. view_menu->get_popup()->add_separator();
  1674. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("Gizmos")),VIEW_GIZMOS);
  1675. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_GIZMOS),true);
  1676. view_menu->get_popup()->add_separator();
  1677. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection"), VIEW_CENTER_TO_SELECTION);
  1678. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_selection_with_view"), VIEW_ALIGN_SELECTION_WITH_VIEW);
  1679. view_menu->get_popup()->connect("item_pressed",this,"_menu_option");
  1680. preview_camera = memnew( Button );
  1681. preview_camera->set_toggle_mode(true);
  1682. preview_camera->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,90);
  1683. preview_camera->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,10);
  1684. preview_camera->set_text("preview");
  1685. surface->add_child(preview_camera);
  1686. preview_camera->hide();
  1687. preview_camera->connect("toggled",this,"_toggle_camera_preview");
  1688. previewing=NULL;
  1689. preview=NULL;
  1690. gizmo_scale=1.0;
  1691. selection_menu = memnew( PopupMenu );
  1692. add_child(selection_menu);
  1693. selection_menu->set_custom_minimum_size(Vector2(100, 0));
  1694. selection_menu->connect("item_pressed", this, "_selection_result_pressed");
  1695. selection_menu->connect("popup_hide", this, "_selection_menu_hide");
  1696. if (p_index==0) {
  1697. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER),true);
  1698. viewport->set_as_audio_listener(true);
  1699. }
  1700. name=TTR("Top");
  1701. _update_name();
  1702. EditorSettings::get_singleton()->connect("settings_changed",this,"update_transform_gizmo_view");
  1703. }
  1704. SpatialEditor *SpatialEditor::singleton=NULL;
  1705. SpatialEditorSelectedItem::~SpatialEditorSelectedItem() {
  1706. if (sbox_instance.is_valid())
  1707. VisualServer::get_singleton()->free(sbox_instance);
  1708. }
  1709. void SpatialEditor::select_gizmo_hilight_axis(int p_axis) {
  1710. for(int i=0;i<3;i++) {
  1711. move_gizmo[i]->surface_set_material(0,i==p_axis?gizmo_hl:gizmo_color[i]);
  1712. rotate_gizmo[i]->surface_set_material(0,(i+3)==p_axis?gizmo_hl:gizmo_color[i]);
  1713. }
  1714. }
  1715. void SpatialEditor::update_transform_gizmo() {
  1716. List<Node*> &selection = editor_selection->get_selected_node_list();
  1717. AABB center;
  1718. bool first=true;
  1719. Matrix3 gizmo_basis;
  1720. bool local_gizmo_coords = transform_menu->get_popup()->is_item_checked( transform_menu->get_popup()->get_item_index(MENU_TRANSFORM_LOCAL_COORDS) );
  1721. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1722. Spatial *sp = E->get()->cast_to<Spatial>();
  1723. if (!sp)
  1724. continue;
  1725. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1726. if (!se)
  1727. continue;
  1728. Transform xf = se->sp->get_global_transform();
  1729. if (first) {
  1730. center.pos=xf.origin;
  1731. first=false;
  1732. if (local_gizmo_coords) {
  1733. gizmo_basis=xf.basis;
  1734. gizmo_basis.orthonormalize();
  1735. }
  1736. } else {
  1737. center.expand_to(xf.origin);
  1738. gizmo_basis=Matrix3();
  1739. }
  1740. // count++;
  1741. }
  1742. Vector3 pcenter = center.pos+center.size*0.5;
  1743. gizmo.visible=!first;
  1744. gizmo.transform.origin=pcenter;
  1745. gizmo.transform.basis=gizmo_basis;
  1746. for(int i=0;i<4;i++) {
  1747. viewports[i]->update_transform_gizmo_view();
  1748. }
  1749. }
  1750. Object *SpatialEditor::_get_editor_data(Object *p_what) {
  1751. Spatial *sp = p_what->cast_to<Spatial>();
  1752. if (!sp)
  1753. return NULL;
  1754. SpatialEditorSelectedItem *si = memnew( SpatialEditorSelectedItem );
  1755. si->sp=sp;
  1756. si->sbox_instance=VisualServer::get_singleton()->instance_create2(selection_box->get_rid(),sp->get_world()->get_scenario());
  1757. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(si->sbox_instance, VS::SHADOW_CASTING_SETTING_OFF);
  1758. if (get_tree()->is_editor_hint())
  1759. editor->call("edit_node",sp);
  1760. return si;
  1761. }
  1762. void SpatialEditor::_generate_selection_box() {
  1763. AABB aabb( Vector3(), Vector3(1,1,1) );
  1764. aabb.grow_by( aabb.get_longest_axis_size()/20.0 );
  1765. Ref<SurfaceTool> st = memnew( SurfaceTool );
  1766. st->begin(Mesh::PRIMITIVE_LINES);
  1767. for (int i=0;i<12;i++) {
  1768. Vector3 a,b;
  1769. aabb.get_edge(i,a,b);
  1770. /*Vector<Vector3> points;
  1771. Vector<Color> colors;
  1772. points.push_back(a);
  1773. points.push_back(b);*/
  1774. st->add_color( Color(1.0,1.0,0.8,0.8) );
  1775. st->add_vertex(a);
  1776. st->add_color( Color(1.0,1.0,0.8,0.4) );
  1777. st->add_vertex(a.linear_interpolate(b,0.2));
  1778. st->add_color( Color(1.0,1.0,0.8,0.4) );
  1779. st->add_vertex(a.linear_interpolate(b,0.8));
  1780. st->add_color( Color(1.0,1.0,0.8,0.8) );
  1781. st->add_vertex(b);
  1782. }
  1783. Ref<FixedMaterial> mat = memnew( FixedMaterial );
  1784. mat->set_flag(Material::FLAG_UNSHADED,true);
  1785. mat->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1));
  1786. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA,true);
  1787. mat->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY,true);
  1788. st->set_material(mat);
  1789. selection_box = st->commit();
  1790. }
  1791. Dictionary SpatialEditor::get_state() const {
  1792. Dictionary d;
  1793. d["snap_enabled"]=snap_enabled;
  1794. d["translate_snap"]=get_translate_snap();
  1795. d["rotate_snap"]=get_rotate_snap();
  1796. d["scale_snap"]=get_scale_snap();
  1797. int local_coords_index=transform_menu->get_popup()->get_item_index(MENU_TRANSFORM_LOCAL_COORDS);
  1798. d["local_coords"]=transform_menu->get_popup()->is_item_checked( local_coords_index );
  1799. int vc=0;
  1800. if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT) ))
  1801. vc=1;
  1802. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS) ))
  1803. vc=2;
  1804. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS) ))
  1805. vc=3;
  1806. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS) ))
  1807. vc=4;
  1808. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT) ))
  1809. vc=5;
  1810. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT) ))
  1811. vc=6;
  1812. d["viewport_mode"]=vc;
  1813. Array vpdata;
  1814. for(int i=0;i<4;i++) {
  1815. vpdata.push_back( viewports[i]->get_state() );
  1816. }
  1817. d["viewports"]=vpdata;
  1818. d["default_light"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_LIGHT) );;
  1819. d["ambient_light_color"]=settings_ambient_color->get_color();
  1820. d["default_srgb"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_SRGB) );;
  1821. d["show_grid"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_GRID) );;
  1822. d["show_origin"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN) );;
  1823. d["fov"]=get_fov();
  1824. d["znear"]=get_znear();
  1825. d["zfar"]=get_zfar();
  1826. d["deflight_rot_x"]=settings_default_light_rot_x;
  1827. d["deflight_rot_y"]=settings_default_light_rot_y;
  1828. return d;
  1829. }
  1830. void SpatialEditor::set_state(const Dictionary& p_state) {
  1831. Dictionary d = p_state;
  1832. if (d.has("snap_enabled")) {
  1833. snap_enabled=d["snap_enabled"];
  1834. int snap_enabled_idx=transform_menu->get_popup()->get_item_index(MENU_TRANSFORM_USE_SNAP);
  1835. transform_menu->get_popup()->set_item_checked( snap_enabled_idx, snap_enabled );
  1836. }
  1837. if (d.has("translate_snap"))
  1838. snap_translate->set_text(d["translate_snap"]);
  1839. if (d.has("rotate_snap"))
  1840. snap_rotate->set_text(d["rotate_snap"]);
  1841. if (d.has("scale_snap"))
  1842. snap_scale->set_text(d["scale_snap"]);
  1843. if (d.has("local_coords")) {
  1844. int local_coords_idx=transform_menu->get_popup()->get_item_index(MENU_TRANSFORM_LOCAL_COORDS);
  1845. transform_menu->get_popup()->set_item_checked( local_coords_idx, d["local_coords"] );
  1846. update_transform_gizmo();
  1847. }
  1848. if (d.has("viewport_mode")) {
  1849. int vc = d["viewport_mode"];
  1850. if (vc==1)
  1851. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  1852. else if (vc==2)
  1853. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  1854. else if (vc==3)
  1855. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  1856. else if (vc==4)
  1857. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  1858. else if (vc==5)
  1859. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  1860. else if (vc==6)
  1861. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  1862. }
  1863. if (d.has("viewports")) {
  1864. Array vp = d["viewports"];
  1865. ERR_FAIL_COND(vp.size()>4);
  1866. for(int i=0;i<4;i++) {
  1867. viewports[i]->set_state(vp[i]);
  1868. }
  1869. }
  1870. if (d.has("zfar"))
  1871. settings_zfar->set_val(float(d["zfar"]));
  1872. if (d.has("znear"))
  1873. settings_znear->set_val(float(d["znear"]));
  1874. if (d.has("fov"))
  1875. settings_fov->set_val(float(d["fov"]));
  1876. if (d.has("default_light")) {
  1877. bool use = d["default_light"];
  1878. bool existing = light_instance.is_valid();
  1879. if (use!=existing) {
  1880. if (existing) {
  1881. VisualServer::get_singleton()->free(light_instance);
  1882. light_instance=RID();
  1883. } else {
  1884. light_instance=VisualServer::get_singleton()->instance_create2(light,get_tree()->get_root()->get_world()->get_scenario());
  1885. VisualServer::get_singleton()->instance_set_transform(light_instance,light_transform);
  1886. }
  1887. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_LIGHT), light_instance.is_valid() );
  1888. }
  1889. }
  1890. if (d.has("ambient_light_color")) {
  1891. settings_ambient_color->set_color(d["ambient_light_color"]);
  1892. viewport_environment->fx_set_param(Environment::FX_PARAM_AMBIENT_LIGHT_COLOR,d["ambient_light_color"]);
  1893. }
  1894. if (d.has("default_srgb")) {
  1895. bool use = d["default_srgb"];
  1896. viewport_environment->set_enable_fx(Environment::FX_SRGB,use);
  1897. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_SRGB), use );
  1898. }
  1899. if (d.has("show_grid")) {
  1900. bool use = d["show_grid"];
  1901. if (use!=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) {
  1902. _menu_item_pressed(MENU_VIEW_GRID);
  1903. }
  1904. }
  1905. if (d.has("show_origin")) {
  1906. bool use = d["show_origin"];
  1907. if (use!=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) {
  1908. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use );
  1909. VisualServer::get_singleton()->instance_geometry_set_flag(origin_instance,VS::INSTANCE_FLAG_VISIBLE,use);
  1910. }
  1911. }
  1912. if (d.has("deflight_rot_x"))
  1913. settings_default_light_rot_x=d["deflight_rot_x"];
  1914. if (d.has("deflight_rot_y"))
  1915. settings_default_light_rot_y=d["deflight_rot_y"];
  1916. _update_default_light_angle();
  1917. }
  1918. void SpatialEditor::edit(Spatial *p_spatial) {
  1919. if (p_spatial!=selected) {
  1920. if (selected) {
  1921. Ref<SpatialEditorGizmo> seg = selected->get_gizmo();
  1922. if (seg.is_valid()) {
  1923. seg->set_selected(false);
  1924. selected->update_gizmo();
  1925. }
  1926. }
  1927. selected=p_spatial;
  1928. over_gizmo_handle=-1;
  1929. if (selected) {
  1930. Ref<SpatialEditorGizmo> seg = selected->get_gizmo();
  1931. if (seg.is_valid()) {
  1932. seg->set_selected(true);
  1933. selected->update_gizmo();
  1934. }
  1935. }
  1936. }
  1937. if (p_spatial) {
  1938. //_validate_selection();
  1939. //if (selected.has(p_spatial->get_instance_ID()) && selected.size()==1)
  1940. // return;
  1941. //_select(p_spatial->get_instance_ID(),false,true);
  1942. // should become the selection
  1943. }
  1944. }
  1945. void SpatialEditor::_xform_dialog_action() {
  1946. Transform t;
  1947. //translation
  1948. Vector3 scale;
  1949. Vector3 rotate;
  1950. Vector3 translate;
  1951. for(int i=0;i<3;i++) {
  1952. translate[i]=xform_translate[i]->get_text().to_double();
  1953. rotate[i]=Math::deg2rad(xform_rotate[i]->get_text().to_double());
  1954. scale[i]=xform_scale[i]->get_text().to_double();
  1955. }
  1956. t.origin=translate;
  1957. for(int i=0;i<3;i++) {
  1958. if (!rotate[i])
  1959. continue;
  1960. Vector3 axis;
  1961. axis[i]=1.0;
  1962. t.basis.rotate(axis,rotate[i]);
  1963. }
  1964. for(int i=0;i<3;i++) {
  1965. if (scale[i]==1)
  1966. continue;
  1967. t.basis.set_axis(i,t.basis.get_axis(i)*scale[i]);
  1968. }
  1969. undo_redo->create_action(TTR("XForm Dialog"));
  1970. List<Node*> &selection = editor_selection->get_selected_node_list();
  1971. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1972. Spatial *sp = E->get()->cast_to<Spatial>();
  1973. if (!sp)
  1974. continue;
  1975. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1976. if (!se)
  1977. continue;
  1978. bool post = xform_type->get_selected()>0;
  1979. Transform tr = sp->get_global_transform();
  1980. if (post)
  1981. tr = tr * t;
  1982. else {
  1983. tr.basis = t.basis * tr.basis;
  1984. tr.origin+=t.origin;
  1985. }
  1986. undo_redo->add_do_method(sp,"set_global_transform",tr);
  1987. undo_redo->add_undo_method(sp,"set_global_transform",sp->get_global_transform());
  1988. }
  1989. undo_redo->commit_action();
  1990. }
  1991. void SpatialEditor::_menu_item_pressed(int p_option) {
  1992. switch(p_option) {
  1993. case MENU_TOOL_SELECT:
  1994. case MENU_TOOL_MOVE:
  1995. case MENU_TOOL_ROTATE:
  1996. case MENU_TOOL_SCALE:
  1997. case MENU_TOOL_LIST_SELECT: {
  1998. for(int i=0;i<TOOL_MAX;i++)
  1999. tool_button[i]->set_pressed(i==p_option);
  2000. tool_mode=(ToolMode)p_option;
  2001. // static const char *_mode[]={"Selection Mode.","Translation Mode.","Rotation Mode.","Scale Mode.","List Selection Mode."};
  2002. // set_message(_mode[p_option],3);
  2003. update_transform_gizmo();
  2004. } break;
  2005. case MENU_TRANSFORM_USE_SNAP: {
  2006. bool is_checked = transform_menu->get_popup()->is_item_checked( transform_menu->get_popup()->get_item_index(p_option) );
  2007. snap_enabled=!is_checked;
  2008. transform_menu->get_popup()->set_item_checked( transform_menu->get_popup()->get_item_index(p_option), snap_enabled );
  2009. } break;
  2010. case MENU_TRANSFORM_CONFIGURE_SNAP: {
  2011. snap_dialog->popup_centered(Size2(200,180));
  2012. } break;
  2013. case MENU_TRANSFORM_LOCAL_COORDS: {
  2014. bool is_checked = transform_menu->get_popup()->is_item_checked( transform_menu->get_popup()->get_item_index(p_option) );
  2015. transform_menu->get_popup()->set_item_checked( transform_menu->get_popup()->get_item_index(p_option), !is_checked );
  2016. update_transform_gizmo();
  2017. } break;
  2018. case MENU_TRANSFORM_DIALOG: {
  2019. for(int i=0;i<3;i++) {
  2020. xform_translate[i]->set_text("0");
  2021. xform_rotate[i]->set_text("0");
  2022. xform_scale[i]->set_text("1");
  2023. }
  2024. xform_dialog->popup_centered(Size2(200,200));
  2025. } break;
  2026. case MENU_VIEW_USE_DEFAULT_LIGHT: {
  2027. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2028. if (is_checked) {
  2029. VisualServer::get_singleton()->free(light_instance);
  2030. light_instance=RID();
  2031. } else {
  2032. light_instance=VisualServer::get_singleton()->instance_create2(light,get_tree()->get_root()->get_world()->get_scenario());
  2033. VisualServer::get_singleton()->instance_set_transform(light_instance,light_transform);
  2034. _update_default_light_angle();
  2035. }
  2036. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), light_instance.is_valid() );
  2037. } break;
  2038. case MENU_VIEW_USE_DEFAULT_SRGB: {
  2039. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2040. if (is_checked) {
  2041. viewport_environment->set_enable_fx(Environment::FX_SRGB,false);
  2042. } else {
  2043. viewport_environment->set_enable_fx(Environment::FX_SRGB,true);
  2044. }
  2045. is_checked = ! is_checked;
  2046. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), is_checked );
  2047. } break;
  2048. case MENU_VIEW_USE_1_VIEWPORT: {
  2049. for(int i=1;i<4;i++) {
  2050. viewports[i]->hide();
  2051. }
  2052. viewports[0]->set_area_as_parent_rect();
  2053. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true );
  2054. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2055. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2056. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2057. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2058. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2059. } break;
  2060. case MENU_VIEW_USE_2_VIEWPORTS: {
  2061. for(int i=1;i<4;i++) {
  2062. if (i==1 || i==3)
  2063. viewports[i]->hide();
  2064. else
  2065. viewports[i]->show();
  2066. }
  2067. viewports[0]->set_area_as_parent_rect();
  2068. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2069. viewports[2]->set_area_as_parent_rect();
  2070. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2071. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2072. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true );
  2073. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2074. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2075. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2076. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2077. } break;
  2078. case MENU_VIEW_USE_2_VIEWPORTS_ALT: {
  2079. for(int i=1;i<4;i++) {
  2080. if (i==1 || i==3)
  2081. viewports[i]->hide();
  2082. else
  2083. viewports[i]->show();
  2084. }
  2085. viewports[0]->set_area_as_parent_rect();
  2086. viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2087. viewports[2]->set_area_as_parent_rect();
  2088. viewports[2]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2089. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2090. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2091. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2092. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2093. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true );
  2094. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2095. } break;
  2096. case MENU_VIEW_USE_3_VIEWPORTS: {
  2097. for(int i=1;i<4;i++) {
  2098. if (i==1)
  2099. viewports[i]->hide();
  2100. else
  2101. viewports[i]->show();
  2102. }
  2103. viewports[0]->set_area_as_parent_rect();
  2104. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2105. viewports[2]->set_area_as_parent_rect();
  2106. viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2107. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2108. viewports[3]->set_area_as_parent_rect();
  2109. viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2110. viewports[3]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2111. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2112. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2113. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true );
  2114. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2115. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2116. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2117. } break;
  2118. case MENU_VIEW_USE_3_VIEWPORTS_ALT: {
  2119. for(int i=1;i<4;i++) {
  2120. if (i==1)
  2121. viewports[i]->hide();
  2122. else
  2123. viewports[i]->show();
  2124. }
  2125. viewports[0]->set_area_as_parent_rect();
  2126. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2127. viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2128. viewports[2]->set_area_as_parent_rect();
  2129. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2130. viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2131. viewports[3]->set_area_as_parent_rect();
  2132. viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2133. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2134. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2135. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2136. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2137. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2138. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true );
  2139. } break;
  2140. case MENU_VIEW_USE_4_VIEWPORTS: {
  2141. for(int i=1;i<4;i++) {
  2142. viewports[i]->show();
  2143. }
  2144. viewports[0]->set_area_as_parent_rect();
  2145. viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2146. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2147. viewports[1]->set_area_as_parent_rect();
  2148. viewports[1]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2149. viewports[1]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2150. viewports[2]->set_area_as_parent_rect();
  2151. viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2152. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2153. viewports[3]->set_area_as_parent_rect();
  2154. viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2155. viewports[3]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2156. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2157. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2158. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2159. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true );
  2160. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2161. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2162. } break;
  2163. case MENU_VIEW_DISPLAY_NORMAL: {
  2164. VisualServer::get_singleton()->scenario_set_debug( get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_DISABLED );
  2165. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), true );
  2166. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false );
  2167. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false );
  2168. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false );
  2169. } break;
  2170. case MENU_VIEW_DISPLAY_WIREFRAME: {
  2171. VisualServer::get_singleton()->scenario_set_debug( get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_WIREFRAME );
  2172. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false );
  2173. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), true );
  2174. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false );
  2175. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false );
  2176. } break;
  2177. case MENU_VIEW_DISPLAY_OVERDRAW: {
  2178. VisualServer::get_singleton()->scenario_set_debug( get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_OVERDRAW );
  2179. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false );
  2180. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false );
  2181. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), true );
  2182. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false );
  2183. } break;
  2184. case MENU_VIEW_DISPLAY_SHADELESS: {
  2185. VisualServer::get_singleton()->scenario_set_debug( get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_SHADELESS );
  2186. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false );
  2187. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false );
  2188. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false );
  2189. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), true );
  2190. } break;
  2191. case MENU_VIEW_ORIGIN: {
  2192. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2193. is_checked=!is_checked;
  2194. VisualServer::get_singleton()->instance_geometry_set_flag(origin_instance,VS::INSTANCE_FLAG_VISIBLE,is_checked);
  2195. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), is_checked);
  2196. } break;
  2197. case MENU_VIEW_GRID: {
  2198. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2199. grid_enabled=!is_checked;
  2200. for(int i=0;i<3;++i) {
  2201. if (grid_enable[i]) {
  2202. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,grid_enabled);
  2203. grid_visible[i]=grid_enabled;
  2204. }
  2205. }
  2206. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), grid_enabled );
  2207. } break;
  2208. case MENU_VIEW_CAMERA_SETTINGS: {
  2209. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size()+Size2(50,50));
  2210. } break;
  2211. }
  2212. }
  2213. void SpatialEditor::_init_indicators() {
  2214. //make sure that the camera indicator is not selectable
  2215. light=VisualServer::get_singleton()->light_create( VisualServer::LIGHT_DIRECTIONAL );
  2216. //VisualServer::get_singleton()->light_set_shadow( light, true );
  2217. light_instance=VisualServer::get_singleton()->instance_create2(light,get_tree()->get_root()->get_world()->get_scenario());
  2218. light_transform.rotate(Vector3(1,0,0),Math_PI/5.0);
  2219. VisualServer::get_singleton()->instance_set_transform(light_instance,light_transform);
  2220. //RID mat = VisualServer::get_singleton()->fixed_material_create();
  2221. ///VisualServer::get_singleton()->fixed_material_set_flag(mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA,true);
  2222. //VisualServer::get_singleton()->fixed_material_set_flag(mat, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  2223. {
  2224. indicator_mat = VisualServer::get_singleton()->fixed_material_create();
  2225. VisualServer::get_singleton()->material_set_flag( indicator_mat, VisualServer::MATERIAL_FLAG_UNSHADED, true );
  2226. VisualServer::get_singleton()->material_set_flag( indicator_mat, VisualServer::MATERIAL_FLAG_ONTOP, false );
  2227. VisualServer::get_singleton()->fixed_material_set_flag(indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA,true);
  2228. VisualServer::get_singleton()->fixed_material_set_flag(indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  2229. DVector<Color> grid_colors[3];
  2230. DVector<Vector3> grid_points[3];
  2231. Vector<Color> origin_colors;
  2232. Vector<Vector3> origin_points;
  2233. for(int i=0;i<3;i++) {
  2234. Vector3 axis;
  2235. axis[i]=1;
  2236. Vector3 axis_n1;
  2237. axis_n1[(i+1)%3]=1;
  2238. Vector3 axis_n2;
  2239. axis_n2[(i+2)%3]=1;
  2240. origin_colors.push_back(Color(axis.x,axis.y,axis.z));
  2241. origin_colors.push_back(Color(axis.x,axis.y,axis.z));
  2242. origin_points.push_back(axis*4096);
  2243. origin_points.push_back(axis*-4096);
  2244. #define ORIGIN_GRID_SIZE 25
  2245. for(int j=-ORIGIN_GRID_SIZE;j<=ORIGIN_GRID_SIZE;j++) {
  2246. grid_colors[i].push_back(Color(axis.x,axis.y,axis.z,0.2));
  2247. grid_colors[i].push_back(Color(axis.x,axis.y,axis.z,0.2));
  2248. grid_colors[i].push_back(Color(axis.x,axis.y,axis.z,0.2));
  2249. grid_colors[i].push_back(Color(axis.x,axis.y,axis.z,0.2));
  2250. grid_points[i].push_back(axis_n1*ORIGIN_GRID_SIZE+axis_n2*j);
  2251. grid_points[i].push_back(-axis_n1*ORIGIN_GRID_SIZE+axis_n2*j);
  2252. grid_points[i].push_back(axis_n2*ORIGIN_GRID_SIZE+axis_n1*j);
  2253. grid_points[i].push_back(-axis_n2*ORIGIN_GRID_SIZE+axis_n1*j);
  2254. }
  2255. grid[i] = VisualServer::get_singleton()->mesh_create();
  2256. Array d;
  2257. d.resize(VS::ARRAY_MAX);
  2258. d[VisualServer::ARRAY_VERTEX]=grid_points[i];
  2259. d[VisualServer::ARRAY_COLOR]=grid_colors[i];
  2260. VisualServer::get_singleton()->mesh_add_surface(grid[i],VisualServer::PRIMITIVE_LINES,d);
  2261. VisualServer::get_singleton()->mesh_surface_set_material(grid[i],0,indicator_mat);
  2262. grid_instance[i] = VisualServer::get_singleton()->instance_create2(grid[i],get_tree()->get_root()->get_world()->get_scenario());
  2263. grid_visible[i]=false;
  2264. grid_enable[i]=false;
  2265. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  2266. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(grid_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2267. VS::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  2268. }
  2269. origin = VisualServer::get_singleton()->mesh_create();
  2270. Array d;
  2271. d.resize(VS::ARRAY_MAX);
  2272. d[VisualServer::ARRAY_VERTEX]=origin_points;
  2273. d[VisualServer::ARRAY_COLOR]=origin_colors;
  2274. VisualServer::get_singleton()->mesh_add_surface(origin,VisualServer::PRIMITIVE_LINES,d);
  2275. VisualServer::get_singleton()->mesh_surface_set_material(origin,0,indicator_mat);
  2276. // origin = VisualServer::get_singleton()->poly_create();
  2277. // VisualServer::get_singleton()->poly_add_primitive(origin,origin_points,Vector<Vector3>(),origin_colors,Vector<Vector3>());
  2278. // VisualServer::get_singleton()->poly_set_material(origin,indicator_mat,true);
  2279. origin_instance = VisualServer::get_singleton()->instance_create2(origin,get_tree()->get_root()->get_world()->get_scenario());
  2280. VS::get_singleton()->instance_set_layer_mask(origin_instance,1<<SpatialEditorViewport::GIZMO_GRID_LAYER);
  2281. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(origin_instance, VS::SHADOW_CASTING_SETTING_OFF);
  2282. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[1],VS::INSTANCE_FLAG_VISIBLE,true);
  2283. grid_enable[1]=true;
  2284. grid_visible[1]=true;
  2285. grid_enabled=true;
  2286. last_grid_snap=1;
  2287. }
  2288. {
  2289. cursor_mesh = VisualServer::get_singleton()->mesh_create();
  2290. DVector<Vector3> cursor_points;
  2291. float cs = 0.25;
  2292. cursor_points.push_back(Vector3(+cs,0,0));
  2293. cursor_points.push_back(Vector3(-cs,0,0));
  2294. cursor_points.push_back(Vector3(0,+cs,0));
  2295. cursor_points.push_back(Vector3(0,-cs,0));
  2296. cursor_points.push_back(Vector3(0,0,+cs));
  2297. cursor_points.push_back(Vector3(0,0,-cs));
  2298. cursor_material=VisualServer::get_singleton()->fixed_material_create();
  2299. VisualServer::get_singleton()->fixed_material_set_param(cursor_material,VS::FIXED_MATERIAL_PARAM_DIFFUSE,Color(0,1,1));
  2300. VisualServer::get_singleton()->material_set_flag( cursor_material, VisualServer::MATERIAL_FLAG_UNSHADED, true );
  2301. VisualServer::get_singleton()->fixed_material_set_flag(cursor_material, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA,true);
  2302. VisualServer::get_singleton()->fixed_material_set_flag(cursor_material, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  2303. Array d;
  2304. d.resize(VS::ARRAY_MAX);
  2305. d[VS::ARRAY_VERTEX]=cursor_points;
  2306. VisualServer::get_singleton()->mesh_add_surface(cursor_mesh,VS::PRIMITIVE_LINES,d);
  2307. VisualServer::get_singleton()->mesh_surface_set_material(cursor_mesh,0,cursor_material);
  2308. cursor_instance = VisualServer::get_singleton()->instance_create2(cursor_mesh,get_tree()->get_root()->get_world()->get_scenario());
  2309. VS::get_singleton()->instance_set_layer_mask(cursor_instance,1<<SpatialEditorViewport::GIZMO_GRID_LAYER);
  2310. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(cursor_instance, VS::SHADOW_CASTING_SETTING_OFF);
  2311. }
  2312. {
  2313. //move gizmo
  2314. float gizmo_alph = EditorSettings::get_singleton()->get("3d_editor/manipulator_gizmo_opacity");
  2315. gizmo_hl = Ref<FixedMaterial>( memnew( FixedMaterial ) );
  2316. gizmo_hl->set_flag(Material::FLAG_UNSHADED, true);
  2317. gizmo_hl->set_flag(Material::FLAG_ONTOP, true);
  2318. gizmo_hl->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2319. gizmo_hl->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1,gizmo_alph+0.2f));
  2320. for(int i=0;i<3;i++) {
  2321. move_gizmo[i]=Ref<Mesh>( memnew( Mesh ) );
  2322. rotate_gizmo[i]=Ref<Mesh>( memnew( Mesh ) );
  2323. Ref<FixedMaterial> mat = memnew( FixedMaterial );
  2324. mat->set_flag(Material::FLAG_UNSHADED, true);
  2325. mat->set_flag(Material::FLAG_ONTOP, true);
  2326. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2327. Color col;
  2328. col[i]=1.0;
  2329. col.a= gizmo_alph;
  2330. mat->set_parameter(FixedMaterial::PARAM_DIFFUSE,col);
  2331. gizmo_color[i]=mat;
  2332. Vector3 ivec;
  2333. ivec[i]=1;
  2334. Vector3 nivec;
  2335. nivec[(i+1)%3]=1;
  2336. nivec[(i+2)%3]=1;
  2337. Vector3 ivec2;
  2338. ivec2[(i+1)%3]=1;
  2339. Vector3 ivec3;
  2340. ivec3[(i+2)%3]=1;
  2341. {
  2342. Ref<SurfaceTool> surftool = memnew( SurfaceTool );
  2343. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  2344. //translate
  2345. const int arrow_points=5;
  2346. Vector3 arrow[5]={
  2347. nivec*0.0+ivec*0.0,
  2348. nivec*0.01+ivec*0.0,
  2349. nivec*0.01+ivec*1.0,
  2350. nivec*0.1+ivec*1.0,
  2351. nivec*0.0+ivec*(1+GIZMO_ARROW_SIZE),
  2352. };
  2353. int arrow_sides=6;
  2354. for(int k = 0; k < 7 ; k++) {
  2355. Matrix3 ma(ivec,Math_PI*2*float(k)/arrow_sides);
  2356. Matrix3 mb(ivec,Math_PI*2*float(k+1)/arrow_sides);
  2357. for(int j=0;j<arrow_points-1;j++) {
  2358. Vector3 points[4]={
  2359. ma.xform(arrow[j]),
  2360. mb.xform(arrow[j]),
  2361. mb.xform(arrow[j+1]),
  2362. ma.xform(arrow[j+1]),
  2363. };
  2364. surftool->add_vertex(points[0]);
  2365. surftool->add_vertex(points[1]);
  2366. surftool->add_vertex(points[2]);
  2367. surftool->add_vertex(points[0]);
  2368. surftool->add_vertex(points[2]);
  2369. surftool->add_vertex(points[3]);
  2370. }
  2371. }
  2372. surftool->set_material(mat);
  2373. surftool->commit(move_gizmo[i]);
  2374. }
  2375. {
  2376. Ref<SurfaceTool> surftool = memnew( SurfaceTool );
  2377. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  2378. Vector3 circle[5]={
  2379. ivec*0.02+ivec2*0.02+ivec2*1.0,
  2380. ivec*-0.02+ivec2*0.02+ivec2*1.0,
  2381. ivec*-0.02+ivec2*-0.02+ivec2*1.0,
  2382. ivec*0.02+ivec2*-0.02+ivec2*1.0,
  2383. ivec*0.02+ivec2*0.02+ivec2*1.0,
  2384. };
  2385. for(int k = 0; k < 33 ; k++) {
  2386. Matrix3 ma(ivec,Math_PI*2*float(k)/32);
  2387. Matrix3 mb(ivec,Math_PI*2*float(k+1)/32);
  2388. for(int j=0;j<4;j++) {
  2389. Vector3 points[4]={
  2390. ma.xform(circle[j]),
  2391. mb.xform(circle[j]),
  2392. mb.xform(circle[j+1]),
  2393. ma.xform(circle[j+1]),
  2394. };
  2395. surftool->add_vertex(points[0]);
  2396. surftool->add_vertex(points[1]);
  2397. surftool->add_vertex(points[2]);
  2398. surftool->add_vertex(points[0]);
  2399. surftool->add_vertex(points[2]);
  2400. surftool->add_vertex(points[3]);
  2401. }
  2402. }
  2403. surftool->set_material(mat);
  2404. surftool->commit(rotate_gizmo[i]);
  2405. }
  2406. }
  2407. }
  2408. /*for(int i=0;i<4;i++) {
  2409. viewports[i]->init_gizmo_instance(i);
  2410. }*/
  2411. _generate_selection_box();
  2412. //get_scene()->get_root_node()->cast_to<EditorNode>()->get_scene_root()->add_child(camera);
  2413. //current_camera=camera;
  2414. }
  2415. void SpatialEditor::_finish_indicators() {
  2416. VisualServer::get_singleton()->free(origin_instance);
  2417. VisualServer::get_singleton()->free(origin);
  2418. for(int i=0;i<3;i++) {
  2419. VisualServer::get_singleton()->free(grid_instance[i]);
  2420. VisualServer::get_singleton()->free(grid[i]);
  2421. }
  2422. VisualServer::get_singleton()->free(light_instance);
  2423. VisualServer::get_singleton()->free(light);
  2424. //VisualServer::get_singleton()->free(poly);
  2425. //VisualServer::get_singleton()->free(indicators_instance);
  2426. //VisualServer::get_singleton()->free(indicators);
  2427. VisualServer::get_singleton()->free(cursor_instance);
  2428. VisualServer::get_singleton()->free(cursor_mesh);
  2429. VisualServer::get_singleton()->free(indicator_mat);
  2430. VisualServer::get_singleton()->free(cursor_material);
  2431. }
  2432. void SpatialEditor::_instance_scene() {
  2433. #if 0
  2434. EditorNode *en = get_scene()->get_root_node()->cast_to<EditorNode>();
  2435. ERR_FAIL_COND(!en);
  2436. String path = en->get_scenes_dock()->get_selected_path();
  2437. if (path=="") {
  2438. set_message(TTR("No scene selected to instance!"));
  2439. return;
  2440. }
  2441. undo_redo->create_action(TTR("Instance at Cursor"));
  2442. Node* scene = en->request_instance_scene(path);
  2443. if (!scene) {
  2444. set_message(TTR("Could not instance scene!"));
  2445. undo_redo->commit_action(); //bleh
  2446. return;
  2447. }
  2448. Spatial *s = scene->cast_to<Spatial>();
  2449. if (s) {
  2450. undo_redo->add_do_method(s,"set_global_transform",Transform(Matrix3(),cursor.cursor_pos));
  2451. }
  2452. undo_redo->commit_action();
  2453. #endif
  2454. }
  2455. void SpatialEditor::_unhandled_key_input(InputEvent p_event) {
  2456. if (!is_visible() || get_viewport()->gui_has_modal_stack())
  2457. return;
  2458. {
  2459. EditorNode *en = editor;
  2460. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  2461. if (!over_plugin_list->empty() && over_plugin_list->forward_input_event(p_event)) {
  2462. return; //ate the over input event
  2463. }
  2464. }
  2465. switch(p_event.type) {
  2466. case InputEvent::KEY: {
  2467. const InputEventKey &k=p_event.key;
  2468. if (!k.pressed)
  2469. break;
  2470. switch(k.scancode) {
  2471. case KEY_Q: _menu_item_pressed(MENU_TOOL_SELECT); break;
  2472. case KEY_W: _menu_item_pressed(MENU_TOOL_MOVE); break;
  2473. case KEY_E: _menu_item_pressed(MENU_TOOL_ROTATE); break;
  2474. case KEY_R: _menu_item_pressed(MENU_TOOL_SCALE); break;
  2475. case KEY_Z: {
  2476. if (k.mod.shift || k.mod.control || k.mod.command)
  2477. break;
  2478. if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME))) {
  2479. _menu_item_pressed(MENU_VIEW_DISPLAY_NORMAL);
  2480. } else {
  2481. _menu_item_pressed(MENU_VIEW_DISPLAY_WIREFRAME);
  2482. }
  2483. } break;
  2484. #if 0
  2485. #endif
  2486. }
  2487. } break;
  2488. }
  2489. }
  2490. void SpatialEditor::_notification(int p_what) {
  2491. if (p_what==NOTIFICATION_READY) {
  2492. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon( get_icon("ToolSelect","EditorIcons") );
  2493. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon( get_icon("ToolMove","EditorIcons") );
  2494. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon( get_icon("ToolRotate","EditorIcons") );
  2495. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon( get_icon("ToolScale","EditorIcons") );
  2496. tool_button[SpatialEditor::TOOL_MODE_LIST_SELECT]->set_icon( get_icon("ListSelect","EditorIcons") );
  2497. instance_button->set_icon( get_icon("SpatialAdd","EditorIcons") );
  2498. instance_button->hide();
  2499. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT),get_icon("Panels1","EditorIcons"));
  2500. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS),get_icon("Panels2","EditorIcons"));
  2501. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT),get_icon("Panels2Alt","EditorIcons"));
  2502. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS),get_icon("Panels3","EditorIcons"));
  2503. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT),get_icon("Panels3Alt","EditorIcons"));
  2504. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS),get_icon("Panels4","EditorIcons"));
  2505. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  2506. get_tree()->connect("node_removed",this,"_node_removed");
  2507. VS::get_singleton()->scenario_set_fallback_environment(get_viewport()->find_world()->get_scenario(),viewport_environment->get_rid());
  2508. }
  2509. if (p_what==NOTIFICATION_ENTER_TREE) {
  2510. gizmos = memnew( SpatialEditorGizmos );
  2511. _init_indicators();
  2512. _update_default_light_angle();
  2513. }
  2514. if (p_what==NOTIFICATION_EXIT_TREE) {
  2515. _finish_indicators();
  2516. memdelete( gizmos );
  2517. }
  2518. }
  2519. void SpatialEditor::add_control_to_menu_panel(Control *p_control) {
  2520. hbc_menu->add_child(p_control);
  2521. }
  2522. void SpatialEditor::set_can_preview(Camera* p_preview) {
  2523. for(int i=0;i<4;i++) {
  2524. viewports[i]->set_can_preview(p_preview);
  2525. }
  2526. }
  2527. VSplitContainer *SpatialEditor::get_shader_split() {
  2528. return shader_split;
  2529. }
  2530. HSplitContainer *SpatialEditor::get_palette_split() {
  2531. return palette_split;
  2532. }
  2533. void SpatialEditor::_request_gizmo(Object* p_obj) {
  2534. Spatial *sp=p_obj->cast_to<Spatial>();
  2535. if (!sp)
  2536. return;
  2537. if (editor->get_edited_scene() && (sp==editor->get_edited_scene() || sp->get_owner()==editor->get_edited_scene() || editor->get_edited_scene()->is_editable_instance(sp->get_owner()))) {
  2538. Ref<SpatialEditorGizmo> seg;
  2539. for(int i=0;i<EditorNode::get_singleton()->get_editor_data().get_editor_plugin_count();i++) {
  2540. seg = EditorNode::get_singleton()->get_editor_data().get_editor_plugin(i)->create_spatial_gizmo(sp);
  2541. if (seg.is_valid())
  2542. break;
  2543. }
  2544. if (!seg.is_valid()) {
  2545. seg = gizmos->get_gizmo(sp);
  2546. }
  2547. if (seg.is_valid()) {
  2548. sp->set_gizmo(seg);
  2549. }
  2550. if (seg.is_valid() && sp==selected) {
  2551. seg->set_selected(true);
  2552. selected->update_gizmo();
  2553. }
  2554. }
  2555. }
  2556. void SpatialEditor::_toggle_maximize_view(Object* p_viewport) {
  2557. if (!p_viewport) return;
  2558. SpatialEditorViewport *current_viewport = p_viewport->cast_to<SpatialEditorViewport>();
  2559. if (!current_viewport) return;
  2560. int index=-1;
  2561. bool maximized = false;
  2562. for(int i=0;i<4;i++) {
  2563. if (viewports[i]==current_viewport) {
  2564. index=i;
  2565. if ( current_viewport->get_global_rect() == viewport_base->get_global_rect() )
  2566. maximized=true;
  2567. break;
  2568. }
  2569. }
  2570. if (index==-1) return;
  2571. if (!maximized) {
  2572. for(int i=0;i<4;i++) {
  2573. if (i==index)
  2574. viewports[i]->set_area_as_parent_rect();
  2575. else
  2576. viewports[i]->hide();
  2577. }
  2578. } else {
  2579. for(int i=0;i<4;i++)
  2580. viewports[i]->show();
  2581. if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT) ))
  2582. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  2583. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS) ))
  2584. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  2585. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT) ))
  2586. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  2587. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS) ))
  2588. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  2589. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT) ))
  2590. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  2591. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS) ))
  2592. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  2593. }
  2594. }
  2595. void SpatialEditor::_node_removed(Node* p_node) {
  2596. if (p_node==selected)
  2597. selected=NULL;
  2598. }
  2599. void SpatialEditor::_bind_methods() {
  2600. // ObjectTypeDB::bind_method("_input_event",&SpatialEditor::_input_event);
  2601. ObjectTypeDB::bind_method("_unhandled_key_input",&SpatialEditor::_unhandled_key_input);
  2602. ObjectTypeDB::bind_method("_node_removed",&SpatialEditor::_node_removed);
  2603. ObjectTypeDB::bind_method("_menu_item_pressed",&SpatialEditor::_menu_item_pressed);
  2604. ObjectTypeDB::bind_method("_xform_dialog_action",&SpatialEditor::_xform_dialog_action);
  2605. ObjectTypeDB::bind_method("_instance_scene",&SpatialEditor::_instance_scene);
  2606. ObjectTypeDB::bind_method("_get_editor_data",&SpatialEditor::_get_editor_data);
  2607. ObjectTypeDB::bind_method("_request_gizmo",&SpatialEditor::_request_gizmo);
  2608. ObjectTypeDB::bind_method("_default_light_angle_input",&SpatialEditor::_default_light_angle_input);
  2609. ObjectTypeDB::bind_method("_update_ambient_light_color",&SpatialEditor::_update_ambient_light_color);
  2610. ObjectTypeDB::bind_method("_toggle_maximize_view",&SpatialEditor::_toggle_maximize_view);
  2611. ADD_SIGNAL( MethodInfo("transform_key_request") );
  2612. }
  2613. void SpatialEditor::clear() {
  2614. settings_fov->set_val(EDITOR_DEF("3d_editor/default_fov",60.0));
  2615. settings_znear->set_val(EDITOR_DEF("3d_editor/default_z_near",0.1));
  2616. settings_zfar->set_val(EDITOR_DEF("3d_editor/default_z_far",1500.0));
  2617. for(int i=0;i<4;i++) {
  2618. viewports[i]->reset();
  2619. }
  2620. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  2621. _menu_item_pressed(MENU_VIEW_DISPLAY_NORMAL);
  2622. VisualServer::get_singleton()->instance_geometry_set_flag(origin_instance,VS::INSTANCE_FLAG_VISIBLE,true);
  2623. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true);
  2624. for(int i=0;i<3;++i) {
  2625. if (grid_enable[i]) {
  2626. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,true);
  2627. grid_visible[i]=true;
  2628. }
  2629. }
  2630. for(int i=0;i<4;i++) {
  2631. viewports[i]->view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(SpatialEditorViewport::VIEW_AUDIO_LISTENER),i==0);
  2632. viewports[i]->viewport->set_as_audio_listener(i==0);
  2633. }
  2634. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_GRID), true );
  2635. settings_default_light_rot_x=Math_PI*0.3;
  2636. settings_default_light_rot_y=Math_PI*0.2;
  2637. viewport_environment->fx_set_param(Environment::FX_PARAM_AMBIENT_LIGHT_COLOR,Color(0.15,0.15,0.15));
  2638. settings_ambient_color->set_color(Color(0.15,0.15,0.15));
  2639. if (!light_instance.is_valid())
  2640. _menu_item_pressed(MENU_VIEW_USE_DEFAULT_LIGHT);
  2641. _update_default_light_angle();
  2642. }
  2643. void SpatialEditor::_update_ambient_light_color(const Color& p_color) {
  2644. viewport_environment->fx_set_param(Environment::FX_PARAM_AMBIENT_LIGHT_COLOR,settings_ambient_color->get_color());
  2645. }
  2646. void SpatialEditor::_update_default_light_angle() {
  2647. Transform t;
  2648. t.basis.rotate(Vector3(1,0,0),settings_default_light_rot_x);
  2649. t.basis.rotate(Vector3(0,1,0),settings_default_light_rot_y);
  2650. settings_dlight->set_transform(t);
  2651. if (light_instance.is_valid()) {
  2652. VS::get_singleton()->instance_set_transform(light_instance,t);
  2653. }
  2654. }
  2655. void SpatialEditor::_default_light_angle_input(const InputEvent& p_event) {
  2656. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&(0x1|0x2|0x4)) {
  2657. settings_default_light_rot_y = Math::fposmod(settings_default_light_rot_y - p_event.mouse_motion.relative_x*0.01,Math_PI*2.0);
  2658. settings_default_light_rot_x = Math::fposmod(settings_default_light_rot_x - p_event.mouse_motion.relative_y*0.01,Math_PI*2.0);
  2659. _update_default_light_angle();
  2660. }
  2661. }
  2662. SpatialEditor::SpatialEditor(EditorNode *p_editor) {
  2663. gizmo.visible=true;
  2664. gizmo.scale=1.0;
  2665. viewport_environment = Ref<Environment>( memnew( Environment ) );
  2666. undo_redo=p_editor->get_undo_redo();
  2667. VBoxContainer *vbc = this;
  2668. custom_camera=NULL;
  2669. singleton=this;
  2670. editor=p_editor;
  2671. editor_selection=editor->get_editor_selection();
  2672. editor_selection->add_editor_plugin(this);
  2673. snap_enabled=false;
  2674. tool_mode = TOOL_MODE_SELECT;
  2675. //set_focus_mode(FOCUS_ALL);
  2676. hbc_menu = memnew( HBoxContainer );
  2677. vbc->add_child(hbc_menu);
  2678. Vector<Variant> button_binds;
  2679. button_binds.resize(1);
  2680. tool_button[TOOL_MODE_SELECT] = memnew( ToolButton );
  2681. hbc_menu->add_child( tool_button[TOOL_MODE_SELECT] );
  2682. tool_button[TOOL_MODE_SELECT]->set_toggle_mode(true);
  2683. tool_button[TOOL_MODE_SELECT]->set_flat(true);
  2684. tool_button[TOOL_MODE_SELECT]->set_pressed(true);
  2685. button_binds[0]=MENU_TOOL_SELECT;
  2686. tool_button[TOOL_MODE_SELECT]->connect("pressed", this,"_menu_item_pressed",button_binds);
  2687. tool_button[TOOL_MODE_SELECT]->set_tooltip("Select Mode (Q)\n"+keycode_get_string(KEY_MASK_CMD)+"Drag: Rotate\nAlt+Drag: Move\nAlt+RMB: Depth list selection");
  2688. tool_button[TOOL_MODE_MOVE] = memnew( ToolButton );
  2689. hbc_menu->add_child( tool_button[TOOL_MODE_MOVE] );
  2690. tool_button[TOOL_MODE_MOVE]->set_toggle_mode(true);
  2691. tool_button[TOOL_MODE_MOVE]->set_flat(true);
  2692. button_binds[0]=MENU_TOOL_MOVE;
  2693. tool_button[TOOL_MODE_MOVE]->connect("pressed", this,"_menu_item_pressed",button_binds);
  2694. tool_button[TOOL_MODE_MOVE]->set_tooltip(TTR("Move Mode (W)"));
  2695. tool_button[TOOL_MODE_ROTATE] = memnew( ToolButton );
  2696. hbc_menu->add_child( tool_button[TOOL_MODE_ROTATE] );
  2697. tool_button[TOOL_MODE_ROTATE]->set_toggle_mode(true);
  2698. tool_button[TOOL_MODE_ROTATE]->set_flat(true);
  2699. button_binds[0]=MENU_TOOL_ROTATE;
  2700. tool_button[TOOL_MODE_ROTATE]->connect("pressed", this,"_menu_item_pressed",button_binds);
  2701. tool_button[TOOL_MODE_ROTATE]->set_tooltip(TTR("Rotate Mode (E)"));
  2702. tool_button[TOOL_MODE_SCALE] = memnew( ToolButton );
  2703. hbc_menu->add_child( tool_button[TOOL_MODE_SCALE] );
  2704. tool_button[TOOL_MODE_SCALE]->set_toggle_mode(true);
  2705. tool_button[TOOL_MODE_SCALE]->set_flat(true);
  2706. button_binds[0]=MENU_TOOL_SCALE;
  2707. tool_button[TOOL_MODE_SCALE]->connect("pressed", this,"_menu_item_pressed",button_binds);
  2708. tool_button[TOOL_MODE_SCALE]->set_tooltip(TTR("Scale Mode (R)"));
  2709. instance_button = memnew( Button );
  2710. hbc_menu->add_child( instance_button );
  2711. instance_button->set_flat(true);
  2712. instance_button->connect("pressed",this,"_instance_scene");
  2713. instance_button->hide();
  2714. VSeparator *vs = memnew( VSeparator );
  2715. hbc_menu->add_child(vs);
  2716. tool_button[TOOL_MODE_LIST_SELECT] = memnew( ToolButton );
  2717. hbc_menu->add_child( tool_button[TOOL_MODE_LIST_SELECT] );
  2718. tool_button[TOOL_MODE_LIST_SELECT]->set_toggle_mode(true);
  2719. tool_button[TOOL_MODE_LIST_SELECT]->set_flat(true);
  2720. button_binds[0]=MENU_TOOL_LIST_SELECT;
  2721. tool_button[TOOL_MODE_LIST_SELECT]->connect("pressed", this,"_menu_item_pressed",button_binds);
  2722. tool_button[TOOL_MODE_LIST_SELECT]->set_tooltip(TTR("Show a list of all objects at the position clicked\n(same as Alt+RMB in select mode)."));
  2723. vs = memnew( VSeparator );
  2724. hbc_menu->add_child(vs);
  2725. ED_SHORTCUT("spatial_editor/bottom_view", TTR("Bottom View"), KEY_MASK_ALT+KEY_KP_7);
  2726. ED_SHORTCUT("spatial_editor/top_view", TTR("Top View"), KEY_KP_7);
  2727. ED_SHORTCUT("spatial_editor/rear_view", TTR("Rear View"), KEY_MASK_ALT+KEY_KP_1);
  2728. ED_SHORTCUT("spatial_editor/front_view", TTR("Front View"), KEY_KP_1);
  2729. ED_SHORTCUT("spatial_editor/left_view", TTR("Left View"), KEY_MASK_ALT+KEY_KP_3);
  2730. ED_SHORTCUT("spatial_editor/right_view", TTR("Right View"), KEY_KP_3);
  2731. ED_SHORTCUT("spatial_editor/switch_perspective_orthogonal", TTR("Switch Perspective/Orthogonal view"), KEY_KP_5);
  2732. ED_SHORTCUT("spatial_editor/snap", TTR("Snap"), KEY_S);
  2733. ED_SHORTCUT("spatial_editor/insert_anim_key", TTR("Insert Animation Key"), KEY_K);
  2734. ED_SHORTCUT("spatial_editor/focus_selection", TTR("Focus Selection"), KEY_F);
  2735. ED_SHORTCUT("spatial_editor/align_selection_with_view", TTR("Align Selection With View"), KEY_MASK_ALT+KEY_MASK_CMD+KEY_F);
  2736. PopupMenu *p;
  2737. transform_menu = memnew( MenuButton );
  2738. transform_menu->set_text(TTR("Transform"));
  2739. hbc_menu->add_child( transform_menu );
  2740. p = transform_menu->get_popup();
  2741. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/use_snap", TTR("Use Snap")), MENU_TRANSFORM_USE_SNAP);
  2742. p->add_shortcut(ED_SHORTCUT("spatial_editor/configure_snap", TTR("Configure Snap..")), MENU_TRANSFORM_CONFIGURE_SNAP);
  2743. p->add_separator();
  2744. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/local_coords", TTR("Local Coords")), MENU_TRANSFORM_LOCAL_COORDS);
  2745. //p->set_item_checked(p->get_item_count()-1,true);
  2746. p->add_separator();
  2747. p->add_shortcut(ED_SHORTCUT("spatial_editor/transform_dialog", TTR("Transform Dialog..")), MENU_TRANSFORM_DIALOG);
  2748. p->connect("item_pressed", this,"_menu_item_pressed");
  2749. view_menu = memnew( MenuButton );
  2750. view_menu->set_text(TTR("View"));
  2751. view_menu->set_pos( Point2( 212,0) );
  2752. hbc_menu->add_child( view_menu );
  2753. p = view_menu->get_popup();
  2754. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/use_default_light", TTR("Use Default Light")), MENU_VIEW_USE_DEFAULT_LIGHT);
  2755. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/use_default_srgb", TTR("Use Default sRGB")), MENU_VIEW_USE_DEFAULT_SRGB);
  2756. p->add_separator();
  2757. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/1_viewport", TTR("1 Viewport"), KEY_MASK_CMD+KEY_1), MENU_VIEW_USE_1_VIEWPORT);
  2758. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports", TTR("2 Viewports"), KEY_MASK_CMD+KEY_2), MENU_VIEW_USE_2_VIEWPORTS);
  2759. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports_alt", TTR("2 Viewports (Alt)"), KEY_MASK_ALT+KEY_MASK_CMD+KEY_2), MENU_VIEW_USE_2_VIEWPORTS_ALT);
  2760. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports", TTR("3 Viewports"),KEY_MASK_CMD+KEY_3), MENU_VIEW_USE_3_VIEWPORTS);
  2761. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports_alt", TTR("3 Viewports (Alt)"), KEY_MASK_ALT+KEY_MASK_CMD+KEY_3), MENU_VIEW_USE_3_VIEWPORTS_ALT);
  2762. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/4_viewports", TTR("4 Viewports"), KEY_MASK_CMD+KEY_4), MENU_VIEW_USE_4_VIEWPORTS);
  2763. p->add_separator();
  2764. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/display_normal", TTR("Display Normal")), MENU_VIEW_DISPLAY_NORMAL);
  2765. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/display_wireframe", TTR("Display Wireframe")), MENU_VIEW_DISPLAY_WIREFRAME);
  2766. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/display_overdraw", TTR("Display Overdraw")), MENU_VIEW_DISPLAY_OVERDRAW);
  2767. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/display_shadeless", TTR("Display Shadeless")), MENU_VIEW_DISPLAY_SHADELESS);
  2768. p->add_separator();
  2769. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_origin", TTR("View Origin")), MENU_VIEW_ORIGIN);
  2770. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid", TTR("View Grid")), MENU_VIEW_GRID);
  2771. p->add_separator();
  2772. p->add_shortcut(ED_SHORTCUT("spatial_editor/settings", TTR("Settings")), MENU_VIEW_CAMERA_SETTINGS);
  2773. p->set_item_checked( p->get_item_index(MENU_VIEW_USE_DEFAULT_LIGHT), true );
  2774. p->set_item_checked( p->get_item_index(MENU_VIEW_DISPLAY_NORMAL), true );
  2775. p->set_item_checked( p->get_item_index(MENU_VIEW_ORIGIN), true );
  2776. p->set_item_checked( p->get_item_index(MENU_VIEW_GRID), true );
  2777. p->connect("item_pressed", this,"_menu_item_pressed");
  2778. /* REST OF MENU */
  2779. palette_split = memnew( HSplitContainer);
  2780. palette_split->set_v_size_flags(SIZE_EXPAND_FILL);
  2781. vbc->add_child(palette_split);
  2782. shader_split = memnew( VSplitContainer );
  2783. shader_split->set_h_size_flags(SIZE_EXPAND_FILL);
  2784. palette_split->add_child(shader_split);
  2785. viewport_base = memnew( Control );
  2786. shader_split->add_child(viewport_base);
  2787. viewport_base->set_v_size_flags(SIZE_EXPAND_FILL);
  2788. for(int i=0;i<4;i++) {
  2789. viewports[i] = memnew( SpatialEditorViewport(this,editor,i) );
  2790. viewports[i]->connect("toggle_maximize_view",this,"_toggle_maximize_view");
  2791. viewport_base->add_child(viewports[i]);
  2792. }
  2793. //vbc->add_child(viewport_base);
  2794. /* SNAP DIALOG */
  2795. snap_dialog = memnew( ConfirmationDialog );
  2796. snap_dialog->set_title(TTR("Snap Settings"));
  2797. add_child(snap_dialog);
  2798. VBoxContainer *snap_dialog_vbc = memnew( VBoxContainer );
  2799. snap_dialog->add_child(snap_dialog_vbc);
  2800. snap_dialog->set_child_rect(snap_dialog_vbc);
  2801. snap_translate = memnew( LineEdit );
  2802. snap_translate->set_text("1");
  2803. snap_dialog_vbc->add_margin_child(TTR("Translate Snap:"),snap_translate);
  2804. snap_rotate = memnew( LineEdit );
  2805. snap_rotate->set_text("5");
  2806. snap_dialog_vbc->add_margin_child(TTR("Rotate Snap (deg.):"),snap_rotate);
  2807. snap_scale = memnew( LineEdit );
  2808. snap_scale->set_text("5");
  2809. snap_dialog_vbc->add_margin_child(TTR("Scale Snap (%):"),snap_scale);
  2810. /* SETTINGS DIALOG */
  2811. settings_dialog = memnew( ConfirmationDialog );
  2812. settings_dialog->set_title(TTR("Viewport Settings"));
  2813. add_child(settings_dialog);
  2814. settings_vbc = memnew( VBoxContainer );
  2815. settings_vbc->set_custom_minimum_size(Size2(200,0));
  2816. settings_dialog->add_child(settings_vbc);
  2817. settings_dialog->set_child_rect(settings_vbc);
  2818. settings_light_base = memnew( Control );
  2819. settings_light_base->set_custom_minimum_size(Size2(128,128));
  2820. settings_light_base->connect("input_event",this,"_default_light_angle_input");
  2821. settings_vbc->add_margin_child(TTR("Default Light Normal:"),settings_light_base);
  2822. settings_light_vp = memnew( Viewport );
  2823. settings_light_vp->set_disable_input(true);
  2824. settings_light_vp->set_use_own_world(true);
  2825. settings_light_base->add_child(settings_light_vp);
  2826. settings_dlight = memnew( DirectionalLight );
  2827. settings_light_vp->add_child(settings_dlight);
  2828. settings_sphere = memnew( ImmediateGeometry );
  2829. settings_sphere->begin(Mesh::PRIMITIVE_TRIANGLES,Ref<Texture>());
  2830. settings_sphere->set_color(Color(1,1,1));
  2831. settings_sphere->add_sphere(32,16,1);
  2832. settings_sphere->end();
  2833. settings_light_vp->add_child(settings_sphere);
  2834. settings_camera = memnew( Camera );
  2835. settings_light_vp->add_child(settings_camera);
  2836. settings_camera->set_translation(Vector3(0,0,2));
  2837. settings_camera->set_orthogonal(2.1,0.1,5);
  2838. settings_default_light_rot_x=Math_PI*0.3;
  2839. settings_default_light_rot_y=Math_PI*0.2;
  2840. settings_ambient_color = memnew( ColorPickerButton );
  2841. settings_vbc->add_margin_child(TTR("Ambient Light Color:"),settings_ambient_color);
  2842. settings_ambient_color->connect("color_changed",this,"_update_ambient_light_color");
  2843. viewport_environment->set_enable_fx(Environment::FX_AMBIENT_LIGHT,true);
  2844. viewport_environment->fx_set_param(Environment::FX_PARAM_AMBIENT_LIGHT_COLOR,Color(0.15,0.15,0.15));
  2845. settings_ambient_color->set_color(Color(0.15,0.15,0.15));
  2846. settings_fov = memnew( SpinBox );
  2847. settings_fov->set_max(179);
  2848. settings_fov->set_min(1);
  2849. settings_fov->set_step(0.01);
  2850. settings_fov->set_val(EDITOR_DEF("3d_editor/default_fov",60.0));
  2851. settings_vbc->add_margin_child(TTR("Perspective FOV (deg.):"),settings_fov);
  2852. settings_znear = memnew( SpinBox );
  2853. settings_znear->set_max(10000);
  2854. settings_znear->set_min(0.1);
  2855. settings_znear->set_step(0.01);
  2856. settings_znear->set_val(EDITOR_DEF("3d_editor/default_z_near",0.1));
  2857. settings_vbc->add_margin_child(TTR("View Z-Near:"),settings_znear);
  2858. settings_zfar = memnew( SpinBox );
  2859. settings_zfar->set_max(10000);
  2860. settings_zfar->set_min(0.1);
  2861. settings_zfar->set_step(0.01);
  2862. settings_zfar->set_val(EDITOR_DEF("3d_editor/default_z_far",1500));
  2863. settings_vbc->add_margin_child(TTR("View Z-Far:"),settings_zfar);
  2864. //settings_dialog->get_cancel()->hide();
  2865. /* XFORM DIALOG */
  2866. xform_dialog = memnew( ConfirmationDialog );
  2867. xform_dialog->set_title(TTR("Transform Change"));
  2868. add_child(xform_dialog);
  2869. Label *l = memnew(Label);
  2870. l->set_text(TTR("Translate:"));
  2871. l->set_pos(Point2(5,5));
  2872. xform_dialog->add_child(l);
  2873. for(int i=0;i<3;i++) {
  2874. xform_translate[i] = memnew( LineEdit );
  2875. xform_translate[i]->set_pos( Point2(15+i*60,22) );
  2876. xform_translate[i]->set_size( Size2(50,12 ) );
  2877. xform_dialog->add_child( xform_translate[i] );
  2878. }
  2879. l = memnew(Label);
  2880. l->set_text(TTR("Rotate (deg.):"));
  2881. l->set_pos(Point2(5,45));
  2882. xform_dialog->add_child(l);
  2883. for(int i=0;i<3;i++) {
  2884. xform_rotate[i] = memnew( LineEdit );
  2885. xform_rotate[i]->set_pos( Point2(15+i*60,62) );
  2886. xform_rotate[i]->set_size( Size2(50,22 ) );
  2887. xform_dialog->add_child(xform_rotate[i]);
  2888. }
  2889. l = memnew(Label);
  2890. l->set_text(TTR("Scale (ratio):"));
  2891. l->set_pos(Point2(5,85));
  2892. xform_dialog->add_child(l);
  2893. for(int i=0;i<3;i++) {
  2894. xform_scale[i] = memnew( LineEdit );
  2895. xform_scale[i]->set_pos( Point2(15+i*60,102) );
  2896. xform_scale[i]->set_size( Size2(50,22 ) );
  2897. xform_dialog->add_child(xform_scale[i]);
  2898. }
  2899. l = memnew(Label);
  2900. l->set_text(TTR("Transform Type"));
  2901. l->set_pos(Point2(5,125));
  2902. xform_dialog->add_child(l);
  2903. xform_type = memnew( OptionButton );
  2904. xform_type->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  2905. xform_type->set_begin( Point2(15,142) );
  2906. xform_type->set_end( Point2(15,75) );
  2907. xform_type->add_item(TTR("Pre"));
  2908. xform_type->add_item(TTR("Post"));
  2909. xform_dialog->add_child(xform_type);
  2910. xform_dialog->connect("confirmed", this,"_xform_dialog_action");
  2911. scenario_debug=VisualServer::SCENARIO_DEBUG_DISABLED;
  2912. selected=NULL;
  2913. set_process_unhandled_key_input(true);
  2914. add_to_group("_spatial_editor_group");
  2915. EDITOR_DEF("3d_editor/manipulator_gizmo_size",80);
  2916. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT,"3d_editor/manipulator_gizmo_size",PROPERTY_HINT_RANGE,"16,1024,1"));
  2917. EDITOR_DEF("3d_editor/manipulator_gizmo_opacity",0.2);
  2918. over_gizmo_handle=-1;
  2919. }
  2920. SpatialEditor::~SpatialEditor() {
  2921. }
  2922. void SpatialEditorPlugin::make_visible(bool p_visible) {
  2923. if (p_visible) {
  2924. spatial_editor->show();
  2925. spatial_editor->set_process(true);
  2926. //VisualServer::get_singleton()->viewport_set_hide_scenario(editor->get_scene_root()->get_viewport(),false);
  2927. spatial_editor->grab_focus();
  2928. } else {
  2929. spatial_editor->hide();
  2930. spatial_editor->set_process(false);
  2931. //VisualServer::get_singleton()->viewport_set_hide_scenario(editor->get_scene_root()->get_viewport(),true);
  2932. }
  2933. }
  2934. void SpatialEditorPlugin::edit(Object *p_object) {
  2935. spatial_editor->edit(p_object->cast_to<Spatial>());
  2936. }
  2937. bool SpatialEditorPlugin::handles(Object *p_object) const {
  2938. return p_object->is_type("Spatial");
  2939. }
  2940. Dictionary SpatialEditorPlugin::get_state() const {
  2941. return spatial_editor->get_state();
  2942. }
  2943. void SpatialEditorPlugin::set_state(const Dictionary& p_state) {
  2944. spatial_editor->set_state(p_state);
  2945. }
  2946. void SpatialEditor::snap_cursor_to_plane(const Plane& p_plane) {
  2947. // cursor.pos=p_plane.project(cursor.pos);
  2948. }
  2949. void SpatialEditorPlugin::_bind_methods() {
  2950. ObjectTypeDB::bind_method("snap_cursor_to_plane",&SpatialEditorPlugin::snap_cursor_to_plane);
  2951. }
  2952. void SpatialEditorPlugin::snap_cursor_to_plane(const Plane& p_plane) {
  2953. spatial_editor->snap_cursor_to_plane(p_plane);
  2954. }
  2955. SpatialEditorPlugin::SpatialEditorPlugin(EditorNode *p_node) {
  2956. editor=p_node;
  2957. spatial_editor = memnew( SpatialEditor(p_node) );
  2958. spatial_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  2959. editor->get_viewport()->add_child(spatial_editor);
  2960. //spatial_editor->set_area_as_parent_rect();
  2961. spatial_editor->hide();
  2962. spatial_editor->connect("transform_key_request",editor,"_transform_keyed");
  2963. //spatial_editor->set_process(true);
  2964. }
  2965. SpatialEditorPlugin::~SpatialEditorPlugin() {
  2966. }