level_editor.vala 141 KB

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