level_editor.vala 143 KB

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