laya.vvmini.js 70 KB

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