1 /**
  2  * The Render Engine
  3  * InputComponent
  4  *
  5  * @fileoverview The base input 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.Input",
 36     "requires":[
 37         "R.components.Base"
 38     ]
 39 });
 40 
 41 /**
 42  * @class A component which can read an input device and make those inputs
 43  *        available to a {@link R.engine.GameObject}.
 44  *
 45  * @param name {String} The name of the component
 46  * @param [priority=1.0] {Number} The component's priority
 47  * @extends R.components.Base
 48  * @constructor
 49  * @description Create an input component.
 50  */
 51 R.components.Input = function () {
 52     "use strict";
 53     return R.components.Base.extend(/** @scope R.components.Input.prototype */{
 54 
 55         recording:false,
 56         playback:false,
 57         script:null,
 58         lastInputTime:0,
 59 
 60         /** @private */
 61         constructor:function (name, priority) {
 62             this.base(name, R.components.Base.TYPE_INPUT, priority || 1.0);
 63             this.recording = false;
 64             this.playback = false;
 65         },
 66 
 67         /** @private */
 68         startRecording:function () {
 69             R.debug.Console.debug("RECORDING INPUT");
 70             this.recording = true;
 71             this.lastInputTime = R.Engine.worldTime;
 72             this.script = [];
 73         },
 74 
 75         /** @private */
 76         stopRecording:function () {
 77             R.debug.Console.debug("RECORDING STOPPED");
 78             this.recording = false;
 79         },
 80 
 81         /** @private */
 82         getScript:function () {
 83             return this.script;
 84         },
 85 
 86         /** @private */
 87         setScript:function (script) {
 88             this.script = script;
 89         },
 90 
 91         /** @private */
 92         playEvent:function () {
 93             // ABSTRACT
 94         },
 95 
 96         /** @private */
 97         playScript:function (script) {
 98             this.recording = false;
 99             this.playback = true;
100             this.script = script;
101 
102             var scriptObj = {
103                 script:script,
104                 playEvent:R.Bind(this, this.playEvent),
105                 evt:null
106             }, popCall;
107 
108             popCall = R.bind(scriptObj, function () {
109                 if (this.script.length == 0) {
110                     return;
111                 }
112                 if (this.evt != null) {
113                     R.debug.Console.log("PLAYBACK:", this.evt.type);
114                     this.playEvent(this.evt);
115                 }
116                 this.evt = this.script.shift();
117                 setTimeout(popCall, this.evt.delay);
118             });
119 
120             popCall();
121         },
122 
123         /** @private */
124         record:function (eventObj, parts, time, dt) {
125             // TODO: Now with engine time and delta we should be able
126             // to accurately record and playback demos
127             if (!this.recording) {
128                 return;
129             }
130             var evtCall = {};
131             for (var x in parts) {
132                 evtCall[parts[x]] = eventObj[parts[x]];
133             }
134             evtCall.delay = R.Engine.worldTime - this.lastInputTime;
135             this.lastInputTime = R.Engine.worldTime;
136             evtCall.type = eventObj.type;
137             this.script.push(evtCall);
138         }
139 
140     }, /** @scope R.components.Input.prototype */{
141         /**
142          * Get the class name of this object
143          *
144          * @return {String} "R.components.Input"
145          */
146         getClassName:function () {
147             return "R.components.Input";
148         }
149     });
150 }