spatial_editor_plugin.cpp 120 KB

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