1 /** 2 * The Render Engine 3 * CheckboxControl 4 * 5 * @fileoverview A check box 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.CheckboxControl", 37 "requires":[ 38 "R.ui.AbstractUIControl" 39 ] 40 }); 41 42 /** 43 * @class UI checkbox control. 44 * 45 * @constructor 46 * @param [checked] {Boolean} Whether the checkbox is checked, or not. 47 * @extends R.ui.AbstractUIControl 48 */ 49 R.ui.CheckboxControl = function () { 50 return R.ui.AbstractUIControl.extend(/** @scope R.ui.CheckboxControl.prototype */{ 51 52 checked:false, 53 54 /** @private */ 55 constructor:function (checked) { 56 this.base("Checkbox"); 57 this.addClass("checkboxcontrol"); 58 this.setChecked(checked || false); 59 }, 60 61 /** 62 * Releases the object back into the object pool. See {@link R.engine.PooledObject#release} 63 * for more information. 64 */ 65 release:function () { 66 this.base(); 67 this.checked = false; 68 }, 69 70 /** 71 * Respond to a "click" action on the checkbox. 72 * @param mouseInfo {R.struct.MouseInfo} A mouse info structure 73 */ 74 click:function (mouseInfo) { 75 this.setChecked(!this.isChecked()); 76 this.base(mouseInfo); 77 }, 78 79 /** 80 * Set the "checked" state of the control. Triggers the "change" 81 * event on the control. 82 * @param state {Boolean} <code>true</code> to mark the control as "checked" 83 */ 84 setChecked:function (state) { 85 this.checked = state; 86 this.triggerEvent("change"); 87 }, 88 89 /** 90 * Get the "checked" state of the control. 91 * @return {String} 92 */ 93 isChecked:function () { 94 return this.checked; 95 }, 96 97 /** 98 * Draw the input component within the 99 * @param renderContext {R.rendercontexts.RenderContext2D} The render context where the control is 100 * drawn. 101 * @param worldTime {Number} The current world time, in milliseconds 102 * @param dt {Number} The time since the last frame was drawn by the engine, in milliseconds 103 */ 104 drawControl:function (renderContext, worldTime, dt) { 105 // Draw a check mark if the control is "checked" 106 if (this.checked) { 107 renderContext.pushTransform(); 108 renderContext.setLineWidth(2); 109 var bBox = this.getBoundingBox(), topLeft = R.clone(bBox.getTopLeft()), 110 topRight = R.clone(bBox.getTopLeft()), bottomRight = R.clone(bBox.getBottomRight()), 111 bottomLeft = R.clone(bBox.getTopLeft()); 112 renderContext.setLineStyle(this.getTextRenderer().getTextColor()); 113 topLeft.x += 2; 114 topLeft.y += 2; 115 bottomRight.x -= 2; 116 bottomRight.y -= 2; 117 renderContext.drawLine(topLeft, bottomRight); 118 topRight.x += bBox.w - 2; 119 topRight.y += 2; 120 bottomLeft.y += bBox.h - 2; 121 bottomLeft.x += 2; 122 renderContext.drawLine(topRight, bottomLeft); 123 topRight.destroy(); 124 bottomLeft.destroy(); 125 topLeft.destroy(); 126 bottomRight.destroy(); 127 renderContext.popTransform(); 128 } 129 }, 130 131 /** 132 * Returns a bean which represents the read or read/write properties 133 * of the object. 134 * 135 * @return {Object} The properties object 136 */ 137 getProperties:function () { 138 var self = this; 139 var prop = this.base(self); 140 return $.extend(prop, { 141 "Checked":[function () { 142 return self.isChecked(); 143 }, { 144 "toggle":true, 145 "fn":function (s) { 146 self.setChecked(s); 147 } 148 }, true] 149 }); 150 } 151 152 }, /** @scope R.ui.CheckboxControl.prototype */{ 153 154 /** 155 * Get the class name of this object 156 * @return {String} The string "R.ui.CheckboxControl" 157 */ 158 getClassName:function () { 159 return "R.ui.CheckboxControl"; 160 }, 161 162 /** 163 * Get a properties object with values for the given object. 164 * @param obj {R.ui.CheckboxControl} The checkbox control to query 165 * @param [defaults] {Object} Default values that don't need to be serialized unless 166 * they are different. 167 * @return {Object} 168 */ 169 serialize:function (obj, defaults) { 170 // Defaults for object properties which can be skipped if no different 171 defaults = defaults || []; 172 $.extend(defaults, { 173 "Checked":false 174 }); 175 return R.ui.AbstractUIControl.serialize(obj, defaults); 176 }, 177 178 /** 179 * Deserialize the object back into a checkbox control. 180 * @param obj {Object} The object to deserialize 181 * @param [clazz] {Class} The object class to populate 182 * @return {R.ui.CheckboxControl} The object which was deserialized 183 */ 184 deserialize:function (obj, clazz) { 185 clazz = clazz || R.ui.CheckboxControl.create(); 186 R.ui.AbstractUIControl.deserialize(obj, clazz); 187 return clazz; 188 } 189 }); 190 191 };