level_editor.vala 144 KB

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