UILayoutManager.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /****************************************************************************
  2. Copyright (c) 2013-2014 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. /**
  21. * Gets the layout manager by ccui.Layout's layout type.
  22. * @param {Number} type
  23. * @returns {ccui.linearVerticalLayoutManager|ccui.linearHorizontalLayoutManager|ccui.relativeLayoutManager|null}
  24. */
  25. ccui.getLayoutManager = function (type) {
  26. switch (type) {
  27. case ccui.Layout.LINEAR_VERTICAL:
  28. return ccui.linearVerticalLayoutManager;
  29. case ccui.Layout.LINEAR_HORIZONTAL:
  30. return ccui.linearHorizontalLayoutManager;
  31. case ccui.Layout.RELATIVE:
  32. return ccui.relativeLayoutManager;
  33. }
  34. return null;
  35. };
  36. /**
  37. * ccui.linearVerticalLayoutManager is a singleton object which is the linear vertical layout manager for ccui.Layout.
  38. * @class
  39. * @name ccui.linearVerticalLayoutManager
  40. */
  41. ccui.linearVerticalLayoutManager = /** @lends ccui.linearVerticalLayoutManager# */{
  42. _doLayout: function(layout){
  43. var layoutSize = layout._getLayoutContentSize();
  44. var container = layout._getLayoutElements();
  45. var topBoundary = layoutSize.height;
  46. for (var i = 0, len = container.length; i < len; i++) {
  47. var child = container[i];
  48. if (child) {
  49. var layoutParameter = child.getLayoutParameter();
  50. if (layoutParameter){
  51. var childGravity = layoutParameter.getGravity();
  52. var ap = child.getAnchorPoint();
  53. var cs = child.getContentSize();
  54. var finalPosX = ap.x * cs.width;
  55. var finalPosY = topBoundary - ((1.0-ap.y) * cs.height);
  56. switch (childGravity){
  57. case ccui.LinearLayoutParameter.NONE:
  58. case ccui.LinearLayoutParameter.LEFT:
  59. break;
  60. case ccui.LinearLayoutParameter.RIGHT:
  61. finalPosX = layoutSize.width - ((1.0 - ap.x) * cs.width);
  62. break;
  63. case ccui.LinearLayoutParameter.CENTER_HORIZONTAL:
  64. finalPosX = layoutSize.width / 2.0 - cs.width * (0.5-ap.x);
  65. break;
  66. default:
  67. break;
  68. }
  69. var mg = layoutParameter.getMargin();
  70. finalPosX += mg.left;
  71. finalPosY -= mg.top;
  72. child.setPosition(finalPosX, finalPosY);
  73. topBoundary = child.getPositionY() - child.getAnchorPoint().y * child.getContentSize().height - mg.bottom;
  74. }
  75. }
  76. }
  77. }
  78. };
  79. /**
  80. * ccui.linearHorizontalLayoutManager is a singleton object which is the linear horizontal layout manager for ccui.Layout
  81. * @class
  82. * @name ccui.linearHorizontalLayoutManager
  83. */
  84. ccui.linearHorizontalLayoutManager = /** @lends ccui.linearHorizontalLayoutManager# */{
  85. _doLayout: function(layout){
  86. var layoutSize = layout._getLayoutContentSize();
  87. var container = layout._getLayoutElements();
  88. var leftBoundary = 0.0;
  89. for (var i = 0, len = container.length; i < len; i++) {
  90. var child = container[i];
  91. if (child) {
  92. var layoutParameter = child.getLayoutParameter();
  93. if (layoutParameter){
  94. var childGravity = layoutParameter.getGravity();
  95. var ap = child.getAnchorPoint();
  96. var cs = child.getSize();
  97. var finalPosX = leftBoundary + (ap.x * cs.width);
  98. var finalPosY = layoutSize.height - (1.0 - ap.y) * cs.height;
  99. switch (childGravity){
  100. case ccui.LinearLayoutParameter.NONE:
  101. case ccui.LinearLayoutParameter.TOP:
  102. break;
  103. case ccui.LinearLayoutParameter.BOTTOM:
  104. finalPosY = ap.y * cs.height;
  105. break;
  106. case ccui.LinearLayoutParameter.CENTER_VERTICAL:
  107. finalPosY = layoutSize.height / 2.0 - cs.height * (0.5 - ap.y);
  108. break;
  109. default:
  110. break;
  111. }
  112. var mg = layoutParameter.getMargin();
  113. finalPosX += mg.left;
  114. finalPosY -= mg.top;
  115. child.setPosition(finalPosX, finalPosY);
  116. leftBoundary = child.getRightBoundary() + mg.right;
  117. }
  118. }
  119. }
  120. }
  121. };
  122. /**
  123. * ccui.relativeLayoutManager is the singleton object which is the relative layout manager for ccui.Layout, it has a _doLayout function to do layout.
  124. * @class
  125. * @name ccui.relativeLayoutManager
  126. */
  127. ccui.relativeLayoutManager = /** @lends ccui.relativeLayoutManager# */{
  128. _unlayoutChildCount: 0,
  129. _widgetChildren: [],
  130. _widget: null,
  131. _finalPositionX:0,
  132. _finalPositionY:0,
  133. _relativeWidgetLP:null,
  134. _doLayout: function(layout){
  135. this._widgetChildren = this._getAllWidgets(layout);
  136. var locChildren = this._widgetChildren;
  137. while (this._unlayoutChildCount > 0) {
  138. for (var i = 0, len = locChildren.length; i < len; i++) {
  139. this._widget = locChildren[i];
  140. var layoutParameter = this._widget.getLayoutParameter();
  141. if (layoutParameter){
  142. if (layoutParameter._put)
  143. continue;
  144. var ret = this._calculateFinalPositionWithRelativeWidget(layout);
  145. if (!ret)
  146. continue;
  147. this._calculateFinalPositionWithRelativeAlign();
  148. this._widget.setPosition(this._finalPositionX, this._finalPositionY);
  149. layoutParameter._put = true;
  150. }
  151. }
  152. this._unlayoutChildCount--;
  153. }
  154. this._widgetChildren.length = 0;
  155. },
  156. _getAllWidgets: function(layout){
  157. var container = layout._getLayoutElements();
  158. var locWidgetChildren = this._widgetChildren;
  159. locWidgetChildren.length = 0;
  160. for (var i = 0, len = container.length; i < len; i++){
  161. var child = container[i];
  162. if (child) {
  163. var layoutParameter = child.getLayoutParameter();
  164. layoutParameter._put = false;
  165. this._unlayoutChildCount++;
  166. locWidgetChildren.push(child);
  167. }
  168. }
  169. return locWidgetChildren;
  170. },
  171. _getRelativeWidget: function(widget){
  172. var relativeWidget = null;
  173. var layoutParameter = widget.getLayoutParameter();
  174. var relativeName = layoutParameter.getRelativeToWidgetName();
  175. if (relativeName && relativeName.length != 0) {
  176. var locChildren = this._widgetChildren;
  177. for(var i = 0, len = locChildren.length; i < len; i++){
  178. var child = locChildren[i];
  179. if (child){
  180. var rlayoutParameter = child.getLayoutParameter();
  181. if (rlayoutParameter && rlayoutParameter.getRelativeName() == relativeName) {
  182. relativeWidget = child;
  183. this._relativeWidgetLP = rlayoutParameter;
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. return relativeWidget;
  190. },
  191. _calculateFinalPositionWithRelativeWidget: function(layout){
  192. var locWidget = this._widget;
  193. var ap = locWidget.getAnchorPoint();
  194. var cs = locWidget.getContentSize();
  195. this._finalPositionX = 0.0;
  196. this._finalPositionY = 0.0;
  197. var relativeWidget = this._getRelativeWidget(locWidget);
  198. var layoutParameter = locWidget.getLayoutParameter();
  199. var align = layoutParameter.getAlign();
  200. var layoutSize = layout._getLayoutContentSize();
  201. switch (align) {
  202. case ccui.RelativeLayoutParameter.NONE:
  203. case ccui.RelativeLayoutParameter.PARENT_TOP_LEFT:
  204. this._finalPositionX = ap.x * cs.width;
  205. this._finalPositionY = layoutSize.height - ((1.0 - ap.y) * cs.height);
  206. break;
  207. case ccui.RelativeLayoutParameter.PARENT_TOP_CENTER_HORIZONTAL:
  208. this._finalPositionX = layoutSize.width * 0.5 - cs.width * (0.5 - ap.x);
  209. this._finalPositionY = layoutSize.height - ((1.0 - ap.y) * cs.height);
  210. break;
  211. case ccui.RelativeLayoutParameter.PARENT_TOP_RIGHT:
  212. this._finalPositionX = layoutSize.width - ((1.0 - ap.x) * cs.width);
  213. this._finalPositionY = layoutSize.height - ((1.0 - ap.y) * cs.height);
  214. break;
  215. case ccui.RelativeLayoutParameter.PARENT_LEFT_CENTER_VERTICAL:
  216. this._finalPositionX = ap.x * cs.width;
  217. this._finalPositionY = layoutSize.height * 0.5 - cs.height * (0.5 - ap.y);
  218. break;
  219. case ccui.RelativeLayoutParameter.CENTER_IN_PARENT:
  220. this._finalPositionX = layoutSize.width * 0.5 - cs.width * (0.5 - ap.x);
  221. this._finalPositionY = layoutSize.height * 0.5 - cs.height * (0.5 - ap.y);
  222. break;
  223. case ccui.RelativeLayoutParameter.PARENT_RIGHT_CENTER_VERTICAL:
  224. this._finalPositionX = layoutSize.width - ((1.0 - ap.x) * cs.width);
  225. this._finalPositionY = layoutSize.height * 0.5 - cs.height * (0.5 - ap.y);
  226. break;
  227. case ccui.RelativeLayoutParameter.PARENT_LEFT_BOTTOM:
  228. this._finalPositionX = ap.x * cs.width;
  229. this._finalPositionY = ap.y * cs.height;
  230. break;
  231. case ccui.RelativeLayoutParameter.PARENT_BOTTOM_CENTER_HORIZONTAL:
  232. this._finalPositionX = layoutSize.width * 0.5 - cs.width * (0.5 - ap.x);
  233. this._finalPositionY = ap.y * cs.height;
  234. break;
  235. case ccui.RelativeLayoutParameter.PARENT_RIGHT_BOTTOM:
  236. this._finalPositionX = layoutSize.width - ((1.0 - ap.x) * cs.width);
  237. this._finalPositionY = ap.y * cs.height;
  238. break;
  239. case ccui.RelativeLayoutParameter.LOCATION_ABOVE_LEFTALIGN:
  240. if (relativeWidget){
  241. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  242. return false;
  243. this._finalPositionY = relativeWidget.getTopBoundary() + ap.y * cs.height;
  244. this._finalPositionX = relativeWidget.getLeftBoundary() + ap.x * cs.width;
  245. }
  246. break;
  247. case ccui.RelativeLayoutParameter.LOCATION_ABOVE_CENTER:
  248. if (relativeWidget){
  249. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  250. return false;
  251. var rbs = relativeWidget.getContentSize();
  252. this._finalPositionY = relativeWidget.getTopBoundary() + ap.y * cs.height;
  253. this._finalPositionX = relativeWidget.getLeftBoundary() + rbs.width * 0.5 + ap.x * cs.width - cs.width * 0.5;
  254. }
  255. break;
  256. case ccui.RelativeLayoutParameter.LOCATION_ABOVE_RIGHTALIGN:
  257. if (relativeWidget) {
  258. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  259. return false;
  260. this._finalPositionY = relativeWidget.getTopBoundary() + ap.y * cs.height;
  261. this._finalPositionX = relativeWidget.getRightBoundary() - (1.0 - ap.x) * cs.width;
  262. }
  263. break;
  264. case ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_TOPALIGN:
  265. if (relativeWidget){
  266. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  267. return false;
  268. this._finalPositionY = relativeWidget.getTopBoundary() - (1.0 - ap.y) * cs.height;
  269. this._finalPositionX = relativeWidget.getLeftBoundary() - (1.0 - ap.x) * cs.width;
  270. }
  271. break;
  272. case ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_CENTER:
  273. if (relativeWidget) {
  274. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  275. return false;
  276. var rbs = relativeWidget.getContentSize();
  277. this._finalPositionX = relativeWidget.getLeftBoundary() - (1.0 - ap.x) * cs.width;
  278. this._finalPositionY = relativeWidget.getBottomBoundary() + rbs.height * 0.5 + ap.y * cs.height - cs.height * 0.5;
  279. }
  280. break;
  281. case ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_BOTTOMALIGN:
  282. if (relativeWidget) {
  283. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  284. return false;
  285. this._finalPositionY = relativeWidget.getBottomBoundary() + ap.y * cs.height;
  286. this._finalPositionX = relativeWidget.getLeftBoundary() - (1.0 - ap.x) * cs.width;
  287. }
  288. break;
  289. case ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_TOPALIGN:
  290. if (relativeWidget){
  291. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  292. return false;
  293. this._finalPositionY = relativeWidget.getTopBoundary() - (1.0 - ap.y) * cs.height;
  294. this._finalPositionX = relativeWidget.getRightBoundary() + ap.x * cs.width;
  295. }
  296. break;
  297. case ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_CENTER:
  298. if (relativeWidget){
  299. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  300. return false;
  301. var rbs = relativeWidget.getContentSize();
  302. var locationRight = relativeWidget.getRightBoundary();
  303. this._finalPositionX = locationRight + ap.x * cs.width;
  304. this._finalPositionY = relativeWidget.getBottomBoundary() + rbs.height * 0.5 + ap.y * cs.height - cs.height * 0.5;
  305. }
  306. break;
  307. case ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_BOTTOMALIGN:
  308. if (relativeWidget){
  309. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  310. return false;
  311. this._finalPositionY = relativeWidget.getBottomBoundary() + ap.y * cs.height;
  312. this._finalPositionX = relativeWidget.getRightBoundary() + ap.x * cs.width;
  313. }
  314. break;
  315. case ccui.RelativeLayoutParameter.LOCATION_BELOW_LEFTALIGN:
  316. if (relativeWidget){
  317. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  318. return false;
  319. this._finalPositionY = relativeWidget.getBottomBoundary() - (1.0 - ap.y) * cs.height;
  320. this._finalPositionX = relativeWidget.getLeftBoundary() + ap.x * cs.width;
  321. }
  322. break;
  323. case ccui.RelativeLayoutParameter.LOCATION_BELOW_CENTER:
  324. if (relativeWidget) {
  325. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  326. return false;
  327. var rbs = relativeWidget.getContentSize();
  328. this._finalPositionY = relativeWidget.getBottomBoundary() - (1.0 - ap.y) * cs.height;
  329. this._finalPositionX = relativeWidget.getLeftBoundary() + rbs.width * 0.5 + ap.x * cs.width - cs.width * 0.5;
  330. }
  331. break;
  332. case ccui.RelativeLayoutParameter.LOCATION_BELOW_RIGHTALIGN:
  333. if (relativeWidget) {
  334. if (this._relativeWidgetLP && !this._relativeWidgetLP._put)
  335. return false;
  336. this._finalPositionY = relativeWidget.getBottomBoundary() - (1.0 - ap.y) * cs.height;
  337. this._finalPositionX = relativeWidget.getRightBoundary() - (1.0 - ap.x) * cs.width;
  338. }
  339. break;
  340. default:
  341. break;
  342. }
  343. return true;
  344. },
  345. _calculateFinalPositionWithRelativeAlign: function(){
  346. var layoutParameter = this._widget.getLayoutParameter();
  347. var mg = layoutParameter.getMargin();
  348. var align = layoutParameter.getAlign();
  349. //handle margin
  350. switch (align) {
  351. case ccui.RelativeLayoutParameter.NONE:
  352. case ccui.RelativeLayoutParameter.PARENT_TOP_LEFT:
  353. this._finalPositionX += mg.left;
  354. this._finalPositionY -= mg.top;
  355. break;
  356. case ccui.RelativeLayoutParameter.PARENT_TOP_CENTER_HORIZONTAL:
  357. this._finalPositionY -= mg.top;
  358. break;
  359. case ccui.RelativeLayoutParameter.PARENT_TOP_RIGHT:
  360. this._finalPositionX -= mg.right;
  361. this._finalPositionY -= mg.top;
  362. break;
  363. case ccui.RelativeLayoutParameter.PARENT_LEFT_CENTER_VERTICAL:
  364. this._finalPositionX += mg.left;
  365. break;
  366. case ccui.RelativeLayoutParameter.CENTER_IN_PARENT:
  367. break;
  368. case ccui.RelativeLayoutParameter.PARENT_RIGHT_CENTER_VERTICAL:
  369. this._finalPositionX -= mg.right;
  370. break;
  371. case ccui.RelativeLayoutParameter.PARENT_LEFT_BOTTOM:
  372. this._finalPositionX += mg.left;
  373. this._finalPositionY += mg.bottom;
  374. break;
  375. case ccui.RelativeLayoutParameter.PARENT_BOTTOM_CENTER_HORIZONTAL:
  376. this._finalPositionY += mg.bottom;
  377. break;
  378. case ccui.RelativeLayoutParameter.PARENT_RIGHT_BOTTOM:
  379. this._finalPositionX -= mg.right;
  380. this._finalPositionY += mg.bottom;
  381. break;
  382. case ccui.RelativeLayoutParameter.LOCATION_ABOVE_LEFTALIGN:
  383. this._finalPositionY += mg.bottom;
  384. this._finalPositionX += mg.left;
  385. break;
  386. case ccui.RelativeLayoutParameter.LOCATION_ABOVE_RIGHTALIGN:
  387. this._finalPositionY += mg.bottom;
  388. this._finalPositionX -= mg.right;
  389. break;
  390. case ccui.RelativeLayoutParameter.LOCATION_ABOVE_CENTER:
  391. this._finalPositionY += mg.bottom;
  392. break;
  393. case ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_TOPALIGN:
  394. this._finalPositionX -= mg.right;
  395. this._finalPositionY -= mg.top;
  396. break;
  397. case ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_BOTTOMALIGN:
  398. this._finalPositionX -= mg.right;
  399. this._finalPositionY += mg.bottom;
  400. break;
  401. case ccui.RelativeLayoutParameter.LOCATION_LEFT_OF_CENTER:
  402. this._finalPositionX -= mg.right;
  403. break;
  404. case ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_TOPALIGN:
  405. this._finalPositionX += mg.left;
  406. this._finalPositionY -= mg.top;
  407. break;
  408. case ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_BOTTOMALIGN:
  409. this._finalPositionX += mg.left;
  410. this._finalPositionY += mg.bottom;
  411. break;
  412. case ccui.RelativeLayoutParameter.LOCATION_RIGHT_OF_CENTER:
  413. this._finalPositionX += mg.left;
  414. break;
  415. case ccui.RelativeLayoutParameter.LOCATION_BELOW_LEFTALIGN:
  416. this._finalPositionY -= mg.top;
  417. this._finalPositionX += mg.left;
  418. break;
  419. case ccui.RelativeLayoutParameter.LOCATION_BELOW_RIGHTALIGN:
  420. this._finalPositionY -= mg.top;
  421. this._finalPositionX -= mg.right;
  422. break;
  423. case ccui.RelativeLayoutParameter.LOCATION_BELOW_CENTER:
  424. this._finalPositionY -= mg.top;
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. };