spatial_editor_plugin.cpp 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188
  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<ObjectID> 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. Object *obj=ObjectDB::get_instance(instances[i]);
  193. if (!obj)
  194. continue;
  195. Spatial *spat=obj->cast_to<Spatial>();
  196. if (!spat)
  197. continue;
  198. Ref<SpatialEditorGizmo> seg = spat->get_gizmo();
  199. if (!seg.is_valid())
  200. continue;
  201. if (found_gizmos.has(seg))
  202. continue;
  203. found_gizmos.insert(seg);
  204. Vector3 point;
  205. Vector3 normal;
  206. int handle=-1;
  207. bool inters = seg->intersect_ray(camera,p_pos,point,normal,NULL,p_alt_select);
  208. if (!inters)
  209. continue;
  210. float dist = pos.distance_to(point);
  211. if (dist<0)
  212. continue;
  213. if (editor_selection->is_selected(spat))
  214. r_includes_current=true;
  215. _RayResult res;
  216. res.item=spat;
  217. res.depth=dist;
  218. res.handle=handle;
  219. results.push_back(res);
  220. }
  221. if (results.empty())
  222. return 0;
  223. results.sort();
  224. Spatial *s=NULL;
  225. if (!r_includes_current || results.size()==1 || (r_gizmo_handle && results.front()->get().handle>=0)) {
  226. //return the nearest one
  227. s = results.front()->get().item;
  228. if (r_gizmo_handle)
  229. *r_gizmo_handle=results.front()->get().handle;
  230. } else {
  231. //returns the next one from a curent selection
  232. List<_RayResult>::Element *E=results.front();
  233. List<_RayResult>::Element *S=NULL;
  234. while(true) {
  235. //very strange loop algorithm that complies with object selection standards (tm).
  236. if (S==E) {
  237. //went all around and anothing was found
  238. //since can't rotate the selection
  239. //just return the first one
  240. s=results.front()->get().item;
  241. break;
  242. }
  243. if (!S && editor_selection->is_selected(E->get().item)) {
  244. //found an item currently in the selection,
  245. //so start from this one
  246. S=E;
  247. }
  248. if (S && !editor_selection->is_selected(E->get().item)) {
  249. // free item after a selected item, this one is desired.
  250. s=E->get().item;
  251. break;
  252. }
  253. E=E->next();
  254. if (!E) {
  255. if (!S) {
  256. //did a loop but nothing was selected, select first
  257. s=results.front()->get().item;
  258. break;
  259. }
  260. E=results.front();
  261. }
  262. }
  263. }
  264. if (!s)
  265. return 0;
  266. return s->get_instance_ID();
  267. }
  268. void SpatialEditorViewport::_find_items_at_pos(const Point2& p_pos,bool &r_includes_current,Vector<_RayResult> &results,bool p_alt_select) {
  269. Vector3 ray=_get_ray(p_pos);
  270. Vector3 pos=_get_ray_pos(p_pos);
  271. Vector<ObjectID> instances=VisualServer::get_singleton()->instances_cull_ray(pos,ray,get_tree()->get_root()->get_world()->get_scenario() );
  272. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  273. r_includes_current=false;
  274. for (int i=0;i<instances.size();i++) {
  275. Object *obj=ObjectDB::get_instance(instances[i]);
  276. if (!obj)
  277. continue;
  278. Spatial *spat=obj->cast_to<Spatial>();
  279. if (!spat)
  280. continue;
  281. Ref<SpatialEditorGizmo> seg = spat->get_gizmo();
  282. if (!seg.is_valid())
  283. continue;
  284. if (found_gizmos.has(seg))
  285. continue;
  286. found_gizmos.insert(seg);
  287. Vector3 point;
  288. Vector3 normal;
  289. int handle=-1;
  290. bool inters = seg->intersect_ray(camera,p_pos,point,normal,NULL,p_alt_select);
  291. if (!inters)
  292. continue;
  293. float dist = pos.distance_to(point);
  294. if (dist<0)
  295. continue;
  296. if (editor_selection->is_selected(spat))
  297. r_includes_current=true;
  298. _RayResult res;
  299. res.item=spat;
  300. res.depth=dist;
  301. res.handle=handle;
  302. results.push_back(res);
  303. }
  304. if (results.empty())
  305. return;
  306. results.sort();
  307. }
  308. Vector3 SpatialEditorViewport::_get_screen_to_space(const Vector3& p_pos) {
  309. CameraMatrix cm;
  310. cm.set_perspective(get_fov(),get_size().get_aspect(),get_znear(),get_zfar());
  311. float screen_w,screen_h;
  312. cm.get_viewport_size(screen_w,screen_h);
  313. Transform camera_transform;
  314. camera_transform.translate( cursor.pos );
  315. camera_transform.basis.rotate(Vector3(0,1,0),cursor.y_rot);
  316. camera_transform.basis.rotate(Vector3(1,0,0),cursor.x_rot);
  317. camera_transform.translate(0,0,cursor.distance);
  318. 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()));
  319. }
  320. void SpatialEditorViewport::_select_region() {
  321. if (cursor.region_begin==cursor.region_end)
  322. return; //nothing really
  323. Vector3 box[4]={
  324. Vector3(
  325. MIN( cursor.region_begin.x, cursor.region_end.x),
  326. MIN( cursor.region_begin.y, cursor.region_end.y),
  327. 0
  328. ),
  329. Vector3(
  330. MAX( cursor.region_begin.x, cursor.region_end.x),
  331. MIN( cursor.region_begin.y, cursor.region_end.y),
  332. 0
  333. ),
  334. Vector3(
  335. MAX( cursor.region_begin.x, cursor.region_end.x),
  336. MAX( cursor.region_begin.y, cursor.region_end.y),
  337. 0
  338. ),
  339. Vector3(
  340. MIN( cursor.region_begin.x, cursor.region_end.x),
  341. MAX( cursor.region_begin.y, cursor.region_end.y),
  342. 0
  343. )
  344. };
  345. Vector<Plane> frustum;
  346. Vector3 cam_pos=_get_camera_pos();
  347. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  348. for(int i=0;i<4;i++) {
  349. Vector3 a=_get_screen_to_space(box[i]);
  350. Vector3 b=_get_screen_to_space(box[(i+1)%4]);
  351. frustum.push_back( Plane(a,b,cam_pos) );
  352. }
  353. Plane near( cam_pos, -_get_camera_normal() );
  354. near.d-=get_znear();
  355. frustum.push_back( near );
  356. Plane far=-near;
  357. far.d+=500.0;
  358. frustum.push_back( far );
  359. Vector<ObjectID> instances=VisualServer::get_singleton()->instances_cull_convex(frustum,get_tree()->get_root()->get_world()->get_scenario());
  360. for (int i=0;i<instances.size();i++) {
  361. Object *obj=ObjectDB::get_instance(instances[i]);
  362. if (!obj)
  363. continue;
  364. Spatial *sp = obj->cast_to<Spatial>();
  365. if (!sp)
  366. continue;
  367. Ref<SpatialEditorGizmo> seg = sp->get_gizmo();
  368. if (!seg.is_valid())
  369. continue;
  370. if (found_gizmos.has(seg))
  371. continue;
  372. if (seg->intersect_frustum(camera,frustum))
  373. _select(sp,true,false);
  374. }
  375. }
  376. void SpatialEditorViewport::_update_name() {
  377. String ortho = orthogonal?TTR("Orthogonal"):TTR("Perspective");
  378. if (name!="")
  379. view_menu->set_text("[ "+name+" "+ortho+" ]");
  380. else
  381. view_menu->set_text("[ "+ortho+" ]");
  382. }
  383. void SpatialEditorViewport::_compute_edit(const Point2& p_point) {
  384. _edit.click_ray=_get_ray( Vector2( p_point.x, p_point.y ) );
  385. _edit.click_ray_pos=_get_ray_pos( Vector2( p_point.x, p_point.y ) );
  386. _edit.plane=TRANSFORM_VIEW;
  387. spatial_editor->update_transform_gizmo();
  388. _edit.center=spatial_editor->get_gizmo_transform().origin;
  389. List<Node*> &selection = editor_selection->get_selected_node_list();
  390. // Vector3 center;
  391. // int nc=0;
  392. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  393. Spatial *sp = E->get()->cast_to<Spatial>();
  394. if (!sp)
  395. continue;
  396. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  397. if (!se)
  398. continue;
  399. se->original=se->sp->get_global_transform();
  400. // center+=se->original.origin;
  401. // nc++;
  402. }
  403. // if (nc)
  404. // _edit.center=center/float(nc);
  405. }
  406. static int _get_key_modifier(const String& p_property) {
  407. switch(EditorSettings::get_singleton()->get(p_property).operator int()) {
  408. case 0: return 0;
  409. case 1: return KEY_SHIFT;
  410. case 2: return KEY_ALT;
  411. case 3: return KEY_META;
  412. case 4: return KEY_CONTROL;
  413. }
  414. return 0;
  415. }
  416. SpatialEditorViewport::NavigationScheme SpatialEditorViewport::_get_navigation_schema(const String& p_property) {
  417. switch(EditorSettings::get_singleton()->get(p_property).operator int()) {
  418. case 0: return NAVIGATION_GODOT;
  419. case 1: return NAVIGATION_MAYA;
  420. case 2: return NAVIGATION_MODO;
  421. }
  422. return NAVIGATION_GODOT;
  423. }
  424. SpatialEditorViewport::NavigationZoomStyle SpatialEditorViewport::_get_navigation_zoom_style(const String& p_property) {
  425. switch(EditorSettings::get_singleton()->get(p_property).operator int()) {
  426. case 0: return NAVIGATION_ZOOM_VERTICAL;
  427. case 1: return NAVIGATION_ZOOM_HORIZONTAL;
  428. }
  429. return NAVIGATION_ZOOM_VERTICAL;
  430. }
  431. bool SpatialEditorViewport::_gizmo_select(const Vector2& p_screenpos,bool p_hilite_only) {
  432. if (!spatial_editor->is_gizmo_visible())
  433. return false;
  434. if (get_selected_count()==0) {
  435. if (p_hilite_only)
  436. spatial_editor->select_gizmo_hilight_axis(-1);
  437. return false;
  438. }
  439. Vector3 ray_pos=_get_ray_pos( Vector2( p_screenpos.x, p_screenpos.y ) );
  440. Vector3 ray=_get_ray( Vector2( p_screenpos.x, p_screenpos.y ) );
  441. Transform gt = spatial_editor->get_gizmo_transform();
  442. float gs=gizmo_scale;
  443. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_MOVE) {
  444. int col_axis=-1;
  445. float col_d=1e20;
  446. for(int i=0;i<3;i++) {
  447. Vector3 grabber_pos = gt.origin+gt.basis.get_axis(i)*gs;
  448. float grabber_radius = gs*GIZMO_ARROW_SIZE;
  449. Vector3 r;
  450. if (Geometry::segment_intersects_sphere(ray_pos,ray_pos+ray*10000.0,grabber_pos,grabber_radius,&r)) {
  451. float d = r.distance_to(ray_pos);
  452. if (d<col_d) {
  453. col_d=d;
  454. col_axis=i;
  455. }
  456. }
  457. }
  458. if (col_axis!=-1) {
  459. if (p_hilite_only) {
  460. spatial_editor->select_gizmo_hilight_axis(col_axis);
  461. } else {
  462. //handle rotate
  463. _edit.mode=TRANSFORM_TRANSLATE;
  464. _compute_edit(Point2(p_screenpos.x,p_screenpos.y));
  465. _edit.plane=TransformPlane(TRANSFORM_X_AXIS+col_axis);
  466. }
  467. return true;
  468. }
  469. }
  470. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_ROTATE) {
  471. int col_axis=-1;
  472. float col_d=1e20;
  473. for(int i=0;i<3;i++) {
  474. Plane plane(gt.origin,gt.basis.get_axis(i).normalized());
  475. Vector3 r;
  476. if (!plane.intersects_ray(ray_pos,ray,&r))
  477. continue;
  478. float dist = r.distance_to(gt.origin);
  479. if (dist > gs*(1-GIZMO_RING_HALF_WIDTH) && dist < gs *(1+GIZMO_RING_HALF_WIDTH)) {
  480. float d = ray_pos.distance_to(r);
  481. if (d<col_d) {
  482. col_d=d;
  483. col_axis=i;
  484. }
  485. }
  486. }
  487. if (col_axis!=-1) {
  488. if (p_hilite_only) {
  489. spatial_editor->select_gizmo_hilight_axis(col_axis+3);
  490. } else {
  491. //handle rotate
  492. _edit.mode=TRANSFORM_ROTATE;
  493. _compute_edit(Point2(p_screenpos.x,p_screenpos.y));
  494. _edit.plane=TransformPlane(TRANSFORM_X_AXIS+col_axis);
  495. }
  496. return true;
  497. }
  498. }
  499. if (p_hilite_only)
  500. spatial_editor->select_gizmo_hilight_axis(-1);
  501. return false;
  502. }
  503. void SpatialEditorViewport::_smouseenter() {
  504. if (!surface->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field()))
  505. surface->grab_focus();
  506. }
  507. void SpatialEditorViewport::_list_select(InputEventMouseButton b) {
  508. _find_items_at_pos(Vector2( b.x, b.y ),clicked_includes_current,selection_results,b.mod.shift);
  509. Node *scene=editor->get_edited_scene();
  510. for(int i=0;i<selection_results.size();i++) {
  511. Spatial *item=selection_results[i].item;
  512. if (item!=scene && item->get_owner()!=scene && !scene->is_editable_instance(item->get_owner())) {
  513. //invalid result
  514. selection_results.remove(i);
  515. i--;
  516. }
  517. }
  518. clicked_wants_append=b.mod.shift;
  519. if (selection_results.size() == 1) {
  520. clicked=selection_results[0].item->get_instance_ID();
  521. selection_results.clear();
  522. if (clicked) {
  523. _select_clicked(clicked_wants_append,true);
  524. clicked=0;
  525. }
  526. } else if (!selection_results.empty()) {
  527. NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
  528. StringName root_name = root_path.get_name(root_path.get_name_count()-1);
  529. for (int i = 0; i < selection_results.size(); i++) {
  530. Spatial *spat=selection_results[i].item;
  531. Ref<Texture> icon;
  532. if (spat->has_meta("_editor_icon"))
  533. icon=spat->get_meta("_editor_icon");
  534. else
  535. icon=get_icon( has_icon(spat->get_type(),"EditorIcons")?spat->get_type():String("Object"),"EditorIcons");
  536. String node_path="/"+root_name+"/"+root_path.rel_path_to(spat->get_path());
  537. selection_menu->add_item(spat->get_name());
  538. selection_menu->set_item_icon(i, icon );
  539. selection_menu->set_item_metadata(i, node_path);
  540. selection_menu->set_item_tooltip(i,String(spat->get_name())+
  541. "\nType: "+spat->get_type()+"\nPath: "+node_path);
  542. }
  543. selection_menu->set_global_pos(Vector2( b.global_x, b.global_y ));
  544. selection_menu->popup();
  545. selection_menu->call_deferred("grab_click_focus");
  546. selection_menu->set_invalidate_click_until_motion();
  547. }
  548. }
  549. void SpatialEditorViewport::_sinput(const InputEvent &p_event) {
  550. if (previewing)
  551. return; //do NONE
  552. {
  553. EditorNode *en = editor;
  554. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  555. if (!over_plugin_list->empty()) {
  556. bool discard = over_plugin_list->forward_spatial_input_event(camera,p_event);
  557. if (discard)
  558. return;
  559. }
  560. }
  561. switch(p_event.type) {
  562. case InputEvent::MOUSE_BUTTON: {
  563. const InputEventMouseButton &b=p_event.mouse_button;
  564. switch(b.button_index) {
  565. case BUTTON_WHEEL_UP: {
  566. cursor.distance/=1.08;
  567. if (cursor.distance<0.001)
  568. cursor.distance=0.001;
  569. } break;
  570. case BUTTON_WHEEL_DOWN: {
  571. if (cursor.distance<0.001)
  572. cursor.distance=0.001;
  573. cursor.distance*=1.08;
  574. } break;
  575. case BUTTON_RIGHT: {
  576. NavigationScheme nav_scheme = _get_navigation_schema("3d_editor/navigation_scheme");
  577. if (b.pressed && _edit.gizmo.is_valid()) {
  578. //restore
  579. _edit.gizmo->commit_handle(_edit.gizmo_handle,_edit.gizmo_initial_value,true);
  580. _edit.gizmo=Ref<SpatialEditorGizmo>();
  581. }
  582. if (_edit.mode==TRANSFORM_NONE && b.pressed) {
  583. Plane cursor_plane(cursor.cursor_pos,_get_camera_normal());
  584. Vector3 ray_origin = _get_ray_pos(Vector2(b.x,b.y));
  585. Vector3 ray_dir = _get_ray(Vector2(b.x,b.y));
  586. //gizmo modify
  587. if (b.mod.control) {
  588. Vector<ObjectID> instances=VisualServer::get_singleton()->instances_cull_ray(ray_origin,ray_dir,get_tree()->get_root()->get_world()->get_scenario() );
  589. Plane p(ray_origin,_get_camera_normal());
  590. float min_d=1e10;
  591. bool found=false;
  592. for (int i=0;i<instances.size();i++) {
  593. Object *obj=ObjectDB::get_instance(instances[i]);
  594. if (!obj)
  595. continue;
  596. VisualInstance *vi=obj->cast_to<VisualInstance>();
  597. if (!vi)
  598. continue;
  599. //optimize by checking AABB (although should pre sort by distance)
  600. AABB aabb = vi->get_global_transform().xform(vi->get_aabb());
  601. if (p.distance_to(aabb.get_support(-ray_dir))>min_d)
  602. continue;
  603. DVector<Face3> faces = vi->get_faces(VisualInstance::FACES_SOLID);
  604. int c = faces.size();
  605. if (c>0) {
  606. DVector<Face3>::Read r = faces.read();
  607. for(int j=0;j<c;j++) {
  608. Vector3 inters;
  609. if (r[j].intersects_ray(ray_origin,ray_dir,&inters)) {
  610. float d = p.distance_to(inters);
  611. if (d<0)
  612. continue;
  613. if (d<min_d) {
  614. min_d=d;
  615. found=true;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. if (found) {
  622. //cursor.cursor_pos=ray_origin+ray_dir*min_d;
  623. //VisualServer::get_singleton()->instance_set_transform(cursor_instance,Transform(Matrix3(),cursor.cursor_pos));
  624. }
  625. } else {
  626. Vector3 new_pos;
  627. if (cursor_plane.intersects_ray(ray_origin,ray_dir,&new_pos)) {
  628. //cursor.cursor_pos=new_pos;
  629. //VisualServer::get_singleton()->instance_set_transform(cursor_instance,Transform(Matrix3(),cursor.cursor_pos));
  630. }
  631. }
  632. if (b.mod.alt) {
  633. if (nav_scheme == NAVIGATION_MAYA)
  634. break;
  635. _list_select(b);
  636. return;
  637. }
  638. }
  639. if (_edit.mode!=TRANSFORM_NONE && b.pressed) {
  640. //cancel motion
  641. _edit.mode=TRANSFORM_NONE;
  642. //_validate_selection();
  643. List<Node*> &selection = editor_selection->get_selected_node_list();
  644. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  645. Spatial *sp = E->get()->cast_to<Spatial>();
  646. if (!sp)
  647. continue;
  648. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  649. if (!se)
  650. continue;
  651. sp->set_global_transform( se->original );
  652. }
  653. surface->update();
  654. //VisualServer::get_singleton()->poly_clear(indicators);
  655. set_message(TTR("Transform Aborted."),3);
  656. }
  657. } break;
  658. case BUTTON_MIDDLE: {
  659. if (b.pressed && _edit.mode!=TRANSFORM_NONE) {
  660. switch(_edit.plane ) {
  661. case TRANSFORM_VIEW: {
  662. _edit.plane=TRANSFORM_X_AXIS;
  663. set_message(TTR("X-Axis Transform."),2);
  664. name="";
  665. _update_name();
  666. } break;
  667. case TRANSFORM_X_AXIS: {
  668. _edit.plane=TRANSFORM_Y_AXIS;
  669. set_message(TTR("Y-Axis Transform."),2);
  670. } break;
  671. case TRANSFORM_Y_AXIS: {
  672. _edit.plane=TRANSFORM_Z_AXIS;
  673. set_message(TTR("Z-Axis Transform."),2);
  674. } break;
  675. case TRANSFORM_Z_AXIS: {
  676. _edit.plane=TRANSFORM_VIEW;
  677. set_message(TTR("View Plane Transform."),2);
  678. } break;
  679. }
  680. }
  681. } break;
  682. case BUTTON_LEFT: {
  683. if (b.pressed) {
  684. NavigationScheme nav_scheme = _get_navigation_schema("3d_editor/navigation_scheme");
  685. if ( (nav_scheme==NAVIGATION_MAYA || nav_scheme==NAVIGATION_MODO) && b.mod.alt) {
  686. break;
  687. }
  688. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_LIST_SELECT) {
  689. _list_select(b);
  690. break;
  691. }
  692. _edit.mouse_pos=Point2(b.x,b.y);
  693. _edit.snap=false;
  694. _edit.mode=TRANSFORM_NONE;
  695. //gizmo has priority over everything
  696. bool can_select_gizmos=true;
  697. {
  698. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  699. can_select_gizmos = view_menu->get_popup()->is_item_checked( idx );
  700. }
  701. if (can_select_gizmos && spatial_editor->get_selected()) {
  702. Ref<SpatialEditorGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  703. if (seg.is_valid()) {
  704. int handle=-1;
  705. Vector3 point;
  706. Vector3 normal;
  707. bool inters = seg->intersect_ray(camera,_edit.mouse_pos,point,normal,&handle,b.mod.shift);
  708. if (inters && handle!=-1) {
  709. _edit.gizmo=seg;
  710. _edit.gizmo_handle=handle;
  711. //_edit.gizmo_initial_pos=seg->get_handle_pos(gizmo_handle);
  712. _edit.gizmo_initial_value=seg->get_handle_value(handle);
  713. break;
  714. }
  715. }
  716. }
  717. if (_gizmo_select(_edit.mouse_pos))
  718. break;
  719. clicked=0;
  720. clicked_includes_current=false;
  721. if ((spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT && b.mod.control) || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_ROTATE) {
  722. /* HANDLE ROTATION */
  723. if (get_selected_count()==0)
  724. break; //bye
  725. //handle rotate
  726. _edit.mode=TRANSFORM_ROTATE;
  727. _compute_edit(Point2(b.x,b.y));
  728. break;
  729. }
  730. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_MOVE) {
  731. if (get_selected_count()==0)
  732. break; //bye
  733. //handle rotate
  734. _edit.mode=TRANSFORM_TRANSLATE;
  735. _compute_edit(Point2(b.x,b.y));
  736. break;
  737. }
  738. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SCALE) {
  739. if (get_selected_count()==0)
  740. break; //bye
  741. //handle rotate
  742. _edit.mode=TRANSFORM_SCALE;
  743. _compute_edit(Point2(b.x,b.y));
  744. break;
  745. }
  746. // todo scale
  747. int gizmo_handle=-1;
  748. clicked=_select_ray(Vector2( b.x, b.y ),b.mod.shift,clicked_includes_current,&gizmo_handle,b.mod.shift);
  749. //clicking is always deferred to either move or release
  750. clicked_wants_append=b.mod.shift;
  751. if (!clicked) {
  752. if (!clicked_wants_append)
  753. _clear_selected();
  754. //default to regionselect
  755. cursor.region_select=true;
  756. cursor.region_begin=Point2(b.x,b.y);
  757. cursor.region_end=Point2(b.x,b.y);
  758. }
  759. if (clicked && gizmo_handle>=0) {
  760. Object *obj=ObjectDB::get_instance(clicked);
  761. if (obj) {
  762. Spatial *spa = obj->cast_to<Spatial>();
  763. if (spa) {
  764. Ref<SpatialEditorGizmo> seg=spa->get_gizmo();
  765. if (seg.is_valid()) {
  766. _edit.gizmo=seg;
  767. _edit.gizmo_handle=gizmo_handle;
  768. //_edit.gizmo_initial_pos=seg->get_handle_pos(gizmo_handle);
  769. _edit.gizmo_initial_value=seg->get_handle_value(gizmo_handle);
  770. //print_line("GIZMO: "+itos(gizmo_handle)+" FROMPOS: "+_edit.orig_gizmo_pos);
  771. break;
  772. }
  773. }
  774. }
  775. //_compute_edit(Point2(b.x,b.y)); //in case a motion happens..
  776. }
  777. surface->update();
  778. } else {
  779. if (_edit.gizmo.is_valid()) {
  780. _edit.gizmo->commit_handle(_edit.gizmo_handle,_edit.gizmo_initial_value,false);
  781. _edit.gizmo=Ref<SpatialEditorGizmo>();
  782. break;
  783. }
  784. if (clicked) {
  785. _select_clicked(clicked_wants_append,true);
  786. //clickd processing was deferred
  787. clicked=0;
  788. }
  789. if (cursor.region_select) {
  790. _select_region();
  791. cursor.region_select=false;
  792. surface->update();
  793. }
  794. if (_edit.mode!=TRANSFORM_NONE) {
  795. static const char* _transform_name[4]={"None","Rotate","Translate","Scale"};
  796. undo_redo->create_action(_transform_name[_edit.mode]);
  797. List<Node*> &selection = editor_selection->get_selected_node_list();
  798. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  799. Spatial *sp = E->get()->cast_to<Spatial>();
  800. if (!sp)
  801. continue;
  802. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  803. if (!se)
  804. continue;
  805. undo_redo->add_do_method(sp,"set_global_transform",sp->get_global_transform());
  806. undo_redo->add_undo_method(sp,"set_global_transform",se->original);
  807. }
  808. undo_redo->commit_action();
  809. _edit.mode=TRANSFORM_NONE;
  810. //VisualServer::get_singleton()->poly_clear(indicators);
  811. set_message("");
  812. }
  813. surface->update();
  814. }
  815. } break;
  816. }
  817. } break;
  818. case InputEvent::MOUSE_MOTION: {
  819. const InputEventMouseMotion &m=p_event.mouse_motion;
  820. _edit.mouse_pos=Point2(p_event.mouse_motion.x,p_event.mouse_motion.y);
  821. if (spatial_editor->get_selected()) {
  822. Ref<SpatialEditorGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  823. if (seg.is_valid()) {
  824. int selected_handle=-1;
  825. int handle=-1;
  826. Vector3 point;
  827. Vector3 normal;
  828. bool inters = seg->intersect_ray(camera,_edit.mouse_pos,point,normal,&handle,false);
  829. if (inters && handle!=-1) {
  830. selected_handle=handle;
  831. }
  832. if (selected_handle!=spatial_editor->get_over_gizmo_handle()) {
  833. spatial_editor->set_over_gizmo_handle(selected_handle);
  834. spatial_editor->get_selected()->update_gizmo();
  835. if (selected_handle!=-1)
  836. spatial_editor->select_gizmo_hilight_axis(-1);
  837. }
  838. }
  839. }
  840. if (spatial_editor->get_over_gizmo_handle()==-1 && !(m.button_mask&1) && !_edit.gizmo.is_valid()) {
  841. _gizmo_select(_edit.mouse_pos,true);
  842. }
  843. NavigationScheme nav_scheme = _get_navigation_schema("3d_editor/navigation_scheme");
  844. NavigationMode nav_mode = NAVIGATION_NONE;
  845. if (_edit.gizmo.is_valid()) {
  846. _edit.gizmo->set_handle(_edit.gizmo_handle,camera,Vector2(m.x,m.y));
  847. Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle);
  848. String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle);
  849. set_message(n+": "+String(v));
  850. } else if (m.button_mask&1) {
  851. if (nav_scheme == NAVIGATION_MAYA && m.mod.alt) {
  852. nav_mode = NAVIGATION_ORBIT;
  853. } else if (nav_scheme == NAVIGATION_MODO && m.mod.alt && m.mod.shift) {
  854. nav_mode = NAVIGATION_PAN;
  855. } else if (nav_scheme == NAVIGATION_MODO && m.mod.alt && m.mod.control) {
  856. nav_mode = NAVIGATION_ZOOM;
  857. } else if (nav_scheme == NAVIGATION_MODO && m.mod.alt) {
  858. nav_mode = NAVIGATION_ORBIT;
  859. } else {
  860. if (clicked) {
  861. if (!clicked_includes_current) {
  862. _select_clicked(clicked_wants_append,true);
  863. //clickd processing was deferred
  864. }
  865. _compute_edit(_edit.mouse_pos);
  866. clicked=0;
  867. _edit.mode=TRANSFORM_TRANSLATE;
  868. }
  869. if (cursor.region_select && nav_mode == NAVIGATION_NONE) {
  870. cursor.region_end=Point2(m.x,m.y);
  871. surface->update();
  872. return;
  873. }
  874. if (_edit.mode==TRANSFORM_NONE && nav_mode == NAVIGATION_NONE)
  875. break;
  876. Vector3 ray_pos=_get_ray_pos( Vector2( m.x, m.y ) );
  877. Vector3 ray=_get_ray( Vector2( m.x, m.y ) );
  878. switch(_edit.mode) {
  879. case TRANSFORM_SCALE: {
  880. Plane plane=Plane(_edit.center,_get_camera_normal());
  881. Vector3 intersection;
  882. if (!plane.intersects_ray(ray_pos,ray,&intersection))
  883. break;
  884. Vector3 click;
  885. if (!plane.intersects_ray(_edit.click_ray_pos,_edit.click_ray,&click))
  886. break;
  887. float center_click_dist = click.distance_to(_edit.center);
  888. float center_inters_dist = intersection.distance_to(_edit.center);
  889. if (center_click_dist==0)
  890. break;
  891. float scale = (center_inters_dist / center_click_dist)*100.0;
  892. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  893. scale = Math::stepify(scale,spatial_editor->get_scale_snap());
  894. }
  895. set_message(vformat(TTR("Scaling to %s%%."),String::num(scale,1)));
  896. scale/=100.0;
  897. Transform r;
  898. r.basis.scale(Vector3(scale,scale,scale));
  899. List<Node*> &selection = editor_selection->get_selected_node_list();
  900. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  901. Spatial *sp = E->get()->cast_to<Spatial>();
  902. if (!sp)
  903. continue;
  904. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  905. if (!se)
  906. continue;
  907. Transform original=se->original;
  908. Transform base=Transform( Matrix3(), _edit.center);
  909. Transform t=base * (r * (base.inverse() * original));
  910. sp->set_global_transform(t);
  911. }
  912. surface->update();
  913. } break;
  914. case TRANSFORM_TRANSLATE: {
  915. Vector3 motion_mask;
  916. Plane plane;
  917. switch( _edit.plane ) {
  918. case TRANSFORM_VIEW:
  919. motion_mask=Vector3(0,0,0);
  920. plane=Plane(_edit.center,_get_camera_normal());
  921. break;
  922. case TRANSFORM_X_AXIS:
  923. motion_mask=spatial_editor->get_gizmo_transform().basis.get_axis(0);
  924. plane=Plane(_edit.center,motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  925. break;
  926. case TRANSFORM_Y_AXIS:
  927. motion_mask=spatial_editor->get_gizmo_transform().basis.get_axis(1);
  928. plane=Plane(_edit.center,motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  929. break;
  930. case TRANSFORM_Z_AXIS:
  931. motion_mask=spatial_editor->get_gizmo_transform().basis.get_axis(2);
  932. plane=Plane(_edit.center,motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  933. break;
  934. }
  935. Vector3 intersection;
  936. if (!plane.intersects_ray(ray_pos,ray,&intersection))
  937. break;
  938. Vector3 click;
  939. if (!plane.intersects_ray(_edit.click_ray_pos,_edit.click_ray,&click))
  940. break;
  941. //_validate_selection();
  942. Vector3 motion = intersection-click;
  943. if (motion_mask!=Vector3()) {
  944. motion=motion_mask.dot(motion)*motion_mask;
  945. }
  946. float snap=0;
  947. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  948. snap = spatial_editor->get_translate_snap();
  949. motion.snap(snap);
  950. }
  951. //set_message("Translating: "+motion);
  952. List<Node*> &selection = editor_selection->get_selected_node_list();
  953. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  954. Spatial *sp = E->get()->cast_to<Spatial>();
  955. if (!sp) {
  956. continue;
  957. }
  958. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  959. if (!se) {
  960. continue;
  961. }
  962. Transform t=se->original;
  963. t.origin+=motion;
  964. sp->set_global_transform(t);
  965. }
  966. } break;
  967. case TRANSFORM_ROTATE: {
  968. Plane plane;
  969. switch( _edit.plane ) {
  970. case TRANSFORM_VIEW:
  971. plane=Plane(_edit.center,_get_camera_normal());
  972. break;
  973. case TRANSFORM_X_AXIS:
  974. plane=Plane(_edit.center,spatial_editor->get_gizmo_transform().basis.get_axis(0));
  975. break;
  976. case TRANSFORM_Y_AXIS:
  977. plane=Plane(_edit.center,spatial_editor->get_gizmo_transform().basis.get_axis(1));
  978. break;
  979. case TRANSFORM_Z_AXIS:
  980. plane=Plane(_edit.center,spatial_editor->get_gizmo_transform().basis.get_axis(2));
  981. break;
  982. }
  983. Vector3 intersection;
  984. if (!plane.intersects_ray(ray_pos,ray,&intersection))
  985. break;
  986. Vector3 click;
  987. if (!plane.intersects_ray(_edit.click_ray_pos,_edit.click_ray,&click))
  988. break;
  989. Vector3 y_axis=(click-_edit.center).normalized();
  990. Vector3 x_axis=plane.normal.cross(y_axis).normalized();
  991. float angle=Math::atan2( x_axis.dot(intersection-_edit.center), y_axis.dot(intersection-_edit.center) );
  992. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  993. float snap = spatial_editor->get_rotate_snap();
  994. if (snap) {
  995. angle=Math::rad2deg(angle)+snap*0.5; //else it wont reach +180
  996. angle-=Math::fmod(angle,snap);
  997. set_message(vformat(TTR("Rotating %s degrees."),rtos(angle)));
  998. angle=Math::deg2rad(angle);
  999. } else
  1000. set_message(vformat(TTR("Rotating %s degrees."),rtos(Math::rad2deg(angle))));
  1001. } else {
  1002. set_message(vformat(TTR("Rotating %s degrees."),rtos(Math::rad2deg(angle))));
  1003. }
  1004. Transform r;
  1005. r.basis.rotate(plane.normal,-angle);
  1006. List<Node*> &selection = editor_selection->get_selected_node_list();
  1007. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1008. Spatial *sp = E->get()->cast_to<Spatial>();
  1009. if (!sp)
  1010. continue;
  1011. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1012. if (!se)
  1013. continue;
  1014. Transform original=se->original;
  1015. Transform base=Transform( Matrix3(), _edit.center);
  1016. Transform t=base * (r * (base.inverse() * original));
  1017. sp->set_global_transform(t);
  1018. }
  1019. surface->update();
  1020. /*
  1021. VisualServer::get_singleton()->poly_clear(indicators);
  1022. Vector<Vector3> points;
  1023. Vector<Vector3> empty;
  1024. Vector<Color> colors;
  1025. points.push_back(intersection);
  1026. points.push_back(_edit.original.origin);
  1027. colors.push_back( Color(255,155,100) );
  1028. colors.push_back( Color(255,155,100) );
  1029. VisualServer::get_singleton()->poly_add_primitive(indicators,points,empty,colors,empty);
  1030. */
  1031. } break;
  1032. default:{}
  1033. }
  1034. }
  1035. } else if (m.button_mask&2) {
  1036. if (nav_scheme == NAVIGATION_MAYA && m.mod.alt) {
  1037. nav_mode = NAVIGATION_ZOOM;
  1038. }
  1039. } else if (m.button_mask&4) {
  1040. if (nav_scheme == NAVIGATION_GODOT) {
  1041. int mod = 0;
  1042. if (m.mod.shift)
  1043. mod=KEY_SHIFT;
  1044. if (m.mod.alt)
  1045. mod=KEY_ALT;
  1046. if (m.mod.control)
  1047. mod=KEY_CONTROL;
  1048. if (m.mod.meta)
  1049. mod=KEY_META;
  1050. if (mod == _get_key_modifier("3d_editor/pan_modifier"))
  1051. nav_mode = NAVIGATION_PAN;
  1052. else if (mod == _get_key_modifier("3d_editor/zoom_modifier"))
  1053. nav_mode = NAVIGATION_ZOOM;
  1054. else if (mod == _get_key_modifier("3d_editor/orbit_modifier"))
  1055. nav_mode = NAVIGATION_ORBIT;
  1056. } else if (nav_scheme == NAVIGATION_MAYA) {
  1057. if (m.mod.alt)
  1058. nav_mode = NAVIGATION_PAN;
  1059. }
  1060. } else if (EditorSettings::get_singleton()->get("3d_editor/emulate_3_button_mouse")) {
  1061. // Handle trackpad (no external mouse) use case
  1062. int mod = 0;
  1063. if (m.mod.shift)
  1064. mod=KEY_SHIFT;
  1065. if (m.mod.alt)
  1066. mod=KEY_ALT;
  1067. if (m.mod.control)
  1068. mod=KEY_CONTROL;
  1069. if (m.mod.meta)
  1070. mod=KEY_META;
  1071. if(mod){
  1072. if (mod == _get_key_modifier("3d_editor/pan_modifier"))
  1073. nav_mode = NAVIGATION_PAN;
  1074. else if (mod == _get_key_modifier("3d_editor/zoom_modifier"))
  1075. nav_mode = NAVIGATION_ZOOM;
  1076. else if (mod == _get_key_modifier("3d_editor/orbit_modifier"))
  1077. nav_mode = NAVIGATION_ORBIT;
  1078. }
  1079. }
  1080. switch(nav_mode) {
  1081. case NAVIGATION_PAN:{
  1082. real_t pan_speed = 1/150.0;
  1083. int pan_speed_modifier = 10;
  1084. if (nav_scheme==NAVIGATION_MAYA && m.mod.shift)
  1085. pan_speed *= pan_speed_modifier;
  1086. Transform camera_transform;
  1087. camera_transform.translate(cursor.pos);
  1088. camera_transform.basis.rotate(Vector3(0,1,0),cursor.y_rot);
  1089. camera_transform.basis.rotate(Vector3(1,0,0),cursor.x_rot);
  1090. Vector3 translation(-m.relative_x*pan_speed,m.relative_y*pan_speed,0);
  1091. translation*=cursor.distance/DISTANCE_DEFAULT;
  1092. camera_transform.translate(translation);
  1093. cursor.pos=camera_transform.origin;
  1094. } break;
  1095. case NAVIGATION_ZOOM: {
  1096. real_t zoom_speed = 1/80.0;
  1097. int zoom_speed_modifier = 10;
  1098. if (nav_scheme==NAVIGATION_MAYA && m.mod.shift)
  1099. zoom_speed *= zoom_speed_modifier;
  1100. NavigationZoomStyle zoom_style = _get_navigation_zoom_style("3d_editor/zoom_style");
  1101. if (zoom_style == NAVIGATION_ZOOM_HORIZONTAL) {
  1102. if ( m.relative_x > 0)
  1103. cursor.distance*=1-m.relative_x*zoom_speed;
  1104. else if (m.relative_x < 0)
  1105. cursor.distance/=1+m.relative_x*zoom_speed;
  1106. }
  1107. else {
  1108. if ( m.relative_y > 0)
  1109. cursor.distance*=1+m.relative_y*zoom_speed;
  1110. else if (m.relative_y < 0)
  1111. cursor.distance/=1-m.relative_y*zoom_speed;
  1112. }
  1113. } break;
  1114. case NAVIGATION_ORBIT: {
  1115. cursor.x_rot+=m.relative_y/80.0;
  1116. cursor.y_rot+=m.relative_x/80.0;
  1117. if (cursor.x_rot>Math_PI/2.0)
  1118. cursor.x_rot=Math_PI/2.0;
  1119. if (cursor.x_rot<-Math_PI/2.0)
  1120. cursor.x_rot=-Math_PI/2.0;
  1121. name="";
  1122. _update_name();
  1123. } break;
  1124. default: {}
  1125. }
  1126. } break;
  1127. case InputEvent::KEY: {
  1128. const InputEventKey &k = p_event.key;
  1129. if (!k.pressed)
  1130. break;
  1131. if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  1132. if (_edit.mode != TRANSFORM_NONE) {
  1133. _edit.snap=true;
  1134. }
  1135. }
  1136. if (ED_IS_SHORTCUT("spatial_editor/bottom_view", p_event)) {
  1137. cursor.y_rot = 0;
  1138. cursor.x_rot = -Math_PI/2.0;
  1139. set_message(TTR("Bottom View."),2);
  1140. name = TTR("Bottom");
  1141. _update_name();
  1142. }
  1143. if (ED_IS_SHORTCUT("spatial_editor/top_view", p_event)) {
  1144. cursor.y_rot = 0;
  1145. cursor.x_rot = Math_PI/2.0;
  1146. set_message(TTR("Top View."),2);
  1147. name = TTR("Top");
  1148. _update_name();
  1149. }
  1150. if (ED_IS_SHORTCUT("spatial_editor/rear_view", p_event)) {
  1151. cursor.x_rot = 0;
  1152. cursor.y_rot = Math_PI;
  1153. set_message(TTR("Rear View."),2);
  1154. name = TTR("Rear");
  1155. _update_name();
  1156. }
  1157. if (ED_IS_SHORTCUT("spatial_editor/front_view", p_event)) {
  1158. cursor.x_rot = 0;
  1159. cursor.y_rot=0;
  1160. set_message(TTR("Front View."),2);
  1161. name=TTR("Front");
  1162. _update_name();
  1163. }
  1164. if (ED_IS_SHORTCUT("spatial_editor/left_view", p_event)) {
  1165. cursor.x_rot=0;
  1166. cursor.y_rot = Math_PI/2.0;
  1167. set_message(TTR("Left View."),2);
  1168. name = TTR("Left");
  1169. _update_name();
  1170. }
  1171. if (ED_IS_SHORTCUT("spatial_editor/right_view", p_event)) {
  1172. cursor.x_rot=0;
  1173. cursor.y_rot = -Math_PI/2.0;
  1174. set_message(TTR("Right View."),2);
  1175. name = TTR("Right");
  1176. _update_name();
  1177. }
  1178. if (ED_IS_SHORTCUT("spatial_editor/switch_perspective_orthogonal", p_event)) {
  1179. _menu_option(orthogonal?VIEW_PERSPECTIVE:VIEW_ORTHOGONAL);
  1180. _update_name();
  1181. }
  1182. if (ED_IS_SHORTCUT("spatial_editor/insert_anim_key", p_event)) {
  1183. if (!get_selected_count() || _edit.mode!=TRANSFORM_NONE)
  1184. break;
  1185. if (!AnimationPlayerEditor::singleton->get_key_editor()->has_keying()) {
  1186. set_message(TTR("Keying is disabled (no key inserted)."));
  1187. break;
  1188. }
  1189. List<Node*> &selection = editor_selection->get_selected_node_list();
  1190. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1191. Spatial *sp = E->get()->cast_to<Spatial>();
  1192. if (!sp)
  1193. continue;
  1194. emit_signal("transform_key_request",sp,"",sp->get_transform());
  1195. }
  1196. set_message(TTR("Animation Key Inserted."));
  1197. }
  1198. if (k.scancode == KEY_SPACE) {
  1199. if (!k.pressed) emit_signal("toggle_maximize_view", this);
  1200. }
  1201. } break;
  1202. }
  1203. }
  1204. void SpatialEditorViewport::set_message(String p_message,float p_time) {
  1205. message=p_message;
  1206. message_time=p_time;
  1207. }
  1208. void SpatialEditorViewport::_notification(int p_what) {
  1209. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  1210. bool visible=is_visible();
  1211. set_process(visible);
  1212. if (visible)
  1213. _update_camera();
  1214. call_deferred("update_transform_gizmo_view");
  1215. }
  1216. if (p_what==NOTIFICATION_RESIZED) {
  1217. call_deferred("update_transform_gizmo_view");
  1218. }
  1219. if (p_what==NOTIFICATION_PROCESS) {
  1220. //force editr camera
  1221. /*
  1222. current_camera=get_root_node()->get_current_camera();
  1223. if (current_camera!=camera) {
  1224. }
  1225. */
  1226. _update_camera();
  1227. Map<Node*,Object*> &selection = editor_selection->get_selection();
  1228. bool changed=false;
  1229. bool exist=false;
  1230. for(Map<Node*,Object*>::Element *E=selection.front();E;E=E->next()) {
  1231. Spatial *sp = E->key()->cast_to<Spatial>();
  1232. if (!sp)
  1233. continue;
  1234. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1235. if (!se)
  1236. continue;
  1237. VisualInstance *vi=sp->cast_to<VisualInstance>();
  1238. if (se->aabb.has_no_surface()) {
  1239. se->aabb=vi?vi->get_aabb():AABB( Vector3(-0.2,-0.2,-0.2),Vector3(0.4,0.4,0.4));
  1240. }
  1241. Transform t=sp->get_global_transform();
  1242. t.translate(se->aabb.pos);
  1243. t.basis.scale( se->aabb.size );
  1244. exist=true;
  1245. if (se->last_xform==t)
  1246. continue;
  1247. changed=true;
  1248. se->last_xform=t;
  1249. VisualServer::get_singleton()->instance_set_transform(se->sbox_instance,t);
  1250. }
  1251. if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
  1252. spatial_editor->update_transform_gizmo();
  1253. }
  1254. if (message_time>0) {
  1255. if (message!=last_message) {
  1256. surface->update();
  1257. last_message=message;
  1258. }
  1259. message_time-=get_fixed_process_delta_time();
  1260. if (message_time<0)
  1261. surface->update();
  1262. }
  1263. }
  1264. if (p_what==NOTIFICATION_ENTER_TREE) {
  1265. surface->connect("draw",this,"_draw");
  1266. surface->connect("input_event",this,"_sinput");
  1267. surface->connect("mouse_enter",this,"_smouseenter");
  1268. preview_camera->set_icon(get_icon("Camera","EditorIcons"));
  1269. _init_gizmo_instance(index);
  1270. }
  1271. if (p_what==NOTIFICATION_EXIT_TREE) {
  1272. _finish_gizmo_instances();
  1273. }
  1274. if (p_what==NOTIFICATION_MOUSE_ENTER) {
  1275. }
  1276. if (p_what==NOTIFICATION_DRAW) {
  1277. }
  1278. }
  1279. void SpatialEditorViewport::_draw() {
  1280. if (surface->has_focus()) {
  1281. Size2 size = surface->get_size();
  1282. Rect2 r =Rect2(Point2(),size);
  1283. get_stylebox("EditorFocus","EditorStyles")->draw(surface->get_canvas_item(),r);
  1284. }
  1285. RID ci=surface->get_canvas_item();
  1286. if (cursor.region_select) {
  1287. 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));
  1288. }
  1289. if (message_time>0) {
  1290. Ref<Font> font = get_font("font","Label");
  1291. Point2 msgpos=Point2(5,get_size().y-20);
  1292. font->draw(ci,msgpos+Point2(1,1),message,Color(0,0,0,0.8));
  1293. font->draw(ci,msgpos+Point2(-1,-1),message,Color(0,0,0,0.8));
  1294. font->draw(ci,msgpos,message,Color(1,1,1,1));
  1295. }
  1296. if (_edit.mode==TRANSFORM_ROTATE) {
  1297. Point2 center = _point_to_screen(_edit.center);
  1298. VisualServer::get_singleton()->canvas_item_add_line(ci,_edit.mouse_pos, center, Color(0.4,0.7,1.0,0.8));
  1299. }
  1300. if (previewing) {
  1301. Size2 ss = Size2( Globals::get_singleton()->get("display/width"), Globals::get_singleton()->get("display/height") );
  1302. float aspect = ss.get_aspect();
  1303. Size2 s = get_size();
  1304. Rect2 draw_rect;
  1305. switch(previewing->get_keep_aspect_mode()) {
  1306. case Camera::KEEP_WIDTH: {
  1307. draw_rect.size = Size2(s.width,s.width/aspect);
  1308. draw_rect.pos.x=0;
  1309. draw_rect.pos.y=(s.height-draw_rect.size.y)*0.5;
  1310. } break;
  1311. case Camera::KEEP_HEIGHT: {
  1312. draw_rect.size = Size2(s.height*aspect,s.height);
  1313. draw_rect.pos.y=0;
  1314. draw_rect.pos.x=(s.width-draw_rect.size.x)*0.5;
  1315. } break;
  1316. }
  1317. draw_rect = Rect2(Vector2(),s).clip(draw_rect);
  1318. 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);
  1319. 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);
  1320. 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);
  1321. 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);
  1322. }
  1323. }
  1324. void SpatialEditorViewport::_menu_option(int p_option) {
  1325. switch(p_option) {
  1326. case VIEW_TOP: {
  1327. cursor.x_rot=Math_PI/2.0;
  1328. cursor.y_rot=0;
  1329. name=TTR("Top");
  1330. _update_name();
  1331. } break;
  1332. case VIEW_BOTTOM: {
  1333. cursor.x_rot=-Math_PI/2.0;
  1334. cursor.y_rot=0;
  1335. name=TTR("Bottom");
  1336. _update_name();
  1337. } break;
  1338. case VIEW_LEFT: {
  1339. cursor.y_rot=Math_PI/2.0;
  1340. cursor.x_rot=0;
  1341. name=TTR("Left");
  1342. _update_name();
  1343. } break;
  1344. case VIEW_RIGHT: {
  1345. cursor.y_rot=-Math_PI/2.0;
  1346. cursor.x_rot=0;
  1347. name=TTR("Right");
  1348. _update_name();
  1349. } break;
  1350. case VIEW_FRONT: {
  1351. cursor.y_rot=0;
  1352. cursor.x_rot=0;
  1353. name=TTR("Front");
  1354. _update_name();
  1355. } break;
  1356. case VIEW_REAR: {
  1357. cursor.y_rot=Math_PI;
  1358. cursor.x_rot=0;
  1359. name=TTR("Rear");
  1360. _update_name();
  1361. } break;
  1362. case VIEW_CENTER_TO_SELECTION: {
  1363. focus_selection();
  1364. } break;
  1365. case VIEW_ALIGN_SELECTION_WITH_VIEW: {
  1366. if (!get_selected_count())
  1367. break;
  1368. Transform camera_transform = camera->get_global_transform();
  1369. List<Node*> &selection = editor_selection->get_selected_node_list();
  1370. undo_redo->create_action(TTR("Align with view"));
  1371. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1372. Spatial *sp = E->get()->cast_to<Spatial>();
  1373. if (!sp)
  1374. continue;
  1375. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1376. if (!se)
  1377. continue;
  1378. Transform xform = camera_transform;
  1379. xform.scale_basis(sp->get_scale());
  1380. undo_redo->add_do_method(sp,"set_global_transform",xform);
  1381. undo_redo->add_undo_method(sp,"set_global_transform",sp->get_global_transform());
  1382. }
  1383. undo_redo->commit_action();
  1384. } break;
  1385. case VIEW_ENVIRONMENT: {
  1386. int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT);
  1387. bool current = view_menu->get_popup()->is_item_checked( idx );
  1388. current=!current;
  1389. if (current) {
  1390. camera->set_environment(RES());
  1391. } else {
  1392. camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  1393. }
  1394. view_menu->get_popup()->set_item_checked( idx, current );
  1395. } break;
  1396. case VIEW_PERSPECTIVE: {
  1397. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true );
  1398. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false );
  1399. orthogonal=false;
  1400. call_deferred("update_transform_gizmo_view");
  1401. _update_name();
  1402. } break;
  1403. case VIEW_ORTHOGONAL: {
  1404. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false );
  1405. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true );
  1406. orthogonal=true;
  1407. call_deferred("update_transform_gizmo_view");
  1408. _update_name();
  1409. } break;
  1410. case VIEW_AUDIO_LISTENER: {
  1411. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  1412. bool current = view_menu->get_popup()->is_item_checked( idx );
  1413. current=!current;
  1414. viewport->set_as_audio_listener(current);
  1415. view_menu->get_popup()->set_item_checked( idx, current );
  1416. } break;
  1417. case VIEW_GIZMOS: {
  1418. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  1419. bool current = view_menu->get_popup()->is_item_checked( idx );
  1420. current=!current;
  1421. if (current)
  1422. camera->set_cull_mask( ((1<<20)-1)|(1<<(GIZMO_BASE_LAYER+index))|(1<<GIZMO_EDIT_LAYER)|(1<<GIZMO_GRID_LAYER) );
  1423. else
  1424. camera->set_cull_mask( ((1<<20)-1)|(1<<(GIZMO_BASE_LAYER+index))|(1<<GIZMO_GRID_LAYER) );
  1425. view_menu->get_popup()->set_item_checked( idx, current );
  1426. } break;
  1427. }
  1428. }
  1429. void SpatialEditorViewport::_preview_exited_scene() {
  1430. preview_camera->set_pressed(false);
  1431. _toggle_camera_preview(false);
  1432. view_menu->show();
  1433. }
  1434. void SpatialEditorViewport::_init_gizmo_instance(int p_idx) {
  1435. uint32_t layer=1<<(GIZMO_BASE_LAYER+p_idx);//|(1<<GIZMO_GRID_LAYER);
  1436. for(int i=0;i<3;i++) {
  1437. move_gizmo_instance[i]=VS::get_singleton()->instance_create();
  1438. VS::get_singleton()->instance_set_base(move_gizmo_instance[i],spatial_editor->get_move_gizmo(i)->get_rid());
  1439. VS::get_singleton()->instance_set_scenario(move_gizmo_instance[i],get_tree()->get_root()->get_world()->get_scenario());
  1440. VS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  1441. //VS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i],VS::INSTANCE_FLAG_DEPH_SCALE,true);
  1442. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  1443. VS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i],layer);
  1444. rotate_gizmo_instance[i]=VS::get_singleton()->instance_create();
  1445. VS::get_singleton()->instance_set_base(rotate_gizmo_instance[i],spatial_editor->get_rotate_gizmo(i)->get_rid());
  1446. VS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i],get_tree()->get_root()->get_world()->get_scenario());
  1447. VS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  1448. //VS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i],VS::INSTANCE_FLAG_DEPH_SCALE,true);
  1449. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  1450. VS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i],layer);
  1451. }
  1452. }
  1453. void SpatialEditorViewport::_finish_gizmo_instances() {
  1454. for(int i=0;i<3;i++) {
  1455. VS::get_singleton()->free(move_gizmo_instance[i]);
  1456. VS::get_singleton()->free(rotate_gizmo_instance[i]);
  1457. }
  1458. }
  1459. void SpatialEditorViewport::_toggle_camera_preview(bool p_activate) {
  1460. ERR_FAIL_COND(p_activate && !preview);
  1461. ERR_FAIL_COND(!p_activate && !previewing);
  1462. if (!p_activate) {
  1463. previewing->disconnect("exit_tree",this,"_preview_exited_scene");
  1464. previewing=NULL;
  1465. VS::get_singleton()->viewport_attach_camera( viewport->get_viewport(), camera->get_camera() ); //restore
  1466. if (!preview)
  1467. preview_camera->hide();
  1468. view_menu->show();
  1469. surface->update();
  1470. } else {
  1471. previewing=preview;
  1472. previewing->connect("exit_tree",this,"_preview_exited_scene");
  1473. VS::get_singleton()->viewport_attach_camera( viewport->get_viewport(), preview->get_camera() ); //replace
  1474. view_menu->hide();
  1475. surface->update();
  1476. }
  1477. }
  1478. void SpatialEditorViewport::_selection_result_pressed(int p_result) {
  1479. if (selection_results.size() <= p_result)
  1480. return;
  1481. clicked=selection_results[p_result].item->get_instance_ID();
  1482. if (clicked) {
  1483. _select_clicked(clicked_wants_append,true);
  1484. clicked=0;
  1485. }
  1486. }
  1487. void SpatialEditorViewport::_selection_menu_hide() {
  1488. selection_results.clear();
  1489. selection_menu->clear();
  1490. selection_menu->set_size(Vector2(0, 0));
  1491. }
  1492. void SpatialEditorViewport::set_can_preview(Camera* p_preview) {
  1493. preview=p_preview;
  1494. if (!preview_camera->is_pressed()) {
  1495. if (p_preview) {
  1496. preview_camera->show();
  1497. } else {
  1498. preview_camera->hide();
  1499. }
  1500. }
  1501. }
  1502. void SpatialEditorViewport::update_transform_gizmo_view() {
  1503. if (!is_visible())
  1504. return;
  1505. Transform xform = spatial_editor->get_gizmo_transform();
  1506. Transform camera_xform = camera->get_transform();
  1507. Vector3 camz = -camera_xform.get_basis().get_axis(2).normalized();
  1508. Vector3 camy = -camera_xform.get_basis().get_axis(1).normalized();
  1509. Plane p(camera_xform.origin,camz);
  1510. float gizmo_d = Math::abs( p.distance_to(xform.origin ));
  1511. float d0 = camera->unproject_position(camera_xform.origin+camz*gizmo_d).y;
  1512. float d1 = camera->unproject_position(camera_xform.origin+camz*gizmo_d+camy).y;
  1513. float dd = Math::abs(d0-d1);
  1514. if (dd==0)
  1515. dd=0.0001;
  1516. float gsize = EditorSettings::get_singleton()->get("3d_editor/manipulator_gizmo_size");
  1517. gizmo_scale=(gsize/Math::abs(dd));
  1518. Vector3 scale = Vector3(1,1,1) * gizmo_scale;
  1519. xform.basis.scale(scale);
  1520. //xform.basis.scale(GIZMO_SCALE_DEFAULT*Vector3(1,1,1));
  1521. for(int i=0;i<3;i++) {
  1522. VisualServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], xform );
  1523. 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) );
  1524. VisualServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], xform );
  1525. 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) );
  1526. }
  1527. }
  1528. void SpatialEditorViewport::set_state(const Dictionary& p_state) {
  1529. cursor.pos=p_state["pos"];
  1530. cursor.x_rot=p_state["x_rot"];
  1531. cursor.y_rot=p_state["y_rot"];
  1532. cursor.distance=p_state["distance"];
  1533. bool env = p_state["use_environment"];
  1534. bool orth = p_state["use_orthogonal"];
  1535. if (orth)
  1536. _menu_option(VIEW_ORTHOGONAL);
  1537. else
  1538. _menu_option(VIEW_PERSPECTIVE);
  1539. if (env != camera->get_environment().is_valid())
  1540. _menu_option(VIEW_ENVIRONMENT);
  1541. if (p_state.has("listener")) {
  1542. bool listener = p_state["listener"];
  1543. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  1544. viewport->set_as_audio_listener(listener);
  1545. view_menu->get_popup()->set_item_checked( idx, listener );
  1546. }
  1547. if (p_state.has("previewing")) {
  1548. Node *pv = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["previewing"]);
  1549. if (pv && pv->cast_to<Camera>()) {
  1550. previewing=pv->cast_to<Camera>();
  1551. previewing->connect("exit_tree",this,"_preview_exited_scene");
  1552. VS::get_singleton()->viewport_attach_camera( viewport->get_viewport(), previewing->get_camera() ); //replace
  1553. view_menu->hide();
  1554. surface->update();
  1555. preview_camera->set_pressed(true);
  1556. preview_camera->show();
  1557. }
  1558. }
  1559. }
  1560. Dictionary SpatialEditorViewport::get_state() const {
  1561. Dictionary d;
  1562. d["pos"]=cursor.pos;
  1563. d["x_rot"]=cursor.x_rot;
  1564. d["y_rot"]=cursor.y_rot;
  1565. d["distance"]=cursor.distance;
  1566. d["use_environment"]=camera->get_environment().is_valid();
  1567. d["use_orthogonal"]=camera->get_projection()==Camera::PROJECTION_ORTHOGONAL;
  1568. d["listener"]=viewport->is_audio_listener();
  1569. if (previewing) {
  1570. d["previewing"]=EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing);
  1571. }
  1572. return d;
  1573. }
  1574. void SpatialEditorViewport::_bind_methods(){
  1575. ObjectTypeDB::bind_method(_MD("_draw"),&SpatialEditorViewport::_draw);
  1576. ObjectTypeDB::bind_method(_MD("_smouseenter"),&SpatialEditorViewport::_smouseenter);
  1577. ObjectTypeDB::bind_method(_MD("_sinput"),&SpatialEditorViewport::_sinput);
  1578. ObjectTypeDB::bind_method(_MD("_menu_option"),&SpatialEditorViewport::_menu_option);
  1579. ObjectTypeDB::bind_method(_MD("_toggle_camera_preview"),&SpatialEditorViewport::_toggle_camera_preview);
  1580. ObjectTypeDB::bind_method(_MD("_preview_exited_scene"),&SpatialEditorViewport::_preview_exited_scene);
  1581. ObjectTypeDB::bind_method(_MD("update_transform_gizmo_view"),&SpatialEditorViewport::update_transform_gizmo_view);
  1582. ObjectTypeDB::bind_method(_MD("_selection_result_pressed"),&SpatialEditorViewport::_selection_result_pressed);
  1583. ObjectTypeDB::bind_method(_MD("_selection_menu_hide"),&SpatialEditorViewport::_selection_menu_hide);
  1584. ADD_SIGNAL( MethodInfo("toggle_maximize_view", PropertyInfo(Variant::OBJECT, "viewport")) );
  1585. }
  1586. void SpatialEditorViewport::reset() {
  1587. orthogonal=false;
  1588. message_time=0;
  1589. message="";
  1590. last_message="";
  1591. name=TTR("Top");
  1592. cursor.x_rot=0;
  1593. cursor.y_rot=0;
  1594. cursor.distance=4;
  1595. cursor.region_select=false;
  1596. _update_name();
  1597. }
  1598. void SpatialEditorViewport::focus_selection() {
  1599. if (!get_selected_count())
  1600. return;
  1601. Vector3 center;
  1602. int count=0;
  1603. List<Node*> &selection = editor_selection->get_selected_node_list();
  1604. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1605. Spatial *sp = E->get()->cast_to<Spatial>();
  1606. if (!sp)
  1607. continue;
  1608. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1609. if (!se)
  1610. continue;
  1611. center+=sp->get_global_transform().origin;
  1612. count++;
  1613. }
  1614. if( count != 0 ) {
  1615. center/=float(count);
  1616. }
  1617. cursor.pos=center;
  1618. }
  1619. SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index) {
  1620. _edit.mode=TRANSFORM_NONE;
  1621. _edit.plane=TRANSFORM_VIEW;
  1622. _edit.edited_gizmo=0;
  1623. _edit.snap=1;
  1624. _edit.gizmo_handle=0;
  1625. index=p_index;
  1626. editor=p_editor;
  1627. editor_selection=editor->get_editor_selection();;
  1628. undo_redo=editor->get_undo_redo();
  1629. clicked=0;
  1630. clicked_includes_current=false;
  1631. orthogonal=false;
  1632. message_time=0;
  1633. spatial_editor=p_spatial_editor;
  1634. ViewportContainer *c=memnew(ViewportContainer);
  1635. add_child(c);
  1636. c->set_area_as_parent_rect();
  1637. viewport = memnew( Viewport );
  1638. viewport->set_disable_input(true);
  1639. c->add_child(viewport);
  1640. surface = memnew( Control );
  1641. add_child(surface);
  1642. surface->set_area_as_parent_rect();
  1643. camera = memnew(Camera);
  1644. camera->set_disable_gizmo(true);
  1645. camera->set_cull_mask( ((1<<20)-1)|(1<<(GIZMO_BASE_LAYER+p_index))|(1<<GIZMO_EDIT_LAYER)|(1<<GIZMO_GRID_LAYER) );
  1646. //camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  1647. viewport->add_child(camera);
  1648. camera->make_current();
  1649. surface->set_focus_mode(FOCUS_ALL);
  1650. view_menu = memnew( MenuButton );
  1651. surface->add_child(view_menu);
  1652. view_menu->set_pos( Point2(4,4));
  1653. view_menu->set_self_modulate(Color(1,1,1,0.5));
  1654. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/top_view"), VIEW_TOP);
  1655. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/bottom_view"), VIEW_BOTTOM);
  1656. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/left_view"), VIEW_LEFT);
  1657. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/right_view"), VIEW_RIGHT);
  1658. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/front_view"), VIEW_FRONT);
  1659. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/rear_view"), VIEW_REAR);
  1660. view_menu->get_popup()->add_separator();
  1661. view_menu->get_popup()->add_check_item(TTR("Perspective") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_PERSPECTIVE);
  1662. view_menu->get_popup()->add_check_item(TTR("Orthogonal") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_ORTHOGONAL);
  1663. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE),true);
  1664. view_menu->get_popup()->add_separator();
  1665. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("Environment")), VIEW_ENVIRONMENT);
  1666. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT),true);
  1667. view_menu->get_popup()->add_separator();
  1668. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener", TTR("Audio Listener")), VIEW_AUDIO_LISTENER);
  1669. view_menu->get_popup()->add_separator();
  1670. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("Gizmos")),VIEW_GIZMOS);
  1671. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_GIZMOS),true);
  1672. view_menu->get_popup()->add_separator();
  1673. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection"), VIEW_CENTER_TO_SELECTION);
  1674. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_selection_with_view"), VIEW_ALIGN_SELECTION_WITH_VIEW);
  1675. view_menu->get_popup()->connect("item_pressed",this,"_menu_option");
  1676. preview_camera = memnew( Button );
  1677. preview_camera->set_toggle_mode(true);
  1678. preview_camera->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,90);
  1679. preview_camera->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,10);
  1680. preview_camera->set_text("preview");
  1681. surface->add_child(preview_camera);
  1682. preview_camera->hide();
  1683. preview_camera->connect("toggled",this,"_toggle_camera_preview");
  1684. previewing=NULL;
  1685. preview=NULL;
  1686. gizmo_scale=1.0;
  1687. selection_menu = memnew( PopupMenu );
  1688. add_child(selection_menu);
  1689. selection_menu->set_custom_minimum_size(Vector2(100, 0));
  1690. selection_menu->connect("item_pressed", this, "_selection_result_pressed");
  1691. selection_menu->connect("popup_hide", this, "_selection_menu_hide");
  1692. if (p_index==0) {
  1693. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER),true);
  1694. viewport->set_as_audio_listener(true);
  1695. }
  1696. name=TTR("Top");
  1697. _update_name();
  1698. EditorSettings::get_singleton()->connect("settings_changed",this,"update_transform_gizmo_view");
  1699. }
  1700. SpatialEditor *SpatialEditor::singleton=NULL;
  1701. SpatialEditorSelectedItem::~SpatialEditorSelectedItem() {
  1702. if (sbox_instance.is_valid())
  1703. VisualServer::get_singleton()->free(sbox_instance);
  1704. }
  1705. void SpatialEditor::select_gizmo_hilight_axis(int p_axis) {
  1706. for(int i=0;i<3;i++) {
  1707. move_gizmo[i]->surface_set_material(0,i==p_axis?gizmo_hl:gizmo_color[i]);
  1708. rotate_gizmo[i]->surface_set_material(0,(i+3)==p_axis?gizmo_hl:gizmo_color[i]);
  1709. }
  1710. }
  1711. void SpatialEditor::update_transform_gizmo() {
  1712. List<Node*> &selection = editor_selection->get_selected_node_list();
  1713. AABB center;
  1714. bool first=true;
  1715. Matrix3 gizmo_basis;
  1716. bool local_gizmo_coords = transform_menu->get_popup()->is_item_checked( transform_menu->get_popup()->get_item_index(MENU_TRANSFORM_LOCAL_COORDS) );
  1717. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1718. Spatial *sp = E->get()->cast_to<Spatial>();
  1719. if (!sp)
  1720. continue;
  1721. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1722. if (!se)
  1723. continue;
  1724. Transform xf = se->sp->get_global_transform();
  1725. if (first) {
  1726. center.pos=xf.origin;
  1727. first=false;
  1728. if (local_gizmo_coords) {
  1729. gizmo_basis=xf.basis;
  1730. gizmo_basis.orthonormalize();
  1731. }
  1732. } else {
  1733. center.expand_to(xf.origin);
  1734. gizmo_basis=Matrix3();
  1735. }
  1736. // count++;
  1737. }
  1738. Vector3 pcenter = center.pos+center.size*0.5;
  1739. gizmo.visible=!first;
  1740. gizmo.transform.origin=pcenter;
  1741. gizmo.transform.basis=gizmo_basis;
  1742. for(int i=0;i<4;i++) {
  1743. viewports[i]->update_transform_gizmo_view();
  1744. }
  1745. }
  1746. Object *SpatialEditor::_get_editor_data(Object *p_what) {
  1747. Spatial *sp = p_what->cast_to<Spatial>();
  1748. if (!sp)
  1749. return NULL;
  1750. SpatialEditorSelectedItem *si = memnew( SpatialEditorSelectedItem );
  1751. si->sp=sp;
  1752. si->sbox_instance=VisualServer::get_singleton()->instance_create2(selection_box->get_rid(),sp->get_world()->get_scenario());
  1753. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(si->sbox_instance, VS::SHADOW_CASTING_SETTING_OFF);
  1754. if (get_tree()->is_editor_hint())
  1755. editor->call("edit_node",sp);
  1756. return si;
  1757. }
  1758. void SpatialEditor::_generate_selection_box() {
  1759. AABB aabb( Vector3(), Vector3(1,1,1) );
  1760. aabb.grow_by( aabb.get_longest_axis_size()/20.0 );
  1761. Ref<SurfaceTool> st = memnew( SurfaceTool );
  1762. st->begin(Mesh::PRIMITIVE_LINES);
  1763. for (int i=0;i<12;i++) {
  1764. Vector3 a,b;
  1765. aabb.get_edge(i,a,b);
  1766. /*Vector<Vector3> points;
  1767. Vector<Color> colors;
  1768. points.push_back(a);
  1769. points.push_back(b);*/
  1770. st->add_color( Color(1.0,1.0,0.8,0.8) );
  1771. st->add_vertex(a);
  1772. st->add_color( Color(1.0,1.0,0.8,0.4) );
  1773. st->add_vertex(a.linear_interpolate(b,0.2));
  1774. st->add_color( Color(1.0,1.0,0.8,0.4) );
  1775. st->add_vertex(a.linear_interpolate(b,0.8));
  1776. st->add_color( Color(1.0,1.0,0.8,0.8) );
  1777. st->add_vertex(b);
  1778. }
  1779. Ref<FixedMaterial> mat = memnew( FixedMaterial );
  1780. /*mat->set_flag(Material::FLAG_UNSHADED,true);
  1781. mat->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1));
  1782. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA,true);
  1783. mat->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY,true);*/
  1784. st->set_material(mat);
  1785. selection_box = st->commit();
  1786. }
  1787. Dictionary SpatialEditor::get_state() const {
  1788. Dictionary d;
  1789. d["snap_enabled"]=snap_enabled;
  1790. d["translate_snap"]=get_translate_snap();
  1791. d["rotate_snap"]=get_rotate_snap();
  1792. d["scale_snap"]=get_scale_snap();
  1793. int local_coords_index=transform_menu->get_popup()->get_item_index(MENU_TRANSFORM_LOCAL_COORDS);
  1794. d["local_coords"]=transform_menu->get_popup()->is_item_checked( local_coords_index );
  1795. int vc=0;
  1796. if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT) ))
  1797. vc=1;
  1798. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS) ))
  1799. vc=2;
  1800. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS) ))
  1801. vc=3;
  1802. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS) ))
  1803. vc=4;
  1804. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT) ))
  1805. vc=5;
  1806. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT) ))
  1807. vc=6;
  1808. d["viewport_mode"]=vc;
  1809. Array vpdata;
  1810. for(int i=0;i<4;i++) {
  1811. vpdata.push_back( viewports[i]->get_state() );
  1812. }
  1813. d["viewports"]=vpdata;
  1814. d["default_light"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_LIGHT) );;
  1815. d["ambient_light_color"]=settings_ambient_color->get_color();
  1816. d["default_srgb"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_SRGB) );;
  1817. d["show_grid"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_GRID) );;
  1818. d["show_origin"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN) );;
  1819. d["fov"]=get_fov();
  1820. d["znear"]=get_znear();
  1821. d["zfar"]=get_zfar();
  1822. d["deflight_rot_x"]=settings_default_light_rot_x;
  1823. d["deflight_rot_y"]=settings_default_light_rot_y;
  1824. return d;
  1825. }
  1826. void SpatialEditor::set_state(const Dictionary& p_state) {
  1827. Dictionary d = p_state;
  1828. if (d.has("snap_enabled")) {
  1829. snap_enabled=d["snap_enabled"];
  1830. int snap_enabled_idx=transform_menu->get_popup()->get_item_index(MENU_TRANSFORM_USE_SNAP);
  1831. transform_menu->get_popup()->set_item_checked( snap_enabled_idx, snap_enabled );
  1832. }
  1833. if (d.has("translate_snap"))
  1834. snap_translate->set_text(d["translate_snap"]);
  1835. if (d.has("rotate_snap"))
  1836. snap_rotate->set_text(d["rotate_snap"]);
  1837. if (d.has("scale_snap"))
  1838. snap_scale->set_text(d["scale_snap"]);
  1839. if (d.has("local_coords")) {
  1840. int local_coords_idx=transform_menu->get_popup()->get_item_index(MENU_TRANSFORM_LOCAL_COORDS);
  1841. transform_menu->get_popup()->set_item_checked( local_coords_idx, d["local_coords"] );
  1842. update_transform_gizmo();
  1843. }
  1844. if (d.has("viewport_mode")) {
  1845. int vc = d["viewport_mode"];
  1846. if (vc==1)
  1847. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  1848. else if (vc==2)
  1849. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  1850. else if (vc==3)
  1851. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  1852. else if (vc==4)
  1853. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  1854. else if (vc==5)
  1855. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  1856. else if (vc==6)
  1857. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  1858. }
  1859. if (d.has("viewports")) {
  1860. Array vp = d["viewports"];
  1861. ERR_FAIL_COND(vp.size()>4);
  1862. for(int i=0;i<4;i++) {
  1863. viewports[i]->set_state(vp[i]);
  1864. }
  1865. }
  1866. if (d.has("zfar"))
  1867. settings_zfar->set_val(float(d["zfar"]));
  1868. if (d.has("znear"))
  1869. settings_znear->set_val(float(d["znear"]));
  1870. if (d.has("fov"))
  1871. settings_fov->set_val(float(d["fov"]));
  1872. if (d.has("default_light")) {
  1873. bool use = d["default_light"];
  1874. bool existing = light_instance.is_valid();
  1875. if (use!=existing) {
  1876. if (existing) {
  1877. VisualServer::get_singleton()->free(light_instance);
  1878. light_instance=RID();
  1879. } else {
  1880. light_instance=VisualServer::get_singleton()->instance_create2(light,get_tree()->get_root()->get_world()->get_scenario());
  1881. VisualServer::get_singleton()->instance_set_transform(light_instance,light_transform);
  1882. }
  1883. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_LIGHT), light_instance.is_valid() );
  1884. }
  1885. }
  1886. if (d.has("ambient_light_color")) {
  1887. settings_ambient_color->set_color(d["ambient_light_color"]);
  1888. //viewport_environment->fx_set_param(Environment::FX_PARAM_AMBIENT_LIGHT_COLOR,d["ambient_light_color"]);
  1889. }
  1890. if (d.has("default_srgb")) {
  1891. bool use = d["default_srgb"];
  1892. //viewport_environment->set_enable_fx(Environment::FX_SRGB,use);
  1893. //view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_SRGB), use );
  1894. }
  1895. if (d.has("show_grid")) {
  1896. bool use = d["show_grid"];
  1897. if (use!=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) {
  1898. _menu_item_pressed(MENU_VIEW_GRID);
  1899. }
  1900. }
  1901. if (d.has("show_origin")) {
  1902. bool use = d["show_origin"];
  1903. if (use!=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) {
  1904. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use );
  1905. VisualServer::get_singleton()->instance_geometry_set_flag(origin_instance,VS::INSTANCE_FLAG_VISIBLE,use);
  1906. }
  1907. }
  1908. if (d.has("deflight_rot_x"))
  1909. settings_default_light_rot_x=d["deflight_rot_x"];
  1910. if (d.has("deflight_rot_y"))
  1911. settings_default_light_rot_y=d["deflight_rot_y"];
  1912. _update_default_light_angle();
  1913. }
  1914. void SpatialEditor::edit(Spatial *p_spatial) {
  1915. if (p_spatial!=selected) {
  1916. if (selected) {
  1917. Ref<SpatialEditorGizmo> seg = selected->get_gizmo();
  1918. if (seg.is_valid()) {
  1919. seg->set_selected(false);
  1920. selected->update_gizmo();
  1921. }
  1922. }
  1923. selected=p_spatial;
  1924. over_gizmo_handle=-1;
  1925. if (selected) {
  1926. Ref<SpatialEditorGizmo> seg = selected->get_gizmo();
  1927. if (seg.is_valid()) {
  1928. seg->set_selected(true);
  1929. selected->update_gizmo();
  1930. }
  1931. }
  1932. }
  1933. if (p_spatial) {
  1934. //_validate_selection();
  1935. //if (selected.has(p_spatial->get_instance_ID()) && selected.size()==1)
  1936. // return;
  1937. //_select(p_spatial->get_instance_ID(),false,true);
  1938. // should become the selection
  1939. }
  1940. }
  1941. void SpatialEditor::_xform_dialog_action() {
  1942. Transform t;
  1943. //translation
  1944. Vector3 scale;
  1945. Vector3 rotate;
  1946. Vector3 translate;
  1947. for(int i=0;i<3;i++) {
  1948. translate[i]=xform_translate[i]->get_text().to_double();
  1949. rotate[i]=Math::deg2rad(xform_rotate[i]->get_text().to_double());
  1950. scale[i]=xform_scale[i]->get_text().to_double();
  1951. }
  1952. t.origin=translate;
  1953. for(int i=0;i<3;i++) {
  1954. if (!rotate[i])
  1955. continue;
  1956. Vector3 axis;
  1957. axis[i]=1.0;
  1958. t.basis.rotate(axis,rotate[i]);
  1959. }
  1960. for(int i=0;i<3;i++) {
  1961. if (scale[i]==1)
  1962. continue;
  1963. t.basis.set_axis(i,t.basis.get_axis(i)*scale[i]);
  1964. }
  1965. undo_redo->create_action(TTR("XForm Dialog"));
  1966. List<Node*> &selection = editor_selection->get_selected_node_list();
  1967. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1968. Spatial *sp = E->get()->cast_to<Spatial>();
  1969. if (!sp)
  1970. continue;
  1971. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1972. if (!se)
  1973. continue;
  1974. bool post = xform_type->get_selected()>0;
  1975. Transform tr = sp->get_global_transform();
  1976. if (post)
  1977. tr = tr * t;
  1978. else {
  1979. tr.basis = t.basis * tr.basis;
  1980. tr.origin+=t.origin;
  1981. }
  1982. undo_redo->add_do_method(sp,"set_global_transform",tr);
  1983. undo_redo->add_undo_method(sp,"set_global_transform",sp->get_global_transform());
  1984. }
  1985. undo_redo->commit_action();
  1986. }
  1987. void SpatialEditor::_menu_item_pressed(int p_option) {
  1988. switch(p_option) {
  1989. case MENU_TOOL_SELECT:
  1990. case MENU_TOOL_MOVE:
  1991. case MENU_TOOL_ROTATE:
  1992. case MENU_TOOL_SCALE:
  1993. case MENU_TOOL_LIST_SELECT: {
  1994. for(int i=0;i<TOOL_MAX;i++)
  1995. tool_button[i]->set_pressed(i==p_option);
  1996. tool_mode=(ToolMode)p_option;
  1997. // static const char *_mode[]={"Selection Mode.","Translation Mode.","Rotation Mode.","Scale Mode.","List Selection Mode."};
  1998. // set_message(_mode[p_option],3);
  1999. update_transform_gizmo();
  2000. } break;
  2001. case MENU_TRANSFORM_USE_SNAP: {
  2002. bool is_checked = transform_menu->get_popup()->is_item_checked( transform_menu->get_popup()->get_item_index(p_option) );
  2003. snap_enabled=!is_checked;
  2004. transform_menu->get_popup()->set_item_checked( transform_menu->get_popup()->get_item_index(p_option), snap_enabled );
  2005. } break;
  2006. case MENU_TRANSFORM_CONFIGURE_SNAP: {
  2007. snap_dialog->popup_centered(Size2(200,180));
  2008. } break;
  2009. case MENU_TRANSFORM_LOCAL_COORDS: {
  2010. bool is_checked = transform_menu->get_popup()->is_item_checked( transform_menu->get_popup()->get_item_index(p_option) );
  2011. transform_menu->get_popup()->set_item_checked( transform_menu->get_popup()->get_item_index(p_option), !is_checked );
  2012. update_transform_gizmo();
  2013. } break;
  2014. case MENU_TRANSFORM_DIALOG: {
  2015. for(int i=0;i<3;i++) {
  2016. xform_translate[i]->set_text("0");
  2017. xform_rotate[i]->set_text("0");
  2018. xform_scale[i]->set_text("1");
  2019. }
  2020. xform_dialog->popup_centered(Size2(200,200));
  2021. } break;
  2022. case MENU_VIEW_USE_DEFAULT_LIGHT: {
  2023. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2024. if (is_checked) {
  2025. VisualServer::get_singleton()->free(light_instance);
  2026. light_instance=RID();
  2027. } else {
  2028. light_instance=VisualServer::get_singleton()->instance_create2(light,get_tree()->get_root()->get_world()->get_scenario());
  2029. VisualServer::get_singleton()->instance_set_transform(light_instance,light_transform);
  2030. _update_default_light_angle();
  2031. }
  2032. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), light_instance.is_valid() );
  2033. } break;
  2034. case MENU_VIEW_USE_DEFAULT_SRGB: {
  2035. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2036. if (is_checked) {
  2037. //viewport_environment->set_enable_fx(Environment::FX_SRGB,false);
  2038. } else {
  2039. //viewport_environment->set_enable_fx(Environment::FX_SRGB,true);
  2040. }
  2041. is_checked = ! is_checked;
  2042. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), is_checked );
  2043. } break;
  2044. case MENU_VIEW_USE_1_VIEWPORT: {
  2045. for(int i=1;i<4;i++) {
  2046. viewports[i]->hide();
  2047. }
  2048. viewports[0]->set_area_as_parent_rect();
  2049. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true );
  2050. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2051. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2052. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2053. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2054. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2055. } break;
  2056. case MENU_VIEW_USE_2_VIEWPORTS: {
  2057. for(int i=1;i<4;i++) {
  2058. if (i==1 || i==3)
  2059. viewports[i]->hide();
  2060. else
  2061. viewports[i]->show();
  2062. }
  2063. viewports[0]->set_area_as_parent_rect();
  2064. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2065. viewports[2]->set_area_as_parent_rect();
  2066. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2067. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2068. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true );
  2069. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2070. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2071. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2072. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2073. } break;
  2074. case MENU_VIEW_USE_2_VIEWPORTS_ALT: {
  2075. for(int i=1;i<4;i++) {
  2076. if (i==1 || i==3)
  2077. viewports[i]->hide();
  2078. else
  2079. viewports[i]->show();
  2080. }
  2081. viewports[0]->set_area_as_parent_rect();
  2082. viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2083. viewports[2]->set_area_as_parent_rect();
  2084. viewports[2]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2085. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2086. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2087. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2088. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2089. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true );
  2090. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2091. } break;
  2092. case MENU_VIEW_USE_3_VIEWPORTS: {
  2093. for(int i=1;i<4;i++) {
  2094. if (i==1)
  2095. viewports[i]->hide();
  2096. else
  2097. viewports[i]->show();
  2098. }
  2099. viewports[0]->set_area_as_parent_rect();
  2100. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2101. viewports[2]->set_area_as_parent_rect();
  2102. viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2103. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2104. viewports[3]->set_area_as_parent_rect();
  2105. viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2106. viewports[3]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2107. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2108. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2109. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true );
  2110. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2111. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2112. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2113. } break;
  2114. case MENU_VIEW_USE_3_VIEWPORTS_ALT: {
  2115. for(int i=1;i<4;i++) {
  2116. if (i==1)
  2117. viewports[i]->hide();
  2118. else
  2119. viewports[i]->show();
  2120. }
  2121. viewports[0]->set_area_as_parent_rect();
  2122. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2123. viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2124. viewports[2]->set_area_as_parent_rect();
  2125. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2126. viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2127. viewports[3]->set_area_as_parent_rect();
  2128. viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2129. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2130. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2131. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2132. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  2133. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2134. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true );
  2135. } break;
  2136. case MENU_VIEW_USE_4_VIEWPORTS: {
  2137. for(int i=1;i<4;i++) {
  2138. viewports[i]->show();
  2139. }
  2140. viewports[0]->set_area_as_parent_rect();
  2141. viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2142. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2143. viewports[1]->set_area_as_parent_rect();
  2144. viewports[1]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2145. viewports[1]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  2146. viewports[2]->set_area_as_parent_rect();
  2147. viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  2148. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2149. viewports[3]->set_area_as_parent_rect();
  2150. viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  2151. viewports[3]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  2152. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  2153. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  2154. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  2155. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true );
  2156. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  2157. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  2158. } break;
  2159. case MENU_VIEW_DISPLAY_NORMAL: {
  2160. VisualServer::get_singleton()->scenario_set_debug( get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_DISABLED );
  2161. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), true );
  2162. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false );
  2163. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false );
  2164. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false );
  2165. } break;
  2166. case MENU_VIEW_DISPLAY_WIREFRAME: {
  2167. VisualServer::get_singleton()->scenario_set_debug( get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_WIREFRAME );
  2168. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false );
  2169. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), true );
  2170. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false );
  2171. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false );
  2172. } break;
  2173. case MENU_VIEW_DISPLAY_OVERDRAW: {
  2174. VisualServer::get_singleton()->scenario_set_debug( get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_OVERDRAW );
  2175. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false );
  2176. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false );
  2177. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), true );
  2178. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false );
  2179. } break;
  2180. case MENU_VIEW_DISPLAY_SHADELESS: {
  2181. VisualServer::get_singleton()->scenario_set_debug( get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_SHADELESS );
  2182. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false );
  2183. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false );
  2184. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false );
  2185. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), true );
  2186. } break;
  2187. case MENU_VIEW_ORIGIN: {
  2188. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2189. is_checked=!is_checked;
  2190. VisualServer::get_singleton()->instance_geometry_set_flag(origin_instance,VS::INSTANCE_FLAG_VISIBLE,is_checked);
  2191. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), is_checked);
  2192. } break;
  2193. case MENU_VIEW_GRID: {
  2194. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2195. grid_enabled=!is_checked;
  2196. for(int i=0;i<3;++i) {
  2197. if (grid_enable[i]) {
  2198. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,grid_enabled);
  2199. grid_visible[i]=grid_enabled;
  2200. }
  2201. }
  2202. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), grid_enabled );
  2203. } break;
  2204. case MENU_VIEW_CAMERA_SETTINGS: {
  2205. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size()+Size2(50,50));
  2206. } break;
  2207. }
  2208. }
  2209. void SpatialEditor::_init_indicators() {
  2210. //make sure that the camera indicator is not selectable
  2211. light=VisualServer::get_singleton()->light_create( VisualServer::LIGHT_DIRECTIONAL );
  2212. //VisualServer::get_singleton()->light_set_shadow( light, true );
  2213. light_instance=VisualServer::get_singleton()->instance_create2(light,get_tree()->get_root()->get_world()->get_scenario());
  2214. light_transform.rotate(Vector3(1,0,0),Math_PI/5.0);
  2215. VisualServer::get_singleton()->instance_set_transform(light_instance,light_transform);
  2216. //RID mat = VisualServer::get_singleton()->fixed_material_create();
  2217. ///VisualServer::get_singleton()->fixed_material_set_flag(mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA,true);
  2218. //VisualServer::get_singleton()->fixed_material_set_flag(mat, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  2219. {
  2220. indicator_mat = VisualServer::get_singleton()->material_create();
  2221. /*VisualServer::get_singleton()->material_set_flag( indicator_mat, VisualServer::MATERIAL_FLAG_UNSHADED, true );
  2222. VisualServer::get_singleton()->material_set_flag( indicator_mat, VisualServer::MATERIAL_FLAG_ONTOP, false );
  2223. VisualServer::get_singleton()->fixed_material_set_flag(indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA,true);
  2224. VisualServer::get_singleton()->fixed_material_set_flag(indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);*/
  2225. DVector<Color> grid_colors[3];
  2226. DVector<Vector3> grid_points[3];
  2227. Vector<Color> origin_colors;
  2228. Vector<Vector3> origin_points;
  2229. Color grid_color = EditorSettings::get_singleton()->get("3d_editor/grid_color");
  2230. for(int i=0;i<3;i++) {
  2231. Vector3 axis;
  2232. axis[i]=1;
  2233. Vector3 axis_n1;
  2234. axis_n1[(i+1)%3]=1;
  2235. Vector3 axis_n2;
  2236. axis_n2[(i+2)%3]=1;
  2237. origin_colors.push_back(Color(axis.x,axis.y,axis.z));
  2238. origin_colors.push_back(Color(axis.x,axis.y,axis.z));
  2239. origin_points.push_back(axis*4096);
  2240. origin_points.push_back(axis*-4096);
  2241. #define ORIGIN_GRID_SIZE 25
  2242. for(int j=-ORIGIN_GRID_SIZE;j<=ORIGIN_GRID_SIZE;j++) {
  2243. grid_colors[i].push_back(grid_color);
  2244. grid_colors[i].push_back(grid_color);
  2245. grid_colors[i].push_back(grid_color);
  2246. grid_colors[i].push_back(grid_color);
  2247. grid_points[i].push_back(axis_n1*ORIGIN_GRID_SIZE+axis_n2*j);
  2248. grid_points[i].push_back(-axis_n1*ORIGIN_GRID_SIZE+axis_n2*j);
  2249. grid_points[i].push_back(axis_n2*ORIGIN_GRID_SIZE+axis_n1*j);
  2250. grid_points[i].push_back(-axis_n2*ORIGIN_GRID_SIZE+axis_n1*j);
  2251. }
  2252. grid[i] = VisualServer::get_singleton()->mesh_create();
  2253. Array d;
  2254. d.resize(VS::ARRAY_MAX);
  2255. d[VisualServer::ARRAY_VERTEX]=grid_points[i];
  2256. d[VisualServer::ARRAY_COLOR]=grid_colors[i];
  2257. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i],VisualServer::PRIMITIVE_LINES,d);
  2258. VisualServer::get_singleton()->mesh_surface_set_material(grid[i],0,indicator_mat);
  2259. grid_instance[i] = VisualServer::get_singleton()->instance_create2(grid[i],get_tree()->get_root()->get_world()->get_scenario());
  2260. grid_visible[i]=false;
  2261. grid_enable[i]=false;
  2262. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  2263. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(grid_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2264. VS::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  2265. }
  2266. origin = VisualServer::get_singleton()->mesh_create();
  2267. Array d;
  2268. d.resize(VS::ARRAY_MAX);
  2269. d[VisualServer::ARRAY_VERTEX]=origin_points;
  2270. d[VisualServer::ARRAY_COLOR]=origin_colors;
  2271. VisualServer::get_singleton()->mesh_add_surface_from_arrays(origin,VisualServer::PRIMITIVE_LINES,d);
  2272. VisualServer::get_singleton()->mesh_surface_set_material(origin,0,indicator_mat);
  2273. // origin = VisualServer::get_singleton()->poly_create();
  2274. // VisualServer::get_singleton()->poly_add_primitive(origin,origin_points,Vector<Vector3>(),origin_colors,Vector<Vector3>());
  2275. // VisualServer::get_singleton()->poly_set_material(origin,indicator_mat,true);
  2276. origin_instance = VisualServer::get_singleton()->instance_create2(origin,get_tree()->get_root()->get_world()->get_scenario());
  2277. VS::get_singleton()->instance_set_layer_mask(origin_instance,1<<SpatialEditorViewport::GIZMO_GRID_LAYER);
  2278. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(origin_instance, VS::SHADOW_CASTING_SETTING_OFF);
  2279. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[1],VS::INSTANCE_FLAG_VISIBLE,true);
  2280. grid_enable[1]=true;
  2281. grid_visible[1]=true;
  2282. grid_enabled=true;
  2283. last_grid_snap=1;
  2284. }
  2285. {
  2286. cursor_mesh = VisualServer::get_singleton()->mesh_create();
  2287. DVector<Vector3> cursor_points;
  2288. float cs = 0.25;
  2289. cursor_points.push_back(Vector3(+cs,0,0));
  2290. cursor_points.push_back(Vector3(-cs,0,0));
  2291. cursor_points.push_back(Vector3(0,+cs,0));
  2292. cursor_points.push_back(Vector3(0,-cs,0));
  2293. cursor_points.push_back(Vector3(0,0,+cs));
  2294. cursor_points.push_back(Vector3(0,0,-cs));
  2295. cursor_material=VisualServer::get_singleton()->material_create();
  2296. /*VisualServer::get_singleton()->fixed_material_set_param(cursor_material,VS::FIXED_MATERIAL_PARAM_DIFFUSE,Color(0,1,1));
  2297. VisualServer::get_singleton()->material_set_flag( cursor_material, VisualServer::MATERIAL_FLAG_UNSHADED, true );
  2298. VisualServer::get_singleton()->fixed_material_set_flag(cursor_material, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA,true);
  2299. VisualServer::get_singleton()->fixed_material_set_flag(cursor_material, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);*/
  2300. Array d;
  2301. d.resize(VS::ARRAY_MAX);
  2302. d[VS::ARRAY_VERTEX]=cursor_points;
  2303. VisualServer::get_singleton()->mesh_add_surface_from_arrays(cursor_mesh,VS::PRIMITIVE_LINES,d);
  2304. VisualServer::get_singleton()->mesh_surface_set_material(cursor_mesh,0,cursor_material);
  2305. cursor_instance = VisualServer::get_singleton()->instance_create2(cursor_mesh,get_tree()->get_root()->get_world()->get_scenario());
  2306. VS::get_singleton()->instance_set_layer_mask(cursor_instance,1<<SpatialEditorViewport::GIZMO_GRID_LAYER);
  2307. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(cursor_instance, VS::SHADOW_CASTING_SETTING_OFF);
  2308. }
  2309. {
  2310. //move gizmo
  2311. float gizmo_alph = EditorSettings::get_singleton()->get("3d_editor/manipulator_gizmo_opacity");
  2312. gizmo_hl = Ref<FixedMaterial>( memnew( FixedMaterial ) );
  2313. /* gizmo_hl->set_flag(Material::FLAG_UNSHADED, true);
  2314. gizmo_hl->set_flag(Material::FLAG_ONTOP, true);
  2315. gizmo_hl->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2316. gizmo_hl->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1,gizmo_alph+0.2f));*/
  2317. for(int i=0;i<3;i++) {
  2318. move_gizmo[i]=Ref<Mesh>( memnew( Mesh ) );
  2319. rotate_gizmo[i]=Ref<Mesh>( memnew( Mesh ) );
  2320. Ref<FixedMaterial> mat = memnew( FixedMaterial );
  2321. /* mat->set_flag(Material::FLAG_UNSHADED, true);
  2322. mat->set_flag(Material::FLAG_ONTOP, true);
  2323. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2324. Color col;
  2325. col[i]=1.0;
  2326. col.a= gizmo_alph;
  2327. mat->set_parameter(FixedMaterial::PARAM_DIFFUSE,col);*/
  2328. gizmo_color[i]=mat;
  2329. Vector3 ivec;
  2330. ivec[i]=1;
  2331. Vector3 nivec;
  2332. nivec[(i+1)%3]=1;
  2333. nivec[(i+2)%3]=1;
  2334. Vector3 ivec2;
  2335. ivec2[(i+1)%3]=1;
  2336. Vector3 ivec3;
  2337. ivec3[(i+2)%3]=1;
  2338. {
  2339. Ref<SurfaceTool> surftool = memnew( SurfaceTool );
  2340. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  2341. //translate
  2342. const int arrow_points=5;
  2343. Vector3 arrow[5]={
  2344. nivec*0.0+ivec*0.0,
  2345. nivec*0.01+ivec*0.0,
  2346. nivec*0.01+ivec*1.0,
  2347. nivec*0.1+ivec*1.0,
  2348. nivec*0.0+ivec*(1+GIZMO_ARROW_SIZE),
  2349. };
  2350. int arrow_sides=6;
  2351. for(int k = 0; k < 7 ; k++) {
  2352. Matrix3 ma(ivec,Math_PI*2*float(k)/arrow_sides);
  2353. Matrix3 mb(ivec,Math_PI*2*float(k+1)/arrow_sides);
  2354. for(int j=0;j<arrow_points-1;j++) {
  2355. Vector3 points[4]={
  2356. ma.xform(arrow[j]),
  2357. mb.xform(arrow[j]),
  2358. mb.xform(arrow[j+1]),
  2359. ma.xform(arrow[j+1]),
  2360. };
  2361. surftool->add_vertex(points[0]);
  2362. surftool->add_vertex(points[1]);
  2363. surftool->add_vertex(points[2]);
  2364. surftool->add_vertex(points[0]);
  2365. surftool->add_vertex(points[2]);
  2366. surftool->add_vertex(points[3]);
  2367. }
  2368. }
  2369. surftool->set_material(mat);
  2370. surftool->commit(move_gizmo[i]);
  2371. }
  2372. {
  2373. Ref<SurfaceTool> surftool = memnew( SurfaceTool );
  2374. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  2375. Vector3 circle[5]={
  2376. ivec*0.02+ivec2*0.02+ivec2*1.0,
  2377. ivec*-0.02+ivec2*0.02+ivec2*1.0,
  2378. ivec*-0.02+ivec2*-0.02+ivec2*1.0,
  2379. ivec*0.02+ivec2*-0.02+ivec2*1.0,
  2380. ivec*0.02+ivec2*0.02+ivec2*1.0,
  2381. };
  2382. for(int k = 0; k < 33 ; k++) {
  2383. Matrix3 ma(ivec,Math_PI*2*float(k)/32);
  2384. Matrix3 mb(ivec,Math_PI*2*float(k+1)/32);
  2385. for(int j=0;j<4;j++) {
  2386. Vector3 points[4]={
  2387. ma.xform(circle[j]),
  2388. mb.xform(circle[j]),
  2389. mb.xform(circle[j+1]),
  2390. ma.xform(circle[j+1]),
  2391. };
  2392. surftool->add_vertex(points[0]);
  2393. surftool->add_vertex(points[1]);
  2394. surftool->add_vertex(points[2]);
  2395. surftool->add_vertex(points[0]);
  2396. surftool->add_vertex(points[2]);
  2397. surftool->add_vertex(points[3]);
  2398. }
  2399. }
  2400. surftool->set_material(mat);
  2401. surftool->commit(rotate_gizmo[i]);
  2402. }
  2403. }
  2404. }
  2405. /*for(int i=0;i<4;i++) {
  2406. viewports[i]->init_gizmo_instance(i);
  2407. }*/
  2408. _generate_selection_box();
  2409. //get_scene()->get_root_node()->cast_to<EditorNode>()->get_scene_root()->add_child(camera);
  2410. //current_camera=camera;
  2411. }
  2412. void SpatialEditor::_finish_indicators() {
  2413. VisualServer::get_singleton()->free(origin_instance);
  2414. VisualServer::get_singleton()->free(origin);
  2415. for(int i=0;i<3;i++) {
  2416. VisualServer::get_singleton()->free(grid_instance[i]);
  2417. VisualServer::get_singleton()->free(grid[i]);
  2418. }
  2419. VisualServer::get_singleton()->free(light_instance);
  2420. VisualServer::get_singleton()->free(light);
  2421. //VisualServer::get_singleton()->free(poly);
  2422. //VisualServer::get_singleton()->free(indicators_instance);
  2423. //VisualServer::get_singleton()->free(indicators);
  2424. VisualServer::get_singleton()->free(cursor_instance);
  2425. VisualServer::get_singleton()->free(cursor_mesh);
  2426. VisualServer::get_singleton()->free(indicator_mat);
  2427. VisualServer::get_singleton()->free(cursor_material);
  2428. }
  2429. void SpatialEditor::_instance_scene() {
  2430. #if 0
  2431. EditorNode *en = get_scene()->get_root_node()->cast_to<EditorNode>();
  2432. ERR_FAIL_COND(!en);
  2433. String path = en->get_scenes_dock()->get_selected_path();
  2434. if (path=="") {
  2435. set_message(TTR("No scene selected to instance!"));
  2436. return;
  2437. }
  2438. undo_redo->create_action(TTR("Instance at Cursor"));
  2439. Node* scene = en->request_instance_scene(path);
  2440. if (!scene) {
  2441. set_message(TTR("Could not instance scene!"));
  2442. undo_redo->commit_action(); //bleh
  2443. return;
  2444. }
  2445. Spatial *s = scene->cast_to<Spatial>();
  2446. if (s) {
  2447. undo_redo->add_do_method(s,"set_global_transform",Transform(Matrix3(),cursor.cursor_pos));
  2448. }
  2449. undo_redo->commit_action();
  2450. #endif
  2451. }
  2452. void SpatialEditor::_unhandled_key_input(InputEvent p_event) {
  2453. if (!is_visible() || get_viewport()->gui_has_modal_stack())
  2454. return;
  2455. #if 0
  2456. //i don't remember this being used, why was it here?
  2457. {
  2458. EditorNode *en = editor;
  2459. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  2460. if (!over_plugin_list->empty() && over_plugin_list->forward_input_event(p_event)) {
  2461. return; //ate the over input event
  2462. }
  2463. }
  2464. #endif
  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( ViewportContainer );
  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. }