From 8de368430601c257c3e4b310b3621f3f033d6c7a Mon Sep 17 00:00:00 2001 From: altovista Date: Wed, 2 Feb 2011 00:45:30 +0100 Subject: [PATCH] removed all seperate files and combined into one big file (yes the make script it ugly) --- demos/demo.html | 14 +- lib/ansii_palette.js | 1 - lib/bitmap.js | 26 - lib/{canvas_lib.js => canvas_library.js} | 1015 +++++++++++----------- lib/display_container.js | 43 - lib/display_object.js | 99 --- lib/ease_default.js | 4 - lib/pixel_sprite.js | 75 -- lib/renderer.js | 48 - lib/shape.js | 154 ---- lib/stacked_loader.js | 96 -- lib/stage.js | 211 ----- lib/text_field.js | 36 - lib/tween.js | 66 -- lib/tween_command.js | 38 - lib/utils.js | 27 - make | 14 +- 17 files changed, 521 insertions(+), 1446 deletions(-) delete mode 100644 lib/ansii_palette.js delete mode 100644 lib/bitmap.js rename lib/{canvas_lib.js => canvas_library.js} (99%) delete mode 100644 lib/display_container.js delete mode 100644 lib/display_object.js delete mode 100644 lib/ease_default.js delete mode 100644 lib/pixel_sprite.js delete mode 100644 lib/renderer.js delete mode 100644 lib/shape.js delete mode 100644 lib/stacked_loader.js delete mode 100644 lib/stage.js delete mode 100644 lib/text_field.js delete mode 100644 lib/tween.js delete mode 100644 lib/tween_command.js delete mode 100644 lib/utils.js diff --git a/demos/demo.html b/demos/demo.html index 0bb0784..ecee0f3 100644 --- a/demos/demo.html +++ b/demos/demo.html @@ -4,19 +4,7 @@ Canvas Lib test - - - - - - - - - - - - - + diff --git a/lib/ansii_palette.js b/lib/ansii_palette.js deleted file mode 100644 index 0519ecb..0000000 --- a/lib/ansii_palette.js +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/lib/bitmap.js b/lib/bitmap.js deleted file mode 100644 index 21ae4ab..0000000 --- a/lib/bitmap.js +++ /dev/null @@ -1,26 +0,0 @@ -var Bitmap; -var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { - for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } - function ctor() { this.constructor = child; } - ctor.prototype = parent.prototype; - child.prototype = new ctor; - child.__super__ = parent.prototype; - return child; -}; -Bitmap = (function() { - __extends(Bitmap, DisplayObject); - function Bitmap(imageData) { - this.imageData = imageData; - Bitmap.__super__.constructor.call(this); - } - Bitmap.prototype.draw = function(context, drawHitarea) { - if (this.imageData) { - if (this.drawHitarea) { - return context.rect(0, 0, this.imageData.width, this.imageData.height); - } else { - return context.drawImage(this.imageData, 0, 0); - } - } - }; - return Bitmap; -})(); \ No newline at end of file diff --git a/lib/canvas_lib.js b/lib/canvas_library.js similarity index 99% rename from lib/canvas_lib.js rename to lib/canvas_library.js index 7c55c96..26577f9 100644 --- a/lib/canvas_lib.js +++ b/lib/canvas_library.js @@ -1,173 +1,266 @@ - -var Bitmap; -var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { - for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } - function ctor() { this.constructor = child; } - ctor.prototype = parent.prototype; - child.prototype = new ctor; - child.__super__ = parent.prototype; - return child; -}; -Bitmap = (function() { - __extends(Bitmap, DisplayObject); - function Bitmap(imageData) { - this.imageData = imageData; - Bitmap.__super__.constructor.call(this); - } - Bitmap.prototype.draw = function(context, drawHitarea) { - if (this.imageData) { - if (this.drawHitarea) { - return context.rect(0, 0, this.imageData.width, this.imageData.height); - } else { - return context.drawImage(this.imageData, 0, 0); - } +var Utils; +Utils = (function() { + function Utils() {} + Utils.hex2rgba = function(hex) { + var num; + num = parseInt(hex.slice(1), 16); + return [num >> 16 & 255, num >> 8 & 255, num & 255, num >> 24 & 255]; + }; + Utils.angleToRadians = function(angle) { + return angle * Math.PI / 180; + }; + Utils.firstUpcase = function(str) { + return str.substr(0, 1).toUpperCase() + str.substr(1); + }; + Utils.offsetPosition = function(obj) { + var left, top; + top = 0; + left = 0; + while (obj) { + left += obj.offsetLeft; + top += obj.offsetTop; + obj = obj.offsetParent; } + return [left, top]; }; - return Bitmap; + return Utils; })(); -var DisplayContainer; -var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { - for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } - function ctor() { this.constructor = child; } - ctor.prototype = parent.prototype; - child.prototype = new ctor; - child.__super__ = parent.prototype; - return child; -}; -DisplayContainer = (function() { - __extends(DisplayContainer, DisplayObject); - function DisplayContainer() { +var Stage; +var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; +Stage = (function() { + function Stage(canvasId) { + this.canvasId = canvasId; + this.scaleX = 1; + this.scaleY = 1; + this.rotation = 0; + this.visible = true; + this.x = 0; + this.y = 0; + this.alpha = 1; + this.mouseX = 0; + this.mouseY = 0; + this.parent = null; + this.isMouseSetup = false; + this.oldMouseX = 0; + this.oldMouseY = 0; this.children = []; - DisplayContainer.__super__.constructor.call(this); + this.canvas = document.getElementById(this.canvasId); + this.context = this.canvas.getContext('2d'); + this.hitBufferCanvas = this.cloneCanvas(); + this.hitBufferContext = this.hitBufferCanvas.getContext('2d'); + this.childrenChanged = false; + this.allChildren = []; + this.canvasOffsetPosition = Utils.offsetPosition(this.canvas); + window.onresize = __bind(function() { + return this.handleWindowResize(); + }, this); } - DisplayContainer.prototype.addChild = function(child) { + Stage.prototype.cloneCanvas = function() { + var canvas; + canvas = document.createElement('canvas'); + canvas.width = this.canvas.width; + canvas.height = this.canvas.height; + return canvas; + }; + Stage.prototype.addChild = function(child) { if (child.parent) { child.parent.removeChild(child); } - if (this.stage) { - this.stage.childrenChanged = true; - } + this.childrenChanged = true; child.parent = this; - child.stage = this.stage; + child.stage = this; this.children.push(child); return this; }; - DisplayContainer.prototype.removeChild = function(child) { + Stage.prototype.removeChild = function(child) { var i; - i = this.children.indexOf(child); - if (i === -1) { - throw 'Child object not found in DisplayList'; + if (i = this.children.indexOf(child) === -1) { + throw 'Child object not found on stage'; } else { - if (this.stage) { - this.stage.childrenChanged = true; - } + child.stage = null; + child.parent = null; + this.childrenChanged = true; this.children.splice(i, 1); - return this; } + return this; }; - DisplayContainer.prototype.draw = function(context, drawHitarea) {}; - return DisplayContainer; -})(); -var PixelSprite; -var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { - for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } - function ctor() { this.constructor = child; } - ctor.prototype = parent.prototype; - child.prototype = new ctor; - child.__super__ = parent.prototype; - return child; -}; -PixelSprite = (function() { - __extends(PixelSprite, Shape); - PixelSprite.defaultColorPalette = ['rgba(0, 0, 0, 1)', 'rgba(170, 0, 0, 1)', 'rgba(0, 170, 0, 1)', 'rgba(170, 85, 0, 1)', 'rgba(0, 0, 170, 1)', 'rgba(170, 0, 170, 1)', 'rgba(0, 170, 170, 1)', 'rgba(170, 170, 170, 1)', 'rgba(85, 85, 85, 1)', 'rgba(255, 85, 85, 1)', 'rgba(82, 255, 85, 1)', 'rgba(255, 255, 85, 1)', 'rgba(85, 85, 255, 1)', 'rgba(255, 85, 255, 1)', 'rgba(85, 255, 255, 1)', 'rgba(255, 255, 255, 1)']; - function PixelSprite(map) { - PixelSprite.__super__.constructor.call(this); - this.pixels = []; - this.colorPalette = PixelSprite.defaultColorPalette; - this.pixelSize = 4; - if (map) { - this.drawPixelMap(map); - } - } - PixelSprite.prototype.drawPixelMap = function(map) { - var color, column, columns, row, rows, x, y, _i, _j, _len, _len2; - x = 0; - y = 0; - rows = map.split("\n"); - for (_i = 0, _len = rows.length; _i < _len; _i++) { - row = rows[_i]; - columns = row.split(''); - x = 0; - for (_j = 0, _len2 = columns.length; _j < _len2; _j++) { - column = columns[_j]; - color = column.charCodeAt(0); - if (this.colorPalette[color]) { - this.drawPixel(x, y, this.colorPalette[color]); - } - x++; - } - y++; + Stage.prototype.render = function(clear) { + this.setupMouse(); + this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); + if (this.childrenChanged) { + this.findAllChildren(); + this.childrenChanged = false; } - return this.submitPixels(); + this.drawAllChildren(); + return this.handleMouseEventsOfAllChildren(); }; - PixelSprite.prototype.drawPixel = function(x, y, color) { - if (!this.pixels[y]) { - this.pixels[y] = []; + Stage.prototype.setupContext = function(context, child) { + context.globalAlpha = child.calculatedAlpha; + context.translate(parseInt(child.calculatedX), parseInt(child.calculatedY)); + context.rotate(Utils.angleToRadians(child.calculatedRotation)); + context.scale(child.calculatedScaleX, child.calculatedScaleY); + if (child.shadow) { + context.shadowBlur = child.shadowBlur; + context.shadowColor = child.shadowColor; + context.shadowOffsetX = child.shadowOffsetX; + return context.shadowOffsetY = child.shadowOffsetY; } - return this.pixels[y][x] = color; - }; - PixelSprite.prototype.removePixel = function(x, y) { - return this.pixels[y][x] = null; }; - PixelSprite.prototype.submitPixels = function() { - var column, row, x, y, _i, _j, _len, _len2, _ref, _results; - x = 0; - y = 0; - _ref = this.pixels; + Stage.prototype.drawAllChildren = function() { + var child, _i, _len, _ref, _results; + _ref = this.allChildren; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { - row = _ref[_i]; - x = 0; - if (row) { - for (_j = 0, _len2 = row.length; _j < _len2; _j++) { - column = row[_j]; - if (column) { - this.fillRect(x * this.pixelSize, y * this.pixelSize, this.pixelSize, this.pixelSize, column); - } - x++; + child = _ref[_i]; + child.calculateCanvasPositions(); + _results.push(child.calculatedVisibility ? (this.context.save(), this.setupContext(this.context, child), child.draw(this.context), this.context.restore()) : void 0); + } + return _results; + }; + Stage.prototype.getObjectUnderCursor = function() { + var child, i, _results; + i = this.allChildren.length - 1; + _results = []; + while (i >= 0) { + child = this.allChildren[i]; + if (child.calculatedVisibility && child.mouseEnabled) { + this.hitBufferContext.clearRect(0, 0, this.canvas.width, this.canvas.height); + this.hitBufferContext.save(); + this.setupContext(this.hitBufferContext, child); + this.hitBufferContext.beginPath(); + child.draw(this.hitBufferContext, true); + this.hitBufferContext.closePath(); + child.localX = this.mouseX - child.calculatedX; + child.localY = this.mouseY - child.calculatedY; + this.hitBufferContext.restore(); + if (this.hitBufferContext.isPointInPath(this.mouseX, this.mouseY)) { + return child; } } - _results.push(y++); + _results.push(i--); } return _results; }; - return PixelSprite; -})(); -var DisplayObject; -DisplayObject = (function() { - function DisplayObject() { - this.id = ''; - this.x = 0; - this.y = 0; - this.oldX = 0; - this.oldY = 0; - this.calculatedX = 0; - this.calculatedY = 0; - this.width = 0; - this.height = 0; - this.alpha = 1; - this.oldAlpha = 1; - this.calculatedAlpha = 1; - this.scaleX = 1; - this.scaleY = 1; - this.oldScaleX = 1; - this.oldScaleY = 1; - this.calculatedScaleX = 1; - this.calculatedScaleY = 1; - this.rotation = 0; - this.oldRotation = 0; - this.calculatedRotation = 0; - this.enabled = true; + Stage.prototype.findAllChildren = function() { + this.allChildren = []; + return this.getChildren(this, this.allChildren); + }; + Stage.prototype.getChildren = function(fromParent, collectedChildren) { + var child, _i, _len, _ref; + _ref = fromParent.children; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + child = _ref[_i]; + collectedChildren.push(child); + if ((child.children != null) && child.children.length > 0) { + this.getChildren(child, collectedChildren); + } + } + return collectedChildren; + }; + Stage.prototype.setupMouse = function() { + if (!this.isMouseSetup) { + this.isMouseSetup = true; + this.canvas.addEventListener('mousemove', __bind(function(event) { + return this.handleCanvasMouseMove(event); + }, this)); + this.canvas.addEventListener('mousedown', __bind(function(event) { + return this.handleCanvasMouseDown(event); + }, this)); + return this.canvas.addEventListener('mouseup', __bind(function(event) { + return this.handleCanvasMouseUp(event); + }, this)); + } + }; + Stage.prototype.handleCanvasMouseMove = function(event) { + this.oldMouseX = this.mouseX; + this.oldMouseY = this.mouseY; + this.mouseX = event.clientX - this.canvasOffsetPosition[0]; + this.mouseY = event.clientY - this.canvasOffsetPosition[1]; + if (this.onMouseMove) { + return this.onMouseMove(); + } + }; + Stage.prototype.handleCanvasMouseDown = function(event) { + if (this.onMouseDown && !this.mouseDown) { + this.onMouseDown(); + } + return this.mouseDown = true; + }; + Stage.prototype.handleCanvasMouseUp = function(event) { + if (this.onMouseUp && this.mouseDown) { + this.onMouseUp(); + } + return this.mouseDown = false; + }; + Stage.prototype.mouseHasMoved = function() { + return this.oldMouseX !== this.mouseX || this.oldMouseY !== this.mouseY; + }; + Stage.prototype.setHandCursor = function(showHand) { + return this.canvas.style.cursor = showHand != null ? showHand : { + 'pointer': '' + }; + }; + Stage.prototype.handleMouseEventsOfAllChildren = function() { + var objectUnderCursor; + objectUnderCursor = this.getObjectUnderCursor(); + if (this.lastObjectUnderCursor !== objectUnderCursor) { + if (this.lastObjectUnderCursor) { + if (this.lastObjectUnderCursor.onMouseOut) { + this.lastObjectUnderCursor.onMouseOut(); + } + if (this.lastObjectUnderCursor.useHandCursor) { + this.setHandCursor(false); + } + } + if (objectUnderCursor) { + if (objectUnderCursor.onMouseOver) { + objectUnderCursor.onMouseOver(); + } + if (objectUnderCursor.onMouseDown && !objectUnderCursor.mouseDown) { + objectUnderCursor.mouseDown = true; + objectUnderCursor.onMouseDown(); + } + if (objectUnderCursor.onMouseUp && objectUnderCursor.mouseDown) { + objectUnderCursor.mouseDown = false; + objectUnderCursor.onMouseUp(); + } + this.setHandCursor(objectUnderCursor.useHandCursor); + } + } else if (objectUnderCursor && this.mouseHasMoved() && objectUnderCursor.onMouseMove) { + objectUnderCursor.onMouseMove(); + } + return this.lastObjectUnderCursor = objectUnderCursor; + }; + Stage.prototype.handleWindowResize = function() { + return this.canvasOffsetPosition = Utils.offsetPosition(this.canvas); + }; + return Stage; +})(); +var DisplayObject; +DisplayObject = (function() { + function DisplayObject() { + this.id = ''; + this.x = 0; + this.y = 0; + this.oldX = 0; + this.oldY = 0; + this.calculatedX = 0; + this.calculatedY = 0; + this.width = 0; + this.height = 0; + this.alpha = 1; + this.oldAlpha = 1; + this.calculatedAlpha = 1; + this.scaleX = 1; + this.scaleY = 1; + this.oldScaleX = 1; + this.oldScaleY = 1; + this.calculatedScaleX = 1; + this.calculatedScaleY = 1; + this.rotation = 0; + this.oldRotation = 0; + this.calculatedRotation = 0; + this.enabled = true; this.visible = true; this.oldVisible = true; this.calculatedVisibility = true; @@ -242,57 +335,48 @@ DisplayObject = (function() { }; return DisplayObject; })(); -var EaseDefault; -EaseDefault = function(t, b, c, d) { - return -c * (t /= d) * (t - 2) + b; +var DisplayContainer; +var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { + for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } + function ctor() { this.constructor = child; } + ctor.prototype = parent.prototype; + child.prototype = new ctor; + child.__super__ = parent.prototype; + return child; }; -var Renderer; -Renderer = (function() { - function Renderer(stage, fps) { - this.timer = null; - this.running = false; - this.frameHandlers = null; - this.stage = stage; - this.setFps(fps); +DisplayContainer = (function() { + __extends(DisplayContainer, DisplayObject); + function DisplayContainer() { + this.children = []; + DisplayContainer.__super__.constructor.call(this); } - Renderer.prototype.setFps = function(fps) { - return this.interval = 1000 / fps; - }; - Renderer.prototype.run = function(fps) { - if (fps) { - this.setFps(fps); - } - if (this.running) { - this.stop(); + DisplayContainer.prototype.addChild = function(child) { + if (child.parent) { + child.parent.removeChild(child); } - this.running = true; - return this.timer = setInterval(this.handleInterval, this.interval, this); - }; - Renderer.prototype.addFrameHandler = function(handler) { - this.frameHandlers || (this.frameHandlers = []); - if (this.frameHandlers.indexOf(handler) === -1) { - this.frameHandlers.push(handler); + if (this.stage) { + this.stage.childrenChanged = true; } + child.parent = this; + child.stage = this.stage; + this.children.push(child); return this; }; - Renderer.prototype.handleInterval = function(self) { - var frameHandler, _i, _len, _ref; - if (self.frameHandlers) { - _ref = self.frameHandlers; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - frameHandler = _ref[_i]; - frameHandler(); + DisplayContainer.prototype.removeChild = function(child) { + var i; + i = this.children.indexOf(child); + if (i === -1) { + throw 'Child object not found in DisplayList'; + } else { + if (this.stage) { + this.stage.childrenChanged = true; } + this.children.splice(i, 1); + return this; } - Tween.update(); - return self.stage.render(true); }; - Renderer.prototype.stop = function() { - clearInterval(this.timer); - this.running = false; - return this.timer = null; - }; - return Renderer; + DisplayContainer.prototype.draw = function(context, drawHitarea) {}; + return DisplayContainer; })(); var Shape; var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { @@ -448,312 +532,31 @@ Shape = (function() { }; return Shape; })(); -var StackedLoader; -var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; -StackedLoader = (function() { - function StackedLoader() {} - StackedLoader.stack = {}; - StackedLoader.loadStack = []; - StackedLoader.toLoad = null; - StackedLoader.onCompleteHandlers = []; - StackedLoader.loading = false; - StackedLoader.add = function(id, type, url) { - if (this.stack[id]) { - this.remove(id); - } - return this.loadStack.push({ - type: type, - url: url, - id: id - }); - }; - StackedLoader.load = function(onCompleteHandler) { - if (onCompleteHandler != null) { - this.onCompleteHandlers.push(onCompleteHandler); - } - if (!this.loading) { - this.loading = true; - return this.loadNext(); - } - }; - StackedLoader.get = function(id) { - return this.stack[id]; - }; - StackedLoader.getClonedBitmap = function(id) { - return new Bitmap(this.stack[this.toLoad.id]); - }; - StackedLoader.loadNext = function() { - var onCompleteHandler, _i, _len, _ref; - if (this.loadStack.length === 0) { - _ref = this.onCompleteHandlers; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - onCompleteHandler = _ref[_i]; - onCompleteHandler(); - } - this.onCompleteHandlers = []; - return this.loading = false; - } else { - this.toLoad = this.loadStack[0]; - return this['load' + Utils.firstUpcase(this.toLoad.type)](); - } - }; - StackedLoader.loadString = function() { - this.stack[this.toLoad.id] = new XMLHttpRequest(); - this.stack[this.toLoad.id].onreadystatechange = __bind(function() { - if (this.stack[this.toLoad.id].readyState === 4 && this.stack[this.toLoad.id].status === 200) { - return this['handle' + Utils.firstUpcase(this.toLoad.type) + 'LoadComplete'](); - } - }, this); - this.stack[this.toLoad.id].open('GET', this.toLoad.url, true); - return this.stack[this.toLoad.id].send(null); - }; - StackedLoader.loadBitmap = function() { - this.stack[this.toLoad.id] = new Image(); - this.stack[this.toLoad.id].onload = __bind(function() { - return this.handleBitmapLoadComplete(); - }, this); - return this.stack[this.toLoad.id].src = this.toLoad.url; - }; - StackedLoader.loadSprite = function() { - return this.loadString(); - }; - StackedLoader.loadAudio = function() { - this.stack[this.toLoad.id] = new Audio(); - this.stack[this.toLoad.id].src = this.toLoad.url; - return this.handleAssetLoadComplete(); - }; - StackedLoader.handleBitmapLoadComplete = function() { - this.stack[this.toLoad.id].onload = null; - this.stack[this.toLoad.id] = new Bitmap(this.stack[this.toLoad.id]); - this.stack[this.toLoad.id].id = this.toLoad.id; - return this.handleAssetLoadComplete(); - }; - StackedLoader.handleStringLoadComplete = function() { - this.stack[this.toLoad.id].onreadystatechange = null; - this.stack[this.toLoad.id] = this.stack[this.toLoad.id].responseText; - return this.handleAssetLoadComplete; - }; - StackedLoader.handleSpriteLoadComplete = function() { - this.stack[this.toLoad.id].onreadystatechange = null; - this.stack[this.toLoad.id] = new PixelSprite(this.stack[this.toLoad.id].responseText); - return this.handleAssetLoadComplete; - }; - StackedLoader.handleAssetLoadComplete = function() { - this.loadStack.splice(0, 1); - return this.loadNext(); - }; - return StackedLoader; -})(); -var Stage; -var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; -Stage = (function() { - function Stage(canvasId) { - this.canvasId = canvasId; - this.scaleX = 1; - this.scaleY = 1; - this.rotation = 0; - this.visible = true; - this.x = 0; - this.y = 0; - this.alpha = 1; - this.mouseX = 0; - this.mouseY = 0; - this.parent = null; - this.isMouseSetup = false; - this.oldMouseX = 0; - this.oldMouseY = 0; - this.children = []; - this.canvas = document.getElementById(this.canvasId); - this.context = this.canvas.getContext('2d'); - this.hitBufferCanvas = this.cloneCanvas(); - this.hitBufferContext = this.hitBufferCanvas.getContext('2d'); - this.childrenChanged = false; - this.allChildren = []; - this.canvasOffsetPosition = Utils.offsetPosition(this.canvas); - window.onresize = __bind(function() { - return this.handleWindowResize(); - }, this); - } - Stage.prototype.cloneCanvas = function() { - var canvas; - canvas = document.createElement('canvas'); - canvas.width = this.canvas.width; - canvas.height = this.canvas.height; - return canvas; - }; - Stage.prototype.addChild = function(child) { - if (child.parent) { - child.parent.removeChild(child); - } - this.childrenChanged = true; - child.parent = this; - child.stage = this; - this.children.push(child); - return this; - }; - Stage.prototype.removeChild = function(child) { - var i; - if (i = this.children.indexOf(child) === -1) { - throw 'Child object not found on stage'; - } else { - child.stage = null; - child.parent = null; - this.childrenChanged = true; - this.children.splice(i, 1); - } - return this; - }; - Stage.prototype.render = function(clear) { - this.setupMouse(); - this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); - if (this.childrenChanged) { - this.findAllChildren(); - this.childrenChanged = false; - } - this.drawAllChildren(); - return this.handleMouseEventsOfAllChildren(); - }; - Stage.prototype.setupContext = function(context, child) { - context.globalAlpha = child.calculatedAlpha; - context.translate(parseInt(child.calculatedX), parseInt(child.calculatedY)); - context.rotate(Utils.angleToRadians(child.calculatedRotation)); - context.scale(child.calculatedScaleX, child.calculatedScaleY); - if (child.shadow) { - context.shadowBlur = child.shadowBlur; - context.shadowColor = child.shadowColor; - context.shadowOffsetX = child.shadowOffsetX; - return context.shadowOffsetY = child.shadowOffsetY; - } - }; - Stage.prototype.drawAllChildren = function() { - var child, _i, _len, _ref, _results; - _ref = this.allChildren; - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - child = _ref[_i]; - child.calculateCanvasPositions(); - _results.push(child.calculatedVisibility ? (this.context.save(), this.setupContext(this.context, child), child.draw(this.context), this.context.restore()) : void 0); - } - return _results; - }; - Stage.prototype.getObjectUnderCursor = function() { - var child, i, _results; - i = this.allChildren.length - 1; - _results = []; - while (i >= 0) { - child = this.allChildren[i]; - if (child.calculatedVisibility && child.mouseEnabled) { - this.hitBufferContext.clearRect(0, 0, this.canvas.width, this.canvas.height); - this.hitBufferContext.save(); - this.setupContext(this.hitBufferContext, child); - this.hitBufferContext.beginPath(); - child.draw(this.hitBufferContext, true); - this.hitBufferContext.closePath(); - child.localX = this.mouseX - child.calculatedX; - child.localY = this.mouseY - child.calculatedY; - this.hitBufferContext.restore(); - if (this.hitBufferContext.isPointInPath(this.mouseX, this.mouseY)) { - return child; - } - } - _results.push(i--); - } - return _results; - }; - Stage.prototype.findAllChildren = function() { - this.allChildren = []; - return this.getChildren(this, this.allChildren); - }; - Stage.prototype.getChildren = function(fromParent, collectedChildren) { - var child, _i, _len, _ref; - _ref = fromParent.children; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - child = _ref[_i]; - collectedChildren.push(child); - if ((child.children != null) && child.children.length > 0) { - this.getChildren(child, collectedChildren); - } - } - return collectedChildren; - }; - Stage.prototype.setupMouse = function() { - if (!this.isMouseSetup) { - this.isMouseSetup = true; - this.canvas.addEventListener('mousemove', __bind(function(event) { - return this.handleCanvasMouseMove(event); - }, this)); - this.canvas.addEventListener('mousedown', __bind(function(event) { - return this.handleCanvasMouseDown(event); - }, this)); - return this.canvas.addEventListener('mouseup', __bind(function(event) { - return this.handleCanvasMouseUp(event); - }, this)); - } - }; - Stage.prototype.handleCanvasMouseMove = function(event) { - this.oldMouseX = this.mouseX; - this.oldMouseY = this.mouseY; - this.mouseX = event.clientX - this.canvasOffsetPosition[0]; - this.mouseY = event.clientY - this.canvasOffsetPosition[1]; - if (this.onMouseMove) { - return this.onMouseMove(); - } - }; - Stage.prototype.handleCanvasMouseDown = function(event) { - if (this.onMouseDown && !this.mouseDown) { - this.onMouseDown(); - } - return this.mouseDown = true; - }; - Stage.prototype.handleCanvasMouseUp = function(event) { - if (this.onMouseUp && this.mouseDown) { - this.onMouseUp(); - } - return this.mouseDown = false; - }; - Stage.prototype.mouseHasMoved = function() { - return this.oldMouseX !== this.mouseX || this.oldMouseY !== this.mouseY; - }; - Stage.prototype.setHandCursor = function(showHand) { - return this.canvas.style.cursor = showHand != null ? showHand : { - 'pointer': '' - }; - }; - Stage.prototype.handleMouseEventsOfAllChildren = function() { - var objectUnderCursor; - objectUnderCursor = this.getObjectUnderCursor(); - if (this.lastObjectUnderCursor !== objectUnderCursor) { - if (this.lastObjectUnderCursor) { - if (this.lastObjectUnderCursor.onMouseOut) { - this.lastObjectUnderCursor.onMouseOut(); - } - if (this.lastObjectUnderCursor.useHandCursor) { - this.setHandCursor(false); - } - } - if (objectUnderCursor) { - if (objectUnderCursor.onMouseOver) { - objectUnderCursor.onMouseOver(); - } - if (objectUnderCursor.onMouseDown && !objectUnderCursor.mouseDown) { - objectUnderCursor.mouseDown = true; - objectUnderCursor.onMouseDown(); - } - if (objectUnderCursor.onMouseUp && objectUnderCursor.mouseDown) { - objectUnderCursor.mouseDown = false; - objectUnderCursor.onMouseUp(); - } - this.setHandCursor(objectUnderCursor.useHandCursor); +var Bitmap; +var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { + for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } + function ctor() { this.constructor = child; } + ctor.prototype = parent.prototype; + child.prototype = new ctor; + child.__super__ = parent.prototype; + return child; +}; +Bitmap = (function() { + __extends(Bitmap, DisplayObject); + function Bitmap(imageData) { + this.imageData = imageData; + Bitmap.__super__.constructor.call(this); + } + Bitmap.prototype.draw = function(context, drawHitarea) { + if (this.imageData) { + if (this.drawHitarea) { + return context.rect(0, 0, this.imageData.width, this.imageData.height); + } else { + return context.drawImage(this.imageData, 0, 0); } - } else if (objectUnderCursor && this.mouseHasMoved() && objectUnderCursor.onMouseMove) { - objectUnderCursor.onMouseMove(); } - return this.lastObjectUnderCursor = objectUnderCursor; - }; - Stage.prototype.handleWindowResize = function() { - return this.canvasOffsetPosition = Utils.offsetPosition(this.canvas); }; - return Stage; + return Bitmap; })(); var TextField; var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { @@ -791,6 +594,81 @@ TextField = (function() { }; return TextField; })(); +var PixelSprite; +var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { + for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } + function ctor() { this.constructor = child; } + ctor.prototype = parent.prototype; + child.prototype = new ctor; + child.__super__ = parent.prototype; + return child; +}; +PixelSprite = (function() { + __extends(PixelSprite, Shape); + PixelSprite.defaultColorPalette = ['rgba(0, 0, 0, 1)', 'rgba(170, 0, 0, 1)', 'rgba(0, 170, 0, 1)', 'rgba(170, 85, 0, 1)', 'rgba(0, 0, 170, 1)', 'rgba(170, 0, 170, 1)', 'rgba(0, 170, 170, 1)', 'rgba(170, 170, 170, 1)', 'rgba(85, 85, 85, 1)', 'rgba(255, 85, 85, 1)', 'rgba(82, 255, 85, 1)', 'rgba(255, 255, 85, 1)', 'rgba(85, 85, 255, 1)', 'rgba(255, 85, 255, 1)', 'rgba(85, 255, 255, 1)', 'rgba(255, 255, 255, 1)']; + function PixelSprite(map) { + PixelSprite.__super__.constructor.call(this); + this.pixels = []; + this.colorPalette = PixelSprite.defaultColorPalette; + this.pixelSize = 4; + if (map) { + this.drawPixelMap(map); + } + } + PixelSprite.prototype.drawPixelMap = function(map) { + var color, column, columns, row, rows, x, y, _i, _j, _len, _len2; + x = 0; + y = 0; + rows = map.split("\n"); + for (_i = 0, _len = rows.length; _i < _len; _i++) { + row = rows[_i]; + columns = row.split(''); + x = 0; + for (_j = 0, _len2 = columns.length; _j < _len2; _j++) { + column = columns[_j]; + color = column.charCodeAt(0); + if (this.colorPalette[color]) { + this.drawPixel(x, y, this.colorPalette[color]); + } + x++; + } + y++; + } + return this.submitPixels(); + }; + PixelSprite.prototype.drawPixel = function(x, y, color) { + if (!this.pixels[y]) { + this.pixels[y] = []; + } + return this.pixels[y][x] = color; + }; + PixelSprite.prototype.removePixel = function(x, y) { + return this.pixels[y][x] = null; + }; + PixelSprite.prototype.submitPixels = function() { + var column, row, x, y, _i, _j, _len, _len2, _ref, _results; + x = 0; + y = 0; + _ref = this.pixels; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + row = _ref[_i]; + x = 0; + if (row) { + for (_j = 0, _len2 = row.length; _j < _len2; _j++) { + column = row[_j]; + if (column) { + this.fillRect(x * this.pixelSize, y * this.pixelSize, this.pixelSize, this.pixelSize, column); + } + x++; + } + } + _results.push(y++); + } + return _results; + }; + return PixelSprite; +})(); var Tween; Tween = (function() { function Tween() {} @@ -895,30 +773,151 @@ TweenCommand = (function() { }; return TweenCommand; })(); -var Utils; -Utils = (function() { - function Utils() {} - Utils.hex2rgba = function(hex) { - var num; - num = parseInt(hex.slice(1), 16); - return [num >> 16 & 255, num >> 8 & 255, num & 255, num >> 24 & 255]; +var EaseDefault; +EaseDefault = function(t, b, c, d) { + return -c * (t /= d) * (t - 2) + b; +}; +var StackedLoader; +var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; +StackedLoader = (function() { + function StackedLoader() {} + StackedLoader.stack = {}; + StackedLoader.loadStack = []; + StackedLoader.toLoad = null; + StackedLoader.onCompleteHandlers = []; + StackedLoader.loading = false; + StackedLoader.add = function(id, type, url) { + if (this.stack[id]) { + this.remove(id); + } + return this.loadStack.push({ + type: type, + url: url, + id: id + }); }; - Utils.angleToRadians = function(angle) { - return angle * Math.PI / 180; + StackedLoader.load = function(onCompleteHandler) { + if (onCompleteHandler != null) { + this.onCompleteHandlers.push(onCompleteHandler); + } + if (!this.loading) { + this.loading = true; + return this.loadNext(); + } }; - Utils.firstUpcase = function(str) { - return str.substr(0, 1).toUpperCase() + str.substr(1); + StackedLoader.get = function(id) { + return this.stack[id]; }; - Utils.offsetPosition = function(obj) { - var left, top; - top = 0; - left = 0; - while (obj) { - left += obj.offsetLeft; - top += obj.offsetTop; - obj = obj.offsetParent; + StackedLoader.getClonedBitmap = function(id) { + return new Bitmap(this.stack[this.toLoad.id]); + }; + StackedLoader.loadNext = function() { + var onCompleteHandler, _i, _len, _ref; + if (this.loadStack.length === 0) { + _ref = this.onCompleteHandlers; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + onCompleteHandler = _ref[_i]; + onCompleteHandler(); + } + this.onCompleteHandlers = []; + return this.loading = false; + } else { + this.toLoad = this.loadStack[0]; + return this['load' + Utils.firstUpcase(this.toLoad.type)](); } - return [left, top]; }; - return Utils; + StackedLoader.loadString = function() { + this.stack[this.toLoad.id] = new XMLHttpRequest(); + this.stack[this.toLoad.id].onreadystatechange = __bind(function() { + if (this.stack[this.toLoad.id].readyState === 4 && this.stack[this.toLoad.id].status === 200) { + return this['handle' + Utils.firstUpcase(this.toLoad.type) + 'LoadComplete'](); + } + }, this); + this.stack[this.toLoad.id].open('GET', this.toLoad.url, true); + return this.stack[this.toLoad.id].send(null); + }; + StackedLoader.loadBitmap = function() { + this.stack[this.toLoad.id] = new Image(); + this.stack[this.toLoad.id].onload = __bind(function() { + return this.handleBitmapLoadComplete(); + }, this); + return this.stack[this.toLoad.id].src = this.toLoad.url; + }; + StackedLoader.loadSprite = function() { + return this.loadString(); + }; + StackedLoader.loadAudio = function() { + this.stack[this.toLoad.id] = new Audio(); + this.stack[this.toLoad.id].src = this.toLoad.url; + return this.handleAssetLoadComplete(); + }; + StackedLoader.handleBitmapLoadComplete = function() { + this.stack[this.toLoad.id].onload = null; + this.stack[this.toLoad.id] = new Bitmap(this.stack[this.toLoad.id]); + this.stack[this.toLoad.id].id = this.toLoad.id; + return this.handleAssetLoadComplete(); + }; + StackedLoader.handleStringLoadComplete = function() { + this.stack[this.toLoad.id].onreadystatechange = null; + this.stack[this.toLoad.id] = this.stack[this.toLoad.id].responseText; + return this.handleAssetLoadComplete; + }; + StackedLoader.handleSpriteLoadComplete = function() { + this.stack[this.toLoad.id].onreadystatechange = null; + this.stack[this.toLoad.id] = new PixelSprite(this.stack[this.toLoad.id].responseText); + return this.handleAssetLoadComplete; + }; + StackedLoader.handleAssetLoadComplete = function() { + this.loadStack.splice(0, 1); + return this.loadNext(); + }; + return StackedLoader; +})(); +var Renderer; +Renderer = (function() { + function Renderer(stage, fps) { + this.timer = null; + this.running = false; + this.frameHandlers = null; + this.stage = stage; + this.setFps(fps); + } + Renderer.prototype.setFps = function(fps) { + return this.interval = 1000 / fps; + }; + Renderer.prototype.run = function(fps) { + if (fps) { + this.setFps(fps); + } + if (this.running) { + this.stop(); + } + this.running = true; + return this.timer = setInterval(this.handleInterval, this.interval, this); + }; + Renderer.prototype.addFrameHandler = function(handler) { + this.frameHandlers || (this.frameHandlers = []); + if (this.frameHandlers.indexOf(handler) === -1) { + this.frameHandlers.push(handler); + } + return this; + }; + Renderer.prototype.handleInterval = function(self) { + var frameHandler, _i, _len, _ref; + if (self.frameHandlers) { + _ref = self.frameHandlers; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + frameHandler = _ref[_i]; + frameHandler(); + } + } + Tween.update(); + return self.stage.render(true); + }; + Renderer.prototype.stop = function() { + clearInterval(this.timer); + this.running = false; + return this.timer = null; + }; + return Renderer; })(); diff --git a/lib/display_container.js b/lib/display_container.js deleted file mode 100644 index 67de248..0000000 --- a/lib/display_container.js +++ /dev/null @@ -1,43 +0,0 @@ -var DisplayContainer; -var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { - for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } - function ctor() { this.constructor = child; } - ctor.prototype = parent.prototype; - child.prototype = new ctor; - child.__super__ = parent.prototype; - return child; -}; -DisplayContainer = (function() { - __extends(DisplayContainer, DisplayObject); - function DisplayContainer() { - this.children = []; - DisplayContainer.__super__.constructor.call(this); - } - DisplayContainer.prototype.addChild = function(child) { - if (child.parent) { - child.parent.removeChild(child); - } - if (this.stage) { - this.stage.childrenChanged = true; - } - child.parent = this; - child.stage = this.stage; - this.children.push(child); - return this; - }; - DisplayContainer.prototype.removeChild = function(child) { - var i; - i = this.children.indexOf(child); - if (i === -1) { - throw 'Child object not found in DisplayList'; - } else { - if (this.stage) { - this.stage.childrenChanged = true; - } - this.children.splice(i, 1); - return this; - } - }; - DisplayContainer.prototype.draw = function(context, drawHitarea) {}; - return DisplayContainer; -})(); \ No newline at end of file diff --git a/lib/display_object.js b/lib/display_object.js deleted file mode 100644 index 780961c..0000000 --- a/lib/display_object.js +++ /dev/null @@ -1,99 +0,0 @@ -var DisplayObject; -DisplayObject = (function() { - function DisplayObject() { - this.id = ''; - this.x = 0; - this.y = 0; - this.oldX = 0; - this.oldY = 0; - this.calculatedX = 0; - this.calculatedY = 0; - this.width = 0; - this.height = 0; - this.alpha = 1; - this.oldAlpha = 1; - this.calculatedAlpha = 1; - this.scaleX = 1; - this.scaleY = 1; - this.oldScaleX = 1; - this.oldScaleY = 1; - this.calculatedScaleX = 1; - this.calculatedScaleY = 1; - this.rotation = 0; - this.oldRotation = 0; - this.calculatedRotation = 0; - this.enabled = true; - this.visible = true; - this.oldVisible = true; - this.calculatedVisibility = true; - this.mouseEnabled = false; - this.mouseDown = false; - this.useHandCursor = false; - this.shadow = false; - this.shadowBlur = 0; - this.shadowColor = 0; - this.shadowOffsetX = 0; - this.shadowOffsetY = 0; - this.onMouseOver = null; - this.onMouseOut = null; - this.onMouseDown = null; - this.onMouseMove = null; - this.localX = 0; - this.localY = 0; - this.isMouseSetup = false; - this.mouseDown = false; - this.lastObjectUnderCursor = null; - this.stage = null; - this.parent = null; - this.childrenChanged = false; - } - DisplayObject.prototype.draw = function(context, drawHitarea) {}; - DisplayObject.prototype.calculateCanvasPositions = function() { - var newVars; - if (this.positionChanged()) { - this.oldX = this.x; - this.oldY = this.y; - this.oldRotation = this.rotation; - this.oldScaleX = this.scaleX; - this.oldScaleY = this.scaleY; - this.oldVisible = this.visible; - this.oldAlpha = this.alpha; - newVars = this.getInheritedTranslatedVars(); - this.calculatedX = newVars[0]; - this.calculatedY = newVars[1]; - this.calculatedRotation = newVars[2]; - this.calculatedScaleX = newVars[3]; - this.calculatedScaleY = newVars[4]; - this.calculatedVisibility = newVars[5]; - return this.calculatedAlpha = newVars[6]; - } - }; - DisplayObject.prototype.getInheritedTranslatedVars = function() { - var theParent, translatedAlpha, translatedRotation, translatedScaleX, translatedScaleY, translatedVisibility, translatedX, translatedY; - theParent = this; - translatedX = 0; - translatedY = 0; - translatedRotation = 0; - translatedScaleX = 1; - translatedScaleY = 1; - translatedVisibility = true; - translatedAlpha = 1; - while (!(theParent === null || theParent === this.stage)) { - translatedX += theParent.x; - translatedY += theParent.y; - translatedRotation += theParent.rotation; - translatedScaleX *= theParent.scaleX; - translatedScaleY *= theParent.scaleY; - if (!theParent.visible) { - translatedVisibility = false; - } - translatedAlpha *= theParent.alpha; - theParent = theParent.parent; - } - return [translatedX, translatedY, translatedRotation, translatedScaleX, translatedScaleY, translatedVisibility, translatedAlpha]; - }; - DisplayObject.prototype.positionChanged = function() { - return this.x !== this.oldX || this.y !== this.oldY || this.rotation !== this.oldRotation || this.scaleX !== this.oldScaleX || this.scaleY !== this.oldScaleY || this.visible !== this.oldVisible || this.alpha !== this.oldAlpha; - }; - return DisplayObject; -})(); \ No newline at end of file diff --git a/lib/ease_default.js b/lib/ease_default.js deleted file mode 100644 index 53c55fa..0000000 --- a/lib/ease_default.js +++ /dev/null @@ -1,4 +0,0 @@ -var EaseDefault; -EaseDefault = function(t, b, c, d) { - return -c * (t /= d) * (t - 2) + b; -}; \ No newline at end of file diff --git a/lib/pixel_sprite.js b/lib/pixel_sprite.js deleted file mode 100644 index eba3582..0000000 --- a/lib/pixel_sprite.js +++ /dev/null @@ -1,75 +0,0 @@ -var PixelSprite; -var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { - for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } - function ctor() { this.constructor = child; } - ctor.prototype = parent.prototype; - child.prototype = new ctor; - child.__super__ = parent.prototype; - return child; -}; -PixelSprite = (function() { - __extends(PixelSprite, Shape); - PixelSprite.defaultColorPalette = ['rgba(0, 0, 0, 1)', 'rgba(170, 0, 0, 1)', 'rgba(0, 170, 0, 1)', 'rgba(170, 85, 0, 1)', 'rgba(0, 0, 170, 1)', 'rgba(170, 0, 170, 1)', 'rgba(0, 170, 170, 1)', 'rgba(170, 170, 170, 1)', 'rgba(85, 85, 85, 1)', 'rgba(255, 85, 85, 1)', 'rgba(82, 255, 85, 1)', 'rgba(255, 255, 85, 1)', 'rgba(85, 85, 255, 1)', 'rgba(255, 85, 255, 1)', 'rgba(85, 255, 255, 1)', 'rgba(255, 255, 255, 1)']; - function PixelSprite(map) { - PixelSprite.__super__.constructor.call(this); - this.pixels = []; - this.colorPalette = PixelSprite.defaultColorPalette; - this.pixelSize = 4; - if (map) { - this.drawPixelMap(map); - } - } - PixelSprite.prototype.drawPixelMap = function(map) { - var color, column, columns, row, rows, x, y, _i, _j, _len, _len2; - x = 0; - y = 0; - rows = map.split("\n"); - for (_i = 0, _len = rows.length; _i < _len; _i++) { - row = rows[_i]; - columns = row.split(''); - x = 0; - for (_j = 0, _len2 = columns.length; _j < _len2; _j++) { - column = columns[_j]; - color = column.charCodeAt(0); - if (this.colorPalette[color]) { - this.drawPixel(x, y, this.colorPalette[color]); - } - x++; - } - y++; - } - return this.submitPixels(); - }; - PixelSprite.prototype.drawPixel = function(x, y, color) { - if (!this.pixels[y]) { - this.pixels[y] = []; - } - return this.pixels[y][x] = color; - }; - PixelSprite.prototype.removePixel = function(x, y) { - return this.pixels[y][x] = null; - }; - PixelSprite.prototype.submitPixels = function() { - var column, row, x, y, _i, _j, _len, _len2, _ref, _results; - x = 0; - y = 0; - _ref = this.pixels; - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - row = _ref[_i]; - x = 0; - if (row) { - for (_j = 0, _len2 = row.length; _j < _len2; _j++) { - column = row[_j]; - if (column) { - this.fillRect(x * this.pixelSize, y * this.pixelSize, this.pixelSize, this.pixelSize, column); - } - x++; - } - } - _results.push(y++); - } - return _results; - }; - return PixelSprite; -})(); \ No newline at end of file diff --git a/lib/renderer.js b/lib/renderer.js deleted file mode 100644 index 97832ce..0000000 --- a/lib/renderer.js +++ /dev/null @@ -1,48 +0,0 @@ -var Renderer; -Renderer = (function() { - function Renderer(stage, fps) { - this.timer = null; - this.running = false; - this.frameHandlers = null; - this.stage = stage; - this.setFps(fps); - } - Renderer.prototype.setFps = function(fps) { - return this.interval = 1000 / fps; - }; - Renderer.prototype.run = function(fps) { - if (fps) { - this.setFps(fps); - } - if (this.running) { - this.stop(); - } - this.running = true; - return this.timer = setInterval(this.handleInterval, this.interval, this); - }; - Renderer.prototype.addFrameHandler = function(handler) { - this.frameHandlers || (this.frameHandlers = []); - if (this.frameHandlers.indexOf(handler) === -1) { - this.frameHandlers.push(handler); - } - return this; - }; - Renderer.prototype.handleInterval = function(self) { - var frameHandler, _i, _len, _ref; - if (self.frameHandlers) { - _ref = self.frameHandlers; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - frameHandler = _ref[_i]; - frameHandler(); - } - } - Tween.update(); - return self.stage.render(true); - }; - Renderer.prototype.stop = function() { - clearInterval(this.timer); - this.running = false; - return this.timer = null; - }; - return Renderer; -})(); \ No newline at end of file diff --git a/lib/shape.js b/lib/shape.js deleted file mode 100644 index cd6c68d..0000000 --- a/lib/shape.js +++ /dev/null @@ -1,154 +0,0 @@ -var Shape; -var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { - for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } - function ctor() { this.constructor = child; } - ctor.prototype = parent.prototype; - child.prototype = new ctor; - child.__super__ = parent.prototype; - return child; -}; -Shape = (function() { - __extends(Shape, DisplayObject); - function Shape() { - Shape.__super__.constructor.call(this); - this.madeChanges = false; - this.drawingCommands = []; - } - Shape.prototype.clear = function() { - this.madeChanges = true; - this.drawingCommands = []; - return true; - }; - Shape.prototype.addColorStops = function(gradient, colorStops) { - var colorStop, _i, _len; - for (_i = 0, _len = colorStops.length; _i < _len; _i++) { - colorStop = colorStops[_i]; - gradient.addColorStop(colorStop[0], colorStop[1]); - } - return gradient; - }; - Shape.prototype.createRadialGradient = function(x1, y1, r1, x2, y2, r2, colorStops) { - var gradient; - gradient = this.stage.backBufferContext.createRadialGradient(x1, y1, r1, x2, y2, r2); - this.addColorStops(gradient, colorStops); - return gradient; - }; - Shape.prototype.createLinearGradient = function(x1, y1, x2, y2, colorStops) { - var gradient; - gradient = this.stage.backBufferContext.createLinearGradient(x1, y1, x2, y2); - this.addColorStops(gradient, colorStops); - return gradient; - }; - Shape.prototype.setRadialGradient = function(x1, y1, r1, x2, y2, r2, colorStops) { - this.fillStyle(this.createRadialGradient(x1, y1, r1, x2, y2, r2, colorStops)); - return this; - }; - Shape.prototype.setLinearGradient = function(x1, y1, x2, y2, colorStops) { - this.fillStyle(this.createLinearGradient(x1, y1, x2, y2, colorStops)); - return this; - }; - Shape.prototype.moveTo = function(x, y) { - this.drawingCommands.push([true, 'moveTo', x, y]); - return this; - }; - Shape.prototype.lineWidth = function(thickness) { - this.drawingCommands.push([true, 'lineWidth', thickness]); - return this; - }; - Shape.prototype.lineCap = function(cap) { - this.drawingCommands.push([true, 'lineCap', cap]); - return true; - }; - Shape.prototype.lineTo = function(x, y) { - this.madeChanges = true; - this.drawingCommands.push([true, 'lineTo', x, y]); - return this; - }; - Shape.prototype.bezierCurveTo = function(cp1x, cp1y, cp2x, cp2y, x, y) { - this.madeChanges = true; - this.drawingCommands.push([true, 'bezierCurveTo', cp1x, cp1y, cp2x, cp2y, x, y]); - return this; - }; - Shape.prototype.quadraticCurveTo = function(cpx, cpy, x, y) { - this.madeChanges = true; - this.drawingCommands.push([true, 'quadraticCurveTo', cpx, cpy, x, y]); - return this; - }; - Shape.prototype.miterLimit = function(ratio) { - this.drawingCommands.push([true, 'miterLimit', ratio]); - return this; - }; - Shape.prototype.beginPath = function() { - this.drawingCommands.push([false, 'beginPath']); - return this; - }; - Shape.prototype.closePath = function() { - this.drawingCommands.push([false, 'closePath']); - return this; - }; - Shape.prototype.fillRect = function(x, y, width, height, color) { - this.madeChanges = true; - if (color) { - this.fillStyle(color); - } - this.drawingCommands.push([true, 'rect', x, y, width, height]); - this.drawingCommands.push([false, 'fillRect', x, y, width, height]); - return this; - }; - Shape.prototype.circle = function(x, y, radius) { - this.arc(x, y, radius / 2, 0, Math.PI * 2, true); - return this; - }; - Shape.prototype.arc = function(x, y, sr, er, cw) { - this.madeChanges = true; - this.drawingCommands.push([true, 'arc', x, y, sr, er, cw]); - return this; - }; - Shape.prototype.strokeRect = function(x, y, width, height, color) { - this.madeChanges = true; - this.drawingCommands.push([true, 'rect', x, y, width, height]); - this.drawingCommands.push([false, 'strokeRect', x, y, width, height]); - return this; - }; - Shape.prototype.clearRect = function(x, y, width, height) { - this.madeChanges = true; - return this.drawingCommands.push([true, 'clearRect', x, y, width, height]); - }; - Shape.prototype.fillStyle = function(color) { - this.drawingCommands.push([false, 'fillStyle=', color]); - return this; - }; - Shape.prototype.strokeStyle = function(color) { - this.drawingCommands.push([false, 'strokeStyle=', color]); - return this; - }; - Shape.prototype.globalAlpha = function(alpha) { - this.madeChanges = true; - this.drawingCommands.push([false, 'globalAlpha=', alpha]); - return this; - }; - Shape.prototype.fill = function() { - this.madeChanges = true; - this.drawingCommands.push([false, 'fill']); - return this; - }; - Shape.prototype.draw = function(context, drawHitarea) { - var command, instruction, _i, _len, _ref; - _ref = this.drawingCommands; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - command = _ref[_i]; - instruction = command[1]; - if (!drawHitarea || (drawHitarea && command[0])) { - if (instruction.substr(-1, 1) === '=' && command.length === 3) { - context[instruction.substr(0, instruction.length - 1)] = command[2]; - } else if (command.length > 2) { - context[instruction].apply(context, command.slice(2)); - } else if (command.length === 2) { - context[instruction](); - } - } - } - return this.madeChanges = false; - }; - return Shape; -})(); \ No newline at end of file diff --git a/lib/stacked_loader.js b/lib/stacked_loader.js deleted file mode 100644 index e1e3bfc..0000000 --- a/lib/stacked_loader.js +++ /dev/null @@ -1,96 +0,0 @@ -var StackedLoader; -var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; -StackedLoader = (function() { - function StackedLoader() {} - StackedLoader.stack = {}; - StackedLoader.loadStack = []; - StackedLoader.toLoad = null; - StackedLoader.onCompleteHandlers = []; - StackedLoader.loading = false; - StackedLoader.add = function(id, type, url) { - if (this.stack[id]) { - this.remove(id); - } - return this.loadStack.push({ - type: type, - url: url, - id: id - }); - }; - StackedLoader.load = function(onCompleteHandler) { - if (onCompleteHandler != null) { - this.onCompleteHandlers.push(onCompleteHandler); - } - if (!this.loading) { - this.loading = true; - return this.loadNext(); - } - }; - StackedLoader.get = function(id) { - return this.stack[id]; - }; - StackedLoader.getClonedBitmap = function(id) { - return new Bitmap(this.stack[this.toLoad.id]); - }; - StackedLoader.loadNext = function() { - var onCompleteHandler, _i, _len, _ref; - if (this.loadStack.length === 0) { - _ref = this.onCompleteHandlers; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - onCompleteHandler = _ref[_i]; - onCompleteHandler(); - } - this.onCompleteHandlers = []; - return this.loading = false; - } else { - this.toLoad = this.loadStack[0]; - return this['load' + Utils.firstUpcase(this.toLoad.type)](); - } - }; - StackedLoader.loadString = function() { - this.stack[this.toLoad.id] = new XMLHttpRequest(); - this.stack[this.toLoad.id].onreadystatechange = __bind(function() { - if (this.stack[this.toLoad.id].readyState === 4 && this.stack[this.toLoad.id].status === 200) { - return this['handle' + Utils.firstUpcase(this.toLoad.type) + 'LoadComplete'](); - } - }, this); - this.stack[this.toLoad.id].open('GET', this.toLoad.url, true); - return this.stack[this.toLoad.id].send(null); - }; - StackedLoader.loadBitmap = function() { - this.stack[this.toLoad.id] = new Image(); - this.stack[this.toLoad.id].onload = __bind(function() { - return this.handleBitmapLoadComplete(); - }, this); - return this.stack[this.toLoad.id].src = this.toLoad.url; - }; - StackedLoader.loadSprite = function() { - return this.loadString(); - }; - StackedLoader.loadAudio = function() { - this.stack[this.toLoad.id] = new Audio(); - this.stack[this.toLoad.id].src = this.toLoad.url; - return this.handleAssetLoadComplete(); - }; - StackedLoader.handleBitmapLoadComplete = function() { - this.stack[this.toLoad.id].onload = null; - this.stack[this.toLoad.id] = new Bitmap(this.stack[this.toLoad.id]); - this.stack[this.toLoad.id].id = this.toLoad.id; - return this.handleAssetLoadComplete(); - }; - StackedLoader.handleStringLoadComplete = function() { - this.stack[this.toLoad.id].onreadystatechange = null; - this.stack[this.toLoad.id] = this.stack[this.toLoad.id].responseText; - return this.handleAssetLoadComplete; - }; - StackedLoader.handleSpriteLoadComplete = function() { - this.stack[this.toLoad.id].onreadystatechange = null; - this.stack[this.toLoad.id] = new PixelSprite(this.stack[this.toLoad.id].responseText); - return this.handleAssetLoadComplete; - }; - StackedLoader.handleAssetLoadComplete = function() { - this.loadStack.splice(0, 1); - return this.loadNext(); - }; - return StackedLoader; -})(); \ No newline at end of file diff --git a/lib/stage.js b/lib/stage.js deleted file mode 100644 index 085e3f9..0000000 --- a/lib/stage.js +++ /dev/null @@ -1,211 +0,0 @@ -var Stage; -var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; -Stage = (function() { - function Stage(canvasId) { - this.canvasId = canvasId; - this.scaleX = 1; - this.scaleY = 1; - this.rotation = 0; - this.visible = true; - this.x = 0; - this.y = 0; - this.alpha = 1; - this.mouseX = 0; - this.mouseY = 0; - this.parent = null; - this.isMouseSetup = false; - this.oldMouseX = 0; - this.oldMouseY = 0; - this.children = []; - this.canvas = document.getElementById(this.canvasId); - this.context = this.canvas.getContext('2d'); - this.hitBufferCanvas = this.cloneCanvas(); - this.hitBufferContext = this.hitBufferCanvas.getContext('2d'); - this.childrenChanged = false; - this.allChildren = []; - this.canvasOffsetPosition = Utils.offsetPosition(this.canvas); - window.onresize = __bind(function() { - return this.handleWindowResize(); - }, this); - } - Stage.prototype.cloneCanvas = function() { - var canvas; - canvas = document.createElement('canvas'); - canvas.width = this.canvas.width; - canvas.height = this.canvas.height; - return canvas; - }; - Stage.prototype.addChild = function(child) { - if (child.parent) { - child.parent.removeChild(child); - } - this.childrenChanged = true; - child.parent = this; - child.stage = this; - this.children.push(child); - return this; - }; - Stage.prototype.removeChild = function(child) { - var i; - if (i = this.children.indexOf(child) === -1) { - throw 'Child object not found on stage'; - } else { - child.stage = null; - child.parent = null; - this.childrenChanged = true; - this.children.splice(i, 1); - } - return this; - }; - Stage.prototype.render = function(clear) { - this.setupMouse(); - this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); - if (this.childrenChanged) { - this.findAllChildren(); - this.childrenChanged = false; - } - this.drawAllChildren(); - return this.handleMouseEventsOfAllChildren(); - }; - Stage.prototype.setupContext = function(context, child) { - context.globalAlpha = child.calculatedAlpha; - context.translate(parseInt(child.calculatedX), parseInt(child.calculatedY)); - context.rotate(Utils.angleToRadians(child.calculatedRotation)); - context.scale(child.calculatedScaleX, child.calculatedScaleY); - if (child.shadow) { - context.shadowBlur = child.shadowBlur; - context.shadowColor = child.shadowColor; - context.shadowOffsetX = child.shadowOffsetX; - return context.shadowOffsetY = child.shadowOffsetY; - } - }; - Stage.prototype.drawAllChildren = function() { - var child, _i, _len, _ref, _results; - _ref = this.allChildren; - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - child = _ref[_i]; - child.calculateCanvasPositions(); - _results.push(child.calculatedVisibility ? (this.context.save(), this.setupContext(this.context, child), child.draw(this.context), this.context.restore()) : void 0); - } - return _results; - }; - Stage.prototype.getObjectUnderCursor = function() { - var child, i, _results; - i = this.allChildren.length - 1; - _results = []; - while (i >= 0) { - child = this.allChildren[i]; - if (child.calculatedVisibility && child.mouseEnabled) { - this.hitBufferContext.clearRect(0, 0, this.canvas.width, this.canvas.height); - this.hitBufferContext.save(); - this.setupContext(this.hitBufferContext, child); - this.hitBufferContext.beginPath(); - child.draw(this.hitBufferContext, true); - this.hitBufferContext.closePath(); - child.localX = this.mouseX - child.calculatedX; - child.localY = this.mouseY - child.calculatedY; - this.hitBufferContext.restore(); - if (this.hitBufferContext.isPointInPath(this.mouseX, this.mouseY)) { - return child; - } - } - _results.push(i--); - } - return _results; - }; - Stage.prototype.findAllChildren = function() { - this.allChildren = []; - return this.getChildren(this, this.allChildren); - }; - Stage.prototype.getChildren = function(fromParent, collectedChildren) { - var child, _i, _len, _ref; - _ref = fromParent.children; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - child = _ref[_i]; - collectedChildren.push(child); - if ((child.children != null) && child.children.length > 0) { - this.getChildren(child, collectedChildren); - } - } - return collectedChildren; - }; - Stage.prototype.setupMouse = function() { - if (!this.isMouseSetup) { - this.isMouseSetup = true; - this.canvas.addEventListener('mousemove', __bind(function(event) { - return this.handleCanvasMouseMove(event); - }, this)); - this.canvas.addEventListener('mousedown', __bind(function(event) { - return this.handleCanvasMouseDown(event); - }, this)); - return this.canvas.addEventListener('mouseup', __bind(function(event) { - return this.handleCanvasMouseUp(event); - }, this)); - } - }; - Stage.prototype.handleCanvasMouseMove = function(event) { - this.oldMouseX = this.mouseX; - this.oldMouseY = this.mouseY; - this.mouseX = event.clientX - this.canvasOffsetPosition[0]; - this.mouseY = event.clientY - this.canvasOffsetPosition[1]; - if (this.onMouseMove) { - return this.onMouseMove(); - } - }; - Stage.prototype.handleCanvasMouseDown = function(event) { - if (this.onMouseDown && !this.mouseDown) { - this.onMouseDown(); - } - return this.mouseDown = true; - }; - Stage.prototype.handleCanvasMouseUp = function(event) { - if (this.onMouseUp && this.mouseDown) { - this.onMouseUp(); - } - return this.mouseDown = false; - }; - Stage.prototype.mouseHasMoved = function() { - return this.oldMouseX !== this.mouseX || this.oldMouseY !== this.mouseY; - }; - Stage.prototype.setHandCursor = function(showHand) { - return this.canvas.style.cursor = showHand != null ? showHand : { - 'pointer': '' - }; - }; - Stage.prototype.handleMouseEventsOfAllChildren = function() { - var objectUnderCursor; - objectUnderCursor = this.getObjectUnderCursor(); - if (this.lastObjectUnderCursor !== objectUnderCursor) { - if (this.lastObjectUnderCursor) { - if (this.lastObjectUnderCursor.onMouseOut) { - this.lastObjectUnderCursor.onMouseOut(); - } - if (this.lastObjectUnderCursor.useHandCursor) { - this.setHandCursor(false); - } - } - if (objectUnderCursor) { - if (objectUnderCursor.onMouseOver) { - objectUnderCursor.onMouseOver(); - } - if (objectUnderCursor.onMouseDown && !objectUnderCursor.mouseDown) { - objectUnderCursor.mouseDown = true; - objectUnderCursor.onMouseDown(); - } - if (objectUnderCursor.onMouseUp && objectUnderCursor.mouseDown) { - objectUnderCursor.mouseDown = false; - objectUnderCursor.onMouseUp(); - } - this.setHandCursor(objectUnderCursor.useHandCursor); - } - } else if (objectUnderCursor && this.mouseHasMoved() && objectUnderCursor.onMouseMove) { - objectUnderCursor.onMouseMove(); - } - return this.lastObjectUnderCursor = objectUnderCursor; - }; - Stage.prototype.handleWindowResize = function() { - return this.canvasOffsetPosition = Utils.offsetPosition(this.canvas); - }; - return Stage; -})(); \ No newline at end of file diff --git a/lib/text_field.js b/lib/text_field.js deleted file mode 100644 index 0d66709..0000000 --- a/lib/text_field.js +++ /dev/null @@ -1,36 +0,0 @@ -var TextField; -var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) { - for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } - function ctor() { this.constructor = child; } - ctor.prototype = parent.prototype; - child.prototype = new ctor; - child.__super__ = parent.prototype; - return child; -}; -TextField = (function() { - __extends(TextField, DisplayObject); - function TextField() { - this.text = ''; - this.textAlign = 'left'; - this.strokeStyle = null; - this.fillStyle = 'rgba(0, 0, 0, 1)'; - this.fontFace = 'Arial'; - this.fontSize = 20; - this.maxWidth = null; - TextField.__super__.constructor.call(this); - } - TextField.prototype.draw = function(context, drawHitarea) { - context.font = this.fontSize + 'px ' + this.fontFace; - context.textBaseline = 'top'; - if (drawHitarea) { - context.beginPath(); - context.rect(0, 0, context.measureText(this.text).width, this.fontSize); - return context.closePath(); - } else { - context.strokeStyle = this.strokeStyle; - context.fillStyle = this.fillStyle; - return context.fillText(this.text, 0, 0); - } - }; - return TextField; -})(); \ No newline at end of file diff --git a/lib/tween.js b/lib/tween.js deleted file mode 100644 index 6afc4b2..0000000 --- a/lib/tween.js +++ /dev/null @@ -1,66 +0,0 @@ -var Tween; -Tween = (function() { - function Tween() {} - Tween.tweens = []; - Tween.initTime = new Date().getTime(); - Tween.to = function(object, duration, toParams, options) { - var tween; - tween = new TweenCommand(object, toParams); - tween.duration = duration; - if (options) { - tween.onComplete = options.onComplete; - tween.delay = options.delay || 0; - tween.ease = options.ease || EaseDefault; - } - tween.finished = false; - return this.tweens.push(tween); - }; - Tween.kill = function(object) { - var i, _results; - i = 0; - _results = []; - while (i < this.tweens.length) { - if (this.tweens[i].object === object) { - this.tweens[i] = null; - this.tweens.splice(i, 1); - i = -1; - } - _results.push(i++); - } - return _results; - }; - Tween.update = function() { - var cleanup, i, time, tween, _i, _len, _ref; - if (this.tweens.length > 0) { - i = 0; - cleanup = false; - time = new Date().getTime(); - _ref = this.tweens; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - tween = _ref[_i]; - tween.update(time); - if (tween.finished) { - cleanup = true; - tween = null; - } - } - if (cleanup) { - return this.cleanup(); - } - } - }; - Tween.cleanup = function() { - var i, _results; - i = 0; - _results = []; - while (i < this.tweens.length) { - if (!this.tweens[i]) { - this.tweens.splice(i, 1); - i = -1; - } - _results.push(i++); - } - return _results; - }; - return Tween; -})(); \ No newline at end of file diff --git a/lib/tween_command.js b/lib/tween_command.js deleted file mode 100644 index 27a6485..0000000 --- a/lib/tween_command.js +++ /dev/null @@ -1,38 +0,0 @@ -var TweenCommand; -TweenCommand = (function() { - function TweenCommand(object, toParams) { - var property; - this.object = object; - this.toParams = toParams; - this.startTime = new Date().getTime(); - this.duration = 0; - this.delay = 0; - this.ease = EaseDefault; - this.finished = false; - this.onComplete = null; - this.startValues = {}; - for (property in this.toParams) { - if (this.object.hasOwnProperty(property)) { - this.startValues[property] = this.object[property]; - this.toParams[property] = this.toParams[property] - this.object[property]; - } - } - } - TweenCommand.prototype.update = function(updateTime) { - var factor, property, time, _results; - time = updateTime - this.startTime; - if (time >= this.duration) { - factor = 1; - this.finished = true; - } else { - this.finished = false; - factor = this.ease(time, 0, 1, this.duration); - } - _results = []; - for (property in this.toParams) { - _results.push(this.object[property] = this.startValues[property] + (factor * this.toParams[property])); - } - return _results; - }; - return TweenCommand; -})(); \ No newline at end of file diff --git a/lib/utils.js b/lib/utils.js deleted file mode 100644 index ee657c4..0000000 --- a/lib/utils.js +++ /dev/null @@ -1,27 +0,0 @@ -var Utils; -Utils = (function() { - function Utils() {} - Utils.hex2rgba = function(hex) { - var num; - num = parseInt(hex.slice(1), 16); - return [num >> 16 & 255, num >> 8 & 255, num & 255, num >> 24 & 255]; - }; - Utils.angleToRadians = function(angle) { - return angle * Math.PI / 180; - }; - Utils.firstUpcase = function(str) { - return str.substr(0, 1).toUpperCase() + str.substr(1); - }; - Utils.offsetPosition = function(obj) { - var left, top; - top = 0; - left = 0; - while (obj) { - left += obj.offsetLeft; - top += obj.offsetTop; - obj = obj.offsetParent; - } - return [left, top]; - }; - return Utils; -})(); \ No newline at end of file diff --git a/make b/make index b7745a8..2a6b8ef 100755 --- a/make +++ b/make @@ -1,2 +1,14 @@ #!/bin/sh -coffee -p -b --compile src/*.coffee > lib/canvas_lib.js \ No newline at end of file +coffee -b -p --compile src/utils.coffee > lib/canvas_library.js +coffee -b -p --compile src/stage.coffee >> lib/canvas_library.js +coffee -b -p --compile src/display_object.coffee >> lib/canvas_library.js +coffee -b -p --compile src/display_container.coffee >> lib/canvas_library.js +coffee -b -p --compile src/shape.coffee >> lib/canvas_library.js +coffee -b -p --compile src/bitmap.coffee >> lib/canvas_library.js +coffee -b -p --compile src/text_field.coffee >> lib/canvas_library.js +coffee -b -p --compile src/pixel_sprite.coffee >> lib/canvas_library.js +coffee -b -p --compile src/tween.coffee >> lib/canvas_library.js +coffee -b -p --compile src/tween_command.coffee >> lib/canvas_library.js +coffee -b -p --compile src/ease_default.coffee >> lib/canvas_library.js +coffee -b -p --compile src/stacked_loader.coffee >> lib/canvas_library.js +coffee -b -p --compile src/renderer.coffee >> lib/canvas_library.js \ No newline at end of file