level_editor.vala 141 KB

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