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 11KB

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