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.bottom = model._margin.bottom;
144         this._margin.left = model._margin.left;
145         this._margin.right = model._margin.right;
146         this._margin.top = model._margin.top;
147     }
148 });
149 
150 /**
151  * allocates and initializes a LayoutParameter.
152  * @constructs
153  * @return {ccui.LayoutParameter}
154  * @example
155  * // example
156  * var uiLayoutParameter = ccui.LayoutParameter.create();
157  */
158 ccui.LayoutParameter.create = function () {
159     return new ccui.LayoutParameter();
160 };
161 
162 // Constants
163 //layout parameter type
164 ccui.LayoutParameter.NONE = 0;
165 ccui.LayoutParameter.LINEAR = 1;
166 ccui.LayoutParameter.RELATIVE = 2;
167 
168 /**
169  * Base class for ccui.LinearLayoutParameter
170  * @class
171  * @extends ccui.LayoutParameter
172  */
173 ccui.LinearLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.LinearLayoutParameter# */{
174     _linearGravity: null,
175     ctor: function () {
176         ccui.LayoutParameter.prototype.ctor.call(this);
177         this._linearGravity = ccui.LinearLayoutParameter.NONE;
178         this._layoutParameterType = ccui.LayoutParameter.LINEAR;
179     },
180 
181     /**
182      * Sets LinearGravity parameter for LayoutParameter.
183      * @param {Number} gravity
184      */
185     setGravity: function (gravity) {
186         this._linearGravity = gravity;
187     },
188 
189     /**
190      * Gets LinearGravity parameter for LayoutParameter.
191      * @returns {Number}
192      */
193     getGravity: function () {
194         return this._linearGravity;
195     },
196 
197     /**
198      * create clone instance.
199      * @returns {ccui.LinearLayoutParameter}
200      */
201     _createCloneInstance: function () {
202         return ccui.LinearLayoutParameter.create();
203     },
204 
205     /**
206      * copy properties
207      * @param {ccui.LinearLayoutParameter} model
208      */
209     _copyProperties: function (model) {
210         ccui.LayoutParameter.prototype._copyProperties.call(this, model);
211         if (model instanceof ccui.LinearLayoutParameter)
212             this.setGravity(model._linearGravity);
213     }
214 });
215 
216 /**
217  * allocates and initializes a LinearLayoutParameter.
218  * @constructs
219  * @return {ccui.LinearLayoutParameter}
220  * @example
221  * // example
222  * var uiLinearLayoutParameter = ccui.LinearLayoutParameter.create();
223  */
224 ccui.LinearLayoutParameter.create = function () {
225     return new ccui.LinearLayoutParameter();
226 };
227 
228 // Constants
229 //Linear layout parameter LinearGravity
230 ccui.LinearLayoutParameter.NONE = 0;
231 ccui.LinearLayoutParameter.LEFT = 1;
232 ccui.LinearLayoutParameter.TOP = 2;
233 ccui.LinearLayoutParameter.RIGHT = 3;
234 ccui.LinearLayoutParameter.BOTTOM = 4;
235 ccui.LinearLayoutParameter.CENTER_VERTICAL = 5;
236 ccui.LinearLayoutParameter.CENTER_HORIZONTAL = 6;
237 
238 /**
239  * Base class for ccui.RelativeLayoutParameter
240  * @class
241  * @extends ccui.LayoutParameter
242  */
243 ccui.RelativeLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.RelativeLayoutParameter# */{
244     _relativeAlign: null,
245     _relativeWidgetName: "",
246     _relativeLayoutName: "",
247     _put:false,
248     ctor: function () {
249         ccui.LayoutParameter.prototype.ctor.call(this);
250         this._relativeAlign = ccui.RelativeLayoutParameter.NONE;
251         this._relativeWidgetName = "";
252         this._relativeLayoutName = "";
253         this._put = false;
254         this._layoutParameterType = ccui.LayoutParameter.RELATIVE;
255     },
256 
257     /**
258      * Sets RelativeAlign parameter for LayoutParameter.
259      * @param {ccui.RELATIVE_ALIGN_*} align
260      */
261     setAlign: function (align) {
262         this._relativeAlign = align;
263     },
264 
265     /**
266      * Gets RelativeAlign parameter for LayoutParameter.
267      * @returns {ccui.RELATIVE_ALIGN_*}
268      */
269     getAlign: function () {
270         return this._relativeAlign;
271     },
272 
273     /**
274      * Sets a key for LayoutParameter. Witch widget named this is relative to.
275      * @param {String} name
276      */
277     setRelativeToWidgetName: function (name) {
278         this._relativeWidgetName = name;
279     },
280 
281     /**
282      * Gets the key of LayoutParameter. Witch widget named this is relative to.
283      * @returns {string}
284      */
285     getRelativeToWidgetName: function () {
286         return this._relativeWidgetName;
287     },
288 
289     /**
290      * Sets a name in Relative Layout for LayoutParameter.
291      * @param {String} name
292      */
293     setRelativeName: function (name) {
294         this._relativeLayoutName = name;
295     },
296 
297     /**
298      * Gets a name in Relative Layout of LayoutParameter.
299      * @returns {string}
300      */
301     getRelativeName: function () {
302         return this._relativeLayoutName;
303     },
304 
305     /**
306      * create clone instance.
307      * @returns {ccui.RelativeLayoutParameter}
308      */
309     _createCloneInstance:function(){
310         return ccui.RelativeLayoutParameter.create();
311     },
312 
313     /**
314      * copy properties
315      * @param {ccui.RelativeLayoutParameter} model
316      */
317     _copyProperties:function(model){
318         ccui.LayoutParameter.prototype._copyProperties.call(this, model);
319         if (model instanceof ccui.RelativeLayoutParameter) {
320             this.setAlign(model._relativeAlign);
321             this.setRelativeToWidgetName(model._relativeWidgetName);
322             this.setRelativeName(model._relativeLayoutName);
323         }
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;