UILayoutParameter.js 16 KB

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