level_editor.vala 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #if CROWN_PLATFORM_WINDOWS
  6. extern uint GetCurrentProcessId();
  7. extern uintptr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);
  8. #elif CROWN_PLATFORM_LINUX
  9. extern Posix.pid_t getpid();
  10. #endif
  11. namespace Crown
  12. {
  13. const int WINDOW_DEFAULT_WIDTH = 1280;
  14. const int WINDOW_DEFAULT_HEIGHT = 720;
  15. const string CROWN_EDITOR_NAME = "Crown Editor";
  16. const string CROWN_EDITOR_ICON_NAME = "org.crownengine.Crown";
  17. const string CROWN_SUBPROCESS_LAUNCHER = "org.crownengine.SubprocessLauncher";
  18. public const string PANEL_WAITING = "panel-waiting";
  19. public const string PANEL_EDITOR = "panel-editor";
  20. public const string PANEL_PROJECTS_LIST = "panel-projects-list";
  21. public const string PANEL_NEW_PROJECT = "panel-new-project";
  22. public enum Theme
  23. {
  24. DARK,
  25. LIGHT,
  26. COUNT
  27. }
  28. public enum ToolType
  29. {
  30. PLACE,
  31. MOVE,
  32. ROTATE,
  33. SCALE,
  34. COUNT
  35. }
  36. public enum SnapMode
  37. {
  38. RELATIVE,
  39. ABSOLUTE
  40. }
  41. public enum ReferenceSystem
  42. {
  43. LOCAL,
  44. WORLD
  45. }
  46. public enum CameraViewType
  47. {
  48. PERSPECTIVE,
  49. FRONT,
  50. BACK,
  51. RIGHT,
  52. LEFT,
  53. TOP,
  54. BOTTOM,
  55. COUNT;
  56. public string to_label()
  57. {
  58. switch (this) {
  59. case PERSPECTIVE:
  60. return "Perspective";
  61. case FRONT:
  62. return "View Front";
  63. case BACK:
  64. return "View Back";
  65. case RIGHT:
  66. return "View Right";
  67. case LEFT:
  68. return "View Left";
  69. case TOP:
  70. return "View Top";
  71. case BOTTOM:
  72. return "View Bottom";
  73. default:
  74. return "View Unknown";
  75. }
  76. }
  77. }
  78. public enum TargetConfig
  79. {
  80. RELEASE,
  81. DEVELOPMENT,
  82. DEBUG,
  83. COUNT;
  84. public string to_key()
  85. {
  86. switch (this) {
  87. case RELEASE:
  88. return "release";
  89. case DEVELOPMENT:
  90. return "development";
  91. case DEBUG:
  92. return "debug";
  93. default:
  94. return "unknown";
  95. }
  96. }
  97. public string to_label()
  98. {
  99. switch (this) {
  100. case RELEASE:
  101. return "Release";
  102. case DEVELOPMENT:
  103. return "Development";
  104. case DEBUG:
  105. return "Debug";
  106. default:
  107. return "unknown";
  108. }
  109. }
  110. }
  111. public enum TargetPlatform
  112. {
  113. ANDROID,
  114. HTML5,
  115. LINUX,
  116. WINDOWS,
  117. COUNT;
  118. public string to_key()
  119. {
  120. switch (this) {
  121. case ANDROID:
  122. return "android";
  123. case HTML5:
  124. return "html5";
  125. case LINUX:
  126. return "linux";
  127. case WINDOWS:
  128. return "windows";
  129. default:
  130. return "unknown";
  131. }
  132. }
  133. public string to_label()
  134. {
  135. switch (this) {
  136. case ANDROID:
  137. return "Android";
  138. case HTML5:
  139. return "HTML5";
  140. case LINUX:
  141. return "Linux";
  142. case WINDOWS:
  143. return "Windows";
  144. default:
  145. return "Unknown";
  146. }
  147. }
  148. }
  149. public enum TargetArch
  150. {
  151. X86,
  152. X64,
  153. ARM,
  154. ARM64,
  155. WASM
  156. }
  157. public class RuntimeInstance
  158. {
  159. public const int QUIT_TIMEOUT_MS = 4000;
  160. public string _name;
  161. public uint32 _process_id;
  162. public bool _stuck;
  163. public uint _revision;
  164. public GLib.SourceFunc _stop_callback;
  165. public GLib.SourceFunc _refresh_callback;
  166. public bool _refresh_success;
  167. public ConsoleClient _client;
  168. public signal void connected(RuntimeInstance ri, string address, int port);
  169. public signal void disconnected(RuntimeInstance ri);
  170. public signal void disconnected_unexpected(RuntimeInstance ri);
  171. public signal void message_received(RuntimeInstance ri, ConsoleClient client, uint8[] json);
  172. public RuntimeInstance(string name)
  173. {
  174. _name = name;
  175. _process_id = uint32.MAX;
  176. _stuck = false;
  177. _revision = 0;
  178. _stop_callback = null;
  179. _refresh_callback = null;
  180. _refresh_success = false;
  181. _client = new ConsoleClient();
  182. _client.connected.connect(on_client_connected);
  183. _client.message_received.connect(on_client_message_received);
  184. }
  185. public void on_client_connected(string address, int port)
  186. {
  187. connected(this, address, port);
  188. }
  189. public void on_client_disconnected()
  190. {
  191. disconnected(this);
  192. if (_stop_callback != null)
  193. _stop_callback();
  194. }
  195. public void on_client_disconnected_unexpected()
  196. {
  197. disconnected_unexpected(this);
  198. try {
  199. if (_process_id != uint32.MAX) {
  200. _subprocess_launcher.wait(_process_id);
  201. _process_id = uint32.MAX;
  202. }
  203. } catch (GLib.Error e) {
  204. loge(e.message);
  205. }
  206. }
  207. public void on_client_message_received(ConsoleClient client, uint8[] json)
  208. {
  209. message_received(this, client, json);
  210. }
  211. // Tries to connect to the @a client. Return the number of tries after
  212. // it succeeded or @a num_tries if failed.
  213. public async int connect_async(string address, int port, int num_tries, int interval)
  214. {
  215. // It is an error if the client disconnects after here.
  216. _client.disconnected.disconnect(on_client_disconnected);
  217. _client.disconnected.connect(on_client_disconnected_unexpected);
  218. // Try to connect to the client.
  219. int tries;
  220. for (tries = 0; tries < num_tries; ++tries) {
  221. _client.connect(address, port);
  222. if (_client.is_connected())
  223. break;
  224. GLib.Thread.usleep(interval*1000);
  225. }
  226. return tries;
  227. }
  228. public async void stop()
  229. {
  230. if (_client != null) {
  231. // Reset "disconnected" signal.
  232. _client.disconnected.disconnect(on_client_disconnected);
  233. _client.disconnected.disconnect(on_client_disconnected_unexpected);
  234. // Explicit call to this function should not produce error messages.
  235. _client.disconnected.connect(on_client_disconnected);
  236. if (_client.is_connected()) {
  237. _stop_callback = stop.callback;
  238. _client.send(RuntimeApi.quit());
  239. // Call it stuck if not disconnected before a while.
  240. GLib.Timeout.add_full(GLib.Priority.HIGH, QUIT_TIMEOUT_MS, () => {
  241. if (_stop_callback != null) {
  242. _stuck = true;
  243. _stop_callback();
  244. }
  245. return GLib.Source.REMOVE;
  246. });
  247. yield; // Wait for _client to disconnect.
  248. _stop_callback = null;
  249. }
  250. }
  251. try {
  252. if (_process_id != uint32.MAX) {
  253. if (_stuck) {
  254. _subprocess_launcher.kill(_process_id);
  255. _stuck = false;
  256. logw("Process %u took more than %d ms to quit: killed".printf(_process_id, QUIT_TIMEOUT_MS));
  257. }
  258. _subprocess_launcher.wait(_process_id);
  259. }
  260. _process_id = uint32.MAX;
  261. } catch (GLib.Error e) {
  262. loge(e.message);
  263. }
  264. }
  265. public void send(string json)
  266. {
  267. _client.send(json);
  268. }
  269. public void send_script(string lua)
  270. {
  271. _client.send_script(lua);
  272. }
  273. public bool is_connected()
  274. {
  275. return _client.is_connected();
  276. }
  277. public async bool refresh(DataCompiler dc)
  278. {
  279. if (_refresh_callback != null)
  280. return false;
  281. if (!is_connected())
  282. return false;
  283. var compiler_revision = dc._revision;
  284. var refresh_list = yield dc.refresh_list(_revision);
  285. _client.send(DeviceApi.refresh(refresh_list));
  286. _client.send(DeviceApi.frame());
  287. _refresh_callback = refresh.callback;
  288. yield; // Wait for client to refresh the resources.
  289. if (_refresh_success)
  290. _revision = compiler_revision;
  291. return _refresh_success;
  292. }
  293. public void refresh_finished(bool success)
  294. {
  295. _refresh_success = success;
  296. if (_refresh_callback != null)
  297. _refresh_callback();
  298. _refresh_callback = null;
  299. }
  300. }
  301. const string BACKEND_STOPPING = "backend-stopping";
  302. const string COMPILER_CRASHED = "compiler-crashed";
  303. const string COMPILER_COMPILING_DATA = "compiler-compiling-data";
  304. const string COMPILER_FAILED_COMPILATION = "compiler-failed-compilation";
  305. const string COMPILER_CONNECTING = "compiler-connecting";
  306. Gtk.Stack make_compiler_stack(Gtk.Widget child)
  307. {
  308. Gtk.Stack stack = new Gtk.Stack();
  309. stack.add_named(stopping_backend(), BACKEND_STOPPING);
  310. stack.add_named(compiling_data(), COMPILER_COMPILING_DATA);
  311. stack.add_named(connecting_to_data_compiler(), COMPILER_CONNECTING);
  312. stack.add_named(compiler_crashed(), COMPILER_CRASHED);
  313. stack.add_named(compiler_failed_compilation(), COMPILER_FAILED_COMPILATION);
  314. stack.add_named(child, "child");
  315. return stack;
  316. }
  317. public class LevelEditorWindow : Gtk.ApplicationWindow
  318. {
  319. public const GLib.ActionEntry[] action_entries =
  320. {
  321. { "fullscreen", on_fullscreen, null, null }
  322. };
  323. public bool _fullscreen;
  324. public LevelEditorWindow(Gtk.Application app, Gtk.HeaderBar header_bar)
  325. {
  326. Object(application: app);
  327. this.add_action_entries(action_entries, this);
  328. this.set_titlebar(header_bar);
  329. this.title = CROWN_EDITOR_NAME;
  330. this.window_state_event.connect(this.on_window_state_event);
  331. this.delete_event.connect(this.on_delete_event);
  332. _fullscreen = false;
  333. this.set_default_size(WINDOW_DEFAULT_WIDTH, WINDOW_DEFAULT_HEIGHT);
  334. }
  335. public void on_fullscreen(GLib.SimpleAction action, GLib.Variant? param)
  336. {
  337. if (_fullscreen)
  338. unfullscreen();
  339. else
  340. fullscreen();
  341. }
  342. public bool on_window_state_event(Gdk.EventWindowState ev)
  343. {
  344. _fullscreen = (ev.new_window_state & Gdk.WindowState.FULLSCREEN) != 0;
  345. return Gdk.EVENT_PROPAGATE;
  346. }
  347. public bool on_delete_event()
  348. {
  349. GLib.Application.get_default().activate_action("quit", null);
  350. return Gdk.EVENT_STOP; // Keep window alive.
  351. }
  352. public Hashtable encode()
  353. {
  354. Hashtable json_obj = new Hashtable();
  355. // This is the appropriate size to save, see:
  356. // https://valadoc.org/gtk+-3.0/Gtk.Window.set_default_size.html
  357. int width;
  358. int height;
  359. this.get_size(out width, out height);
  360. json_obj["width"] = width;
  361. json_obj["height"] = height;
  362. json_obj["maximized"] = this.is_maximized;
  363. json_obj["fullscreen"] = this._fullscreen;
  364. return json_obj;
  365. }
  366. public void decode(Hashtable json_obj)
  367. {
  368. if (json_obj.has_key("width"))
  369. this.default_width = (int)(double)json_obj["width"];
  370. else
  371. this.default_width = WINDOW_DEFAULT_WIDTH;
  372. if (json_obj.has_key("height"))
  373. this.default_height = (int)(double)json_obj["height"];
  374. else
  375. this.default_height = WINDOW_DEFAULT_HEIGHT;
  376. if (json_obj.has_key("maximized")) {
  377. if ((bool)json_obj["maximized"])
  378. this.maximize();
  379. else
  380. this.unmaximize();
  381. }
  382. if (json_obj.has_key("fullscreen")) {
  383. if ((bool)json_obj["fullscreen"])
  384. this.fullscreen();
  385. else
  386. this.unfullscreen();
  387. }
  388. }
  389. }
  390. public enum StartGame
  391. {
  392. NORMAL,
  393. TEST
  394. }
  395. public class LevelEditorApplication : Gtk.Application
  396. {
  397. public const GLib.ActionEntry[] action_entries_file =
  398. {
  399. // parameter type
  400. // name activate() | state
  401. // | | | |
  402. { "menu-file", null, null, null },
  403. { "new-level", on_new_level, null, null },
  404. { "open-level", on_open_level, "s", null },
  405. { "new-project", on_new_project, null, null },
  406. { "add-project", on_add_project, null, null },
  407. { "remove-project", on_remove_project, "s", null },
  408. { "open-project", on_open_project, "(ssi)", null },
  409. { "open-project-null", on_open_project, null, null },
  410. { "open-projects-list", on_open_projects_list, null, null },
  411. { "save", on_save, null, null },
  412. { "save-as", on_save_as, null, null },
  413. { "import", on_import, "(sas)", null },
  414. { "import-null", on_import, null, null },
  415. { "preferences", on_preferences, null, null },
  416. { "deploy", on_deploy, null, null },
  417. { "close-project", on_close_project, null, null },
  418. { "quit", on_quit, null, null },
  419. { "open-resource", on_open_resource, "s", null },
  420. { "copy-path", on_copy_path, "s", null },
  421. { "copy-name", on_copy_name, "s", null },
  422. };
  423. public const GLib.ActionEntry[] action_entries_edit =
  424. {
  425. { "menu-edit", null, null, null },
  426. { "rename", on_rename, "(ss)", null },
  427. { "tool", on_tool, "i", "1" }, // See: Crown.ToolType
  428. { "set-placeable", on_set_placeable, "(ss)", null },
  429. { "cancel-place", on_cancel_place, null, null },
  430. { "snap", on_snap, "i", "0" }, // See: Crown.SnapMode
  431. { "reference-system", on_reference_system, "i", "0" }, // See: Crown.ReferenceSystem
  432. { "snap-to-grid", on_snap_to_grid, null, "false" },
  433. { "menu-grid", null, null, null },
  434. { "grid-show", on_show_grid, null, "true" },
  435. { "grid-size", on_grid_size, "i", "10" }, // 10*meters.
  436. { "menu-rotation-snap", null, null, null },
  437. { "rotation-snap-size", on_rotation_snap_size, "i", "15" }
  438. };
  439. public const GLib.ActionEntry[] action_entries_create =
  440. {
  441. { "menu-create", null, null, null },
  442. { "menu-primitives", null, null, null },
  443. { "primitive-cube", on_spawn_primitive, null, null },
  444. { "primitive-sphere", on_spawn_primitive, null, null },
  445. { "primitive-cone", on_spawn_primitive, null, null },
  446. { "primitive-cylinder", on_spawn_primitive, null, null },
  447. { "primitive-plane", on_spawn_primitive, null, null },
  448. { "camera", on_spawn_primitive, null, null },
  449. { "light", on_spawn_primitive, null, null },
  450. { "sound-source", on_spawn_primitive, null, null },
  451. { "unit-empty", on_spawn_unit, null, null },
  452. { "shading-environment",on_spawn_unit, null, null },
  453. };
  454. public const GLib.ActionEntry[] action_entries_camera =
  455. {
  456. { "menu-camera", null, null, null },
  457. { "camera-frame-all", on_camera_frame_all, null, null }
  458. };
  459. public const GLib.ActionEntry[] action_entries_view =
  460. {
  461. { "menu-view", null, null, null },
  462. { "project-browser", on_project_browser, null, null },
  463. { "console", on_console, null, null },
  464. { "statusbar", on_statusbar, null, null },
  465. { "inspector", on_inspector, null, null },
  466. { "debug-render-world", on_debug_render_world, null, "false" },
  467. { "debug-physics-world", on_debug_physics_world, null, "false" }
  468. };
  469. public const GLib.ActionEntry[] action_entries_debug =
  470. {
  471. { "menu-debug", null, null, null },
  472. { "test-level", on_run_game, null, null },
  473. { "run-game", on_run_game, null, null },
  474. { "build-data", on_build_data, null, null },
  475. { "reload-all", on_reload_all, null, null },
  476. { "restart-backend", on_restart_backend, null, null },
  477. { "restart-editor-view", on_restart_editor_view, null, null }
  478. };
  479. public const GLib.ActionEntry[] action_entries_help =
  480. {
  481. { "menu-help", null, null, null },
  482. { "manual", on_manual, null, null },
  483. { "report-issue", on_report_issue, null, null },
  484. { "browse-logs", on_browse_logs, null, null },
  485. { "changelog", on_changelog, null, null },
  486. { "donate", on_donate, null, null },
  487. { "credits", on_credits, null, null }
  488. };
  489. public const GLib.ActionEntry[] action_entries_project =
  490. {
  491. { "delete-file", on_delete_file, "s", null },
  492. { "delete-directory", on_delete_directory, "s", null },
  493. { "create-directory", on_create_directory, "(ss)", null },
  494. { "create-script", on_create_script, "(ssb)", null },
  495. { "create-unit", on_create_unit, "(ss)", null },
  496. { "create-state-machine", on_create_state_machine, "(sss)", null },
  497. { "create-material", on_create_material, "(ss)", null },
  498. { "open-containing", on_open_containing, "s", null },
  499. { "texture-settings", on_texture_settings, "s", null },
  500. { "state-machine-editor", on_state_machine_editor, "s", null },
  501. { "open-object", on_open_object, "(ss)", null },
  502. { "reveal-resource", on_reveal, "(ss)", null },
  503. };
  504. public const GLib.ActionEntry[] action_entries_package =
  505. {
  506. { "create-package-android", on_create_package_android, "(sississsssi)", null },
  507. { "create-package-html5", on_create_package_html5, "(sis)", null },
  508. { "create-package-linux", on_create_package_linux, "(sis)", null },
  509. { "create-package-windows", on_create_package_windows, "(sis)", null }
  510. };
  511. public const GLib.ActionEntry[] action_entries_unit =
  512. {
  513. { "unit-save-as-prefab", on_unit_save_as_prefab, "(ss)", null },
  514. { "open-unit", on_open_unit, "s", null },
  515. };
  516. // Command line options
  517. public uint _launcher_watch_id;
  518. public string? _source_dir = null;
  519. public string _level_resource = "";
  520. public User _user;
  521. public Hashtable _settings;
  522. public Hashtable _window_state;
  523. // Editor state
  524. public double _grid_size;
  525. public double _rotation_snap;
  526. public bool _show_grid;
  527. public bool _snap_to_grid;
  528. public bool _debug_render_world;
  529. public bool _debug_physics_world;
  530. public ToolType _tool_type;
  531. public ToolType _tool_type_prev;
  532. public SnapMode _snap_mode;
  533. public ReferenceSystem _reference_system;
  534. // Project state
  535. public string _placeable_type;
  536. public string _placeable_name;
  537. // Accelerators
  538. public string[] _tool_place_accels;
  539. public string[] _tool_move_accels;
  540. public string[] _tool_rotate_accels;
  541. public string[] _tool_scale_accels;
  542. public string[] _delete_accels;
  543. public string[] _camera_view_perspective_accels;
  544. public string[] _camera_view_front_accels;
  545. public string[] _camera_view_back_accels;
  546. public string[] _camera_view_right_accels;
  547. public string[] _camera_view_left_accels;
  548. public string[] _camera_view_top_accels;
  549. public string[] _camera_view_bottom_accels;
  550. public string[] _camera_frame_selected_accels;
  551. public string[] _camera_frame_all_accels;
  552. // Engine connections
  553. public Gee.ArrayList<RuntimeInstance> _runtimes;
  554. public RuntimeInstance _compiler;
  555. public RuntimeInstance _editor;
  556. public RuntimeInstance _game;
  557. public RuntimeInstance _thumbnail;
  558. // Level data
  559. public DatabaseEditor _database_editor;
  560. public Database _database;
  561. public Project _project;
  562. public ProjectStore _project_store;
  563. public Level _level;
  564. public DataCompiler _data_compiler;
  565. // Widgets
  566. public Gtk.CssProvider _css_provider;
  567. public ProjectBrowser _project_browser;
  568. public EditorViewport _editor_viewport;
  569. public LevelTreeView _level_treeview;
  570. public LevelLayersTreeView _level_layers_treeview;
  571. public PropertiesView _properties_view;
  572. public PreferencesDialog _preferences_dialog;
  573. public DeployDialog _deploy_dialog;
  574. public TextureSettingsDialog _texture_settings_dialog;
  575. public UnitEditor _unit_editor_dialog;
  576. public StateMachineEditor _state_machine_editor;
  577. public ObjectEditor _object_editor;
  578. public ThumbnailCache _thumbnail_cache;
  579. public Gtk.Stack _project_stack;
  580. public Gtk.Stack _editor_stack;
  581. public Gtk.Stack _inspector_stack;
  582. public Toolbar _toolbar;
  583. public Gtk.Image _game_run_stop_image;
  584. public Gtk.Button _game_run;
  585. public Gtk.Notebook _level_tree_view_notebook;
  586. public Gtk.Notebook _console_notebook;
  587. public Gtk.Notebook _project_notebook;
  588. public Gtk.Notebook _inspector_notebook;
  589. public Gtk.Paned _editor_pane;
  590. public Gtk.Paned _content_pane;
  591. public Gtk.Paned _inspector_pane;
  592. public Gtk.Paned _main_pane;
  593. public Statusbar _statusbar;
  594. public Gtk.Box _main_vbox;
  595. public Gtk.FileFilter _file_filter;
  596. public Gtk.ComboBoxText _combo;
  597. public NewProject _new_project;
  598. public ProjectsList _projects_list;
  599. public Gtk.Stack _main_stack;
  600. public Gtk.HeaderBar _header_bar;
  601. public LevelEditorWindow _level_editor_window;
  602. public uint _save_timer_id;
  603. public signal void ui_read_selection(Guid?[] selection);
  604. public LevelEditorApplication()
  605. {
  606. Object(application_id: "org.crownengine.Crown"
  607. , flags: GLib.ApplicationFlags.FLAGS_NONE
  608. );
  609. GLib.Environment.set_prgname(this.application_id); // FIXME: Drop after GTK4 port.
  610. }
  611. public Theme theme_name_to_enum(string theme)
  612. {
  613. if (theme == "dark")
  614. return Theme.DARK;
  615. else if (theme == "light")
  616. return Theme.LIGHT;
  617. else
  618. return Theme.COUNT;
  619. }
  620. public void set_theme_from_name(string theme_name)
  621. {
  622. Theme theme = theme_name_to_enum(theme_name);
  623. set_theme(theme);
  624. }
  625. public void set_theme(Theme theme)
  626. {
  627. if (theme == Theme.COUNT)
  628. return;
  629. string css = "/org/crownengine/Crown/ui/style-%s.css".printf(theme == Theme.DARK ? "dark" : "light");
  630. _css_provider.load_from_resource(css);
  631. }
  632. public override void startup()
  633. {
  634. base.startup();
  635. Intl.setlocale(LocaleCategory.ALL, "C");
  636. _css_provider = new Gtk.CssProvider();
  637. var default_screen = Gdk.Display.get_default().get_default_screen();
  638. Gtk.StyleContext.add_provider_for_screen(default_screen
  639. , _css_provider
  640. , Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
  641. );
  642. try {
  643. _settings = SJSON.load_from_path(_settings_file.get_path());
  644. } catch (JsonSyntaxError e) {
  645. loge(e.message);
  646. }
  647. try {
  648. _window_state = SJSON.load_from_path(_window_state_file.get_path());
  649. } catch (JsonSyntaxError e) {
  650. loge(e.message);
  651. }
  652. this.add_action_entries(action_entries_file, this);
  653. this.add_action_entries(action_entries_edit, this);
  654. this.add_action_entries(action_entries_create, this);
  655. this.add_action_entries(action_entries_camera, this);
  656. this.add_action_entries(action_entries_view, this);
  657. this.add_action_entries(action_entries_debug, this);
  658. this.add_action_entries(action_entries_help, this);
  659. this.add_action_entries(action_entries_project, this);
  660. this.add_action_entries(action_entries_package, this);
  661. this.add_action_entries(action_entries_unit, this);
  662. this.set_accels_for_action("app.new-level", { "<Primary>N" });
  663. this.set_accels_for_action("app.open-level", new string[] { "<Primary>O" });
  664. this.set_accels_for_action("app.save", new string[] { "<Primary>S" });
  665. this.set_accels_for_action("app.save-as", new string[] { "<Shift><Primary>S" });
  666. this.set_accels_for_action("app.import-null", new string[] { "<Primary>I" });
  667. this.set_accels_for_action("app.close-project", new string[] { "<Primary>W" });
  668. this.set_accels_for_action("app.quit", new string[] { "<Primary>Q" });
  669. this.set_accels_for_action("database.undo", new string[] { "<Primary>Z" });
  670. this.set_accels_for_action("database.redo", new string[] { "<Shift><Primary>Z" });
  671. this.set_accels_for_action("database.duplicate", new string[] { "<Primary>D" });
  672. this.set_accels_for_action("database.delete", new string[] { "Delete" });
  673. this.set_accels_for_action("database.select_none", new string[] { "<Shift><Primary>A" });
  674. this.set_accels_for_action("app.tool(0)", new string[] { "Q" });
  675. this.set_accels_for_action("app.tool(1)", new string[] { "W" });
  676. this.set_accels_for_action("app.tool(2)", new string[] { "E" });
  677. this.set_accels_for_action("app.tool(3)", new string[] { "R" });
  678. this.set_accels_for_action("app.grid-size(0)", new string[] { "<Primary>G" });
  679. this.set_accels_for_action("app.rotation-snap-size(0)", new string[] { "<Primary>H" });
  680. this.set_accels_for_action("viewport.camera-view(0)", new string[] { "KP_5" });
  681. this.set_accels_for_action("viewport.camera-view(1)", new string[] { "KP_1" });
  682. this.set_accels_for_action("viewport.camera-view(2)", new string[] { "<Primary>KP_1" });
  683. this.set_accels_for_action("viewport.camera-view(3)", new string[] { "KP_3" });
  684. this.set_accels_for_action("viewport.camera-view(4)", new string[] { "<Primary>KP_3" });
  685. this.set_accels_for_action("viewport.camera-view(5)", new string[] { "KP_7" });
  686. this.set_accels_for_action("viewport.camera-view(6)", new string[] { "<Primary>KP_7" });
  687. this.set_accels_for_action("viewport.camera-frame-selected", new string[] { "F" });
  688. this.set_accels_for_action("app.camera-frame-all", new string[] { "A" });
  689. this.set_accels_for_action("app.console", new string[] { "<Primary>grave" });
  690. this.set_accels_for_action("win.fullscreen", new string[] { "F11" });
  691. this.set_accels_for_action("app.test-level", new string[] { "F5" });
  692. this.set_accels_for_action("app.build-data", new string[] { "<Primary>B" });
  693. this.set_accels_for_action("app.reload-all", new string[] { "F7" });
  694. this.set_accels_for_action("app.manual", new string[] { "F1" });
  695. _tool_place_accels = this.get_accels_for_action("app.tool(0)");
  696. _tool_move_accels = this.get_accels_for_action("app.tool(1)");
  697. _tool_rotate_accels = this.get_accels_for_action("app.tool(2)");
  698. _tool_scale_accels = this.get_accels_for_action("app.tool(3)");
  699. _delete_accels = this.get_accels_for_action("app.delete");
  700. _camera_view_perspective_accels = this.get_accels_for_action("viewport.camera-view(0)");
  701. _camera_view_front_accels = this.get_accels_for_action("viewport.camera-view(1)");
  702. _camera_view_back_accels = this.get_accels_for_action("viewport.camera-view(2)");
  703. _camera_view_right_accels = this.get_accels_for_action("viewport.camera-view(3)");
  704. _camera_view_left_accels = this.get_accels_for_action("viewport.camera-view(4)");
  705. _camera_view_top_accels = this.get_accels_for_action("viewport.camera-view(5)");
  706. _camera_view_bottom_accels = this.get_accels_for_action("viewport.camera-view(6)");
  707. _camera_frame_selected_accels = this.get_accels_for_action("viewport.camera-frame-selected");
  708. _camera_frame_all_accels = this.get_accels_for_action("app.camera-frame-all");
  709. _runtimes = new Gee.ArrayList<RuntimeInstance>();
  710. _compiler = new RuntimeInstance("data_compiler");
  711. _compiler.message_received.connect(on_message_received);
  712. _compiler.connected.connect(on_runtime_connected);
  713. _compiler.disconnected.connect(on_runtime_disconnected);
  714. _compiler.disconnected_unexpected.connect(on_data_compiler_disconnected_unexpected);
  715. _data_compiler = new DataCompiler(_compiler);
  716. _data_compiler.start.connect(on_data_compiler_start);
  717. _data_compiler.finished.connect(on_data_compiler_finished);
  718. _project = new Project();
  719. _project.set_toolchain_dir(_toolchain_dir.get_path());
  720. _project.register_importer("Sprite", { "png" }, SpriteResource.import, on_import_result, 0.0);
  721. _project.register_importer("Mesh", { "mesh", "fbx" }, MeshResource.import, on_import_result, 1.0);
  722. _project.register_importer("Sound", { "wav", "ogg" }, SoundResource.import, on_import_result, 2.0);
  723. _project.register_importer("Texture", { "dds", "exr", "jpg", "ktx", "png", "pvr", "tga", }, TextureResource.import, on_import_result, 2.0);
  724. _project.register_importer("Font", { "ttf", "otf" }, FontResource.import, on_import_result, 3.0);
  725. _project.project_reset.connect(on_project_reset);
  726. _project.project_loaded.connect(on_project_loaded);
  727. _preferences_dialog = new PreferencesDialog();
  728. _preferences_dialog.delete_event.connect(_preferences_dialog.hide_on_delete);
  729. _preferences_dialog.decode(_settings);
  730. _database_editor = new DatabaseEditor(_project, (uint)_preferences_dialog._undo_redo_max_size.value * 1024 * 1024);
  731. _database_editor.undo.connect((id) => { _statusbar.set_temporary_message("Undo: " + ActionNames[id]); });
  732. _database_editor.redo.connect((id) => { _statusbar.set_temporary_message("Redo: " + ActionNames[id]); });
  733. _database_editor.selection_changed.connect(on_selection_changed);
  734. _editor_viewport = new EditorViewport("editor"
  735. , _database_editor
  736. , _project
  737. , LEVEL_EDITOR_BOOT_DIR
  738. , EDITOR_ADDRESS
  739. , EDITOR_TCP_PORT
  740. );
  741. _editor = _editor_viewport._runtime;
  742. _editor.message_received.connect(on_message_received);
  743. _editor.connected.connect(on_editor_connected);
  744. _editor.disconnected.connect(on_runtime_disconnected);
  745. _editor.disconnected_unexpected.connect(on_editor_disconnected_unexpected);
  746. _runtimes.add(_editor);
  747. _preferences_dialog.set_runtime(_editor);
  748. set_theme_from_name(_preferences_dialog._theme_combo.value);
  749. _game = new RuntimeInstance("game");
  750. _game.message_received.connect(on_message_received);
  751. _game.connected.connect(on_game_connected);
  752. _game.disconnected.connect(on_game_disconnected);
  753. _game.disconnected_unexpected.connect(on_game_disconnected);
  754. _runtimes.add(_game);
  755. _thumbnail = new RuntimeInstance("thumbnail");
  756. _thumbnail.message_received.connect(on_message_received);
  757. _thumbnail.connected.connect(on_runtime_connected);
  758. _thumbnail.disconnected.connect(on_runtime_disconnected);
  759. _thumbnail.disconnected_unexpected.connect(on_runtime_disconnected_unexpected);
  760. _runtimes.add(_thumbnail);
  761. _database = _database_editor._database;
  762. _database.objects_created.connect(on_objects_created);
  763. _database.objects_destroyed.connect(on_objects_destroyed);
  764. _database.objects_changed.connect(on_objects_changed);
  765. _database.object_type_added.connect(on_object_type_added);
  766. _properties_view = new PropertiesView(_database_editor);
  767. create_object_types(_database);
  768. _properties_view.register_object_type(OBJECT_TYPE_UNIT, new UnitView(_database));
  769. _level = new Level(_database, _editor);
  770. // Editor state
  771. _grid_size = 1.0;
  772. _rotation_snap = 15.0;
  773. _show_grid = true;
  774. _snap_to_grid = false;
  775. _debug_render_world = false;
  776. _debug_physics_world = false;
  777. _tool_type = ToolType.MOVE;
  778. _tool_type_prev = _tool_type;
  779. _snap_mode = SnapMode.RELATIVE;
  780. _reference_system = ReferenceSystem.LOCAL;
  781. // Project state
  782. _placeable_type = "";
  783. _placeable_name = "";
  784. _project_store = new ProjectStore(_project);
  785. // Widgets
  786. _combo = new Gtk.ComboBoxText();
  787. _combo.set_tooltip_text("Runtime that will receive the message.");
  788. _combo.append("editor", "Editor");
  789. _combo.append("game", "Game");
  790. _combo.set_active_id("editor");
  791. _combo.set_size_request(50, -1);
  792. _console_view = new ConsoleView(_project, _combo, _preferences_dialog);
  793. _thumbnail_cache = new ThumbnailCache(_project, _thumbnail, (uint)_preferences_dialog._thumbnail_cache_max_size.value * 1024 * 1024);
  794. _project_browser = new ProjectBrowser(_project_store, _thumbnail_cache);
  795. _level_treeview = new LevelTreeView(_database, _level);
  796. _level_treeview.selection_changed.connect(on_level_treeview_selection_changed);
  797. this.ui_read_selection.connect(_level_treeview.read_selection);
  798. this.ui_read_selection.connect(_properties_view.set_objects);
  799. _database.objects_created.connect(_level_treeview.on_objects_created);
  800. _database.objects_destroyed.connect(_level_treeview.on_objects_destroyed);
  801. _database.objects_changed.connect(_level_treeview.on_objects_changed);
  802. _level_layers_treeview = new LevelLayersTreeView(_database, _level);
  803. _level_tree_view_notebook = new Gtk.Notebook();
  804. _level_tree_view_notebook.show_border = false;
  805. _level_tree_view_notebook.append_page(_level_treeview, new Gtk.Image.from_icon_name("level-tree", Gtk.IconSize.SMALL_TOOLBAR));
  806. _level_tree_view_notebook.append_page(_level_layers_treeview, new Gtk.Image.from_icon_name("level-layers", Gtk.IconSize.SMALL_TOOLBAR));
  807. _inspector_notebook = new Gtk.Notebook();
  808. _inspector_notebook.show_border = false;
  809. _inspector_notebook.append_page(_properties_view, new Gtk.Label.with_mnemonic("Properties"));
  810. _inspector_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  811. _inspector_pane.pack1(_level_tree_view_notebook, true, false);
  812. _inspector_pane.pack2(_inspector_notebook, false, false);
  813. _project_stack = make_compiler_stack(_project_browser);
  814. _editor_stack = make_compiler_stack(_editor_viewport);
  815. _inspector_stack = make_compiler_stack(_inspector_pane);
  816. // Game run/stop button.
  817. _game_run_stop_image = new Gtk.Image.from_icon_name("game-run", Gtk.IconSize.MENU);
  818. _game_run_stop_image.margin_bottom
  819. = _game_run_stop_image.margin_end
  820. = _game_run_stop_image.margin_start
  821. = _game_run_stop_image.margin_top
  822. = 8
  823. ;
  824. _game_run = new Gtk.Button();
  825. _game_run.add(_game_run_stop_image);
  826. _game_run.get_style_context().add_class("suggested-action");
  827. _game_run.get_style_context().add_class("image-button");
  828. _game_run.action_name = "app.test-level";
  829. _game_run.can_focus = false;
  830. _toolbar = new Toolbar();
  831. _editor_viewport._overlay.add_overlay(_toolbar);
  832. _console_notebook = new Gtk.Notebook();
  833. _console_notebook.show_border = false;
  834. _console_notebook.append_page(_console_view, new Gtk.Label.with_mnemonic("Console"));
  835. _project_notebook = new Gtk.Notebook();
  836. _project_notebook.show_border = false;
  837. _project_notebook.append_page(_project_stack, new Gtk.Label.with_mnemonic("Project"));
  838. _content_pane = new Gtk.Paned(Gtk.Orientation.VERTICAL);
  839. _content_pane.pack1(_editor_stack, true, false);
  840. _content_pane.pack2(_console_notebook, false, false);
  841. _editor_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  842. _editor_pane.pack1(_project_notebook, false, false);
  843. _editor_pane.pack2(_content_pane, true, false);
  844. _main_pane = new Gtk.Paned(Gtk.Orientation.HORIZONTAL);
  845. _main_pane.pack1(_editor_pane, true, false);
  846. _main_pane.pack2(_inspector_stack, false, false);
  847. _statusbar = new Statusbar();
  848. _main_vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
  849. _main_vbox.pack_start(_main_pane, true, true, 0);
  850. _main_vbox.pack_start(_statusbar, false, false, 0);
  851. _main_vbox.set_visible(true);
  852. _file_filter = new Gtk.FileFilter();
  853. _file_filter.set_filter_name("Level (*.level)");
  854. _file_filter.add_pattern("*.level");
  855. _user = new User();
  856. _new_project = new NewProject(_user, _project);
  857. _new_project.fill_templates_list(_templates_dir.get_path());
  858. _projects_list = new ProjectsList(_user);
  859. _projects_list.set_visible(true); // To make Gtk.Stack work...
  860. _main_stack = new Gtk.Stack();
  861. _main_stack.add_named(new Gtk.Label("Waiting for %s...".printf(CROWN_SUBPROCESS_LAUNCHER)), PANEL_WAITING);
  862. _main_stack.add_named(_projects_list, PANEL_PROJECTS_LIST);
  863. _main_stack.add_named(_new_project, PANEL_NEW_PROJECT);
  864. _main_stack.add_named(_main_vbox, PANEL_EDITOR);
  865. _header_bar = new Gtk.HeaderBar();
  866. _header_bar.show_close_button = true;
  867. _header_bar.has_subtitle = false;
  868. _header_bar.pack_start(_game_run);
  869. // Delete expired logs
  870. if (_preferences_dialog._log_delete_after_days.value != 0) {
  871. try {
  872. FileEnumerator enumerator = _logs_dir.enumerate_children("standard::*"
  873. , FileQueryInfoFlags.NOFOLLOW_SYMLINKS
  874. );
  875. GLib.FileInfo info = null;
  876. while ((info = enumerator.next_file()) != null) {
  877. if (info.get_file_type() != GLib.FileType.REGULAR)
  878. continue; // Skip anything but regular files
  879. // Parse DateTime from log filename
  880. int year = 1970;
  881. int month = 1;
  882. int day = 1;
  883. if (info.get_name().scanf("%d-%d-%d.log", &year, &month, &day) != 3)
  884. continue; // Skip malformed filenames
  885. GLib.DateTime time_log = new GLib.DateTime.local(year, month, day, 0, 0, 0.0);
  886. if (time_log == null)
  887. continue; // Skip invalid dates
  888. GLib.DateTime time_now = new GLib.DateTime.now_local();
  889. if (time_now.difference(time_log) <= GLib.TimeSpan.DAY*_preferences_dialog._log_delete_after_days.value)
  890. continue; // Skip if date is within range
  891. // Delete
  892. GLib.File log_file = _logs_dir.resolve_relative_path(info.get_name());
  893. log_file.delete();
  894. }
  895. } catch (GLib.Error e) {
  896. loge(e.message);
  897. }
  898. }
  899. _user.load(_user_file.get_path());
  900. _console_view._entry_history.load(_console_history_file.get_path());
  901. show_panel(PANEL_WAITING);
  902. this.set_menubar(make_menubar());
  903. }
  904. public override void activate()
  905. {
  906. if (_level_editor_window == null) {
  907. _level_editor_window = new LevelEditorWindow(this, _header_bar);
  908. if (_window_state.has_key("level_editor_window"))
  909. _level_editor_window.decode((Hashtable)_window_state["level_editor_window"]);
  910. _level_editor_window.add(_main_stack);
  911. _level_editor_window.insert_action_group("viewport", _editor_viewport._action_group);
  912. _level_editor_window.insert_action_group("database", _database_editor._action_group);
  913. try {
  914. _level_editor_window.icon = Gtk.IconTheme.get_default().load_icon(CROWN_EDITOR_ICON_NAME, 256, 0);
  915. } catch (Error e) {
  916. loge(e.message);
  917. }
  918. }
  919. this.active_window.show_all();
  920. // Register a callback to be called when SubprocessLauncher service 'appears'.
  921. _launcher_watch_id = GLib.Bus.watch_name(GLib.BusType.SESSION
  922. , CROWN_SUBPROCESS_LAUNCHER
  923. , GLib.BusNameWatcherFlags.NONE
  924. , on_subprocess_launcher_appeared
  925. );
  926. }
  927. public void on_subprocess_launcher_appeared(GLib.DBusConnection connection, string name, string name_owner)
  928. {
  929. try {
  930. GLib.Bus.unwatch_name(_launcher_watch_id);
  931. // Connect to SubprocessLauncher service.
  932. _subprocess_launcher = GLib.Bus.get_proxy_sync(GLib.BusType.SESSION
  933. , CROWN_SUBPROCESS_LAUNCHER
  934. , "/org/crownengine/subprocess_launcher"
  935. );
  936. if (_source_dir == null) {
  937. if (_user.num_projects() == 0) {
  938. Project.create_temporary.begin((obj, res) => {
  939. string temp_source_dir = Project.create_temporary.end(res);
  940. if (temp_source_dir != null) {
  941. GLib.Application.get_default().activate_action("open-project"
  942. , new GLib.Variant.tuple({temp_source_dir, LEVEL_NONE, ProjectFlags.TEMPORARY})
  943. );
  944. } else {
  945. show_panel(PANEL_PROJECTS_LIST);
  946. }
  947. });
  948. } else {
  949. show_panel(PANEL_PROJECTS_LIST);
  950. }
  951. } else {
  952. show_panel(PANEL_EDITOR);
  953. restart_backend.begin();
  954. }
  955. } catch (IOError e) {
  956. loge(e.message);
  957. }
  958. }
  959. public override bool local_command_line(ref unowned string[] args, out int exit_status)
  960. {
  961. if (args.length > 1) {
  962. if (!GLib.FileUtils.test(args[1], FileTest.EXISTS) || !GLib.FileUtils.test(args[1], FileTest.IS_DIR)) {
  963. loge("Source directory does not exist or it is not a directory");
  964. exit_status = 1;
  965. return true;
  966. }
  967. _source_dir = args[1];
  968. }
  969. if (args.length > 2) {
  970. // Validation is done below after the Project object instantiation
  971. _level_resource = args[2];
  972. }
  973. exit_status = 0;
  974. return false;
  975. }
  976. public override int command_line(ApplicationCommandLine command_line)
  977. {
  978. this.activate();
  979. return 0;
  980. }
  981. public RuntimeInstance? current_selected_runtime()
  982. {
  983. if (_combo.get_active_id() == "editor")
  984. return _editor;
  985. else if (_combo.get_active_id() == "game")
  986. return _game;
  987. else
  988. return null;
  989. }
  990. public void on_runtime_connected(RuntimeInstance ri, string address, int port)
  991. {
  992. ri._revision = _data_compiler._revision;
  993. logi("Connected to %s@%s:%d".printf(ri._name, address, port));
  994. }
  995. public void on_runtime_disconnected(RuntimeInstance ri)
  996. {
  997. logi("Disconnected from %s".printf(ri._name));
  998. }
  999. public void on_runtime_disconnected_unexpected(RuntimeInstance ri)
  1000. {
  1001. logw("Disconnected from %s unexpectedly".printf(ri._name));
  1002. }
  1003. public async void on_data_compiler_disconnected_unexpected(RuntimeInstance ri)
  1004. {
  1005. on_runtime_disconnected_unexpected(ri);
  1006. yield stop_heads();
  1007. // Reset the callback
  1008. _data_compiler.compile_finished(false, 0);
  1009. _project_stack.set_visible_child_name(COMPILER_CRASHED);
  1010. _editor_stack.set_visible_child_name(COMPILER_CRASHED);
  1011. _inspector_stack.set_visible_child_name(COMPILER_CRASHED);
  1012. }
  1013. public void on_data_compiler_start()
  1014. {
  1015. _statusbar.set_status("Compiling data...");
  1016. }
  1017. public void on_data_compiler_finished(bool success)
  1018. {
  1019. _statusbar.clear_status();
  1020. if (!success) {
  1021. _statusbar.set_temporary_message("Failed to compile data");
  1022. return;
  1023. }
  1024. _project.data_compiled();
  1025. }
  1026. public void on_editor_connected(RuntimeInstance ri, string address, int port)
  1027. {
  1028. on_runtime_connected(ri, address, port);
  1029. // Update editor view with current editor state.
  1030. _level.send_level();
  1031. _database_editor.send_selection(_editor);
  1032. send_state();
  1033. _preferences_dialog.apply();
  1034. _editor.send(DeviceApi.frame());
  1035. }
  1036. public void on_editor_disconnected_unexpected(RuntimeInstance ri)
  1037. {
  1038. on_runtime_disconnected_unexpected(ri);
  1039. }
  1040. public void on_game_connected(RuntimeInstance ri, string address, int port)
  1041. {
  1042. on_runtime_connected(ri, address, port);
  1043. _combo.set_active_id("game");
  1044. }
  1045. public void on_game_disconnected(RuntimeInstance ri)
  1046. {
  1047. on_runtime_disconnected(ri);
  1048. _combo.set_active_id("editor");
  1049. _game_run_stop_image.set_from_icon_name("game-run", Gtk.IconSize.MENU);
  1050. }
  1051. public void on_message_received(RuntimeInstance ri, ConsoleClient client, uint8[] json)
  1052. {
  1053. try {
  1054. Hashtable msg = JSON.decode(json) as Hashtable;
  1055. handle_message(ri, client, (string)msg["type"], msg);
  1056. } catch (JsonSyntaxError e) {
  1057. loge(e.message);
  1058. }
  1059. }
  1060. public void handle_message(RuntimeInstance ri, ConsoleClient client, string msg_type, Hashtable msg)
  1061. {
  1062. if (msg_type == "message") {
  1063. string system = ri._name + ": " + (string)msg["system"];
  1064. log(system, (string)msg["severity"], (string)msg["message"]);
  1065. } else if (msg_type == "add_file") {
  1066. string path = (string)msg["path"];
  1067. uint64 size = uint64.parse((string)msg["size"]);
  1068. uint64 mtime = uint64.parse((string)msg["mtime"]);
  1069. _project.add_file(path, size, mtime);
  1070. } else if (msg_type == "remove_file") {
  1071. string path = (string)msg["path"];
  1072. _database.set(0, GUID_ZERO, path, null);
  1073. _project.remove_file(path);
  1074. } else if (msg_type == "add_tree") {
  1075. string path = (string)msg["path"];
  1076. _project.add_tree(path);
  1077. } else if (msg_type == "remove_tree") {
  1078. string path = (string)msg["path"];
  1079. _project.remove_tree(path);
  1080. } else if (msg_type == "change_file") {
  1081. string path = (string)msg["path"];
  1082. uint64 size = uint64.parse((string)msg["size"]);
  1083. uint64 mtime = uint64.parse((string)msg["mtime"]);
  1084. _database.set(0, GUID_ZERO, path, null);
  1085. _project.change_file(path, size, mtime);
  1086. } else if (msg_type == "compile") {
  1087. _data_compiler.message(msg);
  1088. } else if (msg_type == "refresh") {
  1089. ri.refresh_finished((bool)msg["success"]);
  1090. } else if (msg_type == "refresh_list") {
  1091. _data_compiler.refresh_list_finished((Gee.ArrayList<Value?>)msg["list"]);
  1092. } else if (msg_type == "unit_spawned") {
  1093. Guid id = Guid.parse((string)msg["id"]);
  1094. string name = (string)msg["name"];
  1095. Gee.ArrayList<Value?> pos = (Gee.ArrayList<Value?>)msg["position"];
  1096. Gee.ArrayList<Value?> rot = (Gee.ArrayList<Value?>)msg["rotation"];
  1097. Gee.ArrayList<Value?> scl = (Gee.ArrayList<Value?>)msg["scale"];
  1098. _level.on_unit_spawned(id
  1099. , name
  1100. , Vector3.from_array(pos)
  1101. , Quaternion.from_array(rot)
  1102. , Vector3.from_array(scl)
  1103. );
  1104. _database_editor.selection_set({ id });
  1105. _database.add_restore_point((int)ActionType.CREATE_OBJECTS, new Guid?[] { id }, ActionTypeFlags.FROM_SERVER);
  1106. } else if (msg_type == "sound_spawned") {
  1107. Guid id = Guid.parse((string)msg["id"]);
  1108. string name = (string)msg["name"];
  1109. Gee.ArrayList<Value?> pos = (Gee.ArrayList<Value?>)msg["position"];
  1110. Gee.ArrayList<Value?> rot = (Gee.ArrayList<Value?>)msg["rotation"];
  1111. Gee.ArrayList<Value?> scl = (Gee.ArrayList<Value?>)msg["scale"];
  1112. double range = (double)msg["range"];
  1113. double volume = (double)msg["volume"];
  1114. bool loop = (bool)msg["loop"];
  1115. _level.on_sound_spawned(id
  1116. , name
  1117. , Vector3.from_array(pos)
  1118. , Quaternion.from_array(rot)
  1119. , Vector3.from_array(scl)
  1120. , range
  1121. , volume
  1122. , loop
  1123. );
  1124. _database.add_restore_point((int)ActionType.CREATE_OBJECTS, new Guid?[] { id }, ActionTypeFlags.FROM_SERVER);
  1125. } else if (msg_type == "move_objects") {
  1126. Hashtable ids = (Hashtable)msg["ids"];
  1127. Hashtable new_positions = (Hashtable)msg["new_positions"];
  1128. Hashtable new_rotations = (Hashtable)msg["new_rotations"];
  1129. Hashtable new_scales = (Hashtable)msg["new_scales"];
  1130. Gee.ArrayList<string> keys = new Gee.ArrayList<string>.wrap(ids.keys.to_array());
  1131. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  1132. Guid?[] n_ids = new Guid?[keys.size];
  1133. Vector3[] n_positions = new Vector3[keys.size];
  1134. Quaternion[] n_rotations = new Quaternion[keys.size];
  1135. Vector3[] n_scales = new Vector3[keys.size];
  1136. for (int i = 0; i < keys.size; ++i) {
  1137. string k = keys[i];
  1138. n_ids[i] = Guid.parse((string)ids[k]);
  1139. n_positions[i] = Vector3.from_array((Gee.ArrayList<Value?>)(new_positions[k]));
  1140. n_rotations[i] = Quaternion.from_array((Gee.ArrayList<Value?>)new_rotations[k]);
  1141. n_scales[i] = Vector3.from_array((Gee.ArrayList<Value?>)new_scales[k]);
  1142. }
  1143. _level.on_move_objects(n_ids, n_positions, n_rotations, n_scales);
  1144. _database.add_restore_point((int)ActionType.CHANGE_OBJECTS, n_ids, ActionTypeFlags.FROM_SERVER);
  1145. } else if (msg_type == "selection") {
  1146. Hashtable objects = (Hashtable)msg["objects"];
  1147. Gee.ArrayList<string> keys = new Gee.ArrayList<string>.wrap(objects.keys.to_array());
  1148. keys.sort(Gee.Functions.get_compare_func_for(typeof(string)));
  1149. Guid?[] ids = new Guid?[keys.size];
  1150. for (int i = 0; i < keys.size; ++i) {
  1151. string k = keys[i];
  1152. ids[i] = Guid.parse((string)objects[k]);
  1153. }
  1154. _database_editor.selection_read(ids);
  1155. ui_read_selection(_database_editor._selection.to_array());
  1156. } else if (msg_type == "camera") {
  1157. if (ri == _editor)
  1158. _level.on_camera(msg);
  1159. } else if (msg_type == "error") {
  1160. loge((string)msg["message"]);
  1161. } else if (msg_type == "thumbnail") {
  1162. string resource_type = (string)msg["resource_type"];
  1163. string resource_name = (string)msg["resource_name"];
  1164. string path = (string)msg["path"];
  1165. _thumbnail_cache.thumbnail_ready(resource_type, resource_name, path);
  1166. } else {
  1167. loge("Unknown message type: " + msg_type);
  1168. }
  1169. }
  1170. public void append_editor_state(StringBuilder sb)
  1171. {
  1172. // This state is common to any project.
  1173. sb.append(LevelEditorApi.set_grid_size(_grid_size));
  1174. sb.append(LevelEditorApi.set_rotation_snap(_rotation_snap));
  1175. sb.append(LevelEditorApi.enable_show_grid(_show_grid));
  1176. sb.append(LevelEditorApi.enable_snap_to_grid(_snap_to_grid));
  1177. sb.append(LevelEditorApi.enable_debug_render_world(_debug_render_world));
  1178. sb.append(LevelEditorApi.enable_debug_physics_world(_debug_physics_world));
  1179. sb.append(LevelEditorApi.set_tool_type(_tool_type));
  1180. sb.append(LevelEditorApi.set_snap_mode(_snap_mode));
  1181. sb.append(LevelEditorApi.set_reference_system(_reference_system));
  1182. }
  1183. public void append_project_state(StringBuilder sb)
  1184. {
  1185. // This state is not guaranteed to be applicable to any project.
  1186. if (_placeable_type != "")
  1187. sb.append(LevelEditorApi.set_placeable(_placeable_type, _placeable_name));
  1188. }
  1189. public void send_state()
  1190. {
  1191. StringBuilder sb = new StringBuilder();
  1192. append_editor_state(sb);
  1193. append_project_state(sb);
  1194. _editor.send_script(sb.str);
  1195. }
  1196. public void on_objects_created(Guid?[] object_ids, uint32 flags)
  1197. {
  1198. if ((flags & ActionTypeFlags.FROM_SERVER) == 0) {
  1199. StringBuilder sb = new StringBuilder();
  1200. _level.generate_spawn_objects(sb, object_ids);
  1201. if (sb.len > 0) {
  1202. _editor.send_script(sb.str);
  1203. _editor_viewport.frame();
  1204. }
  1205. }
  1206. ui_read_selection(_database_editor._selection.to_array());
  1207. update_active_window_title();
  1208. }
  1209. public void on_objects_destroyed(Guid?[] object_ids, uint32 flags = 0)
  1210. {
  1211. if ((flags & ActionTypeFlags.FROM_SERVER) == 0) {
  1212. StringBuilder sb = new StringBuilder();
  1213. _level.generate_destroy_objects(sb, object_ids);
  1214. if (sb.len > 0) {
  1215. _editor.send_script(sb.str);
  1216. _editor_viewport.frame();
  1217. }
  1218. }
  1219. ui_read_selection(_database_editor._selection.to_array());
  1220. update_active_window_title();
  1221. }
  1222. public void on_objects_changed(Guid?[] object_ids, uint32 flags = 0)
  1223. {
  1224. if ((flags & ActionTypeFlags.FROM_SERVER) == 0) {
  1225. StringBuilder sb = new StringBuilder();
  1226. _level.generate_change_objects(sb, object_ids);
  1227. if (sb.len > 0) {
  1228. _editor.send_script(sb.str);
  1229. _editor_viewport.frame();
  1230. }
  1231. }
  1232. ui_read_selection(_database_editor._selection.to_array());
  1233. update_active_window_title();
  1234. }
  1235. public void on_object_type_added(ObjectTypeInfo info)
  1236. {
  1237. if ((info.flags & ObjectTypeFlags.UNIT_COMPONENT) != 0) {
  1238. Unit.register_component_type(info.name, info.user_data != null ? info.user_data : "");
  1239. _properties_view.register_object_type(info.name, null);
  1240. } else if (info.name != OBJECT_TYPE_UNIT) { // FIXME
  1241. _properties_view.register_object_type(info.name, null);
  1242. }
  1243. }
  1244. public async bool restart_backend()
  1245. {
  1246. yield stop_backend();
  1247. // Spawn the data compiler.
  1248. string args[] =
  1249. {
  1250. ENGINE_EXE,
  1251. "--source-dir",
  1252. _project.source_dir(),
  1253. "--data-dir",
  1254. _project.data_dir(),
  1255. "--map-source-dir",
  1256. "core",
  1257. _project.toolchain_dir(),
  1258. "--server",
  1259. "--wait-console"
  1260. };
  1261. try {
  1262. _compiler._process_id = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  1263. } catch (Error e) {
  1264. loge(e.message);
  1265. }
  1266. _project_stack.set_visible_child_name(COMPILER_CONNECTING);
  1267. _editor_stack.set_visible_child_name(COMPILER_CONNECTING);
  1268. _inspector_stack.set_visible_child_name(COMPILER_CONNECTING);
  1269. int tries = yield _compiler.connect_async(DATA_COMPILER_ADDRESS
  1270. , DATA_COMPILER_TCP_PORT
  1271. , DATA_COMPILER_CONNECTION_TRIES
  1272. , DATA_COMPILER_CONNECTION_INTERVAL
  1273. );
  1274. if (tries == DATA_COMPILER_CONNECTION_TRIES) {
  1275. loge("Cannot connect to data_compiler");
  1276. return false;
  1277. }
  1278. _project_stack.set_visible_child_name(COMPILER_COMPILING_DATA);
  1279. _editor_stack.set_visible_child_name(COMPILER_COMPILING_DATA);
  1280. _inspector_stack.set_visible_child_name(COMPILER_COMPILING_DATA);
  1281. // Compile data.
  1282. bool success = yield _data_compiler.compile(_project.data_dir(), _project.platform());
  1283. if (!success) {
  1284. _project_stack.set_visible_child_name(COMPILER_FAILED_COMPILATION);
  1285. _editor_stack.set_visible_child_name(COMPILER_FAILED_COMPILATION);
  1286. _inspector_stack.set_visible_child_name(COMPILER_FAILED_COMPILATION);
  1287. return success;
  1288. }
  1289. // If successful, start the level editor.
  1290. _project_stack.set_visible_child_name("child");
  1291. _editor_stack.set_visible_child_name("child");
  1292. _inspector_stack.set_visible_child_name("child");
  1293. yield _editor_viewport.restart_runtime();
  1294. yield start_thumbnail();
  1295. _project_stack.set_visible_child(_project_browser);
  1296. _inspector_stack.set_visible_child(_inspector_pane);
  1297. _project_browser.select_project_root();
  1298. return success;
  1299. }
  1300. public async void stop_heads()
  1301. {
  1302. yield _game.stop();
  1303. yield _thumbnail.stop();
  1304. yield _editor_viewport.stop_runtime();
  1305. }
  1306. public async void stop_backend()
  1307. {
  1308. _project_stack.set_visible_child_name(BACKEND_STOPPING);
  1309. _editor_stack.set_visible_child_name(BACKEND_STOPPING);
  1310. _inspector_stack.set_visible_child_name(BACKEND_STOPPING);
  1311. yield stop_heads();
  1312. yield stop_data_compiler();
  1313. }
  1314. public async void stop_data_compiler()
  1315. {
  1316. if (_compiler.is_connected())
  1317. _project.reset_files();
  1318. yield _compiler.stop();
  1319. }
  1320. public int dump_test_level()
  1321. {
  1322. try {
  1323. // Save temporary package to reference test level.
  1324. Gee.ArrayList<Value?> level = new Gee.ArrayList<Value?>();
  1325. level.add("_level_editor_test");
  1326. Hashtable package = new Hashtable();
  1327. package["level"] = level;
  1328. SJSON.save(package, _project._level_editor_test_package.get_path());
  1329. } catch (JsonWriteError e) {
  1330. return -1;
  1331. }
  1332. // Save test level to file.
  1333. return _database.dump(_project._level_editor_test_level.get_path(), _level._id);
  1334. }
  1335. public async void start_game(StartGame sg)
  1336. {
  1337. if (sg == StartGame.TEST && dump_test_level() != 0)
  1338. return;
  1339. bool success = yield _data_compiler.compile(_project.data_dir(), _project.platform());
  1340. _project.delete_garbage();
  1341. if (!success) {
  1342. _game_run_stop_image.set_from_icon_name("game-run", Gtk.IconSize.MENU);
  1343. return;
  1344. }
  1345. // Spawn the game.
  1346. string args[] =
  1347. {
  1348. ENGINE_EXE,
  1349. "--data-dir",
  1350. _project.data_dir(),
  1351. "--console-port",
  1352. GAME_TCP_PORT.to_string(),
  1353. "--wait-console",
  1354. "--lua-string",
  1355. sg == StartGame.TEST ? "TEST=true" : ""
  1356. };
  1357. try {
  1358. _game._process_id = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  1359. } catch (Error e) {
  1360. loge(e.message);
  1361. }
  1362. // Try to connect to the game.
  1363. int tries = yield _game.connect_async(GAME_ADDRESS
  1364. , GAME_TCP_PORT
  1365. , GAME_CONNECTION_TRIES
  1366. , GAME_CONNECTION_INTERVAL
  1367. );
  1368. if (tries == GAME_CONNECTION_TRIES) {
  1369. loge("Cannot connect to game");
  1370. return;
  1371. }
  1372. }
  1373. public async void start_thumbnail()
  1374. {
  1375. string args[] =
  1376. {
  1377. ENGINE_EXE,
  1378. "--data-dir",
  1379. _project.data_dir(),
  1380. "--boot-dir",
  1381. THUMBNAIL_BOOT_DIR,
  1382. "--console-port",
  1383. THUMBNAIL_TCP_PORT.to_string(),
  1384. "--wait-console",
  1385. "--pumped",
  1386. "--hidden"
  1387. };
  1388. try {
  1389. _thumbnail._process_id = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  1390. } catch (Error e) {
  1391. loge(e.message);
  1392. }
  1393. // Try to connect to the game.
  1394. int tries = yield _thumbnail.connect_async(THUMBNAIL_ADDRESS
  1395. , THUMBNAIL_TCP_PORT
  1396. , THUMBNAIL_CONNECTION_TRIES
  1397. , THUMBNAIL_CONNECTION_INTERVAL
  1398. );
  1399. if (tries == THUMBNAIL_CONNECTION_TRIES) {
  1400. loge("Cannot connect to thumbnail");
  1401. return;
  1402. }
  1403. }
  1404. public void on_tool(GLib.SimpleAction action, GLib.Variant? param)
  1405. {
  1406. ToolType type = (ToolType)param.get_int32();
  1407. if (type == ToolType.PLACE) {
  1408. // Store previous tool for it to be restored later.
  1409. if (_tool_type != ToolType.PLACE)
  1410. _tool_type_prev = _tool_type;
  1411. }
  1412. _tool_type = type;
  1413. _editor_viewport.grab_focus();
  1414. send_state();
  1415. _editor_viewport.frame();
  1416. action.set_state(param);
  1417. }
  1418. public void on_cancel_place(GLib.SimpleAction action, GLib.Variant? param)
  1419. {
  1420. if (_tool_type != ToolType.PLACE)
  1421. return;
  1422. activate_action("tool", new GLib.Variant.int32(_tool_type_prev));
  1423. }
  1424. public void on_snap(GLib.SimpleAction action, GLib.Variant? param)
  1425. {
  1426. _snap_mode = (SnapMode)param.get_int32();
  1427. send_state();
  1428. _editor_viewport.frame();
  1429. action.set_state(param);
  1430. }
  1431. public void on_reference_system(GLib.SimpleAction action, GLib.Variant? param)
  1432. {
  1433. _reference_system = (ReferenceSystem)param.get_int32();
  1434. send_state();
  1435. _editor_viewport.frame();
  1436. action.set_state(param);
  1437. }
  1438. public void on_grid_size(GLib.SimpleAction action, GLib.Variant? param)
  1439. {
  1440. int32 new_size = param.get_int32();
  1441. if (new_size != 0) {
  1442. _grid_size = (double)new_size / 10.0;
  1443. send_state();
  1444. _editor_viewport.frame();
  1445. action.set_state(param);
  1446. return;
  1447. }
  1448. // Custom grid size.
  1449. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Grid size"
  1450. , this.active_window
  1451. , Gtk.DialogFlags.MODAL
  1452. , "Cancel"
  1453. , Gtk.ResponseType.CANCEL
  1454. , "Ok"
  1455. , Gtk.ResponseType.OK
  1456. , null
  1457. );
  1458. InputDouble sb = new InputDouble(_grid_size, 0.1, 1000);
  1459. sb.value_changed.connect(() => { dg.response(Gtk.ResponseType.OK); });
  1460. dg.get_content_area().add(sb);
  1461. dg.response.connect((response_id) => {
  1462. if (response_id == Gtk.ResponseType.OK) {
  1463. _grid_size = sb.value;
  1464. send_state();
  1465. _editor_viewport.frame();
  1466. action.set_state(param);
  1467. }
  1468. dg.destroy();
  1469. });
  1470. dg.show_all();
  1471. }
  1472. public void update_active_window_title()
  1473. {
  1474. string title = "";
  1475. if (_level._name != null) {
  1476. if (_database.changed())
  1477. title += " • ";
  1478. title += (_level._name == LEVEL_EMPTY) ? "untitled" : _level._name;
  1479. title += " - ";
  1480. }
  1481. title += CROWN_EDITOR_NAME;
  1482. if (this.active_window.title != title)
  1483. this.active_window.title = title;
  1484. }
  1485. public void load_level(string name)
  1486. {
  1487. if (name == _level._name)
  1488. return;
  1489. if (_level.load(name) != 0) {
  1490. loge("Unable to load level %s".printf(name));
  1491. return;
  1492. }
  1493. if (_editor.is_connected()) {
  1494. _level.send_level();
  1495. send_state();
  1496. _editor_viewport.frame();
  1497. }
  1498. update_active_window_title();
  1499. _level_treeview.set_level(_level);
  1500. }
  1501. public bool do_save(string path)
  1502. {
  1503. string resource_filename = _project.resource_filename(path);
  1504. string resource_path = ResourceId.normalize(resource_filename);
  1505. string resource_name = ResourceId.name(resource_path);
  1506. if (_level.save(resource_name) != 0) {
  1507. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  1508. , Gtk.DialogFlags.MODAL
  1509. , Gtk.MessageType.ERROR
  1510. , Gtk.ButtonsType.NONE
  1511. , "Unable to save level '%s'".printf(resource_name)
  1512. );
  1513. md.add_button("_Ok", Gtk.ResponseType.OK);
  1514. md.set_default_response(Gtk.ResponseType.OK);
  1515. md.response.connect(() => { md.destroy(); });
  1516. md.show_all();
  1517. return false;
  1518. }
  1519. _statusbar.set_temporary_message("Saved %s".printf(_level._path));
  1520. update_active_window_title();
  1521. return true;
  1522. }
  1523. public void save_as(string? filename, string? success_action = null, GLib.Variant? variant = null)
  1524. {
  1525. string path = filename;
  1526. if (path != null) {
  1527. if (do_save(path) && success_action != null)
  1528. GLib.Application.get_default().activate_action(success_action, variant);
  1529. } else {
  1530. SaveResourceDialog srd = new SaveResourceDialog("Save As..."
  1531. , this.active_window
  1532. , OBJECT_TYPE_LEVEL
  1533. , ""
  1534. , _project
  1535. );
  1536. srd.safer_response.connect((response_id, path) => {
  1537. if (response_id == Gtk.ResponseType.ACCEPT && path != null) {
  1538. if (do_save(path) && success_action != null)
  1539. GLib.Application.get_default().activate_action(success_action, variant);
  1540. }
  1541. srd.destroy();
  1542. });
  1543. srd.show_all();
  1544. }
  1545. }
  1546. public void save(string? success_action = null, GLib.Variant? variant = null)
  1547. {
  1548. save_as(_level._path, success_action, variant);
  1549. }
  1550. public bool save_timeout()
  1551. {
  1552. if (_level._path != null)
  1553. save();
  1554. return GLib.Source.CONTINUE;
  1555. }
  1556. public Hashtable encode()
  1557. {
  1558. Hashtable json_obj = new Hashtable();
  1559. json_obj["level_editor_window"] = ((LevelEditorWindow)this.active_window).encode();
  1560. return json_obj;
  1561. }
  1562. public override void shutdown()
  1563. {
  1564. // Disable auto-save.
  1565. if (_save_timer_id > 0)
  1566. GLib.Source.remove(_save_timer_id);
  1567. // Save editor settings.
  1568. _user.save(_user_file.get_path());
  1569. _preferences_dialog.encode(_settings);
  1570. try {
  1571. SJSON.save(_settings, _settings_file.get_path());
  1572. } catch (JsonWriteError e) {
  1573. loge(e.message);
  1574. }
  1575. try {
  1576. SJSON.save(encode(), _window_state_file.get_path());
  1577. } catch (JsonWriteError e) {
  1578. loge(e.message);
  1579. }
  1580. _console_view._entry_history.save(_console_history_file.get_path());
  1581. if (_preferences_dialog != null)
  1582. _preferences_dialog.destroy();
  1583. base.shutdown();
  1584. }
  1585. public void do_new_level()
  1586. {
  1587. if (_level.create(LEVEL_EMPTY) != 0) {
  1588. loge("Unable to create a new level.");
  1589. return;
  1590. }
  1591. if (_editor.is_connected()) {
  1592. _level.send_level();
  1593. send_state();
  1594. _editor_viewport.frame();
  1595. }
  1596. update_active_window_title();
  1597. _level_treeview.set_level(_level);
  1598. }
  1599. public void on_new_level(GLib.SimpleAction action, GLib.Variant? param)
  1600. {
  1601. if (!_database.changed()) {
  1602. do_new_level();
  1603. } else {
  1604. Gtk.Dialog dlg = new_level_changed_dialog(this.active_window);
  1605. dlg.response.connect((response_id) => {
  1606. if (response_id == Gtk.ResponseType.NO)
  1607. do_new_level();
  1608. else if (response_id == Gtk.ResponseType.YES)
  1609. save("new-level");
  1610. dlg.destroy();
  1611. });
  1612. dlg.show_all();
  1613. }
  1614. }
  1615. public void do_open_level(string path)
  1616. {
  1617. string resource_filename = _project.resource_filename(path);
  1618. string resource_path = ResourceId.normalize(resource_filename);
  1619. string resource_name = ResourceId.name(resource_path);
  1620. load_level(resource_name);
  1621. }
  1622. public void on_open_level_from_menubar(GLib.SimpleAction action, GLib.Variant? param)
  1623. {
  1624. OpenResourceDialog dlg = new OpenResourceDialog("Open Level..."
  1625. , this.active_window
  1626. , OBJECT_TYPE_LEVEL
  1627. , _project
  1628. );
  1629. dlg.safer_response.connect((response_id, path) => {
  1630. if (response_id == Gtk.ResponseType.ACCEPT && path != null)
  1631. do_open_level(path);
  1632. dlg.destroy();
  1633. });
  1634. dlg.show_all();
  1635. }
  1636. public void on_open_level(GLib.SimpleAction action, GLib.Variant? param)
  1637. {
  1638. string level_name = param.get_string();
  1639. if (level_name != "" && level_name == _level._name)
  1640. return;
  1641. if (!_database.changed()) {
  1642. if (level_name != "")
  1643. load_level(level_name);
  1644. else // Action invoked from menubar File > Open Level...
  1645. on_open_level_from_menubar(action, param);
  1646. } else {
  1647. Gtk.Dialog dlg = new_level_changed_dialog(this.active_window);
  1648. dlg.response.connect((response_id) => {
  1649. if (response_id == Gtk.ResponseType.NO) {
  1650. if (level_name != "")
  1651. load_level(level_name);
  1652. else // Action invoked from menubar File > Open Level...
  1653. on_open_level_from_menubar(action, param);
  1654. } else if (response_id == Gtk.ResponseType.YES) {
  1655. save("open-level", param);
  1656. }
  1657. dlg.destroy();
  1658. });
  1659. dlg.show_all();
  1660. }
  1661. }
  1662. public void do_open_project(string source_dir, string? level_name, int flags)
  1663. {
  1664. if (_project.source_dir() == source_dir) {
  1665. logi("Project `%s` is open already.".printf(source_dir));
  1666. return;
  1667. }
  1668. logi("Loading project: `%s`...".printf(source_dir));
  1669. if (_project.load(source_dir) != 0) {
  1670. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  1671. , Gtk.DialogFlags.MODAL
  1672. , Gtk.MessageType.INFO
  1673. , Gtk.ButtonsType.OK
  1674. , "The folder `%s` does not appear to be a valid Crown project.".printf(source_dir)
  1675. );
  1676. md.set_default_response(Gtk.ResponseType.OK);
  1677. md.response.connect(() => { md.destroy(); });
  1678. md.show_all();
  1679. return;
  1680. }
  1681. _project._flags = flags;
  1682. this.show_panel(PANEL_EDITOR, Gtk.StackTransitionType.NONE);
  1683. if (!_project.is_temporary())
  1684. _user.add_or_touch_recent_project(source_dir, source_dir);
  1685. _console_view.reset();
  1686. logi("Project `%s` loaded.".printf(source_dir));
  1687. if (level_name == null)
  1688. do_new_level();
  1689. else
  1690. load_level(level_name);
  1691. restart_backend.begin();
  1692. }
  1693. public void open_project(string source_dir, string? level_name = null, int flags = ProjectFlags.NONE)
  1694. {
  1695. if (source_dir != "") {
  1696. do_open_project(source_dir, level_name, flags);
  1697. } else {
  1698. Gtk.FileChooserDialog dlg = new_open_project_dialog(this.active_window);
  1699. dlg.response.connect((response_id) => {
  1700. if (response_id == Gtk.ResponseType.ACCEPT)
  1701. do_open_project(dlg.get_file().get_path(), level_name, flags);
  1702. dlg.destroy();
  1703. });
  1704. dlg.show_all();
  1705. }
  1706. }
  1707. public void on_open_project(GLib.SimpleAction action, GLib.Variant? param)
  1708. {
  1709. string source_dir = "";
  1710. string level_name = LEVEL_EMPTY;
  1711. int flags = ProjectFlags.NONE;
  1712. if (param != null) {
  1713. source_dir = (string)param.get_child_value(0);
  1714. level_name = (string)param.get_child_value(1);
  1715. flags = (int)param.get_child_value(2);
  1716. }
  1717. if (!_database.changed()) {
  1718. open_project(source_dir, null, flags);
  1719. } else {
  1720. Gtk.Dialog dlg = new_level_changed_dialog(this.active_window);
  1721. dlg.response.connect((response_id) => {
  1722. if (response_id == Gtk.ResponseType.NO)
  1723. open_project(source_dir, level_name, ProjectFlags.NONE);
  1724. else if (response_id == Gtk.ResponseType.YES)
  1725. save("open-project", new GLib.Variant.tuple({source_dir, level_name, ProjectFlags.NONE}));
  1726. dlg.destroy();
  1727. });
  1728. dlg.show_all();
  1729. }
  1730. }
  1731. public void on_open_projects_list(GLib.SimpleAction action, GLib.Variant? param)
  1732. {
  1733. show_panel(PANEL_PROJECTS_LIST);
  1734. }
  1735. public void do_new_project()
  1736. {
  1737. reset_project();
  1738. stop_backend.begin((obj, res) => {
  1739. stop_backend.end(res);
  1740. show_panel(PANEL_NEW_PROJECT);
  1741. });
  1742. }
  1743. public void on_new_project(GLib.SimpleAction action, GLib.Variant? param)
  1744. {
  1745. if (!_database.changed()) {
  1746. do_new_project();
  1747. } else {
  1748. Gtk.Dialog dlg = new_level_changed_dialog(this.active_window);
  1749. dlg.response.connect((response_id) => {
  1750. if (response_id == Gtk.ResponseType.NO)
  1751. do_new_project();
  1752. else if (response_id == Gtk.ResponseType.YES)
  1753. save("new-project");
  1754. dlg.destroy();
  1755. });
  1756. dlg.show_all();
  1757. }
  1758. }
  1759. public void on_add_project(GLib.SimpleAction action, GLib.Variant? param)
  1760. {
  1761. Gtk.FileChooserDialog dlg = new_open_project_dialog(this.active_window);
  1762. dlg.response.connect((response_id) => {
  1763. if (response_id == Gtk.ResponseType.ACCEPT) {
  1764. string source_dir = dlg.get_file().get_path();
  1765. _user.add_or_touch_recent_project(source_dir, source_dir);
  1766. }
  1767. dlg.destroy();
  1768. });
  1769. dlg.show_all();
  1770. }
  1771. public void on_remove_project(GLib.SimpleAction action, GLib.Variant? param)
  1772. {
  1773. string source_dir = param.get_string();
  1774. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  1775. , Gtk.DialogFlags.MODAL
  1776. , Gtk.MessageType.WARNING
  1777. , Gtk.ButtonsType.NONE
  1778. , "Remove \"%s\" from the list?\n\nThis action removes the project from the list only, files on disk will not be deleted.".printf(source_dir)
  1779. );
  1780. md.add_button("_Cancel", Gtk.ResponseType.CANCEL);
  1781. md.add_button("_Remove", Gtk.ResponseType.YES);
  1782. md.set_default_response(Gtk.ResponseType.CANCEL);
  1783. md.response.connect((response_id) => {
  1784. if (response_id == Gtk.ResponseType.YES) {
  1785. _user.remove_recent_project(source_dir);
  1786. }
  1787. md.destroy();
  1788. });
  1789. md.show_all();
  1790. }
  1791. public void on_save(GLib.SimpleAction action, GLib.Variant? param)
  1792. {
  1793. save();
  1794. }
  1795. public void on_save_as(GLib.SimpleAction action, GLib.Variant? param)
  1796. {
  1797. save_as(null);
  1798. }
  1799. public void on_import_result(ImportResult result)
  1800. {
  1801. if (result == ImportResult.ERROR) {
  1802. loge("Failed to import resource(s)");
  1803. return;
  1804. } else if (result == ImportResult.SUCCESS) {
  1805. compile_and_reload.begin();
  1806. }
  1807. ui_read_selection(_database_editor._selection.to_array());
  1808. }
  1809. public void on_import(GLib.SimpleAction action, GLib.Variant? param)
  1810. {
  1811. string? destination_dir = null;
  1812. string[] filenames = {};
  1813. if (param != null) {
  1814. destination_dir = (string)param.get_child_value(0);
  1815. filenames = (string[])param.get_child_value(1);
  1816. }
  1817. _project.import(destination_dir
  1818. , filenames
  1819. , on_import_result
  1820. , _database
  1821. , this.active_window
  1822. );
  1823. }
  1824. public void on_preferences(GLib.SimpleAction action, GLib.Variant? param)
  1825. {
  1826. _preferences_dialog.set_transient_for(_level_editor_window);
  1827. _preferences_dialog.show_all();
  1828. _preferences_dialog.present();
  1829. }
  1830. public void on_deploy(GLib.SimpleAction action, GLib.Variant? param)
  1831. {
  1832. if (_deploy_dialog == null) {
  1833. _deploy_dialog = new DeployDialog(_project, _editor);
  1834. _deploy_dialog.set_transient_for(_level_editor_window);
  1835. _deploy_dialog.delete_event.connect(_deploy_dialog.hide_on_delete);
  1836. }
  1837. _deploy_dialog.show_all();
  1838. _deploy_dialog.present();
  1839. }
  1840. public void on_texture_settings(GLib.SimpleAction action, GLib.Variant? param)
  1841. {
  1842. string texture_name = param.get_string();
  1843. if (_texture_settings_dialog == null) {
  1844. _texture_settings_dialog = new TextureSettingsDialog(_project, _database);
  1845. _texture_settings_dialog.set_transient_for(_level_editor_window);
  1846. _texture_settings_dialog.delete_event.connect(_texture_settings_dialog.hide_on_delete);
  1847. _texture_settings_dialog.texture_saved.connect(() => {
  1848. compile_and_reload.begin();
  1849. });
  1850. }
  1851. _texture_settings_dialog.show_all();
  1852. _texture_settings_dialog.present();
  1853. _texture_settings_dialog.set_texture(texture_name);
  1854. }
  1855. public void on_state_machine_editor(GLib.SimpleAction action, GLib.Variant? param)
  1856. {
  1857. string state_machine_name = param.get_string();
  1858. if (_state_machine_editor == null) {
  1859. _state_machine_editor = new StateMachineEditor(this
  1860. , _project
  1861. , "core/editors/state_machine_editor"
  1862. , "127.0.0.1"
  1863. , 10844
  1864. , (uint)_preferences_dialog._undo_redo_max_size.value * 1024 * 1024
  1865. );
  1866. _state_machine_editor.set_transient_for(_level_editor_window);
  1867. _state_machine_editor.saved.connect(() => {
  1868. compile_and_reload.begin();
  1869. });
  1870. _runtimes.add(_state_machine_editor._runtime);
  1871. this.add_window(_state_machine_editor);
  1872. }
  1873. _state_machine_editor.show_all();
  1874. _state_machine_editor.present();
  1875. _state_machine_editor.set_state_machine(state_machine_name);
  1876. }
  1877. public void on_open_object(GLib.SimpleAction action, GLib.Variant? param)
  1878. {
  1879. string resource_type = (string)param.get_child_value(0);
  1880. string resource_name = (string)param.get_child_value(1);
  1881. if (_object_editor == null) {
  1882. _object_editor = new ObjectEditor(this
  1883. , _project
  1884. , (uint)_preferences_dialog._undo_redo_max_size.value * 1024 * 1024
  1885. );
  1886. _object_editor.set_transient_for(_level_editor_window);
  1887. _object_editor.saved.connect(() => {
  1888. compile_and_reload.begin();
  1889. });
  1890. this.add_window(_object_editor);
  1891. }
  1892. _object_editor.show_all();
  1893. _object_editor.present();
  1894. _object_editor.set_object(resource_type, resource_name);
  1895. }
  1896. public void on_reveal(GLib.SimpleAction action, GLib.Variant? param)
  1897. {
  1898. string type = (string)param.get_child_value(0);
  1899. string name = (string)param.get_child_value(1);
  1900. if (!_project_notebook.is_visible())
  1901. _project_notebook.show_all();
  1902. _project_browser.reveal(type, name);
  1903. }
  1904. public Gtk.Dialog new_level_changed_dialog(Gtk.Window? parent)
  1905. {
  1906. return new_resource_changed_dialog(parent, "Level");
  1907. }
  1908. public Gtk.FileChooserDialog new_open_project_dialog(Gtk.Window? parent)
  1909. {
  1910. return new Gtk.FileChooserDialog("Open Project..."
  1911. , parent
  1912. , Gtk.FileChooserAction.SELECT_FOLDER
  1913. , "Cancel"
  1914. , Gtk.ResponseType.CANCEL
  1915. , "Open"
  1916. , Gtk.ResponseType.ACCEPT
  1917. );
  1918. }
  1919. public void reset_project()
  1920. {
  1921. _placeable_type = OBJECT_TYPE_UNIT;
  1922. _placeable_name = "core/units/primitives/cube";
  1923. _database_editor.clear_selection();
  1924. _level.reset();
  1925. _project.reset();
  1926. this.active_window.title = CROWN_EDITOR_NAME;
  1927. }
  1928. public Gtk.Dialog new_save_project_dialog(Gtk.Window? parent)
  1929. {
  1930. Gtk.MessageDialog md = new Gtk.MessageDialog(parent
  1931. , Gtk.DialogFlags.MODAL
  1932. , Gtk.MessageType.WARNING
  1933. , Gtk.ButtonsType.NONE
  1934. , "Save the project before closing?\n\nThis is a *temporary* project and will be gone if not saved now."
  1935. );
  1936. Gtk.Widget btn;
  1937. btn = md.add_button("Close _without Saving", Gtk.ResponseType.NO);
  1938. btn.get_style_context().add_class("destructive-action");
  1939. md.add_button("_Cancel", Gtk.ResponseType.CANCEL);
  1940. md.add_button("_Save", Gtk.ResponseType.YES);
  1941. md.set_default_response(Gtk.ResponseType.YES);
  1942. return md;
  1943. }
  1944. public Gtk.FileChooserDialog new_select_project_folder_dialog(Gtk.Window? parent)
  1945. {
  1946. return new Gtk.FileChooserDialog("Select a folder where the project will be saved..."
  1947. , parent
  1948. , Gtk.FileChooserAction.SELECT_FOLDER
  1949. , "Cancel"
  1950. , Gtk.ResponseType.CANCEL
  1951. , "Save Project Here"
  1952. , Gtk.ResponseType.ACCEPT
  1953. );
  1954. }
  1955. public void do_close_project(bool quit)
  1956. {
  1957. reset_project();
  1958. stop_backend.begin((obj, res) => {
  1959. stop_backend.end(res);
  1960. if (quit)
  1961. this.quit();
  1962. else
  1963. show_panel(PANEL_PROJECTS_LIST);
  1964. });
  1965. }
  1966. public int do_save_project(GLib.File destination_dir, string? new_project_name = null)
  1967. {
  1968. string name = new_project_name != null
  1969. ? new_project_name
  1970. : _project.name()
  1971. ;
  1972. string new_source_dir = Path.build_path(Path.DIR_SEPARATOR_S
  1973. , destination_dir.get_path()
  1974. , name
  1975. );
  1976. if (!GLib.File.new_for_path(new_source_dir).make_directory(null))
  1977. return -1;
  1978. if (copy_tree(GLib.File.new_for_path(new_source_dir), GLib.File.new_for_path(_project.source_dir())) != 0)
  1979. return -1;
  1980. _user.add_or_touch_recent_project(new_source_dir, name);
  1981. return 0;
  1982. }
  1983. public void save_project(bool quit)
  1984. {
  1985. assert(_project.is_temporary());
  1986. Gtk.FileChooserDialog dlg = new_select_project_folder_dialog(this.active_window);
  1987. dlg.response.connect((response_id) => {
  1988. if (response_id == Gtk.ResponseType.ACCEPT) {
  1989. GLib.File dest_dir = dlg.get_file();
  1990. if (!is_directory_empty(dest_dir.get_path())) {
  1991. Gtk.MessageDialog md = new Gtk.MessageDialog(dlg
  1992. , Gtk.DialogFlags.MODAL
  1993. , Gtk.MessageType.ERROR
  1994. , Gtk.ButtonsType.NONE
  1995. , "The destination directory must be empty"
  1996. );
  1997. md.add_button("_Ok", Gtk.ResponseType.OK);
  1998. md.set_default_response(Gtk.ResponseType.OK);
  1999. md.response.connect(() => { md.destroy(); });
  2000. md.show_all();
  2001. return;
  2002. }
  2003. if (do_save_project(dest_dir) != 0) {
  2004. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  2005. , Gtk.DialogFlags.MODAL
  2006. , Gtk.MessageType.ERROR
  2007. , Gtk.ButtonsType.NONE
  2008. , "Failed to save project in " + dest_dir.get_path()
  2009. );
  2010. md.add_button("_Ok", Gtk.ResponseType.OK);
  2011. md.set_default_response(Gtk.ResponseType.OK);
  2012. md.response.connect(() => { md.destroy(); });
  2013. md.show_all();
  2014. } else {
  2015. do_close_project(quit);
  2016. }
  2017. }
  2018. dlg.destroy();
  2019. });
  2020. dlg.show_all();
  2021. }
  2022. public void close_project(bool quit = false)
  2023. {
  2024. if (_project.is_temporary()) {
  2025. Gtk.Dialog dlg = new_save_project_dialog(this.active_window);
  2026. dlg.response.connect((response_id) => {
  2027. if (response_id == Gtk.ResponseType.NO)
  2028. do_close_project(quit);
  2029. else if (response_id == Gtk.ResponseType.YES)
  2030. save_project(quit);
  2031. dlg.destroy();
  2032. });
  2033. dlg.show_all();
  2034. } else {
  2035. do_close_project(quit);
  2036. }
  2037. }
  2038. public void on_close_project(GLib.SimpleAction action, GLib.Variant? param)
  2039. {
  2040. if (!_database.changed()) {
  2041. close_project();
  2042. } else {
  2043. Gtk.Dialog dlg = new_level_changed_dialog(this.active_window);
  2044. dlg.response.connect((response_id) => {
  2045. if (response_id == Gtk.ResponseType.NO)
  2046. close_project();
  2047. else if (response_id == Gtk.ResponseType.YES)
  2048. save("close-project");
  2049. dlg.destroy();
  2050. });
  2051. dlg.show_all();
  2052. }
  2053. }
  2054. public void on_quit(GLib.SimpleAction action, GLib.Variant? param)
  2055. {
  2056. if (!_database.changed()) {
  2057. close_project(true);
  2058. } else {
  2059. Gtk.Dialog dlg = new_level_changed_dialog(this.active_window);
  2060. dlg.response.connect((response_id) => {
  2061. if (response_id == Gtk.ResponseType.NO)
  2062. close_project(true);
  2063. else if (response_id == Gtk.ResponseType.YES)
  2064. save("quit");
  2065. dlg.destroy();
  2066. });
  2067. dlg.show_all();
  2068. }
  2069. }
  2070. public void on_open_resource(GLib.SimpleAction action, GLib.Variant? param)
  2071. {
  2072. if (param == null)
  2073. return;
  2074. GLib.AppInfo? app = null;
  2075. string resource_path = param.get_string();
  2076. string? resource_type = ResourceId.type(resource_path);
  2077. string? resource_name = ResourceId.name(resource_path);
  2078. if (resource_type == null || resource_name == null)
  2079. return;
  2080. if (resource_type == OBJECT_TYPE_LEVEL) {
  2081. activate_action("open-level", resource_name);
  2082. return;
  2083. } else if (resource_type == OBJECT_TYPE_TEXTURE) {
  2084. activate_action("texture-settings", resource_name);
  2085. return;
  2086. } else if (resource_type == OBJECT_TYPE_UNIT) {
  2087. activate_action("open-unit", resource_name);
  2088. return;
  2089. } else if (resource_type == OBJECT_TYPE_STATE_MACHINE) {
  2090. activate_action("state-machine-editor", resource_name);
  2091. return;
  2092. } else if (resource_type == "lua") {
  2093. app = _preferences_dialog._external_editor_lua.get_app_info();
  2094. } else if (_project.is_type_image(resource_type)) {
  2095. app = _preferences_dialog._external_editor_image.get_app_info();
  2096. } else if (_project.is_type_mesh(resource_path)) {
  2097. app = _preferences_dialog._external_editor_model.get_app_info();
  2098. } else if (_project.is_type_sound(resource_path)) {
  2099. app = _preferences_dialog._external_editor_sound.get_app_info();
  2100. } else if (_project.is_type_font(resource_path)) {
  2101. app = _preferences_dialog._external_editor_font.get_app_info();
  2102. } else if (_database.has_type(StringId64(resource_type))) {
  2103. activate_action("open-object", new GLib.Variant.tuple({ resource_type, resource_name }));
  2104. return;
  2105. }
  2106. GLib.File file = GLib.File.new_for_path(_project.absolute_path(resource_path));
  2107. try {
  2108. if (app == null)
  2109. app = file.query_default_handler();
  2110. } catch (GLib.Error e) {
  2111. // Ignore.
  2112. }
  2113. if (app == null)
  2114. app = GLib.AppInfo.get_default_for_type("text/plain", false);
  2115. if (app != null) {
  2116. GLib.List<GLib.File> files = new GLib.List<GLib.File>();
  2117. files.append(file);
  2118. try {
  2119. app.launch(files, null);
  2120. } catch (GLib.Error e) {
  2121. open_text_editor(file.get_path());
  2122. }
  2123. } else {
  2124. open_text_editor(file.get_path());
  2125. }
  2126. }
  2127. public void copy_string(string str)
  2128. {
  2129. var clip = Gtk.Clipboard.get_default(Gdk.Display.get_default());
  2130. clip.set_text(str, str.length);
  2131. #if !CROWN_PLATFORM_WINDOWS
  2132. clip.store();
  2133. #endif
  2134. }
  2135. public void on_copy_path(GLib.SimpleAction action, GLib.Variant? param)
  2136. {
  2137. string path = param.get_string();
  2138. copy_string(_project.absolute_path(path));
  2139. }
  2140. public void on_copy_name(GLib.SimpleAction action, GLib.Variant? param)
  2141. {
  2142. copy_string(param.get_string());
  2143. }
  2144. public void on_show_grid(GLib.SimpleAction action, GLib.Variant? param)
  2145. {
  2146. _show_grid = !action.get_state().get_boolean();
  2147. send_state();
  2148. _editor_viewport.frame();
  2149. action.set_state(new GLib.Variant.boolean(_show_grid));
  2150. }
  2151. public void on_rotation_snap_size(GLib.SimpleAction action, GLib.Variant? param)
  2152. {
  2153. int32 new_size = param.get_int32();
  2154. if (new_size != 0) {
  2155. _rotation_snap = (double)new_size;
  2156. send_state();
  2157. _editor_viewport.frame();
  2158. action.set_state(param);
  2159. return;
  2160. }
  2161. // Custom rotation size.
  2162. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Rotation snap"
  2163. , this.active_window
  2164. , Gtk.DialogFlags.MODAL
  2165. , "Cancel"
  2166. , Gtk.ResponseType.CANCEL
  2167. , "Ok"
  2168. , Gtk.ResponseType.OK
  2169. , null
  2170. );
  2171. InputDouble sb = new InputDouble(_rotation_snap, 1.0, 180.0);
  2172. sb.value_changed.connect(() => { dg.response(Gtk.ResponseType.OK); });
  2173. dg.get_content_area().add(sb);
  2174. dg.response.connect((response_id) => {
  2175. if (response_id == Gtk.ResponseType.OK) {
  2176. _rotation_snap = sb.value;
  2177. send_state();
  2178. _editor_viewport.frame();
  2179. action.set_state(param);
  2180. }
  2181. dg.destroy();
  2182. });
  2183. dg.show_all();
  2184. }
  2185. public void on_spawn_primitive(GLib.SimpleAction action, GLib.Variant? param)
  2186. {
  2187. GLib.Variant[] paramz;
  2188. if (action.name == "primitive-cube")
  2189. paramz = { OBJECT_TYPE_UNIT, "core/units/primitives/cube" };
  2190. else if (action.name == "primitive-sphere")
  2191. paramz = { OBJECT_TYPE_UNIT, "core/units/primitives/sphere" };
  2192. else if (action.name == "primitive-cone")
  2193. paramz = { OBJECT_TYPE_UNIT, "core/units/primitives/cone" };
  2194. else if (action.name == "primitive-cylinder")
  2195. paramz = { OBJECT_TYPE_UNIT, "core/units/primitives/cylinder" };
  2196. else if (action.name == "primitive-plane")
  2197. paramz = { OBJECT_TYPE_UNIT, "core/units/primitives/plane" };
  2198. else if (action.name == "camera")
  2199. paramz = { OBJECT_TYPE_UNIT, "core/units/camera" };
  2200. else if (action.name == "light")
  2201. paramz = { OBJECT_TYPE_UNIT, "core/units/light" };
  2202. else if (action.name == "sound-source")
  2203. paramz = { OBJECT_TYPE_SOUND, "sound" };
  2204. else
  2205. paramz = { OBJECT_TYPE_UNIT, "core/units/primitives/cube" };
  2206. activate_action("set-placeable", new GLib.Variant.tuple(paramz));
  2207. }
  2208. public void on_spawn_unit(GLib.SimpleAction action, GLib.Variant? param)
  2209. {
  2210. string? unit_name;
  2211. if (action.name == "empty-unit")
  2212. unit_name = null;
  2213. else if (action.name == "shading-environment")
  2214. unit_name = "core/renderer/default_shading_environment";
  2215. else
  2216. unit_name = null;
  2217. _level.spawn_unit(unit_name);
  2218. _editor_viewport.frame();
  2219. }
  2220. public void on_camera_frame_all(GLib.SimpleAction action, GLib.Variant? param)
  2221. {
  2222. Gee.ArrayList<Guid?> all_objects = new Gee.ArrayList<Guid?>();
  2223. _level.objects(ref all_objects);
  2224. _editor.send_script(LevelEditorApi.frame_objects(all_objects.to_array()));
  2225. _editor_viewport.frame();
  2226. }
  2227. public void on_project_browser(GLib.SimpleAction action, GLib.Variant? param)
  2228. {
  2229. if (_project_notebook.is_visible()) {
  2230. _project_notebook.hide();
  2231. } else {
  2232. _project_notebook.show_all();
  2233. }
  2234. }
  2235. public void on_console(GLib.SimpleAction action, GLib.Variant? param)
  2236. {
  2237. if (_console_notebook.is_visible()) {
  2238. if (_console_view._entry._entry.has_focus)
  2239. _console_notebook.hide();
  2240. else
  2241. _console_view._entry._entry.grab_focus_without_selecting();
  2242. } else {
  2243. _console_notebook.show_all();
  2244. _console_view._entry._entry.grab_focus_without_selecting();
  2245. }
  2246. }
  2247. public void on_statusbar(GLib.SimpleAction action, GLib.Variant? param)
  2248. {
  2249. if (_statusbar.is_visible()) {
  2250. _statusbar.hide();
  2251. } else {
  2252. _statusbar.show_all();
  2253. }
  2254. }
  2255. public void on_inspector(GLib.SimpleAction action, GLib.Variant? param)
  2256. {
  2257. if (_inspector_stack.is_visible()) {
  2258. _inspector_stack.hide();
  2259. } else {
  2260. _inspector_stack.show_all();
  2261. }
  2262. }
  2263. public void on_restart_backend(GLib.SimpleAction action, GLib.Variant? param)
  2264. {
  2265. restart_backend.begin((obj, res) => {
  2266. restart_backend.end(res);
  2267. });
  2268. }
  2269. public void on_restart_editor_view(GLib.SimpleAction action, GLib.Variant? param)
  2270. {
  2271. _editor_viewport.restart_runtime.begin((obj, res) => {
  2272. _editor_viewport.restart_runtime.end(res);
  2273. });
  2274. }
  2275. public void on_build_data(GLib.SimpleAction action, GLib.Variant? param)
  2276. {
  2277. _data_compiler.compile.begin(_project.data_dir(), _project.platform(), (obj, res) => {
  2278. _data_compiler.compile.end(res);
  2279. });
  2280. }
  2281. public void on_reload_all(GLib.SimpleAction action, GLib.Variant? param)
  2282. {
  2283. compile_and_reload.begin();
  2284. }
  2285. public async bool refresh_all_clients()
  2286. {
  2287. bool success = true;
  2288. foreach (var ri in _runtimes)
  2289. if (ri.is_connected() && !yield ri.refresh(_data_compiler))
  2290. success = false;
  2291. if (success) {
  2292. _project_browser.queue_draw();
  2293. // Apply editor changes to reloaded units.
  2294. var sb = new GLib.StringBuilder();
  2295. Gee.ArrayList<Guid?> unit_ids = new Gee.ArrayList<Guid?>();
  2296. _level.units(ref unit_ids);
  2297. Unit.generate_change_commands(sb, unit_ids.to_array(), _database);
  2298. foreach (var id in unit_ids)
  2299. sb.append(LevelEditorApi.unit_freeze(id));
  2300. _editor.send_script(sb.str);
  2301. _editor_viewport.frame();
  2302. }
  2303. return success;
  2304. }
  2305. public void on_snap_to_grid(GLib.SimpleAction action, GLib.Variant? param)
  2306. {
  2307. _snap_to_grid = !action.get_state().get_boolean();
  2308. send_state();
  2309. _editor_viewport.frame();
  2310. action.set_state(new GLib.Variant.boolean(_snap_to_grid));
  2311. }
  2312. public void on_debug_render_world(GLib.SimpleAction action, GLib.Variant? param)
  2313. {
  2314. _debug_render_world = !action.get_state().get_boolean();
  2315. send_state();
  2316. _editor_viewport.frame();
  2317. action.set_state(new GLib.Variant.boolean(_debug_render_world));
  2318. }
  2319. public void on_debug_physics_world(GLib.SimpleAction action, GLib.Variant? param)
  2320. {
  2321. _debug_physics_world = !action.get_state().get_boolean();
  2322. send_state();
  2323. _editor_viewport.frame();
  2324. action.set_state(new GLib.Variant.boolean(_debug_physics_world));
  2325. }
  2326. public void on_run_game(GLib.SimpleAction action, GLib.Variant? param)
  2327. {
  2328. // Trigger a 'focus_out_event' on the currently focused
  2329. // widget within the active_window, if any. This will
  2330. // force 'focus' to commit its pending changes to the
  2331. // database so we do not miss any modifications before
  2332. // launching the game.
  2333. Gtk.Widget? focus = this.active_window.get_focus();
  2334. _editor_viewport.grab_focus();
  2335. if (focus != null)
  2336. focus.grab_focus();
  2337. var icon_displayed = _game_run_stop_image.icon_name;
  2338. _game.stop.begin((obj, res) => {
  2339. _game.stop.end(res);
  2340. if (icon_displayed == "game-run") {
  2341. // Always change icon state regardless of failures.
  2342. _game_run_stop_image.set_from_icon_name("game-stop", Gtk.IconSize.MENU);
  2343. start_game.begin(action.name == "test-level" ? StartGame.TEST : StartGame.NORMAL);
  2344. }
  2345. });
  2346. }
  2347. public void do_rename(Guid object_id, string new_name)
  2348. {
  2349. if (new_name != "" && _database.name(object_id) != new_name) {
  2350. _database.set_name(object_id, new_name);
  2351. _database.add_restore_point((int)ActionType.CHANGE_OBJECTS, new Guid?[] { object_id });
  2352. }
  2353. }
  2354. public void on_rename(GLib.SimpleAction action, GLib.Variant? param)
  2355. {
  2356. Guid object_id = Guid.parse((string)param.get_child_value(0));
  2357. string new_name = (string)param.get_child_value(1);
  2358. if (new_name != "") {
  2359. do_rename(object_id, new_name);
  2360. } else {
  2361. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("New Name"
  2362. , this.active_window
  2363. , Gtk.DialogFlags.MODAL
  2364. , "Cancel"
  2365. , Gtk.ResponseType.CANCEL
  2366. , "Ok"
  2367. , Gtk.ResponseType.OK
  2368. , null
  2369. );
  2370. InputString sb = new InputString();
  2371. sb.value_changed.connect(() => { dg.response(Gtk.ResponseType.OK); });
  2372. sb.value = _database.name(object_id);
  2373. dg.get_content_area().add(sb);
  2374. dg.response.connect((response_id) => {
  2375. if (response_id == Gtk.ResponseType.OK)
  2376. do_rename(object_id, sb.value.strip());
  2377. dg.destroy();
  2378. });
  2379. dg.show_all();
  2380. }
  2381. }
  2382. public void on_manual(GLib.SimpleAction action, GLib.Variant? param)
  2383. {
  2384. try {
  2385. AppInfo.launch_default_for_uri(CROWN_LATEST_DOCS_URL, null);
  2386. } catch (Error e) {
  2387. loge(e.message);
  2388. }
  2389. }
  2390. public void on_report_issue(GLib.SimpleAction action, GLib.Variant? param)
  2391. {
  2392. try {
  2393. AppInfo.launch_default_for_uri("https://github.com/crownengine/crown/issues", null);
  2394. } catch (Error e) {
  2395. loge(e.message);
  2396. }
  2397. }
  2398. public void on_browse_logs(GLib.SimpleAction action, GLib.Variant? param)
  2399. {
  2400. open_directory(_logs_dir.get_path());
  2401. }
  2402. public void on_changelog(GLib.SimpleAction action, GLib.Variant? param)
  2403. {
  2404. try {
  2405. AppInfo.launch_default_for_uri(CROWN_LATEST_CHANGELOG_URL, null);
  2406. } catch (Error e) {
  2407. loge(e.message);
  2408. }
  2409. }
  2410. public void on_donate(GLib.SimpleAction action, GLib.Variant? param)
  2411. {
  2412. try {
  2413. AppInfo.launch_default_for_uri(CROWN_FUND_URL, null);
  2414. } catch (Error e) {
  2415. loge(e.message);
  2416. }
  2417. }
  2418. public void on_credits(GLib.SimpleAction action, GLib.Variant? param)
  2419. {
  2420. try {
  2421. AppInfo.launch_default_for_uri(CROWN_CREDITS_URL, null);
  2422. } catch (Error e) {
  2423. loge(e.message);
  2424. }
  2425. }
  2426. public void do_delete_file(string resource_path)
  2427. {
  2428. string path = _project.absolute_path(resource_path);
  2429. try {
  2430. GLib.File.new_for_path(path).delete();
  2431. } catch (Error e) {
  2432. loge(e.message);
  2433. }
  2434. }
  2435. public void on_delete_file(GLib.SimpleAction action, GLib.Variant? param)
  2436. {
  2437. if (param == null)
  2438. return;
  2439. string resource_path = param.get_string();
  2440. string? resource_type = ResourceId.type(resource_path);
  2441. string? resource_name = ResourceId.name(resource_path);
  2442. if (resource_type != null && resource_name != null) {
  2443. if (resource_name == _level._name) {
  2444. if (!_database.changed()) {
  2445. do_delete_file(resource_path);
  2446. GLib.Application.get_default().activate_action("new-level", null);
  2447. } else {
  2448. Gtk.Dialog dlg = new_level_changed_dialog(this.active_window);
  2449. dlg.response.connect((response_id) => {
  2450. if (response_id == Gtk.ResponseType.NO) {
  2451. do_delete_file(resource_path);
  2452. do_new_level();
  2453. } else if (response_id == Gtk.ResponseType.YES) {
  2454. save("new-level");
  2455. }
  2456. dlg.destroy();
  2457. });
  2458. dlg.show_all();
  2459. }
  2460. } else {
  2461. do_delete_file(resource_path);
  2462. }
  2463. }
  2464. }
  2465. public void do_delete_directory(string dir_name)
  2466. {
  2467. if (dir_name == "")
  2468. return;
  2469. var path = _project.absolute_path(dir_name);
  2470. try {
  2471. _project.delete_tree(GLib.File.new_for_path(path));
  2472. } catch (Error e) {
  2473. loge(e.message);
  2474. }
  2475. }
  2476. public void on_delete_directory(GLib.SimpleAction action, GLib.Variant? param)
  2477. {
  2478. string dir_name = param.get_string();
  2479. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  2480. , Gtk.DialogFlags.MODAL
  2481. , Gtk.MessageType.WARNING
  2482. , Gtk.ButtonsType.NONE
  2483. , "Delete Folder " + dir_name + "?"
  2484. );
  2485. Gtk.Widget btn;
  2486. md.add_button("_Cancel", Gtk.ResponseType.CANCEL);
  2487. btn = md.add_button("_Delete", Gtk.ResponseType.YES);
  2488. btn.get_style_context().add_class("destructive-action");
  2489. md.set_default_response(Gtk.ResponseType.CANCEL);
  2490. md.response.connect((response_id) => {
  2491. if (response_id == Gtk.ResponseType.YES)
  2492. do_delete_directory(dir_name);
  2493. md.destroy();
  2494. });
  2495. md.show_all();
  2496. }
  2497. public void do_create_directory(string parent_dir_name, string dir_name)
  2498. {
  2499. if (dir_name == "")
  2500. return;
  2501. var path = _project.absolute_path(GLib.Path.build_filename(parent_dir_name, dir_name));
  2502. try {
  2503. GLib.File.new_for_path(path).make_directory();
  2504. } catch (Error e) {
  2505. loge(e.message);
  2506. }
  2507. }
  2508. public void on_create_directory(GLib.SimpleAction action, GLib.Variant? param)
  2509. {
  2510. string parent_dir_name = (string)param.get_child_value(0);
  2511. string dir_name = (string)param.get_child_value(1);
  2512. if (dir_name != "") {
  2513. do_create_directory(parent_dir_name, dir_name);
  2514. } else {
  2515. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Folder Name"
  2516. , this.active_window
  2517. , Gtk.DialogFlags.MODAL
  2518. , "Cancel"
  2519. , Gtk.ResponseType.CANCEL
  2520. , "Ok"
  2521. , Gtk.ResponseType.OK
  2522. , null
  2523. );
  2524. InputString sb = new InputString();
  2525. sb.value_changed.connect(() => { dg.response(Gtk.ResponseType.OK); });
  2526. dg.get_content_area().add(sb);
  2527. dg.response.connect((response_id) => {
  2528. if (response_id == Gtk.ResponseType.OK)
  2529. do_create_directory(parent_dir_name, sb.value.strip());
  2530. dg.destroy();
  2531. });
  2532. dg.show_all();
  2533. }
  2534. }
  2535. public async bool compile_and_reload()
  2536. {
  2537. if (yield _data_compiler.compile(_project.data_dir(), _project.platform()))
  2538. return yield refresh_all_clients();
  2539. else
  2540. return false;
  2541. }
  2542. public void do_create_script(string dir_name, string script_name, bool empty)
  2543. {
  2544. if (script_name == "")
  2545. return;
  2546. int ec = _project.create_script(dir_name, script_name, empty);
  2547. if (ec < 0) {
  2548. loge("Failed to create script %s".printf(script_name));
  2549. return;
  2550. }
  2551. compile_and_reload.begin();
  2552. }
  2553. public void on_create_script(GLib.SimpleAction action, GLib.Variant? param)
  2554. {
  2555. string dir_name = (string)param.get_child_value(0);
  2556. string script_name = (string)param.get_child_value(1);
  2557. bool empty = (bool)param.get_child_value(2);
  2558. if (script_name != "") {
  2559. do_create_script(dir_name, script_name, empty);
  2560. } else {
  2561. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Script Name"
  2562. , this.active_window
  2563. , Gtk.DialogFlags.MODAL
  2564. , "Cancel"
  2565. , Gtk.ResponseType.CANCEL
  2566. , "Ok"
  2567. , Gtk.ResponseType.OK
  2568. , null
  2569. );
  2570. InputString sb = new InputString();
  2571. sb.value_changed.connect(() => { dg.response(Gtk.ResponseType.OK); });
  2572. dg.get_content_area().add(sb);
  2573. dg.response.connect((response_id) => {
  2574. if (response_id == Gtk.ResponseType.OK)
  2575. do_create_script(dir_name, sb.value.strip(), empty);
  2576. dg.destroy();
  2577. });
  2578. dg.show_all();
  2579. }
  2580. }
  2581. public void do_create_unit(string dir_name, string unit_name)
  2582. {
  2583. if (unit_name == "")
  2584. return;
  2585. int ec = _project.create_unit(dir_name, unit_name);
  2586. if (ec < 0) {
  2587. loge("Failed to create unit %s".printf(unit_name));
  2588. return;
  2589. }
  2590. compile_and_reload.begin();
  2591. }
  2592. public void on_create_unit(GLib.SimpleAction action, GLib.Variant? param)
  2593. {
  2594. string dir_name = (string)param.get_child_value(0);
  2595. string unit_name = (string)param.get_child_value(1);
  2596. if (unit_name != "") {
  2597. do_create_unit(dir_name, unit_name);
  2598. } else {
  2599. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Unit Name"
  2600. , this.active_window
  2601. , Gtk.DialogFlags.MODAL
  2602. , "Cancel"
  2603. , Gtk.ResponseType.CANCEL
  2604. , "Ok"
  2605. , Gtk.ResponseType.OK
  2606. , null
  2607. );
  2608. InputString sb = new InputString();
  2609. sb.value_changed.connect(() => { dg.response(Gtk.ResponseType.OK); });
  2610. dg.get_content_area().add(sb);
  2611. dg.response.connect((response_id) => {
  2612. if (response_id == Gtk.ResponseType.OK)
  2613. do_create_unit(dir_name, sb.value.strip());
  2614. dg.destroy();
  2615. });
  2616. dg.show_all();
  2617. }
  2618. }
  2619. public void do_create_state_machine(string dir_name, string state_machine_name, string skeleton_name)
  2620. {
  2621. if (state_machine_name == "")
  2622. return;
  2623. int ec = _project.create_state_machine(dir_name, state_machine_name, skeleton_name != "" ? skeleton_name : null);
  2624. if (ec < 0) {
  2625. loge("Failed to create state machine %s".printf(state_machine_name));
  2626. return;
  2627. }
  2628. compile_and_reload.begin();
  2629. }
  2630. public void on_create_state_machine(GLib.SimpleAction action, GLib.Variant? param)
  2631. {
  2632. string dir_name = (string)param.get_child_value(0);
  2633. string state_machine_name = (string)param.get_child_value(1);
  2634. string skeleton_name = (string)param.get_child_value(2);
  2635. if (state_machine_name != "") {
  2636. do_create_state_machine(dir_name, state_machine_name, skeleton_name);
  2637. } else {
  2638. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("State Machine Name"
  2639. , this.active_window
  2640. , Gtk.DialogFlags.MODAL
  2641. , "Cancel"
  2642. , Gtk.ResponseType.CANCEL
  2643. , "Ok"
  2644. , Gtk.ResponseType.OK
  2645. , null
  2646. );
  2647. InputString sb = new InputString();
  2648. sb.value_changed.connect(() => { dg.response(Gtk.ResponseType.OK); });
  2649. dg.get_content_area().add(sb);
  2650. dg.response.connect((response_id) => {
  2651. if (response_id == Gtk.ResponseType.OK)
  2652. do_create_state_machine(dir_name, sb.value.strip(), skeleton_name);
  2653. dg.destroy();
  2654. });
  2655. dg.show_all();
  2656. }
  2657. }
  2658. public void do_create_material(string dir_name, string material_name)
  2659. {
  2660. if (material_name == "")
  2661. return;
  2662. int ec = _project.create_material(dir_name, material_name);
  2663. if (ec < 0) {
  2664. loge("Failed to create material %s".printf(material_name));
  2665. return;
  2666. }
  2667. compile_and_reload.begin();
  2668. }
  2669. public void on_create_material(GLib.SimpleAction action, GLib.Variant? param)
  2670. {
  2671. string dir_name = (string)param.get_child_value(0);
  2672. string material_name = (string)param.get_child_value(1);
  2673. if (material_name != "") {
  2674. do_create_material(dir_name, material_name);
  2675. } else {
  2676. Gtk.Dialog dg = new Gtk.Dialog.with_buttons("Material Name"
  2677. , this.active_window
  2678. , Gtk.DialogFlags.MODAL
  2679. , "Cancel"
  2680. , Gtk.ResponseType.CANCEL
  2681. , "Ok"
  2682. , Gtk.ResponseType.OK
  2683. , null
  2684. );
  2685. InputString sb = new InputString();
  2686. sb.value_changed.connect(() => { dg.response(Gtk.ResponseType.OK); });
  2687. dg.get_content_area().add(sb);
  2688. dg.response.connect((response_id) => {
  2689. if (response_id == Gtk.ResponseType.OK)
  2690. do_create_material(dir_name, sb.value.strip());
  2691. dg.destroy();
  2692. });
  2693. dg.show_all();
  2694. }
  2695. }
  2696. public void on_open_containing(GLib.SimpleAction action, GLib.Variant? param)
  2697. {
  2698. string path = param.get_string();
  2699. GLib.File abs_path = GLib.File.new_for_path(_project.absolute_path(path));
  2700. GLib.File? abs_parent = abs_path.get_parent();
  2701. string abs_parent_path = abs_parent != null
  2702. ? abs_parent.get_path()
  2703. : abs_path.get_path()
  2704. ;
  2705. open_directory(abs_parent_path);
  2706. }
  2707. public void delete_tree(GLib.File file) throws Error
  2708. {
  2709. GLib.FileEnumerator fe = file.enumerate_children("standard::*"
  2710. , GLib.FileQueryInfoFlags.NOFOLLOW_SYMLINKS
  2711. );
  2712. GLib.FileInfo info = null;
  2713. while ((info = fe.next_file()) != null) {
  2714. GLib.File subfile = file.resolve_relative_path(info.get_name());
  2715. if (info.get_file_type() == GLib.FileType.DIRECTORY)
  2716. delete_tree(subfile);
  2717. else
  2718. subfile.delete();
  2719. }
  2720. file.delete();
  2721. }
  2722. public Gtk.Dialog new_package_dir_exists_dialog(string package_dir)
  2723. {
  2724. Gtk.MessageDialog md = new Gtk.MessageDialog(_deploy_dialog
  2725. , Gtk.DialogFlags.MODAL
  2726. , Gtk.MessageType.QUESTION
  2727. , Gtk.ButtonsType.NONE
  2728. , "A file named `%s` already exists.\nOverwrite?".printf(package_dir)
  2729. );
  2730. md.set_default_response(Gtk.ResponseType.NO);
  2731. Gtk.Widget btn;
  2732. md.add_button("_No", Gtk.ResponseType.NO);
  2733. btn = md.add_button("_Yes", Gtk.ResponseType.YES);
  2734. btn.get_style_context().add_class("destructive-action");
  2735. return md;
  2736. }
  2737. public GLib.File deploy_package_dir(out string config_path, string output_path, string app_identifier, TargetPlatform platform, TargetArch arch, TargetConfig config)
  2738. {
  2739. string platform_name[] =
  2740. {
  2741. "android", // TargetArch.ANDROID
  2742. "html5", // TargetArch.HTML5
  2743. "linux", // TargetArch.LINUX
  2744. "windows" // TargetArch.WINDOWS
  2745. };
  2746. string arch_name[] =
  2747. {
  2748. "x86", // TargetArch.X86
  2749. "x64", // TargetArch.X64
  2750. "arm", // TargetArch.ARM
  2751. "arm64", // TargetArch.ARM64
  2752. "wasm" // TargetArch.WASM
  2753. };
  2754. string config_name[] =
  2755. {
  2756. "release", // TargetConfig.RELEASE
  2757. "development", // TargetConfig.DEVELOPMENT
  2758. #if CROWN_DEBUG
  2759. "debug" // TargetConfig.DEBUG
  2760. #endif
  2761. };
  2762. string platform_path = Path.build_path(Path.DIR_SEPARATOR_S, output_path, platform_name[platform], arch_name[arch]);
  2763. config_path = Path.build_path(Path.DIR_SEPARATOR_S, platform_path, config_name[config]);
  2764. return GLib.File.new_for_path(Path.build_path(Path.DIR_SEPARATOR_S, config_path, app_identifier));
  2765. }
  2766. public void do_create_package_android(GLib.File package_dir
  2767. , string config_path
  2768. , string output_path
  2769. , int config
  2770. , string app_title
  2771. , string app_identifier
  2772. , int app_version_code
  2773. , string app_version_name
  2774. , string keystore_path
  2775. , string keystore_pass
  2776. , string key_alias
  2777. , string key_pass
  2778. , int arch
  2779. , string apk_name
  2780. )
  2781. {
  2782. string config_name[] =
  2783. {
  2784. "release",
  2785. "development",
  2786. #if CROWN_DEBUG
  2787. "debug"
  2788. #endif
  2789. };
  2790. AndroidDeployer android = new AndroidDeployer();
  2791. android.check_config();
  2792. logi("Creating Android package (%s)...".printf(arch == TargetArch.ARM ? "ARMv7-A" : "ARMv8-A"));
  2793. var activity_name = "MainActivity";
  2794. var package_path = package_dir.get_path();
  2795. // Architecture-agnostic paths.
  2796. var manifests_path = Path.build_path(Path.DIR_SEPARATOR_S, package_path, "manifests");
  2797. var java_sources_path = Path.build_path(Path.DIR_SEPARATOR_S, package_path, "java");
  2798. var app_sources_path = Path.build_path(Path.DIR_SEPARATOR_S, java_sources_path, app_identifier.replace(".", "/"));
  2799. var assets_path = Path.build_path(Path.DIR_SEPARATOR_S, package_path, "assets");
  2800. var res_path = Path.build_path(Path.DIR_SEPARATOR_S, package_path, "res");
  2801. var bin_path = Path.build_path(Path.DIR_SEPARATOR_S, package_path, "bin");
  2802. var obj_path = Path.build_path(Path.DIR_SEPARATOR_S, package_path, "obj");
  2803. var res_layout_path = Path.build_path(Path.DIR_SEPARATOR_S, res_path, "layout");
  2804. var res_values_path = Path.build_path(Path.DIR_SEPARATOR_S, res_path, "values");
  2805. var res_drawable_path = Path.build_path(Path.DIR_SEPARATOR_S, res_path, "drawable");
  2806. var manifest_xml_path = Path.build_path(Path.DIR_SEPARATOR_S, manifests_path, "AndroidManifest.xml");
  2807. var strings_xml_path = Path.build_path(Path.DIR_SEPARATOR_S, res_path, "values", "strings.xml");
  2808. var activity_java_path = Path.build_path(Path.DIR_SEPARATOR_S, app_sources_path, "%s.java".printf(activity_name));
  2809. var android_jar_path = Path.build_path(Path.DIR_SEPARATOR_S, android._sdk_path, "platforms", "android-" + android._sdk_api_level, "android.jar");
  2810. var libcrown_src_name = "libcrown-" + config_name[config] + ".so";
  2811. var libcpp_name = "libc++_shared.so";
  2812. var signed_apk = Path.build_path(Path.DIR_SEPARATOR_S, bin_path, apk_name + ".signed.apk");
  2813. var unaligned_apk = Path.build_path(Path.DIR_SEPARATOR_S, bin_path, apk_name + ".unaligned.apk");
  2814. var final_apk = Path.build_path(Path.DIR_SEPARATOR_S, config_path, apk_name + ".apk");
  2815. #if CROWN_PLATFORM_LINUX
  2816. string host_platform = "linux-x86_64";
  2817. #elif CROWN_PLATFORM_WINDOWS
  2818. string host_platform = "windows-x86_64";
  2819. #endif
  2820. // Architecture-specific paths.
  2821. string dc_platform = null;
  2822. string bin_folder = null;
  2823. string apk_arch = null;
  2824. string llvm_arch = null;
  2825. if (arch == TargetArch.ARM) {
  2826. dc_platform = "android";
  2827. bin_folder = "android-arm";
  2828. apk_arch = "armeabi-v7a";
  2829. llvm_arch = "arm-linux-androideabi";
  2830. } else if (arch == TargetArch.ARM64) {
  2831. dc_platform = "android-arm64";
  2832. bin_folder = "android-arm64";
  2833. apk_arch = "arm64-v8a";
  2834. llvm_arch = "aarch64-linux-android";
  2835. } else {
  2836. loge("Invalid architecture");
  2837. return;
  2838. }
  2839. var libcrown_src_path = Path.build_path(Path.DIR_SEPARATOR_S, "..", "..", bin_folder, "bin", libcrown_src_name);
  2840. var libcpp_src_path = Path.build_path(Path.DIR_SEPARATOR_S
  2841. , android._ndk_root_path
  2842. , "toolchains"
  2843. , "llvm"
  2844. , "prebuilt"
  2845. , host_platform
  2846. , "sysroot"
  2847. , "usr"
  2848. , "lib"
  2849. , llvm_arch
  2850. , libcpp_name
  2851. );
  2852. var lib_path_relative = Path.build_path(Path.DIR_SEPARATOR_S, "lib", apk_arch);
  2853. var lib_path = Path.build_path(Path.DIR_SEPARATOR_S, package_path, lib_path_relative);
  2854. var libcrown_path_relative = Path.build_path(Path.DIR_SEPARATOR_S, lib_path_relative, "libcrown.so");
  2855. var libcpp_path_relative = Path.build_path(Path.DIR_SEPARATOR_S, lib_path_relative, libcpp_name);
  2856. var libcrown_dst_path = Path.build_path(Path.DIR_SEPARATOR_S, lib_path, "libcrown.so");
  2857. var libcpp_dst_path = Path.build_path(Path.DIR_SEPARATOR_S, lib_path, "libc++_shared.so");
  2858. // Create Android project skeleton.
  2859. try {
  2860. GLib.File.new_for_path(manifests_path).make_directory();
  2861. GLib.File.new_for_path(app_sources_path).make_directory_with_parents();
  2862. GLib.File.new_for_path(lib_path).make_directory_with_parents();
  2863. GLib.File.new_for_path(assets_path).make_directory();
  2864. GLib.File.new_for_path(bin_path).make_directory();
  2865. GLib.File.new_for_path(obj_path).make_directory();
  2866. GLib.File.new_for_path(res_layout_path).make_directory_with_parents();
  2867. GLib.File.new_for_path(res_values_path).make_directory();
  2868. GLib.File.new_for_path(res_drawable_path).make_directory();
  2869. } catch (Error e) {
  2870. loge(e.message);
  2871. }
  2872. // Compile game data.
  2873. try {
  2874. GLib.File.new_for_path(libcrown_src_path).copy(GLib.File.new_for_path(libcrown_dst_path), GLib.FileCopyFlags.NONE);
  2875. GLib.File.new_for_path(libcpp_src_path).copy(GLib.File.new_for_path(libcpp_dst_path), GLib.FileCopyFlags.NONE);
  2876. string[] args;
  2877. // Populate Android assets folder with data.
  2878. args = new string[]
  2879. {
  2880. ENGINE_EXE,
  2881. "--source-dir",
  2882. _project.source_dir(),
  2883. "--map-source-dir",
  2884. "core",
  2885. _project.toolchain_dir(),
  2886. "--bundle-dir",
  2887. assets_path,
  2888. "--compile",
  2889. "--bundle",
  2890. "--platform",
  2891. dc_platform
  2892. };
  2893. uint32 pid = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  2894. int exit_status = _subprocess_launcher.wait(pid);
  2895. if (exit_status != 0) {
  2896. loge("Failed to compile data. exit_status = %d".printf(exit_status));
  2897. return;
  2898. }
  2899. } catch (Error e) {
  2900. loge(e.message);
  2901. return;
  2902. }
  2903. // Create Android manifest.
  2904. string android_manifest = "";
  2905. android_manifest += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
  2906. android_manifest += "\n<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"";
  2907. android_manifest += "\n package=\"%s\"".printf(app_identifier);
  2908. android_manifest += "\n android:versionCode=\"%u\"".printf(app_version_code);
  2909. android_manifest += "\n android:versionName=\"%s\">".printf(app_version_name);
  2910. android_manifest += "\n";
  2911. android_manifest += "\n <uses-sdk android:minSdkVersion=\"23\" />";
  2912. android_manifest += "\n";
  2913. android_manifest += "\n <!-- For ConsoleServer -->";
  2914. android_manifest += "\n <uses-permission android:name=\"android.permission.INTERNET\" />";
  2915. android_manifest += "\n";
  2916. android_manifest += "\n <application";
  2917. android_manifest += "\n android:hasCode=\"true\"";
  2918. android_manifest += "\n android:label=\"%s\">".printf(app_title);
  2919. android_manifest += "\n <activity";
  2920. android_manifest += "\n android:name=\"%s.%s\"".printf(app_identifier, activity_name);
  2921. android_manifest += "\n android:configChanges=\"orientation|keyboardHidden\"";
  2922. android_manifest += "\n android:label=\"@string/activity_label\"";
  2923. android_manifest += "\n android:screenOrientation=\"landscape\"";
  2924. android_manifest += "\n android:theme=\"@android:style/Theme.NoTitleBar.Fullscreen\">";
  2925. android_manifest += "\n";
  2926. android_manifest += "\n <!-- Tell NativeActivity the name of our .so -->";
  2927. android_manifest += "\n <meta-data";
  2928. android_manifest += "\n android:name=\"android.app.lib_name\"";
  2929. android_manifest += "\n android:value=\"crown\" />";
  2930. android_manifest += "\n";
  2931. android_manifest += "\n <intent-filter>";
  2932. android_manifest += "\n <action android:name=\"android.intent.action.MAIN\" />";
  2933. android_manifest += "\n <action android:name=\"android.intent.action.VIEW\" />";
  2934. android_manifest += "\n <category android:name=\"android.intent.category.LAUNCHER\" />";
  2935. android_manifest += "\n </intent-filter>";
  2936. android_manifest += "\n </activity>";
  2937. android_manifest += "\n </application>";
  2938. android_manifest += "\n</manifest>";
  2939. android_manifest += "\n";
  2940. GLib.FileStream? fs = FileStream.open(manifest_xml_path, "w");
  2941. if (fs == null) {
  2942. loge("Failed to open '%s'".printf(manifest_xml_path));
  2943. return;
  2944. }
  2945. fs.write(android_manifest.data);
  2946. // Create Android strings.xml.
  2947. string android_strings = "";
  2948. android_strings += "<resources>";
  2949. android_strings += "\n<string name=\"activity_label\">%s</string>".printf(app_title);
  2950. android_strings += "\n</resources>";
  2951. android_strings += "\n";
  2952. fs = FileStream.open(strings_xml_path, "w");
  2953. if (fs == null) {
  2954. loge("Failed to open '%s'".printf(strings_xml_path));
  2955. return;
  2956. }
  2957. fs.write(android_strings.data);
  2958. // Create Android activity.
  2959. string android_activity = "";
  2960. android_activity += "package %s;".printf(app_identifier);
  2961. android_activity += "\n";
  2962. android_activity += "\nimport android.app.NativeActivity;";
  2963. android_activity += "\nimport android.os.Bundle;";
  2964. android_activity += "\nimport android.view.View;";
  2965. android_activity += "\n";
  2966. android_activity += "\npublic class %s extends NativeActivity".printf(activity_name);
  2967. android_activity += "\n{";
  2968. android_activity += "\n static";
  2969. android_activity += "\n {";
  2970. android_activity += "\n System.loadLibrary(\"crown\");";
  2971. android_activity += "\n }";
  2972. android_activity += "\n";
  2973. android_activity += "\n @Override";
  2974. android_activity += "\n public void onCreate(Bundle savedInstanceState) {";
  2975. android_activity += "\n super.onCreate(savedInstanceState);";
  2976. android_activity += "\n // Init additional stuff here (Ads etc.)";
  2977. android_activity += "\n }";
  2978. android_activity += "\n";
  2979. android_activity += "\n @Override";
  2980. android_activity += "\n public void onDestroy() {";
  2981. android_activity += "\n // Destroy additional stuff here (Ads etc)";
  2982. android_activity += "\n super.onDestroy();";
  2983. android_activity += "\n }";
  2984. android_activity += "\n";
  2985. android_activity += "\n @Override";
  2986. android_activity += "\n public void onWindowFocusChanged(boolean hasFocus) {";
  2987. android_activity += "\n super.onWindowFocusChanged(hasFocus);";
  2988. android_activity += "\n if (hasFocus) {";
  2989. android_activity += "\n hideSystemUI();";
  2990. android_activity += "\n }";
  2991. android_activity += "\n }";
  2992. android_activity += "\n";
  2993. android_activity += "\n public void hideSystemUI() {";
  2994. android_activity += "\n // Enables regular immersive mode.";
  2995. android_activity += "\n // For \"lean back\" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.";
  2996. android_activity += "\n // Or for \"sticky immersive,\" replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY";
  2997. android_activity += "\n View decorView = getWindow().getDecorView();";
  2998. android_activity += "\n decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE";
  2999. android_activity += "\n // Set the content to appear under the system bars so that the";
  3000. android_activity += "\n // content doesn't resize when the system bars hide and show.";
  3001. android_activity += "\n | View.SYSTEM_UI_FLAG_LAYOUT_STABLE";
  3002. android_activity += "\n | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION";
  3003. android_activity += "\n | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN";
  3004. android_activity += "\n // Hide the nav bar and status bar";
  3005. android_activity += "\n | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION";
  3006. android_activity += "\n | View.SYSTEM_UI_FLAG_FULLSCREEN";
  3007. android_activity += "\n );";
  3008. android_activity += "\n }";
  3009. android_activity += "\n";
  3010. android_activity += "\n public void showSystemUI() {";
  3011. android_activity += "\n // Shows the system bars by removing all the flags";
  3012. android_activity += "\n // except for the ones that make the content appear under the system bars.";
  3013. android_activity += "\n View decorView = getWindow().getDecorView();";
  3014. android_activity += "\n decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE";
  3015. android_activity += "\n | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION";
  3016. android_activity += "\n | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN";
  3017. android_activity += "\n );";
  3018. android_activity += "\n }";
  3019. android_activity += "\n}";
  3020. android_activity += "\n";
  3021. fs = FileStream.open(activity_java_path, "w");
  3022. if (fs == null) {
  3023. loge("Failed to open '%s'".printf(activity_java_path));
  3024. return;
  3025. }
  3026. fs.write(android_activity.data);
  3027. // Compile java NativeActivity.
  3028. Thread<int> javac = new Thread<int>("javac", () => {
  3029. try {
  3030. string[] args = new string[]
  3031. {
  3032. android._javac_path,
  3033. "-verbose",
  3034. "-source",
  3035. "8", // https://docs.oracle.com/javase/1.5.0/docs/relnotes/version-5.0.html
  3036. "-target",
  3037. "8", // https://docs.oracle.com/javase/1.5.0/docs/relnotes/version-5.0.html
  3038. "-d",
  3039. obj_path,
  3040. "-classpath",
  3041. "java",
  3042. "-bootclasspath",
  3043. android_jar_path,
  3044. activity_java_path
  3045. };
  3046. var sl = new GLib.SubprocessLauncher(subprocess_flags());
  3047. sl.spawnv(args);
  3048. } catch (Error e) {
  3049. loge(e.message);
  3050. return -1;
  3051. }
  3052. return 0;
  3053. });
  3054. javac.join();
  3055. // Generate Android APK.
  3056. new Thread<int>("post-javac", () => {
  3057. // FIXME: just wait() for javac to terminate...
  3058. var class_path = Path.build_path(Path.DIR_SEPARATOR_S
  3059. , obj_path
  3060. , app_identifier.replace(".", "/")
  3061. , "%s.class".printf(activity_name)
  3062. );
  3063. var class_file = GLib.File.new_for_path(class_path);
  3064. // Wait for javac to generate the .class file.
  3065. GLib.Timer timer = new GLib.Timer();
  3066. timer.start();
  3067. while (!class_file.query_exists() && timer.elapsed() < 5) {
  3068. GLib.Thread.usleep(500*1000);
  3069. }
  3070. if (!class_file.query_exists()) {
  3071. loge("Failed to generate .class file");
  3072. return -1;
  3073. }
  3074. // Generate remaining APK stuff.
  3075. string[] args;
  3076. uint32 pid;
  3077. int exit_status;
  3078. try {
  3079. args = new string[]
  3080. {
  3081. android._d8_path,
  3082. "--output",
  3083. bin_path,
  3084. class_path,
  3085. "--no-desugaring"
  3086. };
  3087. pid = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  3088. exit_status = _subprocess_launcher.wait(pid);
  3089. if (exit_status != 0) {
  3090. loge("Failed to generate dex file. exit_status %d".printf(exit_status));
  3091. return -1;
  3092. }
  3093. args = new string[]
  3094. {
  3095. android._aapt_path,
  3096. "package",
  3097. "-f",
  3098. "-m",
  3099. "-F",
  3100. unaligned_apk,
  3101. "-M",
  3102. manifest_xml_path,
  3103. "-S",
  3104. res_path,
  3105. "-A",
  3106. assets_path,
  3107. "-I",
  3108. android_jar_path
  3109. };
  3110. pid = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  3111. exit_status = _subprocess_launcher.wait(pid);
  3112. if (exit_status != 0) {
  3113. loge("Failed to do something with the APK. exit_status %d".printf(exit_status));
  3114. return -1;
  3115. }
  3116. args = new string[]
  3117. {
  3118. android._aapt_path,
  3119. "add",
  3120. unaligned_apk,
  3121. "classes.dex"
  3122. };
  3123. pid = _subprocess_launcher.spawnv_async(subprocess_flags(), args, bin_path);
  3124. exit_status = _subprocess_launcher.wait(pid);
  3125. if (exit_status != 0) {
  3126. loge("Failed to add classes.dex to APK. exit_status %d".printf(exit_status));
  3127. return -1;
  3128. }
  3129. args = new string[]
  3130. {
  3131. android._aapt_path,
  3132. "add",
  3133. unaligned_apk,
  3134. libcrown_path_relative.replace("\\", "/"),
  3135. libcpp_path_relative.replace("\\", "/")
  3136. };
  3137. pid = _subprocess_launcher.spawnv_async(subprocess_flags(), args, package_path);
  3138. exit_status = _subprocess_launcher.wait(pid);
  3139. if (exit_status != 0) {
  3140. loge("Failed to add libs to APK. exit_status %d".printf(exit_status));
  3141. return -1;
  3142. }
  3143. args = new string[]
  3144. {
  3145. android._jarsigner_path,
  3146. "-keystore",
  3147. keystore_path,
  3148. "-storepass",
  3149. keystore_pass,
  3150. "-keypass",
  3151. key_pass,
  3152. "-signedjar",
  3153. signed_apk,
  3154. unaligned_apk,
  3155. key_alias
  3156. };
  3157. pid = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  3158. exit_status = _subprocess_launcher.wait(pid);
  3159. if (exit_status != 0) {
  3160. loge("Failed sign APK. exit_status %d".printf(exit_status));
  3161. return -1;
  3162. }
  3163. args = new string[]
  3164. {
  3165. android._zipalign_path,
  3166. "-f",
  3167. "4",
  3168. signed_apk,
  3169. final_apk
  3170. };
  3171. pid = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  3172. exit_status = _subprocess_launcher.wait(pid);
  3173. if (exit_status != 0) {
  3174. loge("Failed align APK. exit_status %d".printf(exit_status));
  3175. return -1;
  3176. }
  3177. } catch (Error e) {
  3178. loge(e.message);
  3179. loge("Failed to deploy '%s'".printf(app_title));
  3180. return -1;
  3181. }
  3182. logi("Done: #FILE(%s)".printf(config_path));
  3183. return 0;
  3184. });
  3185. }
  3186. public void on_create_package_android(GLib.SimpleAction action, GLib.Variant? param)
  3187. {
  3188. var output_path = (string)param.get_child_value(0);
  3189. var config = (int)param.get_child_value(1);
  3190. var app_title = (string)param.get_child_value(2);
  3191. var app_identifier = (string)param.get_child_value(3);
  3192. var app_version_code = (int)param.get_child_value(4);
  3193. var app_version_name = (string)param.get_child_value(5);
  3194. var keystore_path = (string)param.get_child_value(6);
  3195. var keystore_pass = (string)param.get_child_value(7);
  3196. var key_alias = (string)param.get_child_value(8);
  3197. var key_pass = (string)param.get_child_value(9);
  3198. var arch = (int)param.get_child_value(10);
  3199. var apk_name = app_identifier + "-" + app_version_name;
  3200. string config_path;
  3201. GLib.File package_dir = deploy_package_dir(out config_path
  3202. , output_path
  3203. , apk_name
  3204. , TargetPlatform.ANDROID
  3205. , arch
  3206. , (TargetConfig)config
  3207. );
  3208. if (package_dir.query_exists()) {
  3209. Gtk.Dialog dlg = new_package_dir_exists_dialog(package_dir.get_basename());
  3210. dlg.response.connect((response_id) => {
  3211. if (response_id == Gtk.ResponseType.YES) {
  3212. try {
  3213. delete_tree(package_dir);
  3214. package_dir.make_directory_with_parents();
  3215. do_create_package_android(package_dir
  3216. , config_path
  3217. , output_path
  3218. , config
  3219. , app_title
  3220. , app_identifier
  3221. , app_version_code
  3222. , app_version_name
  3223. , keystore_path
  3224. , keystore_pass
  3225. , key_alias
  3226. , key_pass
  3227. , arch
  3228. , apk_name
  3229. );
  3230. } catch (Error e) {
  3231. loge(e.message);
  3232. }
  3233. }
  3234. dlg.destroy();
  3235. });
  3236. dlg.show_all();
  3237. } else {
  3238. try {
  3239. package_dir.make_directory_with_parents();
  3240. do_create_package_android(package_dir
  3241. , config_path
  3242. , output_path
  3243. , config
  3244. , app_title
  3245. , app_identifier
  3246. , app_version_code
  3247. , app_version_name
  3248. , keystore_path
  3249. , keystore_pass
  3250. , key_alias
  3251. , key_pass
  3252. , arch
  3253. , apk_name
  3254. );
  3255. } catch (Error e) {
  3256. loge(e.message);
  3257. }
  3258. }
  3259. }
  3260. public void do_create_package_html5(GLib.File package_dir
  3261. , string output_path
  3262. , int config
  3263. , string app_title
  3264. , string exe_name
  3265. )
  3266. {
  3267. string config_name[] =
  3268. {
  3269. "release",
  3270. "development",
  3271. #if CROWN_DEBUG
  3272. "debug"
  3273. #endif
  3274. };
  3275. HTML5Deployer html5 = new HTML5Deployer();
  3276. html5.check_config();
  3277. logi("Creating HTML5 package...");
  3278. string package_path = package_dir.get_path();
  3279. // Create data bundle.
  3280. try {
  3281. string[] args;
  3282. string tmp_bundle_dir = GLib.DirUtils.make_tmp("XXXXXX");
  3283. args = new string[]
  3284. {
  3285. ENGINE_EXE,
  3286. "--source-dir",
  3287. _project.source_dir(),
  3288. "--map-source-dir",
  3289. "core",
  3290. _project.toolchain_dir(),
  3291. "--bundle-dir",
  3292. tmp_bundle_dir,
  3293. "--compile",
  3294. "--bundle",
  3295. "--platform",
  3296. "html5"
  3297. };
  3298. var pid = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  3299. var exit_status = _subprocess_launcher.wait(pid);
  3300. if (exit_status != 0) {
  3301. loge("Failed to compile data. exit_status = %d".printf(exit_status));
  3302. return;
  3303. }
  3304. // Copy runtime executables to package folder.
  3305. var runtime_name_src = "crown-%s".printf(config_name[config]);
  3306. var runtime_path_src = Path.build_path(Path.DIR_SEPARATOR_S, "..", "..", "wasm", "bin", runtime_name_src);
  3307. var runtime_name_dst = Path.build_filename(package_path, runtime_name_src);
  3308. var src = File.new_for_path(runtime_path_src + ".js");
  3309. var dst = File.new_for_path(runtime_name_dst + ".js");
  3310. src.copy(dst, FileCopyFlags.OVERWRITE);
  3311. try {
  3312. src = File.new_for_path(runtime_path_src + ".worker.js");
  3313. dst = File.new_for_path(runtime_name_dst + ".worker.js");
  3314. src.copy(dst, FileCopyFlags.OVERWRITE);
  3315. } catch (GLib.Error e) {
  3316. // NOOP: newer emscripten versions embed .worker.js into main .js.
  3317. }
  3318. src = File.new_for_path(runtime_path_src + ".wasm");
  3319. dst = File.new_for_path(runtime_name_dst + ".wasm");
  3320. src.copy(dst, FileCopyFlags.OVERWRITE);
  3321. // Package bundle data with emscripten's file_packager.
  3322. args = new string[]
  3323. {
  3324. Path.build_path(Path.DIR_SEPARATOR_S, html5._emscripten_sdk_path, "tools", "file_packager"),
  3325. Path.build_path(Path.DIR_SEPARATOR_S, package_path, "data.bin"),
  3326. "--preload",
  3327. "./data",
  3328. "--js-output=" + Path.build_path(Path.DIR_SEPARATOR_S, package_path, "data.js")
  3329. };
  3330. pid = _subprocess_launcher.spawnv_async(subprocess_flags(), args, tmp_bundle_dir);
  3331. exit_status = _subprocess_launcher.wait(pid);
  3332. delete_tree(GLib.File.new_for_path(tmp_bundle_dir));
  3333. if (exit_status != 0) {
  3334. loge("Failed to package data.js. exit_status %d".printf(exit_status));
  3335. return;
  3336. }
  3337. // Generate index.html.
  3338. var index_html_path = Path.build_path(Path.DIR_SEPARATOR_S, package_path, "index.html");
  3339. string index_html = "";
  3340. index_html += "\n<!doctype html>";
  3341. index_html += "\n<html lang=\"en-us\">";
  3342. index_html += "\n<head>";
  3343. index_html += "\n<meta charset=\"utf-8\">";
  3344. index_html += "\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
  3345. index_html += "\n<style>";
  3346. index_html += "\n body {";
  3347. index_html += "\n margin: 0px;";
  3348. index_html += "\n background-color: black;";
  3349. index_html += "\n }";
  3350. index_html += "\n .app {";
  3351. index_html += "\n display: block;";
  3352. index_html += "\n border: 0px;";
  3353. index_html += "\n margin: 0px;";
  3354. index_html += "\n position: absolute;";
  3355. index_html += "\n top: 0;";
  3356. index_html += "\n left: 0;";
  3357. index_html += "\n width: 100%;";
  3358. index_html += "\n height: 100%;";
  3359. index_html += "\n background-color: black;";
  3360. index_html += "\n }";
  3361. index_html += "\n</style>";
  3362. index_html += "\n</head>";
  3363. index_html += "\n<body>";
  3364. index_html += "\n<canvas class=\"app\" id=\"canvas\" oncontextmenu=\"event.preventDefault()\" tabindex=-1></canvas>";
  3365. index_html += "\n<script type='text/javascript'>";
  3366. index_html += "\n var Module = {";
  3367. index_html += "\n preRun: [],";
  3368. index_html += "\n postRun: [],";
  3369. index_html += "\n canvas: (() => {";
  3370. index_html += "\n var canvas = document.getElementById('canvas');";
  3371. index_html += "\n canvas.addEventListener(\"webglcontextlost\", (e) => {";
  3372. index_html += "\n alert('WebGL context lost. You will need to reload the page.');";
  3373. index_html += "\n e.preventDefault();";
  3374. index_html += "\n }, false);";
  3375. index_html += "\n return canvas;";
  3376. index_html += "\n })(),";
  3377. index_html += "\n setStatus: (text) => { },";
  3378. index_html += "\n monitorRunDependencies: (left) => { }";
  3379. index_html += "\n };";
  3380. index_html += "\n window.onerror = (event) => {";
  3381. index_html += "\n console.error('onerror: ' + event.message);";
  3382. index_html += "\n };";
  3383. index_html += "\n</script>";
  3384. index_html += "\n<script async type=\"text/javascript\" src=\"data.js\"></script>";
  3385. index_html += "\n<script async type=\"text/javascript\" src=\"%s.js\"></script>".printf(runtime_name_src);
  3386. index_html += "\n</body>";
  3387. index_html += "\n</html>";
  3388. index_html += "\n";
  3389. GLib.FileStream? fs = FileStream.open(index_html_path, "w");
  3390. if (fs == null) {
  3391. loge("Failed to open '%s'".printf(index_html_path));
  3392. return;
  3393. }
  3394. fs.write(index_html.data);
  3395. } catch (Error e) {
  3396. loge(e.message);
  3397. loge("Failed to deploy '%s'".printf(app_title));
  3398. return;
  3399. }
  3400. logi("Done: #FILE(%s)".printf(package_path));
  3401. }
  3402. public void on_create_package_html5(GLib.SimpleAction action, GLib.Variant? param)
  3403. {
  3404. var output_path = (string)param.get_child_value(0);
  3405. var config = (int)param.get_child_value(1);
  3406. var app_title = (string)param.get_child_value(2);
  3407. var exe_name = app_title.replace(" ", "_").down();
  3408. string config_path;
  3409. GLib.File package_dir = deploy_package_dir(out config_path
  3410. , output_path
  3411. , exe_name
  3412. , TargetPlatform.HTML5
  3413. , TargetArch.WASM
  3414. , (TargetConfig)config
  3415. );
  3416. if (package_dir.query_exists()) {
  3417. Gtk.Dialog dlg = new_package_dir_exists_dialog(package_dir.get_basename());
  3418. dlg.response.connect((response_id) => {
  3419. if (response_id == Gtk.ResponseType.YES) {
  3420. try {
  3421. delete_tree(package_dir);
  3422. package_dir.make_directory_with_parents();
  3423. do_create_package_html5(package_dir, output_path, config, app_title, exe_name);
  3424. } catch (Error e) {
  3425. loge(e.message);
  3426. }
  3427. }
  3428. dlg.destroy();
  3429. });
  3430. dlg.show_all();
  3431. } else {
  3432. try {
  3433. package_dir.make_directory_with_parents();
  3434. do_create_package_html5(package_dir, output_path, config, app_title, exe_name);
  3435. } catch (Error e) {
  3436. loge(e.message);
  3437. }
  3438. }
  3439. }
  3440. public void do_create_package_linux(GLib.File package_dir
  3441. , string output_path
  3442. , int config
  3443. , string app_title
  3444. , string exe_name
  3445. )
  3446. {
  3447. string config_name[] =
  3448. {
  3449. "release",
  3450. "development",
  3451. #if CROWN_DEBUG
  3452. "debug"
  3453. #endif
  3454. };
  3455. logi("Creating Linux package...");
  3456. string package_path = package_dir.get_path();
  3457. // Create data bundle.
  3458. try {
  3459. string args[] =
  3460. {
  3461. ENGINE_EXE,
  3462. "--source-dir",
  3463. _project.source_dir(),
  3464. "--map-source-dir",
  3465. "core",
  3466. _project.toolchain_dir(),
  3467. "--bundle-dir",
  3468. package_path,
  3469. "--compile",
  3470. "--bundle",
  3471. "--platform",
  3472. "linux"
  3473. };
  3474. uint32 compiler = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  3475. int exit_status = _subprocess_launcher.wait(compiler);
  3476. if (exit_status != 0) {
  3477. loge("Failed to compile data. exit_status = %d".printf(exit_status));
  3478. return;
  3479. }
  3480. GLib.File engine_exe_src = File.new_for_path(EXE_PREFIX + "crown-%s".printf(config_name[config]) + EXE_SUFFIX);
  3481. GLib.File engine_exe_dst = File.new_for_path(Path.build_filename(package_path, exe_name + EXE_SUFFIX));
  3482. engine_exe_src.copy(engine_exe_dst, FileCopyFlags.OVERWRITE);
  3483. } catch (Error e) {
  3484. loge(e.message);
  3485. loge("Failed to deploy '%s'".printf(app_title));
  3486. return;
  3487. }
  3488. logi("Done: #FILE(%s)".printf(package_path));
  3489. }
  3490. public void on_create_package_linux(GLib.SimpleAction action, GLib.Variant? param)
  3491. {
  3492. var output_path = (string)param.get_child_value(0);
  3493. var config = (int)param.get_child_value(1);
  3494. var app_title = (string)param.get_child_value(2);
  3495. var exe_name = app_title.replace(" ", "_").down();
  3496. string config_path;
  3497. GLib.File package_dir = deploy_package_dir(out config_path
  3498. , output_path
  3499. , exe_name
  3500. , TargetPlatform.LINUX
  3501. , TargetArch.X64
  3502. , (TargetConfig)config
  3503. );
  3504. if (package_dir.query_exists()) {
  3505. Gtk.Dialog dlg = new_package_dir_exists_dialog(package_dir.get_basename());
  3506. dlg.response.connect((response_id) => {
  3507. if (response_id == Gtk.ResponseType.YES) {
  3508. try {
  3509. delete_tree(package_dir);
  3510. package_dir.make_directory_with_parents();
  3511. do_create_package_linux(package_dir, output_path, config, app_title, exe_name);
  3512. } catch (Error e) {
  3513. loge(e.message);
  3514. }
  3515. }
  3516. dlg.destroy();
  3517. });
  3518. dlg.show_all();
  3519. } else {
  3520. try {
  3521. package_dir.make_directory_with_parents();
  3522. do_create_package_linux(package_dir, output_path, config, app_title, exe_name);
  3523. } catch (Error e) {
  3524. loge(e.message);
  3525. }
  3526. }
  3527. }
  3528. void do_create_package_windows(GLib.File package_dir
  3529. , string output_path
  3530. , int config
  3531. , string app_title
  3532. , string exe_name
  3533. )
  3534. {
  3535. string config_name[] =
  3536. {
  3537. "release",
  3538. "development",
  3539. #if CROWN_DEBUG
  3540. "debug"
  3541. #endif
  3542. };
  3543. logi("Creating Windows package");
  3544. string package_path = package_dir.get_path();
  3545. // Create data bundle.
  3546. try {
  3547. string args[] =
  3548. {
  3549. ENGINE_EXE,
  3550. "--source-dir",
  3551. _project.source_dir(),
  3552. "--map-source-dir",
  3553. "core",
  3554. _project.toolchain_dir(),
  3555. "--bundle-dir",
  3556. package_path,
  3557. "--compile",
  3558. "--bundle",
  3559. "--platform",
  3560. "windows"
  3561. };
  3562. uint32 compiler = _subprocess_launcher.spawnv_async(subprocess_flags(), args, ENGINE_DIR);
  3563. int exit_status = _subprocess_launcher.wait(compiler);
  3564. if (exit_status != 0) {
  3565. loge("Failed to compile data. exit_status = %d".printf(exit_status));
  3566. return;
  3567. }
  3568. GLib.File engine_exe_src = File.new_for_path(EXE_PREFIX + "crown-%s".printf(config_name[config]) + EXE_SUFFIX);
  3569. GLib.File engine_exe_dst = File.new_for_path(Path.build_filename(package_path, exe_name + EXE_SUFFIX));
  3570. engine_exe_src.copy(engine_exe_dst, FileCopyFlags.OVERWRITE);
  3571. string openal_name = "openal-release.dll";
  3572. GLib.File openal_dll_src = File.new_for_path(openal_name);
  3573. GLib.File openal_dll_dst = File.new_for_path(Path.build_filename(package_path, openal_name));
  3574. openal_dll_src.copy(openal_dll_dst, FileCopyFlags.OVERWRITE);
  3575. try {
  3576. string lua_name = "lua51.dll";
  3577. GLib.File lua_dll_src = File.new_for_path(lua_name);
  3578. GLib.File lua_dll_dst = File.new_for_path(Path.build_filename(package_path, lua_name));
  3579. lua_dll_src.copy(lua_dll_dst, FileCopyFlags.OVERWRITE);
  3580. } catch (Error e) {
  3581. // Ignore: runtime's MinGW builds statically link luajit.
  3582. }
  3583. } catch (Error e) {
  3584. loge("%s".printf(e.message));
  3585. loge("Failed to deploy '%s'".printf(app_title));
  3586. return;
  3587. }
  3588. logi("Done: #FILE(%s)".printf(package_path));
  3589. }
  3590. public void on_create_package_windows(GLib.SimpleAction action, GLib.Variant? param)
  3591. {
  3592. var output_path = (string)param.get_child_value(0);
  3593. var config = (int)param.get_child_value(1);
  3594. var app_title = (string)param.get_child_value(2);
  3595. var exe_name = app_title.replace(" ", "_").down();
  3596. string config_path;
  3597. GLib.File package_dir = deploy_package_dir(out config_path
  3598. , output_path
  3599. , exe_name
  3600. , TargetPlatform.WINDOWS
  3601. , TargetArch.X64
  3602. , (TargetConfig)config
  3603. );
  3604. if (package_dir.query_exists()) {
  3605. Gtk.Dialog dlg = new_package_dir_exists_dialog(package_dir.get_basename());
  3606. dlg.response.connect((response_id) => {
  3607. if (response_id == Gtk.ResponseType.YES) {
  3608. try {
  3609. delete_tree(package_dir);
  3610. package_dir.make_directory_with_parents();
  3611. do_create_package_windows(package_dir, output_path, config, app_title, exe_name);
  3612. } catch (Error e) {
  3613. loge(e.message);
  3614. }
  3615. }
  3616. dlg.destroy();
  3617. });
  3618. dlg.show_all();
  3619. } else {
  3620. try {
  3621. package_dir.make_directory_with_parents();
  3622. do_create_package_windows(package_dir, output_path, config, app_title, exe_name);
  3623. } catch (Error e) {
  3624. loge(e.message);
  3625. }
  3626. }
  3627. }
  3628. public void on_unit_save_as_prefab(GLib.SimpleAction action, GLib.Variant? param)
  3629. {
  3630. string guid = (string)param.get_child_value(0);
  3631. string name = (string)param.get_child_value(1);
  3632. SaveResourceDialog srd = new SaveResourceDialog("Save Prefab As..."
  3633. , this.active_window
  3634. , OBJECT_TYPE_UNIT
  3635. , name
  3636. , _project
  3637. );
  3638. srd.safer_response.connect((response_id, path) => {
  3639. if (response_id == Gtk.ResponseType.ACCEPT && path != null) {
  3640. string prefab_filename = _project.resource_filename(path);
  3641. string prefab_path = ResourceId.normalize(prefab_filename);
  3642. string prefab_name = ResourceId.name(prefab_path);
  3643. Guid unit_id = Guid.parse(guid);
  3644. if (Unit(_database, unit_id).prefab() == prefab_name) {
  3645. Gtk.MessageDialog md = new Gtk.MessageDialog(this.active_window
  3646. , Gtk.DialogFlags.MODAL
  3647. , Gtk.MessageType.ERROR
  3648. , Gtk.ButtonsType.NONE
  3649. , "Cannot save prefab '%s' over itself".printf(prefab_name)
  3650. );
  3651. md.add_button("_Ok", Gtk.ResponseType.OK);
  3652. md.set_default_response(Gtk.ResponseType.OK);
  3653. md.response.connect(() => { md.destroy(); });
  3654. md.show_all();
  3655. } else if (_database.has_object(unit_id)) {
  3656. Guid prefab_id = Guid.new_guid();
  3657. Database new_database = new Database(_project);
  3658. _database.duplicate(unit_id, prefab_id, new_database);
  3659. new_database.save(path, prefab_id);
  3660. compile_and_reload.begin((obj, res) => {
  3661. if (compile_and_reload.end(res)) {
  3662. _level.replace_unit(unit_id, prefab_name);
  3663. }
  3664. });
  3665. }
  3666. }
  3667. srd.destroy();
  3668. });
  3669. srd.show_all();
  3670. srd.present();
  3671. }
  3672. public void on_open_unit(GLib.SimpleAction action, GLib.Variant? param)
  3673. {
  3674. string unit_name = param.get_string();
  3675. if (_unit_editor_dialog == null) {
  3676. _unit_editor_dialog = new UnitEditor(this
  3677. , _project
  3678. , "core/editors/level_editor"
  3679. , "127.0.0.1"
  3680. , 10444
  3681. , (uint)_preferences_dialog._undo_redo_max_size.value * 1024 * 1024
  3682. );
  3683. _unit_editor_dialog.set_transient_for(_level_editor_window);
  3684. _unit_editor_dialog.saved.connect(on_unit_editor_saved);
  3685. _runtimes.add(_unit_editor_dialog._runtime);
  3686. }
  3687. _unit_editor_dialog.show_all();
  3688. _unit_editor_dialog.present();
  3689. _unit_editor_dialog.set_unit(unit_name);
  3690. }
  3691. public void on_unit_editor_saved()
  3692. {
  3693. compile_and_reload.begin((obj, res) => {
  3694. if (compile_and_reload.end(res)) {
  3695. ui_read_selection(_database_editor._selection.to_array());
  3696. }
  3697. });
  3698. }
  3699. public void set_autosave_timer(uint minutes)
  3700. {
  3701. if (_save_timer_id > 0)
  3702. GLib.Source.remove(_save_timer_id);
  3703. _save_timer_id = GLib.Timeout.add_seconds(minutes*60, save_timeout);
  3704. }
  3705. public void menu_set_enabled(bool enabled, GLib.ActionEntry[] entries, string[]? whitelist = null)
  3706. {
  3707. for (int ii = 0; ii < entries.length; ++ii) {
  3708. string action_name = entries[ii].name;
  3709. int jj = 0;
  3710. if (whitelist != null) {
  3711. for (; jj < whitelist.length; ++jj) {
  3712. if (action_name == whitelist[jj])
  3713. break;
  3714. }
  3715. }
  3716. if (whitelist == null || whitelist != null && jj == whitelist.length) {
  3717. GLib.SimpleAction sa = this.lookup_action(action_name) as GLib.SimpleAction;
  3718. if (sa != null)
  3719. sa.set_enabled(enabled);
  3720. }
  3721. }
  3722. }
  3723. public void set_conflicting_accels(bool on)
  3724. {
  3725. if (on) {
  3726. this.set_accels_for_action("app.tool(0)", _tool_place_accels);
  3727. this.set_accels_for_action("app.tool(1)", _tool_move_accels);
  3728. this.set_accels_for_action("app.tool(2)", _tool_rotate_accels);
  3729. this.set_accels_for_action("app.tool(3)", _tool_scale_accels);
  3730. this.set_accels_for_action("app.delete", _delete_accels);
  3731. this.set_accels_for_action("viewport.camera-view(0)", _camera_view_perspective_accels);
  3732. this.set_accels_for_action("viewport.camera-view(1)", _camera_view_front_accels);
  3733. this.set_accels_for_action("viewport.camera-view(2)", _camera_view_back_accels);
  3734. this.set_accels_for_action("viewport.camera-view(3)", _camera_view_right_accels);
  3735. this.set_accels_for_action("viewport.camera-view(4)", _camera_view_left_accels);
  3736. this.set_accels_for_action("viewport.camera-view(5)", _camera_view_top_accels);
  3737. this.set_accels_for_action("viewport.camera-view(6)", _camera_view_bottom_accels);
  3738. this.set_accels_for_action("viewport.camera-frame-selected", _camera_frame_selected_accels);
  3739. this.set_accels_for_action("app.camera-frame-all", _camera_frame_all_accels);
  3740. } else {
  3741. this.set_accels_for_action("app.tool(0)", {});
  3742. this.set_accels_for_action("app.tool(1)", {});
  3743. this.set_accels_for_action("app.tool(2)", {});
  3744. this.set_accels_for_action("app.tool(3)", {});
  3745. this.set_accels_for_action("app.delete", {});
  3746. this.set_accels_for_action("viewport.camera-view(0)", {});
  3747. this.set_accels_for_action("viewport.camera-view(1)", {});
  3748. this.set_accels_for_action("viewport.camera-view(2)", {});
  3749. this.set_accels_for_action("viewport.camera-view(3)", {});
  3750. this.set_accels_for_action("viewport.camera-view(4)", {});
  3751. this.set_accels_for_action("viewport.camera-view(5)", {});
  3752. this.set_accels_for_action("viewport.camera-view(6)", {});
  3753. this.set_accels_for_action("viewport.camera-frame-selected", {});
  3754. this.set_accels_for_action("app.camera-frame-all", {});
  3755. }
  3756. }
  3757. public void entry_any_focus_in(Gtk.Widget widget)
  3758. {
  3759. set_conflicting_accels(false);
  3760. }
  3761. public void entry_any_focus_out(Gtk.Widget widget)
  3762. {
  3763. set_conflicting_accels(true);
  3764. }
  3765. public void show_panel(string name, Gtk.StackTransitionType stt = Gtk.StackTransitionType.NONE)
  3766. {
  3767. _main_stack.set_visible_child_full(name, stt);
  3768. if (name == PANEL_EDITOR) {
  3769. // FIXME: save/restore last known window state
  3770. int win_w;
  3771. int win_h;
  3772. this.active_window.get_size(out win_w, out win_h);
  3773. _editor_pane.set_position(320);
  3774. _content_pane.set_position(win_h - 250);
  3775. _inspector_pane.set_position(win_h - 600);
  3776. _main_pane.set_position(win_w - 375);
  3777. menu_set_enabled(true, action_entries_file);
  3778. menu_set_enabled(true, action_entries_edit);
  3779. menu_set_enabled(true, action_entries_create);
  3780. menu_set_enabled(true, action_entries_camera);
  3781. menu_set_enabled(true, action_entries_view);
  3782. menu_set_enabled(true, action_entries_debug);
  3783. menu_set_enabled(true, action_entries_help);
  3784. } else if (name == PANEL_PROJECTS_LIST
  3785. || name == PANEL_NEW_PROJECT
  3786. ) {
  3787. menu_set_enabled(false, action_entries_file, {"new-project", "add-project", "open-project", "open-project-null", "open-projects-list", "remove-project", "quit"});
  3788. menu_set_enabled(false, action_entries_edit);
  3789. menu_set_enabled(false, action_entries_create);
  3790. menu_set_enabled(false, action_entries_camera);
  3791. menu_set_enabled(false, action_entries_view);
  3792. menu_set_enabled(false, action_entries_debug);
  3793. menu_set_enabled(true, action_entries_help);
  3794. }
  3795. }
  3796. public void on_set_placeable(GLib.SimpleAction action, GLib.Variant? param)
  3797. {
  3798. _placeable_type = (string)param.get_child_value(0);
  3799. _placeable_name = (string)param.get_child_value(1);
  3800. _editor.send_script(LevelEditorApi.set_placeable(_placeable_type, _placeable_name));
  3801. activate_action("tool", new GLib.Variant.int32(ToolType.PLACE));
  3802. }
  3803. public void on_project_reset()
  3804. {
  3805. if (!_project.is_loaded())
  3806. return;
  3807. if (!_project.is_temporary()) {
  3808. // Save per-project data.
  3809. try {
  3810. string path = GLib.Path.build_filename(_project.user_dir(), "project_store.sjson");
  3811. SJSON.save(_project_store.encode(), path);
  3812. } catch (JsonWriteError e) {
  3813. loge(e.message);
  3814. }
  3815. }
  3816. // Destroy dialogs.
  3817. _texture_settings_dialog = null;
  3818. _deploy_dialog = null;
  3819. }
  3820. public void on_project_loaded()
  3821. {
  3822. // Load per-project data.
  3823. try {
  3824. string path = GLib.Path.build_filename(_project.user_dir(), "project_store.sjson");
  3825. _project_store.decode(SJSON.load_from_path(path));
  3826. }
  3827. catch (JsonSyntaxError e) {
  3828. // No-op.
  3829. }
  3830. }
  3831. public SelectResourceDialog new_select_resource_dialog(string resource_type)
  3832. {
  3833. return new SelectResourceDialog(resource_type, _project_store, this.active_window);
  3834. }
  3835. public void on_level_treeview_selection_changed(Gee.ArrayList<Guid?> selection)
  3836. {
  3837. _database_editor.selection_read(selection.to_array());
  3838. ui_read_selection.disconnect(_level_treeview.read_selection);
  3839. ui_read_selection(_database_editor._selection.to_array());
  3840. ui_read_selection.connect(_level_treeview.read_selection);
  3841. _database_editor.send_selection(_editor);
  3842. _editor_viewport.frame();
  3843. }
  3844. public void on_selection_changed()
  3845. {
  3846. ui_read_selection(_database_editor._selection.to_array());
  3847. _database_editor.send_selection(_editor);
  3848. _editor_viewport.frame();
  3849. }
  3850. }
  3851. // Global paths
  3852. public static GLib.File _toolchain_dir;
  3853. public static GLib.File _templates_dir;
  3854. public static GLib.File _data_dir;
  3855. public static GLib.File _config_dir;
  3856. public static GLib.File _cache_dir;
  3857. public static GLib.File _logs_dir;
  3858. public static GLib.File _thumbnails_dir;
  3859. public static GLib.File _thumbnails_normal_dir;
  3860. public static GLib.File _documents_dir;
  3861. public static GLib.File _log_file;
  3862. public static GLib.File _settings_file;
  3863. public static GLib.File _user_file;
  3864. public static GLib.File _console_history_file;
  3865. public static GLib.File _window_state_file;
  3866. public static GLib.FileStream _log_stream;
  3867. public static ConsoleView _console_view;
  3868. public static bool _console_view_valid = false;
  3869. public static string _log_prefix;
  3870. public static SubprocessLauncher _subprocess_launcher;
  3871. public static void log(string system, string severity, string message)
  3872. {
  3873. GLib.DateTime now = new GLib.DateTime.now_local();
  3874. int now_us = now.get_microsecond();
  3875. string now_str = now.format("%H:%M:%S");
  3876. string plain_text_line = "%s.%06d %.4s %s: %s\n".printf(now_str
  3877. , now_us
  3878. , severity.ascii_up()
  3879. , system
  3880. , message
  3881. );
  3882. if (_log_stream != null) {
  3883. _log_stream.puts(plain_text_line);
  3884. _log_stream.flush();
  3885. }
  3886. if (_console_view_valid) {
  3887. string line = "%s: %s\n".printf(system, message);
  3888. string time = "%s.%06d ".printf(now_str, now_us);
  3889. _console_view.log(time, severity, line);
  3890. }
  3891. }
  3892. public static void logi(string message)
  3893. {
  3894. log(_log_prefix, "info", message);
  3895. }
  3896. public static void logw(string message)
  3897. {
  3898. log(_log_prefix, "warning", message);
  3899. }
  3900. public static void loge(string message)
  3901. {
  3902. log(_log_prefix, "error", message);
  3903. }
  3904. public void open_directory(string directory)
  3905. {
  3906. #if CROWN_PLATFORM_LINUX
  3907. try {
  3908. GLib.AppInfo.launch_default_for_uri("file://" + directory, null);
  3909. } catch (Error e) {
  3910. loge(e.message);
  3911. }
  3912. #else
  3913. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  3914. try {
  3915. sl.spawnv({ "explorer.exe", directory, null });
  3916. } catch (Error e) {
  3917. loge(e.message);
  3918. }
  3919. #endif
  3920. }
  3921. public void open_text_editor(string path)
  3922. {
  3923. #if CROWN_PLATFORM_WINDOWS
  3924. GLib.SubprocessLauncher sl = new GLib.SubprocessLauncher(subprocess_flags());
  3925. try {
  3926. sl.spawnv({ "notepad.exe", path, null });
  3927. } catch (Error e) {
  3928. loge(e.message);
  3929. }
  3930. #endif
  3931. }
  3932. public static GLib.SubprocessFlags subprocess_flags()
  3933. {
  3934. GLib.SubprocessFlags flags = SubprocessFlags.NONE;
  3935. #if !CROWN_DEBUG
  3936. flags |= SubprocessFlags.STDOUT_SILENCE | SubprocessFlags.STDERR_SILENCE;
  3937. #endif
  3938. return flags;
  3939. }
  3940. public static bool is_directory_empty(string path)
  3941. {
  3942. GLib.File file = GLib.File.new_for_path(path);
  3943. try {
  3944. FileEnumerator enumerator = file.enumerate_children("standard::*"
  3945. , FileQueryInfoFlags.NOFOLLOW_SYMLINKS
  3946. );
  3947. return enumerator.next_file() == null;
  3948. } catch (GLib.Error e) {
  3949. loge(e.message);
  3950. }
  3951. return false;
  3952. }
  3953. public static int copy_tree(GLib.File dst, GLib.File src, GLib.FileCopyFlags flags = GLib.FileCopyFlags.NONE)
  3954. {
  3955. try {
  3956. GLib.FileType src_type = src.query_file_type(GLib.FileQueryInfoFlags.NONE);
  3957. if (src_type == GLib.FileType.DIRECTORY) {
  3958. if (dst.query_exists() == false)
  3959. dst.make_directory();
  3960. string dst_path = dst.get_path();
  3961. string src_path = src.get_path();
  3962. GLib.FileEnumerator fe = src.enumerate_children(GLib.FileAttribute.STANDARD_NAME, GLib.FileQueryInfoFlags.NONE);
  3963. for (GLib.FileInfo? info = fe.next_file(); info != null; info = fe.next_file()) {
  3964. if (copy_tree(GLib.File.new_for_path(GLib.Path.build_filename(dst_path, info.get_name()))
  3965. , GLib.File.new_for_path(GLib.Path.build_filename(src_path, info.get_name()))
  3966. , flags
  3967. ) != 0) {
  3968. fe.close(null);
  3969. return -1;
  3970. }
  3971. }
  3972. } else if (src_type == GLib.FileType.REGULAR) {
  3973. src.copy(dst, flags);
  3974. }
  3975. } catch (GLib.Error e) {
  3976. loge(e.message);
  3977. return -1;
  3978. }
  3979. return 0;
  3980. }
  3981. public void device_frame_delayed(uint delay_ms, RuntimeInstance runtime)
  3982. {
  3983. // FIXME: find a way to time exactly when it is effective to queue a redraw.
  3984. // See: https://blogs.gnome.org/jnelson/2010/10/13/those-realize-map-widget-signals/
  3985. GLib.Timeout.add_full(GLib.Priority.DEFAULT, delay_ms, () => {
  3986. runtime.send(DeviceApi.frame());
  3987. return GLib.Source.REMOVE;
  3988. });
  3989. }
  3990. public static int main(string[] args)
  3991. {
  3992. // If args does not contain --child, spawn the launcher.
  3993. int ii;
  3994. for (ii = 0; ii < args.length; ++ii) {
  3995. if (args[ii] == "--launcher") {
  3996. break;
  3997. }
  3998. }
  3999. if (ii == args.length) {
  4000. _log_prefix = "editor";
  4001. } else {
  4002. _log_prefix = "launcher";
  4003. // Remove --child from args for backward compatibility.
  4004. if (args.length > 1)
  4005. args = args[0 : args.length - 1];
  4006. }
  4007. // Redirect GLib logs to internal log*().
  4008. GLib.set_print_handler((msg) => { logi(msg); });
  4009. GLib.set_printerr_handler((msg) => { loge(msg); });
  4010. GLib.Log.set_writer_func((log_level, fields) => {
  4011. foreach (var field in fields) {
  4012. if (field.key == "MESSAGE") {
  4013. switch (log_level) {
  4014. case LEVEL_DEBUG:
  4015. #if CROWN_DEBUG
  4016. logi((string)field.value);
  4017. #endif
  4018. break;
  4019. case LEVEL_INFO:
  4020. case LEVEL_MESSAGE:
  4021. logi((string)field.value);
  4022. break;
  4023. case LEVEL_CRITICAL:
  4024. case LEVEL_WARNING:
  4025. logw((string)field.value);
  4026. break;
  4027. case LEVEL_ERROR:
  4028. loge((string)field.value);
  4029. break;
  4030. default:
  4031. logw((string)field.value);
  4032. break;
  4033. }
  4034. return GLib.LogWriterOutput.HANDLED;
  4035. }
  4036. }
  4037. return GLib.LogWriterOutput.UNHANDLED;
  4038. });
  4039. // Global paths
  4040. _data_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_data_dir(), "crown"));
  4041. try {
  4042. _data_dir.make_directory();
  4043. } catch (Error e) {
  4044. /* Nobody cares */
  4045. }
  4046. _config_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_config_dir(), "crown"));
  4047. try {
  4048. _config_dir.make_directory();
  4049. } catch (Error e) {
  4050. /* Nobody cares */
  4051. }
  4052. _cache_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_cache_dir(), "crown"));
  4053. try {
  4054. _cache_dir.make_directory();
  4055. } catch (Error e) {
  4056. /* Nobody cares */
  4057. }
  4058. _logs_dir = GLib.File.new_for_path(GLib.Path.build_filename(_data_dir.get_path(), "logs"));
  4059. try {
  4060. _logs_dir.make_directory();
  4061. } catch (Error e) {
  4062. /* Nobody cares */
  4063. }
  4064. _documents_dir = GLib.File.new_for_path(GLib.Environment.get_user_special_dir(GLib.UserDirectory.DOCUMENTS));
  4065. _thumbnails_dir = GLib.File.new_for_path(GLib.Path.build_filename(GLib.Environment.get_user_cache_dir(), "thumbnails"));
  4066. try {
  4067. _thumbnails_dir.make_directory();
  4068. } catch (Error e) {
  4069. /* Nobody cares */
  4070. }
  4071. _thumbnails_normal_dir = GLib.File.new_for_path(GLib.Path.build_filename(_thumbnails_dir.get_path(), "normal"));
  4072. try {
  4073. _thumbnails_normal_dir.make_directory();
  4074. } catch (Error e) {
  4075. /* Nobody cares */
  4076. }
  4077. _log_file = GLib.File.new_for_path(GLib.Path.build_filename(_logs_dir.get_path(), new GLib.DateTime.now_local().format("%Y-%m-%d") + ".log"));
  4078. _log_stream = GLib.FileStream.open(_log_file.get_path(), "a");
  4079. if (_log_prefix == "launcher")
  4080. return launcher_main(args);
  4081. // Spawn launcher process.
  4082. try {
  4083. string[] child_args = args;
  4084. child_args += "--launcher";
  4085. #if CROWN_PLATFORM_WINDOWS
  4086. child_args += ((uint64)OpenProcess(0x00100000 /* SYNCHRONIZE */, true, GetCurrentProcessId())).to_string();
  4087. #elif CROWN_PLATFORM_LINUX
  4088. child_args += ((uint64)getpid()).to_string();
  4089. #endif
  4090. GLib.Pid launcher_pid;
  4091. GLib.Process.spawn_async(null
  4092. , child_args
  4093. , null
  4094. , 0
  4095. , null
  4096. , out launcher_pid
  4097. );
  4098. } catch (GLib.SpawnError e) {
  4099. loge("%s".printf(e.message));
  4100. return 1;
  4101. }
  4102. _settings_file = GLib.File.new_for_path(GLib.Path.build_filename(_config_dir.get_path(), "settings.sjson"));
  4103. _window_state_file = GLib.File.new_for_path(GLib.Path.build_filename(_data_dir.get_path(), "window.sjson"));
  4104. _user_file = GLib.File.new_for_path(GLib.Path.build_filename(_data_dir.get_path(), "user.sjson"));
  4105. _console_history_file = GLib.File.new_for_path(GLib.Path.build_filename(_data_dir.get_path(), "console_history.txt"));
  4106. // Find toolchain path, more desirable paths come first.
  4107. ii = 0;
  4108. string toolchain_paths[] =
  4109. {
  4110. "../../..", // Relative path in release package.
  4111. "../../../samples", // Relative path in git worktree.
  4112. ".",
  4113. };
  4114. for (ii = 0; ii < toolchain_paths.length; ++ii) {
  4115. string path = Path.build_filename(toolchain_paths[ii], "core");
  4116. if (GLib.FileUtils.test(path, FileTest.EXISTS) && GLib.FileUtils.test(path, FileTest.IS_DIR)) {
  4117. _toolchain_dir = File.new_for_path(path).get_parent();
  4118. break;
  4119. }
  4120. }
  4121. if (ii == toolchain_paths.length) {
  4122. loge("Unable to find the toolchain directory");
  4123. return 1;
  4124. }
  4125. // Find templates path, more desirable paths come first.
  4126. string templates_path[] =
  4127. {
  4128. "../../..", // Relative path in release package or git worktree.
  4129. ".",
  4130. };
  4131. for (ii = 0; ii < templates_path.length; ++ii) {
  4132. string path = Path.build_filename(templates_path[ii], "samples");
  4133. if (GLib.FileUtils.test(path, FileTest.EXISTS) && GLib.FileUtils.test(path, FileTest.IS_DIR)) {
  4134. _templates_dir = File.new_for_path(path);
  4135. break;
  4136. }
  4137. }
  4138. if (ii == templates_path.length) {
  4139. loge("Unable to find the templates directory");
  4140. return 1;
  4141. }
  4142. #if CROWN_PLATFORM_LINUX
  4143. Gdk.set_allowed_backends("x11");
  4144. #endif
  4145. // Use fontconfig backend.
  4146. Pango.FontMap fontmap = Pango.CairoFontMap.new_for_font_type(Cairo.FontType.FT);
  4147. Pango.CairoFontMap.set_default((Pango.CairoFontMap)fontmap);
  4148. LevelEditorApplication app = new LevelEditorApplication();
  4149. return app.run(args);
  4150. }
  4151. } /* namespace Crown */