laya.ttmini.js 69 KB

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