1 /** 2 * The Render Engine 3 * RenderComponent 4 * 5 * @fileoverview The base render component. 6 * 7 * @author: Brett Fattori (brettf@renderengine.com) 8 * @author: $Author: bfattori $ 9 * @version: $Revision: 1555 $ 10 * 11 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a copy 14 * of this software and associated documentation files (the "Software"), to deal 15 * in the Software without restriction, including without limitation the rights 16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 * copies of the Software, and to permit persons to whom the Software is 18 * furnished to do so, subject to the following conditions: 19 * 20 * The above copyright notice and this permission notice shall be included in 21 * all copies or substantial portions of the Software. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 * THE SOFTWARE. 30 * 31 */ 32 33 // The class this file defines and its required classes 34 R.Engine.define({ 35 "class":"R.components.Render", 36 "requires":[ 37 "R.components.Base" 38 ] 39 }); 40 41 /** 42 * @class The base component class for components which render 43 * to an {@link R.rendercontexts.AbstractRenderContext render context}. Rendering 44 * consists of anything which alters the visual state of the render context. 45 * 46 * @param name {String} The name of the component 47 * @param priority {Number} The priority of the component between 0.0 and 1.0 48 * @constructor 49 * @extends R.components.Base 50 * @description Creates a render component. 51 */ 52 R.components.Render = function () { 53 "use strict"; 54 return R.components.Base.extend(/** @scope R.components.Render.prototype */{ 55 56 drawMode:0, 57 origin:null, 58 oldDisplay:null, 59 60 /** 61 * @private 62 */ 63 constructor:function (name, priority) { 64 this.base(name, R.components.Base.TYPE_RENDERING, priority || 0.1); 65 this.oldDisplay = null; 66 this.origin = R.math.Point2D.create(0, 0); 67 }, 68 69 /** 70 * Destroy the component instance 71 */ 72 destroy:function () { 73 this.origin.destroy(); 74 this.base(); 75 }, 76 77 /** 78 * Releases the component back into the object pool. See {@link PooledObject#release} 79 * for more information. 80 */ 81 release:function () { 82 this.base(); 83 this.drawMode = 0; 84 }, 85 86 /** 87 * Set the draw mode of the component. Currently this determines 88 * if the component should render itself to the context or not. 89 * 90 * @param drawMode {Number} One of {@link #DRAW} or 91 * {@link #NO_DRAW}. 92 */ 93 setDrawMode:function (drawMode) { 94 this.drawMode = drawMode; 95 }, 96 97 /** 98 * Get the drawing mode of the component. 99 * @return {Number} 100 */ 101 getDrawMode:function () { 102 return this.drawMode; 103 }, 104 105 /** 106 * Adjust the local transformation to accommodate the origin. 107 * 108 * @param renderContext {R.rendercontexts.AbstractRenderContext} The render context 109 * @param before {Boolean} <code>true</code> if the transform is occurring before rendering 110 */ 111 transformOrigin:function (renderContext, before) { 112 if (this.getGameObject().getOrigin().isZero()) { 113 return; 114 } 115 116 if (!this.origin.equals(this.getGameObject().getOrigin())) { 117 this.origin.set(this.getGameObject().getOrigin()); 118 } 119 120 if (before === true) { 121 renderContext.pushTransform(); 122 renderContext.setPosition(this.origin.neg()); 123 this.origin.neg(); 124 } else { 125 renderContext.popTransform(); 126 } 127 }, 128 129 /** 130 * Handles whether or not the component should draw to the 131 * render context. 132 * 133 * @param renderContext {R.rendercontexts.AbstractRenderContext} The rendering context 134 * @param time {Number} The engine time in milliseconds 135 * @param dt {Number} The delta between the world time and the last time the world was updated 136 * in milliseconds. 137 */ 138 execute:function (renderContext, time, dt) { 139 if (this.isDestroyed() || (R.Engine.options.useDirtyRectangles && !this.getGameObject().isDirty())) { 140 // Objects that aren't dirty don't need to re-render 141 return false; 142 } 143 144 // Check visibility 145 if ((this.drawMode == R.components.Render.NO_DRAW) || 146 this.getGameObject().getWorldBox && 147 (!renderContext.getExpandedViewport().isIntersecting(this.getGameObject().getWorldBox()))) { 148 if (this.getGameObject().getElement() && !this.oldDisplay) { 149 this.oldDisplay = this.getGameObject().jQ().css("display"); 150 this.getGameObject().jQ().css("display", "none"); 151 } 152 153 return false; 154 } 155 156 if (this.getGameObject().getElement() && this.oldDisplay) { 157 this.getGameObject().jQ().css("display", this.oldDisplay); 158 this.oldDisplay = null; 159 } 160 161 // The object is visible 162 R.Engine.vObj++; 163 return true; 164 } 165 166 }, /** @scope R.components.Render.prototype */{ 167 168 /** 169 * Get the class name of this object 170 * 171 * @return {String} "R.components.Render" 172 */ 173 getClassName:function () { 174 return "R.components.Render"; 175 }, 176 177 /** 178 * The component should render itself to the rendering context. 179 * @type {Number} 180 */ 181 DRAW:0, 182 183 /** 184 * The component <i>should not</i> render itself to the rendering context. 185 * @type {Number} 186 */ 187 NO_DRAW:1 188 189 }); 190 }