spatial_editor_plugin.cpp 123 KB

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