1 /** 2 * The Render Engine 3 * LabelControl 4 * 5 * @fileoverview A label 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.LabelControl", 37 "requires":[ 38 "R.ui.AbstractUIControl" 39 ] 40 }); 41 42 /** 43 * @class UI label control. 44 * 45 * @constructor 46 * @param text {String} The text to display for the button. 47 * @param [forControl] {R.ui.AbstractUIControl} The control the label will set focus to if clicked 48 * @extends R.ui.AbstractUIControl 49 */ 50 R.ui.LabelControl = function () { 51 return R.ui.AbstractUIControl.extend(/** @scope R.ui.LabelControl.prototype */{ 52 53 text:null, 54 forControl:false, 55 56 /** @private */ 57 constructor:function (text, forControl, textRenderer) { 58 this.base("Label", textRenderer); 59 this.addClass("labelcontrol"); 60 this.text = text || ""; 61 this.forControl = forControl; 62 }, 63 64 /** 65 * Destroy the text input control, releasing its event handlers. 66 */ 67 destroy:function () { 68 this.base(); 69 }, 70 71 /** 72 * Releases the object back into the object pool. See {@link R.engine.PooledObject#release} 73 * for more information. 74 */ 75 release:function () { 76 this.base(); 77 this.text = ""; 78 this.forControl = null; 79 }, 80 81 /** 82 * Set the text of this label control. 83 * @param text {String} Text 84 */ 85 setText:function (text) { 86 this.text = text; 87 }, 88 89 /** 90 * Get the text of this label control. 91 * @return {String} 92 */ 93 getText:function () { 94 return this.text; 95 }, 96 97 /** 98 * Link the label to a UI control. When the label is clicked, the UI control will 99 * receive focus. 100 * @param uiControl {R.ui.AbstractUIControl} The control to link to 101 */ 102 linkTo:function (uiControl) { 103 Assert(uiControl == null || uiControl instanceof R.ui.AbstractUIControl, "Labels can only be linked to UI controls"); 104 this.forControl = uiControl; 105 }, 106 107 /** 108 * Get the UI control this label is linked to. 109 * @return {R.ui.AbstractUIControl} The UI control, or <code>null</code> 110 */ 111 getLinkTo:function () { 112 return this.forControl; 113 }, 114 115 /** 116 * Called when a mouse button is pressed, then released on the control. Triggers the 117 * "click" event, passing the <code>R.struct.MouseInfo</code> structure. 118 * 119 * @param mouseInfo {R.struct.MouseInfo} The mouse info structure 120 */ 121 click:function (mouseInfo) { 122 this.base(mouseInfo); 123 if (this.forControl != null) { 124 this.forControl.click(mouseInfo); 125 } 126 }, 127 128 /** 129 * Draw the input component within the 130 * @param renderContext {R.rendercontexts.RenderContext2D} The render context where the control is 131 * drawn. 132 * @param worldTime {Number} The current world time, in milliseconds 133 * @param dt {Number} The time since the last frame was drawn by the engine, in milliseconds 134 */ 135 drawControl:function (renderContext, worldTime, dt) { 136 this.getTextRenderer().setText(this.text); 137 138 // Draw the current label text. The text baseline is the bottom of the font, 139 // so we need to move that down by the height of the control (with some padding to look right) 140 renderContext.pushTransform(); 141 var pt = R.math.Point2D.create(0, this.calcHeight() - 2); 142 renderContext.setPosition(pt); 143 this.getTextRenderer().update(renderContext, worldTime, dt); 144 pt.destroy(); 145 renderContext.popTransform(); 146 }, 147 148 /** 149 * Returns a bean which represents the read or read/write properties 150 * of the object. 151 * 152 * @return {Object} The properties object 153 */ 154 getProperties:function () { 155 var self = this; 156 var prop = this.base(self); 157 return $.extend(prop, { 158 "Text":[function () { 159 return self.getText(); 160 }, function (i) { 161 self.setText(i); 162 }, true], 163 "ForControl":[function () { 164 return self.getLinkTo() != null ? self.getLinkTo().getControlName() : ""; 165 }, function (i) { 166 // TODO: need to figure out how to do this... 167 //self.linkTo(i); 168 }, true] 169 }); 170 } 171 172 }, /** @scope R.ui.LabelControl.prototype */{ 173 174 /** 175 * Get the class name of this object 176 * @return {String} The string "R.ui.LabelControl" 177 */ 178 getClassName:function () { 179 return "R.ui.LabelControl"; 180 }, 181 182 /** 183 * Get a properties object with values for the given object. 184 * @param obj {R.ui.LabelControl} The label control to query 185 * @param [defaults] {Object} Default values that don't need to be serialized unless 186 * they are different. 187 * @return {Object} 188 */ 189 serialize:function (obj, defaults) { 190 // Defaults for object properties which can be skipped if no different 191 defaults = defaults || []; 192 $.extend(defaults, { 193 "ForControl":"" 194 }); 195 return R.ui.AbstractUIControl.serialize(obj, defaults); 196 }, 197 198 /** 199 * Deserialize the object back into a label control. 200 * @param obj {Object} The object to deserialize 201 * @param [clazz] {Class} The object class to populate 202 * @return {R.ui.LabelControl} The object which was deserialized 203 */ 204 deserialize:function (obj, clazz) { 205 clazz = clazz || R.ui.LabelControl.create(); 206 R.ui.AbstractUIControl.deserialize(obj, clazz); 207 return clazz; 208 } 209 }); 210 211 };