mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
Merge pull request #2571 from dumganhar/optimize
window.__gl -> window.__ccgl, window.canvas -> window.__cccanvas. Touch logic is moved to EventTarget
This commit is contained in:
@@ -97,7 +97,7 @@ var GL_COMMAND_VERTEX_ATTRIB_4FV = 95;
|
||||
var GL_COMMAND_VERTEX_ATTRIB_POINTER = 96;
|
||||
var GL_COMMAND_VIEW_PORT = 97;
|
||||
|
||||
const gl = __gl;
|
||||
const gl = __ccgl;
|
||||
|
||||
// _gl save the orignal gl functions.
|
||||
var _gl = {};
|
||||
@@ -127,10 +127,10 @@ function batchGLCommandsToNative() {
|
||||
}
|
||||
|
||||
function disableBatchGLCommandsToNative() {
|
||||
// Reset __gl variable to the default one.
|
||||
// Reset __ccgl variable to the default one.
|
||||
flushCommands();
|
||||
for (var k in _gl) {
|
||||
__gl[k] = _gl[k];
|
||||
__ccgl[k] = _gl[k];
|
||||
}
|
||||
console.log('Disable batch GL commands optimization!');
|
||||
}
|
||||
|
||||
@@ -23,6 +23,12 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var __targetID = 0;
|
||||
var __listenTouchEventMap = {};
|
||||
|
||||
const __touchEventNames = ['touchstart', 'touchmove', 'touchend', 'touchcancel'];
|
||||
|
||||
|
||||
// Listener types
|
||||
const CAPTURE = 1
|
||||
const BUBBLE = 2
|
||||
@@ -47,12 +53,13 @@ function isObject(x) {
|
||||
*
|
||||
* class A extends EventTarget {}
|
||||
*/
|
||||
function EventTarget() {
|
||||
this._listeners = new Map();
|
||||
}
|
||||
class EventTarget {
|
||||
constructor() {
|
||||
this._targetID = ++__targetID;
|
||||
this._touchListenerCount = 0;
|
||||
this._listeners = new Map();
|
||||
}
|
||||
|
||||
// Should be enumerable, but class methods are not enumerable.
|
||||
EventTarget.prototype = {
|
||||
/**
|
||||
* Add a given listener to this event target.
|
||||
* @param {string} eventName The event name to add.
|
||||
@@ -84,6 +91,11 @@ EventTarget.prototype = {
|
||||
let node = listeners.get(eventName)
|
||||
if (node === undefined) {
|
||||
listeners.set(eventName, newNode)
|
||||
if (__touchEventNames.indexOf(eventName) > -1) {
|
||||
if (this._touchListenerCount === 0)
|
||||
__listenTouchEventMap[this._targetID] = this;
|
||||
++this._touchListenerCount;
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -100,8 +112,13 @@ EventTarget.prototype = {
|
||||
|
||||
// Add it.
|
||||
prev.next = newNode
|
||||
if (__touchEventNames.indexOf(eventName) > -1) {
|
||||
if (this._touchListenerCount === 0)
|
||||
__listenTouchEventMap[this._targetID] = this;
|
||||
++this._touchListenerCount;
|
||||
}
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a given listener from this event target.
|
||||
@@ -132,6 +149,13 @@ EventTarget.prototype = {
|
||||
else {
|
||||
listeners.delete(eventName)
|
||||
}
|
||||
|
||||
if (__touchEventNames.indexOf(eventName) > -1) {
|
||||
--this._touchListenerCount;
|
||||
if (this._touchListenerCount <= 0)
|
||||
delete __listenTouchEventMap[this._targetID];
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -140,7 +164,7 @@ EventTarget.prototype = {
|
||||
}
|
||||
|
||||
return false
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch a given event.
|
||||
@@ -213,17 +237,29 @@ EventTarget.prototype = {
|
||||
event._passiveListener = null
|
||||
|
||||
return !event.defaultPrevented
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// `constructor` is not enumerable.
|
||||
Object.defineProperty(EventTarget.prototype, "constructor", { value: EventTarget, configurable: true, writable: true })
|
||||
function touchEventHandlerFactory(type) {
|
||||
return (touches) => {
|
||||
const touchEvent = new TouchEvent(type)
|
||||
|
||||
// Ensure `eventTarget instanceof window.EventTarget` is `true`.
|
||||
if (typeof window !== "undefined" && typeof window.EventTarget !== "undefined") {
|
||||
Object.setPrototypeOf(EventTarget.prototype, window.EventTarget.prototype)
|
||||
touchEvent.touches = touches;
|
||||
touchEvent.targetTouches = Array.prototype.slice.call(touchEvent.touches)
|
||||
touchEvent.changedTouches = touches;//event.changedTouches
|
||||
// touchEvent.timeStamp = event.timeStamp
|
||||
|
||||
for (let key in __listenTouchEventMap) {
|
||||
__listenTouchEventMap[key].dispatchEvent(touchEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
jsb.onTouchStart = touchEventHandlerFactory('touchstart');
|
||||
jsb.onTouchMove = touchEventHandlerFactory('touchmove');
|
||||
jsb.onTouchEnd = touchEventHandlerFactory('touchend');
|
||||
jsb.onTouchCancel = touchEventHandlerFactory('touchcancel');
|
||||
|
||||
// export { defineEventAttribute, EventTarget }
|
||||
// export default EventTarget
|
||||
module.exports = EventTarget
|
||||
|
||||
@@ -47,168 +47,6 @@ class TextMetrics {
|
||||
}
|
||||
}
|
||||
|
||||
// class CanvasRenderingContext2D {
|
||||
// constructor() {
|
||||
// console.log("==> CanvasRenderingContext2D constructor");
|
||||
|
||||
// // Line styles
|
||||
// this.lineWidth = 1;
|
||||
// this.lineCap = 'butt';
|
||||
// this.lineJoin = 'miter';
|
||||
// this.miterLimit = 10;
|
||||
// this.lineDashOffset = 0;
|
||||
|
||||
// // Text styles
|
||||
// this.font = '10px sans-serif';
|
||||
// this.textAlign = 'start';
|
||||
// this.textBaseline = 'alphabetic';
|
||||
|
||||
// // Fill and stroke styles
|
||||
// this.fillStyle = '#000';
|
||||
// this.strokeStyle = '#000';
|
||||
|
||||
// // Shadows
|
||||
// this.shadowBlur = 0;
|
||||
// this.shadowColor = 'black';
|
||||
// this.shadowOffsetX = 0;
|
||||
// this.shadowOffsetY = 0;
|
||||
|
||||
// // Compositing
|
||||
// this.globalAlpha = 1;
|
||||
// this.globalCompositeOperation = 'source-over';
|
||||
|
||||
// }
|
||||
|
||||
// clearRect(x, y, width, height) {
|
||||
// console.log(`==> CanvasRenderingContext2D clearRect: [${x}, ${y}, ${width}, ${height}]`);
|
||||
// }
|
||||
|
||||
// fillRect(x, y, width, height) {
|
||||
// console.log(`==> CanvasRenderingContext2D fillRect: [${x}, ${y}, ${width}, ${height}]`);
|
||||
// }
|
||||
|
||||
// strokeRect(x, y, width, height) {
|
||||
// console.log(`==> CanvasRenderingContext2D strokeRect: [${x}, ${y}, ${width}, ${height}]`);
|
||||
// }
|
||||
|
||||
// getLineDash() {
|
||||
// return [];
|
||||
// }
|
||||
|
||||
// setLineDash(segments) {
|
||||
|
||||
// }
|
||||
|
||||
// drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight) {
|
||||
// console.log("==> CanvasRenderingContext2D drawImage");
|
||||
// }
|
||||
|
||||
// getImageData(sx, sy, sw, sh) {
|
||||
// console.log(`==> CanvasRenderingContext2D getImageData(${sx}, ${sy}, ${sw}, ${sh})`);
|
||||
// let data = new Uint8ClampedArray(sw * sh * 4);
|
||||
// for (let i = 0; i < data.length; ++i) {
|
||||
// data[i] = Math.floor(Math.random() * 255);
|
||||
// }
|
||||
// return {
|
||||
// width: sw,
|
||||
// height: sh,
|
||||
// data: data
|
||||
// };
|
||||
// }
|
||||
|
||||
// fillText(text, x, y, maxWidth) {
|
||||
// console.log("==> CanvasRenderingContext2D fillText: " + text
|
||||
// + ', font: ' + this.font + ', textAlign: ' + this.textAlign
|
||||
// + ', textBaseline: ' + this.textBaseline);
|
||||
// }
|
||||
|
||||
// strokeText(text, x, y, maxWidth) {
|
||||
// console.log("==> CanvasRenderingContext2D strokeText" + text
|
||||
// + ', font: ' + this.font + ', textAlign: ' + this.textAlign
|
||||
// + ', textBaseline: ' + this.textBaseline);
|
||||
// }
|
||||
|
||||
// measureText(text) {
|
||||
// console.log("==> CanvasRenderingContext2D measureText: " + text
|
||||
// + ', font: ' + this.font + ', textAlign: ' + this.textAlign
|
||||
// + ', textBaseline: ' + this.textBaseline);
|
||||
// if (text === '') {
|
||||
// new Error("measureText empty");
|
||||
// }
|
||||
// return new TextMetrics(100);
|
||||
// }
|
||||
|
||||
// // Gradients and patterns
|
||||
// createLinearGradient(x0, y0, x1, y1) {
|
||||
// console.log("==> CanvasRenderingContext2D createLinearGradient");
|
||||
// return new CanvasGradient();
|
||||
// }
|
||||
|
||||
// createRadialGradient(x0, y0, r0, x1, y1, r1) {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// createPattern(image, repetition) {
|
||||
// return null;
|
||||
// }
|
||||
|
||||
// save() {
|
||||
// console.log("==> CanvasRenderingContext2D save");
|
||||
// }
|
||||
|
||||
// // Paths
|
||||
// beginPath() {
|
||||
// console.log("==> CanvasRenderingContext2D beginPath");
|
||||
// }
|
||||
|
||||
// closePath() {
|
||||
|
||||
// }
|
||||
|
||||
// moveTo() {
|
||||
// console.log("==> CanvasRenderingContext2D moveTo");
|
||||
// }
|
||||
|
||||
// lineTo() {
|
||||
// console.log("==> CanvasRenderingContext2D lineTo");
|
||||
// }
|
||||
|
||||
// bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y) {
|
||||
|
||||
// }
|
||||
|
||||
// quadraticCurveTo(cpx, cpy, x, y) {
|
||||
|
||||
// }
|
||||
|
||||
// arc(x, y, radius, startAngle, endAngle, anticlockwise) {
|
||||
|
||||
// }
|
||||
|
||||
// rect(x, y, width, height) {
|
||||
|
||||
// }
|
||||
|
||||
// // Drawing paths
|
||||
// stroke() {
|
||||
// console.log("==> CanvasRenderingContext2D stroke");
|
||||
// }
|
||||
|
||||
// /*
|
||||
// void ctx.fill([fillRule]);
|
||||
// void ctx.fill(path[, fillRule]);
|
||||
// */
|
||||
// fill() {
|
||||
|
||||
// }
|
||||
|
||||
// restore() {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
const __listenTouchEventCanvasMap = {};
|
||||
|
||||
class HTMLCanvasElement extends HTMLElement {
|
||||
constructor(width, height) {
|
||||
super('canvas')
|
||||
@@ -229,7 +67,7 @@ class HTMLCanvasElement extends HTMLElement {
|
||||
var self = this;
|
||||
// console.log(`==> Canvas getContext(${name})`);
|
||||
if (name === 'webgl' || name === 'experimental-webgl') {
|
||||
return window.__gl;
|
||||
return window.__ccgl;
|
||||
} else if (name === '2d') {
|
||||
if (!this._context2D) {
|
||||
this._context2D = new CanvasRenderingContext2D(this._width, this._height);
|
||||
@@ -245,6 +83,7 @@ class HTMLCanvasElement extends HTMLElement {
|
||||
}
|
||||
|
||||
toDataURL() {
|
||||
//TODO:
|
||||
console.log("==> Canvas toDataURL");
|
||||
return "";
|
||||
}
|
||||
@@ -290,28 +129,6 @@ class HTMLCanvasElement extends HTMLElement {
|
||||
getBoundingClientRect() {
|
||||
return new DOMRect(0, 0, this._width, this._height);
|
||||
}
|
||||
|
||||
addEventListener(eventName, listener, options) {
|
||||
let ret = super.addEventListener(eventName, listener, options);
|
||||
if (ret) {
|
||||
if (eventName === 'touchstart') {
|
||||
__listenTouchEventCanvasMap[this._index] = this;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
removeEventListener(eventName, listener, options) {
|
||||
let ret = super.removeEventListener(eventName, listener, options);
|
||||
if (ret) {
|
||||
if (eventName === 'touchstart') {
|
||||
delete __listenTouchEventCanvasMap[this._index];
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
var ctx2DProto = CanvasRenderingContext2D.prototype;
|
||||
@@ -336,25 +153,5 @@ ctx2DProto.drawImage = function(image, sx, sy, sWidth, sHeight, dx, dy, dWidth,
|
||||
ctx2DProto.bezierCurveTo = function() {}
|
||||
ctx2DProto.fill = function() {}
|
||||
|
||||
function touchEventHandlerFactory(type) {
|
||||
return (touches) => {
|
||||
const touchEvent = new TouchEvent(type)
|
||||
|
||||
touchEvent.touches = touches;
|
||||
touchEvent.targetTouches = Array.prototype.slice.call(touchEvent.touches)
|
||||
touchEvent.changedTouches = touches;//event.changedTouches
|
||||
// touchEvent.timeStamp = event.timeStamp
|
||||
|
||||
for (let key in __listenTouchEventCanvasMap) {
|
||||
__listenTouchEventCanvasMap[key].dispatchEvent(touchEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
jsb.onTouchStart = touchEventHandlerFactory('touchstart');
|
||||
jsb.onTouchMove = touchEventHandlerFactory('touchmove');
|
||||
jsb.onTouchEnd = touchEventHandlerFactory('touchend');
|
||||
jsb.onTouchCancel = touchEventHandlerFactory('touchcancel');
|
||||
|
||||
module.exports = HTMLCanvasElement;
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ class HTMLElement extends Element {
|
||||
this.insertBefore = noop
|
||||
|
||||
this.innerHTML = ''
|
||||
this.parentElement = window.canvas
|
||||
this.parentElement = window.__cccanvas
|
||||
}
|
||||
|
||||
setAttribute(name, value) {
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
const HTMLElement = require('./HTMLElement');
|
||||
const Event = require('./Event');
|
||||
const gl = window.__gl;
|
||||
const gl = window.__ccgl;
|
||||
|
||||
class HTMLImageElement extends HTMLElement {
|
||||
constructor(width, height) {
|
||||
|
||||
@@ -35,6 +35,7 @@ class Document extends Node {
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
|
||||
this.readyState = 'complete'
|
||||
this.visibilityState = 'visible'
|
||||
this.documentElement = window
|
||||
@@ -69,8 +70,8 @@ class Document extends Node {
|
||||
}
|
||||
|
||||
getElementById(id) {
|
||||
if (id === window.canvas.id || id === 'canvas') {
|
||||
return window.canvas
|
||||
if (id === window.__cccanvas.id || id === 'canvas') {
|
||||
return window.__cccanvas
|
||||
}
|
||||
return new HTMLElement(id);
|
||||
}
|
||||
@@ -81,7 +82,7 @@ class Document extends Node {
|
||||
} else if (tagName === 'body') {
|
||||
return [document.body]
|
||||
} else if (tagName === 'canvas') {
|
||||
return [window.canvas]
|
||||
return [window.__cccanvas]
|
||||
}
|
||||
return [new HTMLElement(tagName)]
|
||||
}
|
||||
@@ -92,7 +93,7 @@ class Document extends Node {
|
||||
} else if (tagName === 'body') {
|
||||
return [document.body]
|
||||
} else if (tagName === 'canvas') {
|
||||
return [window.canvas]
|
||||
return [window.__cccanvas]
|
||||
}
|
||||
return [new HTMLElement(tagName)]
|
||||
}
|
||||
@@ -103,9 +104,9 @@ class Document extends Node {
|
||||
} else if (query === 'body') {
|
||||
return document.body
|
||||
} else if (query === 'canvas') {
|
||||
return window.canvas
|
||||
} else if (query === `#${window.canvas.id}`) {
|
||||
return window.canvas
|
||||
return window.__cccanvas
|
||||
} else if (query === `#${window.__cccanvas.id}`) {
|
||||
return window.__cccanvas
|
||||
}
|
||||
return new HTMLElement(query);
|
||||
}
|
||||
@@ -116,7 +117,7 @@ class Document extends Node {
|
||||
} else if (query === 'body') {
|
||||
return [document.body]
|
||||
} else if (query === 'canvas') {
|
||||
return [window.canvas]
|
||||
return [window.__cccanvas]
|
||||
}
|
||||
return [new HTMLElement(query)];
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ function inject () {
|
||||
window.ontouchcancel = null;
|
||||
|
||||
window.pageXOffset = window.pageYOffset = window.clientTop = window.clientLeft = 0;
|
||||
window.outerWidth = window.innerWidth;
|
||||
window.outerHeight = window.innerHeight;
|
||||
|
||||
window.location = require('./location');
|
||||
window.document = require('./document');
|
||||
@@ -42,8 +44,8 @@ function inject () {
|
||||
window.HTMLMediaElement = require('./HTMLMediaElement');
|
||||
window.HTMLAudioElement = require('./HTMLAudioElement');
|
||||
window.HTMLVideoElement = require('./HTMLVideoElement');
|
||||
window.canvas = new HTMLCanvasElement();
|
||||
window.__gl.canvas = window.canvas;
|
||||
window.__cccanvas = new HTMLCanvasElement();
|
||||
window.__ccgl.canvas = window.__cccanvas;
|
||||
window.navigator = require('./navigator');
|
||||
window.Image = require('./Image');
|
||||
window.Audio = require('./Audio');
|
||||
@@ -73,15 +75,15 @@ function inject () {
|
||||
};
|
||||
|
||||
window.addEventListener = function(eventName, listener, options) {
|
||||
window.canvas.addEventListener(eventName, listener, options);
|
||||
window.__cccanvas.addEventListener(eventName, listener, options);
|
||||
};
|
||||
|
||||
window.removeEventListener = function(eventName, listener, options) {
|
||||
window.canvas.removeEventListener(eventName, listener, options);
|
||||
window.__cccanvas.removeEventListener(eventName, listener, options);
|
||||
};
|
||||
|
||||
window.dispatchEvent = function(event) {
|
||||
window.canvas.dispatchEvent(event);
|
||||
window.__cccanvas.dispatchEvent(event);
|
||||
};
|
||||
|
||||
window.getComputedStyle = function(element) {
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
|
||||
require('./jsb_opengl_constants');
|
||||
|
||||
const gl = __gl;
|
||||
const gl = __ccgl;
|
||||
|
||||
gl.drawingBufferWidth = window.innerWidth;
|
||||
gl.drawingBufferHeight = window.innerHeight;
|
||||
@@ -147,7 +147,7 @@ gl.texSubImage2D = function(target, level, xoffset, yoffset, width, height, form
|
||||
gl.getContextAttributes = function() {
|
||||
return {
|
||||
alpha: true,
|
||||
antialias: true,
|
||||
antialias: false,
|
||||
depth: true,
|
||||
failIfMajorPerformanceCaveat: false,
|
||||
premultipliedAlpha: true,
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
const gl = __gl;
|
||||
const gl = __ccgl;
|
||||
|
||||
gl.GCCSO_SHADER_BINARY_FJ = 0x9260;
|
||||
gl._3DC_XY_AMD = 0x87fa;
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ let {enums, glTextureFmt} = require("./enums");
|
||||
let VertexFormat = require("./vertex-format-jsb");
|
||||
|
||||
window.device = gfx.Device.getInstance();
|
||||
window.device._gl = window.gl;
|
||||
window.device._gl = window.__ccgl;
|
||||
|
||||
//FIXME:
|
||||
window.device._stats = { vb: 0 };
|
||||
|
||||
+1
-1
@@ -105,7 +105,7 @@ var _p;
|
||||
|
||||
_p = renderer.ForwardRenderer.prototype;
|
||||
_p._ctor = function(device, builtin) {
|
||||
this.init(device, builtin.programTemplates, builtin.defaultTexture, canvas.width, canvas.height);
|
||||
this.init(device, builtin.programTemplates, builtin.defaultTexture, window.innerWidth, window.innerHeight);
|
||||
};
|
||||
_p.render = function(scene) {
|
||||
fillModelData();
|
||||
|
||||
Reference in New Issue
Block a user