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.LayoutParameter
 28  * @class
 29  * @extends ccui.Class
 30  */
 31 ccui.LayoutParameter = ccui.Class.extend(/** @lends ccui.LayoutParameter# */{
 32     _margin: null,
 33     _layoutParameterType: null,
 34     ctor: function () {
 35         this._margin = new ccui.Margin();
 36         this._layoutParameterType = ccui.LayoutParameter.NONE;
 37     },
 38 
 39     /**
 40      * Sets Margin parameter for LayoutParameter.
 41      * @param {ccui.Margin} margin
 42      */
 43     setMargin: function (margin) {
 44         this._margin.left = margin.left;
 45         this._margin.top = margin.top;
 46         this._margin.right = margin.right;
 47         this._margin.bottom = margin.bottom;
 48     },
 49 
 50     /**
 51      * Gets Margin parameter of LayoutParameter.
 52      * @returns {ccui.Margin}
 53      */
 54     getMargin: function () {
 55         return this._margin;
 56     },
 57 
 58     /**
 59      * Gets LayoutParameterType of LayoutParameter.
 60      * @returns {ccui.UILayoutParameterType}
 61      */
 62     getLayoutType: function () {
 63         return this._layoutParameterType;
 64     },
 65 
 66     clone:function(){
 67         var parameter = this.createCloneInstance();
 68         parameter.copyProperties(this);
 69         return parameter;
 70     },
 71 
 72     /**
 73      * create clone instance.
 74      * @returns {ccui.LayoutParameter}
 75      */
 76     createCloneInstance:function(){
 77         return ccui.LayoutParameter.create();
 78     },
 79 
 80     /**
 81      * copy properties
 82      * @param {ccui.LayoutParameter} model
 83      */
 84     copyProperties:function(model){
 85         this._margin.left = model._margin.left;
 86         this._margin.top = model._margin.top;
 87         this._margin.right = model._margin.right;
 88         this._margin.bottom = model._margin.bottom;
 89     }
 90 });
 91 
 92 /**
 93  * allocates and initializes a LayoutParameter.
 94  * @constructs
 95  * @return {ccui.LayoutParameter}
 96  * @example
 97  * // example
 98  * var uiLayoutParameter = ccui.LayoutParameter.create();
 99  */
100 ccui.LayoutParameter.create = function () {
101     var parameter = new ccui.LayoutParameter();
102     return parameter;
103 };
104 
105 /**
106  * Base class for ccui.LinearLayoutParameter
107  * @class
108  * @extends ccui.LayoutParameter
109  */
110 ccui.LinearLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.LinearLayoutParameter# */{
111     _linearGravity: null,
112     ctor: function () {
113         ccui.LayoutParameter.prototype.ctor.call(this);
114         this._linearGravity = ccui.LINEAR_GRAVITY_NONE;
115         this._layoutParameterType = ccui.LayoutParameter.LINEAR;
116     },
117 
118     /**
119      * Sets LinearGravity parameter for LayoutParameter.
120      * @param {ccui.LINEAR_GRAVITY_NONE|ccui.LINEAR_GRAVITY_TOP|ccui.LINEAR_GRAVITY_RIGHT|ccui.LINEAR_GRAVITY_BOTTOM|ccui.LINEAR_GRAVITY_CENTER_VERTICAL|ccui.LINEAR_GRAVITY_CENTER_HORIZONTAL} gravity
121      */
122     setGravity: function (gravity) {
123         this._linearGravity = gravity;
124     },
125 
126     /**
127      * Gets LinearGravity parameter for LayoutParameter.
128      * @returns {ccui.LINEAR_GRAVITY_NONE|ccui.LINEAR_GRAVITY_TOP|ccui.LINEAR_GRAVITY_RIGHT|ccui.LINEAR_GRAVITY_BOTTOM|ccui.LINEAR_GRAVITY_CENTER_VERTICAL|ccui.LINEAR_GRAVITY_CENTER_HORIZONTAL}
129      */
130     getGravity: function () {
131         return this._linearGravity;
132     },
133 
134     /**
135      * create clone instance.
136      * @returns {ccui.LinearLayoutParameter}
137      */
138     createCloneInstance: function () {
139         return ccui.LinearLayoutParameter.create();
140     },
141 
142     /**
143      * copy properties
144      * @param {ccui.LinearLayoutParameter} model
145      */
146     copyProperties: function (model) {
147         ccui.LayoutParameter.prototype.copyProperties.call(this, model);
148         this.setGravity(model._linearGravity);
149     }
150 });
151 
152 /**
153  * allocates and initializes a LinearLayoutParameter.
154  * @constructs
155  * @return {ccui.LinearLayoutParameter}
156  * @example
157  * // example
158  * var uiLinearLayoutParameter = ccui.LinearLayoutParameter.create();
159  */
160 ccui.LinearLayoutParameter.create = function () {
161     var parameter = new ccui.LinearLayoutParameter();
162     return parameter;
163 };
164 
165 /**
166  * Base class for ccui.RelativeLayoutParameter
167  * @class
168  * @extends ccui.LayoutParameter
169  */
170 ccui.RelativeLayoutParameter = ccui.LayoutParameter.extend(/** @lends ccui.RelativeLayoutParameter# */{
171     _relativeAlign: null,
172     _relativeWidgetName: "",
173     _relativeLayoutName: "",
174     _put:false,
175     ctor: function () {
176         ccui.LayoutParameter.prototype.ctor.call(this);
177         this._relativeAlign = ccui.RELATIVE_ALIGN_NONE;
178         this._relativeWidgetName = "";
179         this._relativeLayoutName = "";
180         this._put = false;
181         this._layoutParameterType = ccui.LayoutParameter.RELATIVE;
182     },
183 
184     /**
185      * Sets RelativeAlign parameter for LayoutParameter.
186      * @param {ccui.RELATIVE_ALIGN_*} align
187      */
188     setAlign: function (align) {
189         this._relativeAlign = align;
190     },
191 
192     /**
193      * Gets RelativeAlign parameter for LayoutParameter.
194      * @returns {ccui.RELATIVE_ALIGN_*}
195      */
196     getAlign: function () {
197         return this._relativeAlign;
198     },
199 
200     /**
201      * Sets a key for LayoutParameter. Witch widget named this is relative to.
202      * @param {String} name
203      */
204     setRelativeToWidgetName: function (name) {
205         this._relativeWidgetName = name;
206     },
207 
208     /**
209      * Gets the key of LayoutParameter. Witch widget named this is relative to.
210      * @returns {string}
211      */
212     getRelativeToWidgetName: function () {
213         return this._relativeWidgetName;
214     },
215 
216     /**
217      * Sets a name in Relative Layout for LayoutParameter.
218      * @param {String} name
219      */
220     setRelativeName: function (name) {
221         this._relativeLayoutName = name;
222     },
223 
224     /**
225      * Gets a name in Relative Layout of LayoutParameter.
226      * @returns {string}
227      */
228     getRelativeName: function () {
229         return this._relativeLayoutName;
230     },
231 
232     /**
233      * create clone instance.
234      * @returns {ccui.RelativeLayoutParameter}
235      */
236     createCloneInstance:function(){
237         return ccui.LinearLayoutParameter.create();
238     },
239 
240     /**
241      * copy properties
242      * @param {ccui.RelativeLayoutParameter} model
243      */
244     copyProperties:function(model){
245         ccui.LayoutParameter.prototype.copyProperties.call(this, model);
246         this.setAlign(model._relativeAlign);
247         this.setRelativeToWidgetName(model._relativeWidgetName);
248         this.setRelativeName(model._relativeLayoutName);
249     }
250 });
251 
252 /**
253  * allocates and initializes a RelativeLayoutParameter.
254  * @constructs
255  * @return {ccui.RelativeLayoutParameter}
256  * @example
257  * // example
258  * var uiRelativeLayoutParameter = ccui.RelativeLayoutParameter.create();
259  */
260 ccui.RelativeLayoutParameter.create = function () {
261     var parameter = new ccui.RelativeLayoutParameter();
262     return parameter;
263 };
264 
265 
266 // Constants
267 //layout parameter type
268 ccui.LayoutParameter.NONE = 0;
269 ccui.LayoutParameter.LINEAR = 1;
270 ccui.LayoutParameter.RELATIVE = 2;