spatial_editor_plugin.cpp 123 KB

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