1 /** 2 * The Render Engine 3 * TextRenderer 4 * 5 * @fileoverview A text renderer object that uses a specific render 6 * object to produce text when a render context cannot. 7 * 8 * 9 * @author: Brett Fattori (brettf@renderengine.com) 10 * 11 * @author: $Author: bfattori $ 12 * @version: $Revision: 1555 $ 13 * 14 * Copyright (c) 2011 Brett Fattori (brettf@renderengine.com) 15 * 16 * Permission is hereby granted, free of charge, to any person obtaining a copy 17 * of this software and associated documentation files (the "Software"), to deal 18 * in the Software without restriction, including without limitation the rights 19 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 * copies of the Software, and to permit persons to whom the Software is 21 * furnished to do so, subject to the following conditions: 22 * 23 * The above copyright notice and this permission notice shall be included in 24 * all copies or substantial portions of the Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 32 * THE SOFTWARE. 33 * 34 */ 35 36 // The class this file defines and its required classes 37 R.Engine.define({ 38 "class":"R.text.TextRenderer", 39 "requires":[ 40 "R.objects.Object2D", 41 "R.components.Transform2D", 42 "R.rendercontexts.RenderContext2D", 43 "R.components.render.Billboard2D" 44 ] 45 }); 46 47 /** 48 * @class A 2d text rendering object. The object hosts the given text 49 * renderer, and a way to position and size the text. It is up 50 * to the renderer provided to present the text within the render 51 * context. 52 * 53 * @constructor 54 * @description Create an instance of one of the text renderers. 55 * @param renderer {AbstractTextRenderer} The text renderer to use 56 * @param text {String} The text to render 57 * @param size {Number} The size of the text to render 58 * @see R.text.VectorText 59 * @see R.text.BitmapText 60 * @see R.text.ContextText 61 */ 62 R.text.TextRenderer = function () { 63 return R.objects.Object2D.extend(/** @scope R.text.TextRenderer.prototype */{ 64 65 drawMode:0, 66 67 renderer:null, 68 69 /** @private */ 70 constructor:function (renderer, text, size) { 71 72 Assert((R.text.AbstractTextRenderer.isInstance(renderer)), "Text renderer must extend AbstractTextRenderer"); 73 74 this.base("TextRenderer"); 75 76 // Add components to move and draw the text 77 this.renderer = renderer; 78 if (!R.Engine.options.textUseBillboards || renderer.isNative()) { 79 this.add(this.renderer); 80 } else { 81 this.add(R.components.render.Billboard2D.create("billboard", this.renderer)); 82 } 83 84 renderer.setText(text || ""); 85 renderer.setSize(size || 1); 86 this.drawMode = R.text.TextRenderer.DRAW_TEXT; 87 }, 88 89 /** 90 * Release the text renderer back into the pool for reuse 91 */ 92 release:function () { 93 this.base(); 94 this.drawMode = R.text.TextRenderer.DRAW_TEXT; 95 this.renderer = null; 96 }, 97 98 /** 99 * Called to render the text to the context. 100 * 101 * @param renderContext {R.rendercontexts.AbstractRenderContext} The context to render the text into 102 * @param time {Number} The engine time in milliseconds 103 * @param dt {Number} The delta between the world time and the last time the world was updated 104 * in milliseconds. 105 */ 106 update:function (renderContext, time, dt) { 107 108 if (this.drawMode == R.text.TextRenderer.DRAW_TEXT) { 109 renderContext.pushTransform(); 110 this.base(renderContext, time, dt); 111 renderContext.popTransform(); 112 } 113 114 }, 115 116 /** 117 * @private 118 */ 119 regen:function () { 120 if (!R.Engine.options.textUseBillboards || this.renderer.isNative()) { 121 return; 122 } else { 123 this.getComponent("billboard").regenerate(); 124 } 125 }, 126 127 /** 128 * Set the text for this object to render. This method 129 * <i>must</i> be implemented by a text renderer. 130 * 131 * @param text {String} The text to render. 132 */ 133 setText:function (text) { 134 this.renderer.setText(text); 135 this.regen(); 136 }, 137 138 /** 139 * Get the text for this object to render. This method 140 * <i>must</i> be implemented by a text renderer. 141 * 142 * @return {String} The text to draw 143 */ 144 getText:function () { 145 return this.renderer.getText(); 146 }, 147 148 /** 149 * Set the size of the text to render. 150 * 151 * @param size {Number} Defaults to 1 152 */ 153 setTextSize:function (size) { 154 this.renderer.setSize(size || 1); 155 this.regen(); 156 }, 157 158 /** 159 * Get the size of the text to render. 160 * @return {Number} The size/scale of the text 161 */ 162 getTextSize:function () { 163 return this.renderer.getSize(); 164 }, 165 166 /** 167 * Set the weight (boldness) of the text. This method 168 * is optional for a text renderer. 169 * 170 * @param weight {Object} The boldness of the given text renderer 171 */ 172 setTextWeight:function (weight) { 173 this.renderer.setTextWeight(weight); 174 this.regen(); 175 }, 176 177 /** 178 * Get the weight of the text. This method is optional 179 * for a text renderer. 180 * @return {Object} The boldness of the given text renderer 181 */ 182 getTextWeight:function () { 183 return this.renderer.getTextWeight(); 184 }, 185 186 /** 187 * Set the font for the text. This method is optional 188 * for a text renderer. 189 * @param font {String} The text font 190 */ 191 setTextFont:function (font) { 192 this.renderer.setTextFont(font); 193 this.regen(); 194 }, 195 196 /** 197 * Get the font for the text. This method is optional 198 * for a text renderer. 199 * @return {String} The text font 200 */ 201 getTextFont:function () { 202 return this.renderer.getTextFont(); 203 }, 204 205 /** 206 * Set the style of the text. This method is optional 207 * for a text renderer. 208 * @param style {String} The text style 209 */ 210 setTextStyle:function (style) { 211 this.renderer.setTextStyle(style); 212 this.regen(); 213 }, 214 215 /** 216 * Get the style for text. This method is optional 217 * for a text renderer. 218 * @return {String} The text style 219 */ 220 getTextStyle:function () { 221 return this.renderer.getTextStyle(); 222 }, 223 224 /** 225 * Set the horizontal alignment of the text. This method is optional 226 * for a text renderer 227 * 228 * @param alignment {Object} A text alignment mode for the given text renderer. 229 */ 230 setTextAlignment:function (alignment) { 231 this.renderer.setTextAlignment(alignment); 232 this.regen(); 233 }, 234 235 /** 236 * Get the horizontal alignment of the text. This method is optional 237 * for a text renderer. 238 * @return {Number} The alignment mode for the given text renderer 239 */ 240 getTextAlignment:function () { 241 return this.renderer.getTextAlignment(); 242 }, 243 244 /** 245 * Set the color of the text to render. 246 * 247 * @param color {String} The color of the text 248 */ 249 setColor:function (color) { 250 this.renderer.setColor(color); 251 this.regen(); 252 }, 253 254 /** 255 * Get the color of the text to render 256 * @return {String} The text color 257 */ 258 getColor:function () { 259 return this.renderer.getColor(); 260 }, 261 262 /** 263 * Set the color of the text. 264 * 265 * @param textColor {String} Color of the text. 266 */ 267 setTextColor:function (textColor) { 268 this.renderer.setColor(textColor); 269 this.regen(); 270 }, 271 272 /** 273 * Get the text color 274 * @return {String} The color or style of the line 275 */ 276 getTextColor:function () { 277 return this.renderer.getColor(); 278 }, 279 280 /** 281 * Set the text drawing mode to either {@link #DRAW_TEXT} or {@link #NO_DRAW}. 282 * 283 * @param drawMode {Number} The drawing mode for the text. 284 */ 285 setDrawMode:function (drawMode) { 286 this.drawMode = drawMode; 287 }, 288 289 /** 290 * Get the current drawing mode for the text. 291 * @return {Number} The text drawing mode 292 */ 293 getDrawMode:function () { 294 return this.drawMode; 295 } 296 }, /** @scope R.text.TextRenderer.prototype */{ 297 298 /** 299 * Get the class name of this object 300 * @return {String} The string "R.text.TextRenderer" 301 */ 302 getClassName:function () { 303 return "R.text.TextRenderer"; 304 }, 305 306 /** 307 * Draw the text to the context. 308 * @type {Number} 309 */ 310 DRAW_TEXT:0, 311 312 /** 313 * Don't draw the text to the context. 314 * @type {Number} 315 */ 316 NO_DRAW:1, 317 318 /** 319 * The size of a text element, in pixels 320 * @type {Number} 321 */ 322 BASE_TEXT_PIXELSIZE:1 323 }); 324 325 };