1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include "UnityPrefix.h"
#include "ScreenManager.h"
#include "Runtime/Shaders/GraphicsCaps.h"
#include "Runtime/GfxDevice/GfxDevice.h"
#include "Runtime/GfxDevice/GfxDeviceSetup.h"
static ScreenManagerPlatform* gScreenManager = NULL;
#if UNITY_IPHONE
extern "C" void NotifyAutoOrientationChange();
#endif
void InitScreenManager()
{
Assert(gScreenManager == NULL);
gScreenManager = new ScreenManagerPlatform();
}
void ReleaseScreenManager()
{
Assert(gScreenManager != NULL);
delete gScreenManager;
gScreenManager = NULL;
}
ScreenManagerPlatform &GetScreenManager()
{
Assert(gScreenManager != NULL);
return *gScreenManager;
}
ScreenManagerPlatform* GetScreenManagerPtr()
{
return gScreenManager;
}
ScreenManager::ScreenManager ()
{
m_Width = 0;
m_Height = 0;
m_SwitchResolutionCallback = NULL;
m_CursorInWindow = false;
m_IsFullscreen = false;
m_AllowCursorHide = true;
m_AllowCursorLock = true;
m_ScreenOrientation = kPortrait;
m_RequestedOrientation = kScreenOrientationUnknown;
// Do not allow to autorotate by default.
m_EnabledOrientations = 0;
}
void ScreenManager::RequestResolution (int width, int height, bool fullscreen, int preferredRefreshRate)
{
m_ResolutionRequest.width = width;
m_ResolutionRequest.height = height;
m_ResolutionRequest.fullScreen = fullscreen ? 1 : 0;
m_ResolutionRequest.refreshRate = preferredRefreshRate;
}
void ScreenManager::RequestSetFullscreen (bool fullscreen)
{
m_ResolutionRequest.fullScreen = fullscreen ? 1 : 0;
}
bool ScreenManager::HasFullscreenRequested () const
{
return m_ResolutionRequest.fullScreen == 1;
}
void ScreenManager::SetCursorInsideWindow (bool insideWindow)
{
m_CursorInWindow = insideWindow;
SetShowCursor(GetShowCursor());
}
int ScreenManager::FindClosestResolution (const ScreenManager::Resolutions& resolutions, int width, int height) const
{
if (resolutions.empty ())
return -1;
int maxDistance = std::numeric_limits<int>::max ();
int index = 0;
for (int i=0;i<resolutions.size ();i++)
{
int curWidth = resolutions[i].width;
int curHeight = resolutions[i].height;
int distance = Abs (width - curWidth) + Abs (height - curHeight);
if (distance < maxDistance)
{
index = i;
maxDistance = distance;
}
}
return index;
}
void ScreenManager::RegisterDidSwitchResolutions (DidSwitchResolutions* resolution)
{
m_SwitchResolutionCallback = resolution;
}
void ScreenManager::SetRequestedResolution ()
{
if (m_ResolutionRequest.width != -1 && m_ResolutionRequest.height != -1)
{
SetResolutionImmediate (m_ResolutionRequest.width, m_ResolutionRequest.height, m_ResolutionRequest.IsFullScreen(), m_ResolutionRequest.refreshRate);
m_ResolutionRequest.Reset();
}
if (m_ResolutionRequest.fullScreen != -1)
{
SetIsFullScreenImmediate (m_ResolutionRequest.fullScreen ? true : false);
m_ResolutionRequest.fullScreen = -1;
}
}
void ScreenManager::SetIsFullScreenImmediate (bool fullscreen)
{
if (fullscreen != IsFullScreen())
SetResolutionImmediate (GetWidth (), GetHeight (), fullscreen, 0);
}
ScreenManager::Resolution ScreenManager::GetCurrentResolution() const
{
Resolution res;
res.width = GetWidth();
res.height = GetHeight();
res.refreshRate = 0;
return res; //@TODO
}
void ScreenManager::SetupScreenManagerEditor( float w, float h )
{
m_Width = RoundfToIntPos(w);
m_Height = RoundfToIntPos(h);
}
void ScreenManager::SetAllowCursorHide (bool allowHide)
{
m_AllowCursorHide = allowHide;
if (!m_AllowCursorHide)
SetShowCursor(true);
}
void ScreenManager::SetAllowCursorLock (bool allowLock)
{
m_AllowCursorLock = allowLock;
if (!m_AllowCursorLock)
SetLockCursor(false);
}
void ScreenManager::SetIsOrientationEnabled(EnabledOrientation orientation, bool enabled)
{
#if UNITY_IPHONE
NotifyAutoOrientationChange();
#endif
#if UNITY_WP8
// upside down portrait is not available on wp8
if (orientation == EnabledOrientation::kAutorotateToPortraitUpsideDown)
enabled = false;
#endif
if (enabled)
m_EnabledOrientations |= orientation;
else
m_EnabledOrientations &= ~orientation;
}
#include "Runtime/Misc/PlayerSettings.h"
void ScreenManager::EnableOrientationsFromPlayerSettings()
{
SetIsOrientationEnabled(kAutorotateToPortrait, GetPlayerSettings().GetAutoRotationAllowed(0));
SetIsOrientationEnabled(kAutorotateToPortraitUpsideDown, GetPlayerSettings().GetAutoRotationAllowed(1));
SetIsOrientationEnabled(kAutorotateToLandscapeRight, GetPlayerSettings().GetAutoRotationAllowed(2));
SetIsOrientationEnabled(kAutorotateToLandscapeLeft, GetPlayerSettings().GetAutoRotationAllowed(3));
}
#define INIT_ORIENT_FROM_PLAYER_SETTINGS_IMPL(name, func) \
void ScreenManager::name(int playerSettingsOrient) \
{ \
switch(playerSettingsOrient) \
{ \
case 0: func(kPortrait); break; \
case 1: func(kPortraitUpsideDown); break; \
case 2: func(kLandscapeRight); break; \
case 3: func(kLandscapeLeft); break; \
\
default: Assert(false && #name "do not accept autorotation."); \
} \
} \
INIT_ORIENT_FROM_PLAYER_SETTINGS_IMPL(SetConcreteOrientationFromPlayerSettings, SetScreenOrientation);
INIT_ORIENT_FROM_PLAYER_SETTINGS_IMPL(RequestConcreteOrientationFromPlayerSettings, RequestOrientation);
|