1 goog.provide('lime.Renderer.CANVAS.SPRITE'); 2 goog.provide('lime.Renderer.DOM.SPRITE'); 3 goog.provide('lime.Sprite'); 4 5 goog.require('goog.events'); 6 goog.require('goog.events.EventTarget'); 7 goog.require('goog.math.Size'); 8 goog.require('lime'); 9 goog.require('lime.Node'); 10 goog.require('lime.fill.Color'); 11 goog.require('lime.fill.Stroke'); 12 goog.require('lime.fill.Fill'); 13 goog.require('lime.fill.Image'); 14 15 /** 16 * Rectangural textured object 17 * @constructor 18 * @extends lime.Node 19 */ 20 lime.Sprite = function() { 21 22 lime.Node.call(this); 23 24 /** 25 * Fill object used while drawing 26 * @type {lime.fill.Fill} 27 * @private 28 */ 29 this.fill_ = null; 30 31 32 this.stroke_ = null; 33 34 35 }; 36 goog.inherits(lime.Sprite, lime.Node); 37 38 /** 39 * Common name for sprite objects 40 * @type {string} 41 */ 42 lime.Sprite.prototype.id = 'sprite'; 43 44 /** @inheritDoc */ 45 lime.Sprite.prototype.supportedRenderers = [ 46 lime.Renderer.DOM.makeSubRenderer(lime.Renderer.DOM.SPRITE), 47 lime.Renderer.CANVAS.makeSubRenderer(lime.Renderer.CANVAS.SPRITE) 48 ]; 49 50 /** 51 * Gets fill parameters 52 * @return {lime.fill.Fill} Fill object. 53 */ 54 lime.Sprite.prototype.getFill = function() { 55 return this.fill_; 56 }; 57 58 /** 59 * Sets fill parameters 60 * @param {*} fill Fill. 61 * @param {number=} opt_g Optionaly use r,g,b,a as parameter. 62 * @param {number=} opt_b Optionaly use r,g,b,a as parameter. 63 * @param {number=} opt_a Optionaly use r,g,b,a as parameter. 64 * @return {lime.Sprite} object itself. 65 */ 66 lime.Sprite.prototype.setFill = function(fill, opt_g, opt_b, opt_a) { 67 this.fill_ = lime.fill.parse(goog.array.toArray(arguments)); 68 this.fill_.initForSprite(this); 69 this.setDirty(lime.Dirty.CONTENT); 70 return this; 71 }; 72 73 /** 74 * Return Stroke object if one is set 75 * @return {lime.fill.Stroke} Stroke object. 76 */ 77 lime.Sprite.prototype.getStroke = function(){ 78 return this.stroke_; 79 }; 80 81 /** 82 * Sets stroke parameters. 83 * @param {*} stroke Stroke object or width and (mixed type) Color. 84 * @return {lime.Sprite} object itself. 85 */ 86 lime.Sprite.prototype.setStroke = function(stroke){ 87 if(stroke && !(stroke instanceof lime.fill.Stroke)){ 88 stroke = new lime.fill.Stroke(goog.array.toArray(arguments)); 89 } 90 this.stroke_ = stroke; 91 this.setDirty(lime.Dirty.CONTENT); 92 return this; 93 }; 94 95 /** 96 * @private 97 */ 98 // todo: move this function to canvas background rendermode 99 lime.Sprite.prototype.getCanvasContextName_ = (function() { 100 var contextID_ = 0; 101 return function() { 102 103 if (!goog.isDef(this.canvasContextName_)) { 104 this.canvasContextName_ = 'limedc' + (contextID_++); 105 } 106 return this.canvasContextName_; 107 }; 108 })(); 109 110 111 /** 112 * @inheritDoc 113 * @this {lime.Sprite} 114 */ 115 lime.Renderer.DOM.SPRITE.draw = function(el) { 116 if (!goog.isNull(this.fill_)) { 117 this.fill_.setDOMStyle(el, this); 118 } 119 if (!goog.isNull(this.stroke_)) { 120 this.stroke_.setDOMStyle(el, this); 121 } else { 122 el.style.border='none'; 123 } 124 }; 125 126 /** 127 * @inheritDoc 128 * @this {lime.Sprite} 129 */ 130 lime.Renderer.CANVAS.SPRITE.draw = function(context) { 131 var size = this.getSize(), fill = this.fill_, stroke = this.stroke_; 132 133 if (!fill && !stroke) return; 134 135 var frame = this.getFrame(); 136 137 138 if(fill){ 139 fill.setCanvasStyle(context, this); 140 141 if(fill.id != 'image' && fill.id!='frame'){ 142 context.fillRect(frame.left,frame.top, 143 size.width, size.height); 144 } 145 } 146 147 148 if(stroke){ 149 stroke.setCanvasStyle(context,this); 150 151 if(this.id=='sprite' || this.id=='label'){ 152 var lw = stroke.width_/2; 153 context.strokeRect(frame.left+lw,frame.top+lw, 154 size.width-2*lw, size.height-2*lw); 155 } 156 } 157 158 }; 159 160