1 /** 2 * The Render Engine 3 * ButtonControl 4 * 5 * @fileoverview A button control. 6 * 7 * @author: Brett Fattori (brettf@renderengine.com) 8 * 9 * @author: $Author: bfattori $ 10 * @version: $Revision: 1555 $ 11 * 12 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a copy 15 * of this software and associated documentation files (the "Software"), to deal 16 * in the Software without restriction, including without limitation the rights 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 * copies of the Software, and to permit persons to whom the Software is 19 * furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be included in 22 * all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 * THE SOFTWARE. 31 * 32 */ 33 34 // The class this file defines and its required classes 35 R.Engine.define({ 36 "class":"R.ui.ButtonControl", 37 "requires":[ 38 "R.ui.AbstractUIControl" 39 ] 40 }); 41 42 /** 43 * @class UI button control. 44 * 45 * @constructor 46 * @param text {String} The text to display on the button. 47 * @extends R.ui.AbstractUIControl 48 */ 49 R.ui.ButtonControl = function () { 50 return R.ui.AbstractUIControl.extend(/** @scope R.ui.ButtonControl.prototype */{ 51 52 text:null, 53 isDown:false, 54 55 /** @private */ 56 constructor:function (text, textRenderer) { 57 this.base("Button", textRenderer); 58 this.addClass("buttoncontrol"); 59 this.text = text || this.getId(); 60 this.isDown = false; 61 }, 62 63 /** 64 * Destroy the text input control, releasing its event handlers. 65 */ 66 destroy:function () { 67 this.base(); 68 }, 69 70 /** 71 * Releases the object back into the object pool. See {@link R.engine.PooledObject#release} 72 * for more information. 73 */ 74 release:function () { 75 this.base(); 76 this.text = ""; 77 }, 78 79 /** 80 * Set the value of the input control. 81 * @param text {String} Text 82 */ 83 setText:function (text) { 84 this.text = text; 85 }, 86 87 /** 88 * Get the value of the input control. 89 * @return {String} 90 */ 91 getText:function () { 92 return this.text; 93 }, 94 95 /** 96 * Calculate and return the width of the control in pixels. 97 * @return {Number} 98 */ 99 calcWidth:function (str) { 100 this.getTextRenderer().setText(this.text); 101 return this.getBoundingBox().w; 102 }, 103 104 /** 105 * Calculate and return the height of the control in pixels. 106 * @return {Number} 107 */ 108 calcHeight:function () { 109 this.getTextRenderer().setText(this.text); 110 return this.getBoundingBox().h; 111 }, 112 113 /** 114 * Draw the input component within the 115 * @param renderContext {R.rendercontexts.RenderContext2D} The render context where the control is 116 * drawn. 117 * @param worldTime {Number} The current world time, in milliseconds 118 * @param dt {Number} The time since the last frame was drawn by the engine, in milliseconds 119 */ 120 drawControl:function (renderContext, worldTime, dt) { 121 // Draw the current input text. The text baseline is the bottom of the font, 122 // so we need to move that down by the height of the control (with some padding to look right) 123 renderContext.pushTransform(); 124 var rect = R.math.Rectangle2D.create(0, 0, this.calcWidth(), this.calcHeight()), 125 center = rect.getCenter(), tCent = this.getTextRenderer().getBoundingBox().getCenter(); 126 center.x -= tCent.x; 127 center.y += tCent.y / 2; 128 renderContext.setPosition(center); 129 this.getTextRenderer().update(renderContext, worldTime, dt); 130 rect.destroy(); 131 center.destroy(); 132 tCent.destroy(); 133 renderContext.popTransform(); 134 }, 135 136 /** 137 * Returns a bean which represents the read or read/write properties 138 * of the object. 139 * 140 * @return {Object} The properties object 141 */ 142 getProperties:function () { 143 var self = this; 144 var prop = this.base(self); 145 return $.extend(prop, { 146 "Text":[function () { 147 return self.getText(); 148 }, function (i) { 149 self.setText(i); 150 }, true] 151 }); 152 } 153 154 }, /** @scope R.ui.ButtonControl.prototype */{ 155 156 /** 157 * Get the class name of this object 158 * @return {String} The string "R.ui.ButtonControl" 159 */ 160 getClassName:function () { 161 return "R.ui.ButtonControl"; 162 }, 163 164 /** 165 * Get a properties object with values for the given object. 166 * @param obj {R.ui.ButtonControl} The button control to query 167 * @param [defaults] {Object} Default values that don't need to be serialized unless 168 * they are different. 169 * @return {Object} 170 */ 171 serialize:function (obj, defaults) { 172 return R.ui.AbstractUIControl.serialize(obj, defaults); 173 }, 174 175 /** 176 * Deserialize the object back into a button control. 177 * @param obj {Object} The object to deserialize 178 * @param [clazz] {Class} The object class to populate 179 * @return {R.ui.ButtonControl} The object which was deserialized 180 */ 181 deserialize:function (obj, clazz) { 182 clazz = clazz || R.ui.ButtonControl.create(); 183 R.ui.AbstractUIControl.deserialize(obj, clazz); 184 return clazz; 185 } 186 }); 187 188 };