laya.xmmini.js 69 KB

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