laya.ttmini.js 70 KB

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