Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_StretchableLayoutManager.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. StretchableLayoutManager::StretchableLayoutManager()
  20. : totalSize (0)
  21. {
  22. }
  23. StretchableLayoutManager::~StretchableLayoutManager()
  24. {
  25. }
  26. //==============================================================================
  27. void StretchableLayoutManager::clearAllItems()
  28. {
  29. items.clear();
  30. totalSize = 0;
  31. }
  32. void StretchableLayoutManager::setItemLayout (const int itemIndex,
  33. const double minimumSize,
  34. const double maximumSize,
  35. const double preferredSize)
  36. {
  37. ItemLayoutProperties* layout = getInfoFor (itemIndex);
  38. if (layout == nullptr)
  39. {
  40. layout = new ItemLayoutProperties();
  41. layout->itemIndex = itemIndex;
  42. int i;
  43. for (i = 0; i < items.size(); ++i)
  44. if (items.getUnchecked (i)->itemIndex > itemIndex)
  45. break;
  46. items.insert (i, layout);
  47. }
  48. layout->minSize = minimumSize;
  49. layout->maxSize = maximumSize;
  50. layout->preferredSize = preferredSize;
  51. layout->currentSize = 0;
  52. }
  53. bool StretchableLayoutManager::getItemLayout (const int itemIndex,
  54. double& minimumSize,
  55. double& maximumSize,
  56. double& preferredSize) const
  57. {
  58. if (const ItemLayoutProperties* const layout = getInfoFor (itemIndex))
  59. {
  60. minimumSize = layout->minSize;
  61. maximumSize = layout->maxSize;
  62. preferredSize = layout->preferredSize;
  63. return true;
  64. }
  65. return false;
  66. }
  67. //==============================================================================
  68. void StretchableLayoutManager::setTotalSize (const int newTotalSize)
  69. {
  70. totalSize = newTotalSize;
  71. fitComponentsIntoSpace (0, items.size(), totalSize, 0);
  72. }
  73. int StretchableLayoutManager::getItemCurrentPosition (const int itemIndex) const
  74. {
  75. int pos = 0;
  76. for (int i = 0; i < itemIndex; ++i)
  77. if (const ItemLayoutProperties* const layout = getInfoFor (i))
  78. pos += layout->currentSize;
  79. return pos;
  80. }
  81. int StretchableLayoutManager::getItemCurrentAbsoluteSize (const int itemIndex) const
  82. {
  83. if (const ItemLayoutProperties* const layout = getInfoFor (itemIndex))
  84. return layout->currentSize;
  85. return 0;
  86. }
  87. double StretchableLayoutManager::getItemCurrentRelativeSize (const int itemIndex) const
  88. {
  89. if (const ItemLayoutProperties* const layout = getInfoFor (itemIndex))
  90. return -layout->currentSize / (double) totalSize;
  91. return 0;
  92. }
  93. void StretchableLayoutManager::setItemPosition (const int itemIndex,
  94. int newPosition)
  95. {
  96. for (int i = items.size(); --i >= 0;)
  97. {
  98. const ItemLayoutProperties* const layout = items.getUnchecked(i);
  99. if (layout->itemIndex == itemIndex)
  100. {
  101. int realTotalSize = jmax (totalSize, getMinimumSizeOfItems (0, items.size()));
  102. const int minSizeAfterThisComp = getMinimumSizeOfItems (i, items.size());
  103. const int maxSizeAfterThisComp = getMaximumSizeOfItems (i + 1, items.size());
  104. newPosition = jmax (newPosition, totalSize - maxSizeAfterThisComp - layout->currentSize);
  105. newPosition = jmin (newPosition, realTotalSize - minSizeAfterThisComp);
  106. int endPos = fitComponentsIntoSpace (0, i, newPosition, 0);
  107. endPos += layout->currentSize;
  108. fitComponentsIntoSpace (i + 1, items.size(), totalSize - endPos, endPos);
  109. updatePrefSizesToMatchCurrentPositions();
  110. break;
  111. }
  112. }
  113. }
  114. //==============================================================================
  115. void StretchableLayoutManager::layOutComponents (Component** const components,
  116. int numComponents,
  117. int x, int y, int w, int h,
  118. const bool vertically,
  119. const bool resizeOtherDimension)
  120. {
  121. setTotalSize (vertically ? h : w);
  122. int pos = vertically ? y : x;
  123. for (int i = 0; i < numComponents; ++i)
  124. {
  125. if (const ItemLayoutProperties* const layout = getInfoFor (i))
  126. {
  127. if (Component* const c = components[i])
  128. {
  129. if (i == numComponents - 1)
  130. {
  131. // if it's the last item, crop it to exactly fit the available space..
  132. if (resizeOtherDimension)
  133. {
  134. if (vertically)
  135. c->setBounds (x, pos, w, jmax (layout->currentSize, h - pos));
  136. else
  137. c->setBounds (pos, y, jmax (layout->currentSize, w - pos), h);
  138. }
  139. else
  140. {
  141. if (vertically)
  142. c->setBounds (c->getX(), pos, c->getWidth(), jmax (layout->currentSize, h - pos));
  143. else
  144. c->setBounds (pos, c->getY(), jmax (layout->currentSize, w - pos), c->getHeight());
  145. }
  146. }
  147. else
  148. {
  149. if (resizeOtherDimension)
  150. {
  151. if (vertically)
  152. c->setBounds (x, pos, w, layout->currentSize);
  153. else
  154. c->setBounds (pos, y, layout->currentSize, h);
  155. }
  156. else
  157. {
  158. if (vertically)
  159. c->setBounds (c->getX(), pos, c->getWidth(), layout->currentSize);
  160. else
  161. c->setBounds (pos, c->getY(), layout->currentSize, c->getHeight());
  162. }
  163. }
  164. }
  165. pos += layout->currentSize;
  166. }
  167. }
  168. }
  169. //==============================================================================
  170. StretchableLayoutManager::ItemLayoutProperties* StretchableLayoutManager::getInfoFor (const int itemIndex) const
  171. {
  172. for (int i = items.size(); --i >= 0;)
  173. if (items.getUnchecked(i)->itemIndex == itemIndex)
  174. return items.getUnchecked(i);
  175. return nullptr;
  176. }
  177. int StretchableLayoutManager::fitComponentsIntoSpace (const int startIndex,
  178. const int endIndex,
  179. const int availableSpace,
  180. int startPos)
  181. {
  182. // calculate the total sizes
  183. double totalIdealSize = 0.0;
  184. int totalMinimums = 0;
  185. for (int i = startIndex; i < endIndex; ++i)
  186. {
  187. ItemLayoutProperties* const layout = items.getUnchecked (i);
  188. layout->currentSize = sizeToRealSize (layout->minSize, totalSize);
  189. totalMinimums += layout->currentSize;
  190. totalIdealSize += sizeToRealSize (layout->preferredSize, totalSize);
  191. }
  192. if (totalIdealSize <= 0)
  193. totalIdealSize = 1.0;
  194. // now calc the best sizes..
  195. int extraSpace = availableSpace - totalMinimums;
  196. while (extraSpace > 0)
  197. {
  198. int numWantingMoreSpace = 0;
  199. int numHavingTakenExtraSpace = 0;
  200. // first figure out how many comps want a slice of the extra space..
  201. for (int i = startIndex; i < endIndex; ++i)
  202. {
  203. ItemLayoutProperties* const layout = items.getUnchecked (i);
  204. double sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);
  205. const int bestSize = jlimit (layout->currentSize,
  206. jmax (layout->currentSize,
  207. sizeToRealSize (layout->maxSize, totalSize)),
  208. roundToInt (sizeWanted * availableSpace / totalIdealSize));
  209. if (bestSize > layout->currentSize)
  210. ++numWantingMoreSpace;
  211. }
  212. // ..share out the extra space..
  213. for (int i = startIndex; i < endIndex; ++i)
  214. {
  215. ItemLayoutProperties* const layout = items.getUnchecked (i);
  216. double sizeWanted = sizeToRealSize (layout->preferredSize, totalSize);
  217. int bestSize = jlimit (layout->currentSize,
  218. jmax (layout->currentSize, sizeToRealSize (layout->maxSize, totalSize)),
  219. roundToInt (sizeWanted * availableSpace / totalIdealSize));
  220. const int extraWanted = bestSize - layout->currentSize;
  221. if (extraWanted > 0)
  222. {
  223. const int extraAllowed = jmin (extraWanted,
  224. extraSpace / jmax (1, numWantingMoreSpace));
  225. if (extraAllowed > 0)
  226. {
  227. ++numHavingTakenExtraSpace;
  228. --numWantingMoreSpace;
  229. layout->currentSize += extraAllowed;
  230. extraSpace -= extraAllowed;
  231. }
  232. }
  233. }
  234. if (numHavingTakenExtraSpace <= 0)
  235. break;
  236. }
  237. // ..and calculate the end position
  238. for (int i = startIndex; i < endIndex; ++i)
  239. {
  240. ItemLayoutProperties* const layout = items.getUnchecked(i);
  241. startPos += layout->currentSize;
  242. }
  243. return startPos;
  244. }
  245. int StretchableLayoutManager::getMinimumSizeOfItems (const int startIndex,
  246. const int endIndex) const
  247. {
  248. int totalMinimums = 0;
  249. for (int i = startIndex; i < endIndex; ++i)
  250. totalMinimums += sizeToRealSize (items.getUnchecked (i)->minSize, totalSize);
  251. return totalMinimums;
  252. }
  253. int StretchableLayoutManager::getMaximumSizeOfItems (const int startIndex, const int endIndex) const
  254. {
  255. int totalMaximums = 0;
  256. for (int i = startIndex; i < endIndex; ++i)
  257. totalMaximums += sizeToRealSize (items.getUnchecked (i)->maxSize, totalSize);
  258. return totalMaximums;
  259. }
  260. void StretchableLayoutManager::updatePrefSizesToMatchCurrentPositions()
  261. {
  262. for (int i = 0; i < items.size(); ++i)
  263. {
  264. ItemLayoutProperties* const layout = items.getUnchecked (i);
  265. layout->preferredSize
  266. = (layout->preferredSize < 0) ? getItemCurrentRelativeSize (i)
  267. : getItemCurrentAbsoluteSize (i);
  268. }
  269. }
  270. int StretchableLayoutManager::sizeToRealSize (double size, int totalSpace)
  271. {
  272. if (size < 0)
  273. size *= -totalSpace;
  274. return roundToInt (size);
  275. }