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 /** 27 * Base class for ccui.Margin 28 * @class 29 * @extends ccui.Class 30 */ 31 ccui.Margin = ccui.Class.extend(/** @lends ccui.Margin# */{ 32 left: 0, 33 top: 0, 34 right: 0, 35 bottom: 0, 36 ctor: function (margin, top, right, bottom) { 37 if (margin && top === undefined) { 38 this.left = margin.left; 39 this.top = margin.top; 40 this.right = margin.right; 41 this.bottom = margin.bottom; 42 } 43 if (bottom !== undefined) { 44 this.left = margin; 45 this.top = top; 46 this.right = right; 47 this.bottom = bottom; 48 } 49 }, 50 /** 51 * set margin 52 * @param {Number} l 53 * @param {Number} t 54 * @param {Number} r 55 * @param {Number} b 56 */ 57 setMargin: function (l, t, r, b) { 58 this.left = l; 59 this.top = t; 60 this.right = r; 61 this.bottom = b; 62 }, 63 /** 64 * check is equals 65 * @param {ccui.Margin} target 66 * @returns {boolean} 67 */ 68 equals: function (target) { 69 return (this.left == target.left && this.top == target.top && this.right == target.right && this.bottom == target.bottom); 70 } 71 }); 72 73 ccui.MarginZero = function(){ 74 return new ccui.Margin(0,0,0,0); 75 }; 76 77 /** 78 * Layout parameter define 79 * @class 80 * @extends ccui.Class 81 */ 82 ccui.LayoutParameter = ccui.Class.extend(/** @lends ccui.LayoutParameter# */{ 83 _margin: null, 84 _layoutParameterType: null, 85 ctor: function () { 86 this._margin = new ccui.Margin(); 87 this._layoutParameterType = ccui.LayoutParameter.NONE; 88 }, 89 90 /** 91 * Sets Margin parameter for LayoutParameter. 92 * @param {ccui.Margin} margin 93 */ 94 setMargin: function (margin) { 95 if(typeof margin === 'object'){ 96 this._margin.left = margin.left; 97 this._margin.top = margin.top; 98 this._margin.right = margin.right; 99 this._margin.bottom = margin.bottom; 100 }else{ 101 this._margin.left = arguments[0]; 102 this._margin.top = arguments[1]; 103 this._margin.right = arguments[2]; 104 this._margin.bottom = arguments[3]; 105 } 106 }, 107 108 /** 109 * Gets Margin parameter of LayoutParameter. 110 * @returns {ccui.Margin} 111 */ 112 getMargin: function () { 113 return this._margin; 114 }, 115 116 /** 117 * Gets LayoutParameterType of LayoutParameter. 118 * @returns {ccui.UILayoutParameterType} 119 */ 120 getLayoutType: function () { 121 return this._layoutParameterType; 122 }, 123 124 clone:function(){ 125 var parameter = this.createCloneInstance(); 126 parameter.copyProperties(this); 127 return parameter; 128 }, 129 130 /** 131 * create clone instance. 132 * @returns {ccui.LayoutParameter} 133 */ 134 createCloneInstance:function(){ 135 return ccui.LayoutParameter.create(); 136 }, 137 138 /** 139 * copy properties 140 * @param {ccui.LayoutParameter} model 141 */ 142 copyProperties:function(model){ 143 this._margin = model._margin; 144 } 145 }); 146 147 /** 148 * allocates and initializes a LayoutParameter. 149 * @constructs 150 * @return {ccui.LayoutParameter} 151 * @example 152 * // example 153 * var uiLayoutParameter = ccui.LayoutParameter.create(); 154 */ 155 ccui.LayoutParameter.create = function () { 156 return new ccui.LayoutParameter(); 157 }; 158 159 // Constants 160 //layout parameter type 161 ccui.LayoutParameter.NONE = 0; 162 ccui.LayoutParameter.LINEAR = 1; 163 ccui.LayoutParameter.RELATIVE = 2; 164 165 /** 166 * Base class for ccui.LinearLayoutParameter 167 * @class 168 * @extends ccui.LayoutParameter 169 */ 170 ccui.LinearLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.LinearLayoutParameter# */{ 171 _linearGravity: null, 172 ctor: function () { 173 ccui.LayoutParameter.prototype.ctor.call(this); 174 this._linearGravity = ccui.LinearLayoutParameter.NONE; 175 this._layoutParameterType = ccui.LayoutParameter.LINEAR; 176 }, 177 178 /** 179 * Sets LinearGravity parameter for LayoutParameter. 180 * @param {Number} gravity 181 */ 182 setGravity: function (gravity) { 183 this._linearGravity = gravity; 184 }, 185 186 /** 187 * Gets LinearGravity parameter for LayoutParameter. 188 * @returns {Number} 189 */ 190 getGravity: function () { 191 return this._linearGravity; 192 }, 193 194 /** 195 * create clone instance. 196 * @returns {ccui.LinearLayoutParameter} 197 */ 198 createCloneInstance: function () { 199 return ccui.LinearLayoutParameter.create(); 200 }, 201 202 /** 203 * copy properties 204 * @param {ccui.LinearLayoutParameter} model 205 */ 206 copyProperties: function (model) { 207 ccui.LayoutParameter.prototype.copyProperties.call(this, model); 208 var parameter = model; 209 if(parameter){ 210 this.setAlign(parameter._relativeAlign); 211 this.setRelativeName(parameter._relativeLayoutName); 212 this.setRelativeToWidgetName(parameter._relativeWidgetName); 213 this.setGravity(model._linearGravity); 214 } 215 } 216 }); 217 218 /** 219 * allocates and initializes a LinearLayoutParameter. 220 * @constructs 221 * @return {ccui.LinearLayoutParameter} 222 * @example 223 * // example 224 * var uiLinearLayoutParameter = ccui.LinearLayoutParameter.create(); 225 */ 226 ccui.LinearLayoutParameter.create = function () { 227 return new ccui.LinearLayoutParameter(); 228 }; 229 230 // Constants 231 //Linear layout parameter LinearGravity 232 ccui.LinearLayoutParameter.NONE = 0; 233 ccui.LinearLayoutParameter.LEFT = 1; 234 ccui.LinearLayoutParameter.TOP = 2; 235 ccui.LinearLayoutParameter.RIGHT = 3; 236 ccui.LinearLayoutParameter.BOTTOM = 4; 237 ccui.LinearLayoutParameter.CENTER_VERTICAL = 5; 238 ccui.LinearLayoutParameter.CENTER_HORIZONTAL = 6; 239 240 /** 241 * Base class for ccui.RelativeLayoutParameter 242 * @class 243 * @extends ccui.LayoutParameter 244 */ 245 ccui.RelativeLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.RelativeLayoutParameter# */{ 246 _relativeAlign: null, 247 _relativeWidgetName: "", 248 _relativeLayoutName: "", 249 _put:false, 250 ctor: function () { 251 ccui.LayoutParameter.prototype.ctor.call(this); 252 this._relativeAlign = ccui.RelativeLayoutParameter.NONE; 253 this._relativeWidgetName = ""; 254 this._relativeLayoutName = ""; 255 this._put = false; 256 this._layoutParameterType = ccui.LayoutParameter.RELATIVE; 257 }, 258 259 /** 260 * Sets RelativeAlign parameter for LayoutParameter. 261 * @param {ccui.RELATIVE_ALIGN_*} align 262 */ 263 setAlign: function (align) { 264 this._relativeAlign = align; 265 }, 266 267 /** 268 * Gets RelativeAlign parameter for LayoutParameter. 269 * @returns {ccui.RELATIVE_ALIGN_*} 270 */ 271 getAlign: function () { 272 return this._relativeAlign; 273 }, 274 275 /** 276 * Sets a key for LayoutParameter. Witch widget named this is relative to. 277 * @param {String} name 278 */ 279 setRelativeToWidgetName: function (name) { 280 this._relativeWidgetName = name; 281 }, 282 283 /** 284 * Gets the key of LayoutParameter. Witch widget named this is relative to. 285 * @returns {string} 286 */ 287 getRelativeToWidgetName: function () { 288 return this._relativeWidgetName; 289 }, 290 291 /** 292 * Sets a name in Relative Layout for LayoutParameter. 293 * @param {String} name 294 */ 295 setRelativeName: function (name) { 296 this._relativeLayoutName = name; 297 }, 298 299 /** 300 * Gets a name in Relative Layout of LayoutParameter. 301 * @returns {string} 302 */ 303 getRelativeName: function () { 304 return this._relativeLayoutName; 305 }, 306 307 /** 308 * create clone instance. 309 * @returns {ccui.RelativeLayoutParameter} 310 */ 311 createCloneInstance:function(){ 312 return ccui.RelativeLayoutParameter.create(); //TODO 313 }, 314 315 /** 316 * copy properties 317 * @param {ccui.RelativeLayoutParameter} model 318 */ 319 copyProperties:function(model){ 320 ccui.LayoutParameter.prototype.copyProperties.call(this, model); 321 this.setAlign(model._relativeAlign); 322 this.setRelativeToWidgetName(model._relativeWidgetName); 323 this.setRelativeName(model._relativeLayoutName); 324 } 325 }); 326 327 /** 328 * allocates and initializes a RelativeLayoutParameter. 329 * @constructs 330 * @return {ccui.RelativeLayoutParameter} 331 * @example 332 * // example 333 * var uiRelativeLayoutParameter = ccui.RelativeLayoutParameter.create(); 334 */ 335 ccui.RelativeLayoutParameter.create = function () { 336 return new ccui.RelativeLayoutParameter(); 337 }; 338 339 // Constants 340 //Relative layout parameter RelativeAlign 341 ccui.RelativeLayoutParameter.NONE = 0; 342 ccui.RelativeLayoutParameter.PARENT_TOP_LEFT = 1; 343 ccui.RelativeLayoutParameter.PARENT_TOP_CENTER_HORIZONTAL = 2; 344 ccui.RelativeLayoutParameter.PARENT_TOP_RIGHT = 3; 345 ccui.RelativeLayoutParameter.PARENT_LEFT_CENTER_VERTICAL = 4; 346 347 ccui.RelativeLayoutParameter.CENTER_IN_PARENT = 5; 348 349 ccui.RelativeLayoutParameter.PARENT_RIGHT_CENTER_VERTICAL = 6; 350 ccui.RelativeLayoutParameter.PARENT_LEFT_BOTTOM = 7; 351 ccui.RelativeLayoutParameter.PARENT_BOTTOM_CENTER_HORIZONTAL = 8; 352 ccui.RelativeLayoutParameter.PARENT_RIGHT_BOTTOM = 9; 353 354 ccui.RelativeLayoutParameter.LOCATION_ABOVE_LEFTALIGN = 10; 355 ccui.RelativeLayoutParameter.LOCATION_ABOVE_CENTER = 11; 356 ccui.RelativeLayoutParameter.LOCATION_ABOVE_RIGHTALIGN = 12; 357 ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_TOPALIGN = 13; 358 ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_CENTER = 14; 359 ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_BOTTOMALIGN = 15; 360 ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_TOPALIGN = 16; 361 ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_CENTER = 17; 362 ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_BOTTOMALIGN = 18; 363 ccui.RelativeLayoutParameter.LOCATION_BELOW_LEFTALIGN = 19; 364 ccui.RelativeLayoutParameter.LOCATION_BELOW_CENTER = 20; 365 ccui.RelativeLayoutParameter.LOCATION_BELOW_RIGHTALIGN = 21; 366 367 /** 368 * @ignore 369 */ 370 ccui.LINEAR_GRAVITY_NONE = 0; 371 ccui.LINEAR_GRAVITY_LEFT = 1; 372 ccui.LINEAR_GRAVITY_TOP = 2; 373 ccui.LINEAR_GRAVITY_RIGHT = 3; 374 ccui.LINEAR_GRAVITY_BOTTOM = 4; 375 ccui.LINEAR_GRAVITY_CENTER_VERTICAL = 5; 376 ccui.LINEAR_GRAVITY_CENTER_HORIZONTAL = 6; 377 378 //RelativeAlign 379 ccui.RELATIVE_ALIGN_NONE = 0; 380 ccui.RELATIVE_ALIGN_PARENT_TOP_LEFT = 1; 381 ccui.RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL = 2; 382 ccui.RELATIVE_ALIGN_PARENT_TOP_RIGHT = 3; 383 ccui.RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL = 4; 384 ccui.RELATIVE_ALIGN_PARENT_CENTER = 5; 385 ccui.RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL = 6; 386 ccui.RELATIVE_ALIGN_PARENT_LEFT_BOTTOM = 7; 387 ccui.RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL = 8; 388 ccui.RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM = 9; 389 390 ccui.RELATIVE_ALIGN_LOCATION_ABOVE_LEFT = 10; 391 ccui.RELATIVE_ALIGN_LOCATION_ABOVE_CENTER = 11; 392 ccui.RELATIVE_ALIGN_LOCATION_ABOVE_RIGHT = 12; 393 394 ccui.RELATIVE_ALIGN_LOCATION_LEFT_TOP = 13; 395 ccui.RELATIVE_ALIGN_LOCATION_LEFT_CENTER = 14; 396 ccui.RELATIVE_ALIGN_LOCATION_LEFT_BOTTOM = 15; 397 398 ccui.RELATIVE_ALIGN_LOCATION_RIGHT_TOP = 16; 399 ccui.RELATIVE_ALIGN_LOCATION_RIGHT_CENTER = 17; 400 ccui.RELATIVE_ALIGN_LOCATION_RIGHT_BOTTOM = 18; 401 402 ccui.RELATIVE_ALIGN_LOCATION_BELOW_TOP = 19; 403 ccui.RELATIVE_ALIGN_LOCATION_BELOW_CENTER = 20; 404 ccui.RELATIVE_ALIGN_LOCATION_BELOW_BOTTOM = 21;