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  * @property {Number}           left       - Left of margin
 32  * @property {Number}           top        - Top of margin
 33  * @property {Number}           right      - right of margin
 34  * @property {Number}           bottom     - bottom of margin
 35  */
 36 ccui.Margin = ccui.Class.extend(/** @lends ccui.Margin# */{
 37     left: 0,
 38     top: 0,
 39     right: 0,
 40     bottom: 0,
 41     /**
 42      * Constructor of ccui.Margin.
 43      * @param {Number|ccui.Margin} margin a margin or left
 44      * @param {Number} [top]
 45      * @param {Number} [right]
 46      * @param {Number} [bottom]
 47      */
 48     ctor: function (margin, top, right, bottom) {
 49         if (margin && top === undefined) {
 50             this.left = margin.left;
 51             this.top = margin.top;
 52             this.right = margin.right;
 53             this.bottom = margin.bottom;
 54         }
 55         if (bottom !== undefined) {
 56             this.left = margin;
 57             this.top = top;
 58             this.right = right;
 59             this.bottom = bottom;
 60         }
 61     },
 62     /**
 63      * Sets boundary of margin
 64      * @param {Number} l left
 65      * @param {Number} t top
 66      * @param {Number} r right
 67      * @param {Number} b bottom
 68      */
 69     setMargin: function (l, t, r, b) {
 70         this.left = l;
 71         this.top = t;
 72         this.right = r;
 73         this.bottom = b;
 74     },
 75     /**
 76      * Checks target whether equals itself.
 77      * @param {ccui.Margin} target
 78      * @returns {boolean}
 79      */
 80     equals: function (target) {
 81         return (this.left == target.left && this.top == target.top && this.right == target.right && this.bottom == target.bottom);
 82     }
 83 });
 84 
 85 /**
 86  * Gets a zero margin object
 87  * @function
 88  * @returns {ccui.Margin}
 89  */
 90 ccui.MarginZero = function(){
 91     return new ccui.Margin(0,0,0,0);
 92 };
 93 
 94 /**
 95  * Layout parameter contains a margin and layout parameter type. It uses for ccui.LayoutManager.
 96  * @class
 97  * @extends ccui.Class
 98  */
 99 ccui.LayoutParameter = ccui.Class.extend(/** @lends ccui.LayoutParameter# */{
100     _margin: null,
101     _layoutParameterType: null,
102 
103     /**
104      * The constructor of ccui.LayoutParameter.
105      * @function
106      */
107     ctor: function () {
108         this._margin = new ccui.Margin();
109         this._layoutParameterType = ccui.LayoutParameter.NONE;
110     },
111 
112     /**
113      * Sets Margin to LayoutParameter.
114      * @param {ccui.Margin} margin
115      */
116     setMargin: function (margin) {
117         if(typeof margin === 'object'){
118             this._margin.left = margin.left;
119             this._margin.top = margin.top;
120             this._margin.right = margin.right;
121             this._margin.bottom = margin.bottom;
122         }else{
123             this._margin.left = arguments[0];
124             this._margin.top = arguments[1];
125             this._margin.right = arguments[2];
126             this._margin.bottom = arguments[3];
127         }
128     },
129 
130     /**
131      * Gets Margin of LayoutParameter.
132      * @returns {ccui.Margin}
133      */
134     getMargin: function () {
135         return this._margin;
136     },
137 
138     /**
139      * Gets LayoutParameterType of LayoutParameter.
140      * @returns {Number}
141      */
142     getLayoutType: function () {
143         return this._layoutParameterType;
144     },
145 
146     /**
147      * Clones a ccui.LayoutParameter object from itself.
148      * @returns {ccui.LayoutParameter}
149      */
150     clone:function(){
151         var parameter = this._createCloneInstance();
152         parameter._copyProperties(this);
153         return parameter;
154     },
155 
156     /**
157      * create clone instance.
158      * @returns {ccui.LayoutParameter}
159      */
160     _createCloneInstance:function(){
161         return new ccui.LayoutParameter();
162     },
163 
164     /**
165      * copy properties from model.
166      * @param {ccui.LayoutParameter} model
167      */
168     _copyProperties:function(model){
169         this._margin.bottom = model._margin.bottom;
170         this._margin.left = model._margin.left;
171         this._margin.right = model._margin.right;
172         this._margin.top = model._margin.top;
173     }
174 });
175 
176 /**
177  * allocates and initializes a LayoutParameter.
178  * @constructs
179  * @return {ccui.LayoutParameter}
180  * @example
181  * // example
182  * var uiLayoutParameter = ccui.LayoutParameter.create();
183  */
184 ccui.LayoutParameter.create = function () {
185     return new ccui.LayoutParameter();
186 };
187 
188 // Constants
189 //layout parameter type
190 /**
191  * The none of ccui.LayoutParameter's type.
192  * @constant
193  * @type {number}
194  */
195 ccui.LayoutParameter.NONE = 0;
196 /**
197  * The linear of ccui.LayoutParameter's type.
198  * @constant
199  * @type {number}
200  */
201 ccui.LayoutParameter.LINEAR = 1;
202 /**
203  * The relative of ccui.LayoutParameter's type.
204  * @constant
205  * @type {number}
206  */
207 ccui.LayoutParameter.RELATIVE = 2;
208 
209 /**
210  * The linear of Layout parameter. its parameter type is ccui.LayoutParameter.LINEAR.
211  * @class
212  * @extends ccui.LayoutParameter
213  */
214 ccui.LinearLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.LinearLayoutParameter# */{
215     _linearGravity: null,
216     /**
217      * The constructor of ccui.LinearLayoutParameter.
218      * @function
219      */
220     ctor: function () {
221         ccui.LayoutParameter.prototype.ctor.call(this);
222         this._linearGravity = ccui.LinearLayoutParameter.NONE;
223         this._layoutParameterType = ccui.LayoutParameter.LINEAR;
224     },
225 
226     /**
227      * Sets LinearGravity to LayoutParameter.
228      * @param {Number} gravity
229      */
230     setGravity: function (gravity) {
231         this._linearGravity = gravity;
232     },
233 
234     /**
235      * Gets LinearGravity of LayoutParameter.
236      * @returns {Number}
237      */
238     getGravity: function () {
239         return this._linearGravity;
240     },
241 
242     _createCloneInstance: function () {
243         return ccui.LinearLayoutParameter.create();
244     },
245 
246     _copyProperties: function (model) {
247         ccui.LayoutParameter.prototype._copyProperties.call(this, model);
248         if (model instanceof ccui.LinearLayoutParameter)
249             this.setGravity(model._linearGravity);
250     }
251 });
252 
253 /**
254  * allocates and initializes a LinearLayoutParameter.
255  * @constructs
256  * @return {ccui.LinearLayoutParameter}
257  * @example
258  * // example
259  * var uiLinearLayoutParameter = ccui.LinearLayoutParameter.create();
260  */
261 ccui.LinearLayoutParameter.create = function () {
262     return new ccui.LinearLayoutParameter();
263 };
264 
265 // Constants
266 //Linear layout parameter LinearGravity
267 /**
268  * The none of ccui.LinearLayoutParameter's linear gravity.
269  * @constant
270  * @type {number}
271  */
272 ccui.LinearLayoutParameter.NONE = 0;
273 
274 /**
275  * The left of ccui.LinearLayoutParameter's linear gravity.
276  * @constant
277  * @type {number}
278  */
279 ccui.LinearLayoutParameter.LEFT = 1;
280 /**
281  * The top of ccui.LinearLayoutParameter's linear gravity.
282  * @constant
283  * @type {number}
284  */
285 ccui.LinearLayoutParameter.TOP = 2;
286 /**
287  * The right of ccui.LinearLayoutParameter's linear gravity.
288  * @constant
289  * @type {number}
290  */
291 ccui.LinearLayoutParameter.RIGHT = 3;
292 /**
293  * The bottom of ccui.LinearLayoutParameter's linear gravity.
294  * @constant
295  * @type {number}
296  */
297 ccui.LinearLayoutParameter.BOTTOM = 4;
298 /**
299  * The center vertical of ccui.LinearLayoutParameter's linear gravity.
300  * @constant
301  * @type {number}
302  */
303 ccui.LinearLayoutParameter.CENTER_VERTICAL = 5;
304 /**
305  * The center horizontal of ccui.LinearLayoutParameter's linear gravity.
306  * @constant
307  * @type {number}
308  */
309 ccui.LinearLayoutParameter.CENTER_HORIZONTAL = 6;
310 
311 /**
312  * The relative of layout parameter. Its layout parameter type is ccui.LayoutParameter.RELATIVE.
313  * @class
314  * @extends ccui.LayoutParameter
315  */
316 ccui.RelativeLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.RelativeLayoutParameter# */{
317     _relativeAlign: null,
318     _relativeWidgetName: "",
319     _relativeLayoutName: "",
320     _put:false,
321     /**
322      * The constructor of ccui.RelativeLayoutParameter
323      * @function
324      */
325     ctor: function () {
326         ccui.LayoutParameter.prototype.ctor.call(this);
327         this._relativeAlign = ccui.RelativeLayoutParameter.NONE;
328         this._relativeWidgetName = "";
329         this._relativeLayoutName = "";
330         this._put = false;
331         this._layoutParameterType = ccui.LayoutParameter.RELATIVE;
332     },
333 
334     /**
335      * Sets RelativeAlign parameter for LayoutParameter.
336      * @param {Number} align
337      */
338     setAlign: function (align) {
339         this._relativeAlign = align;
340     },
341 
342     /**
343      * Gets RelativeAlign parameter for LayoutParameter.
344      * @returns {Number}
345      */
346     getAlign: function () {
347         return this._relativeAlign;
348     },
349 
350     /**
351      * Sets a key for LayoutParameter. Witch widget named this is relative to.
352      * @param {String} name
353      */
354     setRelativeToWidgetName: function (name) {
355         this._relativeWidgetName = name;
356     },
357 
358     /**
359      * Gets the key of LayoutParameter. Witch widget named this is relative to.
360      * @returns {string}
361      */
362     getRelativeToWidgetName: function () {
363         return this._relativeWidgetName;
364     },
365 
366     /**
367      * Sets a name in Relative Layout for LayoutParameter.
368      * @param {String} name
369      */
370     setRelativeName: function (name) {
371         this._relativeLayoutName = name;
372     },
373 
374     /**
375      * Gets a name in Relative Layout of LayoutParameter.
376      * @returns {string}
377      */
378     getRelativeName: function () {
379         return this._relativeLayoutName;
380     },
381 
382     _createCloneInstance:function(){
383         return ccui.RelativeLayoutParameter.create();
384     },
385 
386     _copyProperties:function(model){
387         ccui.LayoutParameter.prototype._copyProperties.call(this, model);
388         if (model instanceof ccui.RelativeLayoutParameter) {
389             this.setAlign(model._relativeAlign);
390             this.setRelativeToWidgetName(model._relativeWidgetName);
391             this.setRelativeName(model._relativeLayoutName);
392         }
393     }
394 });
395 
396 /**
397  * Allocates and initializes a RelativeLayoutParameter.
398  * @function
399  * @deprecated since v3.0, please use new ccui.RelativeLayoutParameter() instead.
400  * @return {ccui.RelativeLayoutParameter}
401  * @example
402  * // example
403  * var uiRelativeLayoutParameter = ccui.RelativeLayoutParameter.create();
404  */
405 ccui.RelativeLayoutParameter.create = function () {
406     return new ccui.RelativeLayoutParameter();
407 };
408 
409 // Constants
410 //Relative layout parameter RelativeAlign
411 /**
412  * The none of ccui.RelativeLayoutParameter's relative align.
413  * @constant
414  * @type {number}
415  */
416 ccui.RelativeLayoutParameter.NONE = 0;
417 /**
418  * The parent's top left of ccui.RelativeLayoutParameter's relative align.
419  * @constant
420  * @type {number}
421  */
422 ccui.RelativeLayoutParameter.PARENT_TOP_LEFT = 1;
423 /**
424  * The parent's top center horizontal of ccui.RelativeLayoutParameter's relative align.
425  * @constant
426  * @type {number}
427  */
428 ccui.RelativeLayoutParameter.PARENT_TOP_CENTER_HORIZONTAL = 2;
429 /**
430  * The parent's top right of ccui.RelativeLayoutParameter's relative align.
431  * @constant
432  * @type {number}
433  */
434 ccui.RelativeLayoutParameter.PARENT_TOP_RIGHT = 3;
435 /**
436  * The parent's left center vertical of ccui.RelativeLayoutParameter's relative align.
437  * @constant
438  * @type {number}
439  */
440 ccui.RelativeLayoutParameter.PARENT_LEFT_CENTER_VERTICAL = 4;
441 
442 /**
443  * The center in parent of ccui.RelativeLayoutParameter's relative align.
444  * @constant
445  * @type {number}
446  */
447 ccui.RelativeLayoutParameter.CENTER_IN_PARENT = 5;
448 
449 /**
450  * The parent's right center vertical of ccui.RelativeLayoutParameter's relative align.
451  * @constant
452  * @type {number}
453  */
454 ccui.RelativeLayoutParameter.PARENT_RIGHT_CENTER_VERTICAL = 6;
455 /**
456  * The parent's left bottom of ccui.RelativeLayoutParameter's relative align.
457  * @constant
458  * @type {number}
459  */
460 ccui.RelativeLayoutParameter.PARENT_LEFT_BOTTOM = 7;
461 /**
462  * The parent's bottom center horizontal of ccui.RelativeLayoutParameter's relative align.
463  * @constant
464  * @type {number}
465  */
466 ccui.RelativeLayoutParameter.PARENT_BOTTOM_CENTER_HORIZONTAL = 8;
467 /**
468  * The parent's right bottom of ccui.RelativeLayoutParameter's relative align.
469  * @constant
470  * @type {number}
471  */
472 ccui.RelativeLayoutParameter.PARENT_RIGHT_BOTTOM = 9;
473 
474 /**
475  * The location above left align of ccui.RelativeLayoutParameter's relative align.
476  * @constant
477  * @type {number}
478  */
479 ccui.RelativeLayoutParameter.LOCATION_ABOVE_LEFTALIGN = 10;
480 /**
481  * The location above center of ccui.RelativeLayoutParameter's relative align.
482  * @constant
483  * @type {number}
484  */
485 ccui.RelativeLayoutParameter.LOCATION_ABOVE_CENTER = 11;
486 /**
487  * The location above right align of ccui.RelativeLayoutParameter's relative align.
488  * @constant
489  * @type {number}
490  */
491 ccui.RelativeLayoutParameter.LOCATION_ABOVE_RIGHTALIGN = 12;
492 /**
493  * The location left of top align of ccui.RelativeLayoutParameter's relative align.
494  * @constant
495  * @type {number}
496  */
497 ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_TOPALIGN = 13;
498 /**
499  * The location left of center of ccui.RelativeLayoutParameter's relative align.
500  * @constant
501  * @type {number}
502  */
503 ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_CENTER = 14;
504 /**
505  * The location left of bottom align of ccui.RelativeLayoutParameter's relative align.
506  * @constant
507  * @type {number}
508  */
509 ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_BOTTOMALIGN = 15;
510 /**
511  * The location right of top align of ccui.RelativeLayoutParameter's relative align.
512  * @constant
513  * @type {number}
514  */
515 ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_TOPALIGN = 16;
516 /**
517  * The location right of center of ccui.RelativeLayoutParameter's relative align.
518  * @constant
519  * @type {number}
520  */
521 ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_CENTER = 17;
522 /**
523  * The location right of bottom align of ccui.RelativeLayoutParameter's relative align.
524  * @constant
525  * @type {number}
526  */
527 ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_BOTTOMALIGN = 18;
528 /**
529  * The location below left align of ccui.RelativeLayoutParameter's relative align.
530  * @constant
531  * @type {number}
532  */
533 ccui.RelativeLayoutParameter.LOCATION_BELOW_LEFTALIGN = 19;
534 /**
535  * The location below center of ccui.RelativeLayoutParameter's relative align.
536  * @constant
537  * @type {number}
538  */
539 ccui.RelativeLayoutParameter.LOCATION_BELOW_CENTER = 20;
540 /**
541  * The location below right align of ccui.RelativeLayoutParameter's relative align.
542  * @constant
543  * @type {number}
544  */
545 ccui.RelativeLayoutParameter.LOCATION_BELOW_RIGHTALIGN = 21;
546 
547 /**
548  * @ignore
549  */
550 ccui.LINEAR_GRAVITY_NONE = 0;
551 ccui.LINEAR_GRAVITY_LEFT = 1;
552 ccui.LINEAR_GRAVITY_TOP = 2;
553 ccui.LINEAR_GRAVITY_RIGHT = 3;
554 ccui.LINEAR_GRAVITY_BOTTOM = 4;
555 ccui.LINEAR_GRAVITY_CENTER_VERTICAL = 5;
556 ccui.LINEAR_GRAVITY_CENTER_HORIZONTAL = 6;
557 
558 //RelativeAlign
559 ccui.RELATIVE_ALIGN_NONE = 0;
560 ccui.RELATIVE_ALIGN_PARENT_TOP_LEFT = 1;
561 ccui.RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL = 2;
562 ccui.RELATIVE_ALIGN_PARENT_TOP_RIGHT = 3;
563 ccui.RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL = 4;
564 ccui.RELATIVE_ALIGN_PARENT_CENTER = 5;
565 ccui.RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL = 6;
566 ccui.RELATIVE_ALIGN_PARENT_LEFT_BOTTOM = 7;
567 ccui.RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL = 8;
568 ccui.RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM = 9;
569 
570 ccui.RELATIVE_ALIGN_LOCATION_ABOVE_LEFT = 10;
571 ccui.RELATIVE_ALIGN_LOCATION_ABOVE_CENTER = 11;
572 ccui.RELATIVE_ALIGN_LOCATION_ABOVE_RIGHT = 12;
573 
574 ccui.RELATIVE_ALIGN_LOCATION_LEFT_TOP = 13;
575 ccui.RELATIVE_ALIGN_LOCATION_LEFT_CENTER = 14;
576 ccui.RELATIVE_ALIGN_LOCATION_LEFT_BOTTOM = 15;
577 
578 ccui.RELATIVE_ALIGN_LOCATION_RIGHT_TOP = 16;
579 ccui.RELATIVE_ALIGN_LOCATION_RIGHT_CENTER = 17;
580 ccui.RELATIVE_ALIGN_LOCATION_RIGHT_BOTTOM = 18;
581 
582 ccui.RELATIVE_ALIGN_LOCATION_BELOW_TOP = 19;
583 ccui.RELATIVE_ALIGN_LOCATION_BELOW_CENTER = 20;
584 ccui.RELATIVE_ALIGN_LOCATION_BELOW_BOTTOM = 21;