1 /**
  2  * The Render Engine
  3  * DOMRenderComponent
  4  *
  5  * @fileoverview DOM element 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.DOM",
 36     "requires":[
 37         "R.components.Render"
 38     ]
 39 });
 40 
 41 /**
 42  * @class Render component for DOM elements.  This component will ensure that
 43  *        the DOM CSS transformations are applied to the game object for each frame.
 44  *
 45  * @param name {String} The name of the component
 46  * @param priority {Number} The priority of the component between 0.0 and 1.0
 47  * @constructor
 48  * @extends R.components.Render
 49  * @description Creates a DOM element render component.
 50  */
 51 R.components.render.DOM = function () {
 52     "use strict";
 53     return R.components.Render.extend(/** @scope R.components.render.DOM.prototype */{
 54 
 55         scenery:false,
 56         doRefresh:false,
 57         originGlyph:null,
 58 
 59         constructor:function (name, scenery) {
 60             this.base(name);
 61             this.scenery = scenery || false;
 62             this.doRefresh = true;
 63 
 64             if (R.Engine.getDebugMode()) {
 65                 this.originGlyph = $("<div style='z-index: 5000; position: absolute; width: 10px; height: 10px; background: red'></div>");
 66                 $("body", document).append(this.originGlyph);
 67             }
 68         },
 69 
 70         destroy:function () {
 71             this.base();
 72             if (R.Engine.getDebugMode()) {
 73                 this.originGlyph.remove();
 74             }
 75         },
 76 
 77         reset:function () {
 78             this.scenery = false;
 79             this.doRefresh = true;
 80             this.base();
 81         },
 82 
 83         refresh:function () {
 84             this.doRefresh = true;
 85         },
 86 
 87         /**
 88          * Handles whether or not the component should draw to the
 89          * render context.
 90          *
 91          * @param renderContext {R.rendercontexts.HTMLElementContext} The rendering context
 92          * @param time {Number} The engine time in milliseconds
 93          * @param dt {Number} The delta between the world time and the last time the world was updated
 94          *          in milliseconds.
 95          */
 96         execute:function (renderContext, time, dt) {
 97             // Due to this being a render component, it won't update if the object goes off-screen
 98             // by default.  So, we need to make sure it continues to update regardless
 99             this.base(renderContext, time, dt);
100 
101             if (this.doRefresh || !this.scenery) {
102                 renderContext.drawElement(this.getGameObject());
103                 this.doRefresh = !this.scenery;
104             }
105 
106             if (R.Engine.getDebugMode()) {
107                 // Draw a dot where the origin is located
108                 var p = R.math.Point2D.create(5, 5),
109                     pt = R.clone(this.getGameObject().getPosition()).add(this.getGameObject().getOrigin()).sub(p);
110                 renderContext.drawElement(null, this.originGlyph, pt);
111                 pt.destroy();
112                 p.destroy();
113             }
114         }
115 
116     }, /** @scope R.components.render.DOM.prototype */{
117 
118         /**
119          * Get the class name of this object
120          *
121          * @return {String} "R.components.render.DOM"
122          */
123         getClassName:function () {
124             return "R.components.render.DOM";
125         }
126     });
127 }