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