laya.xmmini.js 68 KB

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