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.

432 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. Displays::Displays (Desktop& desktop)
  16. {
  17. init (desktop);
  18. }
  19. void Displays::init (Desktop& desktop)
  20. {
  21. findDisplays (desktop.getGlobalScaleFactor());
  22. }
  23. const Displays::Display* Displays::getDisplayForRect (Rectangle<int> rect, bool isPhysical) const noexcept
  24. {
  25. int maxArea = -1;
  26. const Display* foundDisplay = nullptr;
  27. for (auto& display : displays)
  28. {
  29. auto displayArea = display.totalArea;
  30. if (isPhysical)
  31. displayArea = (displayArea.withZeroOrigin() * display.scale) + display.topLeftPhysical;
  32. displayArea = displayArea.getIntersection (rect);
  33. auto area = displayArea.getWidth() * displayArea.getHeight();
  34. if (area >= maxArea)
  35. {
  36. maxArea = area;
  37. foundDisplay = &display;
  38. }
  39. }
  40. return foundDisplay;
  41. }
  42. const Displays::Display* Displays::getDisplayForPoint (Point<int> point, bool isPhysical) const noexcept
  43. {
  44. auto minDistance = std::numeric_limits<int>::max();
  45. const Display* foundDisplay = nullptr;
  46. for (auto& display : displays)
  47. {
  48. auto displayArea = display.totalArea;
  49. if (isPhysical)
  50. displayArea = (displayArea.withZeroOrigin() * display.scale) + display.topLeftPhysical;
  51. if (displayArea.contains (point))
  52. return &display;
  53. auto distance = displayArea.getCentre().getDistanceFrom (point);
  54. if (distance <= minDistance)
  55. {
  56. minDistance = distance;
  57. foundDisplay = &display;
  58. }
  59. }
  60. return foundDisplay;
  61. }
  62. Rectangle<int> Displays::physicalToLogical (Rectangle<int> rect, const Display* useScaleFactorOfDisplay) const noexcept
  63. {
  64. return physicalToLogical (rect.toFloat(), useScaleFactorOfDisplay).toNearestInt();
  65. }
  66. Rectangle<float> Displays::physicalToLogical (Rectangle<float> rect, const Display* useScaleFactorOfDisplay) const noexcept
  67. {
  68. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  69. : getDisplayForRect (rect.toNearestInt(), true);
  70. if (display == nullptr)
  71. return rect;
  72. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  73. return ((rect - display->topLeftPhysical.toFloat()) / (display->scale / globalScale))
  74. + (display->totalArea.getTopLeft().toFloat() * globalScale);
  75. }
  76. Rectangle<int> Displays::logicalToPhysical (Rectangle<int> rect, const Display* useScaleFactorOfDisplay) const noexcept
  77. {
  78. return logicalToPhysical (rect.toFloat(), useScaleFactorOfDisplay).toNearestInt();
  79. }
  80. Rectangle<float> Displays::logicalToPhysical (Rectangle<float> rect, const Display* useScaleFactorOfDisplay) const noexcept
  81. {
  82. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  83. : getDisplayForRect (rect.toNearestInt(), false);
  84. if (display == nullptr)
  85. return rect;
  86. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  87. return ((rect.toFloat() - (display->totalArea.getTopLeft().toFloat() * globalScale)) * (display->scale / globalScale))
  88. + display->topLeftPhysical.toFloat();
  89. }
  90. template <typename ValueType>
  91. Point<ValueType> Displays::physicalToLogical (Point<ValueType> point, const Display* useScaleFactorOfDisplay) const noexcept
  92. {
  93. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  94. : getDisplayForPoint (point.roundToInt(), true);
  95. if (display == nullptr)
  96. return point;
  97. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  98. Point<ValueType> logicalTopLeft (static_cast<ValueType> (display->totalArea.getX()), static_cast<ValueType> (display->totalArea.getY()));
  99. Point<ValueType> physicalTopLeft (static_cast<ValueType> (display->topLeftPhysical.getX()), static_cast<ValueType> (display->topLeftPhysical.getY()));
  100. return ((point - physicalTopLeft) / (display->scale / globalScale)) + (logicalTopLeft * globalScale);
  101. }
  102. template <typename ValueType>
  103. Point<ValueType> Displays::logicalToPhysical (Point<ValueType> point, const Display* useScaleFactorOfDisplay) const noexcept
  104. {
  105. const auto* display = useScaleFactorOfDisplay != nullptr ? useScaleFactorOfDisplay
  106. : getDisplayForPoint (point.roundToInt(), false);
  107. if (display == nullptr)
  108. return point;
  109. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  110. Point<ValueType> logicalTopLeft (static_cast<ValueType> (display->totalArea.getX()), static_cast<ValueType> (display->totalArea.getY()));
  111. Point<ValueType> physicalTopLeft (static_cast<ValueType> (display->topLeftPhysical.getX()), static_cast<ValueType> (display->topLeftPhysical.getY()));
  112. return ((point - (logicalTopLeft * globalScale)) * (display->scale / globalScale)) + physicalTopLeft;
  113. }
  114. const Displays::Display* Displays::getPrimaryDisplay() const noexcept
  115. {
  116. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  117. for (auto& d : displays)
  118. if (d.isMain)
  119. return &d;
  120. return nullptr;
  121. }
  122. RectangleList<int> Displays::getRectangleList (bool userAreasOnly) const
  123. {
  124. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  125. RectangleList<int> rl;
  126. for (auto& d : displays)
  127. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  128. return rl;
  129. }
  130. Rectangle<int> Displays::getTotalBounds (bool userAreasOnly) const
  131. {
  132. return getRectangleList (userAreasOnly).getBounds();
  133. }
  134. void Displays::refresh()
  135. {
  136. Array<Display> oldDisplays;
  137. oldDisplays.swapWith (displays);
  138. init (Desktop::getInstance());
  139. if (oldDisplays != displays)
  140. {
  141. for (auto i = ComponentPeer::getNumPeers(); --i >= 0;)
  142. if (auto* peer = ComponentPeer::getPeer (i))
  143. peer->handleScreenSizeChange();
  144. }
  145. }
  146. bool operator== (const Displays::Display& d1, const Displays::Display& d2) noexcept;
  147. bool operator== (const Displays::Display& d1, const Displays::Display& d2) noexcept
  148. {
  149. return d1.isMain == d2.isMain
  150. && d1.totalArea == d2.totalArea
  151. && d1.userArea == d2.userArea
  152. && d1.topLeftPhysical == d2.topLeftPhysical
  153. && d1.scale == d2.scale
  154. && d1.dpi == d2.dpi;
  155. }
  156. bool operator!= (const Displays::Display& d1, const Displays::Display& d2) noexcept;
  157. bool operator!= (const Displays::Display& d1, const Displays::Display& d2) noexcept { return ! (d1 == d2); }
  158. //==============================================================================
  159. // These methods are used for converting the totalArea and userArea Rectangles in Display from physical to logical
  160. // pixels. We do this by constructing a graph of connected displays where the root node has position (0, 0); this can be
  161. // safely converted to logical pixels using its scale factor and we can then traverse the graph and work out the logical pixels
  162. // for all the other connected displays. We need to do this as the logical bounds of a display depend not only on its scale
  163. // factor but also the scale factor of the displays connected to it.
  164. /**
  165. Represents a node in our graph of displays.
  166. */
  167. struct DisplayNode
  168. {
  169. /** The Display object that this represents. */
  170. Displays::Display* display;
  171. /** True if this represents the 'root' display with position (0, 0). */
  172. bool isRoot = false;
  173. /** The parent node of this node in our display graph. This will have a correct logicalArea. */
  174. DisplayNode* parent = nullptr;
  175. /** The logical area to be calculated. This will be valid after processDisplay() has
  176. been called on this node.
  177. */
  178. Rectangle<double> logicalArea;
  179. };
  180. /** Recursive - will calculate and set the logicalArea member of current. */
  181. static void processDisplay (DisplayNode* currentNode, Array<DisplayNode>& allNodes)
  182. {
  183. const auto physicalArea = currentNode->display->totalArea.toDouble();
  184. const auto scale = currentNode->display->scale;
  185. if (! currentNode->isRoot)
  186. {
  187. const auto logicalWidth = physicalArea.getWidth() / scale;
  188. const auto logicalHeight = physicalArea.getHeight() / scale;
  189. const auto physicalParentArea = currentNode->parent->display->totalArea.toDouble();
  190. const auto logicalParentArea = currentNode->parent->logicalArea; // logical area of parent has already been calculated
  191. const auto parentScale = currentNode->parent->display->scale;
  192. Rectangle<double> logicalArea (0.0, 0.0, logicalWidth, logicalHeight);
  193. if (physicalArea.getRight() == physicalParentArea.getX()) logicalArea.setPosition ({ logicalParentArea.getX() - logicalWidth, physicalArea.getY() / parentScale }); // on left
  194. else if (physicalArea.getX() == physicalParentArea.getRight()) logicalArea.setPosition ({ logicalParentArea.getRight(), physicalArea.getY() / parentScale }); // on right
  195. else if (physicalArea.getBottom() == physicalParentArea.getY()) logicalArea.setPosition ({ physicalArea.getX() / parentScale, logicalParentArea.getY() - logicalHeight }); // on top
  196. else if (physicalArea.getY() == physicalParentArea.getBottom()) logicalArea.setPosition ({ physicalArea.getX() / parentScale, logicalParentArea.getBottom() }); // on bottom
  197. else jassertfalse;
  198. currentNode->logicalArea = logicalArea;
  199. }
  200. else
  201. {
  202. // If currentNode is the root (position (0, 0)) then we can just scale the physical area
  203. currentNode->logicalArea = physicalArea / scale;
  204. currentNode->parent = currentNode;
  205. }
  206. // Find child nodes
  207. Array<DisplayNode*> children;
  208. for (auto& node : allNodes)
  209. {
  210. // Already calculated
  211. if (node.parent != nullptr)
  212. continue;
  213. const auto otherPhysicalArea = node.display->totalArea.toDouble();
  214. // If the displays are touching on any side
  215. if (otherPhysicalArea.getX() == physicalArea.getRight() || otherPhysicalArea.getRight() == physicalArea.getX()
  216. || otherPhysicalArea.getY() == physicalArea.getBottom() || otherPhysicalArea.getBottom() == physicalArea.getY())
  217. {
  218. node.parent = currentNode;
  219. children.add (&node);
  220. }
  221. }
  222. // Recursively process all child nodes
  223. for (auto child : children)
  224. processDisplay (child, allNodes);
  225. }
  226. /** This is called when the displays Array has been filled out with the info for all connected displays and the
  227. totalArea and userArea Rectangles need to be converted from physical to logical coordinates.
  228. */
  229. void Displays::updateToLogical()
  230. {
  231. if (displays.size() == 1)
  232. {
  233. auto& display = displays.getReference (0);
  234. display.totalArea = (display.totalArea.toDouble() / display.scale).toNearestInt();
  235. display.userArea = (display.userArea.toDouble() / display.scale).toNearestInt();
  236. return;
  237. }
  238. Array<DisplayNode> displayNodes;
  239. for (auto& d : displays)
  240. {
  241. DisplayNode node;
  242. node.display = &d;
  243. if (d.totalArea.getTopLeft() == Point<int>())
  244. node.isRoot = true;
  245. displayNodes.add (node);
  246. }
  247. auto* root = [&displayNodes]() -> DisplayNode*
  248. {
  249. for (auto& node : displayNodes)
  250. if (node.isRoot)
  251. return &node;
  252. auto minDistance = std::numeric_limits<int>::max();
  253. DisplayNode* retVal = nullptr;
  254. for (auto& node : displayNodes)
  255. {
  256. auto distance = node.display->totalArea.getTopLeft().getDistanceFrom ({});
  257. if (distance < minDistance)
  258. {
  259. minDistance = distance;
  260. retVal = &node;
  261. }
  262. }
  263. if (retVal != nullptr)
  264. retVal->isRoot = true;
  265. return retVal;
  266. }();
  267. // Must have a root node!
  268. jassert (root != nullptr);
  269. // Recursively traverse the display graph from the root and work out logical bounds
  270. processDisplay (root, displayNodes);
  271. for (auto& node : displayNodes)
  272. {
  273. // All of the nodes should have a parent
  274. jassert (node.parent != nullptr);
  275. auto relativeUserArea = (node.display->userArea.toDouble() - node.display->totalArea.toDouble().getTopLeft()) / node.display->scale;
  276. // Now set Display::totalArea and ::userArea using the logical area that we have calculated
  277. node.display->topLeftPhysical = node.display->totalArea.getTopLeft();
  278. node.display->totalArea = node.logicalArea.toNearestInt();
  279. node.display->userArea = (relativeUserArea + node.logicalArea.getTopLeft()).toNearestInt();
  280. }
  281. }
  282. #ifndef DOXYGEN
  283. // explicit template instantiations
  284. template Point<int> Displays::physicalToLogical (Point<int>, const Display*) const noexcept;
  285. template Point<float> Displays::physicalToLogical (Point<float>, const Display*) const noexcept;
  286. template Point<int> Displays::logicalToPhysical (Point<int>, const Display*) const noexcept;
  287. template Point<float> Displays::logicalToPhysical (Point<float>, const Display*) const noexcept;
  288. #endif
  289. //==============================================================================
  290. // Deprecated methods
  291. const Displays::Display& Displays::getDisplayContaining (Point<int> position) const noexcept
  292. {
  293. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  294. const auto* best = &displays.getReference (0);
  295. auto bestDistance = std::numeric_limits<int>::max();
  296. for (auto& d : displays)
  297. {
  298. if (d.totalArea.contains (position))
  299. {
  300. best = &d;
  301. break;
  302. }
  303. auto distance = d.totalArea.getCentre().getDistanceFrom (position);
  304. if (distance < bestDistance)
  305. {
  306. bestDistance = distance;
  307. best = &d;
  308. }
  309. }
  310. return *best;
  311. }
  312. const Displays::Display& Displays::findDisplayForRect (Rectangle<int> rect, bool isPhysical) const noexcept
  313. {
  314. if (auto* display = getDisplayForRect (rect, isPhysical))
  315. return *display;
  316. return emptyDisplay;
  317. }
  318. const Displays::Display& Displays::findDisplayForPoint (Point<int> point, bool isPhysical) const noexcept
  319. {
  320. if (auto* display = getDisplayForPoint (point, isPhysical))
  321. return *display;
  322. return emptyDisplay;
  323. }
  324. const Displays::Display& Displays::getMainDisplay() const noexcept
  325. {
  326. if (auto* display = getPrimaryDisplay())
  327. return *display;
  328. return emptyDisplay;
  329. }
  330. } // namespace juce