laya.vvmini.js 68 KB

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