laya.quickgamemini.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. window.qgMiniGame = function (exports, Laya) {
  2. 'use strict';
  3. class MiniFileMgr {
  4. static isLocalNativeFile(url) {
  5. for (var i = 0, sz = QGMiniAdapter.nativefiles.length; i < sz; i++) {
  6. if (url.indexOf(QGMiniAdapter.nativefiles[i]) != -1)
  7. return true;
  8. }
  9. return false;
  10. }
  11. static getFileInfo(fileUrl) {
  12. var fileNativePath = fileUrl;
  13. var fileObj = MiniFileMgr.fakeObj[fileNativePath];
  14. if (fileObj == null)
  15. return null;
  16. else
  17. return fileObj;
  18. return null;
  19. }
  20. static read(filePath, encoding = "utf8", callBack = null, readyUrl = "", isSaveFile = false, fileType = "") {
  21. var fileUrl;
  22. if (readyUrl != "" && (readyUrl.indexOf("http://") != -1 || readyUrl.indexOf("https://") != -1)) {
  23. fileUrl = MiniFileMgr.getFileNativePath(filePath);
  24. }
  25. else {
  26. fileUrl = filePath;
  27. }
  28. fileUrl = Laya.URL.getAdptedFilePath(fileUrl);
  29. MiniFileMgr.fs.readFile({ filePath: fileUrl, encoding: encoding, success: function (data) {
  30. callBack != null && callBack.runWith([0, data]);
  31. }, fail: function (data) {
  32. if (data && readyUrl != "")
  33. MiniFileMgr.downFiles(readyUrl, encoding, callBack, readyUrl, isSaveFile, fileType);
  34. else
  35. callBack != null && callBack.runWith([1]);
  36. } });
  37. }
  38. static downFiles(fileUrl, encoding = "ascii", callBack = null, readyUrl = "", isSaveFile = false, fileType = "", isAutoClear = true) {
  39. var downloadTask = MiniFileMgr.wxdown({ url: fileUrl, success: function (data) {
  40. if (data.statusCode === 200)
  41. MiniFileMgr.readFile(data.tempFilePath, encoding, callBack, readyUrl, isSaveFile, fileType, isAutoClear);
  42. else if (data.statusCode === 403) {
  43. callBack != null && callBack.runWith([0, fileUrl]);
  44. }
  45. else {
  46. callBack != null && callBack.runWith([1, data]);
  47. }
  48. }, fail: function (data) {
  49. callBack != null && callBack.runWith([1, data]);
  50. } });
  51. downloadTask.onProgressUpdate(function (data) {
  52. callBack != null && callBack.runWith([2, data.progress]);
  53. });
  54. }
  55. static readFile(filePath, encoding = "utf8", callBack = null, readyUrl = "", isSaveFile = false, fileType = "", isAutoClear = true) {
  56. filePath = Laya.URL.getAdptedFilePath(filePath);
  57. MiniFileMgr.fs.readFile({ filePath: filePath, encoding: encoding, success: function (data) {
  58. if (filePath.indexOf(QGMiniAdapter.window.qg.env.USER_DATA_PATH) == -1 && (filePath.indexOf("http://") != -1 || filePath.indexOf("https://") != -1)) {
  59. if (QGMiniAdapter.autoCacheFile || isSaveFile) {
  60. callBack != null && callBack.runWith([0, data]);
  61. MiniFileMgr.copyFile(filePath, readyUrl, null, encoding, isAutoClear);
  62. }
  63. else
  64. callBack != null && callBack.runWith([0, data]);
  65. }
  66. else
  67. callBack != null && callBack.runWith([0, data]);
  68. }, fail: function (data) {
  69. if (data)
  70. callBack != null && callBack.runWith([1, data]);
  71. } });
  72. }
  73. static downOtherFiles(fileUrl, callBack = null, readyUrl = "", isSaveFile = false, isAutoClear = true) {
  74. MiniFileMgr.wxdown({ url: fileUrl, success: function (data) {
  75. if (data.statusCode === 200) {
  76. if ((QGMiniAdapter.autoCacheFile || isSaveFile) && readyUrl.indexOf("qlogo.cn") == -1 && readyUrl.indexOf(".php") == -1) {
  77. callBack != null && callBack.runWith([0, data.tempFilePath]);
  78. MiniFileMgr.copyFile(data.tempFilePath, readyUrl, null, "", isAutoClear);
  79. }
  80. else
  81. callBack != null && callBack.runWith([0, data.tempFilePath]);
  82. }
  83. else {
  84. callBack != null && callBack.runWith([1, data]);
  85. }
  86. }, fail: function (data) {
  87. callBack != null && callBack.runWith([1, data]);
  88. } });
  89. }
  90. static downLoadFile(fileUrl, fileType = "", callBack = null, encoding = "ascii") {
  91. if (QGMiniAdapter.window.navigator.userAgent.indexOf('MiniGame') < 0 && QGMiniAdapter.window.navigator.userAgent.indexOf('OPPO') < 0) {
  92. Laya.Laya.loader.load(fileUrl, callBack);
  93. }
  94. else {
  95. if (fileType == Laya.Loader.IMAGE || fileType == Laya.Loader.SOUND)
  96. MiniFileMgr.downOtherFiles(fileUrl, callBack, fileUrl, true, false);
  97. else
  98. MiniFileMgr.downFiles(fileUrl, encoding, callBack, fileUrl, true, fileType, false);
  99. }
  100. }
  101. static copyFile(tempFilePath, readyUrl, callBack, encoding = "", isAutoClear = true) {
  102. var temp = tempFilePath.split("/");
  103. var tempFileName = temp[temp.length - 1];
  104. var fileurlkey = readyUrl;
  105. var fileObj = MiniFileMgr.getFileInfo(readyUrl);
  106. var saveFilePath = MiniFileMgr.getFileNativePath(tempFileName);
  107. MiniFileMgr.fakeObj[fileurlkey] = { md5: tempFileName, readyUrl: readyUrl, size: 0, times: Laya.Browser.now(), encoding: encoding };
  108. var totalSize = 50 * 1024 * 1024;
  109. var chaSize = 4 * 1024 * 1024;
  110. var fileUseSize = MiniFileMgr.getCacheUseSize();
  111. if (fileObj) {
  112. if (fileObj.readyUrl != readyUrl) {
  113. MiniFileMgr.fs.getFileInfo({
  114. filePath: tempFilePath,
  115. success: function (data) {
  116. if ((isAutoClear && (fileUseSize + chaSize + data.size) >= totalSize)) {
  117. if (data.size > QGMiniAdapter.minClearSize)
  118. QGMiniAdapter.minClearSize = data.size;
  119. MiniFileMgr.onClearCacheRes();
  120. }
  121. MiniFileMgr.deleteFile(tempFilePath, readyUrl, callBack, encoding, data.size);
  122. },
  123. fail: function (data) {
  124. callBack != null && callBack.runWith([1, data]);
  125. }
  126. });
  127. }
  128. else
  129. callBack != null && callBack.runWith([0]);
  130. }
  131. else {
  132. MiniFileMgr.fs.getFileInfo({
  133. filePath: tempFilePath,
  134. success: function (data) {
  135. if ((isAutoClear && (fileUseSize + chaSize + data.size) >= totalSize)) {
  136. if (data.size > QGMiniAdapter.minClearSize)
  137. QGMiniAdapter.minClearSize = data.size;
  138. MiniFileMgr.onClearCacheRes();
  139. }
  140. MiniFileMgr.fs.copyFile({ srcPath: tempFilePath, destPath: saveFilePath, success: function (data2) {
  141. MiniFileMgr.onSaveFile(readyUrl, tempFileName, true, encoding, callBack, data.size);
  142. }, fail: function (data) {
  143. callBack != null && callBack.runWith([1, data]);
  144. } });
  145. },
  146. fail: function (data) {
  147. callBack != null && callBack.runWith([1, data]);
  148. }
  149. });
  150. }
  151. }
  152. static onClearCacheRes() {
  153. var memSize = QGMiniAdapter.minClearSize;
  154. var tempFileListArr = [];
  155. for (var key in MiniFileMgr.filesListObj) {
  156. if (key != "fileUsedSize")
  157. tempFileListArr.push(MiniFileMgr.filesListObj[key]);
  158. }
  159. MiniFileMgr.sortOn(tempFileListArr, "times", MiniFileMgr.NUMERIC);
  160. var clearSize = 0;
  161. for (var i = 1, sz = tempFileListArr.length; i < sz; i++) {
  162. var fileObj = tempFileListArr[i];
  163. if (clearSize >= memSize)
  164. break;
  165. clearSize += fileObj.size;
  166. MiniFileMgr.deleteFile("", fileObj.readyUrl);
  167. }
  168. }
  169. static sortOn(array, name, options = 0) {
  170. if (options == MiniFileMgr.NUMERIC)
  171. return array.sort(function (a, b) { return a[name] - b[name]; });
  172. if (options == (MiniFileMgr.NUMERIC | MiniFileMgr.DESCENDING))
  173. return array.sort(function (a, b) { return b[name] - a[name]; });
  174. return array.sort(function (a, b) { return a[name] - b[name]; });
  175. }
  176. static getFileNativePath(fileName) {
  177. return MiniFileMgr.fileNativeDir + "/" + fileName;
  178. }
  179. static deleteFile(tempFileName, readyUrl = "", callBack = null, encoding = "", fileSize = 0) {
  180. var fileObj = MiniFileMgr.getFileInfo(readyUrl);
  181. var deleteFileUrl = MiniFileMgr.getFileNativePath(fileObj.md5);
  182. var isAdd = tempFileName != "" ? true : false;
  183. MiniFileMgr.onSaveFile(readyUrl, tempFileName, isAdd, encoding, callBack, fileSize);
  184. MiniFileMgr.fs.unlink({ filePath: deleteFileUrl, success: function (data) {
  185. if (tempFileName != "") {
  186. var saveFilePath = MiniFileMgr.getFileNativePath(tempFileName);
  187. MiniFileMgr.fs.copyFile({ srcPath: tempFileName, destPath: saveFilePath, success: function (data) {
  188. }, fail: function (data) {
  189. callBack != null && callBack.runWith([1, data]);
  190. } });
  191. }
  192. }, fail: function (data) {
  193. } });
  194. }
  195. static deleteAll() {
  196. var tempFileListArr = [];
  197. for (var key in MiniFileMgr.filesListObj) {
  198. if (key != "fileUsedSize")
  199. tempFileListArr.push(MiniFileMgr.filesListObj[key]);
  200. }
  201. for (var i = 1, sz = tempFileListArr.length; i < sz; i++) {
  202. var fileObj = tempFileListArr[i];
  203. MiniFileMgr.deleteFile("", fileObj.readyUrl);
  204. }
  205. if (MiniFileMgr.filesListObj && MiniFileMgr.filesListObj.fileUsedSize) {
  206. MiniFileMgr.filesListObj.fileUsedSize = 0;
  207. }
  208. MiniFileMgr.writeFilesList("", JSON.stringify({}), false);
  209. }
  210. static onSaveFile(readyUrl, md5Name, isAdd = true, encoding = "", callBack = null, fileSize = 0) {
  211. var fileurlkey = readyUrl;
  212. if (MiniFileMgr.filesListObj['fileUsedSize'] == null)
  213. MiniFileMgr.filesListObj['fileUsedSize'] = 0;
  214. if (isAdd) {
  215. var fileNativeName = MiniFileMgr.getFileNativePath(md5Name);
  216. MiniFileMgr.filesListObj[fileurlkey] = { md5: md5Name, readyUrl: readyUrl, size: fileSize, times: Laya.Browser.now(), encoding: encoding };
  217. MiniFileMgr.filesListObj['fileUsedSize'] = parseInt(MiniFileMgr.filesListObj['fileUsedSize']) + fileSize;
  218. MiniFileMgr.writeFilesList(fileurlkey, JSON.stringify(MiniFileMgr.filesListObj), true);
  219. callBack != null && callBack.runWith([0]);
  220. }
  221. else {
  222. if (MiniFileMgr.filesListObj[fileurlkey]) {
  223. var deletefileSize = parseInt(MiniFileMgr.filesListObj[fileurlkey].size);
  224. MiniFileMgr.filesListObj['fileUsedSize'] = parseInt(MiniFileMgr.filesListObj['fileUsedSize']) - deletefileSize;
  225. if (MiniFileMgr.filesListObj[fileurlkey].md5 == MiniFileMgr.fakeObj[fileurlkey].md5) {
  226. delete MiniFileMgr.fakeObj[fileurlkey];
  227. }
  228. delete MiniFileMgr.filesListObj[fileurlkey];
  229. MiniFileMgr.writeFilesList(fileurlkey, JSON.stringify(MiniFileMgr.filesListObj), false);
  230. callBack != null && callBack.runWith([0]);
  231. }
  232. }
  233. }
  234. static writeFilesList(fileurlkey, filesListStr, isAdd) {
  235. var listFilesPath = MiniFileMgr.fileNativeDir + "/" + MiniFileMgr.fileListName;
  236. MiniFileMgr.fs.writeFile({ filePath: listFilesPath, encoding: 'utf8', data: filesListStr, success: function (data) {
  237. }, fail: function (data) {
  238. } });
  239. if (!QGMiniAdapter.isZiYu && QGMiniAdapter.isPosMsgYu && QGMiniAdapter.window.qg.postMessage) {
  240. QGMiniAdapter.window.qg.postMessage && QGMiniAdapter.window.qg.postMessage({ url: fileurlkey, data: MiniFileMgr.filesListObj[fileurlkey], isLoad: "filenative", isAdd: isAdd });
  241. }
  242. }
  243. static getCacheUseSize() {
  244. if (MiniFileMgr.filesListObj && MiniFileMgr.filesListObj['fileUsedSize'])
  245. return MiniFileMgr.filesListObj['fileUsedSize'];
  246. return 0;
  247. }
  248. static existDir(dirPath, callBack) {
  249. MiniFileMgr.fs.mkdir({ dirPath: dirPath, success: function (data) {
  250. callBack != null && callBack.runWith([0, { data: JSON.stringify({}) }]);
  251. }, fail: function (data) {
  252. if (data.errMsg.indexOf("file already exists") != -1)
  253. MiniFileMgr.readSync(MiniFileMgr.fileListName, "utf8", callBack);
  254. else
  255. callBack != null && callBack.runWith([1, data]);
  256. } });
  257. }
  258. static readSync(filePath, encoding = "utf8", callBack = null, readyUrl = "") {
  259. var fileUrl = MiniFileMgr.getFileNativePath(filePath);
  260. var filesListStr;
  261. try {
  262. MiniFileMgr.fs.readFile({
  263. filePath: fileUrl,
  264. encoding: encoding,
  265. success: function (data) {
  266. filesListStr = data.data;
  267. callBack != null && callBack.runWith([0, { data: filesListStr }]);
  268. },
  269. fail: function () {
  270. callBack != null && callBack.runWith([1]);
  271. }
  272. });
  273. }
  274. catch (error) {
  275. callBack != null && callBack.runWith([1]);
  276. }
  277. }
  278. static setNativeFileDir(value) {
  279. MiniFileMgr.fileNativeDir = QGMiniAdapter.window.qg.env.USER_DATA_PATH + value;
  280. }
  281. }
  282. MiniFileMgr.fs = window.qg.getFileSystemManager();
  283. MiniFileMgr.wxdown = window.qg.downloadFile;
  284. MiniFileMgr.filesListObj = {};
  285. MiniFileMgr.fakeObj = {};
  286. MiniFileMgr.fileListName = "layaairfiles.txt";
  287. MiniFileMgr.ziyuFileData = {};
  288. MiniFileMgr.ziyuFileTextureData = {};
  289. MiniFileMgr.loadPath = "";
  290. MiniFileMgr.DESCENDING = 2;
  291. MiniFileMgr.NUMERIC = 16;
  292. class MiniSoundChannel extends Laya.SoundChannel {
  293. constructor(audio, miniSound) {
  294. super();
  295. this._audio = audio;
  296. this._miniSound = miniSound;
  297. this._onEnd = MiniSoundChannel.bindToThis(this.__onEnd, this);
  298. audio.onEnded(this._onEnd);
  299. }
  300. static bindToThis(fun, scope) {
  301. var rst = fun;
  302. rst = fun.bind(scope);
  303. return rst;
  304. }
  305. __onEnd() {
  306. if (this.loops == 1) {
  307. if (this.completeHandler) {
  308. Laya.Laya.systemTimer.once(10, this, this.__runComplete, [this.completeHandler], false);
  309. this.completeHandler = null;
  310. }
  311. this.stop();
  312. this.event(Laya.Event.COMPLETE);
  313. return;
  314. }
  315. if (this.loops > 0) {
  316. this.loops--;
  317. }
  318. this.startTime = 0;
  319. this.play();
  320. }
  321. play() {
  322. this.isStopped = false;
  323. Laya.SoundManager.addChannel(this);
  324. this._audio.play();
  325. }
  326. set startTime(time) {
  327. if (this._audio) {
  328. this._audio.startTime = time;
  329. }
  330. }
  331. set autoplay(value) {
  332. this._audio.autoplay = value;
  333. }
  334. get autoplay() {
  335. return this._audio.autoplay;
  336. }
  337. get position() {
  338. if (!this._audio)
  339. return 0;
  340. return this._audio.currentTime;
  341. }
  342. get duration() {
  343. if (!this._audio)
  344. return 0;
  345. return this._audio.duration;
  346. }
  347. stop() {
  348. this.isStopped = true;
  349. Laya.SoundManager.removeChannel(this);
  350. this.completeHandler = null;
  351. if (!this._audio)
  352. return;
  353. this._audio.stop();
  354. if (!this.loop) {
  355. this._audio.offEnded(null);
  356. this._miniSound.dispose();
  357. this._audio = null;
  358. this._miniSound = null;
  359. this._onEnd = null;
  360. }
  361. }
  362. pause() {
  363. this.isStopped = true;
  364. this._audio.pause();
  365. }
  366. get loop() {
  367. return this._audio.loop;
  368. }
  369. set loop(value) {
  370. this._audio.loop = value;
  371. }
  372. resume() {
  373. if (!this._audio)
  374. return;
  375. this.isStopped = false;
  376. Laya.SoundManager.addChannel(this);
  377. this._audio.play();
  378. }
  379. set volume(v) {
  380. if (!this._audio)
  381. return;
  382. this._audio.volume = v;
  383. }
  384. get volume() {
  385. if (!this._audio)
  386. return 1;
  387. return this._audio.volume;
  388. }
  389. }
  390. class MiniSound extends Laya.EventDispatcher {
  391. constructor() {
  392. super();
  393. this.loaded = false;
  394. }
  395. static _createSound() {
  396. MiniSound._id++;
  397. return QGMiniAdapter.window.qg.createInnerAudioContext();
  398. }
  399. load(url) {
  400. if (!MiniSound._musicAudio)
  401. MiniSound._musicAudio = MiniSound._createSound();
  402. if (!MiniFileMgr.isLocalNativeFile(url)) {
  403. url = Laya.URL.formatURL(url);
  404. }
  405. else {
  406. if (url.indexOf("http://") != -1 || url.indexOf("https://") != -1) {
  407. if (MiniFileMgr.loadPath != "") {
  408. url = url.split(MiniFileMgr.loadPath)[1];
  409. }
  410. else {
  411. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  412. if (tempStr != "")
  413. url = url.split(tempStr)[1];
  414. }
  415. }
  416. }
  417. this.url = url;
  418. this.readyUrl = url;
  419. if (MiniSound._audioCache[this.readyUrl]) {
  420. this.event(Laya.Event.COMPLETE);
  421. return;
  422. }
  423. if (QGMiniAdapter.autoCacheFile && MiniFileMgr.getFileInfo(url)) {
  424. this.onDownLoadCallBack(url, 0);
  425. }
  426. else {
  427. if (!QGMiniAdapter.autoCacheFile) {
  428. this.onDownLoadCallBack(url, 0);
  429. }
  430. else {
  431. if (MiniFileMgr.isLocalNativeFile(url)) {
  432. tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  433. var tempUrl = url;
  434. if (tempStr != "")
  435. url = url.split(tempStr)[1];
  436. if (!url) {
  437. url = tempUrl;
  438. }
  439. if (QGMiniAdapter.subNativeFiles && QGMiniAdapter.subNativeheads.length == 0) {
  440. for (var key in QGMiniAdapter.subNativeFiles) {
  441. var tempArr = QGMiniAdapter.subNativeFiles[key];
  442. QGMiniAdapter.subNativeheads = QGMiniAdapter.subNativeheads.concat(tempArr);
  443. for (var aa = 0; aa < tempArr.length; aa++) {
  444. QGMiniAdapter.subMaps[tempArr[aa]] = key + "/" + tempArr[aa];
  445. }
  446. }
  447. }
  448. if (QGMiniAdapter.subNativeFiles && url.indexOf("/") != -1) {
  449. var curfileHead = url.split("/")[0] + "/";
  450. if (curfileHead && QGMiniAdapter.subNativeheads.indexOf(curfileHead) != -1) {
  451. var newfileHead = QGMiniAdapter.subMaps[curfileHead];
  452. url = url.replace(curfileHead, newfileHead);
  453. }
  454. }
  455. this.onDownLoadCallBack(url, 0);
  456. }
  457. else {
  458. if (!MiniFileMgr.isLocalNativeFile(url) && (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) || (url.indexOf("http://usr/") != -1)) {
  459. this.onDownLoadCallBack(url, 0);
  460. }
  461. else {
  462. MiniFileMgr.downOtherFiles(encodeURI(url), Laya.Handler.create(this, this.onDownLoadCallBack, [url]), url);
  463. }
  464. }
  465. }
  466. }
  467. }
  468. onDownLoadCallBack(sourceUrl, errorCode, tempFilePath = null) {
  469. if (!errorCode) {
  470. var fileNativeUrl;
  471. if (QGMiniAdapter.autoCacheFile) {
  472. if (!tempFilePath) {
  473. if (MiniFileMgr.isLocalNativeFile(sourceUrl)) {
  474. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  475. var tempUrl = sourceUrl;
  476. if (tempStr != "" && (sourceUrl.indexOf("http://") != -1 || sourceUrl.indexOf("https://") != -1))
  477. fileNativeUrl = sourceUrl.split(tempStr)[1];
  478. if (!fileNativeUrl) {
  479. fileNativeUrl = tempUrl;
  480. }
  481. }
  482. else {
  483. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  484. if (fileObj && fileObj.md5) {
  485. var fileMd5Name = fileObj.md5;
  486. fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  487. }
  488. else {
  489. fileNativeUrl = sourceUrl;
  490. }
  491. }
  492. }
  493. else {
  494. fileNativeUrl = tempFilePath;
  495. }
  496. if (this.url != Laya.SoundManager._bgMusic) {
  497. this._sound = MiniSound._createSound();
  498. this._sound.src = this.url = fileNativeUrl;
  499. }
  500. else {
  501. this._sound = MiniSound._musicAudio;
  502. this._sound.src = this.url = fileNativeUrl;
  503. }
  504. }
  505. else {
  506. if (this.url != Laya.SoundManager._bgMusic) {
  507. this._sound = MiniSound._createSound();
  508. this._sound.src = sourceUrl;
  509. }
  510. else {
  511. this._sound = MiniSound._musicAudio;
  512. this._sound.src = sourceUrl;
  513. }
  514. }
  515. this._sound.onCanplay(MiniSound.bindToThis(this.onCanPlay, this));
  516. this._sound.onError(MiniSound.bindToThis(this.onError, this));
  517. }
  518. else {
  519. this.event(Laya.Event.ERROR);
  520. }
  521. }
  522. onError(error) {
  523. try {
  524. console.log("-----1---------------minisound-----id:" + MiniSound._id);
  525. console.log(error);
  526. }
  527. catch (error) {
  528. console.log("-----2---------------minisound-----id:" + MiniSound._id);
  529. console.log(error);
  530. }
  531. this.event(Laya.Event.ERROR);
  532. this._sound.offError(null);
  533. }
  534. onCanPlay() {
  535. this.loaded = true;
  536. this.event(Laya.Event.COMPLETE);
  537. this._sound.offCanplay(null);
  538. }
  539. static bindToThis(fun, scope) {
  540. var rst = fun;
  541. rst = fun.bind(scope);
  542. return rst;
  543. }
  544. play(startTime = 0, loops = 0) {
  545. var tSound;
  546. if (this.url == Laya.SoundManager._bgMusic) {
  547. if (!MiniSound._musicAudio)
  548. MiniSound._musicAudio = MiniSound._createSound();
  549. tSound = MiniSound._musicAudio;
  550. }
  551. else {
  552. if (MiniSound._audioCache[this.readyUrl]) {
  553. tSound = MiniSound._audioCache[this.readyUrl]._sound;
  554. }
  555. else {
  556. tSound = MiniSound._createSound();
  557. }
  558. }
  559. if (!tSound)
  560. return null;
  561. if (QGMiniAdapter.autoCacheFile && MiniFileMgr.getFileInfo(this.url)) {
  562. var fileObj = MiniFileMgr.getFileInfo(this.url);
  563. var fileMd5Name = fileObj.md5;
  564. tSound.src = this.url = MiniFileMgr.getFileNativePath(fileMd5Name);
  565. }
  566. else {
  567. tSound.src = encodeURI(this.url);
  568. }
  569. var channel = new MiniSoundChannel(tSound, this);
  570. channel.url = this.url;
  571. channel.loops = loops;
  572. channel.loop = (loops === 0 ? true : false);
  573. channel.startTime = startTime;
  574. channel.play();
  575. Laya.SoundManager.addChannel(channel);
  576. return channel;
  577. }
  578. get duration() {
  579. return this._sound.duration;
  580. }
  581. dispose() {
  582. var ad = MiniSound._audioCache[this.readyUrl];
  583. if (ad) {
  584. ad.src = "";
  585. if (ad._sound) {
  586. ad._sound.destroy();
  587. ad._sound = null;
  588. ad = null;
  589. }
  590. delete MiniSound._audioCache[this.readyUrl];
  591. }
  592. if (this._sound) {
  593. this._sound.destroy();
  594. this._sound = null;
  595. this.readyUrl = this.url = null;
  596. }
  597. }
  598. }
  599. MiniSound._id = 0;
  600. MiniSound._audioCache = {};
  601. class MiniInput {
  602. constructor() {
  603. }
  604. static _createInputElement() {
  605. Laya.Input['_initInput'](Laya.Input['area'] = Laya.Browser.createElement("textarea"));
  606. Laya.Input['_initInput'](Laya.Input['input'] = Laya.Browser.createElement("input"));
  607. Laya.Input['inputContainer'] = Laya.Browser.createElement("div");
  608. Laya.Input['inputContainer'].style.position = "absolute";
  609. Laya.Input['inputContainer'].style.zIndex = 1E5;
  610. Laya.Browser.container.appendChild(Laya.Input['inputContainer']);
  611. Laya.Laya.stage.on("resize", null, MiniInput._onStageResize);
  612. QGMiniAdapter.window.qg.onWindowResize && QGMiniAdapter.window.qg.onWindowResize(function (res) {
  613. });
  614. Laya.SoundManager._soundClass = MiniSound;
  615. Laya.SoundManager._musicClass = MiniSound;
  616. var model = QGMiniAdapter.systemInfo.model;
  617. var system = QGMiniAdapter.systemInfo.system;
  618. if (model && model.indexOf("iPhone") != -1) {
  619. Laya.Browser.onIPhone = true;
  620. Laya.Browser.onIOS = true;
  621. Laya.Browser.onIPad = true;
  622. Laya.Browser.onAndroid = false;
  623. }
  624. if (system && (system.indexOf("Android") != -1 || system.indexOf("Adr") != -1)) {
  625. Laya.Browser.onAndroid = true;
  626. Laya.Browser.onIPhone = false;
  627. Laya.Browser.onIOS = false;
  628. Laya.Browser.onIPad = false;
  629. }
  630. }
  631. static _onStageResize() {
  632. }
  633. static wxinputFocus(e) {
  634. var _inputTarget = Laya.Input['inputElement'].target;
  635. if (_inputTarget && !_inputTarget.editable) {
  636. return;
  637. }
  638. QGMiniAdapter.window.qg.showKeyboard({ defaultValue: _inputTarget.text, maxLength: _inputTarget.maxChars, multiple: _inputTarget.multiline, confirmHold: true, confirmType: _inputTarget["confirmType"] || 'done', success: function (res) {
  639. }, fail: function (res) {
  640. } });
  641. QGMiniAdapter.window.qg.onKeyboardComplete(function (res) {
  642. QGMiniAdapter.window.qg.offKeyboardComplete();
  643. var str = res ? res.value : "";
  644. if (_inputTarget._restrictPattern) {
  645. str = str.replace(/\u2006|\x27/g, "");
  646. if (_inputTarget._restrictPattern.test(str)) {
  647. str = str.replace(_inputTarget._restrictPattern, "");
  648. }
  649. }
  650. _inputTarget.text = str;
  651. _inputTarget.event(Laya.Event.INPUT);
  652. MiniInput.inputEnter(true);
  653. });
  654. QGMiniAdapter.window.qg.onKeyboardConfirm(function (res) {
  655. var str = res ? res.value : "";
  656. if (_inputTarget._restrictPattern) {
  657. str = str.replace(/\u2006|\x27/g, "");
  658. if (_inputTarget._restrictPattern.test(str)) {
  659. str = str.replace(_inputTarget._restrictPattern, "");
  660. }
  661. }
  662. _inputTarget.text = str;
  663. _inputTarget.event(Laya.Event.INPUT);
  664. MiniInput.inputEnter(true);
  665. _inputTarget.event("confirm");
  666. });
  667. QGMiniAdapter.window.qg.onKeyboardInput(function (res) {
  668. var str = res ? res.value : "";
  669. if (!_inputTarget.multiline) {
  670. if (str.indexOf("\n") != -1) {
  671. MiniInput.inputEnter(false);
  672. return;
  673. }
  674. }
  675. if (_inputTarget._restrictPattern) {
  676. str = str.replace(/\u2006|\x27/g, "");
  677. if (_inputTarget._restrictPattern.test(str)) {
  678. str = str.replace(_inputTarget._restrictPattern, "");
  679. }
  680. }
  681. _inputTarget.text = str;
  682. _inputTarget.event(Laya.Event.INPUT);
  683. MiniInput.inputEnter(false);
  684. });
  685. }
  686. static inputEnter(isBool) {
  687. if (isBool) {
  688. MiniInput.hideKeyboard();
  689. }
  690. if (!Laya.Input['inputElement'].target)
  691. return;
  692. Laya.Input['inputElement'].target.focus = false;
  693. }
  694. static wxinputblur() {
  695. }
  696. static hideKeyboard() {
  697. QGMiniAdapter.window.qg.offKeyboardConfirm();
  698. QGMiniAdapter.window.qg.offKeyboardInput();
  699. QGMiniAdapter.window.qg.hideKeyboard({ success: function (res) {
  700. console.log('隐藏键盘');
  701. }, fail: function (res) {
  702. console.log("隐藏键盘出错:" + (res ? res.errMsg : ""));
  703. } });
  704. }
  705. }
  706. class MiniLoader extends Laya.EventDispatcher {
  707. constructor() {
  708. super();
  709. }
  710. _loadResourceFilter(type, url) {
  711. var thisLoader = this;
  712. this.sourceUrl = Laya.URL.formatURL(url);
  713. if (url.indexOf("http://usr/") == -1 && (url.indexOf("http://") != -1 || url.indexOf("https://") != -1)) {
  714. if (MiniFileMgr.loadPath != "") {
  715. url = url.split(MiniFileMgr.loadPath)[1];
  716. }
  717. else {
  718. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  719. var tempUrl = url;
  720. if (tempStr != "")
  721. url = url.split(tempStr)[1];
  722. if (!url) {
  723. url = tempUrl;
  724. }
  725. }
  726. }
  727. if (QGMiniAdapter.subNativeFiles && QGMiniAdapter.subNativeheads.length == 0) {
  728. for (var key in QGMiniAdapter.subNativeFiles) {
  729. var tempArr = QGMiniAdapter.subNativeFiles[key];
  730. QGMiniAdapter.subNativeheads = QGMiniAdapter.subNativeheads.concat(tempArr);
  731. for (var aa = 0; aa < tempArr.length; aa++) {
  732. QGMiniAdapter.subMaps[tempArr[aa]] = key + "/" + tempArr[aa];
  733. }
  734. }
  735. }
  736. if (QGMiniAdapter.subNativeFiles && url.indexOf("/") != -1) {
  737. var curfileHead = url.split("/")[0] + "/";
  738. if (curfileHead && QGMiniAdapter.subNativeheads.indexOf(curfileHead) != -1) {
  739. var newfileHead = QGMiniAdapter.subMaps[curfileHead];
  740. url = url.replace(curfileHead, newfileHead);
  741. }
  742. }
  743. switch (type) {
  744. case Laya.Loader.IMAGE:
  745. case "htmlimage":
  746. case "nativeimage":
  747. MiniLoader._transformImgUrl(url, type, thisLoader);
  748. break;
  749. case Laya.Loader.SOUND:
  750. thisLoader._loadSound(url);
  751. break;
  752. default:
  753. thisLoader._loadResource(type, url);
  754. }
  755. }
  756. _loadSound(url) {
  757. var thisLoader = this;
  758. var fileNativeUrl;
  759. if (MiniFileMgr.isLocalNativeFile(url)) {
  760. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  761. var tempUrl = url;
  762. if (tempStr != "" && (url.indexOf("http://") != -1 || url.indexOf("https://") != -1))
  763. fileNativeUrl = url.split(tempStr)[1];
  764. if (!fileNativeUrl) {
  765. fileNativeUrl = tempUrl;
  766. }
  767. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  768. }
  769. else {
  770. var tempurl = Laya.URL.formatURL(url);
  771. if (!MiniFileMgr.isLocalNativeFile(url) && (tempurl.indexOf("http://") == -1 && tempurl.indexOf("https://") == -1) || (tempurl.indexOf("http://usr/") != -1)) {
  772. MiniLoader.onDownLoadCallBack(url, thisLoader, 0);
  773. }
  774. else {
  775. MiniFileMgr.downOtherFiles(tempurl, Laya.Handler.create(MiniLoader, MiniLoader.onDownLoadCallBack, [tempurl, thisLoader]), tempurl);
  776. }
  777. }
  778. }
  779. static onDownLoadCallBack(sourceUrl, thisLoader, errorCode, tempFilePath = null) {
  780. if (!errorCode) {
  781. var fileNativeUrl;
  782. if (QGMiniAdapter.autoCacheFile) {
  783. if (!tempFilePath) {
  784. if (MiniFileMgr.isLocalNativeFile(sourceUrl)) {
  785. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL._basePath;
  786. var tempUrl = sourceUrl;
  787. if (tempStr != "" && (sourceUrl.indexOf("http://") != -1 || sourceUrl.indexOf("https://") != -1))
  788. fileNativeUrl = sourceUrl.split(tempStr)[1];
  789. if (!fileNativeUrl) {
  790. fileNativeUrl = tempUrl;
  791. }
  792. }
  793. else {
  794. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  795. if (fileObj && fileObj.md5) {
  796. var fileMd5Name = fileObj.md5;
  797. fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  798. }
  799. else {
  800. fileNativeUrl = sourceUrl;
  801. }
  802. }
  803. }
  804. else {
  805. fileNativeUrl = tempFilePath;
  806. }
  807. }
  808. sourceUrl = fileNativeUrl;
  809. var sound = (new Laya.SoundManager._soundClass());
  810. sound.load(sourceUrl);
  811. thisLoader.onLoaded(sound);
  812. }
  813. else {
  814. thisLoader.event(Laya.Event.ERROR, "Load sound failed");
  815. }
  816. }
  817. static bindToThis(fun, scope) {
  818. var rst = fun;
  819. rst = fun.bind(scope);
  820. return rst;
  821. }
  822. complete(data) {
  823. if (data instanceof Laya.Resource) {
  824. data._setCreateURL(this.sourceUrl);
  825. }
  826. else if ((data instanceof Laya.Texture) && (data.bitmap instanceof Laya.Resource)) {
  827. data.bitmap._setCreateURL(this.sourceUrl);
  828. }
  829. this.originComplete(data);
  830. }
  831. _loadHttpRequestWhat(url, contentType) {
  832. var thisLoader = this;
  833. var encoding = QGMiniAdapter.getUrlEncode(url, contentType);
  834. if (Laya.Loader.preLoadedMap[url])
  835. thisLoader.onLoaded(Laya.Loader.preLoadedMap[url]);
  836. else {
  837. var tempurl = Laya.URL.formatURL(url);
  838. if (!MiniFileMgr.isLocalNativeFile(url) && !MiniFileMgr.getFileInfo(tempurl) && url.indexOf("http://usr/") == -1 && (tempurl.indexOf("http://") != -1 || tempurl.indexOf("https://") != -1) && !QGMiniAdapter.AutoCacheDownFile) {
  839. thisLoader._loadHttpRequest(tempurl, contentType, thisLoader, thisLoader.onLoaded, thisLoader, thisLoader.onProgress, thisLoader, thisLoader.onError);
  840. }
  841. else {
  842. var fileObj = MiniFileMgr.getFileInfo(Laya.URL.formatURL(url));
  843. if (fileObj) {
  844. fileObj.encoding = fileObj.encoding == null ? "utf8" : fileObj.encoding;
  845. MiniFileMgr.readFile(MiniFileMgr.getFileNativePath(fileObj.md5), encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  846. }
  847. else if (thisLoader.type == "image" || thisLoader.type == "htmlimage") {
  848. thisLoader._transformUrl(url, contentType);
  849. }
  850. else {
  851. if (contentType != Laya.Loader.IMAGE && ((tempurl.indexOf("http://") == -1 && tempurl.indexOf("https://") == -1) || MiniFileMgr.isLocalNativeFile(url))) {
  852. MiniFileMgr.readFile(url, encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), url);
  853. }
  854. else {
  855. MiniFileMgr.downFiles(encodeURI(tempurl), encoding, new Laya.Handler(MiniLoader, MiniLoader.onReadNativeCallBack, [url, contentType, thisLoader]), tempurl, true);
  856. }
  857. }
  858. }
  859. }
  860. }
  861. static onReadNativeCallBack(url, type = null, thisLoader = null, errorCode = 0, data = null) {
  862. if (!errorCode) {
  863. var tempData;
  864. if (type == Laya.Loader.JSON || type == Laya.Loader.ATLAS || type == Laya.Loader.PREFAB || type == Laya.Loader.PLF) {
  865. tempData = QGMiniAdapter.getJson(data.data);
  866. }
  867. else if (type == Laya.Loader.XML) {
  868. tempData = Laya.Utils.parseXMLFromString(data.data);
  869. }
  870. else {
  871. tempData = data.data;
  872. }
  873. if (!QGMiniAdapter.isZiYu && QGMiniAdapter.isPosMsgYu && type != Laya.Loader.BUFFER) {
  874. QGMiniAdapter.window.wx.postMessage({ url: url, data: tempData, isLoad: "filedata" });
  875. }
  876. thisLoader.onLoaded(tempData);
  877. }
  878. else if (errorCode == 1) {
  879. thisLoader._loadHttpRequest(url, type, thisLoader, thisLoader.onLoaded, thisLoader, thisLoader.onProgress, thisLoader, thisLoader.onError);
  880. }
  881. }
  882. static _transformImgUrl(url, type, thisLoader) {
  883. if (QGMiniAdapter.isZiYu || MiniFileMgr.isLocalNativeFile(url)) {
  884. thisLoader._loadImage(url, false);
  885. return;
  886. }
  887. if (!QGMiniAdapter.autoCacheFile) {
  888. thisLoader._loadImage(encodeURI(url));
  889. return;
  890. }
  891. if (!MiniFileMgr.isLocalNativeFile(url) && !MiniFileMgr.getFileInfo(Laya.URL.formatURL(url))) {
  892. var tempUrl = Laya.URL.formatURL(url);
  893. if (url.indexOf('http://usr/') == -1 && (tempUrl.indexOf("http://") != -1 || tempUrl.indexOf("https://") != -1)) {
  894. if (QGMiniAdapter.isZiYu) {
  895. thisLoader._loadImage(url);
  896. }
  897. else {
  898. MiniFileMgr.downOtherFiles(tempUrl, new Laya.Handler(MiniLoader, MiniLoader.onDownImgCallBack, [url, thisLoader]), tempUrl);
  899. }
  900. }
  901. else
  902. thisLoader._loadImage(url);
  903. }
  904. else {
  905. MiniLoader.onCreateImage(url, thisLoader);
  906. }
  907. }
  908. static onDownImgCallBack(sourceUrl, thisLoader, errorCode, tempFilePath = "") {
  909. if (!errorCode)
  910. MiniLoader.onCreateImage(sourceUrl, thisLoader, false, tempFilePath);
  911. else {
  912. thisLoader.onError(null);
  913. }
  914. }
  915. static onCreateImage(sourceUrl, thisLoader, isLocal = false, tempFilePath = "") {
  916. var fileNativeUrl;
  917. if (QGMiniAdapter.autoCacheFile) {
  918. if (!isLocal) {
  919. if (tempFilePath != "") {
  920. fileNativeUrl = tempFilePath;
  921. }
  922. else {
  923. var fileObj = MiniFileMgr.getFileInfo(Laya.URL.formatURL(sourceUrl));
  924. var fileMd5Name = fileObj.md5;
  925. fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  926. }
  927. }
  928. else if (QGMiniAdapter.isZiYu) {
  929. var tempUrl = Laya.URL.formatURL(sourceUrl);
  930. if (MiniFileMgr.ziyuFileTextureData[tempUrl]) {
  931. fileNativeUrl = MiniFileMgr.ziyuFileTextureData[tempUrl];
  932. }
  933. else
  934. fileNativeUrl = sourceUrl;
  935. }
  936. else
  937. fileNativeUrl = sourceUrl;
  938. }
  939. else {
  940. if (!isLocal)
  941. fileNativeUrl = tempFilePath;
  942. else
  943. fileNativeUrl = sourceUrl;
  944. }
  945. thisLoader._loadImage(fileNativeUrl, false);
  946. }
  947. }
  948. class QGMiniAdapter {
  949. static getJson(data) {
  950. return JSON.parse(data);
  951. }
  952. static enable() {
  953. QGMiniAdapter.init(Laya.Laya.isWXPosMsg, Laya.Laya.isWXOpenDataContext);
  954. }
  955. static init(isPosMsg = false, isSon = false) {
  956. if (QGMiniAdapter._inited)
  957. return;
  958. QGMiniAdapter._inited = true;
  959. QGMiniAdapter.window = window;
  960. if (!QGMiniAdapter.window.hasOwnProperty('qg'))
  961. return;
  962. if (QGMiniAdapter.window.navigator.userAgent.indexOf('OPPO') < 0)
  963. return;
  964. QGMiniAdapter.isZiYu = isSon;
  965. QGMiniAdapter.isPosMsgYu = isPosMsg;
  966. QGMiniAdapter.EnvConfig = {};
  967. if (!QGMiniAdapter.isZiYu) {
  968. MiniFileMgr.setNativeFileDir("/layaairGame");
  969. MiniFileMgr.existDir(MiniFileMgr.fileNativeDir, Laya.Handler.create(QGMiniAdapter, QGMiniAdapter.onMkdirCallBack));
  970. }
  971. QGMiniAdapter.window.qg.getSystemInfo({
  972. success: function (data) {
  973. QGMiniAdapter.systemInfo = data;
  974. }
  975. });
  976. QGMiniAdapter.window.focus = function () {
  977. };
  978. Laya.Laya['_getUrlPath'] = function () {
  979. return "";
  980. };
  981. Laya.Laya['getUrlPath'] = function () {
  982. return "";
  983. };
  984. QGMiniAdapter.window.logtime = function (str) {
  985. };
  986. QGMiniAdapter.window.alertTimeLog = function (str) {
  987. };
  988. QGMiniAdapter.window.resetShareInfo = function () {
  989. };
  990. QGMiniAdapter.window.document.body.appendChild = function () {
  991. };
  992. QGMiniAdapter.EnvConfig.pixelRatioInt = 0;
  993. Laya.Browser["_pixelRatio"] = QGMiniAdapter.pixelRatio();
  994. QGMiniAdapter._preCreateElement = Laya.Browser.createElement;
  995. Laya.Browser["createElement"] = QGMiniAdapter.createElement;
  996. Laya.RunDriver.createShaderCondition = QGMiniAdapter.createShaderCondition;
  997. Laya.Utils['parseXMLFromString'] = QGMiniAdapter.parseXMLFromString;
  998. Laya.Input['_createInputElement'] = MiniInput['_createInputElement'];
  999. Laya.Loader.prototype._loadResourceFilter = MiniLoader.prototype._loadResourceFilter;
  1000. Laya.Loader.prototype._loadSound = MiniLoader.prototype._loadSound;
  1001. Laya.Loader.prototype.originComplete = Laya.Loader.prototype.complete;
  1002. Laya.Loader.prototype.complete = MiniLoader.prototype.complete;
  1003. Laya.Loader.prototype._loadHttpRequestWhat = MiniLoader.prototype._loadHttpRequestWhat;
  1004. Laya.Config.useRetinalCanvas = true;
  1005. QGMiniAdapter.window.qg.onMessage && QGMiniAdapter.window.qg.onMessage(QGMiniAdapter._onMessage);
  1006. }
  1007. static _onMessage(data) {
  1008. switch (data.type) {
  1009. case "changeMatrix":
  1010. Laya.Laya.stage.transform.identity();
  1011. Laya.Laya.stage._width = data.w;
  1012. Laya.Laya.stage._height = data.h;
  1013. Laya.Laya.stage._canvasTransform = new Laya.Matrix(data.a, data.b, data.c, data.d, data.tx, data.ty);
  1014. break;
  1015. case "display":
  1016. Laya.Laya.stage.frameRate = data.rate || Laya.Stage.FRAME_FAST;
  1017. break;
  1018. case "undisplay":
  1019. Laya.Laya.stage.frameRate = Laya.Stage.FRAME_SLEEP;
  1020. break;
  1021. }
  1022. if (data['isLoad'] == "opendatacontext") {
  1023. if (data.url) {
  1024. MiniFileMgr.ziyuFileData[data.url] = data.atlasdata;
  1025. MiniFileMgr.ziyuFileTextureData[data.imgReadyUrl] = data.imgNativeUrl;
  1026. }
  1027. }
  1028. else if (data['isLoad'] == "openJsondatacontext") {
  1029. if (data.url) {
  1030. MiniFileMgr.ziyuFileData[data.url] = data.atlasdata;
  1031. }
  1032. }
  1033. else if (data['isLoad'] == "openJsondatacontextPic") {
  1034. MiniFileMgr.ziyuFileTextureData[data.imgReadyUrl] = data.imgNativeUrl;
  1035. }
  1036. }
  1037. static getUrlEncode(url, type) {
  1038. if (type == "arraybuffer")
  1039. return "";
  1040. return "utf8";
  1041. }
  1042. static downLoadFile(fileUrl, fileType = "", callBack = null, encoding = "utf8") {
  1043. var fileObj = MiniFileMgr.getFileInfo(fileUrl);
  1044. if (!fileObj)
  1045. MiniFileMgr.downLoadFile(fileUrl, fileType, callBack, encoding);
  1046. else {
  1047. callBack != null && callBack.runWith([0]);
  1048. }
  1049. }
  1050. static remove(fileUrl, callBack = null) {
  1051. MiniFileMgr.deleteFile("", fileUrl, callBack, "", 0);
  1052. }
  1053. static removeAll() {
  1054. MiniFileMgr.deleteAll();
  1055. }
  1056. static hasNativeFile(fileUrl) {
  1057. return MiniFileMgr.isLocalNativeFile(fileUrl);
  1058. }
  1059. static getFileInfo(fileUrl) {
  1060. return MiniFileMgr.getFileInfo(fileUrl);
  1061. }
  1062. static getFileList() {
  1063. return MiniFileMgr.filesListObj;
  1064. }
  1065. static exitMiniProgram() {
  1066. QGMiniAdapter.window["wx"].exitMiniProgram();
  1067. }
  1068. static onMkdirCallBack(errorCode, data) {
  1069. if (!errorCode) {
  1070. MiniFileMgr.filesListObj = JSON.parse(data.data);
  1071. MiniFileMgr.fakeObj = JSON.parse(data.data) || {};
  1072. }
  1073. else {
  1074. MiniFileMgr.fakeObj = {};
  1075. MiniFileMgr.filesListObj = {};
  1076. }
  1077. MiniFileMgr.fs.readdir({
  1078. dirPath: MiniFileMgr.fileNativeDir,
  1079. success: function (data) {
  1080. var tempMd5ListObj = {};
  1081. var fileObj;
  1082. for (let key in MiniFileMgr.filesListObj) {
  1083. if (key != "fileUsedSize") {
  1084. fileObj = MiniFileMgr.filesListObj[key];
  1085. tempMd5ListObj[fileObj.md5] = true;
  1086. }
  1087. }
  1088. var files = data.files;
  1089. var fileName;
  1090. for (let i = 0, sz = files.length; i < sz; i++) {
  1091. fileName = files[i];
  1092. if (fileName == MiniFileMgr.fileListName)
  1093. continue;
  1094. var deleteFileUrl = MiniFileMgr.getFileNativePath(fileName);
  1095. if (tempMd5ListObj[deleteFileUrl])
  1096. continue;
  1097. if (tempMd5ListObj[fileName])
  1098. continue;
  1099. MiniFileMgr.fs.unlink({
  1100. filePath: deleteFileUrl,
  1101. success: function (data) {
  1102. console.log("删除无引用的磁盘文件:" + fileName);
  1103. }
  1104. });
  1105. }
  1106. }
  1107. });
  1108. }
  1109. static pixelRatio() {
  1110. if (!QGMiniAdapter.EnvConfig.pixelRatioInt) {
  1111. try {
  1112. QGMiniAdapter.systemInfo.pixelRatio = QGMiniAdapter.window.devicePixelRatio;
  1113. QGMiniAdapter.EnvConfig.pixelRatioInt = QGMiniAdapter.systemInfo.pixelRatio;
  1114. return QGMiniAdapter.systemInfo.pixelRatio;
  1115. }
  1116. catch (error) {
  1117. }
  1118. }
  1119. return QGMiniAdapter.EnvConfig.pixelRatioInt;
  1120. }
  1121. static createElement(type) {
  1122. if (type == "canvas") {
  1123. var _source;
  1124. if (QGMiniAdapter.idx == 1) {
  1125. if (QGMiniAdapter.isZiYu) {
  1126. _source = QGMiniAdapter.window.document.createElement("canvas");
  1127. _source.style = {};
  1128. }
  1129. else {
  1130. _source = QGMiniAdapter.window.__canvas;
  1131. }
  1132. }
  1133. else {
  1134. _source = QGMiniAdapter.window.document.createElement("canvas");
  1135. }
  1136. QGMiniAdapter.idx++;
  1137. return _source;
  1138. }
  1139. else if (type == "textarea" || type == "input") {
  1140. return QGMiniAdapter.onCreateInput(type);
  1141. }
  1142. else if (type == "div") {
  1143. var node = QGMiniAdapter._preCreateElement(type);
  1144. node.contains = function (value) {
  1145. return null;
  1146. };
  1147. node.removeChild = function (value) {
  1148. };
  1149. return node;
  1150. }
  1151. else {
  1152. return QGMiniAdapter._preCreateElement(type);
  1153. }
  1154. }
  1155. static onCreateInput(type) {
  1156. var node = QGMiniAdapter._preCreateElement(type);
  1157. node.focus = MiniInput.wxinputFocus;
  1158. node.blur = MiniInput.wxinputblur;
  1159. node.style = {};
  1160. node.value = 0;
  1161. node.parentElement = {};
  1162. node.placeholder = {};
  1163. node.type = {};
  1164. node.setColor = function (value) {
  1165. };
  1166. node.setType = function (value) {
  1167. };
  1168. node.setFontFace = function (value) {
  1169. };
  1170. node.addEventListener = function (value) {
  1171. };
  1172. node.contains = function (value) {
  1173. return null;
  1174. };
  1175. node.removeChild = function (value) {
  1176. };
  1177. return node;
  1178. }
  1179. static createShaderCondition(conditionScript) {
  1180. var func = function () {
  1181. return this[conditionScript.replace("this.", "")];
  1182. };
  1183. return func;
  1184. }
  1185. static sendAtlasToOpenDataContext(url) {
  1186. if (!QGMiniAdapter.isZiYu) {
  1187. var atlasJson = Laya.Loader.getRes(Laya.URL.formatURL(url));
  1188. if (atlasJson) {
  1189. var textureArr = atlasJson.meta.image.split(",");
  1190. if (atlasJson.meta && atlasJson.meta.image) {
  1191. var toloadPics = atlasJson.meta.image.split(",");
  1192. var split = url.indexOf("/") >= 0 ? "/" : "\\";
  1193. var idx = url.lastIndexOf(split);
  1194. var folderPath = idx >= 0 ? url.substr(0, idx + 1) : "";
  1195. for (var i = 0, len = toloadPics.length; i < len; i++) {
  1196. toloadPics[i] = folderPath + toloadPics[i];
  1197. }
  1198. }
  1199. else {
  1200. toloadPics = [url.replace(".json", ".png")];
  1201. }
  1202. for (i = 0; i < toloadPics.length; i++) {
  1203. var tempAtlasPngUrl = toloadPics[i];
  1204. QGMiniAdapter.postInfoToContext(url, tempAtlasPngUrl, atlasJson);
  1205. }
  1206. }
  1207. else {
  1208. throw "传递的url没有获取到对应的图集数据信息,请确保图集已经过!";
  1209. }
  1210. }
  1211. }
  1212. static postInfoToContext(url, atlaspngUrl, atlasJson) {
  1213. var postData = { "frames": atlasJson.frames, "meta": atlasJson.meta };
  1214. var textureUrl = atlaspngUrl;
  1215. var fileObj = MiniFileMgr.getFileInfo(Laya.URL.formatURL(atlaspngUrl));
  1216. if (fileObj) {
  1217. var fileMd5Name = fileObj.md5;
  1218. var fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  1219. }
  1220. else {
  1221. fileNativeUrl = textureUrl;
  1222. }
  1223. if (fileNativeUrl) {
  1224. QGMiniAdapter.window.qg.postMessage && QGMiniAdapter.window.qg.postMessage({ url: url, atlasdata: postData, imgNativeUrl: fileNativeUrl, imgReadyUrl: textureUrl, isLoad: "opendatacontext" });
  1225. }
  1226. else {
  1227. throw "获取图集的磁盘url路径不存在!";
  1228. }
  1229. }
  1230. static sendSinglePicToOpenDataContext(url) {
  1231. var tempTextureUrl = Laya.URL.formatURL(url);
  1232. var fileObj = MiniFileMgr.getFileInfo(tempTextureUrl);
  1233. if (fileObj) {
  1234. var fileMd5Name = fileObj.md5;
  1235. var fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  1236. url = tempTextureUrl;
  1237. }
  1238. else {
  1239. fileNativeUrl = url;
  1240. }
  1241. if (fileNativeUrl) {
  1242. QGMiniAdapter.window.qg.postMessage && QGMiniAdapter.window.qg.postMessage({ url: url, imgNativeUrl: fileNativeUrl, imgReadyUrl: url, isLoad: "openJsondatacontextPic" });
  1243. }
  1244. else {
  1245. throw "获取图集的磁盘url路径不存在!";
  1246. }
  1247. }
  1248. static sendJsonDataToDataContext(url) {
  1249. if (!QGMiniAdapter.isZiYu) {
  1250. var atlasJson = Laya.Loader.getRes(url);
  1251. if (atlasJson) {
  1252. QGMiniAdapter.window.qg.postMessage && QGMiniAdapter.window.qg.postMessage({ url: url, atlasdata: atlasJson, isLoad: "openJsondatacontext" });
  1253. }
  1254. else {
  1255. throw "传递的url没有获取到对应的图集数据信息,请确保图集已经过!";
  1256. }
  1257. }
  1258. }
  1259. }
  1260. QGMiniAdapter._inited = false;
  1261. QGMiniAdapter.systemInfo = {};
  1262. QGMiniAdapter.autoCacheFile = true;
  1263. QGMiniAdapter.minClearSize = (5 * 1024 * 1024);
  1264. QGMiniAdapter.nativefiles = ["layaNativeDir"];
  1265. QGMiniAdapter.subNativeheads = [];
  1266. QGMiniAdapter.subMaps = [];
  1267. QGMiniAdapter.AutoCacheDownFile = false;
  1268. QGMiniAdapter.parseXMLFromString = function (value) {
  1269. var rst;
  1270. value = value.replace(/>\s+</g, '><');
  1271. try {
  1272. rst = (new window.DOMParser()).parseFromString(value, 'text/xml');
  1273. }
  1274. catch (error) {
  1275. throw "需要引入xml解析库文件";
  1276. }
  1277. return rst;
  1278. };
  1279. QGMiniAdapter.idx = 1;
  1280. class MiniAccelerator extends Laya.EventDispatcher {
  1281. constructor() {
  1282. super();
  1283. }
  1284. static __init__() {
  1285. try {
  1286. var Acc;
  1287. Acc = Laya.Accelerator;
  1288. if (!Acc)
  1289. return;
  1290. Acc["prototype"]["on"] = MiniAccelerator["prototype"]["on"];
  1291. Acc["prototype"]["off"] = MiniAccelerator["prototype"]["off"];
  1292. }
  1293. catch (e) {
  1294. }
  1295. }
  1296. static startListen(callBack) {
  1297. MiniAccelerator._callBack = callBack;
  1298. if (MiniAccelerator._isListening)
  1299. return;
  1300. MiniAccelerator._isListening = true;
  1301. try {
  1302. QGMiniAdapter.window.qg.onAccelerometerChange(MiniAccelerator.onAccelerometerChange);
  1303. }
  1304. catch (e) { }
  1305. }
  1306. static stopListen() {
  1307. MiniAccelerator._isListening = false;
  1308. try {
  1309. QGMiniAdapter.window.qg.stopAccelerometer({});
  1310. }
  1311. catch (e) { }
  1312. }
  1313. static onAccelerometerChange(res) {
  1314. var e;
  1315. e = {};
  1316. e.acceleration = res;
  1317. e.accelerationIncludingGravity = res;
  1318. e.rotationRate = {};
  1319. if (MiniAccelerator._callBack != null) {
  1320. MiniAccelerator._callBack(e);
  1321. }
  1322. }
  1323. on(type, caller, listener, args = null) {
  1324. super.on(type, caller, listener, args);
  1325. MiniAccelerator.startListen(this["onDeviceOrientationChange"]);
  1326. return this;
  1327. }
  1328. off(type, caller, listener, onceOnly = false) {
  1329. if (!this.hasListener(type))
  1330. MiniAccelerator.stopListen();
  1331. return super.off(type, caller, listener, onceOnly);
  1332. }
  1333. }
  1334. MiniAccelerator._isListening = false;
  1335. class MiniImage {
  1336. _loadImage(url) {
  1337. var thisLoader = this;
  1338. if (QGMiniAdapter.isZiYu) {
  1339. MiniImage.onCreateImage(url, thisLoader, true);
  1340. return;
  1341. }
  1342. var isTransformUrl;
  1343. if (!MiniFileMgr.isLocalNativeFile(url)) {
  1344. isTransformUrl = true;
  1345. url = Laya.URL.formatURL(url);
  1346. }
  1347. else {
  1348. if (url.indexOf("http://usr/") == -1 && (url.indexOf("http://") != -1 || url.indexOf("https://") != -1)) {
  1349. if (MiniFileMgr.loadPath != "") {
  1350. url = url.split(MiniFileMgr.loadPath)[1];
  1351. }
  1352. else {
  1353. var tempStr = Laya.URL.rootPath != "" ? Laya.URL.rootPath : Laya.URL.basePath;
  1354. var tempUrl = url;
  1355. if (tempStr != "")
  1356. url = url.split(tempStr)[1];
  1357. if (!url) {
  1358. url = tempUrl;
  1359. }
  1360. }
  1361. }
  1362. if (QGMiniAdapter.subNativeFiles && QGMiniAdapter.subNativeheads.length == 0) {
  1363. for (var key in QGMiniAdapter.subNativeFiles) {
  1364. var tempArr = QGMiniAdapter.subNativeFiles[key];
  1365. QGMiniAdapter.subNativeheads = QGMiniAdapter.subNativeheads.concat(tempArr);
  1366. for (var aa = 0; aa < tempArr.length; aa++) {
  1367. QGMiniAdapter.subMaps[tempArr[aa]] = key + "/" + tempArr[aa];
  1368. }
  1369. }
  1370. }
  1371. if (QGMiniAdapter.subNativeFiles && url.indexOf("/") != -1) {
  1372. var curfileHead = url.split("/")[0] + "/";
  1373. if (curfileHead && QGMiniAdapter.subNativeheads.indexOf(curfileHead) != -1) {
  1374. var newfileHead = QGMiniAdapter.subMaps[curfileHead];
  1375. url = url.replace(curfileHead, newfileHead);
  1376. }
  1377. }
  1378. }
  1379. if (!MiniFileMgr.getFileInfo(url)) {
  1380. if (url.indexOf('http://usr/') == -1 && (url.indexOf("http://") != -1 || url.indexOf("https://") != -1)) {
  1381. if (QGMiniAdapter.isZiYu) {
  1382. MiniImage.onCreateImage(url, thisLoader, true);
  1383. }
  1384. else {
  1385. MiniFileMgr.downOtherFiles(encodeURI(url), new Laya.Handler(MiniImage, MiniImage.onDownImgCallBack, [url, thisLoader]), url);
  1386. }
  1387. }
  1388. else
  1389. MiniImage.onCreateImage(url, thisLoader, true);
  1390. }
  1391. else {
  1392. MiniImage.onCreateImage(url, thisLoader, !isTransformUrl);
  1393. }
  1394. }
  1395. static onDownImgCallBack(sourceUrl, thisLoader, errorCode, tempFilePath = "") {
  1396. if (!errorCode)
  1397. MiniImage.onCreateImage(sourceUrl, thisLoader, false, tempFilePath);
  1398. else {
  1399. thisLoader.onError(null);
  1400. }
  1401. }
  1402. static onCreateImage(sourceUrl, thisLoader, isLocal = false, tempFilePath = "") {
  1403. var fileNativeUrl;
  1404. if (QGMiniAdapter.autoCacheFile) {
  1405. if (!isLocal) {
  1406. if (tempFilePath != "") {
  1407. fileNativeUrl = tempFilePath;
  1408. }
  1409. else {
  1410. var fileObj = MiniFileMgr.getFileInfo(sourceUrl);
  1411. var fileMd5Name = fileObj.md5;
  1412. fileNativeUrl = MiniFileMgr.getFileNativePath(fileMd5Name);
  1413. }
  1414. }
  1415. else if (QGMiniAdapter.isZiYu) {
  1416. var tempUrl = Laya.URL.formatURL(sourceUrl);
  1417. if (MiniFileMgr.ziyuFileTextureData[tempUrl]) {
  1418. fileNativeUrl = MiniFileMgr.ziyuFileTextureData[tempUrl];
  1419. }
  1420. else
  1421. fileNativeUrl = sourceUrl;
  1422. }
  1423. else
  1424. fileNativeUrl = sourceUrl;
  1425. }
  1426. else {
  1427. if (!isLocal)
  1428. fileNativeUrl = tempFilePath;
  1429. else
  1430. fileNativeUrl = sourceUrl;
  1431. }
  1432. if (thisLoader._imgCache == null)
  1433. thisLoader._imgCache = {};
  1434. var image;
  1435. function clear() {
  1436. var img = thisLoader._imgCache[sourceUrl];
  1437. if (img) {
  1438. img.onload = null;
  1439. img.onerror = null;
  1440. delete thisLoader._imgCache[sourceUrl];
  1441. }
  1442. }
  1443. var onerror = function () {
  1444. clear();
  1445. delete MiniFileMgr.fakeObj[sourceUrl];
  1446. delete MiniFileMgr.filesListObj[sourceUrl];
  1447. thisLoader.event(Laya.Event.ERROR, "Load image failed");
  1448. };
  1449. if (thisLoader._type == "nativeimage") {
  1450. var onload = function () {
  1451. clear();
  1452. thisLoader.onLoaded(image);
  1453. };
  1454. image = new Laya.Browser.window.Image();
  1455. image.crossOrigin = "";
  1456. image.onload = onload;
  1457. image.onerror = onerror;
  1458. image.src = fileNativeUrl;
  1459. thisLoader._imgCache[sourceUrl] = image;
  1460. }
  1461. else {
  1462. var imageSource = new Laya.Browser.window.Image();
  1463. onload = function () {
  1464. image = Laya.HTMLImage.create(imageSource.width, imageSource.height);
  1465. image.loadImageSource(imageSource, true);
  1466. image._setCreateURL(sourceUrl);
  1467. clear();
  1468. thisLoader.onLoaded(image);
  1469. };
  1470. imageSource.crossOrigin = "";
  1471. imageSource.onload = onload;
  1472. imageSource.onerror = onerror;
  1473. imageSource.src = fileNativeUrl;
  1474. thisLoader._imgCache[sourceUrl] = imageSource;
  1475. }
  1476. }
  1477. }
  1478. class MiniLocation {
  1479. constructor() {
  1480. }
  1481. static __init__() {
  1482. QGMiniAdapter.window.navigator.geolocation.getCurrentPosition = MiniLocation.getCurrentPosition;
  1483. QGMiniAdapter.window.navigator.geolocation.watchPosition = MiniLocation.watchPosition;
  1484. QGMiniAdapter.window.navigator.geolocation.clearWatch = MiniLocation.clearWatch;
  1485. }
  1486. static getCurrentPosition(success = null, error = null, options = null) {
  1487. var paramO;
  1488. paramO = {};
  1489. paramO.success = getSuccess;
  1490. paramO.fail = error;
  1491. QGMiniAdapter.window.qg.getLocation(paramO);
  1492. function getSuccess(res) {
  1493. if (success != null) {
  1494. success(res);
  1495. }
  1496. }
  1497. }
  1498. static watchPosition(success = null, error = null, options = null) {
  1499. MiniLocation._curID++;
  1500. var curWatchO;
  1501. curWatchO = {};
  1502. curWatchO.success = success;
  1503. curWatchO.error = error;
  1504. MiniLocation._watchDic[MiniLocation._curID] = curWatchO;
  1505. Laya.Laya.systemTimer.loop(1000, null, MiniLocation._myLoop);
  1506. return MiniLocation._curID;
  1507. }
  1508. static clearWatch(id) {
  1509. delete MiniLocation._watchDic[id];
  1510. if (!MiniLocation._hasWatch()) {
  1511. Laya.Laya.systemTimer.clear(null, MiniLocation._myLoop);
  1512. }
  1513. }
  1514. static _hasWatch() {
  1515. var key;
  1516. for (key in MiniLocation._watchDic) {
  1517. if (MiniLocation._watchDic[key])
  1518. return true;
  1519. }
  1520. return false;
  1521. }
  1522. static _myLoop() {
  1523. MiniLocation.getCurrentPosition(MiniLocation._mySuccess, MiniLocation._myError);
  1524. }
  1525. static _mySuccess(res) {
  1526. var rst = {};
  1527. rst.coords = res;
  1528. rst.timestamp = Laya.Browser.now();
  1529. var key;
  1530. for (key in MiniLocation._watchDic) {
  1531. if (MiniLocation._watchDic[key].success) {
  1532. MiniLocation._watchDic[key].success(rst);
  1533. }
  1534. }
  1535. }
  1536. static _myError(res) {
  1537. var key;
  1538. for (key in MiniLocation._watchDic) {
  1539. if (MiniLocation._watchDic[key].error) {
  1540. MiniLocation._watchDic[key].error(res);
  1541. }
  1542. }
  1543. }
  1544. }
  1545. MiniLocation._watchDic = {};
  1546. MiniLocation._curID = 0;
  1547. class MiniVideo {
  1548. constructor(width = 320, height = 240) {
  1549. this.videoend = false;
  1550. this.videourl = "";
  1551. this.videoW = width;
  1552. this.videoH = height;
  1553. }
  1554. static __init__() {
  1555. }
  1556. on(eventType, ths, callBack) {
  1557. if (eventType == "loadedmetadata") {
  1558. this.onPlayFunc = callBack.bind(ths);
  1559. }
  1560. else if (eventType == "ended") {
  1561. this.onEndedFunC = callBack.bind(ths);
  1562. }
  1563. this.videoElement.onTimeUpdate = this.onTimeUpdateFunc.bind(this);
  1564. }
  1565. onTimeUpdateFunc(data) {
  1566. this.position = data.position;
  1567. this._duration = data.duration;
  1568. }
  1569. get duration() {
  1570. return this._duration;
  1571. }
  1572. onPlayFunction() {
  1573. if (this.videoElement)
  1574. this.videoElement.readyState = 200;
  1575. this.onPlayFunc != null && this.onPlayFunc();
  1576. }
  1577. onEndedFunction() {
  1578. this.videoend = true;
  1579. this.onEndedFunC != null && this.onEndedFunC();
  1580. }
  1581. off(eventType, ths, callBack) {
  1582. if (eventType == "loadedmetadata") {
  1583. this.onPlayFunc = callBack.bind(ths);
  1584. this.videoElement.offPlay = this.onPlayFunction.bind(this);
  1585. }
  1586. else if (eventType == "ended") {
  1587. this.onEndedFunC = callBack.bind(ths);
  1588. this.videoElement.offEnded = this.onEndedFunction.bind(this);
  1589. }
  1590. }
  1591. load(url) {
  1592. if (!this.videoElement) {
  1593. this.videoElement = QGMiniAdapter.window.qg.createVideo({ width: this.videoW, height: this.videoH, autoplay: true, src: url });
  1594. this.videoElement.onPlay = this.onPlayFunction.bind(this);
  1595. this.videoElement.onEnded = this.onEndedFunction.bind(this);
  1596. }
  1597. else {
  1598. this.videoElement.src = url;
  1599. }
  1600. }
  1601. play() {
  1602. if (!this.videoElement)
  1603. return;
  1604. this.videoend = false;
  1605. this.videoElement.play();
  1606. }
  1607. pause() {
  1608. if (!this.videoElement)
  1609. return;
  1610. this.videoend = true;
  1611. this.videoElement.pause();
  1612. }
  1613. get currentTime() {
  1614. if (!this.videoElement)
  1615. return 0;
  1616. return this.videoElement.initialTime;
  1617. }
  1618. set currentTime(value) {
  1619. if (!this.videoElement)
  1620. return;
  1621. this.videoElement.initialTime = value;
  1622. }
  1623. get videoWidth() {
  1624. if (!this.videoElement)
  1625. return 0;
  1626. return this.videoElement.width;
  1627. }
  1628. get videoHeight() {
  1629. if (!this.videoElement)
  1630. return 0;
  1631. return this.videoElement.height;
  1632. }
  1633. get ended() {
  1634. return this.videoend;
  1635. }
  1636. get loop() {
  1637. if (!this.videoElement)
  1638. return false;
  1639. return this.videoElement.loop;
  1640. }
  1641. set loop(value) {
  1642. if (!this.videoElement)
  1643. return;
  1644. this.videoElement.loop = value;
  1645. }
  1646. get playbackRate() {
  1647. if (!this.videoElement)
  1648. return 0;
  1649. return this.videoElement.playbackRate;
  1650. }
  1651. set playbackRate(value) {
  1652. if (!this.videoElement)
  1653. return;
  1654. this.videoElement.playbackRate = value;
  1655. }
  1656. get muted() {
  1657. if (!this.videoElement)
  1658. return false;
  1659. return this.videoElement.muted;
  1660. }
  1661. set muted(value) {
  1662. if (!this.videoElement)
  1663. return;
  1664. this.videoElement.muted = value;
  1665. }
  1666. get paused() {
  1667. if (!this.videoElement)
  1668. return false;
  1669. return this.videoElement.paused;
  1670. }
  1671. size(width, height) {
  1672. if (!this.videoElement)
  1673. return;
  1674. this.videoElement.width = width;
  1675. this.videoElement.height = height;
  1676. }
  1677. get x() {
  1678. if (!this.videoElement)
  1679. return 0;
  1680. return this.videoElement.x;
  1681. }
  1682. set x(value) {
  1683. if (!this.videoElement)
  1684. return;
  1685. this.videoElement.x = value;
  1686. }
  1687. get y() {
  1688. if (!this.videoElement)
  1689. return 0;
  1690. return this.videoElement.y;
  1691. }
  1692. set y(value) {
  1693. if (!this.videoElement)
  1694. return;
  1695. this.videoElement.y = value;
  1696. }
  1697. get currentSrc() {
  1698. return this.videoElement.src;
  1699. }
  1700. destroy() {
  1701. if (this.videoElement)
  1702. this.videoElement.destroy();
  1703. this.videoElement = null;
  1704. this.onEndedFunC = null;
  1705. this.onPlayFunc = null;
  1706. this.videoend = false;
  1707. this.videourl = null;
  1708. }
  1709. reload() {
  1710. if (!this.videoElement)
  1711. return;
  1712. this.videoElement.src = this.videourl;
  1713. }
  1714. }
  1715. exports.MiniAccelerator = MiniAccelerator;
  1716. exports.MiniFileMgr = MiniFileMgr;
  1717. exports.MiniImage = MiniImage;
  1718. exports.MiniInput = MiniInput;
  1719. exports.MiniLoader = MiniLoader;
  1720. exports.MiniLocation = MiniLocation;
  1721. exports.MiniSound = MiniSound;
  1722. exports.MiniSoundChannel = MiniSoundChannel;
  1723. exports.MiniVideo = MiniVideo;
  1724. exports.QGMiniAdapter = QGMiniAdapter;
  1725. }