JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_ScalingHelpers.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce::detail
27{
28
30{
31 template <typename PointOrRect>
32 static PointOrRect unscaledScreenPosToScaled (float scale, PointOrRect pos) noexcept
33 {
34 return ! approximatelyEqual (scale, 1.0f) ? pos / scale : pos;
35 }
36
37 template <typename PointOrRect>
38 static PointOrRect scaledScreenPosToUnscaled (float scale, PointOrRect pos) noexcept
39 {
40 return ! approximatelyEqual (scale, 1.0f) ? pos * scale : pos;
41 }
42
43 // For these, we need to avoid getSmallestIntegerContainer being used, which causes
44 // judder when moving windows
45 static Rectangle<int> unscaledScreenPosToScaled (float scale, Rectangle<int> pos) noexcept
46 {
47 return ! approximatelyEqual (scale, 1.0f) ? Rectangle<int> (roundToInt ((float) pos.getX() / scale),
48 roundToInt ((float) pos.getY() / scale),
49 roundToInt ((float) pos.getWidth() / scale),
50 roundToInt ((float) pos.getHeight() / scale)) : pos;
51 }
52
53 static Rectangle<int> scaledScreenPosToUnscaled (float scale, Rectangle<int> pos) noexcept
54 {
55 return ! approximatelyEqual (scale, 1.0f) ? Rectangle<int> (roundToInt ((float) pos.getX() * scale),
56 roundToInt ((float) pos.getY() * scale),
57 roundToInt ((float) pos.getWidth() * scale),
58 roundToInt ((float) pos.getHeight() * scale)) : pos;
59 }
60
61 static Rectangle<float> unscaledScreenPosToScaled (float scale, Rectangle<float> pos) noexcept
62 {
63 return ! approximatelyEqual (scale, 1.0f) ? Rectangle<float> (pos.getX() / scale,
64 pos.getY() / scale,
65 pos.getWidth() / scale,
66 pos.getHeight() / scale) : pos;
67 }
68
69 static Rectangle<float> scaledScreenPosToUnscaled (float scale, Rectangle<float> pos) noexcept
70 {
71 return ! approximatelyEqual (scale, 1.0f) ? Rectangle<float> (pos.getX() * scale,
72 pos.getY() * scale,
73 pos.getWidth() * scale,
74 pos.getHeight() * scale) : pos;
75 }
76
77 template <typename PointOrRect>
78 static PointOrRect unscaledScreenPosToScaled (PointOrRect pos) noexcept
79 {
80 return unscaledScreenPosToScaled (Desktop::getInstance().getGlobalScaleFactor(), pos);
81 }
82
83 template <typename PointOrRect>
84 static PointOrRect scaledScreenPosToUnscaled (PointOrRect pos) noexcept
85 {
86 return scaledScreenPosToUnscaled (Desktop::getInstance().getGlobalScaleFactor(), pos);
87 }
88
89 template <typename PointOrRect>
90 static PointOrRect unscaledScreenPosToScaled (const Component& comp, PointOrRect pos) noexcept
91 {
92 return unscaledScreenPosToScaled (comp.getDesktopScaleFactor(), pos);
93 }
94
95 template <typename PointOrRect>
96 static PointOrRect scaledScreenPosToUnscaled (const Component& comp, PointOrRect pos) noexcept
97 {
98 return scaledScreenPosToUnscaled (comp.getDesktopScaleFactor(), pos);
99 }
100
101 static Point<int> addPosition (Point<int> p, const Component& c) noexcept { return p + c.getPosition(); }
102 static Rectangle<int> addPosition (Rectangle<int> p, const Component& c) noexcept { return p + c.getPosition(); }
103 static Point<float> addPosition (Point<float> p, const Component& c) noexcept { return p + c.getPosition().toFloat(); }
104 static Rectangle<float> addPosition (Rectangle<float> p, const Component& c) noexcept { return p + c.getPosition().toFloat(); }
105 static Point<int> subtractPosition (Point<int> p, const Component& c) noexcept { return p - c.getPosition(); }
106 static Rectangle<int> subtractPosition (Rectangle<int> p, const Component& c) noexcept { return p - c.getPosition(); }
107 static Point<float> subtractPosition (Point<float> p, const Component& c) noexcept { return p - c.getPosition().toFloat(); }
108 static Rectangle<float> subtractPosition (Rectangle<float> p, const Component& c) noexcept { return p - c.getPosition().toFloat(); }
109
110 static Point<float> screenPosToLocalPos (Component& comp, Point<float> pos)
111 {
112 if (auto* peer = comp.getPeer())
113 {
114 pos = peer->globalToLocal (pos);
115 auto& peerComp = peer->getComponent();
116 return comp.getLocalPoint (&peerComp, unscaledScreenPosToScaled (peerComp, pos));
117 }
118
119 return comp.getLocalPoint (nullptr, unscaledScreenPosToScaled (comp, pos));
120 }
121};
122
123} // namespace juce::detail
The base class for all JUCE user-interface objects.
Point< int > getPosition() const noexcept
Returns the component's top-left position as a Point.
Point< int > getLocalPoint(const Component *sourceComponent, Point< int > pointRelativeToSourceComponent) const
Converts a point to be relative to this component's coordinate space.
ComponentPeer * getPeer() const
Returns the heavyweight window that contains this component.
virtual float getDesktopScaleFactor() const
Returns the default scale factor to use for this component when it is placed on the desktop.
static Desktop &JUCE_CALLTYPE getInstance()
There's only one desktop object, and this method will return it.
A pair of (x, y) coordinates.
Definition juce_Point.h:42
constexpr Point< float > toFloat() const noexcept
Casts this point to a Point<float> object.
Definition juce_Point.h:239
Manages a rectangle and allows geometric operations to be performed on it.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.