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.

350 lines
12KB

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