美素佳儿 litter wizard小游戏
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

8667 satır
383KB

  1. var __reflect = (this && this.__reflect) || function (p, c, t) {
  2. p.__class__ = c, t ? t.push(c) : t = [c], p.__types__ = p.__types__ ? t.concat(p.__types__) : t;
  3. };
  4. var __extends = (this && this.__extends) || function (d, b) {
  5. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. //////////////////////////////////////////////////////////////////////////////////////
  10. //
  11. // Copyright (c) 2014-present, Egret Technology.
  12. // All rights reserved.
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions are met:
  15. //
  16. // * Redistributions of source code must retain the above copyright
  17. // notice, this list of conditions and the following disclaimer.
  18. // * Redistributions in binary form must reproduce the above copyright
  19. // notice, this list of conditions and the following disclaimer in the
  20. // documentation and/or other materials provided with the distribution.
  21. // * Neither the name of the Egret nor the
  22. // names of its contributors may be used to endorse or promote products
  23. // derived from this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  31. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  34. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //////////////////////////////////////////////////////////////////////////////////////
  37. var egret;
  38. (function (egret) {
  39. var web;
  40. (function (web) {
  41. /**
  42. * @private
  43. * 抽象shader类,所有shader的基类
  44. */
  45. var EgretShader = (function () {
  46. function EgretShader(gl) {
  47. // 着色器源码
  48. this.defaultVertexSrc = "attribute vec2 aVertexPosition;\n" +
  49. "attribute vec2 aTextureCoord;\n" +
  50. "attribute vec2 aColor;\n" +
  51. "uniform vec2 projectionVector;\n" +
  52. // "uniform vec2 offsetVector;\n" +
  53. "varying vec2 vTextureCoord;\n" +
  54. "varying vec4 vColor;\n" +
  55. "const vec2 center = vec2(-1.0, 1.0);\n" +
  56. "void main(void) {\n" +
  57. " gl_Position = vec4( (aVertexPosition / projectionVector) + center , 0.0, 1.0);\n" +
  58. " vTextureCoord = aTextureCoord;\n" +
  59. " vColor = vec4(aColor.x, aColor.x, aColor.x, aColor.x);\n" +
  60. "}";
  61. this.fragmentSrc = "";
  62. this.gl = null;
  63. this.program = null;
  64. this.uniforms = {
  65. projectionVector: { type: '2f', value: { x: 0, y: 0 }, dirty: true }
  66. };
  67. this.gl = gl;
  68. }
  69. EgretShader.prototype.init = function () {
  70. var gl = this.gl;
  71. var program = egret.WebGLUtils.compileProgram(gl, this.defaultVertexSrc, this.fragmentSrc);
  72. gl.useProgram(program);
  73. this.aVertexPosition = gl.getAttribLocation(program, "aVertexPosition");
  74. this.aTextureCoord = gl.getAttribLocation(program, "aTextureCoord");
  75. this.colorAttribute = gl.getAttribLocation(program, "aColor");
  76. if (this.colorAttribute === -1) {
  77. this.colorAttribute = 2;
  78. }
  79. this.attributes = [this.aVertexPosition, this.aTextureCoord, this.colorAttribute];
  80. for (var key in this.uniforms) {
  81. this.uniforms[key].uniformLocation = gl.getUniformLocation(program, key);
  82. }
  83. this.initUniforms();
  84. this.program = program;
  85. };
  86. EgretShader.prototype.initUniforms = function () {
  87. if (!this.uniforms) {
  88. return;
  89. }
  90. var gl = this.gl;
  91. var uniform;
  92. for (var key in this.uniforms) {
  93. uniform = this.uniforms[key];
  94. uniform.dirty = true;
  95. var type = uniform.type;
  96. if (type === 'mat2' || type === 'mat3' || type === 'mat4') {
  97. uniform.glMatrix = true;
  98. uniform.glValueLength = 1;
  99. if (type === 'mat2') {
  100. uniform.glFunc = gl.uniformMatrix2fv;
  101. }
  102. else if (type === 'mat3') {
  103. uniform.glFunc = gl.uniformMatrix3fv;
  104. }
  105. else if (type === 'mat4') {
  106. uniform.glFunc = gl.uniformMatrix4fv;
  107. }
  108. }
  109. else {
  110. uniform.glFunc = gl['uniform' + type];
  111. if (type === '2f' || type === '2i') {
  112. uniform.glValueLength = 2;
  113. }
  114. else if (type === '3f' || type === '3i') {
  115. uniform.glValueLength = 3;
  116. }
  117. else if (type === '4f' || type === '4i') {
  118. uniform.glValueLength = 4;
  119. }
  120. else {
  121. uniform.glValueLength = 1;
  122. }
  123. }
  124. }
  125. };
  126. EgretShader.prototype.syncUniforms = function () {
  127. if (!this.uniforms) {
  128. return;
  129. }
  130. var uniform;
  131. var gl = this.gl;
  132. for (var key in this.uniforms) {
  133. uniform = this.uniforms[key];
  134. if (uniform.dirty) {
  135. if (uniform.glValueLength === 1) {
  136. if (uniform.glMatrix === true) {
  137. uniform.glFunc.call(gl, uniform.uniformLocation, uniform.transpose, uniform.value);
  138. }
  139. else {
  140. uniform.glFunc.call(gl, uniform.uniformLocation, uniform.value);
  141. }
  142. }
  143. else if (uniform.glValueLength === 2) {
  144. uniform.glFunc.call(gl, uniform.uniformLocation, uniform.value.x, uniform.value.y);
  145. }
  146. else if (uniform.glValueLength === 3) {
  147. uniform.glFunc.call(gl, uniform.uniformLocation, uniform.value.x, uniform.value.y, uniform.value.z);
  148. }
  149. else if (uniform.glValueLength === 4) {
  150. uniform.glFunc.call(gl, uniform.uniformLocation, uniform.value.x, uniform.value.y, uniform.value.z, uniform.value.w);
  151. }
  152. uniform.dirty = false;
  153. }
  154. }
  155. };
  156. /**
  157. * 同步视角坐标
  158. */
  159. EgretShader.prototype.setProjection = function (projectionX, projectionY) {
  160. var uniform = this.uniforms.projectionVector;
  161. if (uniform.value.x != projectionX || uniform.value.y != projectionY) {
  162. uniform.value.x = projectionX;
  163. uniform.value.y = projectionY;
  164. uniform.dirty = true;
  165. }
  166. };
  167. /**
  168. * 设置attribute pointer
  169. */
  170. EgretShader.prototype.setAttribPointer = function (stride) {
  171. var gl = this.gl;
  172. gl.vertexAttribPointer(this.aVertexPosition, 2, gl.FLOAT, false, stride, 0);
  173. gl.vertexAttribPointer(this.aTextureCoord, 2, gl.FLOAT, false, stride, 2 * 4);
  174. gl.vertexAttribPointer(this.colorAttribute, 1, gl.FLOAT, false, stride, 4 * 4);
  175. };
  176. return EgretShader;
  177. }());
  178. web.EgretShader = EgretShader;
  179. __reflect(EgretShader.prototype, "egret.web.EgretShader");
  180. })(web = egret.web || (egret.web = {}));
  181. })(egret || (egret = {}));
  182. //////////////////////////////////////////////////////////////////////////////////////
  183. //
  184. // Copyright (c) 2014-present, Egret Technology.
  185. // All rights reserved.
  186. // Redistribution and use in source and binary forms, with or without
  187. // modification, are permitted provided that the following conditions are met:
  188. //
  189. // * Redistributions of source code must retain the above copyright
  190. // notice, this list of conditions and the following disclaimer.
  191. // * Redistributions in binary form must reproduce the above copyright
  192. // notice, this list of conditions and the following disclaimer in the
  193. // documentation and/or other materials provided with the distribution.
  194. // * Neither the name of the Egret nor the
  195. // names of its contributors may be used to endorse or promote products
  196. // derived from this software without specific prior written permission.
  197. //
  198. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  199. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  200. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  201. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  202. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  203. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  204. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  205. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  206. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  207. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  208. //
  209. //////////////////////////////////////////////////////////////////////////////////////
  210. var egret;
  211. (function (egret) {
  212. var web;
  213. (function (web) {
  214. /**
  215. * @private
  216. */
  217. var TextureShader = (function (_super) {
  218. __extends(TextureShader, _super);
  219. function TextureShader() {
  220. var _this = _super.apply(this, arguments) || this;
  221. _this.fragmentSrc = "precision lowp float;\n" +
  222. "varying vec2 vTextureCoord;\n" +
  223. "varying vec4 vColor;\n" +
  224. "uniform sampler2D uSampler;\n" +
  225. "void main(void) {\n" +
  226. "gl_FragColor = texture2D(uSampler, vTextureCoord) * vColor;\n" +
  227. "}";
  228. return _this;
  229. // webGL 默认上链接材质缓存,可以不手动上传uSampler属性
  230. // private uSampler:WebGLUniformLocation;
  231. // public init():void {
  232. // super.init();
  233. // this.uSampler = gl.getUniformLocation(program, "uSampler");
  234. // }
  235. }
  236. return TextureShader;
  237. }(web.EgretShader));
  238. web.TextureShader = TextureShader;
  239. __reflect(TextureShader.prototype, "egret.web.TextureShader");
  240. })(web = egret.web || (egret.web = {}));
  241. })(egret || (egret = {}));
  242. //////////////////////////////////////////////////////////////////////////////////////
  243. //
  244. // Copyright (c) 2014-present, Egret Technology.
  245. // All rights reserved.
  246. // Redistribution and use in source and binary forms, with or without
  247. // modification, are permitted provided that the following conditions are met:
  248. //
  249. // * Redistributions of source code must retain the above copyright
  250. // notice, this list of conditions and the following disclaimer.
  251. // * Redistributions in binary form must reproduce the above copyright
  252. // notice, this list of conditions and the following disclaimer in the
  253. // documentation and/or other materials provided with the distribution.
  254. // * Neither the name of the Egret nor the
  255. // names of its contributors may be used to endorse or promote products
  256. // derived from this software without specific prior written permission.
  257. //
  258. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  259. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  260. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  261. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  262. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  263. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  264. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  265. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  266. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  267. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  268. //
  269. //////////////////////////////////////////////////////////////////////////////////////
  270. var egret;
  271. (function (egret) {
  272. var localStorage;
  273. (function (localStorage) {
  274. var web;
  275. (function (web) {
  276. /**
  277. * @private
  278. *
  279. * @param key
  280. * @returns
  281. */
  282. function getItem(key) {
  283. return window.localStorage.getItem(key);
  284. }
  285. /**
  286. * @private
  287. *
  288. * @param key
  289. * @param value
  290. * @returns
  291. */
  292. function setItem(key, value) {
  293. try {
  294. window.localStorage.setItem(key, value);
  295. return true;
  296. }
  297. catch (e) {
  298. egret.$warn(1047, key, value);
  299. return false;
  300. }
  301. }
  302. /**
  303. * @private
  304. *
  305. * @param key
  306. */
  307. function removeItem(key) {
  308. window.localStorage.removeItem(key);
  309. }
  310. /**
  311. * @private
  312. *
  313. */
  314. function clear() {
  315. window.localStorage.clear();
  316. }
  317. localStorage.getItem = getItem;
  318. localStorage.setItem = setItem;
  319. localStorage.removeItem = removeItem;
  320. localStorage.clear = clear;
  321. })(web = localStorage.web || (localStorage.web = {}));
  322. })(localStorage = egret.localStorage || (egret.localStorage = {}));
  323. })(egret || (egret = {}));
  324. //////////////////////////////////////////////////////////////////////////////////////
  325. //
  326. // Copyright (c) 2014-present, Egret Technology.
  327. // All rights reserved.
  328. // Redistribution and use in source and binary forms, with or without
  329. // modification, are permitted provided that the following conditions are met:
  330. //
  331. // * Redistributions of source code must retain the above copyright
  332. // notice, this list of conditions and the following disclaimer.
  333. // * Redistributions in binary form must reproduce the above copyright
  334. // notice, this list of conditions and the following disclaimer in the
  335. // documentation and/or other materials provided with the distribution.
  336. // * Neither the name of the Egret nor the
  337. // names of its contributors may be used to endorse or promote products
  338. // derived from this software without specific prior written permission.
  339. //
  340. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  341. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  342. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  343. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  344. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  345. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  346. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  347. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  348. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  349. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  350. //
  351. //////////////////////////////////////////////////////////////////////////////////////
  352. var egret;
  353. (function (egret) {
  354. var web;
  355. (function (web) {
  356. /**
  357. * @private
  358. * @inheritDoc
  359. */
  360. var HtmlSound = (function (_super) {
  361. __extends(HtmlSound, _super);
  362. /**
  363. * @private
  364. * @inheritDoc
  365. */
  366. function HtmlSound() {
  367. var _this = _super.call(this) || this;
  368. /**
  369. * @private
  370. */
  371. _this.loaded = false;
  372. return _this;
  373. }
  374. Object.defineProperty(HtmlSound.prototype, "length", {
  375. get: function () {
  376. if (this.originAudio) {
  377. return this.originAudio.duration;
  378. }
  379. throw new Error("sound not loaded!");
  380. //return 0;
  381. },
  382. enumerable: true,
  383. configurable: true
  384. });
  385. /**
  386. * @inheritDoc
  387. */
  388. HtmlSound.prototype.load = function (url) {
  389. var self = this;
  390. this.url = url;
  391. if (true && !url) {
  392. egret.$error(3002);
  393. }
  394. var audio = new Audio(url);
  395. audio.addEventListener("canplaythrough", onAudioLoaded);
  396. audio.addEventListener("error", onAudioError);
  397. var ua = navigator.userAgent.toLowerCase();
  398. if (ua.indexOf("firefox") >= 0) {
  399. audio.autoplay = !0;
  400. audio.muted = true;
  401. }
  402. audio.load();
  403. this.originAudio = audio;
  404. HtmlSound.$recycle(this.url, audio);
  405. function onAudioLoaded() {
  406. removeListeners();
  407. if (ua.indexOf("firefox") >= 0) {
  408. audio.pause();
  409. audio.muted = false;
  410. }
  411. self.loaded = true;
  412. self.dispatchEventWith(egret.Event.COMPLETE);
  413. }
  414. function onAudioError() {
  415. removeListeners();
  416. self.dispatchEventWith(egret.IOErrorEvent.IO_ERROR);
  417. }
  418. function removeListeners() {
  419. audio.removeEventListener("canplaythrough", onAudioLoaded);
  420. audio.removeEventListener("error", onAudioError);
  421. }
  422. };
  423. /**
  424. * @inheritDoc
  425. */
  426. HtmlSound.prototype.play = function (startTime, loops) {
  427. startTime = +startTime || 0;
  428. loops = +loops || 0;
  429. if (true && this.loaded == false) {
  430. egret.$error(1049);
  431. }
  432. var audio = HtmlSound.$pop(this.url);
  433. if (audio == null) {
  434. audio = this.originAudio.cloneNode();
  435. }
  436. else {
  437. }
  438. audio.autoplay = true;
  439. var channel = new web.HtmlSoundChannel(audio);
  440. channel.$url = this.url;
  441. channel.$loops = loops;
  442. channel.$startTime = startTime;
  443. channel.$play();
  444. egret.sys.$pushSoundChannel(channel);
  445. return channel;
  446. };
  447. /**
  448. * @inheritDoc
  449. */
  450. HtmlSound.prototype.close = function () {
  451. if (this.loaded == false && this.originAudio)
  452. this.originAudio.src = "";
  453. if (this.originAudio)
  454. this.originAudio = null;
  455. HtmlSound.$clear(this.url);
  456. };
  457. HtmlSound.$clear = function (url) {
  458. var array = HtmlSound.audios[url];
  459. if (array) {
  460. array.length = 0;
  461. }
  462. };
  463. HtmlSound.$pop = function (url) {
  464. var array = HtmlSound.audios[url];
  465. if (array && array.length > 0) {
  466. return array.pop();
  467. }
  468. return null;
  469. };
  470. HtmlSound.$recycle = function (url, audio) {
  471. var array = HtmlSound.audios[url];
  472. if (HtmlSound.audios[url] == null) {
  473. array = HtmlSound.audios[url] = [];
  474. }
  475. array.push(audio);
  476. };
  477. return HtmlSound;
  478. }(egret.EventDispatcher));
  479. /**
  480. * @language en_US
  481. * Background music
  482. * @version Egret 2.4
  483. * @platform Web,Native
  484. */
  485. /**
  486. * @language zh_CN
  487. * 背景音乐
  488. * @version Egret 2.4
  489. * @platform Web,Native
  490. */
  491. HtmlSound.MUSIC = "music";
  492. /**
  493. * @language en_US
  494. * EFFECT
  495. * @version Egret 2.4
  496. * @platform Web,Native
  497. */
  498. /**
  499. * @language zh_CN
  500. * 音效
  501. * @version Egret 2.4
  502. * @platform Web,Native
  503. */
  504. HtmlSound.EFFECT = "effect";
  505. /**
  506. * @private
  507. */
  508. HtmlSound.audios = {};
  509. web.HtmlSound = HtmlSound;
  510. __reflect(HtmlSound.prototype, "egret.web.HtmlSound", ["egret.Sound"]);
  511. })(web = egret.web || (egret.web = {}));
  512. })(egret || (egret = {}));
  513. //////////////////////////////////////////////////////////////////////////////////////
  514. //
  515. // Copyright (c) 2014-present, Egret Technology.
  516. // All rights reserved.
  517. // Redistribution and use in source and binary forms, with or without
  518. // modification, are permitted provided that the following conditions are met:
  519. //
  520. // * Redistributions of source code must retain the above copyright
  521. // notice, this list of conditions and the following disclaimer.
  522. // * Redistributions in binary form must reproduce the above copyright
  523. // notice, this list of conditions and the following disclaimer in the
  524. // documentation and/or other materials provided with the distribution.
  525. // * Neither the name of the Egret nor the
  526. // names of its contributors may be used to endorse or promote products
  527. // derived from this software without specific prior written permission.
  528. //
  529. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  530. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  531. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  532. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  533. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  534. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  535. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  536. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  537. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  538. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  539. //
  540. //////////////////////////////////////////////////////////////////////////////////////
  541. var egret;
  542. (function (egret) {
  543. var web;
  544. (function (web) {
  545. /**
  546. * @private
  547. * @inheritDoc
  548. */
  549. var HtmlSoundChannel = (function (_super) {
  550. __extends(HtmlSoundChannel, _super);
  551. /**
  552. * @private
  553. */
  554. function HtmlSoundChannel(audio) {
  555. var _this = _super.call(this) || this;
  556. /**
  557. * @private
  558. */
  559. _this.$startTime = 0;
  560. /**
  561. * @private
  562. */
  563. _this.audio = null;
  564. //声音是否已经播放完成
  565. _this.isStopped = false;
  566. _this.canPlay = function () {
  567. _this.audio.removeEventListener("canplay", _this.canPlay);
  568. try {
  569. _this.audio.currentTime = _this.$startTime;
  570. }
  571. catch (e) {
  572. }
  573. finally {
  574. _this.audio.play();
  575. }
  576. };
  577. /**
  578. * @private
  579. */
  580. _this.onPlayEnd = function () {
  581. if (_this.$loops == 1) {
  582. _this.stop();
  583. _this.dispatchEventWith(egret.Event.SOUND_COMPLETE);
  584. return;
  585. }
  586. if (_this.$loops > 0) {
  587. _this.$loops--;
  588. }
  589. /////////////
  590. //this.audio.load();
  591. _this.$play();
  592. };
  593. /**
  594. * @private
  595. */
  596. _this._volume = 1;
  597. audio.addEventListener("ended", _this.onPlayEnd);
  598. _this.audio = audio;
  599. return _this;
  600. }
  601. HtmlSoundChannel.prototype.$play = function () {
  602. if (this.isStopped) {
  603. egret.$error(1036);
  604. return;
  605. }
  606. try {
  607. //this.audio.pause();
  608. this.audio.volume = this._volume;
  609. this.audio.currentTime = this.$startTime;
  610. }
  611. catch (e) {
  612. this.audio.addEventListener("canplay", this.canPlay);
  613. return;
  614. }
  615. this.audio.play();
  616. };
  617. /**
  618. * @private
  619. * @inheritDoc
  620. */
  621. HtmlSoundChannel.prototype.stop = function () {
  622. if (!this.audio)
  623. return;
  624. if (!this.isStopped) {
  625. egret.sys.$popSoundChannel(this);
  626. }
  627. this.isStopped = true;
  628. var audio = this.audio;
  629. audio.removeEventListener("ended", this.onPlayEnd);
  630. audio.volume = 0;
  631. this._volume = 0;
  632. this.audio = null;
  633. var url = this.$url;
  634. //延迟一定时间再停止,规避chrome报错
  635. window.setTimeout(function () {
  636. audio.pause();
  637. web.HtmlSound.$recycle(url, audio);
  638. }, 200);
  639. };
  640. Object.defineProperty(HtmlSoundChannel.prototype, "volume", {
  641. /**
  642. * @private
  643. * @inheritDoc
  644. */
  645. get: function () {
  646. return this._volume;
  647. },
  648. /**
  649. * @inheritDoc
  650. */
  651. set: function (value) {
  652. if (this.isStopped) {
  653. egret.$error(1036);
  654. return;
  655. }
  656. this._volume = value;
  657. if (!this.audio)
  658. return;
  659. this.audio.volume = value;
  660. },
  661. enumerable: true,
  662. configurable: true
  663. });
  664. Object.defineProperty(HtmlSoundChannel.prototype, "position", {
  665. /**
  666. * @private
  667. * @inheritDoc
  668. */
  669. get: function () {
  670. if (!this.audio)
  671. return 0;
  672. return this.audio.currentTime;
  673. },
  674. enumerable: true,
  675. configurable: true
  676. });
  677. return HtmlSoundChannel;
  678. }(egret.EventDispatcher));
  679. web.HtmlSoundChannel = HtmlSoundChannel;
  680. __reflect(HtmlSoundChannel.prototype, "egret.web.HtmlSoundChannel", ["egret.SoundChannel", "egret.IEventDispatcher"]);
  681. })(web = egret.web || (egret.web = {}));
  682. })(egret || (egret = {}));
  683. //////////////////////////////////////////////////////////////////////////////////////
  684. //
  685. // Copyright (c) 2014-present, Egret Technology.
  686. // All rights reserved.
  687. // Redistribution and use in source and binary forms, with or without
  688. // modification, are permitted provided that the following conditions are met:
  689. //
  690. // * Redistributions of source code must retain the above copyright
  691. // notice, this list of conditions and the following disclaimer.
  692. // * Redistributions in binary form must reproduce the above copyright
  693. // notice, this list of conditions and the following disclaimer in the
  694. // documentation and/or other materials provided with the distribution.
  695. // * Neither the name of the Egret nor the
  696. // names of its contributors may be used to endorse or promote products
  697. // derived from this software without specific prior written permission.
  698. //
  699. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  700. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  701. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  702. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  703. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  704. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  705. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  706. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  707. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  708. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  709. //
  710. //////////////////////////////////////////////////////////////////////////////////////
  711. var egret;
  712. (function (egret) {
  713. var web;
  714. (function (web) {
  715. /**
  716. * @private
  717. * @inheritDoc
  718. */
  719. var QQSound = (function (_super) {
  720. __extends(QQSound, _super);
  721. /**
  722. * @private
  723. * @inheritDoc
  724. */
  725. function QQSound() {
  726. var _this = _super.call(this) || this;
  727. /**
  728. * @private
  729. */
  730. _this.loaded = false;
  731. return _this;
  732. }
  733. /**
  734. * @inheritDoc
  735. */
  736. QQSound.prototype.load = function (url) {
  737. var self = this;
  738. this.url = url;
  739. if (true && !url) {
  740. egret.$error(3002);
  741. }
  742. QZAppExternal.preloadSound(function (data) {
  743. if (data.code == 0) {
  744. self.loaded = true;
  745. self.dispatchEventWith(egret.Event.COMPLETE);
  746. }
  747. else {
  748. self.dispatchEventWith(egret.IOErrorEvent.IO_ERROR);
  749. }
  750. }, {
  751. bid: -1,
  752. url: web.Html5Capatibility._QQRootPath + url,
  753. refresh: 1
  754. });
  755. };
  756. Object.defineProperty(QQSound.prototype, "length", {
  757. get: function () {
  758. throw new Error("qq sound not supported!");
  759. //return 0;
  760. },
  761. enumerable: true,
  762. configurable: true
  763. });
  764. /**
  765. * @inheritDoc
  766. */
  767. QQSound.prototype.play = function (startTime, loops) {
  768. startTime = +startTime || 0;
  769. loops = +loops || 0;
  770. if (true && this.loaded == false) {
  771. egret.$error(1049);
  772. }
  773. var channel = new web.QQSoundChannel();
  774. channel.$url = this.url;
  775. channel.$loops = loops;
  776. channel.$type = this.type;
  777. channel.$startTime = startTime;
  778. channel.$play();
  779. egret.sys.$pushSoundChannel(channel);
  780. return channel;
  781. };
  782. /**
  783. * @inheritDoc
  784. */
  785. QQSound.prototype.close = function () {
  786. };
  787. return QQSound;
  788. }(egret.EventDispatcher));
  789. /**
  790. * @language en_US
  791. * Background music
  792. * @version Egret 2.4
  793. * @platform Web,Native
  794. */
  795. /**
  796. * @language zh_CN
  797. * 背景音乐
  798. * @version Egret 2.4
  799. * @platform Web,Native
  800. */
  801. QQSound.MUSIC = "music";
  802. /**
  803. * @language en_US
  804. * EFFECT
  805. * @version Egret 2.4
  806. * @platform Web,Native
  807. */
  808. /**
  809. * @language zh_CN
  810. * 音效
  811. * @version Egret 2.4
  812. * @platform Web,Native
  813. */
  814. QQSound.EFFECT = "effect";
  815. web.QQSound = QQSound;
  816. __reflect(QQSound.prototype, "egret.web.QQSound", ["egret.Sound"]);
  817. })(web = egret.web || (egret.web = {}));
  818. })(egret || (egret = {}));
  819. //////////////////////////////////////////////////////////////////////////////////////
  820. //
  821. // Copyright (c) 2014-present, Egret Technology.
  822. // All rights reserved.
  823. // Redistribution and use in source and binary forms, with or without
  824. // modification, are permitted provided that the following conditions are met:
  825. //
  826. // * Redistributions of source code must retain the above copyright
  827. // notice, this list of conditions and the following disclaimer.
  828. // * Redistributions in binary form must reproduce the above copyright
  829. // notice, this list of conditions and the following disclaimer in the
  830. // documentation and/or other materials provided with the distribution.
  831. // * Neither the name of the Egret nor the
  832. // names of its contributors may be used to endorse or promote products
  833. // derived from this software without specific prior written permission.
  834. //
  835. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  836. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  837. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  838. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  839. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  840. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  841. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  842. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  843. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  844. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  845. //
  846. //////////////////////////////////////////////////////////////////////////////////////
  847. var egret;
  848. (function (egret) {
  849. var web;
  850. (function (web) {
  851. /**
  852. * @private
  853. * @inheritDoc
  854. */
  855. var QQSoundChannel = (function (_super) {
  856. __extends(QQSoundChannel, _super);
  857. /**
  858. * @private
  859. */
  860. function QQSoundChannel() {
  861. var _this = _super.call(this) || this;
  862. /**
  863. * @private
  864. */
  865. _this.$startTime = 0;
  866. //声音是否已经播放完成
  867. _this.isStopped = false;
  868. /**
  869. * @private
  870. */
  871. _this.onPlayEnd = function () {
  872. if (_this.$loops == 1) {
  873. _this.stop();
  874. _this.dispatchEventWith(egret.Event.SOUND_COMPLETE);
  875. return;
  876. }
  877. if (_this.$loops > 0) {
  878. _this.$loops--;
  879. }
  880. /////////////
  881. _this.$play();
  882. };
  883. /**
  884. * @private
  885. */
  886. _this._startTime = 0;
  887. return _this;
  888. }
  889. QQSoundChannel.prototype.$play = function () {
  890. if (this.isStopped) {
  891. egret.$error(1036);
  892. return;
  893. }
  894. var self = this;
  895. this._startTime = Date.now();
  896. var loop = 0;
  897. if (self.$loops > 0) {
  898. loop = self.$loops - 1;
  899. }
  900. else {
  901. loop = -1;
  902. }
  903. if (this.$type == egret.Sound.EFFECT) {
  904. QZAppExternal.playLocalSound(function (data) {
  905. //self.onPlayEnd();
  906. //alert(JSON.stringify(data));
  907. }, {
  908. bid: -1,
  909. url: self.$url,
  910. loop: loop //默认为0播放一次,背景音乐和音效同时最多各为一个
  911. });
  912. }
  913. else {
  914. QZAppExternal.playLocalBackSound(function (data) {
  915. //self.onPlayEnd();
  916. //alert(JSON.stringify(data));
  917. }, {
  918. bid: -1,
  919. url: self.$url,
  920. loop: loop //默认为0 播放一次,-1为循环播放。背景音乐和音效同时最多各为一个
  921. });
  922. }
  923. };
  924. /**
  925. * @private
  926. * @inheritDoc
  927. */
  928. QQSoundChannel.prototype.stop = function () {
  929. if (this.$type == egret.Sound.EFFECT) {
  930. QZAppExternal.stopSound();
  931. }
  932. else {
  933. QZAppExternal.stopBackSound();
  934. }
  935. if (!this.isStopped) {
  936. egret.sys.$popSoundChannel(this);
  937. }
  938. this.isStopped = true;
  939. };
  940. Object.defineProperty(QQSoundChannel.prototype, "volume", {
  941. /**
  942. * @private
  943. * @inheritDoc
  944. */
  945. get: function () {
  946. return 1;
  947. },
  948. /**
  949. * @inheritDoc
  950. */
  951. set: function (value) {
  952. if (this.isStopped) {
  953. egret.$error(1036);
  954. return;
  955. }
  956. },
  957. enumerable: true,
  958. configurable: true
  959. });
  960. Object.defineProperty(QQSoundChannel.prototype, "position", {
  961. /**
  962. * @private
  963. * @inheritDoc
  964. */
  965. get: function () {
  966. return (Date.now() - this._startTime) / 1000;
  967. },
  968. enumerable: true,
  969. configurable: true
  970. });
  971. return QQSoundChannel;
  972. }(egret.EventDispatcher));
  973. web.QQSoundChannel = QQSoundChannel;
  974. __reflect(QQSoundChannel.prototype, "egret.web.QQSoundChannel", ["egret.SoundChannel", "egret.IEventDispatcher"]);
  975. })(web = egret.web || (egret.web = {}));
  976. })(egret || (egret = {}));
  977. //////////////////////////////////////////////////////////////////////////////////////
  978. //
  979. // Copyright (c) 2014-present, Egret Technology.
  980. // All rights reserved.
  981. // Redistribution and use in source and binary forms, with or without
  982. // modification, are permitted provided that the following conditions are met:
  983. //
  984. // * Redistributions of source code must retain the above copyright
  985. // notice, this list of conditions and the following disclaimer.
  986. // * Redistributions in binary form must reproduce the above copyright
  987. // notice, this list of conditions and the following disclaimer in the
  988. // documentation and/or other materials provided with the distribution.
  989. // * Neither the name of the Egret nor the
  990. // names of its contributors may be used to endorse or promote products
  991. // derived from this software without specific prior written permission.
  992. //
  993. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  994. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  995. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  996. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  997. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  998. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  999. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1000. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1001. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  1002. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1003. //
  1004. //////////////////////////////////////////////////////////////////////////////////////
  1005. var egret;
  1006. (function (egret) {
  1007. var web;
  1008. (function (web) {
  1009. /**
  1010. * @private
  1011. */
  1012. var WebAudioDecode = (function () {
  1013. function WebAudioDecode() {
  1014. }
  1015. /**
  1016. * @private
  1017. *
  1018. */
  1019. WebAudioDecode.decodeAudios = function () {
  1020. if (WebAudioDecode.decodeArr.length <= 0) {
  1021. return;
  1022. }
  1023. if (WebAudioDecode.isDecoding) {
  1024. return;
  1025. }
  1026. WebAudioDecode.isDecoding = true;
  1027. var decodeInfo = WebAudioDecode.decodeArr.shift();
  1028. WebAudioDecode.ctx.decodeAudioData(decodeInfo["buffer"], function (audioBuffer) {
  1029. decodeInfo["self"].audioBuffer = audioBuffer;
  1030. if (decodeInfo["success"]) {
  1031. decodeInfo["success"]();
  1032. }
  1033. WebAudioDecode.isDecoding = false;
  1034. WebAudioDecode.decodeAudios();
  1035. }, function () {
  1036. alert("sound decode error: " + decodeInfo["url"] + "!\nsee http://edn.egret.com/cn/docs/page/156");
  1037. if (decodeInfo["fail"]) {
  1038. decodeInfo["fail"]();
  1039. }
  1040. WebAudioDecode.isDecoding = false;
  1041. WebAudioDecode.decodeAudios();
  1042. });
  1043. };
  1044. return WebAudioDecode;
  1045. }());
  1046. /**
  1047. * @private
  1048. */
  1049. WebAudioDecode.canUseWebAudio = window["AudioContext"] || window["webkitAudioContext"] || window["mozAudioContext"];
  1050. /**
  1051. * @private
  1052. */
  1053. WebAudioDecode.ctx = WebAudioDecode.canUseWebAudio ? new (window["AudioContext"] || window["webkitAudioContext"] || window["mozAudioContext"])() : undefined;
  1054. /**
  1055. * @private
  1056. */
  1057. WebAudioDecode.decodeArr = [];
  1058. /**
  1059. * @private
  1060. */
  1061. WebAudioDecode.isDecoding = false;
  1062. web.WebAudioDecode = WebAudioDecode;
  1063. __reflect(WebAudioDecode.prototype, "egret.web.WebAudioDecode");
  1064. /**
  1065. * @private
  1066. * @inheritDoc
  1067. */
  1068. var WebAudioSound = (function (_super) {
  1069. __extends(WebAudioSound, _super);
  1070. /**
  1071. * @private
  1072. * @inheritDoc
  1073. */
  1074. function WebAudioSound() {
  1075. var _this = _super.call(this) || this;
  1076. /**
  1077. * @private
  1078. */
  1079. _this.loaded = false;
  1080. return _this;
  1081. }
  1082. Object.defineProperty(WebAudioSound.prototype, "length", {
  1083. get: function () {
  1084. if (this.audioBuffer) {
  1085. return this.audioBuffer.duration;
  1086. }
  1087. throw new Error("sound not loaded!");
  1088. //return 0;
  1089. },
  1090. enumerable: true,
  1091. configurable: true
  1092. });
  1093. /**
  1094. * @inheritDoc
  1095. */
  1096. WebAudioSound.prototype.load = function (url) {
  1097. var self = this;
  1098. this.url = url;
  1099. if (true && !url) {
  1100. egret.$error(3002);
  1101. }
  1102. var request = new XMLHttpRequest();
  1103. request.open("GET", url, true);
  1104. request.responseType = "arraybuffer";
  1105. request.onload = function () {
  1106. WebAudioDecode.decodeArr.push({
  1107. "buffer": request.response,
  1108. "success": onAudioLoaded,
  1109. "fail": onAudioError,
  1110. "self": self,
  1111. "url": self.url
  1112. });
  1113. WebAudioDecode.decodeAudios();
  1114. };
  1115. request.send();
  1116. function onAudioLoaded() {
  1117. self.loaded = true;
  1118. self.dispatchEventWith(egret.Event.COMPLETE);
  1119. }
  1120. function onAudioError() {
  1121. self.dispatchEventWith(egret.IOErrorEvent.IO_ERROR);
  1122. }
  1123. };
  1124. /**
  1125. * @inheritDoc
  1126. */
  1127. WebAudioSound.prototype.play = function (startTime, loops) {
  1128. startTime = +startTime || 0;
  1129. loops = +loops || 0;
  1130. if (true && this.loaded == false) {
  1131. egret.$error(1049);
  1132. }
  1133. var channel = new web.WebAudioSoundChannel();
  1134. channel.$url = this.url;
  1135. channel.$loops = loops;
  1136. channel.$audioBuffer = this.audioBuffer;
  1137. channel.$startTime = startTime;
  1138. channel.$play();
  1139. egret.sys.$pushSoundChannel(channel);
  1140. return channel;
  1141. };
  1142. /**
  1143. * @inheritDoc
  1144. */
  1145. WebAudioSound.prototype.close = function () {
  1146. };
  1147. return WebAudioSound;
  1148. }(egret.EventDispatcher));
  1149. /**
  1150. * @language en_US
  1151. * Background music
  1152. * @version Egret 2.4
  1153. * @platform Web,Native
  1154. */
  1155. /**
  1156. * @language zh_CN
  1157. * 背景音乐
  1158. * @version Egret 2.4
  1159. * @platform Web,Native
  1160. */
  1161. WebAudioSound.MUSIC = "music";
  1162. /**
  1163. * @language en_US
  1164. * EFFECT
  1165. * @version Egret 2.4
  1166. * @platform Web,Native
  1167. */
  1168. /**
  1169. * @language zh_CN
  1170. * 音效
  1171. * @version Egret 2.4
  1172. * @platform Web,Native
  1173. */
  1174. WebAudioSound.EFFECT = "effect";
  1175. web.WebAudioSound = WebAudioSound;
  1176. __reflect(WebAudioSound.prototype, "egret.web.WebAudioSound", ["egret.Sound"]);
  1177. })(web = egret.web || (egret.web = {}));
  1178. })(egret || (egret = {}));
  1179. //////////////////////////////////////////////////////////////////////////////////////
  1180. //
  1181. // Copyright (c) 2014-present, Egret Technology.
  1182. // All rights reserved.
  1183. // Redistribution and use in source and binary forms, with or without
  1184. // modification, are permitted provided that the following conditions are met:
  1185. //
  1186. // * Redistributions of source code must retain the above copyright
  1187. // notice, this list of conditions and the following disclaimer.
  1188. // * Redistributions in binary form must reproduce the above copyright
  1189. // notice, this list of conditions and the following disclaimer in the
  1190. // documentation and/or other materials provided with the distribution.
  1191. // * Neither the name of the Egret nor the
  1192. // names of its contributors may be used to endorse or promote products
  1193. // derived from this software without specific prior written permission.
  1194. //
  1195. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  1196. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  1197. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  1198. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  1199. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1200. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  1201. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1202. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1203. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  1204. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1205. //
  1206. //////////////////////////////////////////////////////////////////////////////////////
  1207. var egret;
  1208. (function (egret) {
  1209. var web;
  1210. (function (web) {
  1211. /**
  1212. * @private
  1213. * @inheritDoc
  1214. */
  1215. var WebAudioSoundChannel = (function (_super) {
  1216. __extends(WebAudioSoundChannel, _super);
  1217. /**
  1218. * @private
  1219. */
  1220. function WebAudioSoundChannel() {
  1221. var _this = _super.call(this) || this;
  1222. /**
  1223. * @private
  1224. */
  1225. _this.$startTime = 0;
  1226. /**
  1227. * @private
  1228. */
  1229. _this.bufferSource = null;
  1230. /**
  1231. * @private
  1232. */
  1233. _this.context = web.WebAudioDecode.ctx;
  1234. //声音是否已经播放完成
  1235. _this.isStopped = false;
  1236. /**
  1237. * @private
  1238. */
  1239. _this._currentTime = 0;
  1240. /**
  1241. * @private
  1242. */
  1243. _this._volume = 1;
  1244. /**
  1245. * @private
  1246. */
  1247. _this.onPlayEnd = function () {
  1248. if (_this.$loops == 1) {
  1249. _this.stop();
  1250. _this.dispatchEventWith(egret.Event.SOUND_COMPLETE);
  1251. return;
  1252. }
  1253. if (_this.$loops > 0) {
  1254. _this.$loops--;
  1255. }
  1256. /////////////
  1257. _this.$play();
  1258. };
  1259. /**
  1260. * @private
  1261. */
  1262. _this._startTime = 0;
  1263. if (_this.context["createGain"]) {
  1264. _this.gain = _this.context["createGain"]();
  1265. }
  1266. else {
  1267. _this.gain = _this.context["createGainNode"]();
  1268. }
  1269. return _this;
  1270. }
  1271. WebAudioSoundChannel.prototype.$play = function () {
  1272. if (this.isStopped) {
  1273. egret.$error(1036);
  1274. return;
  1275. }
  1276. if (this.bufferSource) {
  1277. this.bufferSource.onended = null;
  1278. this.bufferSource = null;
  1279. }
  1280. var context = this.context;
  1281. var gain = this.gain;
  1282. var bufferSource = context.createBufferSource();
  1283. this.bufferSource = bufferSource;
  1284. bufferSource.buffer = this.$audioBuffer;
  1285. bufferSource.connect(gain);
  1286. gain.connect(context.destination);
  1287. bufferSource.onended = this.onPlayEnd;
  1288. this._startTime = Date.now();
  1289. this.gain.gain.value = this._volume;
  1290. bufferSource.start(0, this.$startTime);
  1291. this._currentTime = 0;
  1292. };
  1293. WebAudioSoundChannel.prototype.stop = function () {
  1294. if (this.bufferSource) {
  1295. var sourceNode = this.bufferSource;
  1296. if (sourceNode.stop) {
  1297. sourceNode.stop(0);
  1298. }
  1299. else {
  1300. sourceNode.noteOff(0);
  1301. }
  1302. this.bufferSource.disconnect();
  1303. this.bufferSource = null;
  1304. this.$audioBuffer = null;
  1305. }
  1306. if (!this.isStopped) {
  1307. egret.sys.$popSoundChannel(this);
  1308. }
  1309. this.isStopped = true;
  1310. };
  1311. Object.defineProperty(WebAudioSoundChannel.prototype, "volume", {
  1312. /**
  1313. * @private
  1314. * @inheritDoc
  1315. */
  1316. get: function () {
  1317. return this._volume;
  1318. },
  1319. /**
  1320. * @inheritDoc
  1321. */
  1322. set: function (value) {
  1323. if (this.isStopped) {
  1324. egret.$error(1036);
  1325. return;
  1326. }
  1327. this._volume = value;
  1328. this.gain.gain.value = value;
  1329. },
  1330. enumerable: true,
  1331. configurable: true
  1332. });
  1333. Object.defineProperty(WebAudioSoundChannel.prototype, "position", {
  1334. /**
  1335. * @private
  1336. * @inheritDoc
  1337. */
  1338. get: function () {
  1339. if (this.bufferSource) {
  1340. return (Date.now() - this._startTime) / 1000 + this.$startTime;
  1341. }
  1342. return 0;
  1343. },
  1344. enumerable: true,
  1345. configurable: true
  1346. });
  1347. return WebAudioSoundChannel;
  1348. }(egret.EventDispatcher));
  1349. web.WebAudioSoundChannel = WebAudioSoundChannel;
  1350. __reflect(WebAudioSoundChannel.prototype, "egret.web.WebAudioSoundChannel", ["egret.SoundChannel", "egret.IEventDispatcher"]);
  1351. })(web = egret.web || (egret.web = {}));
  1352. })(egret || (egret = {}));
  1353. //////////////////////////////////////////////////////////////////////////////////////
  1354. //
  1355. // Copyright (c) 2014-present, Egret Technology.
  1356. // All rights reserved.
  1357. // Redistribution and use in source and binary forms, with or without
  1358. // modification, are permitted provided that the following conditions are met:
  1359. //
  1360. // * Redistributions of source code must retain the above copyright
  1361. // notice, this list of conditions and the following disclaimer.
  1362. // * Redistributions in binary form must reproduce the above copyright
  1363. // notice, this list of conditions and the following disclaimer in the
  1364. // documentation and/or other materials provided with the distribution.
  1365. // * Neither the name of the Egret nor the
  1366. // names of its contributors may be used to endorse or promote products
  1367. // derived from this software without specific prior written permission.
  1368. //
  1369. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  1370. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  1371. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  1372. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  1373. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1374. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  1375. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1376. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1377. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  1378. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1379. //
  1380. //////////////////////////////////////////////////////////////////////////////////////
  1381. var egret;
  1382. (function (egret) {
  1383. var web;
  1384. (function (web) {
  1385. /**
  1386. * @private
  1387. * @inheritDoc
  1388. */
  1389. var WebVideo = (function (_super) {
  1390. __extends(WebVideo, _super);
  1391. /**
  1392. * @inheritDoc
  1393. */
  1394. function WebVideo(url, cache) {
  1395. if (cache === void 0) { cache = true; }
  1396. var _this = _super.call(this) || this;
  1397. /**
  1398. * @private
  1399. */
  1400. _this.loaded = false;
  1401. /**
  1402. * @private
  1403. */
  1404. _this.closed = false;
  1405. /**
  1406. * @private
  1407. */
  1408. _this.heightSet = NaN;
  1409. /**
  1410. * @private
  1411. */
  1412. _this.widthSet = NaN;
  1413. _this.isPlayed = false;
  1414. _this.screenChanged = function (e) {
  1415. var isfullscreen = !!_this.video['webkitDisplayingFullscreen'];
  1416. if (!isfullscreen) {
  1417. _this.checkFullScreen(false);
  1418. if (!egret.Capabilities.isMobile) {
  1419. _this._fullscreen = isfullscreen;
  1420. }
  1421. }
  1422. };
  1423. _this._fullscreen = true;
  1424. /**
  1425. * @private
  1426. *
  1427. */
  1428. _this.onVideoLoaded = function () {
  1429. _this.video.removeEventListener("canplay", _this.onVideoLoaded);
  1430. var video = _this.video;
  1431. _this.loaded = true;
  1432. //video.pause();
  1433. if (_this.posterData) {
  1434. _this.posterData.width = _this.getPlayWidth();
  1435. _this.posterData.height = _this.getPlayHeight();
  1436. }
  1437. video.width = video.videoWidth;
  1438. video.height = video.videoHeight;
  1439. _this.$invalidateContentBounds();
  1440. window.setTimeout(function () {
  1441. _this.dispatchEventWith(egret.Event.COMPLETE);
  1442. }, 200);
  1443. };
  1444. _this.$renderNode = new egret.sys.BitmapNode();
  1445. _this.src = url;
  1446. _this.once(egret.Event.ADDED_TO_STAGE, _this.loadPoster, _this);
  1447. if (url) {
  1448. _this.load();
  1449. }
  1450. return _this;
  1451. }
  1452. /**
  1453. * @inheritDoc
  1454. */
  1455. WebVideo.prototype.load = function (url, cache) {
  1456. var _this = this;
  1457. if (cache === void 0) { cache = true; }
  1458. url = url || this.src;
  1459. this.src = url;
  1460. if (true && !url) {
  1461. egret.$error(3002);
  1462. }
  1463. if (this.video && this.video.src == url) {
  1464. return;
  1465. }
  1466. var video;
  1467. if (!this.video || egret.Capabilities.isMobile) {
  1468. video = document.createElement("video");
  1469. this.video = video;
  1470. video.controls = null;
  1471. }
  1472. else {
  1473. video = this.video;
  1474. }
  1475. video.src = url;
  1476. video.setAttribute("autoplay", "autoplay");
  1477. video.setAttribute("webkit-playsinline", "true");
  1478. video.addEventListener("canplay", this.onVideoLoaded);
  1479. video.addEventListener("error", function () { return _this.onVideoError(); });
  1480. video.addEventListener("ended", function () { return _this.onVideoEnded(); });
  1481. video.load();
  1482. video.play();
  1483. video.style.position = "absolute";
  1484. video.style.top = "0px";
  1485. video.style.zIndex = "-88888";
  1486. video.style.left = "0px";
  1487. video.height = 1;
  1488. video.width = 1;
  1489. window.setTimeout(function () { return video.pause(); }, 170);
  1490. };
  1491. /**
  1492. * @inheritDoc
  1493. */
  1494. WebVideo.prototype.play = function (startTime, loop) {
  1495. var _this = this;
  1496. if (loop === void 0) { loop = false; }
  1497. if (this.loaded == false) {
  1498. this.load(this.src);
  1499. this.once(egret.Event.COMPLETE, function (e) { return _this.play(startTime, loop); }, this);
  1500. return;
  1501. }
  1502. this.isPlayed = true;
  1503. var video = this.video;
  1504. if (startTime != undefined)
  1505. video.currentTime = +startTime || 0;
  1506. video.loop = !!loop;
  1507. if (egret.Capabilities.isMobile) {
  1508. video.style.zIndex = "-88888"; //移动端,就算设置成最小,只要全屏,都会在最上层,而且在自动退出去后,不担心挡住canvas
  1509. }
  1510. else {
  1511. video.style.zIndex = "9999";
  1512. }
  1513. video.style.position = "absolute";
  1514. video.style.top = "0px";
  1515. video.style.left = "0px";
  1516. video.height = video.videoHeight;
  1517. video.width = video.videoWidth;
  1518. if (egret.Capabilities.os != "Windows PC" && egret.Capabilities.os != "Mac OS") {
  1519. window.setTimeout(function () {
  1520. video.width = 0;
  1521. }, 1000);
  1522. }
  1523. this.checkFullScreen(this._fullscreen);
  1524. };
  1525. WebVideo.prototype.checkFullScreen = function (playFullScreen) {
  1526. var video = this.video;
  1527. if (playFullScreen) {
  1528. if (video.parentElement == null) {
  1529. video.removeAttribute("webkit-playsinline");
  1530. document.body.appendChild(video);
  1531. }
  1532. egret.stopTick(this.markDirty, this);
  1533. this.goFullscreen();
  1534. }
  1535. else {
  1536. if (video.parentElement != null) {
  1537. video.parentElement.removeChild(video);
  1538. }
  1539. video.setAttribute("webkit-playsinline", "true");
  1540. this.setFullScreenMonitor(false);
  1541. egret.startTick(this.markDirty, this);
  1542. if (egret.Capabilities.isMobile) {
  1543. this.video.currentTime = 0;
  1544. this.onVideoEnded();
  1545. return;
  1546. }
  1547. }
  1548. video.play();
  1549. };
  1550. WebVideo.prototype.goFullscreen = function () {
  1551. var video = this.video;
  1552. var fullscreenType;
  1553. fullscreenType = egret.web.getPrefixStyleName('requestFullscreen', video);
  1554. if (!video[fullscreenType]) {
  1555. fullscreenType = egret.web.getPrefixStyleName('requestFullScreen', video);
  1556. if (!video[fullscreenType]) {
  1557. return true;
  1558. }
  1559. }
  1560. video.removeAttribute("webkit-playsinline");
  1561. video[fullscreenType]();
  1562. this.setFullScreenMonitor(true);
  1563. return true;
  1564. };
  1565. WebVideo.prototype.setFullScreenMonitor = function (use) {
  1566. var video = this.video;
  1567. if (use) {
  1568. video.addEventListener("mozfullscreenchange", this.screenChanged);
  1569. video.addEventListener("webkitfullscreenchange", this.screenChanged);
  1570. video.addEventListener("mozfullscreenerror", this.screenError);
  1571. video.addEventListener("webkitfullscreenerror", this.screenError);
  1572. }
  1573. else {
  1574. video.removeEventListener("mozfullscreenchange", this.screenChanged);
  1575. video.removeEventListener("webkitfullscreenchange", this.screenChanged);
  1576. video.removeEventListener("mozfullscreenerror", this.screenError);
  1577. video.removeEventListener("webkitfullscreenerror", this.screenError);
  1578. }
  1579. };
  1580. WebVideo.prototype.screenError = function () {
  1581. egret.$error(3014);
  1582. };
  1583. WebVideo.prototype.exitFullscreen = function () {
  1584. //退出全屏
  1585. if (document['exitFullscreen']) {
  1586. document['exitFullscreen']();
  1587. }
  1588. else if (document['msExitFullscreen']) {
  1589. document['msExitFullscreen']();
  1590. }
  1591. else if (document['mozCancelFullScreen']) {
  1592. document['mozCancelFullScreen']();
  1593. }
  1594. else if (document['oCancelFullScreen']) {
  1595. document['oCancelFullScreen']();
  1596. }
  1597. else if (document['webkitExitFullscreen']) {
  1598. document['webkitExitFullscreen']();
  1599. }
  1600. else {
  1601. }
  1602. };
  1603. /**
  1604. * @private
  1605. *
  1606. */
  1607. WebVideo.prototype.onVideoEnded = function () {
  1608. this.pause();
  1609. this.isPlayed = false;
  1610. this.$invalidateContentBounds();
  1611. this.dispatchEventWith(egret.Event.ENDED);
  1612. };
  1613. /**
  1614. * @private
  1615. *
  1616. */
  1617. WebVideo.prototype.onVideoError = function () {
  1618. this.dispatchEventWith(egret.IOErrorEvent.IO_ERROR);
  1619. };
  1620. /**
  1621. * @inheritDoc
  1622. */
  1623. WebVideo.prototype.close = function () {
  1624. var _this = this;
  1625. this.closed = true;
  1626. this.video.removeEventListener("canplay", this.onVideoLoaded);
  1627. this.video.removeEventListener("error", function () { return _this.onVideoError(); });
  1628. this.video.removeEventListener("ended", function () { return _this.onVideoEnded(); });
  1629. this.pause();
  1630. if (this.loaded == false && this.video)
  1631. this.video.src = "";
  1632. if (this.video && this.video.parentElement) {
  1633. this.video.parentElement.removeChild(this.video);
  1634. this.video = null;
  1635. }
  1636. this.loaded = false;
  1637. };
  1638. /**
  1639. * @inheritDoc
  1640. */
  1641. WebVideo.prototype.pause = function () {
  1642. if (this.video) {
  1643. this.video.pause();
  1644. }
  1645. egret.stopTick(this.markDirty, this);
  1646. this.$invalidate();
  1647. };
  1648. Object.defineProperty(WebVideo.prototype, "volume", {
  1649. /**
  1650. * @inheritDoc
  1651. */
  1652. get: function () {
  1653. if (!this.video)
  1654. return 1;
  1655. return this.video.volume;
  1656. },
  1657. /**
  1658. * @inheritDoc
  1659. */
  1660. set: function (value) {
  1661. if (!this.video)
  1662. return;
  1663. this.video.volume = value;
  1664. },
  1665. enumerable: true,
  1666. configurable: true
  1667. });
  1668. Object.defineProperty(WebVideo.prototype, "position", {
  1669. /**
  1670. * @inheritDoc
  1671. */
  1672. get: function () {
  1673. if (!this.video)
  1674. return 0;
  1675. return this.video.currentTime;
  1676. },
  1677. /**
  1678. * @inheritDoc
  1679. */
  1680. set: function (value) {
  1681. if (!this.video)
  1682. return;
  1683. this.video.currentTime = value;
  1684. },
  1685. enumerable: true,
  1686. configurable: true
  1687. });
  1688. Object.defineProperty(WebVideo.prototype, "fullscreen", {
  1689. /**
  1690. * @inheritDoc
  1691. */
  1692. get: function () {
  1693. return this._fullscreen;
  1694. },
  1695. /**
  1696. * @inheritDoc
  1697. */
  1698. set: function (value) {
  1699. if (egret.Capabilities.isMobile) {
  1700. return;
  1701. }
  1702. this._fullscreen = !!value;
  1703. if (this.video && this.video.paused == false) {
  1704. this.checkFullScreen(this._fullscreen);
  1705. }
  1706. },
  1707. enumerable: true,
  1708. configurable: true
  1709. });
  1710. Object.defineProperty(WebVideo.prototype, "bitmapData", {
  1711. /**
  1712. * @inheritDoc
  1713. */
  1714. get: function () {
  1715. if (!this.video || !this.loaded)
  1716. return null;
  1717. if (!this._bitmapData) {
  1718. this.video.width = this.video.videoWidth;
  1719. this.video.height = this.video.videoHeight;
  1720. this._bitmapData = new egret.BitmapData(this.video);
  1721. this._bitmapData.$deleteSource = false;
  1722. }
  1723. return this._bitmapData;
  1724. },
  1725. enumerable: true,
  1726. configurable: true
  1727. });
  1728. WebVideo.prototype.loadPoster = function () {
  1729. var _this = this;
  1730. var poster = this.poster;
  1731. if (!poster)
  1732. return;
  1733. var imageLoader = new egret.ImageLoader();
  1734. imageLoader.once(egret.Event.COMPLETE, function (e) {
  1735. var posterData = imageLoader.data;
  1736. _this.posterData = imageLoader.data;
  1737. _this.posterData.width = _this.getPlayWidth();
  1738. _this.posterData.height = _this.getPlayHeight();
  1739. _this.$invalidateContentBounds();
  1740. }, this);
  1741. imageLoader.load(poster);
  1742. };
  1743. /**
  1744. * @private
  1745. */
  1746. WebVideo.prototype.$measureContentBounds = function (bounds) {
  1747. var bitmapData = this.bitmapData;
  1748. var posterData = this.posterData;
  1749. if (bitmapData) {
  1750. bounds.setTo(0, 0, this.getPlayWidth(), this.getPlayHeight());
  1751. }
  1752. else if (posterData) {
  1753. bounds.setTo(0, 0, this.getPlayWidth(), this.getPlayHeight());
  1754. }
  1755. else {
  1756. bounds.setEmpty();
  1757. }
  1758. };
  1759. WebVideo.prototype.getPlayWidth = function () {
  1760. if (!isNaN(this.widthSet)) {
  1761. return this.widthSet;
  1762. }
  1763. if (this.bitmapData) {
  1764. return this.bitmapData.width;
  1765. }
  1766. if (this.posterData) {
  1767. return this.posterData.width;
  1768. }
  1769. return NaN;
  1770. };
  1771. WebVideo.prototype.getPlayHeight = function () {
  1772. if (!isNaN(this.heightSet)) {
  1773. return this.heightSet;
  1774. }
  1775. if (this.bitmapData) {
  1776. return this.bitmapData.height;
  1777. }
  1778. if (this.posterData) {
  1779. return this.posterData.height;
  1780. }
  1781. return NaN;
  1782. };
  1783. /**
  1784. * @private
  1785. */
  1786. WebVideo.prototype.$render = function () {
  1787. var node = this.$renderNode;
  1788. var bitmapData = this.bitmapData;
  1789. var posterData = this.posterData;
  1790. var width = this.getPlayWidth();
  1791. var height = this.getPlayHeight();
  1792. if ((!this.isPlayed || egret.Capabilities.isMobile) && posterData) {
  1793. node.image = posterData;
  1794. node.imageWidth = width;
  1795. node.imageHeight = height;
  1796. node.drawImage(0, 0, posterData.width, posterData.height, 0, 0, width, height);
  1797. }
  1798. else if (this.isPlayed && bitmapData) {
  1799. node.image = bitmapData;
  1800. node.imageWidth = bitmapData.width;
  1801. node.imageHeight = bitmapData.height;
  1802. egret.WebGLUtils.deleteWebGLTexture(bitmapData.webGLTexture);
  1803. bitmapData.webGLTexture = null;
  1804. node.drawImage(0, 0, bitmapData.width, bitmapData.height, 0, 0, width, height);
  1805. }
  1806. };
  1807. WebVideo.prototype.markDirty = function () {
  1808. this.$invalidate();
  1809. return true;
  1810. };
  1811. /**
  1812. * @private
  1813. * 设置显示高度
  1814. */
  1815. WebVideo.prototype.$setHeight = function (value) {
  1816. this.heightSet = +value || 0;
  1817. this.$invalidate();
  1818. this.$invalidateContentBounds();
  1819. return _super.prototype.$setHeight.call(this, value);
  1820. };
  1821. /**
  1822. * @private
  1823. * 设置显示宽度
  1824. */
  1825. WebVideo.prototype.$setWidth = function (value) {
  1826. this.widthSet = +value || 0;
  1827. this.$invalidate();
  1828. this.$invalidateContentBounds();
  1829. return _super.prototype.$setWidth.call(this, value);
  1830. };
  1831. Object.defineProperty(WebVideo.prototype, "paused", {
  1832. get: function () {
  1833. if (this.video) {
  1834. return this.video.paused;
  1835. }
  1836. return true;
  1837. },
  1838. enumerable: true,
  1839. configurable: true
  1840. });
  1841. Object.defineProperty(WebVideo.prototype, "length", {
  1842. /**
  1843. * @inheritDoc
  1844. */
  1845. get: function () {
  1846. if (this.video) {
  1847. return this.video.duration;
  1848. }
  1849. throw new Error("Video not loaded!");
  1850. },
  1851. enumerable: true,
  1852. configurable: true
  1853. });
  1854. return WebVideo;
  1855. }(egret.DisplayObject));
  1856. web.WebVideo = WebVideo;
  1857. __reflect(WebVideo.prototype, "egret.web.WebVideo", ["egret.Video"]);
  1858. egret.Video = WebVideo;
  1859. })(web = egret.web || (egret.web = {}));
  1860. })(egret || (egret = {}));
  1861. //////////////////////////////////////////////////////////////////////////////////////
  1862. //
  1863. // Copyright (c) 2014-present, Egret Technology.
  1864. // All rights reserved.
  1865. // Redistribution and use in source and binary forms, with or without
  1866. // modification, are permitted provided that the following conditions are met:
  1867. //
  1868. // * Redistributions of source code must retain the above copyright
  1869. // notice, this list of conditions and the following disclaimer.
  1870. // * Redistributions in binary form must reproduce the above copyright
  1871. // notice, this list of conditions and the following disclaimer in the
  1872. // documentation and/or other materials provided with the distribution.
  1873. // * Neither the name of the Egret nor the
  1874. // names of its contributors may be used to endorse or promote products
  1875. // derived from this software without specific prior written permission.
  1876. //
  1877. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  1878. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  1879. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  1880. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  1881. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  1882. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  1883. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  1884. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  1885. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  1886. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1887. //
  1888. //////////////////////////////////////////////////////////////////////////////////////
  1889. var egret;
  1890. (function (egret) {
  1891. var web;
  1892. (function (web) {
  1893. /**
  1894. * @private
  1895. */
  1896. var WebHttpRequest = (function (_super) {
  1897. __extends(WebHttpRequest, _super);
  1898. /**
  1899. * @private
  1900. */
  1901. function WebHttpRequest() {
  1902. var _this = _super.call(this) || this;
  1903. /**
  1904. * @private
  1905. */
  1906. _this._url = "";
  1907. _this._method = "";
  1908. return _this;
  1909. }
  1910. Object.defineProperty(WebHttpRequest.prototype, "response", {
  1911. /**
  1912. * @private
  1913. * 本次请求返回的数据,数据类型根据responseType设置的值确定。
  1914. */
  1915. get: function () {
  1916. if (!this._xhr) {
  1917. return null;
  1918. }
  1919. if (this._xhr.response != undefined) {
  1920. return this._xhr.response;
  1921. }
  1922. if (this._responseType == "text") {
  1923. return this._xhr.responseText;
  1924. }
  1925. if (this._responseType == "arraybuffer" && /msie 9.0/i.test(navigator.userAgent)) {
  1926. var w = window;
  1927. return w.convertResponseBodyToText(this._xhr["responseBody"]);
  1928. }
  1929. if (this._responseType == "document") {
  1930. return this._xhr.responseXML;
  1931. }
  1932. /*if (this._xhr.responseXML) {
  1933. return this._xhr.responseXML;
  1934. }
  1935. if (this._xhr.responseText != undefined) {
  1936. return this._xhr.responseText;
  1937. }*/
  1938. return null;
  1939. },
  1940. enumerable: true,
  1941. configurable: true
  1942. });
  1943. Object.defineProperty(WebHttpRequest.prototype, "responseType", {
  1944. /**
  1945. * @private
  1946. * 设置返回的数据格式,请使用 HttpResponseType 里定义的枚举值。设置非法的值或不设置,都将使用HttpResponseType.TEXT。
  1947. */
  1948. get: function () {
  1949. return this._responseType;
  1950. },
  1951. set: function (value) {
  1952. this._responseType = value;
  1953. },
  1954. enumerable: true,
  1955. configurable: true
  1956. });
  1957. Object.defineProperty(WebHttpRequest.prototype, "withCredentials", {
  1958. /**
  1959. * @private
  1960. * 表明在进行跨站(cross-site)的访问控制(Access-Control)请求时,是否使用认证信息(例如cookie或授权的header)。 默认为 false。(这个标志不会影响同站的请求)
  1961. */
  1962. get: function () {
  1963. return this._withCredentials;
  1964. },
  1965. set: function (value) {
  1966. this._withCredentials = value;
  1967. },
  1968. enumerable: true,
  1969. configurable: true
  1970. });
  1971. /**
  1972. * @private
  1973. *
  1974. * @returns
  1975. */
  1976. WebHttpRequest.prototype.getXHR = function () {
  1977. if (window["XMLHttpRequest"]) {
  1978. return new window["XMLHttpRequest"]();
  1979. }
  1980. else {
  1981. return new ActiveXObject("MSXML2.XMLHTTP");
  1982. }
  1983. };
  1984. /**
  1985. * @private
  1986. * 初始化一个请求.注意,若在已经发出请求的对象上调用此方法,相当于立即调用abort().
  1987. * @param url 该请求所要访问的URL该请求所要访问的URL
  1988. * @param method 请求所使用的HTTP方法, 请使用 HttpMethod 定义的枚举值.
  1989. */
  1990. WebHttpRequest.prototype.open = function (url, method) {
  1991. if (method === void 0) { method = "GET"; }
  1992. this._url = url;
  1993. this._method = method;
  1994. if (this._xhr) {
  1995. this._xhr.abort();
  1996. this._xhr = null;
  1997. }
  1998. this._xhr = this.getXHR(); //new XMLHttpRequest();
  1999. this._xhr.onreadystatechange = this.onReadyStateChange.bind(this);
  2000. this._xhr.onprogress = this.updateProgress.bind(this);
  2001. this._xhr.open(this._method, this._url, true);
  2002. };
  2003. /**
  2004. * @private
  2005. * 发送请求.
  2006. * @param data 需要发送的数据
  2007. */
  2008. WebHttpRequest.prototype.send = function (data) {
  2009. if (this._responseType != null) {
  2010. this._xhr.responseType = this._responseType;
  2011. }
  2012. if (this._withCredentials != null) {
  2013. this._xhr.withCredentials = this._withCredentials;
  2014. }
  2015. if (this.headerObj) {
  2016. for (var key in this.headerObj) {
  2017. this._xhr.setRequestHeader(key, this.headerObj[key]);
  2018. }
  2019. }
  2020. this._xhr.send(data);
  2021. };
  2022. /**
  2023. * @private
  2024. * 如果请求已经被发送,则立刻中止请求.
  2025. */
  2026. WebHttpRequest.prototype.abort = function () {
  2027. if (this._xhr) {
  2028. this._xhr.abort();
  2029. }
  2030. };
  2031. /**
  2032. * @private
  2033. * 返回所有响应头信息(响应头名和值), 如果响应头还没接受,则返回"".
  2034. */
  2035. WebHttpRequest.prototype.getAllResponseHeaders = function () {
  2036. if (!this._xhr) {
  2037. return null;
  2038. }
  2039. var result = this._xhr.getAllResponseHeaders();
  2040. return result ? result : "";
  2041. };
  2042. /**
  2043. * @private
  2044. * 给指定的HTTP请求头赋值.在这之前,您必须确认已经调用 open() 方法打开了一个url.
  2045. * @param header 将要被赋值的请求头名称.
  2046. * @param value 给指定的请求头赋的值.
  2047. */
  2048. WebHttpRequest.prototype.setRequestHeader = function (header, value) {
  2049. if (!this.headerObj) {
  2050. this.headerObj = {};
  2051. }
  2052. this.headerObj[header] = value;
  2053. };
  2054. /**
  2055. * @private
  2056. * 返回指定的响应头的值, 如果响应头还没被接受,或该响应头不存在,则返回"".
  2057. * @param header 要返回的响应头名称
  2058. */
  2059. WebHttpRequest.prototype.getResponseHeader = function (header) {
  2060. if (!this._xhr) {
  2061. return null;
  2062. }
  2063. var result = this._xhr.getResponseHeader(header);
  2064. return result ? result : "";
  2065. };
  2066. /**
  2067. * @private
  2068. */
  2069. WebHttpRequest.prototype.onReadyStateChange = function () {
  2070. var xhr = this._xhr;
  2071. if (xhr.readyState == 4) {
  2072. var ioError_1 = (xhr.status >= 400 || xhr.status == 0);
  2073. var url_1 = this._url;
  2074. var self_1 = this;
  2075. window.setTimeout(function () {
  2076. if (ioError_1) {
  2077. if (true && !self_1.hasEventListener(egret.IOErrorEvent.IO_ERROR)) {
  2078. egret.$error(1011, url_1);
  2079. }
  2080. self_1.dispatchEventWith(egret.IOErrorEvent.IO_ERROR);
  2081. }
  2082. else {
  2083. self_1.dispatchEventWith(egret.Event.COMPLETE);
  2084. }
  2085. }, 0);
  2086. }
  2087. };
  2088. /**
  2089. * @private
  2090. */
  2091. WebHttpRequest.prototype.updateProgress = function (event) {
  2092. if (event.lengthComputable) {
  2093. egret.ProgressEvent.dispatchProgressEvent(this, egret.ProgressEvent.PROGRESS, event.loaded, event.total);
  2094. }
  2095. };
  2096. return WebHttpRequest;
  2097. }(egret.EventDispatcher));
  2098. web.WebHttpRequest = WebHttpRequest;
  2099. __reflect(WebHttpRequest.prototype, "egret.web.WebHttpRequest", ["egret.HttpRequest"]);
  2100. egret.HttpRequest = WebHttpRequest;
  2101. if (true) {
  2102. egret.$markReadOnly(WebHttpRequest, "response");
  2103. }
  2104. })(web = egret.web || (egret.web = {}));
  2105. })(egret || (egret = {}));
  2106. //////////////////////////////////////////////////////////////////////////////////////
  2107. //
  2108. // Copyright (c) 2014-present, Egret Technology.
  2109. // All rights reserved.
  2110. // Redistribution and use in source and binary forms, with or without
  2111. // modification, are permitted provided that the following conditions are met:
  2112. //
  2113. // * Redistributions of source code must retain the above copyright
  2114. // notice, this list of conditions and the following disclaimer.
  2115. // * Redistributions in binary form must reproduce the above copyright
  2116. // notice, this list of conditions and the following disclaimer in the
  2117. // documentation and/or other materials provided with the distribution.
  2118. // * Neither the name of the Egret nor the
  2119. // names of its contributors may be used to endorse or promote products
  2120. // derived from this software without specific prior written permission.
  2121. //
  2122. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  2123. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  2124. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  2125. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  2126. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2127. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  2128. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  2129. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  2130. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  2131. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2132. //
  2133. //////////////////////////////////////////////////////////////////////////////////////
  2134. var egret;
  2135. (function (egret) {
  2136. var web;
  2137. (function (web) {
  2138. var winURL = window["URL"] || window["webkitURL"];
  2139. /**
  2140. * @private
  2141. * ImageLoader 类可用于加载图像(JPG、PNG 或 GIF)文件。使用 load() 方法来启动加载。被加载的图像对象数据将存储在 ImageLoader.data 属性上 。
  2142. */
  2143. var WebImageLoader = (function (_super) {
  2144. __extends(WebImageLoader, _super);
  2145. function WebImageLoader() {
  2146. var _this = _super.apply(this, arguments) || this;
  2147. /**
  2148. * @private
  2149. * 使用 load() 方法加载成功的 BitmapData 图像数据。
  2150. */
  2151. _this.data = null;
  2152. /**
  2153. * @private
  2154. * 当从其他站点加载一个图片时,指定是否启用跨域资源共享(CORS),默认值为null。
  2155. * 可以设置为"anonymous","use-credentials"或null,设置为其他值将等同于"anonymous"。
  2156. */
  2157. _this._crossOrigin = null;
  2158. /**
  2159. * @private
  2160. * 标记crossOrigin有没有被设置过,设置过之后使用设置的属性
  2161. */
  2162. _this._hasCrossOriginSet = false;
  2163. /**
  2164. * @private
  2165. */
  2166. _this.currentImage = null;
  2167. /**
  2168. * @private
  2169. */
  2170. _this.request = null;
  2171. return _this;
  2172. }
  2173. Object.defineProperty(WebImageLoader.prototype, "crossOrigin", {
  2174. get: function () {
  2175. return this._crossOrigin;
  2176. },
  2177. set: function (value) {
  2178. this._hasCrossOriginSet = true;
  2179. this._crossOrigin = value;
  2180. },
  2181. enumerable: true,
  2182. configurable: true
  2183. });
  2184. /**
  2185. * @private
  2186. * 启动一次图像加载。注意:若之前已经调用过加载请求,重新调用 load() 将终止先前的请求,并开始新的加载。
  2187. * @param url 要加载的图像文件的地址。
  2188. */
  2189. WebImageLoader.prototype.load = function (url) {
  2190. if (web.Html5Capatibility._canUseBlob
  2191. && url.indexOf("wxLocalResource:") != 0 //微信专用不能使用 blob
  2192. && url.indexOf("data:") != 0
  2193. && url.indexOf("http:") != 0
  2194. && url.indexOf("https:") != 0) {
  2195. var request = this.request;
  2196. if (!request) {
  2197. request = this.request = new egret.web.WebHttpRequest();
  2198. request.addEventListener(egret.Event.COMPLETE, this.onBlobLoaded, this);
  2199. request.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onBlobError, this);
  2200. request.responseType = "blob";
  2201. }
  2202. if (true) {
  2203. this.currentURL = url;
  2204. }
  2205. request.open(url);
  2206. request.send();
  2207. }
  2208. else {
  2209. this.loadImage(url);
  2210. }
  2211. };
  2212. /**
  2213. * @private
  2214. */
  2215. WebImageLoader.prototype.onBlobLoaded = function (event) {
  2216. var blob = this.request.response;
  2217. this.loadImage(winURL.createObjectURL(blob));
  2218. };
  2219. /**
  2220. * @private
  2221. */
  2222. WebImageLoader.prototype.onBlobError = function (event) {
  2223. this.dispatchIOError(this.currentURL);
  2224. };
  2225. /**
  2226. * @private
  2227. */
  2228. WebImageLoader.prototype.loadImage = function (src) {
  2229. var image = new Image();
  2230. this.data = null;
  2231. this.currentImage = image;
  2232. if (this._hasCrossOriginSet) {
  2233. if (this._crossOrigin) {
  2234. image.crossOrigin = this._crossOrigin;
  2235. }
  2236. }
  2237. else {
  2238. if (WebImageLoader.crossOrigin) {
  2239. image.crossOrigin = WebImageLoader.crossOrigin;
  2240. }
  2241. }
  2242. /*else {
  2243. if (image.hasAttribute("crossOrigin")) {//兼容猎豹
  2244. image.removeAttribute("crossOrigin");
  2245. }
  2246. }*/
  2247. image.onload = this.onImageComplete.bind(this);
  2248. image.onerror = this.onLoadError.bind(this);
  2249. image.src = src;
  2250. };
  2251. /**
  2252. * @private
  2253. */
  2254. WebImageLoader.prototype.onImageComplete = function (event) {
  2255. var image = this.getImage(event);
  2256. if (!image) {
  2257. return;
  2258. }
  2259. this.data = new egret.BitmapData(image);
  2260. var self = this;
  2261. window.setTimeout(function () {
  2262. self.dispatchEventWith(egret.Event.COMPLETE);
  2263. }, 0);
  2264. };
  2265. /**
  2266. * @private
  2267. */
  2268. WebImageLoader.prototype.onLoadError = function (event) {
  2269. var image = this.getImage(event);
  2270. if (!image) {
  2271. return;
  2272. }
  2273. this.dispatchIOError(image.src);
  2274. };
  2275. WebImageLoader.prototype.dispatchIOError = function (url) {
  2276. var self = this;
  2277. window.setTimeout(function () {
  2278. if (true && !self.hasEventListener(egret.IOErrorEvent.IO_ERROR)) {
  2279. egret.$error(1011, url);
  2280. }
  2281. self.dispatchEventWith(egret.IOErrorEvent.IO_ERROR);
  2282. }, 0);
  2283. };
  2284. /**
  2285. * @private
  2286. */
  2287. WebImageLoader.prototype.getImage = function (event) {
  2288. var image = event.target;
  2289. var url = image.src;
  2290. if (url.indexOf("blob:") == 0) {
  2291. try {
  2292. winURL.revokeObjectURL(image.src);
  2293. }
  2294. catch (e) {
  2295. egret.$warn(1037);
  2296. }
  2297. }
  2298. image.onerror = null;
  2299. image.onload = null;
  2300. if (this.currentImage !== image) {
  2301. return null;
  2302. }
  2303. this.currentImage = null;
  2304. return image;
  2305. };
  2306. return WebImageLoader;
  2307. }(egret.EventDispatcher));
  2308. /**
  2309. * @private
  2310. * 指定是否启用跨域资源共享,如果ImageLoader实例有设置过crossOrigin属性将使用设置的属性
  2311. */
  2312. WebImageLoader.crossOrigin = null;
  2313. web.WebImageLoader = WebImageLoader;
  2314. __reflect(WebImageLoader.prototype, "egret.web.WebImageLoader", ["egret.ImageLoader"]);
  2315. egret.ImageLoader = WebImageLoader;
  2316. })(web = egret.web || (egret.web = {}));
  2317. })(egret || (egret = {}));
  2318. //////////////////////////////////////////////////////////////////////////////////////
  2319. //
  2320. // Copyright (c) 2014-present, Egret Technology.
  2321. // All rights reserved.
  2322. // Redistribution and use in source and binary forms, with or without
  2323. // modification, are permitted provided that the following conditions are met:
  2324. //
  2325. // * Redistributions of source code must retain the above copyright
  2326. // notice, this list of conditions and the following disclaimer.
  2327. // * Redistributions in binary form must reproduce the above copyright
  2328. // notice, this list of conditions and the following disclaimer in the
  2329. // documentation and/or other materials provided with the distribution.
  2330. // * Neither the name of the Egret nor the
  2331. // names of its contributors may be used to endorse or promote products
  2332. // derived from this software without specific prior written permission.
  2333. //
  2334. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  2335. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  2336. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  2337. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  2338. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  2339. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  2340. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  2341. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  2342. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  2343. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2344. //
  2345. //////////////////////////////////////////////////////////////////////////////////////
  2346. var egret;
  2347. (function (egret) {
  2348. var web;
  2349. (function (web) {
  2350. /**
  2351. * @classdesc
  2352. * @extends egret.StageText
  2353. * @private
  2354. */
  2355. var HTML5StageText = (function (_super) {
  2356. __extends(HTML5StageText, _super);
  2357. /**
  2358. * @private
  2359. */
  2360. function HTML5StageText() {
  2361. var _this = _super.call(this) || this;
  2362. /**
  2363. * @private
  2364. */
  2365. _this._isNeedShow = false;
  2366. /**
  2367. * @private
  2368. */
  2369. _this.inputElement = null;
  2370. /**
  2371. * @private
  2372. */
  2373. _this.inputDiv = null;
  2374. /**
  2375. * @private
  2376. */
  2377. _this._gscaleX = 0;
  2378. /**
  2379. * @private
  2380. */
  2381. _this._gscaleY = 0;
  2382. /**
  2383. * @private
  2384. */
  2385. _this._isNeesHide = false;
  2386. /**
  2387. * @private
  2388. */
  2389. _this.textValue = "";
  2390. /**
  2391. * @private
  2392. */
  2393. _this.colorValue = 0xffffff;
  2394. /**
  2395. * @private
  2396. */
  2397. _this._styleInfoes = {};
  2398. return _this;
  2399. }
  2400. /**
  2401. * @private
  2402. *
  2403. * @param textfield
  2404. */
  2405. HTML5StageText.prototype.$setTextField = function (textfield) {
  2406. this.$textfield = textfield;
  2407. return true;
  2408. };
  2409. /**
  2410. * @private
  2411. *
  2412. */
  2413. HTML5StageText.prototype.$addToStage = function () {
  2414. this.htmlInput = egret.web.$getTextAdapter(this.$textfield);
  2415. };
  2416. /**
  2417. * @private
  2418. *
  2419. */
  2420. HTML5StageText.prototype._initElement = function () {
  2421. var point = this.$textfield.localToGlobal(0, 0);
  2422. var x = point.x;
  2423. var y = point.y;
  2424. // let m = this.$textfield.$renderNode.renderMatrix;
  2425. // let cX = m.a;
  2426. // let cY = m.d;
  2427. var scaleX = this.htmlInput.$scaleX;
  2428. var scaleY = this.htmlInput.$scaleY;
  2429. this.inputDiv.style.left = x * scaleX + "px";
  2430. this.inputDiv.style.top = y * scaleY + "px";
  2431. if (this.$textfield.multiline && this.$textfield.height > this.$textfield.size) {
  2432. this.inputDiv.style.top = (y) * scaleY + "px";
  2433. this.inputElement.style.top = (-this.$textfield.lineSpacing / 2) * scaleY + "px";
  2434. }
  2435. else {
  2436. this.inputDiv.style.top = y * scaleY + "px";
  2437. this.inputElement.style.top = 0 + "px";
  2438. }
  2439. var node = this.$textfield;
  2440. var cX = 1;
  2441. var cY = 1;
  2442. var rotation = 0;
  2443. while (node.parent) {
  2444. cX *= node.scaleX;
  2445. cY *= node.scaleY;
  2446. rotation += node.rotation;
  2447. node = node.parent;
  2448. }
  2449. var transformKey = egret.web.getPrefixStyleName("transform");
  2450. this.inputDiv.style[transformKey] = "rotate(" + rotation + "deg)";
  2451. this._gscaleX = scaleX * cX;
  2452. this._gscaleY = scaleY * cY;
  2453. };
  2454. /**
  2455. * @private
  2456. *
  2457. */
  2458. HTML5StageText.prototype.$show = function () {
  2459. if (!this.htmlInput.isCurrentStageText(this)) {
  2460. this.inputElement = this.htmlInput.getInputElement(this);
  2461. if (!this.$textfield.multiline) {
  2462. this.inputElement.type = this.$textfield.inputType;
  2463. }
  2464. else {
  2465. this.inputElement.type = "text";
  2466. }
  2467. this.inputDiv = this.htmlInput._inputDIV;
  2468. }
  2469. else {
  2470. this.inputElement.onblur = null;
  2471. }
  2472. this.htmlInput._needShow = true;
  2473. //标记当前文本被选中
  2474. this._isNeedShow = true;
  2475. this._initElement();
  2476. };
  2477. /**
  2478. * @private
  2479. *
  2480. */
  2481. HTML5StageText.prototype.onBlurHandler = function () {
  2482. this.htmlInput.clearInputElement();
  2483. window.scrollTo(0, 0);
  2484. };
  2485. /**
  2486. * @private
  2487. *
  2488. */
  2489. HTML5StageText.prototype.executeShow = function () {
  2490. var self = this;
  2491. //打开
  2492. this.inputElement.value = this.$getText();
  2493. if (this.inputElement.onblur == null) {
  2494. this.inputElement.onblur = this.onBlurHandler.bind(this);
  2495. }
  2496. this.$resetStageText();
  2497. if (this.$textfield.maxChars > 0) {
  2498. this.inputElement.setAttribute("maxlength", this.$textfield.maxChars);
  2499. }
  2500. else {
  2501. this.inputElement.removeAttribute("maxlength");
  2502. }
  2503. this.inputElement.selectionStart = this.inputElement.value.length;
  2504. this.inputElement.selectionEnd = this.inputElement.value.length;
  2505. this.inputElement.focus();
  2506. };
  2507. /**
  2508. * @private
  2509. *
  2510. */
  2511. HTML5StageText.prototype.$hide = function () {
  2512. //标记当前点击其他地方关闭
  2513. this._isNeesHide = true;
  2514. if (this.htmlInput && egret.web.Html5Capatibility._System_OS == egret.web.SystemOSType.IOS) {
  2515. this.htmlInput.disconnectStageText(this);
  2516. }
  2517. };
  2518. /**
  2519. * @private
  2520. *
  2521. * @returns
  2522. */
  2523. HTML5StageText.prototype.$getText = function () {
  2524. if (!this.textValue) {
  2525. this.textValue = "";
  2526. }
  2527. return this.textValue;
  2528. };
  2529. /**
  2530. * @private
  2531. *
  2532. * @param value
  2533. */
  2534. HTML5StageText.prototype.$setText = function (value) {
  2535. this.textValue = value;
  2536. this.resetText();
  2537. return true;
  2538. };
  2539. /**
  2540. * @private
  2541. *
  2542. */
  2543. HTML5StageText.prototype.resetText = function () {
  2544. if (this.inputElement) {
  2545. this.inputElement.value = this.textValue;
  2546. }
  2547. };
  2548. HTML5StageText.prototype.$setColor = function (value) {
  2549. this.colorValue = value;
  2550. this.resetColor();
  2551. return true;
  2552. };
  2553. /**
  2554. * @private
  2555. *
  2556. */
  2557. HTML5StageText.prototype.resetColor = function () {
  2558. if (this.inputElement) {
  2559. this.setElementStyle("color", egret.toColorString(this.colorValue));
  2560. }
  2561. };
  2562. HTML5StageText.prototype.$onBlur = function () {
  2563. if (web.Html5Capatibility._System_OS == web.SystemOSType.WPHONE) {
  2564. egret.Event.dispatchEvent(this, "updateText", false);
  2565. }
  2566. };
  2567. /**
  2568. * @private
  2569. *
  2570. */
  2571. HTML5StageText.prototype._onInput = function () {
  2572. var self = this;
  2573. if (web.Html5Capatibility._System_OS == web.SystemOSType.WPHONE) {
  2574. var values = this.$textfield.$TextField;
  2575. if (values[35 /* restrictAnd */] == null && values[36 /* restrictNot */] == null) {
  2576. self.textValue = self.inputElement.value;
  2577. egret.Event.dispatchEvent(self, "updateText", false);
  2578. }
  2579. else {
  2580. window.setTimeout(function () {
  2581. if (self.inputElement && self.inputElement.selectionStart && self.inputElement.selectionEnd) {
  2582. if (self.inputElement.selectionStart == self.inputElement.selectionEnd) {
  2583. self.textValue = self.inputElement.value;
  2584. egret.Event.dispatchEvent(self, "updateText", false);
  2585. }
  2586. }
  2587. }, 0);
  2588. }
  2589. }
  2590. else {
  2591. window.setTimeout(function () {
  2592. if (self.inputElement && self.inputElement.selectionStart == self.inputElement.selectionEnd) {
  2593. self.textValue = self.inputElement.value;
  2594. egret.Event.dispatchEvent(self, "updateText", false);
  2595. }
  2596. }, 0);
  2597. }
  2598. };
  2599. HTML5StageText.prototype.setAreaHeight = function () {
  2600. var textfield = this.$textfield;
  2601. if (textfield.multiline) {
  2602. var textheight = egret.TextFieldUtils.$getTextHeight(textfield);
  2603. if (textfield.height <= textfield.size) {
  2604. this.setElementStyle("height", (textfield.size) * this._gscaleY + "px");
  2605. this.setElementStyle("padding", "0px");
  2606. this.setElementStyle("lineHeight", (textfield.size) * this._gscaleY + "px");
  2607. }
  2608. else if (textfield.height < textheight) {
  2609. this.setElementStyle("height", (textfield.height) * this._gscaleY + "px");
  2610. this.setElementStyle("padding", "0px");
  2611. this.setElementStyle("lineHeight", (textfield.size + textfield.lineSpacing) * this._gscaleY + "px");
  2612. }
  2613. else {
  2614. this.setElementStyle("height", (textheight + textfield.lineSpacing) * this._gscaleY + "px");
  2615. var rap = (textfield.height - textheight) * this._gscaleY;
  2616. var valign = egret.TextFieldUtils.$getValign(textfield);
  2617. var top_1 = rap * valign;
  2618. var bottom = rap - top_1;
  2619. this.setElementStyle("padding", top_1 + "px 0px " + bottom + "px 0px");
  2620. this.setElementStyle("lineHeight", (textfield.size + textfield.lineSpacing) * this._gscaleY + "px");
  2621. }
  2622. }
  2623. };
  2624. /**
  2625. * @private
  2626. *
  2627. * @param e
  2628. */
  2629. HTML5StageText.prototype._onClickHandler = function (e) {
  2630. if (this._isNeedShow) {
  2631. e.stopImmediatePropagation();
  2632. //e.preventDefault();
  2633. this._isNeedShow = false;
  2634. this.executeShow();
  2635. this.dispatchEvent(new egret.Event("focus"));
  2636. }
  2637. };
  2638. /**
  2639. * @private
  2640. *
  2641. */
  2642. HTML5StageText.prototype._onDisconnect = function () {
  2643. this.inputElement = null;
  2644. this.dispatchEvent(new egret.Event("blur"));
  2645. };
  2646. /**
  2647. * @private
  2648. *
  2649. * @param style
  2650. * @param value
  2651. */
  2652. HTML5StageText.prototype.setElementStyle = function (style, value) {
  2653. if (this.inputElement) {
  2654. if (this._styleInfoes[style] != value) {
  2655. this.inputElement.style[style] = value;
  2656. }
  2657. }
  2658. };
  2659. /**
  2660. * @private
  2661. *
  2662. */
  2663. HTML5StageText.prototype.$removeFromStage = function () {
  2664. if (this.inputElement) {
  2665. this.htmlInput.disconnectStageText(this);
  2666. }
  2667. };
  2668. /**
  2669. * 修改位置
  2670. * @private
  2671. */
  2672. HTML5StageText.prototype.$resetStageText = function () {
  2673. if (this.inputElement) {
  2674. var textfield = this.$textfield;
  2675. this.setElementStyle("fontFamily", textfield.fontFamily);
  2676. this.setElementStyle("fontStyle", textfield.italic ? "italic" : "normal");
  2677. this.setElementStyle("fontWeight", textfield.bold ? "bold" : "normal");
  2678. this.setElementStyle("textAlign", textfield.textAlign);
  2679. this.setElementStyle("fontSize", textfield.size * this._gscaleY + "px");
  2680. this.setElementStyle("color", egret.toColorString(textfield.textColor));
  2681. var tw = void 0;
  2682. if (textfield.stage) {
  2683. tw = textfield.localToGlobal(0, 0).x;
  2684. tw = Math.min(textfield.width, textfield.stage.stageWidth - tw);
  2685. }
  2686. else {
  2687. tw = textfield.width;
  2688. }
  2689. this.setElementStyle("width", tw * this._gscaleX + "px");
  2690. this.setElementStyle("verticalAlign", textfield.verticalAlign);
  2691. if (textfield.multiline) {
  2692. this.setAreaHeight();
  2693. }
  2694. else {
  2695. this.setElementStyle("lineHeight", (textfield.size) * this._gscaleY + "px");
  2696. if (textfield.height < textfield.size) {
  2697. this.setElementStyle("height", (textfield.size) * this._gscaleY + "px");
  2698. var bottom = (textfield.size / 2) * this._gscaleY;
  2699. this.setElementStyle("padding", "0px 0px " + bottom + "px 0px");
  2700. }
  2701. else {
  2702. this.setElementStyle("height", (textfield.size) * this._gscaleY + "px");
  2703. var rap = (textfield.height - textfield.size) * this._gscaleY;
  2704. var valign = egret.TextFieldUtils.$getValign(textfield);
  2705. var top_2 = rap * valign;
  2706. var bottom = rap - top_2;
  2707. if (bottom < textfield.size / 2 * this._gscaleY) {
  2708. bottom = textfield.size / 2 * this._gscaleY;
  2709. }
  2710. this.setElementStyle("padding", top_2 + "px 0px " + bottom + "px 0px");
  2711. }
  2712. }
  2713. this.inputDiv.style.clip = "rect(0px " + (textfield.width * this._gscaleX) + "px " + (textfield.height * this._gscaleY) + "px 0px)";
  2714. this.inputDiv.style.height = textfield.height * this._gscaleY + "px";
  2715. this.inputDiv.style.width = tw * this._gscaleX + "px";
  2716. }
  2717. };
  2718. return HTML5StageText;
  2719. }(egret.EventDispatcher));
  2720. web.HTML5StageText = HTML5StageText;
  2721. __reflect(HTML5StageText.prototype, "egret.web.HTML5StageText", ["egret.StageText"]);
  2722. egret.StageText = HTML5StageText;
  2723. })(web = egret.web || (egret.web = {}));
  2724. })(egret || (egret = {}));
  2725. (function (egret) {
  2726. var web;
  2727. (function (web) {
  2728. /**
  2729. * @private
  2730. */
  2731. var HTMLInput = (function () {
  2732. function HTMLInput() {
  2733. /**
  2734. * @private
  2735. */
  2736. this._needShow = false;
  2737. /**
  2738. * @private
  2739. */
  2740. this.$scaleX = 1;
  2741. /**
  2742. * @private
  2743. */
  2744. this.$scaleY = 1;
  2745. }
  2746. /**
  2747. * @private
  2748. *
  2749. * @returns
  2750. */
  2751. HTMLInput.prototype.isInputOn = function () {
  2752. return this._stageText != null;
  2753. };
  2754. /**
  2755. * @private
  2756. *
  2757. * @param stageText
  2758. * @returns
  2759. */
  2760. HTMLInput.prototype.isCurrentStageText = function (stageText) {
  2761. return this._stageText == stageText;
  2762. };
  2763. /**
  2764. * @private
  2765. *
  2766. * @param dom
  2767. */
  2768. HTMLInput.prototype.initValue = function (dom) {
  2769. dom.style.position = "absolute";
  2770. dom.style.left = "0px";
  2771. dom.style.top = "0px";
  2772. dom.style.border = "none";
  2773. dom.style.padding = "0";
  2774. };
  2775. /**
  2776. * @private
  2777. *
  2778. */
  2779. HTMLInput.prototype.$updateSize = function () {
  2780. if (!this.canvas) {
  2781. return;
  2782. }
  2783. var stageW = this.canvas.width;
  2784. var stageH = this.canvas.height;
  2785. var screenW = this.canvas.style.width.split("px")[0];
  2786. var screenH = this.canvas.style.height.split("px")[0];
  2787. this.$scaleX = screenW / stageW;
  2788. this.$scaleY = screenH / stageH;
  2789. this.StageDelegateDiv.style.left = this.canvas.style.left;
  2790. this.StageDelegateDiv.style.top = this.canvas.style.top;
  2791. var transformKey = egret.web.getPrefixStyleName("transform");
  2792. this.StageDelegateDiv.style[transformKey] = this.canvas.style[transformKey];
  2793. this.StageDelegateDiv.style[egret.web.getPrefixStyleName("transformOrigin")] = "0% 0% 0px";
  2794. };
  2795. /**
  2796. * @private
  2797. *
  2798. * @param container
  2799. * @param canvas
  2800. * @returns
  2801. */
  2802. HTMLInput.prototype._initStageDelegateDiv = function (container, canvas) {
  2803. this.canvas = canvas;
  2804. var self = this;
  2805. var stageDelegateDiv;
  2806. if (!stageDelegateDiv) {
  2807. stageDelegateDiv = document.createElement("div");
  2808. this.StageDelegateDiv = stageDelegateDiv;
  2809. stageDelegateDiv.id = "StageDelegateDiv";
  2810. container.appendChild(stageDelegateDiv);
  2811. self.initValue(stageDelegateDiv);
  2812. self._inputDIV = document.createElement("div");
  2813. self.initValue(self._inputDIV);
  2814. self._inputDIV.style.width = "0px";
  2815. self._inputDIV.style.height = "0px";
  2816. self._inputDIV.style.left = 0 + "px";
  2817. self._inputDIV.style.top = "-100px";
  2818. self._inputDIV.style[egret.web.getPrefixStyleName("transformOrigin")] = "0% 0% 0px";
  2819. stageDelegateDiv.appendChild(self._inputDIV);
  2820. this.canvas.addEventListener("click", function (e) {
  2821. if (self._needShow) {
  2822. self._needShow = false;
  2823. self._stageText._onClickHandler(e);
  2824. self.show();
  2825. }
  2826. else {
  2827. if (self._inputElement) {
  2828. self.clearInputElement();
  2829. self._inputElement.blur();
  2830. self._inputElement = null;
  2831. }
  2832. }
  2833. });
  2834. self.initInputElement(true);
  2835. self.initInputElement(false);
  2836. }
  2837. };
  2838. //初始化输入框
  2839. HTMLInput.prototype.initInputElement = function (multiline) {
  2840. var self = this;
  2841. //增加1个空的textarea
  2842. var inputElement;
  2843. if (multiline) {
  2844. inputElement = document.createElement("textarea");
  2845. inputElement.style["resize"] = "none";
  2846. self._multiElement = inputElement;
  2847. inputElement.id = "egretTextarea";
  2848. }
  2849. else {
  2850. inputElement = document.createElement("input");
  2851. self._simpleElement = inputElement;
  2852. inputElement.id = "egretInput";
  2853. }
  2854. inputElement.type = "text";
  2855. self._inputDIV.appendChild(inputElement);
  2856. inputElement.setAttribute("tabindex", "-1");
  2857. inputElement.style.width = "1px";
  2858. inputElement.style.height = "12px";
  2859. self.initValue(inputElement);
  2860. inputElement.style.outline = "thin";
  2861. inputElement.style.background = "none";
  2862. inputElement.style.overflow = "hidden";
  2863. inputElement.style.wordBreak = "break-all";
  2864. //隐藏输入框
  2865. inputElement.style.opacity = 0;
  2866. inputElement.oninput = function () {
  2867. if (self._stageText) {
  2868. self._stageText._onInput();
  2869. }
  2870. };
  2871. };
  2872. /**
  2873. * @private
  2874. *
  2875. */
  2876. HTMLInput.prototype.show = function () {
  2877. var self = this;
  2878. var inputElement = self._inputElement;
  2879. //隐藏输入框
  2880. egret.$callAsync(function () {
  2881. inputElement.style.opacity = 1;
  2882. }, self);
  2883. };
  2884. /**
  2885. * @private
  2886. *
  2887. * @param stageText
  2888. */
  2889. HTMLInput.prototype.disconnectStageText = function (stageText) {
  2890. if (this._stageText == null || this._stageText == stageText) {
  2891. this.clearInputElement();
  2892. if (this._inputElement) {
  2893. this._inputElement.blur();
  2894. }
  2895. }
  2896. };
  2897. /**
  2898. * @private
  2899. *
  2900. */
  2901. HTMLInput.prototype.clearInputElement = function () {
  2902. var self = this;
  2903. if (self._inputElement) {
  2904. self._inputElement.value = "";
  2905. self._inputElement.onblur = null;
  2906. self._inputElement.style.width = "1px";
  2907. self._inputElement.style.height = "12px";
  2908. self._inputElement.style.left = "0px";
  2909. self._inputElement.style.top = "0px";
  2910. self._inputElement.style.opacity = 0;
  2911. var otherElement = void 0;
  2912. if (self._simpleElement == self._inputElement) {
  2913. otherElement = self._multiElement;
  2914. }
  2915. else {
  2916. otherElement = self._simpleElement;
  2917. }
  2918. otherElement.style.display = "block";
  2919. self._inputDIV.style.left = 0 + "px";
  2920. self._inputDIV.style.top = "-100px";
  2921. self._inputDIV.style.height = 0 + "px";
  2922. self._inputDIV.style.width = 0 + "px";
  2923. }
  2924. if (self._stageText) {
  2925. self._stageText._onDisconnect();
  2926. self._stageText = null;
  2927. this.canvas['userTyping'] = false;
  2928. }
  2929. };
  2930. /**
  2931. * @private
  2932. *
  2933. * @param stageText
  2934. * @returns
  2935. */
  2936. HTMLInput.prototype.getInputElement = function (stageText) {
  2937. var self = this;
  2938. self.clearInputElement();
  2939. self._stageText = stageText;
  2940. this.canvas['userTyping'] = true;
  2941. if (self._stageText.$textfield.multiline) {
  2942. self._inputElement = self._multiElement;
  2943. }
  2944. else {
  2945. self._inputElement = self._simpleElement;
  2946. }
  2947. var otherElement;
  2948. if (self._simpleElement == self._inputElement) {
  2949. otherElement = self._multiElement;
  2950. }
  2951. else {
  2952. otherElement = self._simpleElement;
  2953. }
  2954. otherElement.style.display = "none";
  2955. return self._inputElement;
  2956. };
  2957. return HTMLInput;
  2958. }());
  2959. web.HTMLInput = HTMLInput;
  2960. __reflect(HTMLInput.prototype, "egret.web.HTMLInput");
  2961. })(web = egret.web || (egret.web = {}));
  2962. })(egret || (egret = {}));
  2963. (function (egret) {
  2964. var web;
  2965. (function (web) {
  2966. var stageToTextLayerMap = {};
  2967. var stageToCanvasMap = {};
  2968. var stageToContainerMap = {};
  2969. /**
  2970. * @private
  2971. * 获取
  2972. */
  2973. function $getTextAdapter(textfield) {
  2974. var stageHash = textfield.stage ? textfield.stage.$hashCode : 0;
  2975. var adapter = stageToTextLayerMap[stageHash];
  2976. var canvas = stageToCanvasMap[stageHash];
  2977. var container = stageToContainerMap[stageHash];
  2978. if (canvas && container) {
  2979. //adapter._initStageDelegateDiv(container, canvas);
  2980. //adapter.$updateSize();
  2981. delete stageToCanvasMap[stageHash];
  2982. delete stageToContainerMap[stageHash];
  2983. }
  2984. return adapter;
  2985. }
  2986. web.$getTextAdapter = $getTextAdapter;
  2987. /**
  2988. * @private
  2989. */
  2990. function $cacheTextAdapter(adapter, stage, container, canvas) {
  2991. adapter._initStageDelegateDiv(container, canvas);
  2992. stageToTextLayerMap[stage.$hashCode] = adapter;
  2993. stageToCanvasMap[stage.$hashCode] = canvas;
  2994. stageToContainerMap[stage.$hashCode] = container;
  2995. }
  2996. web.$cacheTextAdapter = $cacheTextAdapter;
  2997. })(web = egret.web || (egret.web = {}));
  2998. })(egret || (egret = {}));
  2999. //////////////////////////////////////////////////////////////////////////////////////
  3000. //
  3001. // Copyright (c) 2014-present, Egret Technology.
  3002. // All rights reserved.
  3003. // Redistribution and use in source and binary forms, with or without
  3004. // modification, are permitted provided that the following conditions are met:
  3005. //
  3006. // * Redistributions of source code must retain the above copyright
  3007. // notice, this list of conditions and the following disclaimer.
  3008. // * Redistributions in binary form must reproduce the above copyright
  3009. // notice, this list of conditions and the following disclaimer in the
  3010. // documentation and/or other materials provided with the distribution.
  3011. // * Neither the name of the Egret nor the
  3012. // names of its contributors may be used to endorse or promote products
  3013. // derived from this software without specific prior written permission.
  3014. //
  3015. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  3016. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  3017. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  3018. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  3019. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3020. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  3021. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  3022. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  3023. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  3024. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3025. //
  3026. //////////////////////////////////////////////////////////////////////////////////////
  3027. var egret;
  3028. (function (egret) {
  3029. var web;
  3030. (function (web) {
  3031. /**
  3032. * @private
  3033. */
  3034. var context = null;
  3035. /**
  3036. * @private
  3037. */
  3038. var fontCache = {};
  3039. /**
  3040. * 测量文本在指定样式下的宽度。
  3041. * @param text 要测量的文本内容。
  3042. * @param fontFamily 字体名称
  3043. * @param fontSize 字体大小
  3044. * @param bold 是否粗体
  3045. * @param italic 是否斜体
  3046. */
  3047. function measureText(text, fontFamily, fontSize, bold, italic) {
  3048. if (!context) {
  3049. createContext();
  3050. }
  3051. var font = "";
  3052. if (italic)
  3053. font += "italic ";
  3054. if (bold)
  3055. font += "bold ";
  3056. font += (fontSize || 12) + "px ";
  3057. font += (fontFamily || "Arial");
  3058. context.font = font;
  3059. return context.measureText(text).width;
  3060. }
  3061. /**
  3062. * @private
  3063. */
  3064. function createContext() {
  3065. context = egret.sys.canvasHitTestBuffer.context;
  3066. context.textAlign = "left";
  3067. context.textBaseline = "middle";
  3068. }
  3069. egret.sys.measureText = measureText;
  3070. })(web = egret.web || (egret.web = {}));
  3071. })(egret || (egret = {}));
  3072. //////////////////////////////////////////////////////////////////////////////////////
  3073. //
  3074. // Copyright (c) 2014-present, Egret Technology.
  3075. // All rights reserved.
  3076. // Redistribution and use in source and binary forms, with or without
  3077. // modification, are permitted provided that the following conditions are met:
  3078. //
  3079. // * Redistributions of source code must retain the above copyright
  3080. // notice, this list of conditions and the following disclaimer.
  3081. // * Redistributions in binary form must reproduce the above copyright
  3082. // notice, this list of conditions and the following disclaimer in the
  3083. // documentation and/or other materials provided with the distribution.
  3084. // * Neither the name of the Egret nor the
  3085. // names of its contributors may be used to endorse or promote products
  3086. // derived from this software without specific prior written permission.
  3087. //
  3088. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  3089. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  3090. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  3091. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  3092. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3093. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  3094. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  3095. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  3096. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  3097. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3098. //
  3099. //////////////////////////////////////////////////////////////////////////////////////
  3100. var egret;
  3101. (function (egret) {
  3102. var web;
  3103. (function (web) {
  3104. /**
  3105. * 创建一个canvas。
  3106. */
  3107. function createCanvas(width, height) {
  3108. var canvas = document.createElement("canvas");
  3109. if (!isNaN(width) && !isNaN(height)) {
  3110. canvas.width = width;
  3111. canvas.height = height;
  3112. }
  3113. var context = canvas.getContext("2d");
  3114. if (context["imageSmoothingEnabled"] === undefined) {
  3115. var keys = ["webkitImageSmoothingEnabled", "mozImageSmoothingEnabled", "msImageSmoothingEnabled"];
  3116. var key_1;
  3117. for (var i = keys.length - 1; i >= 0; i--) {
  3118. key_1 = keys[i];
  3119. if (context[key_1] !== void 0) {
  3120. break;
  3121. }
  3122. }
  3123. try {
  3124. Object.defineProperty(context, "imageSmoothingEnabled", {
  3125. get: function () {
  3126. return this[key_1];
  3127. },
  3128. set: function (value) {
  3129. this[key_1] = value;
  3130. }
  3131. });
  3132. }
  3133. catch (e) {
  3134. context["imageSmoothingEnabled"] = context[key_1];
  3135. }
  3136. }
  3137. return canvas;
  3138. }
  3139. var sharedCanvas;
  3140. /**
  3141. * @private
  3142. * Canvas2D渲染缓冲
  3143. */
  3144. var CanvasRenderBuffer = (function () {
  3145. function CanvasRenderBuffer(width, height, root) {
  3146. this.surface = createCanvas(width, height);
  3147. this.context = this.surface.getContext("2d");
  3148. }
  3149. Object.defineProperty(CanvasRenderBuffer.prototype, "width", {
  3150. /**
  3151. * 渲染缓冲的宽度,以像素为单位。
  3152. * @readOnly
  3153. */
  3154. get: function () {
  3155. return this.surface.width;
  3156. },
  3157. enumerable: true,
  3158. configurable: true
  3159. });
  3160. Object.defineProperty(CanvasRenderBuffer.prototype, "height", {
  3161. /**
  3162. * 渲染缓冲的高度,以像素为单位。
  3163. * @readOnly
  3164. */
  3165. get: function () {
  3166. return this.surface.height;
  3167. },
  3168. enumerable: true,
  3169. configurable: true
  3170. });
  3171. /**
  3172. * 改变渲染缓冲的大小并清空缓冲区
  3173. * @param width 改变后的宽
  3174. * @param height 改变后的高
  3175. * @param useMaxSize 若传入true,则将改变后的尺寸与已有尺寸对比,保留较大的尺寸。
  3176. */
  3177. CanvasRenderBuffer.prototype.resize = function (width, height, useMaxSize) {
  3178. var surface = this.surface;
  3179. if (useMaxSize) {
  3180. var change = false;
  3181. if (surface.width < width) {
  3182. surface.width = width;
  3183. change = true;
  3184. }
  3185. if (surface.height < height) {
  3186. surface.height = height;
  3187. change = true;
  3188. }
  3189. //尺寸没有变化时,将绘制属性重置
  3190. if (!change) {
  3191. this.context.globalCompositeOperation = "source-over";
  3192. this.context.setTransform(1, 0, 0, 1, 0, 0);
  3193. this.context.globalAlpha = 1;
  3194. }
  3195. }
  3196. else {
  3197. if (surface.width != width) {
  3198. surface.width = width;
  3199. }
  3200. if (surface.height != height) {
  3201. surface.height = height;
  3202. }
  3203. }
  3204. this.clear();
  3205. };
  3206. /**
  3207. * 改变渲染缓冲为指定大小,但保留原始图像数据
  3208. * @param width 改变后的宽
  3209. * @param height 改变后的高
  3210. * @param offsetX 原始图像数据在改变后缓冲区的绘制起始位置x
  3211. * @param offsetY 原始图像数据在改变后缓冲区的绘制起始位置y
  3212. */
  3213. CanvasRenderBuffer.prototype.resizeTo = function (width, height, offsetX, offsetY) {
  3214. if (!sharedCanvas) {
  3215. sharedCanvas = createCanvas();
  3216. }
  3217. var oldContext = this.context;
  3218. var oldSurface = this.surface;
  3219. var newSurface = sharedCanvas;
  3220. var newContext = newSurface.getContext("2d");
  3221. sharedCanvas = oldSurface;
  3222. this.context = newContext;
  3223. this.surface = newSurface;
  3224. newSurface.width = Math.max(width, 257);
  3225. newSurface.height = Math.max(height, 257);
  3226. newContext.setTransform(1, 0, 0, 1, 0, 0);
  3227. newContext.drawImage(oldSurface, offsetX, offsetY);
  3228. oldSurface.height = 1;
  3229. oldSurface.width = 1;
  3230. };
  3231. CanvasRenderBuffer.prototype.setDirtyRegionPolicy = function (state) {
  3232. };
  3233. /**
  3234. * 清空并设置裁切
  3235. * @param regions 矩形列表
  3236. * @param offsetX 矩形要加上的偏移量x
  3237. * @param offsetY 矩形要加上的偏移量y
  3238. */
  3239. CanvasRenderBuffer.prototype.beginClip = function (regions, offsetX, offsetY) {
  3240. offsetX = +offsetX || 0;
  3241. offsetY = +offsetY || 0;
  3242. var context = this.context;
  3243. context.save();
  3244. context.beginPath();
  3245. context.setTransform(1, 0, 0, 1, offsetX, offsetY);
  3246. var length = regions.length;
  3247. for (var i = 0; i < length; i++) {
  3248. var region = regions[i];
  3249. context.clearRect(region.minX, region.minY, region.width, region.height);
  3250. context.rect(region.minX, region.minY, region.width, region.height);
  3251. }
  3252. context.clip();
  3253. };
  3254. /**
  3255. * 取消上一次设置的clip。
  3256. */
  3257. CanvasRenderBuffer.prototype.endClip = function () {
  3258. this.context.restore();
  3259. };
  3260. /**
  3261. * 获取指定区域的像素
  3262. */
  3263. CanvasRenderBuffer.prototype.getPixels = function (x, y, width, height) {
  3264. if (width === void 0) { width = 1; }
  3265. if (height === void 0) { height = 1; }
  3266. return this.context.getImageData(x, y, width, height).data;
  3267. };
  3268. /**
  3269. * 转换成base64字符串,如果图片(或者包含的图片)跨域,则返回null
  3270. * @param type 转换的类型,如: "image/png","image/jpeg"
  3271. */
  3272. CanvasRenderBuffer.prototype.toDataURL = function (type, encoderOptions) {
  3273. return this.surface.toDataURL(type, encoderOptions);
  3274. };
  3275. /**
  3276. * 清空缓冲区数据
  3277. */
  3278. CanvasRenderBuffer.prototype.clear = function () {
  3279. this.context.setTransform(1, 0, 0, 1, 0, 0);
  3280. this.context.clearRect(0, 0, this.surface.width, this.surface.height);
  3281. };
  3282. /**
  3283. * 销毁绘制对象
  3284. */
  3285. CanvasRenderBuffer.prototype.destroy = function () {
  3286. this.surface.width = this.surface.height = 0;
  3287. };
  3288. return CanvasRenderBuffer;
  3289. }());
  3290. web.CanvasRenderBuffer = CanvasRenderBuffer;
  3291. __reflect(CanvasRenderBuffer.prototype, "egret.web.CanvasRenderBuffer", ["egret.sys.RenderBuffer"]);
  3292. })(web = egret.web || (egret.web = {}));
  3293. })(egret || (egret = {}));
  3294. //////////////////////////////////////////////////////////////////////////////////////
  3295. //
  3296. // Copyright (c) 2014-present, Egret Technology.
  3297. // All rights reserved.
  3298. // Redistribution and use in source and binary forms, with or without
  3299. // modification, are permitted provided this the following conditions are met:
  3300. //
  3301. // * Redistributions of source code must retain the above copyright
  3302. // notice, this list of conditions and the following disclaimer.
  3303. // * Redistributions in binary form must reproduce the above copyright
  3304. // notice, this list of conditions and the following disclaimer in the
  3305. // documentation and/or other materials provided with the distribution.
  3306. // * Neither the name of the Egret nor the
  3307. // names of its contributors may be used to endorse or promote products
  3308. // derived from this software without specific prior written permission.
  3309. //
  3310. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  3311. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  3312. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  3313. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  3314. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3315. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  3316. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  3317. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  3318. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  3319. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3320. //
  3321. //////////////////////////////////////////////////////////////////////////////////////
  3322. var egret;
  3323. (function (egret) {
  3324. var web;
  3325. (function (web) {
  3326. /**
  3327. * @private
  3328. */
  3329. var WebTouchHandler = (function (_super) {
  3330. __extends(WebTouchHandler, _super);
  3331. /**
  3332. * @private
  3333. */
  3334. function WebTouchHandler(stage, canvas) {
  3335. var _this = _super.call(this) || this;
  3336. /**
  3337. * @private
  3338. */
  3339. _this.onTouchBegin = function (event) {
  3340. var location = _this.getLocation(event);
  3341. _this.touch.onTouchBegin(location.x, location.y, event.identifier);
  3342. };
  3343. /**
  3344. * @private
  3345. */
  3346. _this.onTouchMove = function (event) {
  3347. var location = _this.getLocation(event);
  3348. _this.touch.onTouchMove(location.x, location.y, event.identifier);
  3349. };
  3350. /**
  3351. * @private
  3352. */
  3353. _this.onTouchEnd = function (event) {
  3354. var location = _this.getLocation(event);
  3355. _this.touch.onTouchEnd(location.x, location.y, event.identifier);
  3356. };
  3357. /**
  3358. * @private
  3359. */
  3360. _this.scaleX = 1;
  3361. /**
  3362. * @private
  3363. */
  3364. _this.scaleY = 1;
  3365. /**
  3366. * @private
  3367. */
  3368. _this.rotation = 0;
  3369. _this.canvas = canvas;
  3370. _this.touch = new egret.sys.TouchHandler(stage);
  3371. _this.addListeners();
  3372. return _this;
  3373. }
  3374. /**
  3375. * @private
  3376. * 添加事件监听
  3377. */
  3378. WebTouchHandler.prototype.addListeners = function () {
  3379. var _this = this;
  3380. if (window.navigator.msPointerEnabled) {
  3381. this.canvas.addEventListener("MSPointerDown", function (event) {
  3382. event.identifier = event.pointerId;
  3383. _this.onTouchBegin(event);
  3384. _this.prevent(event);
  3385. }, false);
  3386. this.canvas.addEventListener("MSPointerMove", function (event) {
  3387. event.identifier = event.pointerId;
  3388. _this.onTouchMove(event);
  3389. _this.prevent(event);
  3390. }, false);
  3391. this.canvas.addEventListener("MSPointerUp", function (event) {
  3392. event.identifier = event.pointerId;
  3393. _this.onTouchEnd(event);
  3394. _this.prevent(event);
  3395. }, false);
  3396. }
  3397. else {
  3398. if (!egret.Capabilities.$isMobile) {
  3399. this.addMouseListener();
  3400. }
  3401. this.addTouchListener();
  3402. }
  3403. };
  3404. /**
  3405. * @private
  3406. *
  3407. */
  3408. WebTouchHandler.prototype.addMouseListener = function () {
  3409. this.canvas.addEventListener("mousedown", this.onTouchBegin);
  3410. this.canvas.addEventListener("mousemove", this.onTouchMove);
  3411. this.canvas.addEventListener("mouseup", this.onTouchEnd);
  3412. };
  3413. /**
  3414. * @private
  3415. *
  3416. */
  3417. WebTouchHandler.prototype.addTouchListener = function () {
  3418. var _this = this;
  3419. this.canvas.addEventListener("touchstart", function (event) {
  3420. var l = event.changedTouches.length;
  3421. for (var i = 0; i < l; i++) {
  3422. _this.onTouchBegin(event.changedTouches[i]);
  3423. }
  3424. _this.prevent(event);
  3425. }, false);
  3426. this.canvas.addEventListener("touchmove", function (event) {
  3427. var l = event.changedTouches.length;
  3428. for (var i = 0; i < l; i++) {
  3429. _this.onTouchMove(event.changedTouches[i]);
  3430. }
  3431. _this.prevent(event);
  3432. }, false);
  3433. this.canvas.addEventListener("touchend", function (event) {
  3434. var l = event.changedTouches.length;
  3435. for (var i = 0; i < l; i++) {
  3436. _this.onTouchEnd(event.changedTouches[i]);
  3437. }
  3438. _this.prevent(event);
  3439. }, false);
  3440. this.canvas.addEventListener("touchcancel", function (event) {
  3441. var l = event.changedTouches.length;
  3442. for (var i = 0; i < l; i++) {
  3443. _this.onTouchEnd(event.changedTouches[i]);
  3444. }
  3445. _this.prevent(event);
  3446. }, false);
  3447. };
  3448. /**
  3449. * @private
  3450. */
  3451. WebTouchHandler.prototype.prevent = function (event) {
  3452. event.stopPropagation();
  3453. if (event["isScroll"] != true && !this.canvas['userTyping']) {
  3454. event.preventDefault();
  3455. }
  3456. };
  3457. /**
  3458. * @private
  3459. */
  3460. WebTouchHandler.prototype.getLocation = function (event) {
  3461. event.identifier = +event.identifier || 0;
  3462. var doc = document.documentElement;
  3463. var box = this.canvas.getBoundingClientRect();
  3464. var left = box.left + window.pageXOffset - doc.clientLeft;
  3465. var top = box.top + window.pageYOffset - doc.clientTop;
  3466. var x = event.pageX - left, newx = x;
  3467. var y = event.pageY - top, newy = y;
  3468. if (this.rotation == 90) {
  3469. newx = y;
  3470. newy = box.width - x;
  3471. }
  3472. else if (this.rotation == -90) {
  3473. newx = box.height - y;
  3474. newy = x;
  3475. }
  3476. newx = newx / this.scaleX;
  3477. newy = newy / this.scaleY;
  3478. return egret.$TempPoint.setTo(Math.round(newx), Math.round(newy));
  3479. };
  3480. /**
  3481. * @private
  3482. * 更新屏幕当前的缩放比例,用于计算准确的点击位置。
  3483. * @param scaleX 水平方向的缩放比例。
  3484. * @param scaleY 垂直方向的缩放比例。
  3485. */
  3486. WebTouchHandler.prototype.updateScaleMode = function (scaleX, scaleY, rotation) {
  3487. this.scaleX = scaleX;
  3488. this.scaleY = scaleY;
  3489. this.rotation = rotation;
  3490. };
  3491. /**
  3492. * @private
  3493. * 更新同时触摸点的数量
  3494. */
  3495. WebTouchHandler.prototype.$updateMaxTouches = function () {
  3496. this.touch.$initMaxTouches();
  3497. };
  3498. return WebTouchHandler;
  3499. }(egret.HashObject));
  3500. web.WebTouchHandler = WebTouchHandler;
  3501. __reflect(WebTouchHandler.prototype, "egret.web.WebTouchHandler");
  3502. })(web = egret.web || (egret.web = {}));
  3503. })(egret || (egret = {}));
  3504. //////////////////////////////////////////////////////////////////////////////////////
  3505. //
  3506. // Copyright (c) 2014-present, Egret Technology.
  3507. // All rights reserved.
  3508. // Redistribution and use in source and binary forms, with or without
  3509. // modification, are permitted provided that the following conditions are met:
  3510. //
  3511. // * Redistributions of source code must retain the above copyright
  3512. // notice, this list of conditions and the following disclaimer.
  3513. // * Redistributions in binary form must reproduce the above copyright
  3514. // notice, this list of conditions and the following disclaimer in the
  3515. // documentation and/or other materials provided with the distribution.
  3516. // * Neither the name of the Egret nor the
  3517. // names of its contributors may be used to endorse or promote products
  3518. // derived from this software without specific prior written permission.
  3519. //
  3520. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  3521. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  3522. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  3523. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  3524. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3525. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  3526. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  3527. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  3528. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  3529. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3530. //
  3531. //////////////////////////////////////////////////////////////////////////////////////
  3532. var egret;
  3533. (function (egret) {
  3534. var web;
  3535. (function (web) {
  3536. /**
  3537. * @private
  3538. */
  3539. var WebHideHandler = (function (_super) {
  3540. __extends(WebHideHandler, _super);
  3541. /**
  3542. * @private
  3543. */
  3544. function WebHideHandler(stage) {
  3545. var _this = _super.call(this) || this;
  3546. /**
  3547. * @private
  3548. */
  3549. _this.isActivate = true;
  3550. _this.stage = stage;
  3551. _this.registerListener();
  3552. return _this;
  3553. }
  3554. /**
  3555. * @private
  3556. *
  3557. */
  3558. WebHideHandler.prototype.registerListener = function () {
  3559. var self = this;
  3560. //失去焦点
  3561. var onBlurHandler = function () {
  3562. if (!self.isActivate) {
  3563. return;
  3564. }
  3565. self.isActivate = false;
  3566. self.stage.dispatchEvent(new egret.Event(egret.Event.DEACTIVATE));
  3567. };
  3568. //激活
  3569. var onFocusHandler = function () {
  3570. if (self.isActivate) {
  3571. return;
  3572. }
  3573. self.isActivate = true;
  3574. self.stage.dispatchEvent(new egret.Event(egret.Event.ACTIVATE));
  3575. };
  3576. var handleVisibilityChange = function () {
  3577. if (!document[hidden]) {
  3578. onFocusHandler();
  3579. }
  3580. else {
  3581. onBlurHandler();
  3582. }
  3583. };
  3584. window.addEventListener("focus", onFocusHandler, false);
  3585. window.addEventListener("blur", onBlurHandler, false);
  3586. var hidden, visibilityChange;
  3587. if (typeof document.hidden !== "undefined") {
  3588. hidden = "hidden";
  3589. visibilityChange = "visibilitychange";
  3590. }
  3591. else if (typeof document["mozHidden"] !== "undefined") {
  3592. hidden = "mozHidden";
  3593. visibilityChange = "mozvisibilitychange";
  3594. }
  3595. else if (typeof document["msHidden"] !== "undefined") {
  3596. hidden = "msHidden";
  3597. visibilityChange = "msvisibilitychange";
  3598. }
  3599. else if (typeof document["webkitHidden"] !== "undefined") {
  3600. hidden = "webkitHidden";
  3601. visibilityChange = "webkitvisibilitychange";
  3602. }
  3603. else if (typeof document["oHidden"] !== "undefined") {
  3604. hidden = "oHidden";
  3605. visibilityChange = "ovisibilitychange";
  3606. }
  3607. if ("onpageshow" in window && "onpagehide" in window) {
  3608. window.addEventListener("pageshow", onFocusHandler, false);
  3609. window.addEventListener("pagehide", onBlurHandler, false);
  3610. }
  3611. if (hidden && visibilityChange) {
  3612. document.addEventListener(visibilityChange, handleVisibilityChange, false);
  3613. }
  3614. var ua = navigator.userAgent;
  3615. var isWX = /micromessenger/gi.test(ua);
  3616. var isQQBrowser = /mqq/ig.test(ua);
  3617. var isQQ = /mobile.*qq/gi.test(ua);
  3618. if (isQQ || isWX) {
  3619. isQQBrowser = false;
  3620. }
  3621. if (isQQBrowser) {
  3622. var browser = window["browser"] || {};
  3623. browser.execWebFn = browser.execWebFn || {};
  3624. browser.execWebFn.postX5GamePlayerMessage = function (event) {
  3625. var eventType = event.type;
  3626. if (eventType == "app_enter_background") {
  3627. onBlurHandler();
  3628. }
  3629. else if (eventType == "app_enter_foreground") {
  3630. onFocusHandler();
  3631. }
  3632. };
  3633. window["browser"] = browser;
  3634. }
  3635. };
  3636. return WebHideHandler;
  3637. }(egret.HashObject));
  3638. web.WebHideHandler = WebHideHandler;
  3639. __reflect(WebHideHandler.prototype, "egret.web.WebHideHandler");
  3640. })(web = egret.web || (egret.web = {}));
  3641. })(egret || (egret = {}));
  3642. //////////////////////////////////////////////////////////////////////////////////////
  3643. //
  3644. // Copyright (c) 2014-present, Egret Technology.
  3645. // All rights reserved.
  3646. // Redistribution and use in source and binary forms, with or without
  3647. // modification, are permitted provided that the following conditions are met:
  3648. //
  3649. // * Redistributions of source code must retain the above copyright
  3650. // notice, this list of conditions and the following disclaimer.
  3651. // * Redistributions in binary form must reproduce the above copyright
  3652. // notice, this list of conditions and the following disclaimer in the
  3653. // documentation and/or other materials provided with the distribution.
  3654. // * Neither the name of the Egret nor the
  3655. // names of its contributors may be used to endorse or promote products
  3656. // derived from this software without specific prior written permission.
  3657. //
  3658. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  3659. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  3660. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  3661. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  3662. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3663. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  3664. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  3665. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  3666. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  3667. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3668. //
  3669. //////////////////////////////////////////////////////////////////////////////////////
  3670. var egret;
  3671. (function (egret) {
  3672. var web;
  3673. (function (web) {
  3674. /**
  3675. * @private
  3676. */
  3677. var AudioType = (function () {
  3678. function AudioType() {
  3679. }
  3680. return AudioType;
  3681. }());
  3682. /**
  3683. * @private
  3684. */
  3685. AudioType.QQ_AUDIO = 1;
  3686. /**
  3687. * @private
  3688. */
  3689. AudioType.WEB_AUDIO = 2;
  3690. /**
  3691. * @private
  3692. */
  3693. AudioType.HTML5_AUDIO = 3;
  3694. web.AudioType = AudioType;
  3695. __reflect(AudioType.prototype, "egret.web.AudioType");
  3696. /**
  3697. * @private
  3698. */
  3699. var SystemOSType = (function () {
  3700. function SystemOSType() {
  3701. }
  3702. return SystemOSType;
  3703. }());
  3704. /**
  3705. * @private
  3706. */
  3707. SystemOSType.WPHONE = 1;
  3708. /**
  3709. * @private
  3710. */
  3711. SystemOSType.IOS = 2;
  3712. /**
  3713. * @private
  3714. */
  3715. SystemOSType.ADNROID = 3;
  3716. web.SystemOSType = SystemOSType;
  3717. __reflect(SystemOSType.prototype, "egret.web.SystemOSType");
  3718. /**
  3719. * html5兼容性配置
  3720. * @private
  3721. */
  3722. var Html5Capatibility = (function (_super) {
  3723. __extends(Html5Capatibility, _super);
  3724. /**
  3725. * @private
  3726. */
  3727. function Html5Capatibility() {
  3728. return _super.call(this) || this;
  3729. }
  3730. /**
  3731. * @private
  3732. *
  3733. */
  3734. Html5Capatibility.$init = function () {
  3735. var ua = navigator.userAgent.toLowerCase();
  3736. Html5Capatibility.ua = ua;
  3737. egret.Capabilities.$isMobile = (ua.indexOf('mobile') != -1 || ua.indexOf('android') != -1);
  3738. Html5Capatibility._canUseBlob = false;
  3739. var checkAudioType;
  3740. var audioType = Html5Capatibility._audioType;
  3741. var canUseWebAudio = window["AudioContext"] || window["webkitAudioContext"] || window["mozAudioContext"];
  3742. if (audioType == 1 || audioType == 2 || audioType == 3) {
  3743. checkAudioType = false;
  3744. Html5Capatibility.setAudioType(audioType);
  3745. }
  3746. else {
  3747. checkAudioType = true;
  3748. Html5Capatibility.setAudioType(AudioType.HTML5_AUDIO);
  3749. }
  3750. if (ua.indexOf("windows phone") >= 0) {
  3751. Html5Capatibility._System_OS = SystemOSType.WPHONE;
  3752. egret.Capabilities.$os = "Windows Phone";
  3753. }
  3754. else if (ua.indexOf("android") >= 0) {
  3755. egret.Capabilities.$os = "Android";
  3756. Html5Capatibility._System_OS = SystemOSType.ADNROID;
  3757. if (checkAudioType) {
  3758. if (canUseWebAudio) {
  3759. Html5Capatibility.setAudioType(AudioType.WEB_AUDIO);
  3760. }
  3761. else {
  3762. Html5Capatibility.setAudioType(AudioType.HTML5_AUDIO);
  3763. }
  3764. }
  3765. if (window.hasOwnProperty("QZAppExternal") && ua.indexOf("qzone") >= 0) {
  3766. if (checkAudioType) {
  3767. Html5Capatibility.setAudioType(AudioType.QQ_AUDIO);
  3768. }
  3769. var bases = document.getElementsByTagName('base');
  3770. if (bases && bases.length > 0) {
  3771. Html5Capatibility._QQRootPath = bases[0]["baseURI"];
  3772. }
  3773. else {
  3774. var endIdx = window.location.href.indexOf("?");
  3775. if (endIdx == -1) {
  3776. endIdx = window.location.href.length;
  3777. }
  3778. var url = window.location.href.substring(0, endIdx);
  3779. url = url.substring(0, url.lastIndexOf("/"));
  3780. Html5Capatibility._QQRootPath = url + "/";
  3781. }
  3782. }
  3783. }
  3784. else if (ua.indexOf("iphone") >= 0 || ua.indexOf("ipad") >= 0 || ua.indexOf("ipod") >= 0) {
  3785. egret.Capabilities.$os = "iOS";
  3786. Html5Capatibility._System_OS = SystemOSType.IOS;
  3787. if (Html5Capatibility.getIOSVersion() >= 7) {
  3788. Html5Capatibility._canUseBlob = true;
  3789. if (checkAudioType) {
  3790. Html5Capatibility.setAudioType(AudioType.WEB_AUDIO);
  3791. }
  3792. }
  3793. }
  3794. else {
  3795. if (ua.indexOf("windows nt") != -1) {
  3796. egret.Capabilities.$os = "Windows PC";
  3797. }
  3798. else if (ua.indexOf("mac os") != -1) {
  3799. egret.Capabilities.$os = "Mac OS";
  3800. }
  3801. }
  3802. var winURL = window["URL"] || window["webkitURL"];
  3803. if (!winURL) {
  3804. Html5Capatibility._canUseBlob = false;
  3805. }
  3806. egret.Sound = Html5Capatibility._AudioClass;
  3807. };
  3808. Html5Capatibility.setAudioType = function (type) {
  3809. Html5Capatibility._audioType = type;
  3810. switch (type) {
  3811. case AudioType.QQ_AUDIO:
  3812. Html5Capatibility._AudioClass = egret.web.QQSound;
  3813. break;
  3814. case AudioType.WEB_AUDIO:
  3815. Html5Capatibility._AudioClass = egret.web.WebAudioSound;
  3816. break;
  3817. case AudioType.HTML5_AUDIO:
  3818. Html5Capatibility._AudioClass = egret.web.HtmlSound;
  3819. break;
  3820. }
  3821. };
  3822. /**
  3823. * @private
  3824. * 获取ios版本
  3825. * @returns {string}
  3826. */
  3827. Html5Capatibility.getIOSVersion = function () {
  3828. var value = Html5Capatibility.ua.toLowerCase().match(/cpu [^\d]*\d.*like mac os x/)[0];
  3829. return parseInt(value.match(/\d+(_\d)*/)[0]) || 0;
  3830. };
  3831. /**
  3832. * @private
  3833. *
  3834. */
  3835. Html5Capatibility.checkHtml5Support = function () {
  3836. var language = (navigator.language || navigator["browserLanguage"]).toLowerCase();
  3837. var strings = language.split("-");
  3838. if (strings.length > 1) {
  3839. strings[1] = strings[1].toUpperCase();
  3840. }
  3841. egret.Capabilities.$language = strings.join("-");
  3842. };
  3843. return Html5Capatibility;
  3844. }(egret.HashObject));
  3845. //当前浏览器版本是否支持blob
  3846. Html5Capatibility._canUseBlob = false;
  3847. //当前浏览器版本是否支持webaudio
  3848. Html5Capatibility._audioType = 0;
  3849. /**
  3850. * @private
  3851. */
  3852. Html5Capatibility._QQRootPath = "";
  3853. /**
  3854. * @private
  3855. */
  3856. Html5Capatibility._System_OS = 0;
  3857. /**
  3858. * @private
  3859. */
  3860. Html5Capatibility.ua = "";
  3861. web.Html5Capatibility = Html5Capatibility;
  3862. __reflect(Html5Capatibility.prototype, "egret.web.Html5Capatibility");
  3863. /**
  3864. * @private
  3865. */
  3866. var currentPrefix = null;
  3867. /**
  3868. * @private
  3869. */
  3870. function getPrefixStyleName(name, element) {
  3871. var header = "";
  3872. if (element != null) {
  3873. header = getPrefix(name, element);
  3874. }
  3875. else {
  3876. if (currentPrefix == null) {
  3877. var tempStyle = document.createElement('div').style;
  3878. currentPrefix = getPrefix("transform", tempStyle);
  3879. }
  3880. header = currentPrefix;
  3881. }
  3882. if (header == "") {
  3883. return name;
  3884. }
  3885. return header + name.charAt(0).toUpperCase() + name.substring(1, name.length);
  3886. }
  3887. web.getPrefixStyleName = getPrefixStyleName;
  3888. /**
  3889. * @private
  3890. */
  3891. function getPrefix(name, element) {
  3892. if (name in element) {
  3893. return "";
  3894. }
  3895. name = name.charAt(0).toUpperCase() + name.substring(1, name.length);
  3896. var transArr = ["webkit", "ms", "Moz", "O"];
  3897. for (var i = 0; i < transArr.length; i++) {
  3898. var tempStyle = transArr[i] + name;
  3899. if (tempStyle in element) {
  3900. return transArr[i];
  3901. }
  3902. }
  3903. return "";
  3904. }
  3905. web.getPrefix = getPrefix;
  3906. })(web = egret.web || (egret.web = {}));
  3907. })(egret || (egret = {}));
  3908. //////////////////////////////////////////////////////////////////////////////////////
  3909. //
  3910. // Copyright (c) 2014-present, Egret Technology.
  3911. // All rights reserved.
  3912. // Redistribution and use in source and binary forms, with or without
  3913. // modification, are permitted provided that the following conditions are met:
  3914. //
  3915. // * Redistributions of source code must retain the above copyright
  3916. // notice, this list of conditions and the following disclaimer.
  3917. // * Redistributions in binary form must reproduce the above copyright
  3918. // notice, this list of conditions and the following disclaimer in the
  3919. // documentation and/or other materials provided with the distribution.
  3920. // * Neither the name of the Egret nor the
  3921. // names of its contributors may be used to endorse or promote products
  3922. // derived from this software without specific prior written permission.
  3923. //
  3924. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  3925. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  3926. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  3927. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  3928. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  3929. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  3930. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  3931. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  3932. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  3933. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3934. //
  3935. //////////////////////////////////////////////////////////////////////////////////////
  3936. var egret;
  3937. (function (egret) {
  3938. var web;
  3939. (function (web) {
  3940. /**
  3941. * @private
  3942. * 刷新所有Egret播放器的显示区域尺寸。仅当使用外部JavaScript代码动态修改了Egret容器大小时,需要手动调用此方法刷新显示区域。
  3943. * 当网页尺寸发生改变时此方法会自动被调用。
  3944. */
  3945. function updateAllScreens() {
  3946. if (!isRunning) {
  3947. return;
  3948. }
  3949. var containerList = document.querySelectorAll(".egret-player");
  3950. var length = containerList.length;
  3951. for (var i = 0; i < length; i++) {
  3952. var container = containerList[i];
  3953. var player = container["egret-player"];
  3954. player.updateScreenSize();
  3955. }
  3956. }
  3957. var isRunning = false;
  3958. /**
  3959. * @private
  3960. * 网页加载完成,实例化页面中定义的Egret标签
  3961. */
  3962. function runEgret(options) {
  3963. if (isRunning) {
  3964. return;
  3965. }
  3966. isRunning = true;
  3967. if (!options) {
  3968. options = {};
  3969. }
  3970. web.Html5Capatibility._audioType = options.audioType;
  3971. web.Html5Capatibility.$init();
  3972. // WebGL上下文参数自定义
  3973. if (options.renderMode == "webgl") {
  3974. // WebGL抗锯齿默认关闭,提升PC及某些平台性能
  3975. var antialias = options.antialias;
  3976. web.WebGLRenderContext.antialias = !!antialias;
  3977. }
  3978. egret.sys.CanvasRenderBuffer = web.CanvasRenderBuffer;
  3979. setRenderMode(options.renderMode);
  3980. var ticker = egret.sys.$ticker;
  3981. startTicker(ticker);
  3982. if (options.screenAdapter) {
  3983. egret.sys.screenAdapter = options.screenAdapter;
  3984. }
  3985. else if (!egret.sys.screenAdapter) {
  3986. egret.sys.screenAdapter = new egret.sys.DefaultScreenAdapter();
  3987. }
  3988. var list = document.querySelectorAll(".egret-player");
  3989. var length = list.length;
  3990. for (var i = 0; i < length; i++) {
  3991. var container = list[i];
  3992. var player = new web.WebPlayer(container, options);
  3993. container["egret-player"] = player;
  3994. //webgl模式关闭脏矩形
  3995. if (egret.Capabilities.$renderMode == "webgl") {
  3996. player.stage.dirtyRegionPolicy = egret.DirtyRegionPolicy.OFF;
  3997. }
  3998. }
  3999. if (egret.Capabilities.$renderMode == "webgl") {
  4000. egret.sys.DisplayList.prototype.setDirtyRegionPolicy = function () { };
  4001. }
  4002. }
  4003. /**
  4004. * 设置渲染模式。"auto","webgl","canvas"
  4005. * @param renderMode
  4006. */
  4007. function setRenderMode(renderMode) {
  4008. if (renderMode == "webgl" && egret.WebGLUtils.checkCanUseWebGL()) {
  4009. egret.sys.RenderBuffer = web.WebGLRenderBuffer;
  4010. egret.sys.systemRenderer = new web.WebGLRenderer();
  4011. egret.sys.canvasRenderer = new egret.CanvasRenderer();
  4012. egret.sys.customHitTestBuffer = new web.WebGLRenderBuffer(3, 3);
  4013. egret.sys.canvasHitTestBuffer = new web.CanvasRenderBuffer(3, 3);
  4014. egret.Capabilities.$renderMode = "webgl";
  4015. }
  4016. else {
  4017. egret.sys.RenderBuffer = web.CanvasRenderBuffer;
  4018. egret.sys.systemRenderer = new egret.CanvasRenderer();
  4019. egret.sys.canvasRenderer = egret.sys.systemRenderer;
  4020. egret.sys.customHitTestBuffer = new web.CanvasRenderBuffer(3, 3);
  4021. egret.sys.canvasHitTestBuffer = egret.sys.customHitTestBuffer;
  4022. egret.Capabilities.$renderMode = "canvas";
  4023. }
  4024. }
  4025. /**
  4026. * @private
  4027. * 启动心跳计时器。
  4028. */
  4029. function startTicker(ticker) {
  4030. var requestAnimationFrame = window["requestAnimationFrame"] ||
  4031. window["webkitRequestAnimationFrame"] ||
  4032. window["mozRequestAnimationFrame"] ||
  4033. window["oRequestAnimationFrame"] ||
  4034. window["msRequestAnimationFrame"];
  4035. if (!requestAnimationFrame) {
  4036. requestAnimationFrame = function (callback) {
  4037. return window.setTimeout(callback, 1000 / 60);
  4038. };
  4039. }
  4040. requestAnimationFrame.call(window, onTick);
  4041. function onTick() {
  4042. ticker.update();
  4043. requestAnimationFrame.call(window, onTick);
  4044. }
  4045. }
  4046. //覆盖原生的isNaN()方法实现,在不同浏览器上有2~10倍性能提升。
  4047. window["isNaN"] = function (value) {
  4048. value = +value;
  4049. return value !== value;
  4050. };
  4051. egret.runEgret = runEgret;
  4052. egret.updateAllScreens = updateAllScreens;
  4053. var resizeTimer = NaN;
  4054. function doResize() {
  4055. resizeTimer = NaN;
  4056. egret.updateAllScreens();
  4057. }
  4058. window.addEventListener("resize", function () {
  4059. if (isNaN(resizeTimer)) {
  4060. resizeTimer = window.setTimeout(doResize, 300);
  4061. }
  4062. });
  4063. })(web = egret.web || (egret.web = {}));
  4064. })(egret || (egret = {}));
  4065. if (true) {
  4066. var language = navigator.language || navigator["browserLanguage"] || "en_US";
  4067. language = language.replace("-", "_");
  4068. if (language in egret.$locale_strings)
  4069. egret.$language = language;
  4070. }
  4071. //////////////////////////////////////////////////////////////////////////////////////
  4072. //
  4073. // Copyright (c) 2014-present, Egret Technology.
  4074. // All rights reserved.
  4075. // Redistribution and use in source and binary forms, with or without
  4076. // modification, are permitted provided that the following conditions are met:
  4077. //
  4078. // * Redistributions of source code must retain the above copyright
  4079. // notice, this list of conditions and the following disclaimer.
  4080. // * Redistributions in binary form must reproduce the above copyright
  4081. // notice, this list of conditions and the following disclaimer in the
  4082. // documentation and/or other materials provided with the distribution.
  4083. // * Neither the name of the Egret nor the
  4084. // names of its contributors may be used to endorse or promote products
  4085. // derived from this software without specific prior written permission.
  4086. //
  4087. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  4088. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  4089. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  4090. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  4091. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4092. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  4093. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  4094. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  4095. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  4096. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4097. //
  4098. //////////////////////////////////////////////////////////////////////////////////////
  4099. var egret;
  4100. (function (egret) {
  4101. var web;
  4102. (function (web) {
  4103. /**
  4104. * @private
  4105. */
  4106. var WebCapability = (function () {
  4107. function WebCapability() {
  4108. }
  4109. /**
  4110. * @private
  4111. * 检测系统属性
  4112. */
  4113. WebCapability.detect = function () {
  4114. var capabilities = egret.Capabilities;
  4115. var ua = navigator.userAgent.toLowerCase();
  4116. capabilities.$isMobile = (ua.indexOf('mobile') != -1 || ua.indexOf('android') != -1);
  4117. if (capabilities.$isMobile) {
  4118. if (ua.indexOf("windows") < 0 && (ua.indexOf("iphone") != -1 || ua.indexOf("ipad") != -1 || ua.indexOf("ipod") != -1)) {
  4119. capabilities.$os = "iOS";
  4120. }
  4121. else if (ua.indexOf("android") != -1 && ua.indexOf("linux") != -1) {
  4122. capabilities.$os = "Android";
  4123. }
  4124. else if (ua.indexOf("windows") != -1) {
  4125. capabilities.$os = "Windows Phone";
  4126. }
  4127. }
  4128. else {
  4129. if (ua.indexOf("windows nt") != -1) {
  4130. capabilities.$os = "Windows PC";
  4131. }
  4132. else if (ua.indexOf("mac os") != -1) {
  4133. capabilities.$os = "Mac OS";
  4134. }
  4135. }
  4136. var language = (navigator.language || navigator["browserLanguage"]).toLowerCase();
  4137. var strings = language.split("-");
  4138. if (strings.length > 1) {
  4139. strings[1] = strings[1].toUpperCase();
  4140. }
  4141. capabilities.$language = strings.join("-");
  4142. WebCapability.injectUIntFixOnIE9();
  4143. };
  4144. WebCapability.injectUIntFixOnIE9 = function () {
  4145. if (/msie 9.0/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) {
  4146. var IEBinaryToArray_ByteStr_Script = "<!-- IEBinaryToArray_ByteStr -->\r\n" +
  4147. "<script type='text/vbscript' language='VBScript'>\r\n" +
  4148. "Function IEBinaryToArray_ByteStr(Binary)\r\n" +
  4149. " IEBinaryToArray_ByteStr = CStr(Binary)\r\n" +
  4150. "End Function\r\n" +
  4151. "Function IEBinaryToArray_ByteStr_Last(Binary)\r\n" +
  4152. " Dim lastIndex\r\n" +
  4153. " lastIndex = LenB(Binary)\r\n" +
  4154. " if lastIndex mod 2 Then\r\n" +
  4155. " IEBinaryToArray_ByteStr_Last = Chr( AscB( MidB( Binary, lastIndex, 1 ) ) )\r\n" +
  4156. " Else\r\n" +
  4157. " IEBinaryToArray_ByteStr_Last = " + '""' + "\r\n" +
  4158. " End If\r\n" +
  4159. "End Function\r\n" + "<\/script>\r\n" +
  4160. "<!-- convertResponseBodyToText -->\r\n" +
  4161. "<script>\r\n" +
  4162. "let convertResponseBodyToText = function (binary) {\r\n" +
  4163. " let byteMapping = {};\r\n" +
  4164. " for ( let i = 0; i < 256; i++ ) {\r\n" +
  4165. " for ( let j = 0; j < 256; j++ ) {\r\n" +
  4166. " byteMapping[ String.fromCharCode( i + j * 256 ) ] =\r\n" +
  4167. " String.fromCharCode(i) + String.fromCharCode(j);\r\n" +
  4168. " }\r\n" +
  4169. " }\r\n" +
  4170. " let rawBytes = IEBinaryToArray_ByteStr(binary);\r\n" +
  4171. " let lastChr = IEBinaryToArray_ByteStr_Last(binary);\r\n" +
  4172. " return rawBytes.replace(/[\\s\\S]/g," +
  4173. " function( match ) { return byteMapping[match]; }) + lastChr;\r\n" +
  4174. "};\r\n" +
  4175. "<\/script>\r\n";
  4176. document.write(IEBinaryToArray_ByteStr_Script);
  4177. }
  4178. };
  4179. return WebCapability;
  4180. }());
  4181. web.WebCapability = WebCapability;
  4182. __reflect(WebCapability.prototype, "egret.web.WebCapability");
  4183. WebCapability.detect();
  4184. })(web = egret.web || (egret.web = {}));
  4185. })(egret || (egret = {}));
  4186. //////////////////////////////////////////////////////////////////////////////////////
  4187. //
  4188. // Copyright (c) 2014-present, Egret Technology.
  4189. // All rights reserved.
  4190. // Redistribution and use in source and binary forms, with or without
  4191. // modification, are permitted provided that the following conditions are met:
  4192. //
  4193. // * Redistributions of source code must retain the above copyright
  4194. // notice, this list of conditions and the following disclaimer.
  4195. // * Redistributions in binary form must reproduce the above copyright
  4196. // notice, this list of conditions and the following disclaimer in the
  4197. // documentation and/or other materials provided with the distribution.
  4198. // * Neither the name of the Egret nor the
  4199. // names of its contributors may be used to endorse or promote products
  4200. // derived from this software without specific prior written permission.
  4201. //
  4202. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  4203. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  4204. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  4205. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  4206. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4207. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  4208. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  4209. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  4210. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  4211. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4212. //
  4213. //////////////////////////////////////////////////////////////////////////////////////
  4214. var egret;
  4215. (function (egret) {
  4216. var web;
  4217. (function (web) {
  4218. var blendModes = ["source-over", "lighter", "destination-out"];
  4219. var defaultCompositeOp = "source-over";
  4220. var BLACK_COLOR = "#000000";
  4221. var CAPS_STYLES = { none: 'butt', square: 'square', round: 'round' };
  4222. var renderBufferPool = []; //渲染缓冲区对象池
  4223. /**
  4224. * @private
  4225. * WebGL渲染器
  4226. */
  4227. var WebGLRenderer = (function () {
  4228. function WebGLRenderer() {
  4229. this.nestLevel = 0; //渲染的嵌套层次,0表示在调用堆栈的最外层。
  4230. }
  4231. /**
  4232. * 渲染一个显示对象
  4233. * @param displayObject 要渲染的显示对象
  4234. * @param buffer 渲染缓冲
  4235. * @param matrix 要对显示对象整体叠加的变换矩阵
  4236. * @param dirtyList 脏矩形列表
  4237. * @param forRenderTexture 绘制目标是RenderTexture的标志
  4238. * @returns drawCall触发绘制的次数
  4239. */
  4240. WebGLRenderer.prototype.render = function (displayObject, buffer, matrix, dirtyList, forRenderTexture) {
  4241. this.nestLevel++;
  4242. var webglBuffer = buffer;
  4243. var webglBufferContext = webglBuffer.context;
  4244. var root = forRenderTexture ? displayObject : null;
  4245. webglBufferContext.pushBuffer(webglBuffer);
  4246. //绘制显示对象
  4247. this.drawDisplayObject(displayObject, webglBuffer, dirtyList, matrix, null, null, root);
  4248. webglBufferContext.$drawWebGL();
  4249. var drawCall = webglBuffer.$drawCalls;
  4250. webglBuffer.onRenderFinish();
  4251. webglBufferContext.popBuffer();
  4252. this.nestLevel--;
  4253. if (this.nestLevel === 0) {
  4254. //最大缓存6个渲染缓冲
  4255. if (renderBufferPool.length > 6) {
  4256. renderBufferPool.length = 6;
  4257. }
  4258. var length_1 = renderBufferPool.length;
  4259. for (var i = 0; i < length_1; i++) {
  4260. renderBufferPool[i].resize(0, 0);
  4261. }
  4262. }
  4263. return drawCall;
  4264. };
  4265. /**
  4266. * @private
  4267. * 绘制一个显示对象
  4268. */
  4269. WebGLRenderer.prototype.drawDisplayObject = function (displayObject, buffer, dirtyList, matrix, displayList, clipRegion, root) {
  4270. var drawCalls = 0;
  4271. var node;
  4272. var filterPushed = false;
  4273. if (displayList && !root) {
  4274. if (displayList.isDirty) {
  4275. drawCalls += displayList.drawToSurface();
  4276. }
  4277. node = displayList.$renderNode;
  4278. }
  4279. else {
  4280. node = displayObject.$getRenderNode();
  4281. }
  4282. if (node) {
  4283. if (dirtyList) {
  4284. var renderRegion = node.renderRegion;
  4285. if (clipRegion && !clipRegion.intersects(renderRegion)) {
  4286. node.needRedraw = false;
  4287. }
  4288. else if (!node.needRedraw) {
  4289. var l = dirtyList.length;
  4290. for (var j = 0; j < l; j++) {
  4291. if (renderRegion.intersects(dirtyList[j])) {
  4292. node.needRedraw = true;
  4293. break;
  4294. }
  4295. }
  4296. }
  4297. }
  4298. else {
  4299. node.needRedraw = true;
  4300. }
  4301. if (node.needRedraw) {
  4302. drawCalls++;
  4303. var renderAlpha = void 0;
  4304. var m = void 0;
  4305. if (root) {
  4306. renderAlpha = displayObject.$getConcatenatedAlphaAt(root, displayObject.$getConcatenatedAlpha());
  4307. m = egret.Matrix.create().copyFrom(displayObject.$getConcatenatedMatrix());
  4308. displayObject.$getConcatenatedMatrixAt(root, m);
  4309. matrix.$preMultiplyInto(m, m);
  4310. buffer.setTransform(m.a, m.b, m.c, m.d, m.tx, m.ty);
  4311. egret.Matrix.release(m);
  4312. }
  4313. else {
  4314. renderAlpha = node.renderAlpha;
  4315. m = node.renderMatrix;
  4316. buffer.setTransform(m.a, m.b, m.c, m.d, m.tx + matrix.tx, m.ty + matrix.ty);
  4317. }
  4318. buffer.globalAlpha = renderAlpha;
  4319. this.renderNode(node, buffer);
  4320. node.needRedraw = false;
  4321. }
  4322. }
  4323. if (displayList && !root) {
  4324. return drawCalls;
  4325. }
  4326. var children = displayObject.$children;
  4327. if (children) {
  4328. var length_2 = children.length;
  4329. for (var i = 0; i < length_2; i++) {
  4330. var child = children[i];
  4331. if (!child.$visible || child.$alpha <= 0 || child.$maskedObject) {
  4332. continue;
  4333. }
  4334. var filters = child.$getFilters();
  4335. if (filters && filters.length > 0) {
  4336. drawCalls += this.drawWithFilter(child, buffer, dirtyList, matrix, clipRegion, root);
  4337. }
  4338. else if ((child.$blendMode !== 0 ||
  4339. (child.$mask && (child.$mask.$parentDisplayList || root)))) {
  4340. drawCalls += this.drawWithClip(child, buffer, dirtyList, matrix, clipRegion, root);
  4341. }
  4342. else if (child.$scrollRect || child.$maskRect) {
  4343. drawCalls += this.drawWithScrollRect(child, buffer, dirtyList, matrix, clipRegion, root);
  4344. }
  4345. else {
  4346. if (child["isFPS"]) {
  4347. buffer.context.$drawWebGL();
  4348. buffer.$computeDrawCall = false;
  4349. this.drawDisplayObject(child, buffer, dirtyList, matrix, child.$displayList, clipRegion, root);
  4350. buffer.context.$drawWebGL();
  4351. buffer.$computeDrawCall = true;
  4352. }
  4353. else {
  4354. drawCalls += this.drawDisplayObject(child, buffer, dirtyList, matrix, child.$displayList, clipRegion, root);
  4355. }
  4356. }
  4357. }
  4358. }
  4359. return drawCalls;
  4360. };
  4361. /**
  4362. * @private
  4363. */
  4364. WebGLRenderer.prototype.drawWithFilter = function (displayObject, buffer, dirtyList, matrix, clipRegion, root) {
  4365. var drawCalls = 0;
  4366. var filters = displayObject.$getFilters();
  4367. var hasBlendMode = (displayObject.$blendMode !== 0);
  4368. var compositeOp;
  4369. if (hasBlendMode) {
  4370. compositeOp = blendModes[displayObject.$blendMode];
  4371. if (!compositeOp) {
  4372. compositeOp = defaultCompositeOp;
  4373. }
  4374. }
  4375. if (filters.length == 1 && filters[0].type == "colorTransform" && !displayObject.$children) {
  4376. if (hasBlendMode) {
  4377. buffer.context.setGlobalCompositeOperation(compositeOp);
  4378. }
  4379. buffer.context.$filter = filters[0];
  4380. if ((displayObject.$mask && (displayObject.$mask.$parentDisplayList || root))) {
  4381. drawCalls += this.drawWithClip(displayObject, buffer, dirtyList, matrix, clipRegion, root);
  4382. }
  4383. else if (displayObject.$scrollRect || displayObject.$maskRect) {
  4384. drawCalls += this.drawWithScrollRect(displayObject, buffer, dirtyList, matrix, clipRegion, root);
  4385. }
  4386. else {
  4387. drawCalls += this.drawDisplayObject(displayObject, buffer, dirtyList, matrix, displayObject.$displayList, clipRegion, root);
  4388. }
  4389. buffer.context.$filter = null;
  4390. if (hasBlendMode) {
  4391. buffer.context.setGlobalCompositeOperation(defaultCompositeOp);
  4392. }
  4393. return drawCalls;
  4394. }
  4395. // 获取显示对象的链接矩阵
  4396. var displayMatrix = egret.Matrix.create();
  4397. displayMatrix.copyFrom(displayObject.$getConcatenatedMatrix());
  4398. if (root) {
  4399. displayObject.$getConcatenatedMatrixAt(root, displayMatrix);
  4400. }
  4401. // 获取显示对象的矩形区域
  4402. var region;
  4403. region = egret.sys.Region.create();
  4404. var bounds = displayObject.$getOriginalBounds();
  4405. region.updateRegion(bounds, displayMatrix);
  4406. // 为显示对象创建一个新的buffer
  4407. // todo 这里应该计算 region.x region.y
  4408. var displayBuffer = this.createRenderBuffer(region.width, region.height);
  4409. displayBuffer.context.pushBuffer(displayBuffer);
  4410. displayBuffer.setTransform(1, 0, 0, 1, -region.minX, -region.minY);
  4411. var offsetM = egret.Matrix.create().setTo(1, 0, 0, 1, -region.minX, -region.minY);
  4412. //todo 可以优化减少draw次数
  4413. if ((displayObject.$mask && (displayObject.$mask.$parentDisplayList || root))) {
  4414. drawCalls += this.drawWithClip(displayObject, displayBuffer, dirtyList, offsetM, region, root);
  4415. }
  4416. else if (displayObject.$scrollRect || displayObject.$maskRect) {
  4417. drawCalls += this.drawWithScrollRect(displayObject, displayBuffer, dirtyList, offsetM, region, root);
  4418. }
  4419. else {
  4420. drawCalls += this.drawDisplayObject(displayObject, displayBuffer, dirtyList, offsetM, displayObject.$displayList, region, root);
  4421. }
  4422. egret.Matrix.release(offsetM);
  4423. displayBuffer.context.popBuffer();
  4424. //绘制结果到屏幕
  4425. if (drawCalls > 0) {
  4426. if (hasBlendMode) {
  4427. buffer.context.setGlobalCompositeOperation(compositeOp);
  4428. }
  4429. drawCalls++;
  4430. buffer.globalAlpha = 1;
  4431. buffer.setTransform(1, 0, 0, 1, region.minX + matrix.tx, region.minY + matrix.ty);
  4432. // 绘制结果的时候,应用滤镜
  4433. buffer.context.drawTargetWidthFilters(filters, displayBuffer);
  4434. if (hasBlendMode) {
  4435. buffer.context.setGlobalCompositeOperation(defaultCompositeOp);
  4436. }
  4437. }
  4438. renderBufferPool.push(displayBuffer);
  4439. egret.sys.Region.release(region);
  4440. egret.Matrix.release(displayMatrix);
  4441. return drawCalls;
  4442. };
  4443. /**
  4444. * @private
  4445. */
  4446. WebGLRenderer.prototype.drawWithClip = function (displayObject, buffer, dirtyList, matrix, clipRegion, root) {
  4447. var drawCalls = 0;
  4448. var hasBlendMode = (displayObject.$blendMode !== 0);
  4449. var compositeOp;
  4450. if (hasBlendMode) {
  4451. compositeOp = blendModes[displayObject.$blendMode];
  4452. if (!compositeOp) {
  4453. compositeOp = defaultCompositeOp;
  4454. }
  4455. }
  4456. var scrollRect = displayObject.$scrollRect ? displayObject.$scrollRect : displayObject.$maskRect;
  4457. var mask = displayObject.$mask;
  4458. if (mask) {
  4459. var maskRenderNode = mask.$getRenderNode();
  4460. if (maskRenderNode) {
  4461. var maskRenderMatrix = maskRenderNode.renderMatrix;
  4462. //遮罩scaleX或scaleY为0,放弃绘制
  4463. if ((maskRenderMatrix.a == 0 && maskRenderMatrix.b == 0) || (maskRenderMatrix.c == 0 && maskRenderMatrix.d == 0)) {
  4464. return drawCalls;
  4465. }
  4466. }
  4467. }
  4468. //if (mask && !mask.$parentDisplayList) {
  4469. // mask = null; //如果遮罩不在显示列表中,放弃绘制遮罩。
  4470. //}
  4471. //计算scrollRect和mask的clip区域是否需要绘制,不需要就直接返回,跳过所有子项的遍历。
  4472. var maskRegion;
  4473. var displayMatrix = egret.Matrix.create();
  4474. displayMatrix.copyFrom(displayObject.$getConcatenatedMatrix());
  4475. if (displayObject.$parentDisplayList) {
  4476. var displayRoot = displayObject.$parentDisplayList.root;
  4477. if (displayRoot !== displayObject.$stage) {
  4478. displayObject.$getConcatenatedMatrixAt(displayRoot, displayMatrix);
  4479. }
  4480. }
  4481. var bounds;
  4482. if (mask) {
  4483. bounds = mask.$getOriginalBounds();
  4484. maskRegion = egret.sys.Region.create();
  4485. var m = egret.Matrix.create();
  4486. m.copyFrom(mask.$getConcatenatedMatrix());
  4487. maskRegion.updateRegion(bounds, m);
  4488. egret.Matrix.release(m);
  4489. }
  4490. var region;
  4491. if (scrollRect) {
  4492. region = egret.sys.Region.create();
  4493. region.updateRegion(scrollRect, displayMatrix);
  4494. }
  4495. if (region && maskRegion) {
  4496. region.intersect(maskRegion);
  4497. egret.sys.Region.release(maskRegion);
  4498. }
  4499. else if (!region && maskRegion) {
  4500. region = maskRegion;
  4501. }
  4502. if (region) {
  4503. if (region.isEmpty() || (clipRegion && !clipRegion.intersects(region))) {
  4504. egret.sys.Region.release(region);
  4505. egret.Matrix.release(displayMatrix);
  4506. return drawCalls;
  4507. }
  4508. }
  4509. else {
  4510. region = egret.sys.Region.create();
  4511. bounds = displayObject.$getOriginalBounds();
  4512. region.updateRegion(bounds, displayMatrix);
  4513. }
  4514. var found = false;
  4515. if (!dirtyList) {
  4516. found = true;
  4517. }
  4518. else {
  4519. var l = dirtyList.length;
  4520. for (var j = 0; j < l; j++) {
  4521. if (region.intersects(dirtyList[j])) {
  4522. found = true;
  4523. break;
  4524. }
  4525. }
  4526. }
  4527. if (!found) {
  4528. egret.sys.Region.release(region);
  4529. egret.Matrix.release(displayMatrix);
  4530. return drawCalls;
  4531. }
  4532. //没有遮罩,同时显示对象没有子项
  4533. if (!mask && (!displayObject.$children || displayObject.$children.length == 0)) {
  4534. if (scrollRect) {
  4535. var m = displayMatrix;
  4536. buffer.setTransform(m.a, m.b, m.c, m.d, m.tx - region.minX, m.ty - region.minY);
  4537. buffer.context.pushMask(scrollRect);
  4538. }
  4539. //绘制显示对象
  4540. if (hasBlendMode) {
  4541. buffer.context.setGlobalCompositeOperation(compositeOp);
  4542. }
  4543. drawCalls += this.drawDisplayObject(displayObject, buffer, dirtyList, matrix, displayObject.$displayList, clipRegion, root);
  4544. if (hasBlendMode) {
  4545. buffer.context.setGlobalCompositeOperation(defaultCompositeOp);
  4546. }
  4547. if (scrollRect) {
  4548. buffer.context.popMask();
  4549. }
  4550. egret.sys.Region.release(region);
  4551. egret.Matrix.release(displayMatrix);
  4552. return drawCalls;
  4553. }
  4554. else {
  4555. //绘制显示对象自身,若有scrollRect,应用clip
  4556. var displayBuffer = this.createRenderBuffer(region.width, region.height);
  4557. // let displayContext = displayBuffer.context;
  4558. displayBuffer.context.pushBuffer(displayBuffer);
  4559. displayBuffer.setTransform(1, 0, 0, 1, -region.minX, -region.minY);
  4560. var offsetM = egret.Matrix.create().setTo(1, 0, 0, 1, -region.minX, -region.minY);
  4561. drawCalls += this.drawDisplayObject(displayObject, displayBuffer, dirtyList, offsetM, displayObject.$displayList, region, root);
  4562. //绘制遮罩
  4563. if (mask) {
  4564. //如果只有一次绘制或是已经被cache直接绘制到displayContext
  4565. //webgl暂时无法添加,因为会有边界像素没有被擦除
  4566. //let maskRenderNode = mask.$getRenderNode();
  4567. //if (maskRenderNode && maskRenderNode.$getRenderCount() == 1 || mask.$displayList) {
  4568. // displayBuffer.context.setGlobalCompositeOperation("destination-in");
  4569. // drawCalls += this.drawDisplayObject(mask, displayBuffer, dirtyList, offsetM,
  4570. // mask.$displayList, region, root);
  4571. //}
  4572. //else {
  4573. var maskBuffer = this.createRenderBuffer(region.width, region.height);
  4574. maskBuffer.context.pushBuffer(maskBuffer);
  4575. maskBuffer.setTransform(1, 0, 0, 1, -region.minX, -region.minY);
  4576. offsetM = egret.Matrix.create().setTo(1, 0, 0, 1, -region.minX, -region.minY);
  4577. drawCalls += this.drawDisplayObject(mask, maskBuffer, dirtyList, offsetM, mask.$displayList, region, root);
  4578. maskBuffer.context.popBuffer();
  4579. displayBuffer.context.setGlobalCompositeOperation("destination-in");
  4580. displayBuffer.setTransform(1, 0, 0, -1, 0, maskBuffer.height);
  4581. displayBuffer.globalAlpha = 1;
  4582. var maskBufferWidth = maskBuffer.rootRenderTarget.width;
  4583. var maskBufferHeight = maskBuffer.rootRenderTarget.height;
  4584. displayBuffer.context.drawTexture(maskBuffer.rootRenderTarget.texture, 0, 0, maskBufferWidth, maskBufferHeight, 0, 0, maskBufferWidth, maskBufferHeight, maskBufferWidth, maskBufferHeight);
  4585. displayBuffer.context.setGlobalCompositeOperation("source-over");
  4586. renderBufferPool.push(maskBuffer);
  4587. }
  4588. egret.Matrix.release(offsetM);
  4589. displayBuffer.context.setGlobalCompositeOperation(defaultCompositeOp);
  4590. displayBuffer.context.popBuffer();
  4591. //绘制结果到屏幕
  4592. if (drawCalls > 0) {
  4593. drawCalls++;
  4594. if (hasBlendMode) {
  4595. buffer.context.setGlobalCompositeOperation(compositeOp);
  4596. }
  4597. if (scrollRect) {
  4598. var m = displayMatrix;
  4599. displayBuffer.setTransform(m.a, m.b, m.c, m.d, m.tx - region.minX, m.ty - region.minY);
  4600. displayBuffer.context.pushMask(scrollRect);
  4601. }
  4602. buffer.globalAlpha = 1;
  4603. buffer.setTransform(1, 0, 0, -1, region.minX + matrix.tx, region.minY + matrix.ty + displayBuffer.height);
  4604. var displayBufferWidth = displayBuffer.rootRenderTarget.width;
  4605. var displayBufferHeight = displayBuffer.rootRenderTarget.height;
  4606. buffer.context.drawTexture(displayBuffer.rootRenderTarget.texture, 0, 0, displayBufferWidth, displayBufferHeight, 0, 0, displayBufferWidth, displayBufferHeight, displayBufferWidth, displayBufferHeight);
  4607. if (scrollRect) {
  4608. displayBuffer.context.popMask();
  4609. }
  4610. if (hasBlendMode) {
  4611. buffer.context.setGlobalCompositeOperation(defaultCompositeOp);
  4612. }
  4613. }
  4614. renderBufferPool.push(displayBuffer);
  4615. egret.sys.Region.release(region);
  4616. egret.Matrix.release(displayMatrix);
  4617. return drawCalls;
  4618. }
  4619. };
  4620. /**
  4621. * @private
  4622. */
  4623. WebGLRenderer.prototype.drawWithScrollRect = function (displayObject, buffer, dirtyList, matrix, clipRegion, root) {
  4624. var drawCalls = 0;
  4625. var scrollRect = displayObject.$scrollRect ? displayObject.$scrollRect : displayObject.$maskRect;
  4626. if (scrollRect.isEmpty()) {
  4627. return drawCalls;
  4628. }
  4629. var m = egret.Matrix.create();
  4630. m.copyFrom(displayObject.$getConcatenatedMatrix());
  4631. if (root) {
  4632. displayObject.$getConcatenatedMatrixAt(root, m);
  4633. }
  4634. else if (displayObject.$parentDisplayList) {
  4635. var displayRoot = displayObject.$parentDisplayList.root;
  4636. if (displayRoot !== displayObject.$stage) {
  4637. displayObject.$getConcatenatedMatrixAt(displayRoot, m);
  4638. }
  4639. }
  4640. var region = egret.sys.Region.create();
  4641. region.updateRegion(scrollRect, m);
  4642. if (region.isEmpty() || (clipRegion && !clipRegion.intersects(region))) {
  4643. egret.sys.Region.release(region);
  4644. egret.Matrix.release(m);
  4645. return drawCalls;
  4646. }
  4647. var found = false;
  4648. if (!dirtyList) {
  4649. found = true;
  4650. }
  4651. else {
  4652. var l = dirtyList.length;
  4653. for (var j = 0; j < l; j++) {
  4654. if (region.intersects(dirtyList[j])) {
  4655. found = true;
  4656. break;
  4657. }
  4658. }
  4659. }
  4660. if (!found) {
  4661. egret.sys.Region.release(region);
  4662. egret.Matrix.release(m);
  4663. return drawCalls;
  4664. }
  4665. //绘制显示对象自身
  4666. buffer.setTransform(m.a, m.b, m.c, m.d, m.tx + matrix.tx, m.ty + matrix.ty);
  4667. var context = buffer.context;
  4668. var scissor = false;
  4669. if (buffer.$hasScissor || m.b != 0 || m.c != 0) {
  4670. context.pushMask(scrollRect);
  4671. }
  4672. else {
  4673. var x = scrollRect.x;
  4674. var y = scrollRect.y;
  4675. var w = scrollRect.width;
  4676. var h = scrollRect.height;
  4677. x = x * m.a + m.tx + matrix.tx;
  4678. y = y * m.d + m.ty + matrix.ty;
  4679. w = w * m.a;
  4680. h = h * m.d;
  4681. context.enableScissor(x, -y - h + buffer.height, w, h);
  4682. scissor = true;
  4683. }
  4684. drawCalls += this.drawDisplayObject(displayObject, buffer, dirtyList, matrix, displayObject.$displayList, region, root);
  4685. buffer.setTransform(m.a, m.b, m.c, m.d, m.tx + matrix.tx, m.ty + matrix.ty);
  4686. if (scissor) {
  4687. context.disableScissor();
  4688. }
  4689. else {
  4690. context.popMask();
  4691. }
  4692. egret.sys.Region.release(region);
  4693. egret.Matrix.release(m);
  4694. return drawCalls;
  4695. };
  4696. /**
  4697. * 将一个RenderNode对象绘制到渲染缓冲
  4698. * @param node 要绘制的节点
  4699. * @param buffer 渲染缓冲
  4700. * @param matrix 要叠加的矩阵
  4701. * @param forHitTest 绘制结果是用于碰撞检测。若为true,当渲染GraphicsNode时,会忽略透明度样式设置,全都绘制为不透明的。
  4702. */
  4703. WebGLRenderer.prototype.drawNodeToBuffer = function (node, buffer, matrix, forHitTest) {
  4704. var webglBuffer = buffer;
  4705. //pushRenderTARGET
  4706. webglBuffer.context.pushBuffer(webglBuffer);
  4707. webglBuffer.setTransform(matrix.a, matrix.b, matrix.c, matrix.d, matrix.tx, matrix.ty);
  4708. this.renderNode(node, buffer, forHitTest);
  4709. webglBuffer.context.$drawWebGL();
  4710. webglBuffer.onRenderFinish();
  4711. //popRenderTARGET
  4712. webglBuffer.context.popBuffer();
  4713. };
  4714. /**
  4715. * @private
  4716. */
  4717. WebGLRenderer.prototype.renderNode = function (node, buffer, forHitTest) {
  4718. switch (node.type) {
  4719. case 1 /* BitmapNode */:
  4720. this.renderBitmap(node, buffer);
  4721. break;
  4722. case 2 /* TextNode */:
  4723. this.renderText(node, buffer);
  4724. break;
  4725. case 3 /* GraphicsNode */:
  4726. this.renderGraphics(node, buffer, forHitTest);
  4727. break;
  4728. case 4 /* GroupNode */:
  4729. this.renderGroup(node, buffer);
  4730. break;
  4731. case 6 /* SetAlphaNode */:
  4732. buffer.globalAlpha = node.drawData[0];
  4733. break;
  4734. case 7 /* MeshNode */:
  4735. this.renderMesh(node, buffer);
  4736. break;
  4737. }
  4738. };
  4739. /**
  4740. * @private
  4741. */
  4742. WebGLRenderer.prototype.renderBitmap = function (node, buffer) {
  4743. var image = node.image;
  4744. if (!image) {
  4745. return;
  4746. }
  4747. //buffer.imageSmoothingEnabled = node.smoothing;
  4748. var data = node.drawData;
  4749. var length = data.length;
  4750. var pos = 0;
  4751. var m = node.matrix;
  4752. var blendMode = node.blendMode;
  4753. var alpha = node.alpha;
  4754. if (m) {
  4755. buffer.saveTransform();
  4756. buffer.transform(m.a, m.b, m.c, m.d, m.tx, m.ty);
  4757. }
  4758. //这里不考虑嵌套
  4759. if (blendMode) {
  4760. buffer.context.setGlobalCompositeOperation(blendModes[blendMode]);
  4761. }
  4762. var originAlpha;
  4763. if (alpha == alpha) {
  4764. originAlpha = buffer.globalAlpha;
  4765. buffer.globalAlpha *= alpha;
  4766. }
  4767. if (node.filter) {
  4768. buffer.context.$filter = node.filter;
  4769. while (pos < length) {
  4770. buffer.context.drawImage(image, data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], node.imageWidth, node.imageHeight);
  4771. }
  4772. buffer.context.$filter = null;
  4773. }
  4774. else {
  4775. while (pos < length) {
  4776. buffer.context.drawImage(image, data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], node.imageWidth, node.imageHeight);
  4777. }
  4778. }
  4779. if (blendMode) {
  4780. buffer.context.setGlobalCompositeOperation(defaultCompositeOp);
  4781. }
  4782. if (alpha == alpha) {
  4783. buffer.globalAlpha = originAlpha;
  4784. }
  4785. if (m) {
  4786. buffer.restoreTransform();
  4787. }
  4788. };
  4789. /**
  4790. * @private
  4791. */
  4792. WebGLRenderer.prototype.renderMesh = function (node, buffer) {
  4793. var image = node.image;
  4794. //buffer.imageSmoothingEnabled = node.smoothing;
  4795. var data = node.drawData;
  4796. var length = data.length;
  4797. var pos = 0;
  4798. var m = node.matrix;
  4799. if (m) {
  4800. buffer.saveTransform();
  4801. buffer.transform(m.a, m.b, m.c, m.d, m.tx, m.ty);
  4802. }
  4803. while (pos < length) {
  4804. buffer.context.drawMesh(image, data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], data[pos++], node.imageWidth, node.imageHeight, node.uvs, node.vertices, node.indices, node.bounds);
  4805. }
  4806. if (m) {
  4807. buffer.restoreTransform();
  4808. }
  4809. };
  4810. /**
  4811. * @private
  4812. */
  4813. WebGLRenderer.prototype.renderText = function (node, buffer) {
  4814. var width = node.width - node.x;
  4815. var height = node.height - node.y;
  4816. if (node.drawData.length == 0) {
  4817. return;
  4818. }
  4819. if (!this.canvasRenderBuffer || !this.canvasRenderBuffer.context) {
  4820. this.canvasRenderer = new egret.CanvasRenderer();
  4821. this.canvasRenderBuffer = new web.CanvasRenderBuffer(width, height);
  4822. }
  4823. else if (node.dirtyRender) {
  4824. this.canvasRenderBuffer.resize(width, height);
  4825. }
  4826. if (!this.canvasRenderBuffer.context) {
  4827. return;
  4828. }
  4829. if (node.x || node.y) {
  4830. if (node.dirtyRender) {
  4831. this.canvasRenderBuffer.context.translate(-node.x, -node.y);
  4832. }
  4833. buffer.transform(1, 0, 0, 1, node.x, node.y);
  4834. }
  4835. if (node.dirtyRender) {
  4836. var surface = this.canvasRenderBuffer.surface;
  4837. this.canvasRenderer.renderText(node, this.canvasRenderBuffer.context);
  4838. // 拷贝canvas到texture
  4839. var texture = node.$texture;
  4840. if (!texture) {
  4841. texture = buffer.context.createTexture(surface);
  4842. node.$texture = texture;
  4843. }
  4844. else {
  4845. // 重新拷贝新的图像
  4846. buffer.context.updateTexture(texture, surface);
  4847. }
  4848. // 保存材质尺寸
  4849. node.$textureWidth = surface.width;
  4850. node.$textureHeight = surface.height;
  4851. }
  4852. var textureWidth = node.$textureWidth;
  4853. var textureHeight = node.$textureHeight;
  4854. buffer.context.drawTexture(node.$texture, 0, 0, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight, textureWidth, textureHeight);
  4855. if (node.x || node.y) {
  4856. if (node.dirtyRender) {
  4857. this.canvasRenderBuffer.context.translate(node.x, node.y);
  4858. }
  4859. buffer.transform(1, 0, 0, 1, -node.x, -node.y);
  4860. }
  4861. node.dirtyRender = false;
  4862. };
  4863. /**
  4864. * @private
  4865. */
  4866. WebGLRenderer.prototype.renderGraphics = function (node, buffer, forHitTest) {
  4867. var width = node.width;
  4868. var height = node.height;
  4869. if (width <= 0 || height <= 0 || !width || !height || node.drawData.length == 0) {
  4870. return;
  4871. }
  4872. if (!this.canvasRenderBuffer || !this.canvasRenderBuffer.context) {
  4873. this.canvasRenderer = new egret.CanvasRenderer();
  4874. this.canvasRenderBuffer = new web.CanvasRenderBuffer(width, height);
  4875. }
  4876. else if (node.dirtyRender || forHitTest) {
  4877. this.canvasRenderBuffer.resize(width, height);
  4878. }
  4879. if (!this.canvasRenderBuffer.context) {
  4880. return;
  4881. }
  4882. if (node.x || node.y) {
  4883. if (node.dirtyRender || forHitTest) {
  4884. this.canvasRenderBuffer.context.translate(-node.x, -node.y);
  4885. }
  4886. buffer.transform(1, 0, 0, 1, node.x, node.y);
  4887. }
  4888. var surface = this.canvasRenderBuffer.surface;
  4889. if (forHitTest) {
  4890. this.canvasRenderer.renderGraphics(node, this.canvasRenderBuffer.context, true);
  4891. egret.WebGLUtils.deleteWebGLTexture(surface);
  4892. var texture = buffer.context.getWebGLTexture(surface);
  4893. buffer.context.drawTexture(texture, 0, 0, width, height, 0, 0, width, height, surface.width, surface.height);
  4894. }
  4895. else {
  4896. if (node.dirtyRender) {
  4897. this.canvasRenderer.renderGraphics(node, this.canvasRenderBuffer.context);
  4898. // 拷贝canvas到texture
  4899. var texture = node.$texture;
  4900. if (!texture) {
  4901. texture = buffer.context.createTexture(surface);
  4902. node.$texture = texture;
  4903. }
  4904. else {
  4905. // 重新拷贝新的图像
  4906. buffer.context.updateTexture(texture, surface);
  4907. }
  4908. // 保存材质尺寸
  4909. node.$textureWidth = surface.width;
  4910. node.$textureHeight = surface.height;
  4911. }
  4912. var textureWidth = node.$textureWidth;
  4913. var textureHeight = node.$textureHeight;
  4914. buffer.context.drawTexture(node.$texture, 0, 0, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight, textureWidth, textureHeight);
  4915. }
  4916. if (node.x || node.y) {
  4917. if (node.dirtyRender || forHitTest) {
  4918. this.canvasRenderBuffer.context.translate(node.x, node.y);
  4919. }
  4920. buffer.transform(1, 0, 0, 1, -node.x, -node.y);
  4921. }
  4922. if (!forHitTest) {
  4923. node.dirtyRender = false;
  4924. }
  4925. };
  4926. WebGLRenderer.prototype.renderGroup = function (groupNode, buffer) {
  4927. var children = groupNode.drawData;
  4928. var length = children.length;
  4929. for (var i = 0; i < length; i++) {
  4930. var node = children[i];
  4931. this.renderNode(node, buffer);
  4932. }
  4933. };
  4934. /**
  4935. * @private
  4936. */
  4937. WebGLRenderer.prototype.createRenderBuffer = function (width, height) {
  4938. var buffer = renderBufferPool.pop();
  4939. if (buffer) {
  4940. buffer.resize(width, height);
  4941. }
  4942. else {
  4943. buffer = new web.WebGLRenderBuffer(width, height);
  4944. buffer.$computeDrawCall = false;
  4945. }
  4946. return buffer;
  4947. };
  4948. return WebGLRenderer;
  4949. }());
  4950. web.WebGLRenderer = WebGLRenderer;
  4951. __reflect(WebGLRenderer.prototype, "egret.web.WebGLRenderer", ["egret.sys.SystemRenderer"]);
  4952. })(web = egret.web || (egret.web = {}));
  4953. })(egret || (egret = {}));
  4954. //////////////////////////////////////////////////////////////////////////////////////
  4955. //
  4956. // Copyright (c) 2014-present, Egret Technology.
  4957. // All rights reserved.
  4958. // Redistribution and use in source and binary forms, with or without
  4959. // modification, are permitted provided that the following conditions are met:
  4960. //
  4961. // * Redistributions of source code must retain the above copyright
  4962. // notice, this list of conditions and the following disclaimer.
  4963. // * Redistributions in binary form must reproduce the above copyright
  4964. // notice, this list of conditions and the following disclaimer in the
  4965. // documentation and/or other materials provided with the distribution.
  4966. // * Neither the name of the Egret nor the
  4967. // names of its contributors may be used to endorse or promote products
  4968. // derived from this software without specific prior written permission.
  4969. //
  4970. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  4971. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  4972. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  4973. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  4974. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  4975. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  4976. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  4977. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  4978. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  4979. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4980. //
  4981. //////////////////////////////////////////////////////////////////////////////////////
  4982. var egret;
  4983. (function (egret) {
  4984. var web;
  4985. (function (web) {
  4986. /**
  4987. * @private
  4988. */
  4989. function getOption(key) {
  4990. if (window.location) {
  4991. var search = location.search;
  4992. if (search == "") {
  4993. return "";
  4994. }
  4995. search = search.slice(1);
  4996. var searchArr = search.split("&");
  4997. var length_3 = searchArr.length;
  4998. for (var i = 0; i < length_3; i++) {
  4999. var str = searchArr[i];
  5000. var arr = str.split("=");
  5001. if (arr[0] == key) {
  5002. return arr[1];
  5003. }
  5004. }
  5005. }
  5006. return "";
  5007. }
  5008. web.getOption = getOption;
  5009. egret.getOption = getOption;
  5010. })(web = egret.web || (egret.web = {}));
  5011. })(egret || (egret = {}));
  5012. //////////////////////////////////////////////////////////////////////////////////////
  5013. //
  5014. // Copyright (c) 2014-present, Egret Technology.
  5015. // All rights reserved.
  5016. // Redistribution and use in source and binary forms, with or without
  5017. // modification, are permitted provided that the following conditions are met:
  5018. //
  5019. // * Redistributions of source code must retain the above copyright
  5020. // notice, this list of conditions and the following disclaimer.
  5021. // * Redistributions in binary form must reproduce the above copyright
  5022. // notice, this list of conditions and the following disclaimer in the
  5023. // documentation and/or other materials provided with the distribution.
  5024. // * Neither the name of the Egret nor the
  5025. // names of its contributors may be used to endorse or promote products
  5026. // derived from this software without specific prior written permission.
  5027. //
  5028. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  5029. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  5030. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  5031. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  5032. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5033. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  5034. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5035. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5036. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5037. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5038. //
  5039. //////////////////////////////////////////////////////////////////////////////////////
  5040. var egret;
  5041. (function (egret) {
  5042. var web;
  5043. (function (web) {
  5044. /**
  5045. * @private
  5046. */
  5047. var WebPlayer = (function (_super) {
  5048. __extends(WebPlayer, _super);
  5049. function WebPlayer(container, options) {
  5050. var _this = _super.call(this) || this;
  5051. _this.init(container, options);
  5052. _this.initOrientation();
  5053. return _this;
  5054. }
  5055. WebPlayer.prototype.init = function (container, options) {
  5056. var option = this.readOption(container, options);
  5057. var stage = new egret.Stage();
  5058. stage.$screen = this;
  5059. stage.$scaleMode = option.scaleMode;
  5060. stage.$orientation = option.orientation;
  5061. stage.$maxTouches = option.maxTouches;
  5062. stage.frameRate = option.frameRate;
  5063. stage.textureScaleFactor = option.textureScaleFactor;
  5064. var buffer = new egret.sys.RenderBuffer(undefined, undefined, true);
  5065. var canvas = buffer.surface;
  5066. this.attachCanvas(container, canvas);
  5067. var webTouch = new web.WebTouchHandler(stage, canvas);
  5068. var player = new egret.sys.Player(buffer, stage, option.entryClassName);
  5069. var webHide = new egret.web.WebHideHandler(stage);
  5070. var webInput = new web.HTMLInput();
  5071. player.showPaintRect(option.showPaintRect);
  5072. if (option.showFPS || option.showLog) {
  5073. player.displayFPS(option.showFPS, option.showLog, option.logFilter, option.fpsStyles);
  5074. }
  5075. this.playerOption = option;
  5076. this.container = container;
  5077. this.canvas = canvas;
  5078. this.stage = stage;
  5079. this.player = player;
  5080. this.webTouchHandler = webTouch;
  5081. this.webInput = webInput;
  5082. this.webHide = webHide;
  5083. egret.web.$cacheTextAdapter(webInput, stage, container, canvas);
  5084. this.updateScreenSize();
  5085. this.updateMaxTouches();
  5086. player.start();
  5087. };
  5088. WebPlayer.prototype.initOrientation = function () {
  5089. var self = this;
  5090. window.addEventListener("orientationchange", function () {
  5091. window.setTimeout(function () {
  5092. egret.StageOrientationEvent.dispatchStageOrientationEvent(self.stage, egret.StageOrientationEvent.ORIENTATION_CHANGE);
  5093. }, 350);
  5094. });
  5095. };
  5096. /**
  5097. * 读取初始化参数
  5098. */
  5099. WebPlayer.prototype.readOption = function (container, options) {
  5100. var option = {};
  5101. option.entryClassName = container.getAttribute("data-entry-class");
  5102. option.scaleMode = container.getAttribute("data-scale-mode") || egret.StageScaleMode.NO_SCALE;
  5103. option.frameRate = +container.getAttribute("data-frame-rate") || 30;
  5104. option.contentWidth = +container.getAttribute("data-content-width") || 480;
  5105. option.contentHeight = +container.getAttribute("data-content-height") || 800;
  5106. option.orientation = container.getAttribute("data-orientation") || egret.OrientationMode.AUTO;
  5107. option.maxTouches = +container.getAttribute("data-multi-fingered") || 2;
  5108. option.textureScaleFactor = +container.getAttribute("texture-scale-factor") || 1;
  5109. if (options.renderMode == "webgl") {
  5110. option.showPaintRect = false;
  5111. }
  5112. else {
  5113. option.showPaintRect = container.getAttribute("data-show-paint-rect") == "true";
  5114. }
  5115. option.showFPS = container.getAttribute("data-show-fps") == "true";
  5116. var styleStr = container.getAttribute("data-show-fps-style") || "";
  5117. var stylesArr = styleStr.split(",");
  5118. var styles = {};
  5119. for (var i = 0; i < stylesArr.length; i++) {
  5120. var tempStyleArr = stylesArr[i].split(":");
  5121. styles[tempStyleArr[0]] = tempStyleArr[1];
  5122. }
  5123. option.fpsStyles = styles;
  5124. option.showLog = container.getAttribute("data-show-log") == "true";
  5125. option.logFilter = container.getAttribute("data-log-filter");
  5126. return option;
  5127. };
  5128. /**
  5129. * @private
  5130. * 添加canvas到container。
  5131. */
  5132. WebPlayer.prototype.attachCanvas = function (container, canvas) {
  5133. var style = canvas.style;
  5134. style.cursor = "inherit";
  5135. style.position = "absolute";
  5136. style.top = "0";
  5137. style.bottom = "0";
  5138. style.left = "0";
  5139. style.right = "0";
  5140. container.appendChild(canvas);
  5141. style = container.style;
  5142. style.overflow = "hidden";
  5143. style.position = "relative";
  5144. style["webkitTransform"] = "translateZ(0)";
  5145. };
  5146. /**
  5147. * @private
  5148. * 更新播放器视口尺寸
  5149. */
  5150. WebPlayer.prototype.updateScreenSize = function () {
  5151. var canvas = this.canvas;
  5152. if (canvas['userTyping'])
  5153. return;
  5154. var option = this.playerOption;
  5155. var screenRect = this.container.getBoundingClientRect();
  5156. var shouldRotate = false;
  5157. var orientation = this.stage.$orientation;
  5158. if (orientation != egret.OrientationMode.AUTO) {
  5159. shouldRotate = orientation != egret.OrientationMode.PORTRAIT && screenRect.height > screenRect.width
  5160. || orientation == egret.OrientationMode.PORTRAIT && screenRect.width > screenRect.height;
  5161. }
  5162. var screenWidth = shouldRotate ? screenRect.height : screenRect.width;
  5163. var screenHeight = shouldRotate ? screenRect.width : screenRect.height;
  5164. egret.Capabilities.$boundingClientWidth = screenWidth;
  5165. egret.Capabilities.$boundingClientHeight = screenHeight;
  5166. var stageSize = egret.sys.screenAdapter.calculateStageSize(this.stage.$scaleMode, screenWidth, screenHeight, option.contentWidth, option.contentHeight);
  5167. var stageWidth = stageSize.stageWidth;
  5168. var stageHeight = stageSize.stageHeight;
  5169. var displayWidth = stageSize.displayWidth;
  5170. var displayHeight = stageSize.displayHeight;
  5171. if (canvas.width !== stageWidth) {
  5172. canvas.width = stageWidth;
  5173. }
  5174. if (canvas.height !== stageHeight) {
  5175. canvas.height = stageHeight;
  5176. }
  5177. canvas.style[egret.web.getPrefixStyleName("transformOrigin")] = "0% 0% 0px";
  5178. canvas.style.width = displayWidth + "px";
  5179. canvas.style.height = displayHeight + "px";
  5180. var rotation = 0;
  5181. if (shouldRotate) {
  5182. if (orientation == egret.OrientationMode.LANDSCAPE) {
  5183. rotation = 90;
  5184. canvas.style.top = (screenRect.height - displayWidth) / 2 + "px";
  5185. canvas.style.left = (screenRect.width + displayHeight) / 2 + "px";
  5186. }
  5187. else {
  5188. rotation = -90;
  5189. canvas.style.top = (screenRect.height + displayWidth) / 2 + "px";
  5190. canvas.style.left = (screenRect.width - displayHeight) / 2 + "px";
  5191. }
  5192. }
  5193. else {
  5194. canvas.style.top = (screenRect.height - displayHeight) / 2 + "px";
  5195. canvas.style.left = (screenRect.width - displayWidth) / 2 + "px";
  5196. }
  5197. var transform = "rotate(" + rotation + "deg)";
  5198. canvas.style[egret.web.getPrefixStyleName("transform")] = transform;
  5199. var scalex = displayWidth / stageWidth, scaley = displayHeight / stageHeight;
  5200. this.webTouchHandler.updateScaleMode(scalex, scaley, rotation);
  5201. this.webInput.$updateSize();
  5202. this.player.updateStageSize(stageWidth, stageHeight); //不要在这个方法后面修改属性
  5203. };
  5204. WebPlayer.prototype.setContentSize = function (width, height) {
  5205. var option = this.playerOption;
  5206. option.contentWidth = width;
  5207. option.contentHeight = height;
  5208. this.updateScreenSize();
  5209. };
  5210. /**
  5211. * @private
  5212. * 更新触摸数量
  5213. */
  5214. WebPlayer.prototype.updateMaxTouches = function () {
  5215. this.webTouchHandler.$updateMaxTouches();
  5216. };
  5217. return WebPlayer;
  5218. }(egret.HashObject));
  5219. web.WebPlayer = WebPlayer;
  5220. __reflect(WebPlayer.prototype, "egret.web.WebPlayer", ["egret.sys.Screen"]);
  5221. })(web = egret.web || (egret.web = {}));
  5222. })(egret || (egret = {}));
  5223. //////////////////////////////////////////////////////////////////////////////////////
  5224. //
  5225. // Copyright (c) 2014-present, Egret Technology.
  5226. // All rights reserved.
  5227. // Redistribution and use in source and binary forms, with or without
  5228. // modification, are permitted provided that the following conditions are met:
  5229. //
  5230. // * Redistributions of source code must retain the above copyright
  5231. // notice, this list of conditions and the following disclaimer.
  5232. // * Redistributions in binary form must reproduce the above copyright
  5233. // notice, this list of conditions and the following disclaimer in the
  5234. // documentation and/or other materials provided with the distribution.
  5235. // * Neither the name of the Egret nor the
  5236. // names of its contributors may be used to endorse or promote products
  5237. // derived from this software without specific prior written permission.
  5238. //
  5239. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  5240. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  5241. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  5242. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  5243. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5244. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  5245. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5246. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5247. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5248. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5249. //
  5250. //////////////////////////////////////////////////////////////////////////////////////
  5251. var egret;
  5252. (function (egret) {
  5253. var web;
  5254. (function (web) {
  5255. var sharedCanvas;
  5256. var sharedContext;
  5257. /**
  5258. * @private
  5259. */
  5260. function convertImageToCanvas(texture, rect) {
  5261. if (!sharedCanvas) {
  5262. sharedCanvas = document.createElement("canvas");
  5263. sharedContext = sharedCanvas.getContext("2d");
  5264. }
  5265. var w = texture.$getTextureWidth();
  5266. var h = texture.$getTextureHeight();
  5267. if (rect == null) {
  5268. rect = egret.$TempRectangle;
  5269. rect.x = 0;
  5270. rect.y = 0;
  5271. rect.width = w;
  5272. rect.height = h;
  5273. }
  5274. rect.x = Math.min(rect.x, w - 1);
  5275. rect.y = Math.min(rect.y, h - 1);
  5276. rect.width = Math.min(rect.width, w - rect.x);
  5277. rect.height = Math.min(rect.height, h - rect.y);
  5278. var iWidth = rect.width;
  5279. var iHeight = rect.height;
  5280. var surface = sharedCanvas;
  5281. surface["style"]["width"] = iWidth + "px";
  5282. surface["style"]["height"] = iHeight + "px";
  5283. sharedCanvas.width = iWidth;
  5284. sharedCanvas.height = iHeight;
  5285. if (egret.Capabilities.$renderMode == "webgl") {
  5286. var renderTexture = void 0;
  5287. //webgl下非RenderTexture纹理先画到RenderTexture
  5288. if (!texture.$renderBuffer) {
  5289. renderTexture = new egret.RenderTexture();
  5290. renderTexture.drawToTexture(new egret.Bitmap(texture));
  5291. }
  5292. else {
  5293. renderTexture = texture;
  5294. }
  5295. //从RenderTexture中读取像素数据,填入canvas
  5296. var pixels = renderTexture.$renderBuffer.getPixels(rect.x, rect.y, iWidth, iHeight);
  5297. var imageData = new ImageData(iWidth, iHeight);
  5298. for (var i = 0; i < pixels.length; i++) {
  5299. imageData.data[i] = pixels[i];
  5300. }
  5301. sharedContext.putImageData(imageData, 0, 0);
  5302. if (!texture.$renderBuffer) {
  5303. renderTexture.dispose();
  5304. }
  5305. return surface;
  5306. }
  5307. else {
  5308. var bitmapData = texture;
  5309. var offsetX = Math.round(bitmapData._offsetX);
  5310. var offsetY = Math.round(bitmapData._offsetY);
  5311. var bitmapWidth = bitmapData._bitmapWidth;
  5312. var bitmapHeight = bitmapData._bitmapHeight;
  5313. sharedContext.drawImage(bitmapData._bitmapData.source, bitmapData._bitmapX + rect.x / egret.$TextureScaleFactor, bitmapData._bitmapY + rect.y / egret.$TextureScaleFactor, bitmapWidth * rect.width / w, bitmapHeight * rect.height / h, offsetX, offsetY, rect.width, rect.height);
  5314. return surface;
  5315. }
  5316. }
  5317. /**
  5318. * @private
  5319. */
  5320. function toDataURL(type, rect) {
  5321. try {
  5322. var surface = convertImageToCanvas(this, rect);
  5323. var result = surface.toDataURL(type);
  5324. return result;
  5325. }
  5326. catch (e) {
  5327. egret.$error(1033);
  5328. }
  5329. return null;
  5330. }
  5331. function saveToFile(type, filePath, rect) {
  5332. var base64 = toDataURL.call(this, type, rect);
  5333. if (base64 == null) {
  5334. return;
  5335. }
  5336. var href = base64.replace(/^data:image[^;]*/, "data:image/octet-stream");
  5337. var aLink = document.createElement('a');
  5338. aLink['download'] = filePath;
  5339. aLink.href = href;
  5340. var evt = document.createEvent("HTMLEvents");
  5341. evt.initEvent("click", false, false); //initEvent 不加后两个参数在FF下会报错
  5342. aLink.dispatchEvent(evt);
  5343. }
  5344. function getPixel32(x, y) {
  5345. egret.$warn(1041, "getPixel32", "getPixels");
  5346. return this.getPixels(x, y);
  5347. }
  5348. function getPixels(x, y, width, height) {
  5349. if (width === void 0) { width = 1; }
  5350. if (height === void 0) { height = 1; }
  5351. try {
  5352. var surface = convertImageToCanvas(this);
  5353. var result = sharedContext.getImageData(x, y, width, height).data;
  5354. return result;
  5355. }
  5356. catch (e) {
  5357. egret.$error(1039);
  5358. }
  5359. }
  5360. egret.Texture.prototype.toDataURL = toDataURL;
  5361. egret.Texture.prototype.saveToFile = saveToFile;
  5362. egret.Texture.prototype.getPixel32 = getPixel32;
  5363. egret.Texture.prototype.getPixels = getPixels;
  5364. })(web = egret.web || (egret.web = {}));
  5365. })(egret || (egret = {}));
  5366. //////////////////////////////////////////////////////////////////////////////////////
  5367. //
  5368. // Copyright (c) 2014-present, Egret Technology.
  5369. // All rights reserved.
  5370. // Redistribution and use in source and binary forms, with or without
  5371. // modification, are permitted provided that the following conditions are met:
  5372. //
  5373. // * Redistributions of source code must retain the above copyright
  5374. // notice, this list of conditions and the following disclaimer.
  5375. // * Redistributions in binary form must reproduce the above copyright
  5376. // notice, this list of conditions and the following disclaimer in the
  5377. // documentation and/or other materials provided with the distribution.
  5378. // * Neither the name of the Egret nor the
  5379. // names of its contributors may be used to endorse or promote products
  5380. // derived from this software without specific prior written permission.
  5381. //
  5382. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  5383. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  5384. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  5385. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  5386. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5387. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  5388. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5389. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5390. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5391. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5392. //
  5393. //////////////////////////////////////////////////////////////////////////////////////
  5394. var egret;
  5395. (function (egret) {
  5396. var web;
  5397. (function (web) {
  5398. /**
  5399. * @private
  5400. * XML节点基类
  5401. */
  5402. var XMLNode = (function () {
  5403. /**
  5404. * @private
  5405. */
  5406. function XMLNode(nodeType, parent) {
  5407. this.nodeType = nodeType;
  5408. this.parent = parent;
  5409. }
  5410. return XMLNode;
  5411. }());
  5412. web.XMLNode = XMLNode;
  5413. __reflect(XMLNode.prototype, "egret.web.XMLNode");
  5414. /**
  5415. * @private
  5416. * XML节点对象
  5417. */
  5418. var XML = (function (_super) {
  5419. __extends(XML, _super);
  5420. /**
  5421. * @private
  5422. */
  5423. function XML(localName, parent, prefix, namespace, name) {
  5424. var _this = _super.call(this, 1, parent) || this;
  5425. /**
  5426. * @private
  5427. * 当前节点上的属性列表
  5428. */
  5429. _this.attributes = {};
  5430. /**
  5431. * @private
  5432. * 当前节点的子节点列表
  5433. */
  5434. _this.children = [];
  5435. _this.localName = localName;
  5436. _this.prefix = prefix;
  5437. _this.namespace = namespace;
  5438. _this.name = name;
  5439. return _this;
  5440. }
  5441. return XML;
  5442. }(XMLNode));
  5443. web.XML = XML;
  5444. __reflect(XML.prototype, "egret.web.XML");
  5445. /**
  5446. * @private
  5447. * XML文本节点
  5448. */
  5449. var XMLText = (function (_super) {
  5450. __extends(XMLText, _super);
  5451. /**
  5452. * @private
  5453. */
  5454. function XMLText(text, parent) {
  5455. var _this = _super.call(this, 3, parent) || this;
  5456. _this.text = text;
  5457. return _this;
  5458. }
  5459. return XMLText;
  5460. }(XMLNode));
  5461. web.XMLText = XMLText;
  5462. __reflect(XMLText.prototype, "egret.web.XMLText");
  5463. var parser = new DOMParser();
  5464. /**
  5465. * @private
  5466. * 解析字符串为XML对象
  5467. * @param text 要解析的字符串
  5468. */
  5469. function parse(text) {
  5470. var xmlDoc = parser.parseFromString(text, "text/xml");
  5471. var length = xmlDoc.childNodes.length;
  5472. for (var i = 0; i < length; i++) {
  5473. var node = xmlDoc.childNodes[i];
  5474. if (node.nodeType == 1) {
  5475. return parseNode(node, null);
  5476. }
  5477. }
  5478. return null;
  5479. }
  5480. /**
  5481. * @private
  5482. * 解析一个节点
  5483. */
  5484. function parseNode(node, parent) {
  5485. if (node.localName == "parsererror") {
  5486. throw new Error(node.textContent);
  5487. }
  5488. var xml = new XML(node.localName, parent, node["prefix"], node.namespaceURI, node.nodeName);
  5489. var nodeAttributes = node.attributes;
  5490. var attributes = xml.attributes;
  5491. var length = nodeAttributes.length;
  5492. for (var i = 0; i < length; i++) {
  5493. var attributeNode = nodeAttributes[i];
  5494. var name_1 = attributeNode.name;
  5495. if (name_1.indexOf("xmlns:") == 0) {
  5496. continue;
  5497. }
  5498. attributes[name_1] = attributeNode.value;
  5499. xml["$" + name_1] = attributeNode.value;
  5500. }
  5501. var childNodes = node.childNodes;
  5502. length = childNodes.length;
  5503. var children = xml.children;
  5504. for (var i = 0; i < length; i++) {
  5505. var childNode = childNodes[i];
  5506. var nodeType = childNode.nodeType;
  5507. var childXML = null;
  5508. if (nodeType == 1) {
  5509. childXML = parseNode(childNode, xml);
  5510. }
  5511. else if (nodeType == 3) {
  5512. var text = childNode.textContent.trim();
  5513. if (text) {
  5514. childXML = new XMLText(text, xml);
  5515. }
  5516. }
  5517. if (childXML) {
  5518. children.push(childXML);
  5519. }
  5520. }
  5521. return xml;
  5522. }
  5523. egret.XML = { parse: parse };
  5524. })(web = egret.web || (egret.web = {}));
  5525. })(egret || (egret = {}));
  5526. var egret;
  5527. (function (egret) {
  5528. var web;
  5529. (function (web) {
  5530. /**
  5531. * @private
  5532. */
  5533. var WebDeviceOrientation = (function (_super) {
  5534. __extends(WebDeviceOrientation, _super);
  5535. function WebDeviceOrientation() {
  5536. var _this = _super.apply(this, arguments) || this;
  5537. /**
  5538. * @private
  5539. */
  5540. _this.onChange = function (e) {
  5541. var event = new egret.OrientationEvent(egret.Event.CHANGE);
  5542. event.beta = e.beta;
  5543. event.gamma = e.gamma;
  5544. event.alpha = e.alpha;
  5545. _this.dispatchEvent(event);
  5546. };
  5547. return _this;
  5548. }
  5549. /**
  5550. * @private
  5551. *
  5552. */
  5553. WebDeviceOrientation.prototype.start = function () {
  5554. window.addEventListener("deviceorientation", this.onChange);
  5555. };
  5556. /**
  5557. * @private
  5558. *
  5559. */
  5560. WebDeviceOrientation.prototype.stop = function () {
  5561. window.removeEventListener("deviceorientation", this.onChange);
  5562. };
  5563. return WebDeviceOrientation;
  5564. }(egret.EventDispatcher));
  5565. web.WebDeviceOrientation = WebDeviceOrientation;
  5566. __reflect(WebDeviceOrientation.prototype, "egret.web.WebDeviceOrientation", ["egret.DeviceOrientation"]);
  5567. })(web = egret.web || (egret.web = {}));
  5568. })(egret || (egret = {}));
  5569. egret.DeviceOrientation = egret.web.WebDeviceOrientation;
  5570. var egret;
  5571. (function (egret) {
  5572. var web;
  5573. (function (web) {
  5574. /**
  5575. * @private
  5576. */
  5577. var WebGeolocation = (function (_super) {
  5578. __extends(WebGeolocation, _super);
  5579. /**
  5580. * @private
  5581. */
  5582. function WebGeolocation(option) {
  5583. var _this = _super.call(this) || this;
  5584. /**
  5585. * @private
  5586. */
  5587. _this.onUpdate = function (position) {
  5588. var event = new egret.GeolocationEvent(egret.Event.CHANGE);
  5589. var coords = position.coords;
  5590. event.altitude = coords.altitude;
  5591. event.heading = coords.heading;
  5592. event.accuracy = coords.accuracy;
  5593. event.latitude = coords.latitude;
  5594. event.longitude = coords.longitude;
  5595. event.speed = coords.speed;
  5596. event.altitudeAccuracy = coords.altitudeAccuracy;
  5597. _this.dispatchEvent(event);
  5598. };
  5599. /**
  5600. * @private
  5601. */
  5602. _this.onError = function (error) {
  5603. var errorType = egret.GeolocationEvent.UNAVAILABLE;
  5604. if (error.code == error.PERMISSION_DENIED)
  5605. errorType = egret.GeolocationEvent.PERMISSION_DENIED;
  5606. var event = new egret.GeolocationEvent(egret.IOErrorEvent.IO_ERROR);
  5607. event.errorType = errorType;
  5608. event.errorMessage = error.message;
  5609. _this.dispatchEvent(event);
  5610. };
  5611. _this.geolocation = navigator.geolocation;
  5612. return _this;
  5613. }
  5614. /**
  5615. * @private
  5616. *
  5617. */
  5618. WebGeolocation.prototype.start = function () {
  5619. var geo = this.geolocation;
  5620. if (geo)
  5621. this.watchId = geo.watchPosition(this.onUpdate, this.onError);
  5622. else
  5623. this.onError({
  5624. code: 2,
  5625. message: egret.sys.tr(3004),
  5626. PERMISSION_DENIED: 1,
  5627. POSITION_UNAVAILABLE: 2
  5628. });
  5629. };
  5630. /**
  5631. * @private
  5632. *
  5633. */
  5634. WebGeolocation.prototype.stop = function () {
  5635. var geo = this.geolocation;
  5636. geo.clearWatch(this.watchId);
  5637. };
  5638. return WebGeolocation;
  5639. }(egret.EventDispatcher));
  5640. web.WebGeolocation = WebGeolocation;
  5641. __reflect(WebGeolocation.prototype, "egret.web.WebGeolocation", ["egret.Geolocation"]);
  5642. egret.Geolocation = egret.web.WebGeolocation;
  5643. })(web = egret.web || (egret.web = {}));
  5644. })(egret || (egret = {}));
  5645. var egret;
  5646. (function (egret) {
  5647. var web;
  5648. (function (web) {
  5649. /**
  5650. * @private
  5651. */
  5652. var WebMotion = (function (_super) {
  5653. __extends(WebMotion, _super);
  5654. function WebMotion() {
  5655. var _this = _super.apply(this, arguments) || this;
  5656. /**
  5657. * @private
  5658. */
  5659. _this.onChange = function (e) {
  5660. var event = new egret.MotionEvent(egret.Event.CHANGE);
  5661. var acceleration = {
  5662. x: e.acceleration.x,
  5663. y: e.acceleration.y,
  5664. z: e.acceleration.z
  5665. };
  5666. var accelerationIncludingGravity = {
  5667. x: e.accelerationIncludingGravity.x,
  5668. y: e.accelerationIncludingGravity.y,
  5669. z: e.accelerationIncludingGravity.z
  5670. };
  5671. var rotation = {
  5672. alpha: e.rotationRate.alpha,
  5673. beta: e.rotationRate.beta,
  5674. gamma: e.rotationRate.gamma
  5675. };
  5676. event.acceleration = acceleration;
  5677. event.accelerationIncludingGravity = accelerationIncludingGravity;
  5678. event.rotationRate = rotation;
  5679. _this.dispatchEvent(event);
  5680. };
  5681. return _this;
  5682. }
  5683. /**
  5684. * @private
  5685. *
  5686. */
  5687. WebMotion.prototype.start = function () {
  5688. window.addEventListener("devicemotion", this.onChange);
  5689. };
  5690. /**
  5691. * @private
  5692. *
  5693. */
  5694. WebMotion.prototype.stop = function () {
  5695. window.removeEventListener("devicemotion", this.onChange);
  5696. };
  5697. return WebMotion;
  5698. }(egret.EventDispatcher));
  5699. web.WebMotion = WebMotion;
  5700. __reflect(WebMotion.prototype, "egret.web.WebMotion", ["egret.Motion"]);
  5701. egret.Motion = egret.web.WebMotion;
  5702. })(web = egret.web || (egret.web = {}));
  5703. })(egret || (egret = {}));
  5704. //////////////////////////////////////////////////////////////////////////////////////
  5705. //
  5706. // Copyright (c) 2014-present, Egret Technology.
  5707. // All rights reserved.
  5708. // Redistribution and use in source and binary forms, with or without
  5709. // modification, are permitted provided that the following conditions are met:
  5710. //
  5711. // * Redistributions of source code must retain the above copyright
  5712. // notice, this list of conditions and the following disclaimer.
  5713. // * Redistributions in binary form must reproduce the above copyright
  5714. // notice, this list of conditions and the following disclaimer in the
  5715. // documentation and/or other materials provided with the distribution.
  5716. // * Neither the name of the Egret nor the
  5717. // names of its contributors may be used to endorse or promote products
  5718. // derived from this software without specific prior written permission.
  5719. //
  5720. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  5721. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  5722. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  5723. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  5724. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5725. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  5726. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5727. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5728. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5729. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5730. //
  5731. //////////////////////////////////////////////////////////////////////////////////////
  5732. var egret;
  5733. (function (egret) {
  5734. var web;
  5735. (function (web) {
  5736. if (true) {
  5737. var logFuncs_1;
  5738. function setLogLevel(logType) {
  5739. if (logFuncs_1 == null) {
  5740. logFuncs_1 = {
  5741. "error": console.error,
  5742. "debug": console.debug,
  5743. "warn": console.warn,
  5744. "info": console.info,
  5745. "log": console.log
  5746. };
  5747. }
  5748. switch (logType) {
  5749. case egret.Logger.OFF:
  5750. console.error = function () {
  5751. };
  5752. case egret.Logger.ERROR:
  5753. console.warn = function () {
  5754. };
  5755. case egret.Logger.WARN:
  5756. console.info = function () {
  5757. };
  5758. console.log = function () {
  5759. };
  5760. case egret.Logger.INFO:
  5761. console.debug = function () {
  5762. };
  5763. default:
  5764. break;
  5765. }
  5766. switch (logType) {
  5767. case egret.Logger.ALL:
  5768. case egret.Logger.true:
  5769. console.debug = logFuncs_1["debug"];
  5770. case egret.Logger.INFO:
  5771. console.log = logFuncs_1["log"];
  5772. console.info = logFuncs_1["info"];
  5773. case egret.Logger.WARN:
  5774. console.warn = logFuncs_1["warn"];
  5775. case egret.Logger.ERROR:
  5776. console.error = logFuncs_1["error"];
  5777. default:
  5778. break;
  5779. }
  5780. }
  5781. Object.defineProperty(egret.Logger, "logLevel", {
  5782. set: setLogLevel,
  5783. enumerable: true,
  5784. configurable: true
  5785. });
  5786. }
  5787. })(web = egret.web || (egret.web = {}));
  5788. })(egret || (egret = {}));
  5789. //////////////////////////////////////////////////////////////////////////////////////
  5790. //
  5791. // Copyright (c) 2014-present, Egret Technology.
  5792. // All rights reserved.
  5793. // Redistribution and use in source and binary forms, with or without
  5794. // modification, are permitted provided that the following conditions are met:
  5795. //
  5796. // * Redistributions of source code must retain the above copyright
  5797. // notice, this list of conditions and the following disclaimer.
  5798. // * Redistributions in binary form must reproduce the above copyright
  5799. // notice, this list of conditions and the following disclaimer in the
  5800. // documentation and/or other materials provided with the distribution.
  5801. // * Neither the name of the Egret nor the
  5802. // names of its contributors may be used to endorse or promote products
  5803. // derived from this software without specific prior written permission.
  5804. //
  5805. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  5806. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  5807. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  5808. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  5809. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5810. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  5811. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5812. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5813. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5814. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5815. //
  5816. //////////////////////////////////////////////////////////////////////////////////////
  5817. // There is no HTMLDivElement in webkit for air
  5818. if (true && window['HTMLVideoElement'] == undefined) {
  5819. window['HTMLVideoElement'] = HTMLDivElement;
  5820. }
  5821. var egret;
  5822. (function (egret) {
  5823. var web;
  5824. (function (web) {
  5825. var className = "egret.BitmapData";
  5826. egret.registerClass(HTMLImageElement, className);
  5827. egret.registerClass(HTMLCanvasElement, className);
  5828. egret.registerClass(HTMLVideoElement, className);
  5829. })(web = egret.web || (egret.web = {}));
  5830. })(egret || (egret = {}));
  5831. (function (egret) {
  5832. /**
  5833. * 转换 Image,Canvas,Video 为 Egret 框架内使用的 BitmapData 对象。
  5834. * @param data 需要转换的对象,包括HTMLImageElement|HTMLCanvasElement|HTMLVideoElement
  5835. * @deprecated
  5836. */
  5837. function $toBitmapData(data) {
  5838. data["hashCode"] = data["$hashCode"] = egret.$hashCount++;
  5839. return data;
  5840. }
  5841. egret.$toBitmapData = $toBitmapData;
  5842. })(egret || (egret = {}));
  5843. //////////////////////////////////////////////////////////////////////////////////////
  5844. //
  5845. // Copyright (c) 2014-present, Egret Technology.
  5846. // All rights reserved.
  5847. // Redistribution and use in source and binary forms, with or without
  5848. // modification, are permitted provided that the following conditions are met:
  5849. //
  5850. // * Redistributions of source code must retain the above copyright
  5851. // notice, this list of conditions and the following disclaimer.
  5852. // * Redistributions in binary form must reproduce the above copyright
  5853. // notice, this list of conditions and the following disclaimer in the
  5854. // documentation and/or other materials provided with the distribution.
  5855. // * Neither the name of the Egret nor the
  5856. // names of its contributors may be used to endorse or promote products
  5857. // derived from this software without specific prior written permission.
  5858. //
  5859. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  5860. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  5861. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  5862. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  5863. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5864. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  5865. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5866. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5867. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5868. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5869. //
  5870. //////////////////////////////////////////////////////////////////////////////////////
  5871. var egret;
  5872. (function (egret) {
  5873. var web;
  5874. (function (web) {
  5875. /**
  5876. * @private
  5877. */
  5878. var WebExternalInterface = (function () {
  5879. function WebExternalInterface() {
  5880. }
  5881. /**
  5882. * @private
  5883. * @param functionName
  5884. * @param value
  5885. */
  5886. WebExternalInterface.call = function (functionName, value) {
  5887. };
  5888. /**
  5889. * @private
  5890. * @param functionName
  5891. * @param listener
  5892. */
  5893. WebExternalInterface.addCallback = function (functionName, listener) {
  5894. };
  5895. return WebExternalInterface;
  5896. }());
  5897. web.WebExternalInterface = WebExternalInterface;
  5898. __reflect(WebExternalInterface.prototype, "egret.web.WebExternalInterface", ["egret.ExternalInterface"]);
  5899. egret.ExternalInterface = WebExternalInterface;
  5900. })(web = egret.web || (egret.web = {}));
  5901. })(egret || (egret = {}));
  5902. //////////////////////////////////////////////////////////////////////////////////////
  5903. //
  5904. // Copyright (c) 2014-present, Egret Technology.
  5905. // All rights reserved.
  5906. // Redistribution and use in source and binary forms, with or without
  5907. // modification, are permitted provided that the following conditions are met:
  5908. //
  5909. // * Redistributions of source code must retain the above copyright
  5910. // notice, this list of conditions and the following disclaimer.
  5911. // * Redistributions in binary form must reproduce the above copyright
  5912. // notice, this list of conditions and the following disclaimer in the
  5913. // documentation and/or other materials provided with the distribution.
  5914. // * Neither the name of the Egret nor the
  5915. // names of its contributors may be used to endorse or promote products
  5916. // derived from this software without specific prior written permission.
  5917. //
  5918. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  5919. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  5920. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  5921. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  5922. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5923. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  5924. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5925. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5926. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5927. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5928. //
  5929. //////////////////////////////////////////////////////////////////////////////////////
  5930. var egret;
  5931. (function (egret) {
  5932. var web;
  5933. (function (web) {
  5934. /**
  5935. * @private
  5936. */
  5937. var PrimitiveShader = (function (_super) {
  5938. __extends(PrimitiveShader, _super);
  5939. function PrimitiveShader() {
  5940. var _this = _super.apply(this, arguments) || this;
  5941. _this.fragmentSrc = "precision lowp float;\n" +
  5942. "varying vec2 vTextureCoord;\n" +
  5943. "varying vec4 vColor;\n" +
  5944. "void main(void) {\n" +
  5945. "gl_FragColor = vColor;\n" +
  5946. "}";
  5947. return _this;
  5948. }
  5949. return PrimitiveShader;
  5950. }(web.EgretShader));
  5951. web.PrimitiveShader = PrimitiveShader;
  5952. __reflect(PrimitiveShader.prototype, "egret.web.PrimitiveShader");
  5953. })(web = egret.web || (egret.web = {}));
  5954. })(egret || (egret = {}));
  5955. //////////////////////////////////////////////////////////////////////////////////////
  5956. //
  5957. // Copyright (c) 2014-present, Egret Technology.
  5958. // All rights reserved.
  5959. // Redistribution and use in source and binary forms, with or without
  5960. // modification, are permitted provided that the following conditions are met:
  5961. //
  5962. // * Redistributions of source code must retain the above copyright
  5963. // notice, this list of conditions and the following disclaimer.
  5964. // * Redistributions in binary form must reproduce the above copyright
  5965. // notice, this list of conditions and the following disclaimer in the
  5966. // documentation and/or other materials provided with the distribution.
  5967. // * Neither the name of the Egret nor the
  5968. // names of its contributors may be used to endorse or promote products
  5969. // derived from this software without specific prior written permission.
  5970. //
  5971. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  5972. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  5973. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  5974. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  5975. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  5976. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  5977. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  5978. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  5979. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  5980. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  5981. //
  5982. //////////////////////////////////////////////////////////////////////////////////////
  5983. var egret;
  5984. (function (egret) {
  5985. var web;
  5986. (function (web) {
  5987. /**
  5988. * @private
  5989. */
  5990. var BlurShader = (function (_super) {
  5991. __extends(BlurShader, _super);
  5992. function BlurShader() {
  5993. var _this = _super.apply(this, arguments) || this;
  5994. _this.fragmentSrc = "precision mediump float;" +
  5995. "uniform vec2 blur;" +
  5996. "uniform sampler2D uSampler;" +
  5997. "varying vec2 vTextureCoord;" +
  5998. "uniform vec2 uTextureSize;" +
  5999. "void main()" +
  6000. "{" +
  6001. "const int sampleRadius = 5;" +
  6002. "const int samples = sampleRadius * 2 + 1;" +
  6003. "vec2 blurUv = blur / uTextureSize;" +
  6004. "vec4 color = vec4(0, 0, 0, 0);" +
  6005. "vec2 uv = vec2(0.0, 0.0);" +
  6006. "blurUv /= float(sampleRadius);" +
  6007. "for (int i = -sampleRadius; i <= sampleRadius; i++) {" +
  6008. "uv.x = vTextureCoord.x + float(i) * blurUv.x;" +
  6009. "uv.y = vTextureCoord.y + float(i) * blurUv.y;" +
  6010. "color += texture2D(uSampler, uv);" +
  6011. '}' +
  6012. "color /= float(samples);" +
  6013. "gl_FragColor = color;" +
  6014. "}";
  6015. _this.uniforms = {
  6016. projectionVector: { type: '2f', value: { x: 0, y: 0 }, dirty: true },
  6017. blur: { type: '2f', value: { x: 2, y: 2 }, dirty: true },
  6018. uTextureSize: { type: '2f', value: { x: 100, y: 100 }, dirty: true }
  6019. };
  6020. return _this;
  6021. }
  6022. BlurShader.prototype.setBlur = function (blurX, blurY) {
  6023. var uniform = this.uniforms.blur;
  6024. if (uniform.value.x != blurX || uniform.value.y != blurY) {
  6025. uniform.value.x = blurX;
  6026. uniform.value.y = blurY;
  6027. uniform.dirty = true;
  6028. }
  6029. };
  6030. /**
  6031. * 设置采样材质的尺寸
  6032. */
  6033. BlurShader.prototype.setTextureSize = function (width, height) {
  6034. var uniform = this.uniforms.uTextureSize;
  6035. if (width != uniform.value.x || height != uniform.value.y) {
  6036. uniform.value.x = width;
  6037. uniform.value.y = height;
  6038. uniform.dirty = true;
  6039. }
  6040. };
  6041. return BlurShader;
  6042. }(web.TextureShader));
  6043. web.BlurShader = BlurShader;
  6044. __reflect(BlurShader.prototype, "egret.web.BlurShader");
  6045. })(web = egret.web || (egret.web = {}));
  6046. })(egret || (egret = {}));
  6047. //////////////////////////////////////////////////////////////////////////////////////
  6048. //
  6049. // Copyright (c) 2014-present, Egret Technology.
  6050. // All rights reserved.
  6051. // Redistribution and use in source and binary forms, with or without
  6052. // modification, are permitted provided that the following conditions are met:
  6053. //
  6054. // * Redistributions of source code must retain the above copyright
  6055. // notice, this list of conditions and the following disclaimer.
  6056. // * Redistributions in binary form must reproduce the above copyright
  6057. // notice, this list of conditions and the following disclaimer in the
  6058. // documentation and/or other materials provided with the distribution.
  6059. // * Neither the name of the Egret nor the
  6060. // names of its contributors may be used to endorse or promote products
  6061. // derived from this software without specific prior written permission.
  6062. //
  6063. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  6064. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  6065. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  6066. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  6067. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6068. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  6069. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6070. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6071. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6072. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6073. //
  6074. //////////////////////////////////////////////////////////////////////////////////////
  6075. var egret;
  6076. (function (egret) {
  6077. var web;
  6078. (function (web) {
  6079. /**
  6080. * @private
  6081. */
  6082. var ColorTransformShader = (function (_super) {
  6083. __extends(ColorTransformShader, _super);
  6084. function ColorTransformShader() {
  6085. var _this = _super.apply(this, arguments) || this;
  6086. _this.fragmentSrc = "precision mediump float;\n" +
  6087. "varying vec2 vTextureCoord;\n" +
  6088. "varying vec4 vColor;\n" +
  6089. "uniform mat4 matrix;\n" +
  6090. "uniform vec4 colorAdd;\n" +
  6091. "uniform sampler2D uSampler;\n" +
  6092. "void main(void) {\n" +
  6093. "vec4 texColor = texture2D(uSampler, vTextureCoord);\n" +
  6094. "if(texColor.a > 0.) {" +
  6095. // 抵消预乘的alpha通道
  6096. "texColor = vec4(texColor.rgb / texColor.a, texColor.a);\n" +
  6097. "}" +
  6098. "vec4 locColor = clamp(texColor * matrix + colorAdd, 0., 1.);\n" +
  6099. "gl_FragColor = vColor * vec4(locColor.rgb * locColor.a, locColor.a);\n" +
  6100. "}";
  6101. _this.uniforms = {
  6102. projectionVector: { type: '2f', value: { x: 0, y: 0 }, dirty: true },
  6103. matrix: { type: 'mat4', value: [1, 0, 0, 0,
  6104. 0, 1, 0, 0,
  6105. 0, 0, 1, 0,
  6106. 0, 0, 0, 1], dirty: true },
  6107. colorAdd: { type: '4f', value: { x: 0, y: 0, z: 0, w: 0 }, dirty: true }
  6108. };
  6109. return _this;
  6110. }
  6111. ColorTransformShader.prototype.setMatrix = function (matrix) {
  6112. var uniform = this.uniforms.matrix;
  6113. if (uniform.value[0] != matrix[0] ||
  6114. uniform.value[0] != matrix[0] ||
  6115. uniform.value[1] != matrix[1] ||
  6116. uniform.value[2] != matrix[2] ||
  6117. uniform.value[3] != matrix[3] ||
  6118. uniform.value[4] != matrix[5] ||
  6119. uniform.value[5] != matrix[6] ||
  6120. uniform.value[6] != matrix[7] ||
  6121. uniform.value[7] != matrix[8] ||
  6122. uniform.value[8] != matrix[10] ||
  6123. uniform.value[9] != matrix[11] ||
  6124. uniform.value[10] != matrix[12] ||
  6125. uniform.value[11] != matrix[13] ||
  6126. uniform.value[12] != matrix[15] ||
  6127. uniform.value[13] != matrix[16] ||
  6128. uniform.value[14] != matrix[17] ||
  6129. uniform.value[15] != matrix[18]) {
  6130. uniform.value[0] = matrix[0];
  6131. uniform.value[1] = matrix[1];
  6132. uniform.value[2] = matrix[2];
  6133. uniform.value[3] = matrix[3];
  6134. uniform.value[4] = matrix[5];
  6135. uniform.value[5] = matrix[6];
  6136. uniform.value[6] = matrix[7];
  6137. uniform.value[7] = matrix[8];
  6138. uniform.value[8] = matrix[10];
  6139. uniform.value[9] = matrix[11];
  6140. uniform.value[10] = matrix[12];
  6141. uniform.value[11] = matrix[13];
  6142. uniform.value[12] = matrix[15];
  6143. uniform.value[13] = matrix[16];
  6144. uniform.value[14] = matrix[17];
  6145. uniform.value[15] = matrix[18];
  6146. uniform.dirty = true;
  6147. }
  6148. var uniform2 = this.uniforms.colorAdd;
  6149. if (uniform2.value.x != matrix[4] / 255.0 ||
  6150. uniform2.value.y != matrix[9] / 255.0 ||
  6151. uniform2.value.z != matrix[14] / 255.0 ||
  6152. uniform2.value.w != matrix[19] / 255.0) {
  6153. uniform2.value.x = matrix[4] / 255.0;
  6154. uniform2.value.y = matrix[9] / 255.0;
  6155. uniform2.value.z = matrix[14] / 255.0;
  6156. uniform2.value.w = matrix[19] / 255.0;
  6157. uniform2.dirty = true;
  6158. }
  6159. };
  6160. return ColorTransformShader;
  6161. }(web.TextureShader));
  6162. web.ColorTransformShader = ColorTransformShader;
  6163. __reflect(ColorTransformShader.prototype, "egret.web.ColorTransformShader");
  6164. })(web = egret.web || (egret.web = {}));
  6165. })(egret || (egret = {}));
  6166. //////////////////////////////////////////////////////////////////////////////////////
  6167. //
  6168. // Copyright (c) 2014-present, Egret Technology.
  6169. // All rights reserved.
  6170. // Redistribution and use in source and binary forms, with or without
  6171. // modification, are permitted provided that the following conditions are met:
  6172. //
  6173. // * Redistributions of source code must retain the above copyright
  6174. // notice, this list of conditions and the following disclaimer.
  6175. // * Redistributions in binary form must reproduce the above copyright
  6176. // notice, this list of conditions and the following disclaimer in the
  6177. // documentation and/or other materials provided with the distribution.
  6178. // * Neither the name of the Egret nor the
  6179. // names of its contributors may be used to endorse or promote products
  6180. // derived from this software without specific prior written permission.
  6181. //
  6182. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  6183. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  6184. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  6185. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  6186. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6187. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  6188. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6189. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6190. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6191. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6192. //
  6193. //////////////////////////////////////////////////////////////////////////////////////
  6194. var egret;
  6195. (function (egret) {
  6196. var web;
  6197. (function (web) {
  6198. /**
  6199. * @private
  6200. */
  6201. var GlowShader = (function (_super) {
  6202. __extends(GlowShader, _super);
  6203. function GlowShader() {
  6204. var _this = _super.apply(this, arguments) || this;
  6205. _this.fragmentSrc = [
  6206. 'precision mediump float;',
  6207. 'varying vec2 vTextureCoord;',
  6208. 'uniform sampler2D uSampler;',
  6209. 'uniform float distance;',
  6210. 'uniform float angle;',
  6211. 'uniform vec4 color;',
  6212. 'uniform float alpha;',
  6213. 'uniform float blurX;',
  6214. 'uniform float blurY;',
  6215. // 'uniform vec4 quality;',
  6216. 'uniform float strength;',
  6217. 'uniform float inner;',
  6218. 'uniform float knockout;',
  6219. 'uniform float hideObject;',
  6220. "uniform vec2 uTextureSize;" +
  6221. 'vec2 px = vec2(1.0 / uTextureSize.x, 1.0 / uTextureSize.y);',
  6222. 'float random(vec3 scale, float seed)',
  6223. '{',
  6224. 'return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);',
  6225. '}',
  6226. 'void main(void) {',
  6227. // TODO 自动调节采样次数?
  6228. 'const float linearSamplingTimes = 7.0;',
  6229. 'const float circleSamplingTimes = 12.0;',
  6230. 'vec4 ownColor = texture2D(uSampler, vTextureCoord);',
  6231. 'vec4 curColor;',
  6232. 'float totalAlpha = 0.0;',
  6233. 'float maxTotalAlpha = 0.0;',
  6234. 'float curDistanceX = 0.0;',
  6235. 'float curDistanceY = 0.0;',
  6236. 'float offsetX = distance * cos(angle) * px.x;',
  6237. 'float offsetY = distance * sin(angle) * px.y;',
  6238. 'const float PI = 3.14159265358979323846264;',
  6239. 'float cosAngle;',
  6240. 'float sinAngle;',
  6241. 'float offset = PI * 2.0 / circleSamplingTimes * random(vec3(12.9898, 78.233, 151.7182), 0.0);',
  6242. 'float stepX = blurX * px.x / linearSamplingTimes;',
  6243. 'float stepY = blurY * px.y / linearSamplingTimes;',
  6244. 'for (float a = 0.0; a <= PI * 2.0; a += PI * 2.0 / circleSamplingTimes) {',
  6245. 'cosAngle = cos(a + offset);',
  6246. 'sinAngle = sin(a + offset);',
  6247. 'for (float i = 1.0; i <= linearSamplingTimes; i++) {',
  6248. 'curDistanceX = i * stepX * cosAngle;',
  6249. 'curDistanceY = i * stepY * sinAngle;',
  6250. 'curColor = texture2D(uSampler, vec2(vTextureCoord.x + curDistanceX - offsetX, vTextureCoord.y + curDistanceY + offsetY));',
  6251. 'totalAlpha += (linearSamplingTimes - i) * curColor.a;',
  6252. 'maxTotalAlpha += (linearSamplingTimes - i);',
  6253. '}',
  6254. '}',
  6255. 'ownColor.a = max(ownColor.a, 0.0001);',
  6256. 'ownColor.rgb = ownColor.rgb / ownColor.a;',
  6257. 'float outerGlowAlpha = (totalAlpha / maxTotalAlpha) * strength * alpha * (1. - inner) * max(min(hideObject, knockout), 1. - ownColor.a);',
  6258. 'float innerGlowAlpha = ((maxTotalAlpha - totalAlpha) / maxTotalAlpha) * strength * alpha * inner * ownColor.a;',
  6259. 'ownColor.a = max(ownColor.a * knockout * (1. - hideObject), 0.0001);',
  6260. 'vec3 mix1 = mix(ownColor.rgb, color.rgb, innerGlowAlpha / (innerGlowAlpha + ownColor.a));',
  6261. 'vec3 mix2 = mix(mix1, color.rgb, outerGlowAlpha / (innerGlowAlpha + ownColor.a + outerGlowAlpha));',
  6262. 'float resultAlpha = min(ownColor.a + outerGlowAlpha + innerGlowAlpha, 1.);',
  6263. 'gl_FragColor = vec4(mix2 * resultAlpha, resultAlpha);',
  6264. '}',
  6265. ].join("\n");
  6266. _this.uniforms = {
  6267. projectionVector: { type: '2f', value: { x: 0, y: 0 }, dirty: true },
  6268. distance: { type: '1f', value: 15, dirty: true },
  6269. angle: { type: '1f', value: 1, dirty: true },
  6270. color: { type: '4f', value: { x: 1, y: 0, z: 0, w: 0 }, dirty: true },
  6271. alpha: { type: '1f', value: 1, dirty: true },
  6272. blurX: { type: '1f', value: 1, dirty: true },
  6273. blurY: { type: '1f', value: 1, dirty: true },
  6274. strength: { type: '1f', value: 1, dirty: true },
  6275. inner: { type: '1f', value: 1, dirty: true },
  6276. knockout: { type: '1f', value: 1, dirty: true },
  6277. hideObject: { type: '1f', value: 0, dirty: true },
  6278. uTextureSize: { type: '2f', value: { x: 100, y: 100 }, dirty: true }
  6279. };
  6280. return _this;
  6281. }
  6282. GlowShader.prototype.setDistance = function (distance) {
  6283. var uniform = this.uniforms.distance;
  6284. if (uniform.value != distance) {
  6285. uniform.value = distance;
  6286. uniform.dirty = true;
  6287. }
  6288. };
  6289. GlowShader.prototype.setAngle = function (angle) {
  6290. var uniform = this.uniforms.angle;
  6291. if (uniform.value != angle) {
  6292. uniform.value = angle;
  6293. uniform.dirty = true;
  6294. }
  6295. };
  6296. GlowShader.prototype.setColor = function (red, green, blue) {
  6297. var uniform = this.uniforms.color;
  6298. if (uniform.value.x != red || uniform.value.y != green || uniform.value.z != blue) {
  6299. uniform.value.x = red;
  6300. uniform.value.y = green;
  6301. uniform.value.z = blue;
  6302. uniform.dirty = true;
  6303. }
  6304. };
  6305. GlowShader.prototype.setAlpha = function (alpha) {
  6306. var uniform = this.uniforms.alpha;
  6307. if (uniform.value != alpha) {
  6308. uniform.value = alpha;
  6309. uniform.dirty = true;
  6310. }
  6311. };
  6312. GlowShader.prototype.setBlurX = function (blurX) {
  6313. var uniform = this.uniforms.blurX;
  6314. if (uniform.value != blurX) {
  6315. uniform.value = blurX;
  6316. uniform.dirty = true;
  6317. }
  6318. };
  6319. GlowShader.prototype.setBlurY = function (blurY) {
  6320. var uniform = this.uniforms.blurY;
  6321. if (uniform.value != blurY) {
  6322. uniform.value = blurY;
  6323. uniform.dirty = true;
  6324. }
  6325. };
  6326. GlowShader.prototype.setStrength = function (strength) {
  6327. var uniform = this.uniforms.strength;
  6328. if (uniform.value != strength) {
  6329. uniform.value = strength;
  6330. uniform.dirty = true;
  6331. }
  6332. };
  6333. GlowShader.prototype.setInner = function (inner) {
  6334. var uniform = this.uniforms.inner;
  6335. if (uniform.value != inner) {
  6336. uniform.value = inner;
  6337. uniform.dirty = true;
  6338. }
  6339. };
  6340. GlowShader.prototype.setKnockout = function (knockout) {
  6341. var uniform = this.uniforms.knockout;
  6342. if (uniform.value != knockout) {
  6343. uniform.value = knockout;
  6344. uniform.dirty = true;
  6345. }
  6346. };
  6347. GlowShader.prototype.setHideObject = function (hideObject) {
  6348. var uniform = this.uniforms.hideObject;
  6349. if (uniform.value != hideObject) {
  6350. uniform.value = hideObject;
  6351. uniform.dirty = true;
  6352. }
  6353. };
  6354. /**
  6355. * 设置采样材质的尺寸
  6356. */
  6357. GlowShader.prototype.setTextureSize = function (width, height) {
  6358. var uniform = this.uniforms.uTextureSize;
  6359. if (width != uniform.value.x || height != uniform.value.y) {
  6360. uniform.value.x = width;
  6361. uniform.value.y = height;
  6362. uniform.dirty = true;
  6363. }
  6364. };
  6365. return GlowShader;
  6366. }(web.TextureShader));
  6367. web.GlowShader = GlowShader;
  6368. __reflect(GlowShader.prototype, "egret.web.GlowShader");
  6369. })(web = egret.web || (egret.web = {}));
  6370. })(egret || (egret = {}));
  6371. //////////////////////////////////////////////////////////////////////////////////////
  6372. //
  6373. // Copyright (c) 2014-present, Egret Technology.
  6374. // All rights reserved.
  6375. // Redistribution and use in source and binary forms, with or without
  6376. // modification, are permitted provided that the following conditions are met:
  6377. //
  6378. // * Redistributions of source code must retain the above copyright
  6379. // notice, this list of conditions and the following disclaimer.
  6380. // * Redistributions in binary form must reproduce the above copyright
  6381. // notice, this list of conditions and the following disclaimer in the
  6382. // documentation and/or other materials provided with the distribution.
  6383. // * Neither the name of the Egret nor the
  6384. // names of its contributors may be used to endorse or promote products
  6385. // derived from this software without specific prior written permission.
  6386. //
  6387. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  6388. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  6389. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  6390. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  6391. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6392. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  6393. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6394. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6395. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6396. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6397. //
  6398. //////////////////////////////////////////////////////////////////////////////////////
  6399. var egret;
  6400. (function (egret) {
  6401. var web;
  6402. (function (web) {
  6403. /**
  6404. *
  6405. * @private
  6406. */
  6407. var WebGLShaderManager = (function () {
  6408. function WebGLShaderManager(gl) {
  6409. this.gl = null;
  6410. this.maxAttibs = 10;
  6411. this.attribState = [];
  6412. this.tempAttribState = [];
  6413. this.currentShader = null;
  6414. this.defaultShader = null;
  6415. this.primitiveShader = null;
  6416. this.colorTransformShader = null;
  6417. this.blurShader = null;
  6418. this.glowShader = null;
  6419. for (var i = 0; i < this.maxAttibs; i++) {
  6420. this.attribState[i] = false;
  6421. }
  6422. this.setContext(gl);
  6423. }
  6424. WebGLShaderManager.prototype.setContext = function (gl) {
  6425. this.gl = gl;
  6426. this.primitiveShader = new web.PrimitiveShader(gl);
  6427. this.defaultShader = new web.TextureShader(gl);
  6428. this.colorTransformShader = new web.ColorTransformShader(gl);
  6429. this.glowShader = new web.GlowShader(gl);
  6430. this.blurShader = new web.BlurShader(gl);
  6431. this.primitiveShader.init();
  6432. this.defaultShader.init();
  6433. this.colorTransformShader.init();
  6434. this.blurShader.init();
  6435. this.glowShader.init();
  6436. };
  6437. WebGLShaderManager.prototype.activateShader = function (shader, stride) {
  6438. if (this.currentShader != shader) {
  6439. this.gl.useProgram(shader.program);
  6440. this.setAttribs(shader.attributes);
  6441. shader.setAttribPointer(stride);
  6442. this.currentShader = shader;
  6443. }
  6444. };
  6445. WebGLShaderManager.prototype.setAttribs = function (attribs) {
  6446. var i;
  6447. var l;
  6448. l = this.tempAttribState.length;
  6449. for (i = 0; i < l; i++) {
  6450. this.tempAttribState[i] = false;
  6451. }
  6452. l = attribs.length;
  6453. for (i = 0; i < l; i++) {
  6454. var attribId = attribs[i];
  6455. this.tempAttribState[attribId] = true;
  6456. }
  6457. var gl = this.gl;
  6458. l = this.attribState.length;
  6459. for (i = 0; i < l; i++) {
  6460. if (this.attribState[i] !== this.tempAttribState[i]) {
  6461. this.attribState[i] = this.tempAttribState[i];
  6462. if (this.tempAttribState[i]) {
  6463. gl.enableVertexAttribArray(i);
  6464. }
  6465. else {
  6466. gl.disableVertexAttribArray(i);
  6467. }
  6468. }
  6469. }
  6470. };
  6471. return WebGLShaderManager;
  6472. }());
  6473. web.WebGLShaderManager = WebGLShaderManager;
  6474. __reflect(WebGLShaderManager.prototype, "egret.web.WebGLShaderManager");
  6475. })(web = egret.web || (egret.web = {}));
  6476. })(egret || (egret = {}));
  6477. //////////////////////////////////////////////////////////////////////////////////////
  6478. //
  6479. // Copyright (c) 2014-present, Egret Technology.
  6480. // All rights reserved.
  6481. // Redistribution and use in source and binary forms, with or without
  6482. // modification, are permitted provided that the following conditions are met:
  6483. //
  6484. // * Redistributions of source code must retain the above copyright
  6485. // notice, this list of conditions and the following disclaimer.
  6486. // * Redistributions in binary form must reproduce the above copyright
  6487. // notice, this list of conditions and the following disclaimer in the
  6488. // documentation and/or other materials provided with the distribution.
  6489. // * Neither the name of the Egret nor the
  6490. // names of its contributors may be used to endorse or promote products
  6491. // derived from this software without specific prior written permission.
  6492. //
  6493. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  6494. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  6495. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  6496. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  6497. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6498. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  6499. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6500. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6501. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6502. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6503. //
  6504. //////////////////////////////////////////////////////////////////////////////////////
  6505. var egret;
  6506. (function (egret) {
  6507. var web;
  6508. (function (web) {
  6509. /**
  6510. * @private
  6511. * 绘制指令管理器
  6512. * 用来维护drawData数组
  6513. */
  6514. var WebGLDrawCmdManager = (function () {
  6515. function WebGLDrawCmdManager() {
  6516. /**
  6517. * 用于缓存绘制命令的数组
  6518. */
  6519. this.drawData = [];
  6520. this.drawDataLen = 0;
  6521. }
  6522. /**
  6523. * 压入绘制矩形指令
  6524. */
  6525. WebGLDrawCmdManager.prototype.pushDrawRect = function () {
  6526. if (this.drawDataLen == 0 || this.drawData[this.drawDataLen - 1].type != 1 /* RECT */) {
  6527. var data = this.drawData[this.drawDataLen] || {};
  6528. data.type = 1 /* RECT */;
  6529. data.count = 0;
  6530. this.drawData[this.drawDataLen] = data;
  6531. this.drawDataLen++;
  6532. }
  6533. this.drawData[this.drawDataLen - 1].count += 2;
  6534. };
  6535. /**
  6536. * 压入绘制texture指令
  6537. */
  6538. WebGLDrawCmdManager.prototype.pushDrawTexture = function (texture, count, filter, textureWidth, textureHeight) {
  6539. if (count === void 0) { count = 2; }
  6540. if (filter) {
  6541. // 目前有滤镜的情况下不会合并绘制
  6542. var data = this.drawData[this.drawDataLen] || {};
  6543. data.type = 0 /* TEXTURE */;
  6544. data.texture = texture;
  6545. data.filter = filter;
  6546. data.count = count;
  6547. data.textureWidth = textureWidth;
  6548. data.textureHeight = textureHeight;
  6549. this.drawData[this.drawDataLen] = data;
  6550. this.drawDataLen++;
  6551. }
  6552. else {
  6553. if (this.drawDataLen == 0 || this.drawData[this.drawDataLen - 1].type != 0 /* TEXTURE */ || texture != this.drawData[this.drawDataLen - 1].texture || this.drawData[this.drawDataLen - 1].filter) {
  6554. var data = this.drawData[this.drawDataLen] || {};
  6555. data.type = 0 /* TEXTURE */;
  6556. data.texture = texture;
  6557. data.count = 0;
  6558. this.drawData[this.drawDataLen] = data;
  6559. this.drawDataLen++;
  6560. }
  6561. this.drawData[this.drawDataLen - 1].count += count;
  6562. }
  6563. };
  6564. /**
  6565. * 压入pushMask指令
  6566. */
  6567. WebGLDrawCmdManager.prototype.pushPushMask = function (count) {
  6568. if (count === void 0) { count = 1; }
  6569. var data = this.drawData[this.drawDataLen] || {};
  6570. data.type = 2 /* PUSH_MASK */;
  6571. data.count = count * 2;
  6572. this.drawData[this.drawDataLen] = data;
  6573. this.drawDataLen++;
  6574. };
  6575. /**
  6576. * 压入popMask指令
  6577. */
  6578. WebGLDrawCmdManager.prototype.pushPopMask = function (count) {
  6579. if (count === void 0) { count = 1; }
  6580. var data = this.drawData[this.drawDataLen] || {};
  6581. data.type = 3 /* POP_MASK */;
  6582. data.count = count * 2;
  6583. this.drawData[this.drawDataLen] = data;
  6584. this.drawDataLen++;
  6585. };
  6586. /**
  6587. * 压入混色指令
  6588. */
  6589. WebGLDrawCmdManager.prototype.pushSetBlend = function (value) {
  6590. var len = this.drawDataLen;
  6591. // 有无遍历到有效绘图操作
  6592. var drawState = false;
  6593. for (var i = len - 1; i >= 0; i--) {
  6594. var data = this.drawData[i];
  6595. if (data) {
  6596. if (data.type == 0 /* TEXTURE */ || data.type == 1 /* RECT */) {
  6597. drawState = true;
  6598. }
  6599. // 如果与上一次blend操作之间无有效绘图,上一次操作无效
  6600. if (!drawState && data.type == 4 /* BLEND */) {
  6601. this.drawData.splice(i, 1);
  6602. this.drawDataLen--;
  6603. continue;
  6604. }
  6605. // 如果与上一次blend操作重复,本次操作无效
  6606. if (data.type == 4 /* BLEND */) {
  6607. if (data.value == value) {
  6608. return;
  6609. }
  6610. else {
  6611. break;
  6612. }
  6613. }
  6614. }
  6615. }
  6616. var _data = this.drawData[this.drawDataLen] || {};
  6617. _data.type = 4 /* BLEND */;
  6618. _data.value = value;
  6619. this.drawData[this.drawDataLen] = _data;
  6620. this.drawDataLen++;
  6621. };
  6622. /*
  6623. * 压入resize render target命令
  6624. */
  6625. WebGLDrawCmdManager.prototype.pushResize = function (buffer, width, height) {
  6626. var data = this.drawData[this.drawDataLen] || {};
  6627. data.type = 5 /* RESIZE_TARGET */;
  6628. data.buffer = buffer;
  6629. data.width = width;
  6630. data.height = height;
  6631. this.drawData[this.drawDataLen] = data;
  6632. this.drawDataLen++;
  6633. };
  6634. /*
  6635. * 压入clear color命令
  6636. */
  6637. WebGLDrawCmdManager.prototype.pushClearColor = function () {
  6638. var data = this.drawData[this.drawDataLen] || {};
  6639. data.type = 6 /* CLEAR_COLOR */;
  6640. this.drawData[this.drawDataLen] = data;
  6641. this.drawDataLen++;
  6642. };
  6643. /**
  6644. * 压入激活buffer命令
  6645. */
  6646. WebGLDrawCmdManager.prototype.pushActivateBuffer = function (buffer) {
  6647. var len = this.drawDataLen;
  6648. // 有无遍历到有效绘图操作
  6649. var drawState = false;
  6650. for (var i = len - 1; i >= 0; i--) {
  6651. var data = this.drawData[i];
  6652. if (data) {
  6653. if (data.type != 4 /* BLEND */ && data.type != 7 /* ACT_BUFFER */) {
  6654. drawState = true;
  6655. }
  6656. // 如果与上一次buffer操作之间无有效绘图,上一次操作无效
  6657. if (!drawState && data.type == 7 /* ACT_BUFFER */) {
  6658. this.drawData.splice(i, 1);
  6659. this.drawDataLen--;
  6660. continue;
  6661. }
  6662. }
  6663. }
  6664. var _data = this.drawData[this.drawDataLen] || {};
  6665. _data.type = 7 /* ACT_BUFFER */;
  6666. _data.buffer = buffer;
  6667. _data.width = buffer.rootRenderTarget.width;
  6668. _data.height = buffer.rootRenderTarget.height;
  6669. this.drawData[this.drawDataLen] = _data;
  6670. this.drawDataLen++;
  6671. };
  6672. /*
  6673. * 压入enabel scissor命令
  6674. */
  6675. WebGLDrawCmdManager.prototype.pushEnableScissor = function (x, y, width, height) {
  6676. var data = this.drawData[this.drawDataLen] || {};
  6677. data.type = 8 /* ENABLE_SCISSOR */;
  6678. data.x = x;
  6679. data.y = y;
  6680. data.width = width;
  6681. data.height = height;
  6682. this.drawData[this.drawDataLen] = data;
  6683. this.drawDataLen++;
  6684. };
  6685. /*
  6686. * 压入disable scissor命令
  6687. */
  6688. WebGLDrawCmdManager.prototype.pushDisableScissor = function () {
  6689. var data = this.drawData[this.drawDataLen] || {};
  6690. data.type = 9 /* DISABLE_SCISSOR */;
  6691. this.drawData[this.drawDataLen] = data;
  6692. this.drawDataLen++;
  6693. };
  6694. /**
  6695. * 清空命令数组
  6696. */
  6697. WebGLDrawCmdManager.prototype.clear = function () {
  6698. for (var i = 0; i < this.drawDataLen; i++) {
  6699. var data = this.drawData[i];
  6700. data.type = 0;
  6701. data.count = 0;
  6702. data.texture = null;
  6703. data.filter = null;
  6704. data.uv = null;
  6705. data.value = "";
  6706. data.buffer = null;
  6707. data.width = 0;
  6708. data.height = 0;
  6709. }
  6710. this.drawDataLen = 0;
  6711. };
  6712. return WebGLDrawCmdManager;
  6713. }());
  6714. web.WebGLDrawCmdManager = WebGLDrawCmdManager;
  6715. __reflect(WebGLDrawCmdManager.prototype, "egret.web.WebGLDrawCmdManager");
  6716. })(web = egret.web || (egret.web = {}));
  6717. })(egret || (egret = {}));
  6718. //////////////////////////////////////////////////////////////////////////////////////
  6719. //
  6720. // Copyright (c) 2014-present, Egret Technology.
  6721. // All rights reserved.
  6722. // Redistribution and use in source and binary forms, with or without
  6723. // modification, are permitted provided that the following conditions are met:
  6724. //
  6725. // * Redistributions of source code must retain the above copyright
  6726. // notice, this list of conditions and the following disclaimer.
  6727. // * Redistributions in binary form must reproduce the above copyright
  6728. // notice, this list of conditions and the following disclaimer in the
  6729. // documentation and/or other materials provided with the distribution.
  6730. // * Neither the name of the Egret nor the
  6731. // names of its contributors may be used to endorse or promote products
  6732. // derived from this software without specific prior written permission.
  6733. //
  6734. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  6735. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  6736. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  6737. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  6738. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6739. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  6740. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6741. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6742. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6743. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6744. //
  6745. //////////////////////////////////////////////////////////////////////////////////////
  6746. var egret;
  6747. (function (egret) {
  6748. var web;
  6749. (function (web) {
  6750. /**
  6751. * @private
  6752. * 顶点数组管理对象
  6753. * 用来维护顶点数组
  6754. */
  6755. var WebGLVertexArrayObject = (function () {
  6756. function WebGLVertexArrayObject() {
  6757. this.size = 2000;
  6758. this.vertexMaxSize = this.size * 4;
  6759. this.indicesMaxSize = this.size * 6;
  6760. this.vertSize = 5;
  6761. this.vertices = null;
  6762. this.indices = null;
  6763. this.indicesForMesh = null;
  6764. this.vertexIndex = 0;
  6765. this.indexIndex = 0;
  6766. this.hasMesh = false;
  6767. var numVerts = this.vertexMaxSize * this.vertSize;
  6768. var numIndices = this.indicesMaxSize;
  6769. this.vertices = new Float32Array(numVerts);
  6770. this.indices = new Uint16Array(numIndices);
  6771. this.indicesForMesh = new Uint16Array(numIndices);
  6772. for (var i = 0, j = 0; i < numIndices; i += 6, j += 4) {
  6773. this.indices[i + 0] = j + 0;
  6774. this.indices[i + 1] = j + 1;
  6775. this.indices[i + 2] = j + 2;
  6776. this.indices[i + 3] = j + 0;
  6777. this.indices[i + 4] = j + 2;
  6778. this.indices[i + 5] = j + 3;
  6779. }
  6780. }
  6781. /**
  6782. * 是否达到最大缓存数量
  6783. */
  6784. WebGLVertexArrayObject.prototype.reachMaxSize = function (vertexCount, indexCount) {
  6785. if (vertexCount === void 0) { vertexCount = 4; }
  6786. if (indexCount === void 0) { indexCount = 6; }
  6787. return this.vertexIndex > this.vertexMaxSize - vertexCount || this.indexIndex > this.indicesMaxSize - indexCount;
  6788. };
  6789. /**
  6790. * 获取缓存完成的顶点数组
  6791. */
  6792. WebGLVertexArrayObject.prototype.getVertices = function () {
  6793. var view = this.vertices.subarray(0, this.vertexIndex * this.vertSize);
  6794. return view;
  6795. };
  6796. /**
  6797. * 获取缓存完成的索引数组
  6798. */
  6799. WebGLVertexArrayObject.prototype.getIndices = function () {
  6800. return this.indices;
  6801. };
  6802. /**
  6803. * 获取缓存完成的mesh索引数组
  6804. */
  6805. WebGLVertexArrayObject.prototype.getMeshIndices = function () {
  6806. return this.indicesForMesh;
  6807. };
  6808. /**
  6809. * 切换成mesh索引缓存方式
  6810. */
  6811. WebGLVertexArrayObject.prototype.changeToMeshIndices = function () {
  6812. if (!this.hasMesh) {
  6813. // 拷贝默认index信息到for mesh中
  6814. for (var i = 0, l = this.indexIndex; i < l; ++i) {
  6815. this.indicesForMesh[i] = this.indices[i];
  6816. }
  6817. this.hasMesh = true;
  6818. }
  6819. };
  6820. WebGLVertexArrayObject.prototype.isMesh = function () {
  6821. return this.hasMesh;
  6822. };
  6823. /**
  6824. * 默认构成矩形
  6825. */
  6826. // private defaultMeshVertices = [0, 0, 1, 0, 1, 1, 0, 1];
  6827. // private defaultMeshUvs = [
  6828. // 0, 0,
  6829. // 1, 0,
  6830. // 1, 1,
  6831. // 0, 1
  6832. // ];
  6833. // private defaultMeshIndices = [0, 1, 2, 0, 2, 3];
  6834. /**
  6835. * 缓存一组顶点
  6836. */
  6837. WebGLVertexArrayObject.prototype.cacheArrays = function (transform, alpha, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, textureSourceWidth, textureSourceHeight, meshUVs, meshVertices, meshIndices) {
  6838. //计算出绘制矩阵,之后把矩阵还原回之前的
  6839. var locWorldTransform = transform;
  6840. var originalA = locWorldTransform.a;
  6841. var originalB = locWorldTransform.b;
  6842. var originalC = locWorldTransform.c;
  6843. var originalD = locWorldTransform.d;
  6844. var originalTx = locWorldTransform.tx;
  6845. var originalTy = locWorldTransform.ty;
  6846. if (destX != 0 || destY != 0) {
  6847. locWorldTransform.append(1, 0, 0, 1, destX, destY);
  6848. }
  6849. if (sourceWidth / destWidth != 1 || sourceHeight / destHeight != 1) {
  6850. locWorldTransform.append(destWidth / sourceWidth, 0, 0, destHeight / sourceHeight, 0, 0);
  6851. }
  6852. var a = locWorldTransform.a;
  6853. var b = locWorldTransform.b;
  6854. var c = locWorldTransform.c;
  6855. var d = locWorldTransform.d;
  6856. var tx = locWorldTransform.tx;
  6857. var ty = locWorldTransform.ty;
  6858. locWorldTransform.a = originalA;
  6859. locWorldTransform.b = originalB;
  6860. locWorldTransform.c = originalC;
  6861. locWorldTransform.d = originalD;
  6862. locWorldTransform.tx = originalTx;
  6863. locWorldTransform.ty = originalTy;
  6864. if (meshVertices) {
  6865. // 计算索引位置与赋值
  6866. var vertices = this.vertices;
  6867. var index = this.vertexIndex * this.vertSize;
  6868. // 缓存顶点数组
  6869. var i = 0, iD = 0, l = 0;
  6870. var u = 0, v = 0, x = 0, y = 0;
  6871. for (i = 0, l = meshUVs.length; i < l; i += 2) {
  6872. iD = i * 5 / 2;
  6873. x = meshVertices[i];
  6874. y = meshVertices[i + 1];
  6875. u = meshUVs[i];
  6876. v = meshUVs[i + 1];
  6877. // xy
  6878. vertices[index + iD + 0] = a * x + c * y + tx;
  6879. vertices[index + iD + 1] = b * x + d * y + ty;
  6880. // uv
  6881. vertices[index + iD + 2] = (sourceX + u * sourceWidth) / textureSourceWidth;
  6882. vertices[index + iD + 3] = (sourceY + v * sourceHeight) / textureSourceHeight;
  6883. // alpha
  6884. vertices[index + iD + 4] = alpha;
  6885. }
  6886. // 缓存索引数组
  6887. if (this.hasMesh) {
  6888. for (var i_1 = 0, l_1 = meshIndices.length; i_1 < l_1; ++i_1) {
  6889. this.indicesForMesh[this.indexIndex + i_1] = meshIndices[i_1] + this.vertexIndex;
  6890. }
  6891. }
  6892. this.vertexIndex += meshUVs.length / 2;
  6893. this.indexIndex += meshIndices.length;
  6894. }
  6895. else {
  6896. var width = textureSourceWidth;
  6897. var height = textureSourceHeight;
  6898. var w = sourceWidth;
  6899. var h = sourceHeight;
  6900. sourceX = sourceX / width;
  6901. sourceY = sourceY / height;
  6902. sourceWidth = sourceWidth / width;
  6903. sourceHeight = sourceHeight / height;
  6904. var vertices = this.vertices;
  6905. var index = this.vertexIndex * this.vertSize;
  6906. // xy
  6907. vertices[index++] = tx;
  6908. vertices[index++] = ty;
  6909. // uv
  6910. vertices[index++] = sourceX;
  6911. vertices[index++] = sourceY;
  6912. // alpha
  6913. vertices[index++] = alpha;
  6914. // xy
  6915. vertices[index++] = a * w + tx;
  6916. vertices[index++] = b * w + ty;
  6917. // uv
  6918. vertices[index++] = sourceWidth + sourceX;
  6919. vertices[index++] = sourceY;
  6920. // alpha
  6921. vertices[index++] = alpha;
  6922. // xy
  6923. vertices[index++] = a * w + c * h + tx;
  6924. vertices[index++] = d * h + b * w + ty;
  6925. // uv
  6926. vertices[index++] = sourceWidth + sourceX;
  6927. vertices[index++] = sourceHeight + sourceY;
  6928. // alpha
  6929. vertices[index++] = alpha;
  6930. // xy
  6931. vertices[index++] = c * h + tx;
  6932. vertices[index++] = d * h + ty;
  6933. // uv
  6934. vertices[index++] = sourceX;
  6935. vertices[index++] = sourceHeight + sourceY;
  6936. // alpha
  6937. vertices[index++] = alpha;
  6938. // 缓存索引数组
  6939. if (this.hasMesh) {
  6940. var indicesForMesh = this.indicesForMesh;
  6941. indicesForMesh[this.indexIndex + 0] = 0 + this.vertexIndex;
  6942. indicesForMesh[this.indexIndex + 1] = 1 + this.vertexIndex;
  6943. indicesForMesh[this.indexIndex + 2] = 2 + this.vertexIndex;
  6944. indicesForMesh[this.indexIndex + 3] = 0 + this.vertexIndex;
  6945. indicesForMesh[this.indexIndex + 4] = 2 + this.vertexIndex;
  6946. indicesForMesh[this.indexIndex + 5] = 3 + this.vertexIndex;
  6947. }
  6948. this.vertexIndex += 4;
  6949. this.indexIndex += 6;
  6950. }
  6951. };
  6952. WebGLVertexArrayObject.prototype.clear = function () {
  6953. this.hasMesh = false;
  6954. this.vertexIndex = 0;
  6955. this.indexIndex = 0;
  6956. };
  6957. return WebGLVertexArrayObject;
  6958. }());
  6959. web.WebGLVertexArrayObject = WebGLVertexArrayObject;
  6960. __reflect(WebGLVertexArrayObject.prototype, "egret.web.WebGLVertexArrayObject");
  6961. })(web = egret.web || (egret.web = {}));
  6962. })(egret || (egret = {}));
  6963. //////////////////////////////////////////////////////////////////////////////////////
  6964. //
  6965. // Copyright (c) 2014-present, Egret Technology.
  6966. // All rights reserved.
  6967. // Redistribution and use in source and binary forms, with or without
  6968. // modification, are permitted provided that the following conditions are met:
  6969. //
  6970. // * Redistributions of source code must retain the above copyright
  6971. // notice, this list of conditions and the following disclaimer.
  6972. // * Redistributions in binary form must reproduce the above copyright
  6973. // notice, this list of conditions and the following disclaimer in the
  6974. // documentation and/or other materials provided with the distribution.
  6975. // * Neither the name of the Egret nor the
  6976. // names of its contributors may be used to endorse or promote products
  6977. // derived from this software without specific prior written permission.
  6978. //
  6979. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  6980. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  6981. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  6982. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  6983. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  6984. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  6985. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  6986. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  6987. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  6988. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  6989. //
  6990. //////////////////////////////////////////////////////////////////////////////////////
  6991. var egret;
  6992. (function (egret) {
  6993. var web;
  6994. (function (web) {
  6995. /**
  6996. * @private
  6997. * WebGLRenderTarget类
  6998. * 一个WebGL渲染目标,拥有一个frame buffer和texture
  6999. */
  7000. var WebGLRenderTarget = (function (_super) {
  7001. __extends(WebGLRenderTarget, _super);
  7002. function WebGLRenderTarget(gl, width, height) {
  7003. var _this = _super.call(this) || this;
  7004. // 清除色
  7005. _this.clearColor = [0, 0, 0, 0];
  7006. // 是否启用frame buffer, 默认为true
  7007. _this.useFrameBuffer = true;
  7008. _this.gl = gl;
  7009. // 如果尺寸为 0 chrome会报警
  7010. _this.width = width || 1;
  7011. _this.height = height || 1;
  7012. // 创建材质
  7013. _this.texture = _this.createTexture();
  7014. // 创建frame buffer
  7015. _this.frameBuffer = gl.createFramebuffer();
  7016. gl.bindFramebuffer(gl.FRAMEBUFFER, _this.frameBuffer);
  7017. // 绑定材质
  7018. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, _this.texture, 0);
  7019. // 绑定stencil buffer
  7020. _this.stencilBuffer = gl.createRenderbuffer();
  7021. gl.bindRenderbuffer(gl.RENDERBUFFER, _this.stencilBuffer);
  7022. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, _this.width, _this.height);
  7023. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, _this.stencilBuffer);
  7024. return _this;
  7025. }
  7026. /**
  7027. * 重置render target的尺寸
  7028. */
  7029. WebGLRenderTarget.prototype.resize = function (width, height) {
  7030. var gl = this.gl;
  7031. // 设置texture尺寸
  7032. gl.bindTexture(gl.TEXTURE_2D, this.texture);
  7033. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  7034. // gl.bindTexture(gl.TEXTURE_2D, null);
  7035. // 设置render buffer的尺寸
  7036. gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffer); // 是否需要强制绑定?
  7037. gl.bindRenderbuffer(gl.RENDERBUFFER, this.stencilBuffer); // 是否需要强制绑定?
  7038. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, width, height);
  7039. this.width = width;
  7040. this.height = height;
  7041. // 此处不解绑是否会造成bug?
  7042. // gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  7043. };
  7044. /**
  7045. * 激活此render target
  7046. */
  7047. WebGLRenderTarget.prototype.activate = function () {
  7048. var gl = this.gl;
  7049. gl.bindFramebuffer(gl.FRAMEBUFFER, this.getFrameBuffer());
  7050. };
  7051. /**
  7052. * 获取frame buffer
  7053. */
  7054. WebGLRenderTarget.prototype.getFrameBuffer = function () {
  7055. if (!this.useFrameBuffer) {
  7056. return null;
  7057. }
  7058. return this.frameBuffer;
  7059. };
  7060. /**
  7061. * 创建材质
  7062. * TODO 创建材质的方法可以合并
  7063. */
  7064. WebGLRenderTarget.prototype.createTexture = function () {
  7065. var gl = this.gl;
  7066. var texture = gl.createTexture();
  7067. gl.bindTexture(gl.TEXTURE_2D, texture);
  7068. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  7069. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  7070. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  7071. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  7072. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  7073. return texture;
  7074. };
  7075. /**
  7076. * 清除render target颜色缓存
  7077. */
  7078. WebGLRenderTarget.prototype.clear = function (bind) {
  7079. var gl = this.gl;
  7080. if (bind) {
  7081. this.activate();
  7082. }
  7083. gl.colorMask(true, true, true, true);
  7084. gl.clearColor(this.clearColor[0], this.clearColor[1], this.clearColor[2], this.clearColor[3]);
  7085. gl.clear(gl.COLOR_BUFFER_BIT);
  7086. };
  7087. return WebGLRenderTarget;
  7088. }(egret.HashObject));
  7089. web.WebGLRenderTarget = WebGLRenderTarget;
  7090. __reflect(WebGLRenderTarget.prototype, "egret.web.WebGLRenderTarget");
  7091. })(web = egret.web || (egret.web = {}));
  7092. })(egret || (egret = {}));
  7093. //////////////////////////////////////////////////////////////////////////////////////
  7094. //
  7095. // Copyright (c) 2014-present, Egret Technology.
  7096. // All rights reserved.
  7097. // Redistribution and use in source and binary forms, with or without
  7098. // modification, are permitted provided that the following conditions are met:
  7099. //
  7100. // * Redistributions of source code must retain the above copyright
  7101. // notice, this list of conditions and the following disclaimer.
  7102. // * Redistributions in binary form must reproduce the above copyright
  7103. // notice, this list of conditions and the following disclaimer in the
  7104. // documentation and/or other materials provided with the distribution.
  7105. // * Neither the name of the Egret nor the
  7106. // names of its contributors may be used to endorse or promote products
  7107. // derived from this software without specific prior written permission.
  7108. //
  7109. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  7110. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  7111. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  7112. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  7113. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7114. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  7115. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  7116. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  7117. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  7118. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7119. //
  7120. //////////////////////////////////////////////////////////////////////////////////////
  7121. var egret;
  7122. (function (egret) {
  7123. var web;
  7124. (function (web) {
  7125. /**
  7126. * 创建一个canvas。
  7127. */
  7128. function createCanvas(width, height) {
  7129. var canvas = document.createElement("canvas");
  7130. if (!isNaN(width) && !isNaN(height)) {
  7131. canvas.width = width;
  7132. canvas.height = height;
  7133. }
  7134. return canvas;
  7135. }
  7136. /**
  7137. * @private
  7138. * WebGL上下文对象,提供简单的绘图接口
  7139. * 抽象出此类,以实现共用一个context
  7140. */
  7141. var WebGLRenderContext = (function () {
  7142. function WebGLRenderContext(width, height) {
  7143. this.glID = null;
  7144. this.projectionX = NaN;
  7145. this.projectionY = NaN;
  7146. this.shaderManager = null;
  7147. this.contextLost = false;
  7148. this.$scissorState = false;
  7149. this.vertSize = 5;
  7150. this.blurFilter = null;
  7151. this.surface = createCanvas(width, height);
  7152. this.initWebGL();
  7153. this.$bufferStack = [];
  7154. var gl = this.context;
  7155. this.vertexBuffer = gl.createBuffer();
  7156. this.indexBuffer = gl.createBuffer();
  7157. gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
  7158. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
  7159. this.drawCmdManager = new web.WebGLDrawCmdManager();
  7160. this.vao = new web.WebGLVertexArrayObject();
  7161. this.setGlobalCompositeOperation("source-over");
  7162. }
  7163. WebGLRenderContext.getInstance = function (width, height) {
  7164. if (this.instance) {
  7165. return this.instance;
  7166. }
  7167. this.instance = new WebGLRenderContext(width, height);
  7168. return this.instance;
  7169. };
  7170. /**
  7171. * 推入一个RenderBuffer并绑定
  7172. */
  7173. WebGLRenderContext.prototype.pushBuffer = function (buffer) {
  7174. this.$bufferStack.push(buffer);
  7175. if (buffer != this.currentBuffer) {
  7176. if (this.currentBuffer) {
  7177. }
  7178. this.drawCmdManager.pushActivateBuffer(buffer);
  7179. }
  7180. this.currentBuffer = buffer;
  7181. };
  7182. /**
  7183. * 推出一个RenderBuffer并绑定上一个RenderBuffer
  7184. */
  7185. WebGLRenderContext.prototype.popBuffer = function () {
  7186. // 如果只剩下一个buffer,则不执行pop操作
  7187. // 保证舞台buffer永远在最开始
  7188. if (this.$bufferStack.length <= 1) {
  7189. return;
  7190. }
  7191. var buffer = this.$bufferStack.pop();
  7192. var lastBuffer = this.$bufferStack[this.$bufferStack.length - 1];
  7193. // 重新绑定
  7194. if (buffer != lastBuffer) {
  7195. // this.$drawWebGL();
  7196. this.drawCmdManager.pushActivateBuffer(lastBuffer);
  7197. }
  7198. this.currentBuffer = lastBuffer;
  7199. };
  7200. /**
  7201. * 启用RenderBuffer
  7202. */
  7203. WebGLRenderContext.prototype.activateBuffer = function (buffer) {
  7204. buffer.rootRenderTarget.activate();
  7205. if (!this.bindIndices) {
  7206. this.uploadIndicesArray(this.vao.getIndices());
  7207. this.bindIndices = true;
  7208. }
  7209. buffer.restoreStencil();
  7210. buffer.restoreScissor();
  7211. this.onResize(buffer.width, buffer.height);
  7212. };
  7213. /**
  7214. * 上传顶点数据
  7215. */
  7216. WebGLRenderContext.prototype.uploadVerticesArray = function (array) {
  7217. var gl = this.context;
  7218. gl.bufferData(gl.ARRAY_BUFFER, array, gl.STREAM_DRAW);
  7219. // gl.bufferSubData(gl.ARRAY_BUFFER, 0, array);
  7220. };
  7221. /**
  7222. * 上传索引数据
  7223. */
  7224. WebGLRenderContext.prototype.uploadIndicesArray = function (array) {
  7225. var gl = this.context;
  7226. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, array, gl.STATIC_DRAW);
  7227. };
  7228. /**
  7229. * 销毁绘制对象
  7230. */
  7231. WebGLRenderContext.prototype.destroy = function () {
  7232. this.surface.width = this.surface.height = 0;
  7233. };
  7234. WebGLRenderContext.prototype.onResize = function (width, height) {
  7235. width = width || this.surface.width;
  7236. height = height || this.surface.height;
  7237. this.projectionX = width / 2;
  7238. this.projectionY = -height / 2;
  7239. if (this.context) {
  7240. this.context.viewport(0, 0, width, height);
  7241. }
  7242. };
  7243. /**
  7244. * 改变渲染缓冲的大小并清空缓冲区
  7245. * @param width 改变后的宽
  7246. * @param height 改变后的高
  7247. * @param useMaxSize 若传入true,则将改变后的尺寸与已有尺寸对比,保留较大的尺寸。
  7248. */
  7249. WebGLRenderContext.prototype.resize = function (width, height, useMaxSize) {
  7250. var surface = this.surface;
  7251. if (useMaxSize) {
  7252. if (surface.width < width) {
  7253. surface.width = width;
  7254. }
  7255. if (surface.height < height) {
  7256. surface.height = height;
  7257. }
  7258. }
  7259. else {
  7260. if (surface.width != width) {
  7261. surface.width = width;
  7262. }
  7263. if (surface.height != height) {
  7264. surface.height = height;
  7265. }
  7266. }
  7267. this.onResize();
  7268. };
  7269. WebGLRenderContext.prototype.initWebGL = function () {
  7270. this.onResize();
  7271. this.surface.addEventListener("webglcontextlost", this.handleContextLost.bind(this), false);
  7272. this.surface.addEventListener("webglcontextrestored", this.handleContextRestored.bind(this), false);
  7273. this.getWebGLContext();
  7274. this.shaderManager = new web.WebGLShaderManager(this.context);
  7275. };
  7276. WebGLRenderContext.prototype.handleContextLost = function () {
  7277. this.contextLost = true;
  7278. };
  7279. WebGLRenderContext.prototype.handleContextRestored = function () {
  7280. this.initWebGL();
  7281. //this.shaderManager.setContext(this.context);
  7282. this.contextLost = false;
  7283. };
  7284. WebGLRenderContext.prototype.getWebGLContext = function () {
  7285. var options = {
  7286. antialias: WebGLRenderContext.antialias,
  7287. stencil: true //设置可以使用模板(用于不规则遮罩)
  7288. };
  7289. var gl;
  7290. //todo 是否使用chrome源码names
  7291. //let contextNames = ["moz-webgl", "webkit-3d", "experimental-webgl", "webgl", "3d"];
  7292. var names = ["webgl", "experimental-webgl"];
  7293. for (var i = 0; i < names.length; i++) {
  7294. try {
  7295. gl = this.surface.getContext(names[i], options);
  7296. }
  7297. catch (e) {
  7298. }
  7299. if (gl) {
  7300. break;
  7301. }
  7302. }
  7303. if (!gl) {
  7304. egret.$error(1021);
  7305. }
  7306. this.setContext(gl);
  7307. };
  7308. WebGLRenderContext.prototype.setContext = function (gl) {
  7309. this.context = gl;
  7310. gl.id = WebGLRenderContext.glContextId++;
  7311. this.glID = gl.id;
  7312. gl.disable(gl.DEPTH_TEST);
  7313. gl.disable(gl.CULL_FACE);
  7314. gl.enable(gl.BLEND);
  7315. gl.colorMask(true, true, true, true);
  7316. // 目前只使用0号材质单元,默认开启
  7317. gl.activeTexture(gl.TEXTURE0);
  7318. };
  7319. /**
  7320. * 开启模版检测
  7321. */
  7322. WebGLRenderContext.prototype.enableStencilTest = function () {
  7323. var gl = this.context;
  7324. gl.enable(gl.STENCIL_TEST);
  7325. };
  7326. /**
  7327. * 关闭模版检测
  7328. */
  7329. WebGLRenderContext.prototype.disableStencilTest = function () {
  7330. var gl = this.context;
  7331. gl.disable(gl.STENCIL_TEST);
  7332. };
  7333. /**
  7334. * 开启scissor检测
  7335. */
  7336. WebGLRenderContext.prototype.enableScissorTest = function (rect) {
  7337. var gl = this.context;
  7338. gl.enable(gl.SCISSOR_TEST);
  7339. gl.scissor(rect.x, rect.y, rect.width, rect.height);
  7340. };
  7341. /**
  7342. * 关闭scissor检测
  7343. */
  7344. WebGLRenderContext.prototype.disableScissorTest = function () {
  7345. var gl = this.context;
  7346. gl.disable(gl.SCISSOR_TEST);
  7347. };
  7348. /**
  7349. * 获取像素信息
  7350. */
  7351. WebGLRenderContext.prototype.getPixels = function (x, y, width, height, pixels) {
  7352. var gl = this.context;
  7353. gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
  7354. };
  7355. /**
  7356. * 创建一个WebGLTexture
  7357. */
  7358. WebGLRenderContext.prototype.createTexture = function (bitmapData) {
  7359. var gl = this.context;
  7360. var texture = gl.createTexture();
  7361. if (!texture) {
  7362. //先创建texture失败,然后lost事件才发出来..
  7363. this.contextLost = true;
  7364. return;
  7365. }
  7366. texture.glContext = gl;
  7367. gl.bindTexture(gl.TEXTURE_2D, texture);
  7368. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
  7369. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmapData);
  7370. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  7371. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  7372. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  7373. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  7374. return texture;
  7375. };
  7376. WebGLRenderContext.prototype.createTextureFromCompressedData = function (data, width, height, levels, internalFormat) {
  7377. return null;
  7378. };
  7379. /**
  7380. * 更新材质的bitmapData
  7381. */
  7382. WebGLRenderContext.prototype.updateTexture = function (texture, bitmapData) {
  7383. var gl = this.context;
  7384. gl.bindTexture(gl.TEXTURE_2D, texture);
  7385. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmapData);
  7386. };
  7387. /**
  7388. * 获取一个WebGLTexture
  7389. * 如果有缓存的texture返回缓存的texture,如果没有则创建并缓存texture
  7390. */
  7391. WebGLRenderContext.prototype.getWebGLTexture = function (bitmapData) {
  7392. if (!bitmapData.webGLTexture) {
  7393. if (bitmapData.format == "image") {
  7394. bitmapData.webGLTexture = this.createTexture(bitmapData.source);
  7395. }
  7396. else if (bitmapData.format == "pvr") {
  7397. bitmapData.webGLTexture = this.createTextureFromCompressedData(bitmapData.source.pvrtcData, bitmapData.width, bitmapData.height, bitmapData.source.mipmapsCount, bitmapData.source.format);
  7398. }
  7399. if (bitmapData.$deleteSource && bitmapData.webGLTexture) {
  7400. bitmapData.source = null;
  7401. }
  7402. }
  7403. return bitmapData.webGLTexture;
  7404. };
  7405. /**
  7406. * 清除矩形区域
  7407. */
  7408. WebGLRenderContext.prototype.clearRect = function (x, y, width, height) {
  7409. if (x != 0 || y != 0 || width != this.surface.width || height != this.surface.height) {
  7410. var buffer = this.currentBuffer;
  7411. if (buffer.$hasScissor) {
  7412. this.setGlobalCompositeOperation("destination-out");
  7413. this.drawRect(x, y, width, height);
  7414. this.setGlobalCompositeOperation("source-over");
  7415. }
  7416. else {
  7417. var m = buffer.globalMatrix;
  7418. if (m.b == 0 && m.c == 0) {
  7419. x = x * m.a + m.tx;
  7420. y = y * m.d + m.ty;
  7421. width = width * m.a;
  7422. height = height * m.d;
  7423. this.enableScissor(x, -y - height + buffer.height, width, height);
  7424. this.clear();
  7425. this.disableScissor();
  7426. }
  7427. else {
  7428. this.setGlobalCompositeOperation("destination-out");
  7429. this.drawRect(x, y, width, height);
  7430. this.setGlobalCompositeOperation("source-over");
  7431. }
  7432. }
  7433. }
  7434. else {
  7435. this.clear();
  7436. }
  7437. };
  7438. /**
  7439. * 设置混色
  7440. */
  7441. WebGLRenderContext.prototype.setGlobalCompositeOperation = function (value) {
  7442. this.drawCmdManager.pushSetBlend(value);
  7443. };
  7444. /**
  7445. * 绘制图片,image参数可以是BitmapData或者renderTarget
  7446. */
  7447. WebGLRenderContext.prototype.drawImage = function (image, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, imageSourceWidth, imageSourceHeight) {
  7448. var buffer = this.currentBuffer;
  7449. if (this.contextLost || !image || !buffer) {
  7450. return;
  7451. }
  7452. var texture;
  7453. if (image["texture"] || (image.source && image.source["texture"])) {
  7454. // 如果是render target
  7455. texture = image["texture"] || image.source["texture"];
  7456. buffer.saveTransform();
  7457. buffer.transform(1, 0, 0, -1, 0, destHeight + destY * 2); // 翻转
  7458. }
  7459. else if (!image.source && !image.webGLTexture) {
  7460. return;
  7461. }
  7462. else {
  7463. texture = this.getWebGLTexture(image);
  7464. }
  7465. if (!texture) {
  7466. return;
  7467. }
  7468. this.drawTexture(texture, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, imageSourceWidth, imageSourceHeight);
  7469. if (image.source && image.source["texture"]) {
  7470. buffer.restoreTransform();
  7471. }
  7472. };
  7473. /**
  7474. * 绘制Mesh
  7475. */
  7476. WebGLRenderContext.prototype.drawMesh = function (image, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, imageSourceWidth, imageSourceHeight, meshUVs, meshVertices, meshIndices, bounds) {
  7477. var buffer = this.currentBuffer;
  7478. if (this.contextLost || !image || !buffer) {
  7479. return;
  7480. }
  7481. var texture;
  7482. if (image["texture"] || (image.source && image.source["texture"])) {
  7483. // 如果是render target
  7484. texture = image["texture"] || image.source["texture"];
  7485. buffer.saveTransform();
  7486. buffer.transform(1, 0, 0, -1, 0, destHeight + destY * 2); // 翻转
  7487. }
  7488. else if (!image.source && !image.webGLTexture) {
  7489. return;
  7490. }
  7491. else {
  7492. texture = this.getWebGLTexture(image);
  7493. }
  7494. if (!texture) {
  7495. return;
  7496. }
  7497. this.drawTexture(texture, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, imageSourceWidth, imageSourceHeight, meshUVs, meshVertices, meshIndices, bounds);
  7498. };
  7499. /**
  7500. * 绘制材质
  7501. */
  7502. WebGLRenderContext.prototype.drawTexture = function (texture, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, textureWidth, textureHeight, meshUVs, meshVertices, meshIndices, bounds) {
  7503. var buffer = this.currentBuffer;
  7504. if (this.contextLost || !texture || !buffer) {
  7505. return;
  7506. }
  7507. if (meshVertices && meshIndices) {
  7508. if (this.vao.reachMaxSize(meshVertices.length / 2, meshIndices.length)) {
  7509. this.$drawWebGL();
  7510. }
  7511. }
  7512. else {
  7513. if (this.vao.reachMaxSize()) {
  7514. this.$drawWebGL();
  7515. }
  7516. }
  7517. if (meshUVs) {
  7518. this.vao.changeToMeshIndices();
  7519. }
  7520. var transform = buffer.globalMatrix;
  7521. var alpha = buffer.globalAlpha;
  7522. var count = meshIndices ? meshIndices.length / 3 : 2;
  7523. // 应用$filter,因为只可能是colorMatrixFilter,最后两个参数可不传
  7524. this.drawCmdManager.pushDrawTexture(texture, count, this.$filter);
  7525. this.vao.cacheArrays(transform, alpha, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, textureWidth, textureHeight, meshUVs, meshVertices, meshIndices);
  7526. };
  7527. /**
  7528. * 绘制矩形(仅用于遮罩擦除等)
  7529. */
  7530. WebGLRenderContext.prototype.drawRect = function (x, y, width, height) {
  7531. var buffer = this.currentBuffer;
  7532. if (this.contextLost || !buffer) {
  7533. return;
  7534. }
  7535. if (this.vao.reachMaxSize()) {
  7536. this.$drawWebGL();
  7537. }
  7538. this.drawCmdManager.pushDrawRect();
  7539. this.vao.cacheArrays(buffer.globalMatrix, buffer.globalAlpha, 0, 0, width, height, x, y, width, height, width, height);
  7540. };
  7541. /**
  7542. * 绘制遮罩
  7543. */
  7544. WebGLRenderContext.prototype.pushMask = function (mask) {
  7545. var buffer = this.currentBuffer;
  7546. if (this.contextLost || !buffer) {
  7547. return;
  7548. }
  7549. buffer.$stencilList.push(mask);
  7550. if (this.vao.reachMaxSize()) {
  7551. this.$drawWebGL();
  7552. }
  7553. var length = mask.length;
  7554. if (length) {
  7555. this.drawCmdManager.pushPushMask(length);
  7556. for (var i = 0; i < length; i++) {
  7557. var item = mask[i];
  7558. this.vao.cacheArrays(buffer.globalMatrix, buffer.globalAlpha, 0, 0, item.width, item.height, item.minX, item.minY, item.width, item.height, item.width, item.height);
  7559. }
  7560. }
  7561. else {
  7562. this.drawCmdManager.pushPushMask();
  7563. this.vao.cacheArrays(buffer.globalMatrix, buffer.globalAlpha, 0, 0, mask.width, mask.height, mask.x, mask.y, mask.width, mask.height, mask.width, mask.height);
  7564. }
  7565. };
  7566. /**
  7567. * 恢复遮罩
  7568. */
  7569. WebGLRenderContext.prototype.popMask = function () {
  7570. var buffer = this.currentBuffer;
  7571. if (this.contextLost || !buffer) {
  7572. return;
  7573. }
  7574. var mask = buffer.$stencilList.pop();
  7575. if (this.vao.reachMaxSize()) {
  7576. this.$drawWebGL();
  7577. }
  7578. var length = mask.length;
  7579. if (length) {
  7580. this.drawCmdManager.pushPopMask(length);
  7581. for (var i = 0; i < length; i++) {
  7582. var item = mask[i];
  7583. this.vao.cacheArrays(buffer.globalMatrix, buffer.globalAlpha, 0, 0, item.width, item.height, item.minX, item.minY, item.width, item.height, item.width, item.height);
  7584. }
  7585. }
  7586. else {
  7587. this.drawCmdManager.pushPopMask();
  7588. this.vao.cacheArrays(buffer.globalMatrix, buffer.globalAlpha, 0, 0, mask.width, mask.height, mask.x, mask.y, mask.width, mask.height, mask.width, mask.height);
  7589. }
  7590. };
  7591. /**
  7592. * 清除颜色缓存
  7593. */
  7594. WebGLRenderContext.prototype.clear = function () {
  7595. this.drawCmdManager.pushClearColor();
  7596. };
  7597. /**
  7598. * 开启scissor test
  7599. */
  7600. WebGLRenderContext.prototype.enableScissor = function (x, y, width, height) {
  7601. var buffer = this.currentBuffer;
  7602. this.drawCmdManager.pushEnableScissor(x, y, width, height);
  7603. buffer.$hasScissor = true;
  7604. };
  7605. /**
  7606. * 关闭scissor test
  7607. */
  7608. WebGLRenderContext.prototype.disableScissor = function () {
  7609. var buffer = this.currentBuffer;
  7610. this.drawCmdManager.pushDisableScissor();
  7611. buffer.$hasScissor = false;
  7612. };
  7613. WebGLRenderContext.prototype.$drawWebGL = function () {
  7614. if (this.drawCmdManager.drawDataLen == 0 || this.contextLost) {
  7615. return;
  7616. }
  7617. this.uploadVerticesArray(this.vao.getVertices());
  7618. // 有mesh,则使用indicesForMesh
  7619. if (this.vao.isMesh()) {
  7620. this.uploadIndicesArray(this.vao.getMeshIndices());
  7621. }
  7622. var length = this.drawCmdManager.drawDataLen;
  7623. var offset = 0;
  7624. for (var i = 0; i < length; i++) {
  7625. var data = this.drawCmdManager.drawData[i];
  7626. offset = this.drawData(data, offset);
  7627. // 计算draw call
  7628. if (data.type == 7 /* ACT_BUFFER */) {
  7629. this.activatedBuffer = data.buffer;
  7630. }
  7631. if (data.type == 0 /* TEXTURE */ || data.type == 1 /* RECT */ || data.type == 2 /* PUSH_MASK */ || data.type == 3 /* POP_MASK */) {
  7632. if (this.activatedBuffer && this.activatedBuffer.$computeDrawCall) {
  7633. this.activatedBuffer.$drawCalls++;
  7634. }
  7635. }
  7636. }
  7637. // 切换回默认indices
  7638. if (this.vao.isMesh()) {
  7639. this.uploadIndicesArray(this.vao.getIndices());
  7640. }
  7641. // 清空数据
  7642. this.drawCmdManager.clear();
  7643. this.vao.clear();
  7644. };
  7645. /**
  7646. * 执行绘制命令
  7647. */
  7648. WebGLRenderContext.prototype.drawData = function (data, offset) {
  7649. if (!data) {
  7650. return;
  7651. }
  7652. var shader;
  7653. switch (data.type) {
  7654. case 0 /* TEXTURE */:
  7655. var filter = data.filter;
  7656. if (filter) {
  7657. if (filter.type == "colorTransform") {
  7658. shader = this.shaderManager.colorTransformShader;
  7659. shader.setMatrix(filter.$matrix);
  7660. }
  7661. else if (filter.type == "blur") {
  7662. shader = this.shaderManager.blurShader;
  7663. shader.setBlur(filter.$blurX, filter.$blurY);
  7664. shader.setTextureSize(data.textureWidth, data.textureHeight);
  7665. }
  7666. else if (filter.type == "glow") {
  7667. shader = this.shaderManager.glowShader;
  7668. shader.setDistance(filter.$distance || 0);
  7669. shader.setAngle(filter.$angle ? filter.$angle / 180 * Math.PI : 0);
  7670. shader.setColor(filter.$red / 255, filter.$green / 255, filter.$blue / 255);
  7671. shader.setAlpha(filter.$alpha);
  7672. shader.setBlurX(filter.$blurX);
  7673. shader.setBlurY(filter.$blurY);
  7674. shader.setStrength(filter.$strength);
  7675. shader.setInner(filter.$inner ? 1 : 0);
  7676. shader.setKnockout(filter.$knockout ? 0 : 1);
  7677. shader.setHideObject(filter.$hideObject ? 1 : 0);
  7678. shader.setTextureSize(data.textureWidth, data.textureHeight);
  7679. }
  7680. }
  7681. else {
  7682. shader = this.shaderManager.defaultShader;
  7683. }
  7684. shader.setProjection(this.projectionX, this.projectionY);
  7685. this.shaderManager.activateShader(shader, this.vertSize * 4);
  7686. shader.syncUniforms();
  7687. offset += this.drawTextureElements(data, offset);
  7688. break;
  7689. case 1 /* RECT */:
  7690. shader = this.shaderManager.primitiveShader;
  7691. shader.setProjection(this.projectionX, this.projectionY);
  7692. this.shaderManager.activateShader(shader, this.vertSize * 4);
  7693. shader.syncUniforms();
  7694. offset += this.drawRectElements(data, offset);
  7695. break;
  7696. case 2 /* PUSH_MASK */:
  7697. shader = this.shaderManager.primitiveShader;
  7698. shader.setProjection(this.projectionX, this.projectionY);
  7699. this.shaderManager.activateShader(shader, this.vertSize * 4);
  7700. shader.syncUniforms();
  7701. offset += this.drawPushMaskElements(data, offset);
  7702. break;
  7703. case 3 /* POP_MASK */:
  7704. shader = this.shaderManager.primitiveShader;
  7705. shader.setProjection(this.projectionX, this.projectionY);
  7706. this.shaderManager.activateShader(shader, this.vertSize * 4);
  7707. shader.syncUniforms();
  7708. offset += this.drawPopMaskElements(data, offset);
  7709. break;
  7710. case 4 /* BLEND */:
  7711. this.setBlendMode(data.value);
  7712. break;
  7713. case 5 /* RESIZE_TARGET */:
  7714. data.buffer.rootRenderTarget.resize(data.width, data.height);
  7715. this.onResize(data.width, data.height);
  7716. break;
  7717. case 6 /* CLEAR_COLOR */:
  7718. if (this.activatedBuffer) {
  7719. var target = this.activatedBuffer.rootRenderTarget;
  7720. if (target.width != 0 || target.height != 0) {
  7721. target.clear();
  7722. }
  7723. }
  7724. break;
  7725. case 7 /* ACT_BUFFER */:
  7726. this.activateBuffer(data.buffer);
  7727. break;
  7728. case 8 /* ENABLE_SCISSOR */:
  7729. var buffer = this.activatedBuffer;
  7730. if (buffer) {
  7731. buffer.enableScissor(data.x, data.y, data.width, data.height);
  7732. }
  7733. break;
  7734. case 9 /* DISABLE_SCISSOR */:
  7735. buffer = this.activatedBuffer;
  7736. if (buffer) {
  7737. buffer.disableScissor();
  7738. }
  7739. break;
  7740. default:
  7741. break;
  7742. }
  7743. return offset;
  7744. };
  7745. /**
  7746. * 画texture
  7747. **/
  7748. WebGLRenderContext.prototype.drawTextureElements = function (data, offset) {
  7749. var gl = this.context;
  7750. gl.bindTexture(gl.TEXTURE_2D, data.texture);
  7751. var size = data.count * 3;
  7752. gl.drawElements(gl.TRIANGLES, size, gl.UNSIGNED_SHORT, offset * 2);
  7753. return size;
  7754. };
  7755. /**
  7756. * @private
  7757. * 画rect
  7758. **/
  7759. WebGLRenderContext.prototype.drawRectElements = function (data, offset) {
  7760. var gl = this.context;
  7761. // gl.bindTexture(gl.TEXTURE_2D, null);
  7762. var size = data.count * 3;
  7763. gl.drawElements(gl.TRIANGLES, size, gl.UNSIGNED_SHORT, offset * 2);
  7764. return size;
  7765. };
  7766. /**
  7767. * 画push mask
  7768. **/
  7769. WebGLRenderContext.prototype.drawPushMaskElements = function (data, offset) {
  7770. var gl = this.context;
  7771. var size = data.count * 3;
  7772. var buffer = this.activatedBuffer;
  7773. if (buffer) {
  7774. if (buffer.stencilHandleCount == 0) {
  7775. buffer.enableStencil();
  7776. gl.clear(gl.STENCIL_BUFFER_BIT); // clear
  7777. }
  7778. var level = buffer.stencilHandleCount;
  7779. buffer.stencilHandleCount++;
  7780. gl.colorMask(false, false, false, false);
  7781. gl.stencilFunc(gl.EQUAL, level, 0xFF);
  7782. gl.stencilOp(gl.KEEP, gl.KEEP, gl.INCR);
  7783. // gl.bindTexture(gl.TEXTURE_2D, null);
  7784. gl.drawElements(gl.TRIANGLES, size, gl.UNSIGNED_SHORT, offset * 2);
  7785. gl.stencilFunc(gl.EQUAL, level + 1, 0xFF);
  7786. gl.colorMask(true, true, true, true);
  7787. gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
  7788. }
  7789. return size;
  7790. };
  7791. /**
  7792. * 画pop mask
  7793. **/
  7794. WebGLRenderContext.prototype.drawPopMaskElements = function (data, offset) {
  7795. var gl = this.context;
  7796. var size = data.count * 3;
  7797. var buffer = this.activatedBuffer;
  7798. if (buffer) {
  7799. buffer.stencilHandleCount--;
  7800. if (buffer.stencilHandleCount == 0) {
  7801. buffer.disableStencil(); // skip this draw
  7802. }
  7803. else {
  7804. var level = buffer.stencilHandleCount;
  7805. gl.colorMask(false, false, false, false);
  7806. gl.stencilFunc(gl.EQUAL, level + 1, 0xFF);
  7807. gl.stencilOp(gl.KEEP, gl.KEEP, gl.DECR);
  7808. // gl.bindTexture(gl.TEXTURE_2D, null);
  7809. gl.drawElements(gl.TRIANGLES, size, gl.UNSIGNED_SHORT, offset * 2);
  7810. gl.stencilFunc(gl.EQUAL, level, 0xFF);
  7811. gl.colorMask(true, true, true, true);
  7812. gl.stencilOp(gl.KEEP, gl.KEEP, gl.KEEP);
  7813. }
  7814. }
  7815. return size;
  7816. };
  7817. /**
  7818. * 设置混色
  7819. */
  7820. WebGLRenderContext.prototype.setBlendMode = function (value) {
  7821. var gl = this.context;
  7822. var blendModeWebGL = WebGLRenderContext.blendModesForGL[value];
  7823. if (blendModeWebGL) {
  7824. gl.blendFunc(blendModeWebGL[0], blendModeWebGL[1]);
  7825. }
  7826. };
  7827. /**
  7828. * 应用滤镜绘制给定的render target
  7829. * 此方法不会导致input被释放,所以如果需要释放input,需要调用此方法后手动调用release
  7830. */
  7831. WebGLRenderContext.prototype.drawTargetWidthFilters = function (filters, input) {
  7832. var originInput = input, filtersLen = filters.length, output;
  7833. // 应用前面的滤镜
  7834. if (filtersLen > 1) {
  7835. for (var i = 0; i < filtersLen - 1; i++) {
  7836. var filter_1 = filters[i];
  7837. var width = input.rootRenderTarget.width;
  7838. var height = input.rootRenderTarget.height;
  7839. output = web.WebGLRenderBuffer.create(width, height);
  7840. output.setTransform(1, 0, 0, 1, 0, 0);
  7841. output.globalAlpha = 1;
  7842. this.drawToRenderTarget(filter_1, input, output);
  7843. if (input != originInput) {
  7844. web.WebGLRenderBuffer.release(input);
  7845. }
  7846. input = output;
  7847. }
  7848. }
  7849. // 应用最后一个滤镜并绘制到当前场景中
  7850. var filter = filters[filtersLen - 1];
  7851. this.drawToRenderTarget(filter, input, this.currentBuffer);
  7852. // 释放掉用于交换的buffer
  7853. if (input != originInput) {
  7854. web.WebGLRenderBuffer.release(input);
  7855. }
  7856. };
  7857. /**
  7858. * 向一个renderTarget中绘制
  7859. * */
  7860. WebGLRenderContext.prototype.drawToRenderTarget = function (filter, input, output) {
  7861. if (this.contextLost) {
  7862. return;
  7863. }
  7864. if (this.vao.reachMaxSize()) {
  7865. this.$drawWebGL();
  7866. }
  7867. this.pushBuffer(output);
  7868. var originInput = input, temp, width = input.rootRenderTarget.width, height = input.rootRenderTarget.height;
  7869. // 模糊滤镜实现为blurX与blurY的叠加
  7870. if (filter.type == "blur") {
  7871. if (!this.blurFilter) {
  7872. this.blurFilter = new egret.BlurFilter(2, 2);
  7873. }
  7874. if (filter.blurX != 0 && filter.blurY != 0) {
  7875. this.blurFilter.blurX = filter.blurX;
  7876. this.blurFilter.blurY = 0;
  7877. temp = web.WebGLRenderBuffer.create(width, height);
  7878. temp.setTransform(1, 0, 0, 1, 0, 0);
  7879. temp.globalAlpha = 1;
  7880. this.drawToRenderTarget(this.blurFilter, input, temp);
  7881. if (input != originInput) {
  7882. web.WebGLRenderBuffer.release(input);
  7883. }
  7884. input = temp;
  7885. this.blurFilter.blurX = 0;
  7886. this.blurFilter.blurY = filter.blurY;
  7887. }
  7888. else {
  7889. this.blurFilter.blurX = filter.blurX;
  7890. this.blurFilter.blurY = filter.blurY;
  7891. }
  7892. filter = this.blurFilter;
  7893. }
  7894. // 绘制input结果到舞台
  7895. output.saveTransform();
  7896. output.transform(1, 0, 0, -1, 0, height);
  7897. this.vao.cacheArrays(output.globalMatrix, output.globalAlpha, 0, 0, width, height, 0, 0, width, height, width, height);
  7898. output.restoreTransform();
  7899. var filterData;
  7900. if (filter.type == "blur") {
  7901. // 实现blurx与blurY分开处理,会借用公用filter
  7902. // 为了允许公用filter的存在,这里拷贝filter到对象中
  7903. filterData = { type: "blur", $blurX: filter.$blurX, $blurY: filter.$blurY };
  7904. }
  7905. else {
  7906. filterData = filter;
  7907. }
  7908. this.drawCmdManager.pushDrawTexture(input["rootRenderTarget"].texture, 2, filterData, width, height);
  7909. // 释放掉input
  7910. if (input != originInput) {
  7911. web.WebGLRenderBuffer.release(input);
  7912. }
  7913. this.popBuffer();
  7914. };
  7915. WebGLRenderContext.initBlendMode = function () {
  7916. WebGLRenderContext.blendModesForGL = {};
  7917. WebGLRenderContext.blendModesForGL["source-over"] = [1, 771];
  7918. WebGLRenderContext.blendModesForGL["lighter"] = [1, 1];
  7919. WebGLRenderContext.blendModesForGL["lighter-in"] = [770, 771];
  7920. WebGLRenderContext.blendModesForGL["destination-out"] = [0, 771];
  7921. WebGLRenderContext.blendModesForGL["destination-in"] = [0, 770];
  7922. };
  7923. return WebGLRenderContext;
  7924. }());
  7925. /**
  7926. * 改变渲染缓冲为指定大小,但保留原始图像数据
  7927. * @param width 改变后的宽
  7928. * @param height 改变后的高
  7929. * @param offsetX 原始图像数据在改变后缓冲区的绘制起始位置x
  7930. * @param offsetY 原始图像数据在改变后缓冲区的绘制起始位置y
  7931. */
  7932. // public resizeTo(width:number, height:number, offsetX:number, offsetY:number):void {
  7933. // this.surface.width = width;
  7934. // this.surface.height = height;
  7935. // }
  7936. WebGLRenderContext.glContextId = 0;
  7937. WebGLRenderContext.blendModesForGL = null;
  7938. web.WebGLRenderContext = WebGLRenderContext;
  7939. __reflect(WebGLRenderContext.prototype, "egret.web.WebGLRenderContext");
  7940. WebGLRenderContext.initBlendMode();
  7941. })(web = egret.web || (egret.web = {}));
  7942. })(egret || (egret = {}));
  7943. //////////////////////////////////////////////////////////////////////////////////////
  7944. //
  7945. // Copyright (c) 2014-present, Egret Technology.
  7946. // All rights reserved.
  7947. // Redistribution and use in source and binary forms, with or without
  7948. // modification, are permitted provided that the following conditions are met:
  7949. //
  7950. // * Redistributions of source code must retain the above copyright
  7951. // notice, this list of conditions and the following disclaimer.
  7952. // * Redistributions in binary form must reproduce the above copyright
  7953. // notice, this list of conditions and the following disclaimer in the
  7954. // documentation and/or other materials provided with the distribution.
  7955. // * Neither the name of the Egret nor the
  7956. // names of its contributors may be used to endorse or promote products
  7957. // derived from this software without specific prior written permission.
  7958. //
  7959. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  7960. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  7961. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  7962. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  7963. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  7964. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  7965. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  7966. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  7967. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  7968. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  7969. //
  7970. //////////////////////////////////////////////////////////////////////////////////////
  7971. var egret;
  7972. (function (egret) {
  7973. var web;
  7974. (function (web) {
  7975. /**
  7976. * @private
  7977. * WebGL渲染缓存
  7978. */
  7979. var WebGLRenderBuffer = (function (_super) {
  7980. __extends(WebGLRenderBuffer, _super);
  7981. function WebGLRenderBuffer(width, height, root) {
  7982. var _this = _super.call(this) || this;
  7983. _this.globalAlpha = 1;
  7984. /**
  7985. * stencil state
  7986. * 模版开关状态
  7987. */
  7988. _this.stencilState = false;
  7989. _this.$stencilList = [];
  7990. _this.stencilHandleCount = 0;
  7991. /**
  7992. * scissor state
  7993. * scissor 开关状态
  7994. */
  7995. _this.$scissorState = false;
  7996. _this.scissorRect = new egret.Rectangle();
  7997. _this.$hasScissor = false;
  7998. // dirtyRegionPolicy hack
  7999. _this.dirtyRegionPolicy = true;
  8000. _this._dirtyRegionPolicy = true; // 默认设置为true,保证第一帧绘制在frameBuffer上
  8001. _this.$drawCalls = 0;
  8002. _this.$computeDrawCall = false;
  8003. _this.globalMatrix = new egret.Matrix();
  8004. _this.savedGlobalMatrix = new egret.Matrix();
  8005. // 获取webglRenderContext
  8006. _this.context = web.WebGLRenderContext.getInstance(width, height);
  8007. // buffer 对应的 render target
  8008. _this.rootRenderTarget = new web.WebGLRenderTarget(_this.context.context, 3, 3);
  8009. if (width && height) {
  8010. _this.resize(width, height);
  8011. }
  8012. // 如果是第一个加入的buffer,说明是舞台buffer
  8013. _this.root = root;
  8014. // 如果是用于舞台渲染的renderBuffer,则默认添加renderTarget到renderContext中,而且是第一个
  8015. if (_this.root) {
  8016. _this.context.pushBuffer(_this);
  8017. // 画布
  8018. _this.surface = _this.context.surface;
  8019. }
  8020. else {
  8021. // 由于创建renderTarget造成的frameBuffer绑定,这里重置绑定
  8022. var lastBuffer = _this.context.activatedBuffer;
  8023. if (lastBuffer) {
  8024. lastBuffer.rootRenderTarget.activate();
  8025. }
  8026. _this.surface = _this.rootRenderTarget;
  8027. }
  8028. return _this;
  8029. }
  8030. WebGLRenderBuffer.prototype.enableStencil = function () {
  8031. if (!this.stencilState) {
  8032. this.context.enableStencilTest();
  8033. this.stencilState = true;
  8034. }
  8035. };
  8036. WebGLRenderBuffer.prototype.disableStencil = function () {
  8037. if (this.stencilState) {
  8038. this.context.disableStencilTest();
  8039. this.stencilState = false;
  8040. }
  8041. };
  8042. WebGLRenderBuffer.prototype.restoreStencil = function () {
  8043. if (this.stencilState) {
  8044. this.context.enableStencilTest();
  8045. }
  8046. else {
  8047. this.context.disableStencilTest();
  8048. }
  8049. };
  8050. WebGLRenderBuffer.prototype.enableScissor = function (x, y, width, height) {
  8051. if (!this.$scissorState) {
  8052. this.$scissorState = true;
  8053. this.scissorRect.setTo(x, y, width, height);
  8054. this.context.enableScissorTest(this.scissorRect);
  8055. }
  8056. };
  8057. WebGLRenderBuffer.prototype.disableScissor = function () {
  8058. if (this.$scissorState) {
  8059. this.$scissorState = false;
  8060. this.scissorRect.setEmpty();
  8061. this.context.disableScissorTest();
  8062. }
  8063. };
  8064. WebGLRenderBuffer.prototype.restoreScissor = function () {
  8065. if (this.$scissorState) {
  8066. this.context.enableScissorTest(this.scissorRect);
  8067. }
  8068. else {
  8069. this.context.disableScissorTest();
  8070. }
  8071. };
  8072. Object.defineProperty(WebGLRenderBuffer.prototype, "width", {
  8073. /**
  8074. * 渲染缓冲的宽度,以像素为单位。
  8075. * @readOnly
  8076. */
  8077. get: function () {
  8078. return this.rootRenderTarget.width;
  8079. },
  8080. enumerable: true,
  8081. configurable: true
  8082. });
  8083. Object.defineProperty(WebGLRenderBuffer.prototype, "height", {
  8084. /**
  8085. * 渲染缓冲的高度,以像素为单位。
  8086. * @readOnly
  8087. */
  8088. get: function () {
  8089. return this.rootRenderTarget.height;
  8090. },
  8091. enumerable: true,
  8092. configurable: true
  8093. });
  8094. /**
  8095. * 改变渲染缓冲的大小并清空缓冲区
  8096. * @param width 改变后的宽
  8097. * @param height 改变后的高
  8098. * @param useMaxSize 若传入true,则将改变后的尺寸与已有尺寸对比,保留较大的尺寸。
  8099. */
  8100. WebGLRenderBuffer.prototype.resize = function (width, height, useMaxSize) {
  8101. this.context.pushBuffer(this);
  8102. width = width || 1;
  8103. height = height || 1;
  8104. // render target 尺寸重置
  8105. if (width != this.rootRenderTarget.width || height != this.rootRenderTarget.height) {
  8106. this.context.drawCmdManager.pushResize(this, width, height);
  8107. // 同步更改宽高
  8108. this.rootRenderTarget.width = width;
  8109. this.rootRenderTarget.height = height;
  8110. }
  8111. // 如果是舞台的渲染缓冲,执行resize,否则surface大小不随之改变
  8112. if (this.root) {
  8113. this.context.resize(width, height, useMaxSize);
  8114. }
  8115. this.context.clear();
  8116. this.context.popBuffer();
  8117. };
  8118. /**
  8119. * 改变渲染缓冲为指定大小,但保留原始图像数据
  8120. * @param width 改变后的宽
  8121. * @param height 改变后的高
  8122. * @param offsetX 原始图像数据在改变后缓冲区的绘制起始位置x
  8123. * @param offsetY 原始图像数据在改变后缓冲区的绘制起始位置y
  8124. */
  8125. WebGLRenderBuffer.prototype.resizeTo = function (width, height, offsetX, offsetY) {
  8126. this.context.pushBuffer(this);
  8127. var oldWidth = this.rootRenderTarget.width;
  8128. var oldHeight = this.rootRenderTarget.height;
  8129. var tempBuffer = WebGLRenderBuffer.create(oldWidth, oldHeight);
  8130. this.context.pushBuffer(tempBuffer);
  8131. this.context.drawImage(this.rootRenderTarget, 0, 0, oldWidth, oldHeight, 0, 0, oldWidth, oldHeight, oldWidth, oldHeight);
  8132. this.context.popBuffer();
  8133. this.resize(width, height);
  8134. this.setTransform(1, 0, 0, 1, 0, 0);
  8135. this.context.drawImage(tempBuffer.rootRenderTarget, 0, 0, oldWidth, oldHeight, offsetX, offsetY, oldWidth, oldHeight, oldWidth, oldHeight);
  8136. WebGLRenderBuffer.release(tempBuffer);
  8137. this.context.popBuffer();
  8138. };
  8139. WebGLRenderBuffer.prototype.setDirtyRegionPolicy = function (state) {
  8140. this.dirtyRegionPolicy = (state == "on");
  8141. };
  8142. /**
  8143. * 清空并设置裁切
  8144. * @param regions 矩形列表
  8145. * @param offsetX 矩形要加上的偏移量x
  8146. * @param offsetY 矩形要加上的偏移量y
  8147. */
  8148. WebGLRenderBuffer.prototype.beginClip = function (regions, offsetX, offsetY) {
  8149. this.context.pushBuffer(this);
  8150. if (this.root) {
  8151. // dirtyRegionPolicy hack
  8152. if (this._dirtyRegionPolicy) {
  8153. this.rootRenderTarget.useFrameBuffer = true;
  8154. this.rootRenderTarget.activate();
  8155. }
  8156. else {
  8157. this.rootRenderTarget.useFrameBuffer = false;
  8158. this.rootRenderTarget.activate();
  8159. this.context.clear();
  8160. }
  8161. }
  8162. offsetX = +offsetX || 0;
  8163. offsetY = +offsetY || 0;
  8164. this.setTransform(1, 0, 0, 1, offsetX, offsetY);
  8165. var length = regions.length;
  8166. //只有一个区域且刚好为舞台大小时,不设置模板
  8167. if (length == 1 && regions[0].minX == 0 && regions[0].minY == 0 &&
  8168. regions[0].width == this.rootRenderTarget.width && regions[0].height == this.rootRenderTarget.height) {
  8169. this.maskPushed = false;
  8170. this.rootRenderTarget.useFrameBuffer && this.context.clear();
  8171. this.context.popBuffer();
  8172. return;
  8173. }
  8174. // 擦除脏矩形区域
  8175. for (var i = 0; i < length; i++) {
  8176. var region = regions[i];
  8177. this.context.clearRect(region.minX, region.minY, region.width, region.height);
  8178. }
  8179. // 设置模版
  8180. if (length > 0) {
  8181. // 对第一个且只有一个mask用scissor处理
  8182. if (!this.$hasScissor && length == 1) {
  8183. var region = regions[0];
  8184. regions = regions.slice(1);
  8185. var x = region.minX + offsetX;
  8186. var y = region.minY + offsetY;
  8187. var width = region.width;
  8188. var height = region.height;
  8189. this.context.enableScissor(x, -y - height + this.height, width, height);
  8190. this.scissorEnabled = true;
  8191. }
  8192. else {
  8193. this.scissorEnabled = false;
  8194. }
  8195. if (regions.length > 0) {
  8196. this.context.pushMask(regions);
  8197. this.maskPushed = true;
  8198. }
  8199. else {
  8200. this.maskPushed = false;
  8201. }
  8202. this.offsetX = offsetX;
  8203. this.offsetY = offsetY;
  8204. }
  8205. else {
  8206. this.maskPushed = false;
  8207. }
  8208. this.context.popBuffer();
  8209. };
  8210. /**
  8211. * 取消上一次设置的clip。
  8212. */
  8213. WebGLRenderBuffer.prototype.endClip = function () {
  8214. if (this.maskPushed || this.scissorEnabled) {
  8215. this.context.pushBuffer(this);
  8216. if (this.maskPushed) {
  8217. this.setTransform(1, 0, 0, 1, this.offsetX, this.offsetY);
  8218. this.context.popMask();
  8219. }
  8220. if (this.scissorEnabled) {
  8221. this.context.disableScissor();
  8222. }
  8223. this.context.popBuffer();
  8224. }
  8225. };
  8226. /**
  8227. * 获取指定区域的像素
  8228. */
  8229. WebGLRenderBuffer.prototype.getPixels = function (x, y, width, height) {
  8230. if (width === void 0) { width = 1; }
  8231. if (height === void 0) { height = 1; }
  8232. var pixels = new Uint8Array(4 * width * height);
  8233. var useFrameBuffer = this.rootRenderTarget.useFrameBuffer;
  8234. this.rootRenderTarget.useFrameBuffer = true;
  8235. this.rootRenderTarget.activate();
  8236. this.context.getPixels(x, y, width, height, pixels);
  8237. this.rootRenderTarget.useFrameBuffer = useFrameBuffer;
  8238. this.rootRenderTarget.activate();
  8239. //图像反转
  8240. var result = new Uint8Array(4 * width * height);
  8241. for (var i = 0; i < height; i++) {
  8242. for (var j = 0; j < width; j++) {
  8243. result[(width * (height - i - 1) + j) * 4] = pixels[(width * i + j) * 4];
  8244. result[(width * (height - i - 1) + j) * 4 + 1] = pixels[(width * i + j) * 4 + 1];
  8245. result[(width * (height - i - 1) + j) * 4 + 2] = pixels[(width * i + j) * 4 + 2];
  8246. result[(width * (height - i - 1) + j) * 4 + 3] = pixels[(width * i + j) * 4 + 3];
  8247. }
  8248. }
  8249. return result;
  8250. };
  8251. /**
  8252. * 转换成base64字符串,如果图片(或者包含的图片)跨域,则返回null
  8253. * @param type 转换的类型,如: "image/png","image/jpeg"
  8254. */
  8255. WebGLRenderBuffer.prototype.toDataURL = function (type, encoderOptions) {
  8256. return this.context.surface.toDataURL(type, encoderOptions);
  8257. };
  8258. /**
  8259. * 销毁绘制对象
  8260. */
  8261. WebGLRenderBuffer.prototype.destroy = function () {
  8262. this.context.destroy();
  8263. };
  8264. WebGLRenderBuffer.prototype.onRenderFinish = function () {
  8265. this.$drawCalls = 0;
  8266. // 如果是舞台渲染buffer,判断脏矩形策略
  8267. if (this.root) {
  8268. // dirtyRegionPolicy hack
  8269. if (!this._dirtyRegionPolicy && this.dirtyRegionPolicy) {
  8270. this.drawSurfaceToFrameBuffer(0, 0, this.rootRenderTarget.width, this.rootRenderTarget.height, 0, 0, this.rootRenderTarget.width, this.rootRenderTarget.height, true);
  8271. }
  8272. if (this._dirtyRegionPolicy) {
  8273. this.drawFrameBufferToSurface(0, 0, this.rootRenderTarget.width, this.rootRenderTarget.height, 0, 0, this.rootRenderTarget.width, this.rootRenderTarget.height);
  8274. }
  8275. this._dirtyRegionPolicy = this.dirtyRegionPolicy;
  8276. }
  8277. };
  8278. /**
  8279. * 交换frameBuffer中的图像到surface中
  8280. * @param width 宽度
  8281. * @param height 高度
  8282. */
  8283. WebGLRenderBuffer.prototype.drawFrameBufferToSurface = function (sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, clear) {
  8284. if (clear === void 0) { clear = false; }
  8285. this.rootRenderTarget.useFrameBuffer = false;
  8286. this.rootRenderTarget.activate();
  8287. this.context.disableStencilTest(); // 切换frameBuffer注意要禁用STENCIL_TEST
  8288. this.context.disableScissorTest();
  8289. this.setTransform(1, 0, 0, 1, 0, 0);
  8290. this.globalAlpha = 1;
  8291. this.context.setGlobalCompositeOperation("source-over");
  8292. clear && this.context.clear();
  8293. this.context.drawImage(this.rootRenderTarget, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, sourceWidth, sourceHeight);
  8294. this.context.$drawWebGL();
  8295. this.rootRenderTarget.useFrameBuffer = true;
  8296. this.rootRenderTarget.activate();
  8297. this.restoreStencil();
  8298. this.restoreScissor();
  8299. };
  8300. /**
  8301. * 交换surface的图像到frameBuffer中
  8302. * @param width 宽度
  8303. * @param height 高度
  8304. */
  8305. WebGLRenderBuffer.prototype.drawSurfaceToFrameBuffer = function (sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, clear) {
  8306. if (clear === void 0) { clear = false; }
  8307. this.rootRenderTarget.useFrameBuffer = true;
  8308. this.rootRenderTarget.activate();
  8309. this.context.disableStencilTest(); // 切换frameBuffer注意要禁用STENCIL_TEST
  8310. this.context.disableScissorTest();
  8311. this.setTransform(1, 0, 0, 1, 0, 0);
  8312. this.globalAlpha = 1;
  8313. this.context.setGlobalCompositeOperation("source-over");
  8314. clear && this.context.clear();
  8315. this.context.drawImage(this.context.surface, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight, sourceWidth, sourceHeight);
  8316. this.context.$drawWebGL();
  8317. this.rootRenderTarget.useFrameBuffer = false;
  8318. this.rootRenderTarget.activate();
  8319. this.restoreStencil();
  8320. this.restoreScissor();
  8321. };
  8322. /**
  8323. * 清空缓冲区数据
  8324. */
  8325. WebGLRenderBuffer.prototype.clear = function () {
  8326. this.context.clear();
  8327. };
  8328. WebGLRenderBuffer.prototype.setTransform = function (a, b, c, d, tx, ty) {
  8329. // this.globalMatrix.setTo(a, b, c, d, tx, ty);
  8330. var matrix = this.globalMatrix;
  8331. matrix.a = a;
  8332. matrix.b = b;
  8333. matrix.c = c;
  8334. matrix.d = d;
  8335. matrix.tx = tx;
  8336. matrix.ty = ty;
  8337. };
  8338. WebGLRenderBuffer.prototype.transform = function (a, b, c, d, tx, ty) {
  8339. // this.globalMatrix.append(a, b, c, d, tx, ty);
  8340. var matrix = this.globalMatrix;
  8341. var a1 = matrix.a;
  8342. var b1 = matrix.b;
  8343. var c1 = matrix.c;
  8344. var d1 = matrix.d;
  8345. if (a != 1 || b != 0 || c != 0 || d != 1) {
  8346. matrix.a = a * a1 + b * c1;
  8347. matrix.b = a * b1 + b * d1;
  8348. matrix.c = c * a1 + d * c1;
  8349. matrix.d = c * b1 + d * d1;
  8350. }
  8351. matrix.tx = tx * a1 + ty * c1 + matrix.tx;
  8352. matrix.ty = tx * b1 + ty * d1 + matrix.ty;
  8353. };
  8354. WebGLRenderBuffer.prototype.translate = function (dx, dy) {
  8355. // this.globalMatrix.translate(dx, dy);
  8356. var matrix = this.globalMatrix;
  8357. matrix.tx += dx;
  8358. matrix.ty += dy;
  8359. };
  8360. WebGLRenderBuffer.prototype.saveTransform = function () {
  8361. // this.savedGlobalMatrix.copyFrom(this.globalMatrix);
  8362. var matrix = this.globalMatrix;
  8363. var sMatrix = this.savedGlobalMatrix;
  8364. sMatrix.a = matrix.a;
  8365. sMatrix.b = matrix.b;
  8366. sMatrix.c = matrix.c;
  8367. sMatrix.d = matrix.d;
  8368. sMatrix.tx = matrix.tx;
  8369. sMatrix.ty = matrix.ty;
  8370. };
  8371. WebGLRenderBuffer.prototype.restoreTransform = function () {
  8372. // this.globalMatrix.copyFrom(this.savedGlobalMatrix);
  8373. var matrix = this.globalMatrix;
  8374. var sMatrix = this.savedGlobalMatrix;
  8375. matrix.a = sMatrix.a;
  8376. matrix.b = sMatrix.b;
  8377. matrix.c = sMatrix.c;
  8378. matrix.d = sMatrix.d;
  8379. matrix.tx = sMatrix.tx;
  8380. matrix.ty = sMatrix.ty;
  8381. };
  8382. /**
  8383. * 创建一个buffer实例
  8384. */
  8385. WebGLRenderBuffer.create = function (width, height) {
  8386. var buffer = renderBufferPool.pop();
  8387. // width = Math.min(width, 1024);
  8388. // height = Math.min(height, 1024);
  8389. if (buffer) {
  8390. buffer.resize(width, height);
  8391. var matrix = buffer.globalMatrix;
  8392. matrix.a = 1;
  8393. matrix.b = 0;
  8394. matrix.c = 0;
  8395. matrix.d = 1;
  8396. matrix.tx = 0;
  8397. matrix.ty = 0;
  8398. }
  8399. else {
  8400. buffer = new WebGLRenderBuffer(width, height);
  8401. buffer.$computeDrawCall = false;
  8402. }
  8403. return buffer;
  8404. };
  8405. /**
  8406. * 回收一个buffer实例
  8407. */
  8408. WebGLRenderBuffer.release = function (buffer) {
  8409. renderBufferPool.push(buffer);
  8410. };
  8411. return WebGLRenderBuffer;
  8412. }(egret.HashObject));
  8413. web.WebGLRenderBuffer = WebGLRenderBuffer;
  8414. __reflect(WebGLRenderBuffer.prototype, "egret.web.WebGLRenderBuffer", ["egret.sys.RenderBuffer"]);
  8415. var renderBufferPool = []; //渲染缓冲区对象池
  8416. })(web = egret.web || (egret.web = {}));
  8417. })(egret || (egret = {}));
  8418. //////////////////////////////////////////////////////////////////////////////////////
  8419. //
  8420. // Copyright (c) 2014-present, Egret Technology.
  8421. // All rights reserved.
  8422. // Redistribution and use in source and binary forms, with or without
  8423. // modification, are permitted provided that the following conditions are met:
  8424. //
  8425. // * Redistributions of source code must retain the above copyright
  8426. // notice, this list of conditions and the following disclaimer.
  8427. // * Redistributions in binary form must reproduce the above copyright
  8428. // notice, this list of conditions and the following disclaimer in the
  8429. // documentation and/or other materials provided with the distribution.
  8430. // * Neither the name of the Egret nor the
  8431. // names of its contributors may be used to endorse or promote products
  8432. // derived from this software without specific prior written permission.
  8433. //
  8434. // THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  8435. // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  8436. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  8437. // IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  8438. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  8439. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
  8440. // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  8441. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  8442. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  8443. // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8444. //
  8445. //////////////////////////////////////////////////////////////////////////////////////
  8446. var egret;
  8447. (function (egret) {
  8448. var web;
  8449. (function (web) {
  8450. /**
  8451. * @private
  8452. */
  8453. var WebFps = (function (_super) {
  8454. __extends(WebFps, _super);
  8455. function WebFps(stage, showFPS, showLog, logFilter, styles) {
  8456. var _this = _super.call(this) || this;
  8457. _this.showPanle = true;
  8458. _this.fpsHeight = 0;
  8459. _this.WIDTH = 101;
  8460. _this.HEIGHT = 20;
  8461. _this.bgCanvasColor = "#18304b";
  8462. _this.fpsFrontColor = "#18fefe";
  8463. _this.WIDTH_COST = 33;
  8464. _this.cost1Color = "#18fefe";
  8465. _this.cost2Color = "#ffff00";
  8466. _this.cost3Color = "#ff0000";
  8467. _this.arrFps = [];
  8468. _this.arrCost = [];
  8469. _this.arrLog = [];
  8470. if (showFPS || showLog) {
  8471. if (egret.Capabilities.renderMode == 'canvas') {
  8472. _this.renderMode = "Canvas";
  8473. }
  8474. else {
  8475. _this.renderMode = "WebGL";
  8476. }
  8477. _this.panelX = styles["x"] === undefined ? 0 : parseInt(styles['x']);
  8478. _this.panelY = styles["y"] === undefined ? 0 : parseInt(styles['y']);
  8479. _this.fontColor = styles["textColor"] === undefined ? '#ffffff' : styles['textColor'].replace("0x", "#");
  8480. _this.fontSize = styles["size"] === undefined ? 12 : parseInt(styles['size']);
  8481. if (egret.Capabilities.isMobile) {
  8482. _this.fontSize -= 2;
  8483. }
  8484. var all = document.createElement('div');
  8485. all.style.position = 'absolute';
  8486. all.style.background = "rgba(0,0,0," + styles['bgAlpha'] + ")";
  8487. all.style.left = _this.panelX + 'px';
  8488. all.style.top = _this.panelY + 'px';
  8489. all.style.pointerEvents = 'none';
  8490. document.body.appendChild(all);
  8491. var container = document.createElement('div');
  8492. container.style.color = _this.fontColor;
  8493. container.style.fontSize = _this.fontSize + 'px';
  8494. container.style.lineHeight = _this.fontSize + 'px';
  8495. container.style.margin = '4px 4px 4px 4px';
  8496. _this.container = container;
  8497. all.appendChild(container);
  8498. if (showFPS)
  8499. _this.addFps();
  8500. if (showLog)
  8501. _this.addLog();
  8502. }
  8503. return _this;
  8504. }
  8505. WebFps.prototype.addFps = function () {
  8506. var div = document.createElement('div');
  8507. div.style.display = 'inline-block';
  8508. this.containerFps = div;
  8509. this.container.appendChild(div);
  8510. var fps = document.createElement('div');
  8511. fps.style.paddingBottom = '2px';
  8512. this.fps = fps;
  8513. this.containerFps.appendChild(fps);
  8514. fps.innerHTML = "0 FPS " + this.renderMode + "<br/>min0 max0 avg0";
  8515. var canvas = document.createElement('canvas');
  8516. this.containerFps.appendChild(canvas);
  8517. canvas.width = this.WIDTH;
  8518. canvas.height = this.HEIGHT;
  8519. this.canvasFps = canvas;
  8520. var context = canvas.getContext('2d');
  8521. this.contextFps = context;
  8522. context.fillStyle = this.bgCanvasColor;
  8523. context.fillRect(0, 0, this.WIDTH, this.HEIGHT);
  8524. var divDatas = document.createElement('div');
  8525. this.divDatas = divDatas;
  8526. this.containerFps.appendChild(divDatas);
  8527. var left = document.createElement('div');
  8528. left.style['float'] = 'left';
  8529. left.innerHTML = "Draw<br/>Dirty<br/>Cost";
  8530. divDatas.appendChild(left);
  8531. var right = document.createElement('div');
  8532. right.style.paddingLeft = left.offsetWidth + 20 + "px";
  8533. divDatas.appendChild(right);
  8534. var draw = document.createElement('div');
  8535. this.divDraw = draw;
  8536. draw.innerHTML = "0<br/>0<br/>";
  8537. right.appendChild(draw);
  8538. var cost = document.createElement('div');
  8539. this.divCost = cost;
  8540. cost.innerHTML = "<font style=\"color:" + this.cost1Color + "\">0<font/> <font style=\"color:" + this.cost2Color + "\">0<font/> <font style=\"color:" + this.cost3Color + "\">0<font/>";
  8541. right.appendChild(cost);
  8542. canvas = document.createElement('canvas');
  8543. this.canvasCost = canvas;
  8544. this.containerFps.appendChild(canvas);
  8545. canvas.width = this.WIDTH;
  8546. canvas.height = this.HEIGHT;
  8547. context = canvas.getContext('2d');
  8548. this.contextCost = context;
  8549. context.fillStyle = this.bgCanvasColor;
  8550. context.fillRect(0, 0, this.WIDTH, this.HEIGHT);
  8551. context.fillStyle = "#000000";
  8552. context.fillRect(this.WIDTH_COST, 0, 1, this.HEIGHT);
  8553. context.fillRect(this.WIDTH_COST * 2 + 1, 0, 1, this.HEIGHT);
  8554. this.fpsHeight = this.container.offsetHeight;
  8555. };
  8556. WebFps.prototype.addLog = function () {
  8557. var log = document.createElement('div');
  8558. log.style.maxWidth = document.body.clientWidth - 8 - this.panelX + 'px';
  8559. log.style.wordWrap = "break-word";
  8560. this.log = log;
  8561. this.container.appendChild(log);
  8562. };
  8563. WebFps.prototype.update = function (datas, showLastData) {
  8564. if (showLastData === void 0) { showLastData = false; }
  8565. var numFps;
  8566. var numCostTicker;
  8567. var numCostDirty;
  8568. var numCostRender;
  8569. if (!showLastData) {
  8570. numFps = datas.fps;
  8571. numCostTicker = datas.costTicker;
  8572. numCostDirty = datas.costDirty;
  8573. numCostRender = datas.costRender;
  8574. this.lastNumDraw = datas.draw;
  8575. this.lastNumDirty = datas.dirty;
  8576. this.arrFps.push(numFps);
  8577. this.arrCost.push([numCostTicker, numCostDirty, numCostRender]);
  8578. }
  8579. else {
  8580. numFps = this.arrFps[this.arrFps.length - 1];
  8581. numCostTicker = this.arrCost[this.arrCost.length - 1][0];
  8582. numCostDirty = this.arrCost[this.arrCost.length - 1][1];
  8583. numCostRender = this.arrCost[this.arrCost.length - 1][2];
  8584. }
  8585. var fpsTotal = 0;
  8586. var lenFps = this.arrFps.length;
  8587. if (lenFps > 101) {
  8588. lenFps = 101;
  8589. this.arrFps.shift();
  8590. }
  8591. var fpsMin = this.arrFps[0];
  8592. var fpsMax = this.arrFps[0];
  8593. for (var i = 0; i < lenFps; i++) {
  8594. var num = this.arrFps[i];
  8595. fpsTotal += num;
  8596. if (num < fpsMin)
  8597. fpsMin = num;
  8598. else if (num > fpsMax)
  8599. fpsMax = num;
  8600. }
  8601. var WIDTH = this.WIDTH;
  8602. var HEIGHT = this.HEIGHT;
  8603. var context = this.contextFps;
  8604. context.drawImage(this.canvasFps, 1, 0, WIDTH - 1, HEIGHT, 0, 0, WIDTH - 1, HEIGHT);
  8605. context.fillStyle = this.bgCanvasColor;
  8606. context.fillRect(WIDTH - 1, 0, 1, HEIGHT);
  8607. var lastHeight = Math.floor(numFps / 60 * 20);
  8608. if (lastHeight < 1)
  8609. lastHeight = 1;
  8610. context.fillStyle = this.fpsFrontColor;
  8611. context.fillRect(WIDTH - 1, 20 - lastHeight, 1, lastHeight);
  8612. var WIDTH_COST = this.WIDTH_COST;
  8613. context = this.contextCost;
  8614. context.drawImage(this.canvasCost, 1, 0, WIDTH_COST - 1, HEIGHT, 0, 0, WIDTH_COST - 1, HEIGHT);
  8615. context.drawImage(this.canvasCost, WIDTH_COST + 2, 0, WIDTH_COST - 1, HEIGHT, WIDTH_COST + 1, 0, WIDTH_COST - 1, HEIGHT);
  8616. context.drawImage(this.canvasCost, WIDTH_COST * 2 + 3, 0, WIDTH_COST - 1, HEIGHT, WIDTH_COST * 2 + 2, 0, WIDTH_COST - 1, HEIGHT);
  8617. var c1Height = Math.floor(numCostTicker / 2);
  8618. if (c1Height < 1)
  8619. c1Height = 1;
  8620. else if (c1Height > 20)
  8621. c1Height = 20;
  8622. var c2Height = Math.floor(numCostDirty / 2);
  8623. if (c2Height < 1)
  8624. c2Height = 1;
  8625. else if (c2Height > 20)
  8626. c2Height = 20;
  8627. var c3Height = Math.floor(numCostRender / 2);
  8628. if (c3Height < 1)
  8629. c3Height = 1;
  8630. else if (c3Height > 20)
  8631. c3Height = 20;
  8632. context.fillStyle = this.bgCanvasColor;
  8633. context.fillRect(WIDTH_COST - 1, 0, 1, HEIGHT);
  8634. context.fillRect(WIDTH_COST * 2, 0, 1, HEIGHT);
  8635. context.fillRect(WIDTH_COST * 3 + 1, 0, 1, HEIGHT);
  8636. context.fillStyle = this.cost1Color;
  8637. context.fillRect(WIDTH_COST - 1, 20 - c1Height, 1, c1Height);
  8638. context.fillStyle = this.cost2Color;
  8639. context.fillRect(WIDTH_COST * 2, 20 - c2Height, 1, c2Height);
  8640. context.fillStyle = this.cost3Color;
  8641. context.fillRect(WIDTH_COST * 3 + 1, 20 - c3Height, 1, c3Height);
  8642. var fpsAvg = Math.floor(fpsTotal / lenFps);
  8643. var fpsOutput = numFps + " FPS " + this.renderMode;
  8644. if (this.showPanle) {
  8645. fpsOutput += "<br/>min" + fpsMin + " max" + fpsMax + " avg" + fpsAvg;
  8646. this.divDraw.innerHTML = this.lastNumDraw + "<br/>" + this.lastNumDirty + "%<br/>";
  8647. this.divCost.innerHTML = "<font style=\"color:#18fefe\">" + numCostTicker + "<font/> <font style=\"color:#ffff00\">" + numCostDirty + "<font/> <font style=\"color:#ff0000\">" + numCostRender + "<font/>";
  8648. }
  8649. this.fps.innerHTML = fpsOutput;
  8650. };
  8651. ;
  8652. WebFps.prototype.updateInfo = function (info) {
  8653. this.arrLog.push(info);
  8654. this.log.innerHTML = this.arrLog.join('<br/>');
  8655. while (document.body.clientHeight < (this.log.offsetHeight + this.fpsHeight + this.panelY + this.fontSize * 2)) {
  8656. this.arrLog.shift();
  8657. this.log.innerHTML = this.arrLog.join('<br/>');
  8658. }
  8659. };
  8660. return WebFps;
  8661. }(egret.DisplayObject));
  8662. web.WebFps = WebFps;
  8663. __reflect(WebFps.prototype, "egret.web.WebFps", ["egret.FPSDisplay"]);
  8664. egret.FPSDisplay = WebFps;
  8665. })(web = egret.web || (egret.web = {}));
  8666. })(egret || (egret = {}));