StackLayout.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "StackLayout.h"
  23. #include "Types.h"
  24. #include "SpecificProperties.h"
  25. namespace Crown
  26. {
  27. StackLayout::StackLayout(Widget* parent):
  28. Widget(parent), mOrientation(SO_VERTICAL)
  29. {
  30. SetMouseSensible(false);
  31. EnumProperty* prop = new EnumProperty("Orientation", (int32_t*)&mOrientation);
  32. prop->AddValueMapping("Horizontal", SO_HORIZONTAL);
  33. prop->AddValueMapping("Vertical", SO_VERTICAL);
  34. AddProperty(prop);
  35. }
  36. StackLayout::~StackLayout()
  37. {
  38. }
  39. void StackLayout::SetOrientation(StackOrientation value)
  40. {
  41. mOrientation = value;
  42. NotifyNeedsLayout();
  43. }
  44. void StackLayout::OnSetProperty(const Str& name)
  45. {
  46. if (name == "Orientation")
  47. {
  48. NotifyNeedsLayout();
  49. }
  50. else
  51. {
  52. Widget::OnSetProperty(name);
  53. }
  54. }
  55. void StackLayout::OnArrange(Point32_t2 position, Point32_t2 size)
  56. {
  57. Move(position);
  58. Resize(size);
  59. //La misurazione è stata fatta, ogni widget figlio sa le dimensioni che desidera o se sono arbitrarie
  60. if (mOrientation == SO_VERTICAL)
  61. {
  62. //Calculate the remaining space, given that some widgets have an height set
  63. //Obiettivo: assegnare la dimensione desiderata ai widget che la hanno,
  64. // individuare lo spazio rimanente e finchè ad almeno un widget non va bene
  65. // riprovare a calcolarlo
  66. List<WidgetSizeAcceptedEnum> acceptedStatus;
  67. const List<Widget*>& children = GetChildren();
  68. int32_t fixedHeight = 0;
  69. int32_t fixedHeightCount = 0;
  70. for (int32_t i = 0; i < children.GetSize(); i++)
  71. {
  72. Widget* child = children[i];
  73. int32_t measuredY = child->GetMeasuredSize().y;
  74. if (measuredY >= 0)
  75. {
  76. acceptedStatus.Append(WSA_FIXED);
  77. fixedHeightCount ++;
  78. //Take margins int32_to account
  79. fixedHeight += measuredY + child->GetMargins().GetFixedMarginsY();
  80. }
  81. else
  82. acceptedStatus.Append(WSA_ACCEPTED);
  83. }
  84. float suggestedHeight = ((float)size.y - fixedHeight) / (children.GetSize() - fixedHeightCount);
  85. float roundupRemainder = suggestedHeight - (int32_t)(suggestedHeight);
  86. float accumulator = 0.0f;
  87. suggestedHeight = (float)((int32_t)suggestedHeight);
  88. if (suggestedHeight < 0.0f)
  89. {
  90. //No free space to assign, go over to actual assignment phase
  91. suggestedHeight = 0.0f;
  92. }
  93. else
  94. {
  95. for (int32_t i = 0; i < children.GetSize(); i++)
  96. {
  97. Widget* child = children[i];
  98. WidgetSizeAcceptedEnum accepted = acceptedStatus[i];
  99. if (accepted != WSA_ACCEPTED)
  100. continue;
  101. else
  102. {
  103. //Try to propose the calculated height and see if the response changes
  104. //If so, restart the scan with a new suggestedHeight
  105. int32_t height = (int32_t)suggestedHeight;
  106. accumulator += roundupRemainder;
  107. if (accumulator >= 0.9999f)
  108. {
  109. accumulator -= 1.0f;
  110. height++;
  111. }
  112. WidgetSizeAcceptedEnum acceptedNew = child->GetSizeAcceptedY(height);
  113. if (acceptedNew != accepted)
  114. {
  115. //Based on acceptedNew, which is now either WSA_MIN_SIZE or WSA_MAX_SIZE, add the min or max size to the fixedHeight and start over
  116. switch (acceptedNew)
  117. {
  118. case WSA_MIN_SIZE:
  119. fixedHeight += child->GetMinimumSize().y;
  120. break;
  121. case WSA_MAX_SIZE:
  122. fixedHeight += child->GetMaximumSize().y;
  123. break;
  124. }
  125. //Consider the fixed margins as well
  126. fixedHeight += child->GetMargins().GetFixedMarginsY();
  127. fixedHeightCount += 1;
  128. if (children.GetSize() == fixedHeightCount)
  129. {
  130. //No more variable sized widgets to calculate, exit the acceptation for loop
  131. break;
  132. }
  133. suggestedHeight = ((float)size.y - fixedHeight) / (children.GetSize() - fixedHeightCount);
  134. roundupRemainder = suggestedHeight - (int32_t)(suggestedHeight);
  135. accumulator = 0.0f;
  136. suggestedHeight = (float)((int32_t)suggestedHeight);
  137. //Replace the acceptedStatus and start over
  138. acceptedStatus[i] = acceptedNew;
  139. i = -1;
  140. }
  141. }
  142. }
  143. }
  144. accumulator = 0.0f;
  145. position = Point32_t2::ZERO;
  146. int32_t xSpace = size.x;
  147. size.y = 0;
  148. for (int32_t i = 0; i < children.GetSize(); i++)
  149. {
  150. Widget* child = children[i];
  151. Point32_t2 pos = position;
  152. int32_t occupiedSpace = 0;
  153. bool applyMargins = false;
  154. switch (acceptedStatus[i])
  155. {
  156. case WSA_ACCEPTED:
  157. {
  158. //Use the suggested height
  159. int32_t height = (int32_t)suggestedHeight;
  160. accumulator += roundupRemainder;
  161. if (accumulator >= 0.9999f)
  162. {
  163. accumulator -= 1.0f;
  164. height++;
  165. }
  166. occupiedSpace = height;
  167. size.y = height;
  168. child->ErodeMarginsY(pos, size);
  169. }break;
  170. case WSA_MIN_SIZE:
  171. {
  172. //Pass in the min size plus the margins
  173. size.y = child->GetMinimumSize().y;
  174. applyMargins = true;
  175. }break;
  176. case WSA_MAX_SIZE:
  177. {
  178. //Pass in the min size plus the margins
  179. size.y = child->GetMaximumSize().y;
  180. applyMargins = true;
  181. }break;
  182. case WSA_FIXED:
  183. {
  184. //Pass in the min size plus the margins
  185. size.y = child->GetMeasuredSize().y;
  186. applyMargins = true;
  187. }break;
  188. }
  189. if (applyMargins)
  190. {
  191. occupiedSpace = size.y + child->GetMargins().GetFixedMarginsY();
  192. if (child->GetMargins().top > 0)
  193. {
  194. pos.y += child->GetMargins().top;
  195. }
  196. }
  197. size.x = xSpace;
  198. child->FitMeasuredSizeX(pos, size);
  199. child->OnArrange(pos, size);
  200. position.y += occupiedSpace;
  201. }
  202. }
  203. else
  204. {
  205. //Calculate the remaining space, given that some widgets have an height set
  206. //Obiettivo: assegnare la dimensione desiderata ai widget che la hanno,
  207. // individuare lo spazio rimanente e finchè ad almeno un widget non va bene
  208. // riprovare a calcolarlo
  209. List<WidgetSizeAcceptedEnum> acceptedStatus;
  210. const List<Widget*>& children = GetChildren();
  211. int32_t fixedHeight = 0;
  212. int32_t fixedHeightCount = 0;
  213. for (int32_t i = 0; i < children.GetSize(); i++)
  214. {
  215. Widget* child = children[i];
  216. int32_t measuredX = child->GetMeasuredSize().x;
  217. if (measuredX >= 0)
  218. {
  219. acceptedStatus.Append(WSA_FIXED);
  220. fixedHeightCount ++;
  221. //Take margins int32_to account
  222. fixedHeight += measuredX + child->GetMargins().GetFixedMarginsX();
  223. }
  224. else
  225. acceptedStatus.Append(WSA_ACCEPTED);
  226. }
  227. float suggestedHeight = ((float)size.x - fixedHeight) / (children.GetSize() - fixedHeightCount);
  228. float roundupRemainder = suggestedHeight - (int32_t)(suggestedHeight);
  229. float accumulator = 0.0f;
  230. suggestedHeight = (float)((int32_t)suggestedHeight);
  231. if (suggestedHeight < 0.0f)
  232. {
  233. //No free space to assign, go over to actual assignment phase
  234. suggestedHeight = 0.0f;
  235. }
  236. else
  237. {
  238. for (int32_t i = 0; i < children.GetSize(); i++)
  239. {
  240. Widget* child = children[i];
  241. WidgetSizeAcceptedEnum accepted = acceptedStatus[i];
  242. if (accepted != WSA_ACCEPTED)
  243. continue;
  244. else
  245. {
  246. //Try to propose the calculated height and see if the response changes
  247. //If so, restart the scan with a new suggestedHeight
  248. int32_t width = (int32_t)suggestedHeight;
  249. accumulator += roundupRemainder;
  250. if (accumulator >= 0.9999f)
  251. {
  252. accumulator -= 1.0f;
  253. width++;
  254. }
  255. WidgetSizeAcceptedEnum acceptedNew = child->GetSizeAcceptedX(width);
  256. if (acceptedNew != accepted)
  257. {
  258. //Based on acceptedNew, which is now either WSA_MIN_SIZE or WSA_MAX_SIZE, add the min or max size to the fixedHeight and start over
  259. switch (acceptedNew)
  260. {
  261. case WSA_MIN_SIZE:
  262. fixedHeight += child->GetMinimumSize().x;
  263. break;
  264. case WSA_MAX_SIZE:
  265. fixedHeight += child->GetMaximumSize().x;
  266. break;
  267. }
  268. //Consider the fixed margins as well
  269. fixedHeight += child->GetMargins().GetFixedMarginsX();
  270. fixedHeightCount += 1;
  271. if (children.GetSize() == fixedHeightCount)
  272. {
  273. //No more variable sized widgets to calculate, exit the acceptation for loop
  274. break;
  275. }
  276. suggestedHeight = ((float)size.x - fixedHeight) / (children.GetSize() - fixedHeightCount);
  277. roundupRemainder = suggestedHeight - (int32_t)(suggestedHeight);
  278. accumulator = 0.0f;
  279. suggestedHeight = (float)((int32_t)suggestedHeight);
  280. //Replace the acceptedStatus and start over
  281. acceptedStatus[i] = acceptedNew;
  282. i = -1;
  283. }
  284. }
  285. }
  286. }
  287. accumulator = 0.0f;
  288. position = Point32_t2::ZERO;
  289. int32_t ySpace = size.y;
  290. size.x = 0;
  291. for (int32_t i = 0; i < children.GetSize(); i++)
  292. {
  293. Widget* child = children[i];
  294. Point32_t2 pos = position;
  295. int32_t occupiedSpace = 0;
  296. bool applyMargins = false;
  297. switch (acceptedStatus[i])
  298. {
  299. case WSA_ACCEPTED:
  300. {
  301. //Use the suggested height
  302. int32_t height = (int32_t)suggestedHeight;
  303. accumulator += roundupRemainder;
  304. if (accumulator >= 0.9999f)
  305. {
  306. accumulator -= 1.0f;
  307. height++;
  308. }
  309. occupiedSpace = height;
  310. size.x = height;
  311. child->ErodeMarginsX(pos, size);
  312. }break;
  313. case WSA_MIN_SIZE:
  314. {
  315. //Pass in the min size plus the margins
  316. size.x = child->GetMinimumSize().x;
  317. applyMargins = true;
  318. }break;
  319. case WSA_MAX_SIZE:
  320. {
  321. //Pass in the min size plus the margins
  322. size.x = child->GetMaximumSize().x;
  323. applyMargins = true;
  324. }break;
  325. case WSA_FIXED:
  326. {
  327. //Pass in the min size plus the margins
  328. size.x = child->GetMeasuredSize().x;
  329. applyMargins = true;
  330. }break;
  331. }
  332. if (applyMargins)
  333. {
  334. occupiedSpace = size.x + child->GetMargins().GetFixedMarginsX();
  335. if (child->GetMargins().left > 0)
  336. {
  337. pos.x += child->GetMargins().left;
  338. }
  339. }
  340. size.y = ySpace;
  341. child->FitMeasuredSizeY(pos, size);
  342. child->OnArrange(pos, size);
  343. position.x += occupiedSpace;
  344. }
  345. }
  346. }
  347. void StackLayout::OnMeasureFitX()
  348. {
  349. int32_t msx = 0;
  350. const List<Widget*> children = GetChildren();
  351. for (int32_t i = 0; i < children.GetSize(); i++)
  352. {
  353. Widget* child = children[i];
  354. int32_t w = child->GetMeasuredSize().x;
  355. if (child->GetMargins().left > 0)
  356. w += child->GetMargins().left;
  357. if (child->GetMargins().right > 0)
  358. w += child->GetMargins().right;
  359. if (mOrientation == SO_VERTICAL)
  360. msx = Math::Max(msx, w);
  361. else
  362. msx += w;
  363. }
  364. mMeasuredSize.x = msx;
  365. }
  366. void StackLayout::OnMeasureFitY()
  367. {
  368. int32_t msy = 0;
  369. const List<Widget*> children = GetChildren();
  370. for (int32_t i = 0; i < children.GetSize(); i++)
  371. {
  372. Widget* child = children[i];
  373. int32_t h = child->GetMeasuredSize().y;
  374. if (child->GetMargins().top > 0)
  375. h += child->GetMargins().top;
  376. if (child->GetMargins().bottom > 0)
  377. h += child->GetMargins().bottom;
  378. if (mOrientation == SO_HORIZONTAL)
  379. msy = Math::Max(msy, h);
  380. else
  381. msy += h;
  382. }
  383. mMeasuredSize.y = msy;
  384. }
  385. }