diff --git a/cocos2d/core/renderer/canvas/renderers/graphics/impl.js b/cocos2d/core/renderer/canvas/renderers/graphics/impl.js index 9ecc8255e6..92092057f2 100644 --- a/cocos2d/core/renderer/canvas/renderers/graphics/impl.js +++ b/cocos2d/core/renderer/canvas/renderers/graphics/impl.js @@ -25,103 +25,53 @@ const Helper = require('../../../../graphics/helper'); const Types = require('../../../../graphics/types'); +const js = require('../../../../platform/js'); const PointFlags = Types.PointFlags; const LineJoin = Types.LineJoin; const LineCap = Types.LineCap; -class Impl { - constructor () { - this.cmds = []; +function Impl () { + this.cmds = []; - this.style = { - strokeStyle: 'black', - fillStyle: 'white', - lineCap: 'butt', - lineJoin: 'miter', - miterLimit: 10 - }; - } + this.style = { + strokeStyle: 'black', + fillStyle: 'white', + lineCap: 'butt', + lineJoin: 'miter', + miterLimit: 10 + }; +} - set strokeColor (v) { - var strokeStyle = 'rgba(' + (0 | v.r) + ',' + (0 | v.g) + ',' + (0 | v.b) + ',' + v.a / 255 + ')'; - this.cmds.push(['strokeStyle', strokeStyle]); - this.style.strokeStyle = strokeStyle; - } - - set fillColor (v) { - var fillStyle = 'rgba(' + (0 | v.r) + ',' + (0 | v.g) + ',' + (0 | v.b) + ',' + v.a / 255 + ')'; - this.cmds.push(['fillStyle', fillStyle]); - this.style.fillStyle = fillStyle; - } - - set lineWidth (v) { - this.cmds.push(['lineWidth', v]); - this.style.lineWidth = v; - } - - set lineCap (v) { - var lineCap = 'butt'; - if (v === LineCap.BUTT) { - lineCap = 'butt'; - } - else if (v === LineCap.ROUND) { - lineCap = 'round'; - } - else if (v === LineCap.SQUARE) { - lineCap = 'square'; - } - this.cmds.push(['lineCap', lineCap]); - this.style.lineCap = lineCap; - } - - set lineJoin (v) { - var lineJoin = 'bevel'; - if (v === LineJoin.BEVEL) { - lineJoin = 'bevel'; - } - else if (v === LineJoin.ROUND) { - lineJoin = 'round'; - } - else if (v === LineJoin.MITER) { - lineJoin = 'miter'; - } - - this.cmds.push(['lineJoin', lineJoin]); - this.style.lineJoin = lineJoin; - } - - set miterLimit (v) { - this.cmds.push(['miterLimit', v]); - this.style.miterLimit = v; - } +let _proto = Impl.prototype; +js.mixin(_proto, { moveTo (x, y) { this.cmds.push(['moveTo', [x, y]]); - } + }, lineTo (x, y) { this.cmds.push(['lineTo', [x, y]]); - } + }, bezierCurveTo (c1x, c1y, c2x, c2y, x, y) { this.cmds.push(['bezierCurveTo', [c1x, c1y, c2x, c2y, x, y]]); - } + }, quadraticCurveTo (cx, cy, x, y) { this.cmds.push(['quadraticCurveTo', [cx, cy, x, y]]); - } + }, arc (cx, cy, r, startAngle, endAngle, counterclockwise) { Helper.arc(this, cx, cy, r, startAngle, endAngle, counterclockwise); - } + }, ellipse (cx, cy, rx, ry) { Helper.ellipse(this, cx, cy, rx, ry); - } + }, circle (cx, cy, r) { Helper.ellipse(this, cx, cy, r, r); - } + }, rect (x, y, w, h) { this.moveTo(x, y); @@ -129,27 +79,81 @@ class Impl { this.lineTo(x + w, y + h); this.lineTo(x + w, y); this.close(); - } + }, roundRect (x, y, w, h, r) { Helper.roundRect(this, x, y, w, h, r); - } + }, clear (comp, clean) { this.cmds.length = 0; - } + }, close () { this.cmds.push(['closePath', []]); - } + }, stroke () { this.cmds.push(['stroke', []]); - } + }, fill () { this.cmds.push(['fill', []]); } -} +}); + +js.set(_proto, 'strokeColor', function (v) { + var strokeStyle = 'rgba(' + (0 | v.r) + ',' + (0 | v.g) + ',' + (0 | v.b) + ',' + v.a / 255 + ')'; + this.cmds.push(['strokeStyle', strokeStyle]); + this.style.strokeStyle = strokeStyle; +}); + +js.set(_proto, 'fillColor', function (v) { + var fillStyle = 'rgba(' + (0 | v.r) + ',' + (0 | v.g) + ',' + (0 | v.b) + ',' + v.a / 255 + ')'; + this.cmds.push(['fillStyle', fillStyle]); + this.style.fillStyle = fillStyle; +}); + +js.set(_proto, 'lineWidth', function (v) { + this.cmds.push(['lineWidth', v]); + this.style.lineWidth = v; +}); + + +js.set(_proto, 'lineCap', function (v) { + var lineCap = 'butt'; + if (v === LineCap.BUTT) { + lineCap = 'butt'; + } + else if (v === LineCap.ROUND) { + lineCap = 'round'; + } + else if (v === LineCap.SQUARE) { + lineCap = 'square'; + } + this.cmds.push(['lineCap', lineCap]); + this.style.lineCap = lineCap; +}); + +js.set(_proto, 'lineJoin', function (v) { + var lineJoin = 'bevel'; + if (v === LineJoin.BEVEL) { + lineJoin = 'bevel'; + } + else if (v === LineJoin.ROUND) { + lineJoin = 'round'; + } + else if (v === LineJoin.MITER) { + lineJoin = 'miter'; + } + + this.cmds.push(['lineJoin', lineJoin]); + this.style.lineJoin = lineJoin; +}); + +js.set(_proto, 'miterLimit', function (v) { + this.cmds.push(['miterLimit', v]); + this.style.miterLimit = v; +}); module.exports = Impl; diff --git a/cocos2d/core/renderer/utils/dynamic-atlas/atlas.js b/cocos2d/core/renderer/utils/dynamic-atlas/atlas.js index e16f3ec84e..00f65b60da 100644 --- a/cocos2d/core/renderer/utils/dynamic-atlas/atlas.js +++ b/cocos2d/core/renderer/utils/dynamic-atlas/atlas.js @@ -1,24 +1,24 @@ const space = 2; -class Atlas { - constructor (width, height) { - let texture = new cc.RenderTexture(); - texture.initWithSize(width, height); - texture.update(); - - this._texture = texture; +function Atlas (width, height) { + let texture = new cc.RenderTexture(); + texture.initWithSize(width, height); + texture.update(); + + this._texture = texture; - this._x = space; - this._y = space; - this._nexty = space; + this._x = space; + this._y = space; + this._nexty = space; - this._width = width; - this._height = height; + this._width = width; + this._height = height; - this._innerTextureInfos = {}; - this._innerSpriteFrames = []; - } + this._innerTextureInfos = {}; + this._innerSpriteFrames = []; +} +cc.js.mixin(Atlas.prototype, { insertSpriteFrame (spriteFrame) { let rect = spriteFrame._rect, texture = spriteFrame._texture, @@ -79,13 +79,13 @@ class Atlas { this._innerSpriteFrames.push(spriteFrame); return true; - } + }, update () { if (!this._dirty) return; this._texture.update(); this._dirty = false; - } + }, reset () { this._x = space; @@ -107,12 +107,12 @@ class Atlas { } this._innerSpriteFrames.length = 0; this._innerTextureInfos = {}; - } + }, destroy () { this.reset(); this._texture.destroy(); } -} +}); module.exports = Atlas; diff --git a/cocos2d/core/renderer/webgl/assemblers/graphics/impl.js b/cocos2d/core/renderer/webgl/assemblers/graphics/impl.js index 7580931ece..a51c1eaf9c 100644 --- a/cocos2d/core/renderer/webgl/assemblers/graphics/impl.js +++ b/cocos2d/core/renderer/webgl/assemblers/graphics/impl.js @@ -2,11 +2,12 @@ const Helper = require('../../../../graphics/helper'); const PointFlags = require('../../../../graphics/types').PointFlags; -class Point extends cc.Vec2 { - constructor (x, y) { - super(x, y); +let Point = cc.Class({ + extends: cc.Vec2, + + ctor (x, y) { this.reset(); - } + }, reset () { this.dx = 0; @@ -16,13 +17,13 @@ class Point extends cc.Vec2 { this.flags = 0; this.len = 0; } +}); + +function Path () { + this.reset(); } -class Path { - constructor () { - this.reset(); - } - +cc.js.mixin(Path.prototype, { reset () { this.closed = false; this.nbevel = 0; @@ -35,33 +36,33 @@ class Path { this.points = []; } } +}); + +function Impl () { + // inner properties + this._tessTol = 0.25; + this._distTol = 0.01; + this._updatePathOffset = false; + + this._paths = null; + this._pathLength = 0; + this._pathOffset = 0; + + this._points = null; + this._pointsOffset = 0; + + this._commandx = 0; + this._commandy = 0; + + this._paths = []; + this._points = []; + + this._renderDatas = []; + + this._dataOffset = 0; } -class Impl { - constructor () { - // inner properties - this._tessTol = 0.25; - this._distTol = 0.01; - this._updatePathOffset = false; - - this._paths = null; - this._pathLength = 0; - this._pathOffset = 0; - - this._points = null; - this._pointsOffset = 0; - - this._commandx = 0; - this._commandy = 0; - - this._paths = []; - this._points = []; - - this._renderDatas = []; - - this._dataOffset = 0; - } - +cc.js.mixin(Impl.prototype, { moveTo (x, y) { if (this._updatePathOffset) { this._pathOffset = this._pathLength; @@ -73,14 +74,14 @@ class Impl { this._commandx = x; this._commandy = y; - } + }, lineTo (x, y) { this._addPoint(x, y, PointFlags.PT_CORNER); this._commandx = x; this._commandy = y; - } + }, bezierCurveTo (c1x, c1y, c2x, c2y, x, y) { var path = this._curPath; @@ -95,27 +96,27 @@ class Impl { this._commandx = x; this._commandy = y; - } + }, quadraticCurveTo (cx, cy, x, y) { var x0 = this._commandx; var y0 = this._commandy; this.bezierCurveTo(x0 + 2.0 / 3.0 * (cx - x0), y0 + 2.0 / 3.0 * (cy - y0), x + 2.0 / 3.0 * (cx - x), y + 2.0 / 3.0 * (cy - y), x, y); - } + }, arc (cx, cy, r, startAngle, endAngle, counterclockwise) { Helper.arc(this, cx, cy, r, startAngle, endAngle, counterclockwise); - } + }, ellipse (cx, cy, rx, ry) { Helper.ellipse(this, cx, cy, rx, ry); this._curPath.complex = false; - } + }, circle (cx, cy, r) { Helper.ellipse(this, cx, cy, r, r); this._curPath.complex = false; - } + }, rect (x, y, w, h) { this.moveTo(x, y); @@ -124,12 +125,12 @@ class Impl { this.lineTo(x + w, y); this.close(); this._curPath.complex = false; - } + }, roundRect (x, y, w, h, r) { Helper.roundRect(this, x, y, w, h, r); this._curPath.complex = false; - } + }, clear (comp, clean) { this._pathLength = 0; @@ -157,11 +158,11 @@ class Impl { data.vertexCount = 0; } } - } + }, close () { this._curPath.closed = true; - } + }, _addPath () { var offset = this._pathLength; @@ -179,7 +180,7 @@ class Impl { this._curPath = path; return path; - } + }, _addPoint (x, y, flags) { var path = this._curPath; @@ -203,6 +204,6 @@ class Impl { pt.flags = flags; pathPoints.push(pt); } -} +}); module.exports = Impl; diff --git a/cocos2d/core/renderer/webgl/mesh-buffer.js b/cocos2d/core/renderer/webgl/mesh-buffer.js index 9615794efe..422d6022ba 100644 --- a/cocos2d/core/renderer/webgl/mesh-buffer.js +++ b/cocos2d/core/renderer/webgl/mesh-buffer.js @@ -1,8 +1,8 @@ const renderEngine = require('../render-engine'); const gfx = renderEngine.gfx; -class MeshBuffer { - constructor (renderer, vertexFormat) { +let MeshBuffer = cc.Class({ + ctor (renderer, vertexFormat) { this.byteStart = 0; this.byteOffset = 0; this.indiceStart = 0; @@ -38,7 +38,7 @@ class MeshBuffer { this._initIDataCount = 256 * 6; this._reallocBuffer(); - } + }, uploadData () { if (this.byteOffset === 0) { @@ -54,7 +54,7 @@ class MeshBuffer { let ib = this._ib; ib.update(0, indicesData); - } + }, request (vertexCount, indiceCount) { if (this._renderer._buffer !== this) { @@ -83,7 +83,7 @@ class MeshBuffer { this.indiceOffset += indiceCount; this.byteOffset = byteOffset; - } + }, _reallocBuffer () { let oldVData = this._vData; @@ -101,7 +101,7 @@ class MeshBuffer { this._vb._bytes = this._vData.byteLength; this._ib._bytes = this._iData.byteLength; - } + }, reset () { this.byteStart = 0; @@ -110,6 +110,6 @@ class MeshBuffer { this.indiceOffset = 0; this.vertexOffset = 0; } -} +}); module.exports = MeshBuffer; diff --git a/cocos2d/core/renderer/webgl/quad-buffer.js b/cocos2d/core/renderer/webgl/quad-buffer.js index a33521f3f1..807324a42c 100644 --- a/cocos2d/core/renderer/webgl/quad-buffer.js +++ b/cocos2d/core/renderer/webgl/quad-buffer.js @@ -1,6 +1,8 @@ const MeshBuffer = require('./mesh-buffer'); -class QuadBuffer extends MeshBuffer { +let QuadBuffer = cc.Class({ + extends: MeshBuffer, + _fillQuadBuffer () { let count = this._initIDataCount / 6; let buffer = this._iData; @@ -16,18 +18,18 @@ class QuadBuffer extends MeshBuffer { let indicesData = new Uint16Array(this._iData.buffer, 0, count * 6 ); this._ib.update(0, indicesData); - } + }, uploadData () { // update vertext data let vertexsData = new Float32Array(this._vData.buffer, 0, this.byteOffset >> 2); this._vb.update(0, vertexsData); - } + }, _reallocBuffer () { MeshBuffer.prototype._reallocBuffer.call(this); this._fillQuadBuffer(); } -} +}); module.exports = QuadBuffer; diff --git a/cocos2d/core/utils/profiler/counter.js b/cocos2d/core/utils/profiler/counter.js index 551619947f..47739a133a 100644 --- a/cocos2d/core/utils/profiler/counter.js +++ b/cocos2d/core/utils/profiler/counter.js @@ -1,51 +1,57 @@ -class Counter { - constructor(id, opts, now) { - this._id = id; - this._opts = opts || {}; - - this._value = 0; - this._total = 0; - this._averageValue = 0; - this._accumValue = 0; - this._accumSamples = 0; - this._accumStart = now; - } - - _average(v, now) { - if (this._opts.average) { - this._accumValue += v; - ++this._accumSamples; - - let t = now; - if (t - this._accumStart >= this._opts.average) { - this._averageValue = this._accumValue / this._accumSamples; - this._accumValue = 0; - this._accumStart = t; - this._accumSamples = 0; - } - } - } - - get value() { return this._value; } - set value(v) { - this._value = v; - } - - sample(now) { - this._average(this._value, now); - } - - human() { - let v = this._opts.average ? this._averageValue : this._value; - return Math.round(v * 100) / 100; - } - - alarm() { - return ( - (this._opts.below && this._value < this._opts.below) || - (this._opts.over && this._value > this._opts.over) - ); - } - } +let Counter = cc.Class({ + ctor (id, opts, now) { + this._id = id; + this._opts = opts || {}; - module.exports = Counter; + this._value = 0; + this._total = 0; + this._averageValue = 0; + this._accumValue = 0; + this._accumSamples = 0; + this._accumStart = now; + }, + + properties: { + value: { + get () { + return this._value; + }, + set (v) { + this._value = v; + } + } + }, + + _average (v, now) { + if (this._opts.average) { + this._accumValue += v; + ++this._accumSamples; + + let t = now; + if (t - this._accumStart >= this._opts.average) { + this._averageValue = this._accumValue / this._accumSamples; + this._accumValue = 0; + this._accumStart = t; + this._accumSamples = 0; + } + } + }, + + sample (now) { + this._average(this._value, now); + }, + + human () { + let v = this._opts.average ? this._averageValue : this._value; + return Math.round(v * 100) / 100; + }, + + alarm () { + return ( + (this._opts.below && this._value < this._opts.below) || + (this._opts.over && this._value > this._opts.over) + ); + } +}) + +module.exports = Counter; diff --git a/cocos2d/core/utils/profiler/perf-counter.js b/cocos2d/core/utils/profiler/perf-counter.js index 778810ec0a..effea7f1b6 100644 --- a/cocos2d/core/utils/profiler/perf-counter.js +++ b/cocos2d/core/utils/profiler/perf-counter.js @@ -1,22 +1,22 @@ const Counter = require('./counter'); -class PerfCounter extends Counter { - constructor(id, opts, now) { - super(id, opts, now); - +let PerfCounter = cc.Class({ + extends: Counter, + + ctor (id, opts, now) { // DISABLE // this._idstart = `${id}_start`; // this._idend = `${id}_end`; this._time = now; - } + }, start(now) { this._time = now; // DISABLE: long time running will cause performance drop down // window.performance.mark(this._idstart); - } + }, end(now) { this._value = now - this._time; @@ -26,12 +26,12 @@ class PerfCounter extends Counter { // window.performance.measure(this._id, this._idstart, this._idend); this._average(this._value); - } + }, tick() { this.end(); this.start(); - } + }, frame(now) { let t = now; @@ -46,6 +46,6 @@ class PerfCounter extends Counter { this._average(this._value); } } -} +}); module.exports = PerfCounter;