level_editor.vala 130 KB

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