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 !== undefined && 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(cc.isObject(margin)){
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  */
181 ccui.LayoutParameter.create = function () {
182     return new ccui.LayoutParameter();
183 };
184 
185 // Constants
186 //layout parameter type
187 /**
188  * The none of ccui.LayoutParameter's type.
189  * @constant
190  * @type {number}
191  */
192 ccui.LayoutParameter.NONE = 0;
193 /**
194  * The linear of ccui.LayoutParameter's type.
195  * @constant
196  * @type {number}
197  */
198 ccui.LayoutParameter.LINEAR = 1;
199 /**
200  * The relative of ccui.LayoutParameter's type.
201  * @constant
202  * @type {number}
203  */
204 ccui.LayoutParameter.RELATIVE = 2;
205 
206 /**
207  * The linear of Layout parameter. its parameter type is ccui.LayoutParameter.LINEAR.
208  * @class
209  * @extends ccui.LayoutParameter
210  */
211 ccui.LinearLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.LinearLayoutParameter# */{
212     _linearGravity: null,
213     /**
214      * The constructor of ccui.LinearLayoutParameter.
215      * @function
216      */
217     ctor: function () {
218         ccui.LayoutParameter.prototype.ctor.call(this);
219         this._linearGravity = ccui.LinearLayoutParameter.NONE;
220         this._layoutParameterType = ccui.LayoutParameter.LINEAR;
221     },
222 
223     /**
224      * Sets LinearGravity to LayoutParameter.
225      * @param {Number} gravity
226      */
227     setGravity: function (gravity) {
228         this._linearGravity = gravity;
229     },
230 
231     /**
232      * Gets LinearGravity of LayoutParameter.
233      * @returns {Number}
234      */
235     getGravity: function () {
236         return this._linearGravity;
237     },
238 
239     _createCloneInstance: function () {
240         return new ccui.LinearLayoutParameter();
241     },
242 
243     _copyProperties: function (model) {
244         ccui.LayoutParameter.prototype._copyProperties.call(this, model);
245         if (model instanceof ccui.LinearLayoutParameter)
246             this.setGravity(model._linearGravity);
247     }
248 });
249 
250 /**
251  * allocates and initializes a LinearLayoutParameter.
252  * @constructs
253  * @return {ccui.LinearLayoutParameter}
254  * @deprecated since v3.0, please use new construction instead
255  */
256 ccui.LinearLayoutParameter.create = function () {
257     return new ccui.LinearLayoutParameter();
258 };
259 
260 // Constants
261 //Linear layout parameter LinearGravity
262 /**
263  * The none of ccui.LinearLayoutParameter's linear gravity.
264  * @constant
265  * @type {number}
266  */
267 ccui.LinearLayoutParameter.NONE = 0;
268 
269 /**
270  * The left of ccui.LinearLayoutParameter's linear gravity.
271  * @constant
272  * @type {number}
273  */
274 ccui.LinearLayoutParameter.LEFT = 1;
275 /**
276  * The top of ccui.LinearLayoutParameter's linear gravity.
277  * @constant
278  * @type {number}
279  */
280 ccui.LinearLayoutParameter.TOP = 2;
281 /**
282  * The right of ccui.LinearLayoutParameter's linear gravity.
283  * @constant
284  * @type {number}
285  */
286 ccui.LinearLayoutParameter.RIGHT = 3;
287 /**
288  * The bottom of ccui.LinearLayoutParameter's linear gravity.
289  * @constant
290  * @type {number}
291  */
292 ccui.LinearLayoutParameter.BOTTOM = 4;
293 /**
294  * The center vertical of ccui.LinearLayoutParameter's linear gravity.
295  * @constant
296  * @type {number}
297  */
298 ccui.LinearLayoutParameter.CENTER_VERTICAL = 5;
299 /**
300  * The center horizontal of ccui.LinearLayoutParameter's linear gravity.
301  * @constant
302  * @type {number}
303  */
304 ccui.LinearLayoutParameter.CENTER_HORIZONTAL = 6;
305 
306 /**
307  * The relative of layout parameter. Its layout parameter type is ccui.LayoutParameter.RELATIVE.
308  * @class
309  * @extends ccui.LayoutParameter
310  */
311 ccui.RelativeLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.RelativeLayoutParameter# */{
312     _relativeAlign: null,
313     _relativeWidgetName: "",
314     _relativeLayoutName: "",
315     _put:false,
316     /**
317      * The constructor of ccui.RelativeLayoutParameter
318      * @function
319      */
320     ctor: function () {
321         ccui.LayoutParameter.prototype.ctor.call(this);
322         this._relativeAlign = ccui.RelativeLayoutParameter.NONE;
323         this._relativeWidgetName = "";
324         this._relativeLayoutName = "";
325         this._put = false;
326         this._layoutParameterType = ccui.LayoutParameter.RELATIVE;
327     },
328 
329     /**
330      * Sets RelativeAlign parameter for LayoutParameter.
331      * @param {Number} align
332      */
333     setAlign: function (align) {
334         this._relativeAlign = align;
335     },
336 
337     /**
338      * Gets RelativeAlign parameter for LayoutParameter.
339      * @returns {Number}
340      */
341     getAlign: function () {
342         return this._relativeAlign;
343     },
344 
345     /**
346      * Sets a key for LayoutParameter. Witch widget named this is relative to.
347      * @param {String} name
348      */
349     setRelativeToWidgetName: function (name) {
350         this._relativeWidgetName = name;
351     },
352 
353     /**
354      * Gets the key of LayoutParameter. Witch widget named this is relative to.
355      * @returns {string}
356      */
357     getRelativeToWidgetName: function () {
358         return this._relativeWidgetName;
359     },
360 
361     /**
362      * Sets a name in Relative Layout for LayoutParameter.
363      * @param {String} name
364      */
365     setRelativeName: function (name) {
366         this._relativeLayoutName = name;
367     },
368 
369     /**
370      * Gets a name in Relative Layout of LayoutParameter.
371      * @returns {string}
372      */
373     getRelativeName: function () {
374         return this._relativeLayoutName;
375     },
376 
377     _createCloneInstance:function(){
378         return new ccui.RelativeLayoutParameter();
379     },
380 
381     _copyProperties:function(model){
382         ccui.LayoutParameter.prototype._copyProperties.call(this, model);
383         if (model instanceof ccui.RelativeLayoutParameter) {
384             this.setAlign(model._relativeAlign);
385             this.setRelativeToWidgetName(model._relativeWidgetName);
386             this.setRelativeName(model._relativeLayoutName);
387         }
388     }
389 });
390 
391 /**
392  * Allocates and initializes a RelativeLayoutParameter.
393  * @function
394  * @deprecated since v3.0, please use new ccui.RelativeLayoutParameter() instead.
395  * @return {ccui.RelativeLayoutParameter}
396  */
397 ccui.RelativeLayoutParameter.create = function () {
398     return new ccui.RelativeLayoutParameter();
399 };
400 
401 // Constants
402 //Relative layout parameter RelativeAlign
403 /**
404  * The none of ccui.RelativeLayoutParameter's relative align.
405  * @constant
406  * @type {number}
407  */
408 ccui.RelativeLayoutParameter.NONE = 0;
409 /**
410  * The parent's top left of ccui.RelativeLayoutParameter's relative align.
411  * @constant
412  * @type {number}
413  */
414 ccui.RelativeLayoutParameter.PARENT_TOP_LEFT = 1;
415 /**
416  * The parent's top center horizontal of ccui.RelativeLayoutParameter's relative align.
417  * @constant
418  * @type {number}
419  */
420 ccui.RelativeLayoutParameter.PARENT_TOP_CENTER_HORIZONTAL = 2;
421 /**
422  * The parent's top right of ccui.RelativeLayoutParameter's relative align.
423  * @constant
424  * @type {number}
425  */
426 ccui.RelativeLayoutParameter.PARENT_TOP_RIGHT = 3;
427 /**
428  * The parent's left center vertical of ccui.RelativeLayoutParameter's relative align.
429  * @constant
430  * @type {number}
431  */
432 ccui.RelativeLayoutParameter.PARENT_LEFT_CENTER_VERTICAL = 4;
433 
434 /**
435  * The center in parent of ccui.RelativeLayoutParameter's relative align.
436  * @constant
437  * @type {number}
438  */
439 ccui.RelativeLayoutParameter.CENTER_IN_PARENT = 5;
440 
441 /**
442  * The parent's right center vertical of ccui.RelativeLayoutParameter's relative align.
443  * @constant
444  * @type {number}
445  */
446 ccui.RelativeLayoutParameter.PARENT_RIGHT_CENTER_VERTICAL = 6;
447 /**
448  * The parent's left bottom of ccui.RelativeLayoutParameter's relative align.
449  * @constant
450  * @type {number}
451  */
452 ccui.RelativeLayoutParameter.PARENT_LEFT_BOTTOM = 7;
453 /**
454  * The parent's bottom center horizontal of ccui.RelativeLayoutParameter's relative align.
455  * @constant
456  * @type {number}
457  */
458 ccui.RelativeLayoutParameter.PARENT_BOTTOM_CENTER_HORIZONTAL = 8;
459 /**
460  * The parent's right bottom of ccui.RelativeLayoutParameter's relative align.
461  * @constant
462  * @type {number}
463  */
464 ccui.RelativeLayoutParameter.PARENT_RIGHT_BOTTOM = 9;
465 
466 /**
467  * The location above left align of ccui.RelativeLayoutParameter's relative align.
468  * @constant
469  * @type {number}
470  */
471 ccui.RelativeLayoutParameter.LOCATION_ABOVE_LEFTALIGN = 10;
472 /**
473  * The location above center of ccui.RelativeLayoutParameter's relative align.
474  * @constant
475  * @type {number}
476  */
477 ccui.RelativeLayoutParameter.LOCATION_ABOVE_CENTER = 11;
478 /**
479  * The location above right align of ccui.RelativeLayoutParameter's relative align.
480  * @constant
481  * @type {number}
482  */
483 ccui.RelativeLayoutParameter.LOCATION_ABOVE_RIGHTALIGN = 12;
484 /**
485  * The location left of top align of ccui.RelativeLayoutParameter's relative align.
486  * @constant
487  * @type {number}
488  */
489 ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_TOPALIGN = 13;
490 /**
491  * The location left of center of ccui.RelativeLayoutParameter's relative align.
492  * @constant
493  * @type {number}
494  */
495 ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_CENTER = 14;
496 /**
497  * The location left of bottom align of ccui.RelativeLayoutParameter's relative align.
498  * @constant
499  * @type {number}
500  */
501 ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_BOTTOMALIGN = 15;
502 /**
503  * The location right of top align of ccui.RelativeLayoutParameter's relative align.
504  * @constant
505  * @type {number}
506  */
507 ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_TOPALIGN = 16;
508 /**
509  * The location right of center of ccui.RelativeLayoutParameter's relative align.
510  * @constant
511  * @type {number}
512  */
513 ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_CENTER = 17;
514 /**
515  * The location right of bottom align of ccui.RelativeLayoutParameter's relative align.
516  * @constant
517  * @type {number}
518  */
519 ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_BOTTOMALIGN = 18;
520 /**
521  * The location below left align of ccui.RelativeLayoutParameter's relative align.
522  * @constant
523  * @type {number}
524  */
525 ccui.RelativeLayoutParameter.LOCATION_BELOW_LEFTALIGN = 19;
526 /**
527  * The location below center of ccui.RelativeLayoutParameter's relative align.
528  * @constant
529  * @type {number}
530  */
531 ccui.RelativeLayoutParameter.LOCATION_BELOW_CENTER = 20;
532 /**
533  * The location below right align of ccui.RelativeLayoutParameter's relative align.
534  * @constant
535  * @type {number}
536  */
537 ccui.RelativeLayoutParameter.LOCATION_BELOW_RIGHTALIGN = 21;
538 
539 /**
540  * @ignore
541  */
542 ccui.LINEAR_GRAVITY_NONE = 0;
543 ccui.LINEAR_GRAVITY_LEFT = 1;
544 ccui.LINEAR_GRAVITY_TOP = 2;
545 ccui.LINEAR_GRAVITY_RIGHT = 3;
546 ccui.LINEAR_GRAVITY_BOTTOM = 4;
547 ccui.LINEAR_GRAVITY_CENTER_VERTICAL = 5;
548 ccui.LINEAR_GRAVITY_CENTER_HORIZONTAL = 6;
549 
550 //RelativeAlign
551 ccui.RELATIVE_ALIGN_NONE = 0;
552 ccui.RELATIVE_ALIGN_PARENT_TOP_LEFT = 1;
553 ccui.RELATIVE_ALIGN_PARENT_TOP_CENTER_HORIZONTAL = 2;
554 ccui.RELATIVE_ALIGN_PARENT_TOP_RIGHT = 3;
555 ccui.RELATIVE_ALIGN_PARENT_LEFT_CENTER_VERTICAL = 4;
556 ccui.RELATIVE_ALIGN_PARENT_CENTER = 5;
557 ccui.RELATIVE_ALIGN_PARENT_RIGHT_CENTER_VERTICAL = 6;
558 ccui.RELATIVE_ALIGN_PARENT_LEFT_BOTTOM = 7;
559 ccui.RELATIVE_ALIGN_PARENT_BOTTOM_CENTER_HORIZONTAL = 8;
560 ccui.RELATIVE_ALIGN_PARENT_RIGHT_BOTTOM = 9;
561 
562 ccui.RELATIVE_ALIGN_LOCATION_ABOVE_LEFT = 10;
563 ccui.RELATIVE_ALIGN_LOCATION_ABOVE_CENTER = 11;
564 ccui.RELATIVE_ALIGN_LOCATION_ABOVE_RIGHT = 12;
565 
566 ccui.RELATIVE_ALIGN_LOCATION_LEFT_TOP = 13;
567 ccui.RELATIVE_ALIGN_LOCATION_LEFT_CENTER = 14;
568 ccui.RELATIVE_ALIGN_LOCATION_LEFT_BOTTOM = 15;
569 
570 ccui.RELATIVE_ALIGN_LOCATION_RIGHT_TOP = 16;
571 ccui.RELATIVE_ALIGN_LOCATION_RIGHT_CENTER = 17;
572 ccui.RELATIVE_ALIGN_LOCATION_RIGHT_BOTTOM = 18;
573 
574 ccui.RELATIVE_ALIGN_LOCATION_BELOW_TOP = 19;
575 ccui.RELATIVE_ALIGN_LOCATION_BELOW_CENTER = 20;
576 ccui.RELATIVE_ALIGN_LOCATION_BELOW_BOTTOM = 21;