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_Displays.cpp 14KB

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