1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 4 http://www.cocos2d-x.org 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 ****************************************************************************/ 24 /** 25 * Base class for ccs.SceneReader 26 * @class 27 * @extends ccs.Class 28 */ 29 ccs.GUIReader = ccs.Class.extend(/** @lends ccs.GUIReader# */{ 30 _filePath: "", 31 _olderVersion: false, 32 _fileDesignSizes: {}, 33 ctor: function () { 34 this._filePath = ""; 35 this._olderVersion = false; 36 this._fileDesignSizes = {}; 37 }, 38 39 /** 40 * get version 41 * @param {String} str 42 * @returns {Number} 43 */ 44 getVersionInteger: function (str) { 45 if(!str) 46 return 0; 47 var strVersion = str; 48 var versionLength = strVersion.length; 49 if (versionLength < 7) { 50 return 0; 51 } 52 var pos = strVersion.indexOf("."); 53 var t = strVersion.substr(0, pos); 54 strVersion = strVersion.substr(pos + 1, versionLength - 1); 55 56 pos = strVersion.indexOf("."); 57 var h = strVersion.substr(0, pos); 58 strVersion = strVersion.substr(pos + 1, versionLength - 1); 59 60 pos = strVersion.indexOf("."); 61 var te = strVersion.substr(0, pos); 62 strVersion = strVersion.substr(pos + 1, versionLength - 1); 63 64 pos = strVersion.indexOf("."); 65 var s; 66 if (pos == -1) { 67 s = strVersion; 68 } else { 69 s = strVersion.substr(0, pos); 70 } 71 72 var it = parseInt(t); 73 var ih = parseInt(h); 74 var ite = parseInt(te); 75 var is = parseInt(s); 76 77 var version = it * 1000 + ih * 100 + ite * 10 + is; 78 return version; 79 }, 80 81 /** 82 * store file designSize 83 * @param {String} fileName 84 * @param {cc.Size} size 85 */ 86 storeFileDesignSize: function (fileName, size) { 87 this._fileDesignSizes[fileName] = size; 88 }, 89 90 /** 91 * 92 * @param {String} fileName 93 * @returns {cc.Size} 94 */ 95 getFileDesignSize: function (fileName) { 96 return this._fileDesignSizes[fileName]; 97 }, 98 99 /** 100 * create uiWidget from a josn file that exported by cocostudio UI editor 101 * @param {String} fileName 102 * @returns {ccs.Widget} 103 */ 104 widgetFromJsonFile: function (fileName) { 105 var jsonPath = fileName || ""; 106 var pos = jsonPath.lastIndexOf('/'); 107 this._filePath = jsonPath.substr(0, pos + 1); 108 var des = cc.FileUtils.getInstance().getTextFileData(jsonPath); 109 if (!des) { 110 cc.log("read json file[" + fileName + "] error!"); 111 return null; 112 } 113 var jsonDict = JSON.parse(des); 114 115 var fileVersion = jsonDict["version"]; 116 var pReader, widget; 117 var versionInteger = this.getVersionInteger(fileVersion); 118 if (fileVersion) { 119 if (versionInteger < 250) { 120 pReader = new ccs.WidgetPropertiesReader0250(); 121 widget = pReader.createWidget(jsonDict, this._filePath, fileName); 122 } else { 123 pReader = new ccs.WidgetPropertiesReader0300(); 124 widget = pReader.createWidget(jsonDict, this._filePath, fileName); 125 } 126 } else { 127 pReader = new ccs.WidgetPropertiesReader0250(); 128 widget = pReader.createWidget(jsonDict, this._filePath, fileName); 129 } 130 131 if (!fileVersion || versionInteger < 250) { 132 this._olderVersion = true; 133 } 134 jsonDict = null; 135 des = null; 136 return widget; 137 } 138 }); 139 ccs.WidgetPropertiesReader = ccs.Class.extend({ 140 _filePath: "", 141 createWidget: function (jsonDict, fullPath, fileName) { 142 }, 143 widgetFromJsonDictionary: function (data) { 144 } 145 }); 146 ccs.WidgetPropertiesReader0250 = ccs.WidgetPropertiesReader.extend({ 147 createWidget: function (jsonDict, fullPath, fileName) { 148 this._filePath = fullPath; 149 var textures = jsonDict["textures"]; 150 for (var i = 0; i < textures.length; i++) { 151 var file = textures[i]; 152 var tp = fullPath; 153 tp += file; 154 cc.SpriteFrameCache.getInstance().addSpriteFrames(tp); 155 } 156 var fileDesignWidth = jsonDict["designWidth"]; 157 var fileDesignHeight = jsonDict["designHeight"]; 158 if (fileDesignWidth <= 0 || fileDesignHeight <= 0) { 159 cc.log("Read design size error!"); 160 var winSize = cc.Director.getInstance().getWinSize(); 161 ccs.GUIReader.getInstance().storeFileDesignSize(fileName, winSize); 162 } 163 else { 164 ccs.GUIReader.getInstance().storeFileDesignSize(fileName, cc.size(fileDesignWidth, fileDesignHeight)); 165 } 166 var widgetTree = jsonDict["widgetTree"]; 167 var widget = this.widgetFromJsonDictionary(widgetTree); 168 169 var size = widget.getContentSize(); 170 if (size.width == 0 && size.height == 0) { 171 widget.setSize(cc.size(fileDesignWidth, fileDesignHeight)); 172 } 173 174 var actions = jsonDict["animation"]; 175 var rootWidget = widget; 176 ccs.ActionManager.getInstance().initWithDictionary(fileName, actions, rootWidget); 177 178 widgetTree = null; 179 actions = null; 180 return widget; 181 }, 182 widgetFromJsonDictionary: function (data) { 183 var widget = null; 184 var classname = data["classname"]; 185 var uiOptions = data["options"]; 186 if (classname == "Button") { 187 widget = ccs.Button.create(); 188 this.setPropsForButtonFromJsonDictionary(widget, uiOptions); 189 } 190 else if (classname == "CheckBox") { 191 widget = ccs.CheckBox.create(); 192 this.setPropsForCheckBoxFromJsonDictionary(widget, uiOptions); 193 } 194 else if (classname == "Label") { 195 widget = ccs.Label.create(); 196 this.setPropsForLabelFromJsonDictionary(widget, uiOptions); 197 } 198 else if (classname == "LabelAtlas") { 199 widget = ccs.LabelAtlas.create(); 200 this.setPropsForLabelAtlasFromJsonDictionary(widget, uiOptions); 201 } 202 else if (classname == "LoadingBar") { 203 widget = ccs.LoadingBar.create(); 204 this.setPropsForLoadingBarFromJsonDictionary(widget, uiOptions); 205 } else if (classname == "ScrollView") { 206 widget = ccs.ScrollView.create(); 207 this.setPropsForScrollViewFromJsonDictionary(widget, uiOptions); 208 } 209 else if (classname == "TextArea") { 210 widget = ccs.Label.create(); 211 this.setPropsForLabelFromJsonDictionary(widget, uiOptions); 212 } 213 else if (classname == "TextButton") { 214 widget = ccs.Button.create(); 215 this.setPropsForButtonFromJsonDictionary(widget, uiOptions); 216 } 217 else if (classname == "TextField") { 218 widget = ccs.TextField.create(); 219 this.setPropsForTextFieldFromJsonDictionary(widget, uiOptions); 220 } 221 else if (classname == "ImageView") { 222 widget = ccs.ImageView.create(); 223 this.setPropsForImageViewFromJsonDictionary(widget, uiOptions); 224 } 225 else if (classname == "Panel") { 226 widget = ccs.Layout.create(); 227 this.setPropsForLayoutFromJsonDictionary(widget, uiOptions); 228 } 229 else if (classname == "Slider") { 230 widget = ccs.Slider.create(); 231 this.setPropsForSliderFromJsonDictionary(widget, uiOptions); 232 } 233 else if (classname == "LabelBMFont") { 234 widget = ccs.LabelBMFont.create(); 235 this.setPropsForLabelBMFontFromJsonDictionary(widget, uiOptions); 236 } 237 else if (classname == "DragPanel") { 238 widget = ccs.ScrollView.create(); 239 this.setPropsForScrollViewFromJsonDictionary(widget, uiOptions); 240 } 241 var children = data["children"]; 242 for (var i = 0; i < children.length; i++) { 243 var subData = children[i]; 244 var child = this.widgetFromJsonDictionary(subData); 245 if (child) { 246 widget.addChild(child); 247 } 248 subData = null; 249 } 250 251 uiOptions = null; 252 return widget; 253 }, 254 255 256 setPropsForWidgetFromJsonDictionary: function (widget, options) { 257 if (options.hasOwnProperty("ignoreSize")) { 258 widget.ignoreContentAdaptWithSize(options["ignoreSize"]); 259 } 260 261 var w = options["width"]; 262 var h = options["height"]; 263 widget.setSize(cc.size(w, h)); 264 265 widget.setTag(options["tag"]); 266 widget.setActionTag(options["actiontag"]); 267 widget.setTouchEnabled(options["touchAble"]); 268 var name = options["name"]; 269 var widgetName = name ? name : "default"; 270 widget.setName(widgetName); 271 var x = options["x"]; 272 var y = options["y"]; 273 widget.setPosition(cc.p(x, y)); 274 if (options.hasOwnProperty("scaleX")) { 275 widget.setScaleX(options["scaleX"]); 276 } 277 if (options.hasOwnProperty("scaleY")) { 278 widget.setScaleY(options["scaleY"]); 279 } 280 if (options.hasOwnProperty("rotation")) { 281 widget.setRotation(options["rotation"]); 282 } 283 if (options.hasOwnProperty("visible")) { 284 widget.setVisible(options["visible"]); 285 } 286 287 var z = options["ZOrder"]; 288 widget.setZOrder(z); 289 }, 290 291 setColorPropsForWidgetFromJsonDictionary: function (widget, options) { 292 if (options.hasOwnProperty("opacity")) { 293 widget.setOpacity(options["opacity"]); 294 } 295 var colorR = options.hasOwnProperty("colorR") ? options["colorR"] : 255; 296 var colorG = options.hasOwnProperty("colorG") ? options["colorG"] : 255; 297 var colorB = options.hasOwnProperty("colorB") ? options["colorB"] : 255; 298 widget.setColor(cc.c3b(colorR, colorG, colorB)); 299 var apx = options.hasOwnProperty("anchorPointX") ? options["anchorPointX"] : ((widget.getWidgetType() == ccs.WidgetType.widget) ? 0.5 : 0); 300 var apy = options.hasOwnProperty("anchorPointY") ? options["anchorPointY"] : ((widget.getWidgetType() == ccs.WidgetType.widget) ? 0.5 : 0); 301 widget.setAnchorPoint(apx, apy); 302 var flipX = options["flipX"]; 303 var flipY = options["flipY"]; 304 widget.setFlippedX(flipX); 305 widget.setFlippedY(flipY); 306 }, 307 308 setPropsForButtonFromJsonDictionary: function (widget, options) { 309 this.setPropsForWidgetFromJsonDictionary(widget, options); 310 var button = widget; 311 var scale9Enable = options["scale9Enable"]; 312 button.setScale9Enabled(scale9Enable); 313 314 var normalFileName = options["normal"]; 315 var pressedFileName = options["pressed"]; 316 var disabledFileName = options["disabled"]; 317 318 var normalFileName_tp = normalFileName ? this._filePath + normalFileName : null; 319 var pressedFileName_tp = pressedFileName ? this._filePath + pressedFileName : null; 320 var disabledFileName_tp = disabledFileName ? this._filePath + disabledFileName : null; 321 var useMergedTexture = options["useMergedTexture"]; 322 if (scale9Enable) { 323 var cx = options["capInsetsX"]; 324 var cy = options["capInsetsY"]; 325 var cw = options["capInsetsWidth"]; 326 var ch = options["capInsetsHeight"]; 327 328 if (useMergedTexture) { 329 button.loadTextures(normalFileName, pressedFileName, disabledFileName, ccs.TextureResType.plist); 330 } 331 else { 332 button.loadTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp); 333 } 334 //button.setCapInsets(cc.rect(cx, cy, cw, ch)); 335 if (options.hasOwnProperty("scale9Width") && options.hasOwnProperty("scale9Height")) { 336 var swf = options["scale9Width"]; 337 var shf = options["scale9Height"]; 338 button.setSize(cc.size(swf, shf)); 339 } 340 } 341 else { 342 if (useMergedTexture) { 343 button.loadTextures(normalFileName, pressedFileName, disabledFileName, ccs.TextureResType.plist); 344 } 345 else { 346 button.loadTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp); 347 } 348 } 349 if (options.hasOwnProperty("text")) { 350 var text = options["text"] || ""; 351 if (text) 352 button.setTitleText(text); 353 } 354 if (options.hasOwnProperty("fontSize")) { 355 button.setTitleFontSize(options["fontSize"]); 356 } 357 if (options.hasOwnProperty("fontName")) { 358 button.setTitleFontName(options["fontName"]); 359 } 360 var cr = options.hasOwnProperty("textColorR") ? options["textColorR"] : 255; 361 var cg = options.hasOwnProperty("textColorG") ? options["textColorG"] : 255; 362 var cb = options.hasOwnProperty("textColorB") ? options["textColorB"] : 255; 363 var tc = cc.c3b(cr, cg, cb); 364 button.setTitleColor(tc); 365 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 366 }, 367 368 setPropsForCheckBoxFromJsonDictionary: function (widget, options) { 369 this.setPropsForWidgetFromJsonDictionary(widget, options); 370 var checkBox = widget; 371 var backGroundFileName = options["backGroundBox"]; 372 var backGroundSelectedFileName = options["backGroundBoxSelected"]; 373 var frontCrossFileName = options["frontCross"]; 374 var backGroundDisabledFileName = options["backGroundBoxDisabled"]; 375 var frontCrossDisabledFileName = options["frontCrossDisabled"]; 376 377 var locFilePath = this._filePath; 378 379 var backGroundFileName_tp = backGroundFileName ? locFilePath + backGroundFileName : null; 380 var backGroundSelectedFileName_tp = backGroundSelectedFileName ? locFilePath + backGroundSelectedFileName : null; 381 var frontCrossFileName_tp = frontCrossFileName ? locFilePath + frontCrossFileName : null; 382 var backGroundDisabledFileName_tp = backGroundDisabledFileName ? locFilePath + backGroundDisabledFileName : null; 383 var frontCrossDisabledFileName_tp = frontCrossDisabledFileName ? locFilePath + frontCrossDisabledFileName : null; 384 var useMergedTexture = options["useMergedTexture"]; 385 386 if (useMergedTexture) { 387 checkBox.loadTextures(backGroundFileName, backGroundSelectedFileName, frontCrossFileName, backGroundDisabledFileName, frontCrossDisabledFileName, ccs.TextureResType.plist); 388 } 389 else { 390 checkBox.loadTextures(backGroundFileName_tp, backGroundSelectedFileName_tp, frontCrossFileName_tp, backGroundDisabledFileName_tp, frontCrossDisabledFileName_tp); 391 } 392 393 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 394 }, 395 396 setPropsForImageViewFromJsonDictionary: function (widget, options) { 397 this.setPropsForWidgetFromJsonDictionary(widget, options); 398 399 var imageView = widget; 400 var imageFileName = options["fileName"]; 401 var scale9Enable = options["scale9Enable"] || false; 402 imageView.setScale9Enabled(scale9Enable); 403 404 var tp_i = this._filePath; 405 var imageFileName_tp = null; 406 if (imageFileName) { 407 imageFileName_tp = tp_i + imageFileName; 408 } 409 410 var useMergedTexture = options["useMergedTexture"]; 411 if (scale9Enable) { 412 if (useMergedTexture) { 413 imageView.loadTexture(imageFileName, ccs.TextureResType.plist); 414 } 415 else { 416 imageView.loadTexture(imageFileName_tp); 417 } 418 419 if (options.hasOwnProperty("scale9Width") && options.hasOwnProperty("scale9Height")) { 420 var swf = options["scale9Width"]; 421 var shf = options["scale9Height"]; 422 imageView.setSize(cc.size(swf, shf)); 423 } 424 425 var cx = options["capInsetsX"]; 426 var cy = options["capInsetsY"]; 427 var cw = options["capInsetsWidth"]; 428 var ch = options["capInsetsHeight"]; 429 imageView.setCapInsets(cc.rect(cx, cy, cw, ch)); 430 431 } 432 else { 433 if (useMergedTexture) { 434 imageView.loadTexture(imageFileName, ccs.TextureResType.plist); 435 } 436 else { 437 imageView.loadTexture(imageFileName_tp); 438 } 439 } 440 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 441 }, 442 443 setPropsForLabelFromJsonDictionary: function (widget, options) { 444 this.setPropsForWidgetFromJsonDictionary(widget, options); 445 var label = widget; 446 var touchScaleChangeAble = options["touchScaleEnable"]; 447 label.setTouchScaleChangeAble(touchScaleChangeAble); 448 var text = options["text"]; 449 label.setText(text); 450 if (options.hasOwnProperty("fontSize")) { 451 label.setFontSize(options["fontSize"]); 452 } 453 if (options.hasOwnProperty("fontName")) { 454 label.setFontName(options["fontName"]); 455 } 456 if (options.hasOwnProperty("areaWidth") && options.hasOwnProperty("areaHeight")) { 457 var size = cc.size(options["areaWidth"], options["areaHeight"]); 458 label.setTextAreaSize(size); 459 } 460 if (options.hasOwnProperty("hAlignment")) { 461 label.setTextHorizontalAlignment(options["hAlignment"]); 462 } 463 if (options.hasOwnProperty("vAlignment")) { 464 label.setTextVerticalAlignment(options["vAlignment"]); 465 } 466 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 467 }, 468 469 setPropsForLabelAtlasFromJsonDictionary: function (widget, options) { 470 this.setPropsForWidgetFromJsonDictionary(widget, options); 471 var labelAtlas = widget; 472 var sv = options.hasOwnProperty("stringValue"); 473 var cmf = options.hasOwnProperty("charMapFile"); 474 var iw = options.hasOwnProperty("itemWidth"); 475 var ih = options.hasOwnProperty("itemHeight"); 476 var scm = options.hasOwnProperty("startCharMap"); 477 if (sv && cmf && iw && ih && scm && options["charMapFile"]) { 478 var cmft = options["charMapFile"]; 479 var cmf_tp = this._filePath + cmft; 480 481 labelAtlas.setProperty(options["stringValue"], cmf_tp, options["itemWidth"], options["itemHeight"], options["startCharMap"]); 482 } 483 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 484 }, 485 486 setPropsForLayoutFromJsonDictionary: function (widget, options) { 487 this.setPropsForWidgetFromJsonDictionary(widget, options); 488 var containerWidget = widget; 489 if (!(containerWidget instanceof ccs.ScrollView) && !(containerWidget instanceof ccs.ListView)) { 490 containerWidget.setClippingEnabled(options["clipAble"]); 491 } 492 var panel = widget; 493 var backGroundScale9Enable = options["backGroundScale9Enable"]; 494 panel.setBackGroundImageScale9Enabled(backGroundScale9Enable); 495 var cr = options["bgColorR"]; 496 var cg = options["bgColorG"]; 497 var cb = options["bgColorB"]; 498 499 var scr = options["bgStartColorR"]; 500 var scg = options["bgStartColorG"]; 501 var scb = options["bgStartColorB"]; 502 503 var ecr = options["bgEndColorR"]; 504 var ecg = options["bgEndColorG"]; 505 var ecb = options["bgEndColorB"]; 506 507 var bgcv1 = options["vectorX"]; 508 var bgcv2 = options["vectorY"]; 509 panel.setBackGroundColorVector(cc.p(bgcv1, bgcv2)); 510 511 var co = options["bgColorOpacity"]; 512 513 var colorType = options["colorType"]; 514 panel.setBackGroundColorType(colorType); 515 panel.setBackGroundColor(cc.c3b(scr, scg, scb), cc.c3b(ecr, ecg, ecb)); 516 panel.setBackGroundColor(cc.c3b(cr, cg, cb)); 517 panel.setBackGroundColorOpacity(co); 518 519 var imageFileName = options["backGroundImage"]; 520 var imageFileName_tp = imageFileName ? this._filePath + imageFileName : null; 521 var useMergedTexture = options["useMergedTexture"]; 522 if (useMergedTexture) { 523 panel.setBackGroundImage(imageFileName, ccs.TextureResType.plist); 524 } 525 else { 526 panel.setBackGroundImage(imageFileName_tp); 527 } 528 if (backGroundScale9Enable) { 529 var cx = options["capInsetsX"]; 530 var cy = options["capInsetsY"]; 531 var cw = options["capInsetsWidth"]; 532 var ch = options["capInsetsHeight"]; 533 panel.setBackGroundImageCapInsets(cc.rect(cx, cy, cw, ch)); 534 } 535 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 536 }, 537 538 539 setPropsForScrollViewFromJsonDictionary: function (widget, options) { 540 this.setPropsForLayoutFromJsonDictionary(widget, options); 541 var scrollView = widget; 542 var innerWidth = options["innerWidth"]; 543 var innerHeight = options["innerHeight"]; 544 scrollView.setInnerContainerSize(cc.size(innerWidth, innerHeight)); 545 var direction = options["direction"]; 546 scrollView.setDirection(direction); 547 scrollView.setBounceEnabled(options["bounceEnable"]); 548 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 549 }, 550 551 setPropsForContainerWidgetFromJsonDictionary: function (widget, options) { 552 this.setPropsForWidgetFromJsonDictionary(widget, options); 553 var containerWidget = widget; 554 if (containerWidget instanceof ccs.ScrollView || 555 containerWidget instanceof ccs.ListView) { 556 containerWidget.setClippingEnabled(options["clipAble"]); 557 } 558 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 559 }, 560 561 setPropsForSliderFromJsonDictionary: function (widget, options) { 562 563 this.setPropsForWidgetFromJsonDictionary(widget, options); 564 var slider = widget; 565 566 var barTextureScale9Enable = options["barTextureScale9Enable"] || false; 567 slider.setScale9Enabled(barTextureScale9Enable); 568 var barLength = options["length"]; 569 var useMergedTexture = options["useMergedTexture"]; 570 var bt = options.hasOwnProperty("barFileName"); 571 if (bt) { 572 if (barTextureScale9Enable) { 573 var imageFileName = options["barFileName"]; 574 var imageFileName_tp = imageFileName ? this._filePath + imageFileName : null; 575 if (useMergedTexture) { 576 slider.loadBarTexture(imageFileName, ccs.TextureResType.plist); 577 } 578 else { 579 slider.loadBarTexture(imageFileName_tp); 580 } 581 slider.setSize(cc.size(barLength, slider.getContentSize().height)); 582 } 583 else { 584 var imageFileName = options["barFileName"]; 585 var imageFileName_tp = imageFileName ? this._filePath + imageFileName : null; 586 if (useMergedTexture) { 587 slider.loadBarTexture(imageFileName, ccs.TextureResType.plist); 588 } 589 else { 590 slider.loadBarTexture(imageFileName_tp); 591 } 592 } 593 } 594 595 var normalFileName = options["ballNormal"]; 596 var pressedFileName = options["ballPressed"]; 597 var disabledFileName = options["ballDisabled"]; 598 599 var normalFileName_tp = normalFileName ? this._filePath + normalFileName : null; 600 var pressedFileName_tp = pressedFileName ? this._filePath + pressedFileName : null; 601 var disabledFileName_tp = disabledFileName ? this._filePath + disabledFileName : null; 602 if (useMergedTexture) { 603 slider.loadSlidBallTextures(normalFileName, pressedFileName, disabledFileName, ccs.TextureResType.plist); 604 } 605 else { 606 slider.loadSlidBallTextures(normalFileName_tp, pressedFileName_tp, disabledFileName_tp); 607 } 608 slider.setPercent(options["percent"]); 609 610 var imageFileName = options["progressBarFileName"]; 611 var imageFileName_tp = imageFileName ? this._filePath + imageFileName : null; 612 if (useMergedTexture) { 613 slider.loadProgressBarTexture(imageFileName, ccs.TextureResType.plist); 614 } 615 else { 616 slider.loadProgressBarTexture(imageFileName_tp); 617 } 618 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 619 }, 620 621 setPropsForTextAreaFromJsonDictionary: function (widget, options) { 622 this.setPropsForWidgetFromJsonDictionary(widget, options); 623 var textArea = widget; 624 textArea.setText(options["text"]); 625 if (options.hasOwnProperty("fontSize")) { 626 textArea.setFontSize(options["fontSize"]); 627 } 628 var cr = options["colorR"] 629 var cg = options["colorG"]; 630 var cb = options["colorB"]; 631 textArea.setColor(cc.c3b(cr, cg, cb)); 632 textArea.setFontName(options["fontName"]); 633 if (options.hasOwnProperty("areaWidth") && options.hasOwnProperty("areaHeight")) { 634 var size = cc.size(options["areaWidth"], options["areaHeight"]); 635 textArea.setTextAreaSize(size); 636 } 637 if (options.hasOwnProperty("hAlignment")) { 638 textArea.setTextHorizontalAlignment(options["hAlignment"]); 639 } 640 if (options.hasOwnProperty("vAlignment")) { 641 textArea.setTextVerticalAlignment(options["vAlignment"]); 642 } 643 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 644 }, 645 646 setPropsForTextButtonFromJsonDictionary: function (widget, options) { 647 this.setPropsForButtonFromJsonDictionary(widget, options); 648 649 var textButton = widget; 650 textButton.setTitleText(options["text"] || ""); 651 var cri = options.hasOwnProperty("textColorR") ? options["textColorR"] : 255; 652 var cgi = options.hasOwnProperty("textColorG") ? options["textColorG"] : 255; 653 var cbi = options.hasOwnProperty("textColorB") ? options["textColorB"] : 255; 654 textButton.setTitleColor(cc.c3b(cri, cgi, cbi)); 655 if (options.hasOwnProperty("fontSize")) { 656 textButton.setTitleFontSize(options["fontSize"]); 657 } 658 if (options.hasOwnProperty("fontName")) { 659 textButton.setTitleFontName(options["fontName"]); 660 } 661 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 662 }, 663 664 setPropsForTextFieldFromJsonDictionary: function (widget, options) { 665 this.setPropsForWidgetFromJsonDictionary(widget, options); 666 var textField = widget; 667 if (options.hasOwnProperty("placeHolder")) { 668 textField.setPlaceHolder(options["placeHolder"]); 669 } 670 textField.setText(options["text"]); 671 if (options.hasOwnProperty("fontSize")) { 672 textField.setFontSize(options["fontSize"]); 673 } 674 if (options.hasOwnProperty("fontName")) { 675 textField.setFontName(options["fontName"]); 676 } 677 if (options.hasOwnProperty("touchSizeWidth") && options.hasOwnProperty("touchSizeHeight")) { 678 textField.setTouchSize(cc.size(options["touchSizeWidth"], options["touchSizeHeight"])); 679 } 680 681 var dw = options["width"]; 682 var dh = options["height"]; 683 if (dw > 0.0 || dh > 0.0) { 684 //textField.setSize(CCSizeMake(dw, dh)); 685 } 686 var maxLengthEnable = options["maxLengthEnable"]; 687 textField.setMaxLengthEnabled(maxLengthEnable); 688 689 if (maxLengthEnable) { 690 var maxLength = options["maxLength"]; 691 textField.setMaxLength(maxLength); 692 } 693 var passwordEnable = options["passwordEnable"]; 694 textField.setPasswordEnabled(passwordEnable); 695 if (passwordEnable) { 696 textField.setPasswordStyleText(options["passwordStyleText"]); 697 } 698 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 699 }, 700 701 setPropsForLoadingBarFromJsonDictionary: function (widget, options) { 702 703 this.setPropsForWidgetFromJsonDictionary(widget, options); 704 var loadingBar = widget; 705 var useMergedTexture = options["useMergedTexture"]; 706 var imageFileName = options["texture"]; 707 var imageFileName_tp = imageFileName ? this._filePath + imageFileName : null; 708 if (useMergedTexture) { 709 loadingBar.loadTexture(imageFileName, ccs.TextureResType.plist); 710 } 711 else { 712 loadingBar.loadTexture(imageFileName_tp); 713 } 714 loadingBar.setDirection(options["direction"]); 715 loadingBar.setPercent(options["percent"]); 716 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 717 }, 718 719 setPropsForListViewFromJsonDictionary: function (widget, options) { 720 this.setPropsForLayoutFromJsonDictionary(widget, options); 721 }, 722 723 setPropsForPageViewFromJsonDictionary: function (widget, options) { 724 this.setPropsForLayoutFromJsonDictionary(widget, options); 725 }, 726 727 setPropsForLabelBMFontFromJsonDictionary: function (widget, options) { 728 this.setPropsForWidgetFromJsonDictionary(widget, options); 729 var labelBMFont = widget; 730 var cmft = options["fileName"]; 731 var cmf_tp = this._filePath + cmft; 732 labelBMFont.setFntFile(cmf_tp); 733 var text = options["text"]; 734 labelBMFont.setText(text); 735 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 736 } 737 }); 738 739 ccs.WidgetPropertiesReader0300 = ccs.WidgetPropertiesReader.extend({ 740 createWidget: function (jsonDict, fullPath, fileName) { 741 this._filePath = fullPath; 742 var textures = jsonDict["textures"]; 743 for (var i = 0; i < textures.length; i++) { 744 var file = textures[i]; 745 var tp = fullPath; 746 tp += file; 747 cc.SpriteFrameCache.getInstance().addSpriteFrames(tp); 748 } 749 var fileDesignWidth = jsonDict["designWidth"]; 750 var fileDesignHeight = jsonDict["designHeight"]; 751 if (fileDesignWidth <= 0 || fileDesignHeight <= 0) { 752 cc.log("Read design size error!"); 753 var winSize = cc.Director.getInstance().getWinSize(); 754 ccs.GUIReader.getInstance().storeFileDesignSize(fileName, winSize); 755 } 756 else { 757 ccs.GUIReader.getInstance().storeFileDesignSize(fileName, cc.size(fileDesignWidth, fileDesignHeight)); 758 } 759 var widgetTree = jsonDict["widgetTree"]; 760 var widget = this.widgetFromJsonDictionary(widgetTree); 761 762 var size = widget.getContentSize(); 763 if (size.width == 0 && size.height == 0) { 764 widget.setSize(cc.size(fileDesignWidth, fileDesignHeight)); 765 } 766 767 var actions = jsonDict["animation"]; 768 var rootWidget = widget; 769 ccs.ActionManager.getInstance().initWithDictionary(fileName, actions, rootWidget); 770 771 widgetTree = null; 772 actions = null; 773 return widget; 774 }, 775 widgetFromJsonDictionary: function (data) { 776 var widget = null; 777 var classname = data["classname"]; 778 var uiOptions = data["options"]; 779 if (classname == "Button") { 780 widget = ccs.Button.create(); 781 this.setPropsForButtonFromJsonDictionary(widget, uiOptions); 782 } 783 else if (classname == "CheckBox") { 784 widget = ccs.CheckBox.create(); 785 this.setPropsForCheckBoxFromJsonDictionary(widget, uiOptions); 786 } 787 else if (classname == "Label") { 788 widget = ccs.Label.create(); 789 this.setPropsForLabelFromJsonDictionary(widget, uiOptions); 790 } 791 else if (classname == "LabelAtlas") { 792 widget = ccs.LabelAtlas.create(); 793 this.setPropsForLabelAtlasFromJsonDictionary(widget, uiOptions); 794 } 795 else if (classname == "LoadingBar") { 796 widget = ccs.LoadingBar.create(); 797 this.setPropsForLoadingBarFromJsonDictionary(widget, uiOptions); 798 } else if (classname == "ScrollView") { 799 widget = ccs.ScrollView.create(); 800 this.setPropsForScrollViewFromJsonDictionary(widget, uiOptions); 801 } 802 else if (classname == "TextArea") { 803 widget = ccs.Label.create(); 804 this.setPropsForLabelFromJsonDictionary(widget, uiOptions); 805 } 806 else if (classname == "TextButton") { 807 widget = ccs.Button.create(); 808 this.setPropsForButtonFromJsonDictionary(widget, uiOptions); 809 } 810 else if (classname == "TextField") { 811 widget = ccs.TextField.create(); 812 this.setPropsForTextFieldFromJsonDictionary(widget, uiOptions); 813 } 814 else if (classname == "ImageView") { 815 widget = ccs.ImageView.create(); 816 this.setPropsForImageViewFromJsonDictionary(widget, uiOptions); 817 } 818 else if (classname == "Panel") { 819 widget = ccs.Layout.create(); 820 this.setPropsForLayoutFromJsonDictionary(widget, uiOptions); 821 } 822 else if (classname == "Slider") { 823 widget = ccs.Slider.create(); 824 this.setPropsForSliderFromJsonDictionary(widget, uiOptions); 825 } 826 else if (classname == "LabelBMFont") { 827 widget = ccs.LabelBMFont.create(); 828 this.setPropsForLabelBMFontFromJsonDictionary(widget, uiOptions); 829 } 830 else if (classname == "DragPanel") { 831 widget = ccs.ScrollView.create(); 832 this.setPropsForScrollViewFromJsonDictionary(widget, uiOptions); 833 } 834 else if (classname == "ListView") { 835 widget = ccs.ListView.create(); 836 this.setPropsForListViewFromJsonDictionary(widget, uiOptions); 837 } 838 else if (classname == "PageView") { 839 widget = ccs.PageView.create(); 840 this.setPropsForPageViewFromJsonDictionary(widget, uiOptions); 841 } 842 var children = data["children"]; 843 for (var i = 0; i < children.length; i++) { 844 var subData = children[i]; 845 var child = this.widgetFromJsonDictionary(subData); 846 if (child) { 847 if (widget instanceof ccs.PageView && child instanceof ccs.Layout) { 848 widget.addPage(child); 849 } else if (widget instanceof ccs.ListView) { 850 widget.pushBackCustomItem(child); 851 } else { 852 widget.addChild(child); 853 } 854 } 855 subData = null; 856 } 857 858 uiOptions = null; 859 return widget; 860 }, 861 862 863 setPropsForWidgetFromJsonDictionary: function (widget, options) { 864 if (options.hasOwnProperty("ignoreSize")) { 865 widget.ignoreContentAdaptWithSize(options["ignoreSize"]); 866 } 867 widget.setSizeType(options["sizeType"]); 868 widget.setPositionType(options["positionType"]); 869 870 widget.setSizePercent(cc.p(options["sizePercentX"], options["sizePercentY"])); 871 widget.setPositionPercent(cc.p(options["positionPercentX"], options["positionPercentY"])); 872 873 var w = options["width"]; 874 var h = options["height"]; 875 widget.setSize(cc.size(w, h)); 876 877 widget.setTag(options["tag"]); 878 widget.setActionTag(options["actiontag"]); 879 widget.setTouchEnabled(options["touchAble"]); 880 var name = options["name"]; 881 var widgetName = name ? name : "default"; 882 widget.setName(widgetName); 883 var x = options["x"]; 884 var y = options["y"]; 885 widget.setPosition(cc.p(x, y)); 886 if (options.hasOwnProperty("scaleX")) { 887 widget.setScaleX(options["scaleX"]); 888 } 889 if (options.hasOwnProperty("scaleY")) { 890 widget.setScaleY(options["scaleY"]); 891 } 892 if (options.hasOwnProperty("rotation")) { 893 widget.setRotation(options["rotation"]); 894 } 895 if (options.hasOwnProperty("visible")) { 896 widget.setVisible(options["visible"]); 897 } 898 899 widget.setZOrder(options["ZOrder"]); 900 var layoutParameterDic = options["layoutParameter"]; 901 if (layoutParameterDic) { 902 var paramType = layoutParameterDic["type"]; 903 var parameter; 904 switch (paramType) { 905 case 0: 906 break; 907 case 1: 908 parameter = ccs.LinearLayoutParameter.create(); 909 var gravity = layoutParameterDic["gravity"]; 910 parameter.setGravity(gravity); 911 break; 912 case 2: 913 parameter = ccs.RelativeLayoutParameter.create(); 914 var relativeName = layoutParameterDic["relativeName"]; 915 parameter.setRelativeName(relativeName); 916 var relativeToName = layoutParameterDic["relativeToName"]; 917 parameter.setRelativeToWidgetName(relativeToName); 918 parameter.setAlign(layoutParameterDic["align"]); 919 break; 920 default: 921 break; 922 } 923 var mgl = layoutParameterDic["marginLeft"]; 924 var mgt = layoutParameterDic["marginTop"]; 925 var mgr = layoutParameterDic["marginRight"]; 926 var mgb = layoutParameterDic["marginDown"]; 927 parameter.setMargin(new ccs.Margin(mgl, mgt, mgr, mgb)); 928 widget.setLayoutParameter(parameter); 929 } 930 }, 931 932 setColorPropsForWidgetFromJsonDictionary: function (widget, options) { 933 if (options.hasOwnProperty("opacity")) { 934 widget.setOpacity(options["opacity"]); 935 } 936 var colorR = options.hasOwnProperty("colorR") ? options["colorR"] : 255; 937 var colorG = options.hasOwnProperty("colorG") ? options["colorG"] : 255; 938 var colorB = options.hasOwnProperty("colorB") ? options["colorB"] : 255; 939 widget.setColor(cc.c3b(colorR, colorG, colorB)); 940 var apx = options.hasOwnProperty("anchorPointX") ? options["anchorPointX"] : ((widget.getWidgetType() == ccs.WidgetType.widget) ? 0.5 : 0); 941 var apy = options.hasOwnProperty("anchorPointY") ? options["anchorPointY"] : ((widget.getWidgetType() == ccs.WidgetType.widget) ? 0.5 : 0); 942 widget.setAnchorPoint(apx, apy); 943 var flipX = options["flipX"]; 944 var flipY = options["flipY"]; 945 widget.setFlippedX(flipX); 946 widget.setFlippedY(flipY); 947 }, 948 949 setPropsForButtonFromJsonDictionary: function (widget, options) { 950 951 952 this.setPropsForWidgetFromJsonDictionary(widget, options); 953 var button = widget; 954 var scale9Enable = options["scale9Enable"]; 955 button.setScale9Enabled(scale9Enable); 956 957 var normalDic = options["normalData"]; 958 var normalType = normalDic["resourceType"]; 959 switch (normalType) { 960 case 0: 961 var normalFileName = normalDic["path"]; 962 var normalFileName_tp = normalFileName ? this._filePath + normalFileName : null; 963 button.loadTextureNormal(normalFileName_tp); 964 break; 965 case 1: 966 var normalFileName = normalDic["path"]; 967 button.loadTextureNormal(normalFileName, ccs.TextureResType.plist); 968 break; 969 default: 970 break; 971 } 972 normalDic = null; 973 var pressedDic = options["pressedData"]; 974 var pressedType = pressedDic["resourceType"]; 975 switch (pressedType) { 976 case 0: 977 var pressedFileName = pressedDic["path"]; 978 var pressedFileName_tp = pressedFileName ? this._filePath + pressedFileName : null; 979 button.loadTexturePressed(pressedFileName_tp); 980 break; 981 case 1: 982 var pressedFileName = pressedDic["path"]; 983 button.loadTexturePressed(pressedFileName, ccs.TextureResType.plist); 984 break; 985 default: 986 break; 987 } 988 pressedDic = null; 989 var disabledDic = options["disabledData"]; 990 var disabledType = disabledDic["resourceType"]; 991 switch (disabledType) { 992 case 0: 993 var disabledFileName = disabledDic["path"]; 994 var disabledFileName_tp = disabledFileName ? this._filePath + disabledFileName : null; 995 button.loadTextureDisabled(disabledFileName_tp); 996 break; 997 case 1: 998 var disabledFileName = disabledDic["path"]; 999 button.loadTextureDisabled(disabledFileName, ccs.TextureResType.plist); 1000 break; 1001 default: 1002 break; 1003 } 1004 disabledDic = null; 1005 if (scale9Enable) { 1006 var cx = options["capInsetsX"]; 1007 var cy = options["capInsetsY"]; 1008 var cw = options["capInsetsWidth"]; 1009 var ch = options["capInsetsHeight"]; 1010 1011 button.setCapInsets(cc.rect(cx, cy, cw, ch)); 1012 if (options.hasOwnProperty("scale9Width") && options.hasOwnProperty("scale9Height")) { 1013 var swf = options["scale9Width"]; 1014 var shf = options["scale9Height"]; 1015 button.setSize(cc.size(swf, shf)); 1016 } 1017 } 1018 if (options.hasOwnProperty("text")) { 1019 var text = options["text"] || ""; 1020 if (text) 1021 button.setTitleText(text); 1022 } 1023 if (options.hasOwnProperty("fontSize")) { 1024 button.setTitleFontSize(options["fontSize"]); 1025 } 1026 if (options.hasOwnProperty("fontName")) { 1027 button.setTitleFontName(options["fontName"]); 1028 } 1029 var cr = options.hasOwnProperty("textColorR") ? options["textColorR"] : 255; 1030 var cg = options.hasOwnProperty("textColorG") ? options["textColorG"] : 255; 1031 var cb = options.hasOwnProperty("textColorB") ? options["textColorB"] : 255; 1032 var tc = cc.c3b(cr, cg, cb); 1033 button.setTitleColor(tc); 1034 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1035 }, 1036 1037 setPropsForCheckBoxFromJsonDictionary: function (widget, options) { 1038 this.setPropsForWidgetFromJsonDictionary(widget, options); 1039 var checkBox = widget; 1040 var backGroundDic = options["backGroundBoxData"]; 1041 var backGroundType = backGroundDic["resourceType"]; 1042 switch (backGroundType) { 1043 case 0: 1044 var backGroundFileName = backGroundDic["path"]; 1045 var backGroundFileName_tp = backGroundFileName ? this._filePath + backGroundFileName : null; 1046 checkBox.loadTextureBackGround(backGroundFileName_tp); 1047 break; 1048 case 1: 1049 var backGroundFileName = backGroundDic["path"]; 1050 checkBox.loadTextureBackGround(backGroundFileName, ccs.TextureResType.plist); 1051 break; 1052 default: 1053 break; 1054 } 1055 backGroundDic = null; 1056 var backGroundSelectedDic = options["backGroundBoxSelectedData"]; 1057 var backGroundSelectedType = backGroundSelectedDic["resourceType"]; 1058 switch (backGroundSelectedType) { 1059 case 0: 1060 var backGroundSelectedFileName = backGroundSelectedDic["path"]; 1061 var backGroundSelectedFileName_tp = backGroundSelectedFileName ? this._filePath + backGroundSelectedFileName : null; 1062 checkBox.loadTextureBackGroundSelected(backGroundSelectedFileName_tp); 1063 break; 1064 case 1: 1065 var backGroundSelectedFileName = backGroundSelectedDic["path"]; 1066 checkBox.loadTextureBackGroundSelected(backGroundSelectedFileName, ccs.TextureResType.plist); 1067 break; 1068 default: 1069 break; 1070 } 1071 backGroundSelectedDic = null; 1072 1073 var frontCrossDic = options["frontCrossData"]; 1074 var frontCrossType = frontCrossDic["resourceType"]; 1075 switch (frontCrossType) { 1076 case 0: 1077 var frontCrossFileName = frontCrossDic["path"]; 1078 var frontCrossFileName_tp = frontCrossFileName ? this._filePath + frontCrossFileName : null; 1079 checkBox.loadTextureFrontCross(frontCrossFileName_tp); 1080 break; 1081 case 1: 1082 var frontCrossFileName = frontCrossDic["path"]; 1083 checkBox.loadTextureFrontCross(frontCrossFileName, ccs.TextureResType.plist); 1084 break; 1085 default: 1086 break; 1087 } 1088 frontCrossDic = null; 1089 1090 var backGroundDisabledDic = options["backGroundBoxDisabledData"]; 1091 var backGroundDisabledType = backGroundDisabledDic["resourceType"]; 1092 switch (backGroundDisabledType) { 1093 case 0: 1094 var backGroundDisabledFileName = backGroundDisabledDic["path"]; 1095 var backGroundDisabledFileName_tp = backGroundDisabledFileName ? this._filePath + backGroundDisabledFileName : null; 1096 checkBox.loadTextureBackGroundDisabled(backGroundDisabledFileName_tp); 1097 break; 1098 case 1: 1099 var backGroundDisabledFileName = backGroundDisabledDic["path"]; 1100 checkBox.loadTextureBackGroundDisabled(backGroundDisabledFileName, ccs.TextureResType.plist); 1101 break; 1102 default: 1103 break; 1104 } 1105 backGroundDisabledDic = null; 1106 1107 var frontCrossDisabledDic = options["frontCrossDisabledData"]; 1108 var frontCrossDisabledType = frontCrossDisabledDic["resourceType"]; 1109 switch (frontCrossDisabledType) { 1110 case 0: 1111 var frontCrossDisabledFileName = options["path"]; 1112 var frontCrossDisabledFileName_tp = frontCrossDisabledFileName ? this._filePath + frontCrossDisabledFileName : null; 1113 checkBox.loadTextureFrontCrossDisabled(frontCrossDisabledFileName_tp); 1114 break; 1115 case 1: 1116 var frontCrossDisabledFileName = options["path"]; 1117 checkBox.loadTextureFrontCrossDisabled(frontCrossDisabledFileName, ccs.TextureResType.plist); 1118 break; 1119 default: 1120 break; 1121 } 1122 frontCrossDisabledDic = null; 1123 1124 var selectedState = options["selectedState"] || false; 1125 widget.setSelectedState(selectedState); 1126 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1127 }, 1128 1129 setPropsForImageViewFromJsonDictionary: function (widget, options) { 1130 this.setPropsForWidgetFromJsonDictionary(widget, options); 1131 1132 var imageView = widget; 1133 1134 var imageFileNameDic = options["fileNameData"]; 1135 var imageFileNameType = imageFileNameDic["resourceType"]; 1136 switch (imageFileNameType) { 1137 case 0: 1138 var tp_i = this._filePath; 1139 var imageFileName = imageFileNameDic["path"]; 1140 var imageFileName_tp = null; 1141 if (imageFileName) { 1142 imageFileName_tp = tp_i + imageFileName; 1143 imageView.loadTexture(imageFileName_tp); 1144 } 1145 break; 1146 case 1: 1147 var imageFileName = imageFileNameDic["path"]; 1148 imageView.loadTexture(imageFileName, ccs.TextureResType.plist); 1149 break; 1150 default: 1151 break; 1152 } 1153 imageFileNameDic = null; 1154 1155 var scale9Enable = options["scale9Enable"] || false; 1156 imageView.setScale9Enabled(scale9Enable); 1157 1158 if (scale9Enable) { 1159 if (options.hasOwnProperty("scale9Width") && options.hasOwnProperty("scale9Height")) { 1160 var swf = options["scale9Width"]; 1161 var shf = options["scale9Height"]; 1162 imageView.setSize(cc.size(swf, shf)); 1163 } 1164 1165 var cx = options["capInsetsX"]; 1166 var cy = options["capInsetsY"]; 1167 var cw = options["capInsetsWidth"]; 1168 var ch = options["capInsetsHeight"]; 1169 1170 imageView.setCapInsets(cc.rect(cx, cy, cw, ch)); 1171 1172 } 1173 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1174 }, 1175 1176 setPropsForLabelFromJsonDictionary: function (widget, options) { 1177 this.setPropsForWidgetFromJsonDictionary(widget, options); 1178 var label = widget; 1179 var touchScaleChangeAble = options["touchScaleEnable"]; 1180 label.setTouchScaleChangeAble(touchScaleChangeAble); 1181 var text = options["text"]; 1182 label.setText(text); 1183 if (options.hasOwnProperty("fontSize")) { 1184 label.setFontSize(options["fontSize"]); 1185 } 1186 if (options.hasOwnProperty("fontName")) { 1187 label.setFontName(options["fontName"]); 1188 } 1189 if (options.hasOwnProperty("areaWidth") && options.hasOwnProperty("areaHeight")) { 1190 var size = cc.size(options["areaWidth"], options["areaHeight"]); 1191 label.setTextAreaSize(size); 1192 } 1193 if (options.hasOwnProperty("hAlignment")) { 1194 label.setTextHorizontalAlignment(options["hAlignment"]); 1195 } 1196 if (options.hasOwnProperty("vAlignment")) { 1197 label.setTextVerticalAlignment(options["vAlignment"]); 1198 } 1199 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1200 }, 1201 1202 setPropsForLabelAtlasFromJsonDictionary: function (widget, options) { 1203 this.setPropsForWidgetFromJsonDictionary(widget, options); 1204 var labelAtlas = widget; 1205 var sv = options.hasOwnProperty("stringValue"); 1206 var cmf = options.hasOwnProperty("charMapFile"); 1207 var iw = options.hasOwnProperty("itemWidth"); 1208 var ih = options.hasOwnProperty("itemHeight"); 1209 var scm = options.hasOwnProperty("startCharMap"); 1210 if (sv && cmf && iw && ih && scm) { 1211 1212 var cmftDic = options["charMapFileData"]; 1213 var cmfType = cmftDic["resourceType"]; 1214 switch (cmfType) { 1215 case 0: 1216 var cmfPath = cmftDic["path"]; 1217 var cmf_tp = this._filePath + cmfPath; 1218 labelAtlas.setProperty(options["stringValue"], cmf_tp, options["itemWidth"], options["itemHeight"], options["startCharMap"]); 1219 break; 1220 case 1: 1221 cc.log("Wrong res type of LabelAtlas!"); 1222 break; 1223 default: 1224 break; 1225 } 1226 cmftDic = null; 1227 } 1228 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1229 }, 1230 1231 setPropsForLayoutFromJsonDictionary: function (widget, options) { 1232 this.setPropsForWidgetFromJsonDictionary(widget, options); 1233 var panel = widget; 1234 if (!(panel instanceof ccs.ScrollView) && !(panel instanceof ccs.ListView)) { 1235 panel.setClippingEnabled(options["clipAble"]); 1236 } 1237 var backGroundScale9Enable = options["backGroundScale9Enable"]; 1238 panel.setBackGroundImageScale9Enabled(backGroundScale9Enable); 1239 var cr = options["bgColorR"]; 1240 var cg = options["bgColorG"]; 1241 var cb = options["bgColorB"]; 1242 1243 var scr = options["bgStartColorR"]; 1244 var scg = options["bgStartColorG"] 1245 var scb = options["bgStartColorB"]; 1246 1247 var ecr = options["bgEndColorR"]; 1248 var ecg = options["bgEndColorG"]; 1249 var ecb = options["bgEndColorB"]; 1250 1251 var bgcv1 = options["vectorX"]; 1252 var bgcv2 = options["vectorY"]; 1253 panel.setBackGroundColorVector(cc.p(bgcv1, bgcv2)); 1254 1255 var co = options["bgColorOpacity"]; 1256 1257 var colorType = options["colorType"]; 1258 panel.setBackGroundColorType(colorType); 1259 panel.setBackGroundColor(cc.c3b(scr, scg, scb), cc.c3b(ecr, ecg, ecb)); 1260 panel.setBackGroundColor(cc.c3b(cr, cg, cb)); 1261 panel.setBackGroundColorOpacity(co); 1262 1263 1264 var imageFileNameDic = options["backGroundImageData"] || {}; 1265 var imageFileNameType = imageFileNameDic["resourceType"]; 1266 switch (imageFileNameType) { 1267 case 0: 1268 var imageFileName = imageFileNameDic["path"]; 1269 var imageFileName_tp = imageFileName ? this._filePath + imageFileName : null; 1270 panel.setBackGroundImage(imageFileName_tp); 1271 break; 1272 case 1: 1273 var imageFileName = imageFileNameDic["path"]; 1274 panel.setBackGroundImage(imageFileName, ccs.TextureResType.plist); 1275 break; 1276 default: 1277 break; 1278 } 1279 imageFileNameDic = null; 1280 1281 if (backGroundScale9Enable) { 1282 var cx = options["capInsetsX"]; 1283 var cy = options["capInsetsY"]; 1284 var cw = options["capInsetsWidth"]; 1285 var ch = options["capInsetsHeight"]; 1286 panel.setBackGroundImageCapInsets(cc.rect(cx, cy, cw, ch)); 1287 } 1288 panel.setLayoutType(options["layoutType"]); 1289 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1290 }, 1291 1292 1293 setPropsForScrollViewFromJsonDictionary: function (widget, options) { 1294 this.setPropsForLayoutFromJsonDictionary(widget, options); 1295 var scrollView = widget; 1296 var innerWidth = options["innerWidth"]; 1297 var innerHeight = options["innerHeight"]; 1298 scrollView.setInnerContainerSize(cc.size(innerWidth, innerHeight)); 1299 var direction = options["direction"]; 1300 scrollView.setDirection(direction); 1301 scrollView.setBounceEnabled(options["bounceEnable"]); 1302 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1303 }, 1304 1305 setPropsForSliderFromJsonDictionary: function (widget, options) { 1306 this.setPropsForWidgetFromJsonDictionary(widget, options); 1307 var slider = widget; 1308 1309 var barTextureScale9Enable = options["barTextureScale9Enable"] || false; 1310 slider.setScale9Enabled(barTextureScale9Enable); 1311 var barLength = options["length"]; 1312 var bt = options.hasOwnProperty("barFileName"); 1313 if (bt) { 1314 if (barTextureScale9Enable) { 1315 var imageFileNameDic = options["barFileNameData"]; 1316 var imageFileType = imageFileNameDic["resourceType"]; 1317 switch (imageFileType) { 1318 case 0: 1319 var imageFileName = imageFileNameDic["path"]; 1320 var imageFileName_tp = imageFileName ? this._filePath + imageFileName : null; 1321 slider.loadBarTexture(imageFileName_tp); 1322 break; 1323 case 1: 1324 var imageFileName = imageFileNameDic["path"]; 1325 slider.loadBarTexture(imageFileName, ccs.TextureResType.plist); 1326 break; 1327 default: 1328 break; 1329 } 1330 1331 slider.setSize(cc.size(barLength, slider.getContentSize().height)); 1332 imageFileNameDic = null; 1333 } 1334 else { 1335 var imageFileNameDic = options["barFileNameData"]; 1336 var imageFileType = imageFileNameDic["resourceType"]; 1337 switch (imageFileType) { 1338 case 0: 1339 var imageFileName = imageFileNameDic["path"]; 1340 var imageFileName_tp = imageFileName ? this._filePath + imageFileName : null; 1341 slider.loadBarTexture(imageFileName_tp); 1342 break; 1343 case 1: 1344 var imageFileName = imageFileNameDic["path"]; 1345 slider.loadBarTexture(imageFileName, ccs.TextureResType.plist); 1346 break; 1347 default: 1348 break; 1349 } 1350 imageFileNameDic = null; 1351 } 1352 } 1353 1354 var normalDic = options["ballNormalData"]; 1355 var normalType = normalDic["resourceType"]; 1356 switch (normalType) { 1357 case 0: 1358 var normalFileName = normalDic["path"]; 1359 var normalFileName_tp = normalFileName ? this._filePath + normalFileName : null; 1360 slider.loadSlidBallTextureNormal(normalFileName_tp); 1361 break; 1362 case 1: 1363 var normalFileName = normalDic["path"]; 1364 slider.loadSlidBallTextureNormal(normalFileName, ccs.TextureResType.plist); 1365 break; 1366 default: 1367 break; 1368 } 1369 normalDic = null; 1370 1371 var pressedDic = options["ballPressedData"]; 1372 var pressedType = pressedDic["resourceType"]; 1373 switch (pressedType) { 1374 case 0: 1375 var pressedFileName = pressedDic["path"]; 1376 var pressedFileName_tp = pressedFileName ? this._filePath + pressedFileName : null; 1377 slider.loadSlidBallTexturePressed(pressedFileName_tp); 1378 break; 1379 case 1: 1380 var pressedFileName = pressedDic["path"]; 1381 slider.loadSlidBallTexturePressed(pressedFileName, ccs.TextureResType.plist); 1382 break; 1383 default: 1384 break; 1385 } 1386 pressedDic = null; 1387 1388 var disabledDic = options["ballDisabledData"]; 1389 var disabledType = disabledDic["resourceType"]; 1390 switch (disabledType) { 1391 case 0: 1392 var disabledFileName = disabledDic["path"]; 1393 var disabledFileName_tp = disabledFileName ? this._filePath + disabledFileName : null; 1394 slider.loadSlidBallTextureDisabled(disabledFileName_tp); 1395 break; 1396 case 1: 1397 var disabledFileName = disabledDic["path"]; 1398 slider.loadSlidBallTextureDisabled(disabledFileName, ccs.TextureResType.plist); 1399 break; 1400 default: 1401 break; 1402 } 1403 disabledDic = null; 1404 1405 var progressBarDic = options["progressBarData"]; 1406 var progressBarType = progressBarDic["resourceType"]; 1407 switch (progressBarType) { 1408 case 0: 1409 var imageFileName = progressBarDic["path"]; 1410 var imageFileName_tp = imageFileName ? this._filePath + imageFileName : null; 1411 slider.loadProgressBarTexture(imageFileName_tp); 1412 break; 1413 case 1: 1414 var imageFileName = progressBarDic["path"]; 1415 slider.loadProgressBarTexture(imageFileName, ccs.TextureResType.plist); 1416 break; 1417 default: 1418 break; 1419 } 1420 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1421 1422 slider.setPercent(options["percent"]); 1423 }, 1424 1425 setPropsForTextAreaFromJsonDictionary: function (widget, options) { 1426 this.setPropsForWidgetFromJsonDictionary(widget, options); 1427 var textArea = widget; 1428 textArea.setText(options["text"]); 1429 if (options.hasOwnProperty("fontSize")) { 1430 textArea.setFontSize(options["fontSize"]); 1431 } 1432 var cr = options["colorR"] 1433 var cg = options["colorG"]; 1434 var cb = options["colorB"]; 1435 textArea.setColor(cc.c3b(cr, cg, cb)); 1436 textArea.setFontName(options["fontName"]); 1437 if (options.hasOwnProperty("areaWidth") && options.hasOwnProperty("areaHeight")) { 1438 var size = cc.size(options["areaWidth"], options["areaHeight"]); 1439 textArea.setTextAreaSize(size); 1440 } 1441 if (options.hasOwnProperty("hAlignment")) { 1442 textArea.setTextHorizontalAlignment(options["hAlignment"]); 1443 } 1444 if (options.hasOwnProperty("vAlignment")) { 1445 textArea.setTextVerticalAlignment(options["vAlignment"]); 1446 } 1447 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1448 }, 1449 1450 setPropsForTextButtonFromJsonDictionary: function (widget, options) { 1451 this.setPropsForButtonFromJsonDictionary(widget, options); 1452 1453 var textButton = widget; 1454 textButton.setTitleText(options["text"] || ""); 1455 var cri = options.hasOwnProperty("textColorR") ? options["textColorR"] : 255; 1456 var cgi = options.hasOwnProperty("textColorG") ? options["textColorG"] : 255; 1457 var cbi = options.hasOwnProperty("textColorB") ? options["textColorB"] : 255; 1458 textButton.setTitleColor(cc.c3b(cri, cgi, cbi)); 1459 if (options.hasOwnProperty("fontSize")) { 1460 textButton.setTitleFontSize(options["fontSize"]); 1461 } 1462 if (options.hasOwnProperty("fontName")) { 1463 textButton.setTitleFontName(options["fontName"]); 1464 } 1465 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1466 }, 1467 1468 setPropsForTextFieldFromJsonDictionary: function (widget, options) { 1469 this.setPropsForWidgetFromJsonDictionary(widget, options); 1470 var textField = widget; 1471 if (options.hasOwnProperty("placeHolder")) { 1472 textField.setPlaceHolder(options["placeHolder"]); 1473 } 1474 textField.setText(options["text"]); 1475 if (options.hasOwnProperty("fontSize")) { 1476 textField.setFontSize(options["fontSize"]); 1477 } 1478 if (options.hasOwnProperty("fontName")) { 1479 textField.setFontName(options["fontName"]); 1480 } 1481 if (options.hasOwnProperty("touchSizeWidth") && options.hasOwnProperty("touchSizeHeight")) { 1482 textField.setTouchSize(cc.size(options["touchSizeWidth"], options["touchSizeHeight"])); 1483 } 1484 1485 var dw = options["width"]; 1486 var dh = options["height"]; 1487 if (dw > 0.0 || dh > 0.0) { 1488 //textField.setSize(CCSizeMake(dw, dh)); 1489 } 1490 var maxLengthEnable = options["maxLengthEnable"]; 1491 textField.setMaxLengthEnabled(maxLengthEnable); 1492 1493 if (maxLengthEnable) { 1494 var maxLength = options["maxLength"]; 1495 textField.setMaxLength(maxLength); 1496 } 1497 var passwordEnable = options["passwordEnable"]; 1498 textField.setPasswordEnabled(passwordEnable); 1499 if (passwordEnable) { 1500 textField.setPasswordStyleText(options["passwordStyleText"]); 1501 } 1502 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1503 }, 1504 1505 setPropsForLoadingBarFromJsonDictionary: function (widget, options) { 1506 this.setPropsForWidgetFromJsonDictionary(widget, options); 1507 var loadingBar = widget; 1508 1509 var imageFileNameDic = options["textureData"]; 1510 var imageFileNameType = imageFileNameDic["resourceType"]; 1511 switch (imageFileNameType) { 1512 case 0: 1513 var tp_i = this._filePath; 1514 var imageFileName = imageFileNameDic["path"]; 1515 var imageFileName_tp = null; 1516 if (imageFileName) { 1517 imageFileName_tp = tp_i + imageFileName; 1518 loadingBar.loadTexture(imageFileName_tp); 1519 } 1520 break; 1521 case 1: 1522 var imageFileName = imageFileNameDic["path"]; 1523 loadingBar.loadTexture(imageFileName, ccs.TextureResType.plist); 1524 break; 1525 default: 1526 break; 1527 } 1528 imageFileNameDic = null; 1529 1530 var scale9Enable = options["scale9Enable"]; 1531 loadingBar.setScale9Enabled(scale9Enable); 1532 1533 if (scale9Enable) { 1534 var cx = options["capInsetsX"]; 1535 var cy = options["capInsetsY"]; 1536 var cw = options["capInsetsWidth"]; 1537 var ch = options["capInsetsHeight"]; 1538 1539 loadingBar.setCapInsets(cc.rect(cx, cy, cw, ch)); 1540 1541 var width = options["width"]; 1542 var height = options["height"]; 1543 loadingBar.setSize(cc.size(width, height)); 1544 } 1545 1546 loadingBar.setDirection(options["direction"]); 1547 loadingBar.setPercent(options["percent"]); 1548 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1549 1550 }, 1551 1552 setPropsForListViewFromJsonDictionary: function (widget, options) { 1553 this.setPropsForLayoutFromJsonDictionary(widget, options); 1554 var innerWidth = options["innerWidth"] || 0; 1555 var innerHeight = options["innerHeight"] || 0; 1556 widget.setInnerContainerSize(cc.size(innerWidth, innerHeight)); 1557 widget.setDirection(options["direction"] || 0); 1558 widget.setGravity(options["gravity"] || 0); 1559 widget.setItemsMargin(options["itemMargin"] || 0); 1560 }, 1561 1562 setPropsForPageViewFromJsonDictionary: function (widget, options) { 1563 this.setPropsForLayoutFromJsonDictionary(widget, options); 1564 }, 1565 1566 setPropsForLabelBMFontFromJsonDictionary: function (widget, options) { 1567 this.setPropsForWidgetFromJsonDictionary(widget, options); 1568 1569 var labelBMFont = widget; 1570 1571 var cmftDic = options["fileNameData"]; 1572 var cmfType = cmftDic["resourceType"]; 1573 switch (cmfType) { 1574 case 0: 1575 var cmfPath = cmftDic["path"]; 1576 var cmf_tp = this._filePath + cmfPath; 1577 labelBMFont.setFntFile(cmf_tp); 1578 break; 1579 case 1: 1580 cc.log("Wrong res type of LabelAtlas!"); 1581 break; 1582 default: 1583 break; 1584 } 1585 cmftDic = null; 1586 1587 var text = options["text"]; 1588 labelBMFont.setText(text); 1589 1590 this.setColorPropsForWidgetFromJsonDictionary(widget, options); 1591 } 1592 }); 1593 1594 ccs.GUIReader._instance = null; 1595 /** 1596 * returns a shared instance of the GUIReader 1597 * @function 1598 * @return {ccs.GUIReader} 1599 */ 1600 ccs.GUIReader.getInstance = function () { 1601 if (!this._instance) { 1602 this._instance = new ccs.GUIReader(); 1603 } 1604 return this._instance; 1605 }; 1606 1607 /** 1608 * purge instance 1609 */ 1610 ccs.GUIReader.purge = function(){ 1611 this._instance = null; 1612 };