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.

336 lines
11KB

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