-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathFScreen.cs
More file actions
353 lines (284 loc) · 10.7 KB
/
FScreen.cs
File metadata and controls
353 lines (284 loc) · 10.7 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FScreen
{
public float width; //in points, not pixels
public float height; //in points, not pixels
public float halfWidth; //for convenience, in points
public float halfHeight; //for convenience, in points
public float pixelWidth; //in pixels
public float pixelHeight; //in pixels
public delegate void ScreenOrientationChangeDelegate();
public event ScreenOrientationChangeDelegate SignalOrientationChange;
//the bool in SignalResize represents wasOrientationChange,
//which tells you whether the resize was due to an orientation change or not
public delegate void ScreenResizeDelegate(bool wasResizedDueToOrientationChange);
public event ScreenResizeDelegate SignalResize;
//this is populated by the FutileParams
private float _originX;
private float _originY;
private ScreenOrientation _currentOrientation;
private float _screenLongLength;
private float _screenShortLength;
//if true, the fscreen (pixel) width is unity Screen width, if false fscreen width is Screen height
private bool _fscreenWidthIsScreenWidth;
private bool _didJustResize;
private float _oldWidth;
private float _oldHeight;
private FResolutionLevel _resLevel;
private FutileParams _futileParams;
public FScreen (FutileParams futileParams)
{
_futileParams = futileParams;
#if UNITY_IPHONE || UNITY_ANDROID
#if UNITY_3_5
TouchScreenKeyboard.autorotateToLandscapeLeft = false;
TouchScreenKeyboard.autorotateToLandscapeRight = false;
TouchScreenKeyboard.autorotateToPortrait = false;
TouchScreenKeyboard.autorotateToPortraitUpsideDown = false;
#else
Screen.autorotateToLandscapeLeft = false;
Screen.autorotateToLandscapeRight = false;
Screen.autorotateToPortrait = false;
Screen.autorotateToPortraitUpsideDown = false;
#endif
#endif
//Non-mobile unity always defaults to portrait for some reason, so fix this manually
if(Screen.height > Screen.width)
{
_currentOrientation = ScreenOrientation.Portrait;
}
else
{
_currentOrientation = ScreenOrientation.LandscapeLeft;
}
//get the correct orientation if we're on a mobile platform
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID)
_currentOrientation = Screen.orientation;
#endif
//special "single orientation" mode
if(_futileParams.singleOrientation != ScreenOrientation.Unknown)
{
_currentOrientation = _futileParams.singleOrientation;
}
else //if we're not in a supported orientation, put us in one!
{
if(_currentOrientation == ScreenOrientation.LandscapeLeft && !_futileParams.supportsLandscapeLeft)
{
if(_futileParams.supportsLandscapeRight) _currentOrientation = ScreenOrientation.LandscapeRight;
else if(_futileParams.supportsPortrait) _currentOrientation = ScreenOrientation.Portrait;
else if(_futileParams.supportsPortraitUpsideDown) _currentOrientation = ScreenOrientation.PortraitUpsideDown;
}
else if(_currentOrientation == ScreenOrientation.LandscapeRight && !_futileParams.supportsLandscapeRight)
{
if(_futileParams.supportsLandscapeLeft) _currentOrientation = ScreenOrientation.LandscapeLeft;
else if(_futileParams.supportsPortrait) _currentOrientation = ScreenOrientation.Portrait;
else if(_futileParams.supportsPortraitUpsideDown) _currentOrientation = ScreenOrientation.PortraitUpsideDown;
}
else if(_currentOrientation == ScreenOrientation.Portrait && !_futileParams.supportsPortrait)
{
if(_futileParams.supportsPortraitUpsideDown) _currentOrientation = ScreenOrientation.PortraitUpsideDown;
else if(_futileParams.supportsLandscapeLeft) _currentOrientation = ScreenOrientation.LandscapeLeft;
else if(_futileParams.supportsLandscapeRight) _currentOrientation = ScreenOrientation.LandscapeRight;
}
else if(_currentOrientation == ScreenOrientation.PortraitUpsideDown && !_futileParams.supportsPortraitUpsideDown)
{
if(_futileParams.supportsPortrait) _currentOrientation = ScreenOrientation.Portrait;
else if(_futileParams.supportsLandscapeLeft) _currentOrientation = ScreenOrientation.LandscapeLeft;
else if(_futileParams.supportsLandscapeRight) _currentOrientation = ScreenOrientation.LandscapeRight;
}
}
Screen.orientation = _currentOrientation;
_screenLongLength = Math.Max(Screen.height, Screen.width);
_screenShortLength = Math.Min(Screen.height, Screen.width);
if(_currentOrientation == ScreenOrientation.Portrait || _currentOrientation == ScreenOrientation.PortraitUpsideDown)
{
pixelWidth = _screenShortLength;
pixelHeight = _screenLongLength;
}
else //landscape
{
pixelWidth = _screenLongLength;
pixelHeight = _screenShortLength;
}
_fscreenWidthIsScreenWidth=(pixelWidth==Screen.width);
//get the resolution level - the one we're closest to WITHOUT going over, price is right rules :)
_resLevel = null;
foreach(FResolutionLevel resLevel in _futileParams.resLevels)
{
if(_screenLongLength <= resLevel.maxLength) //we've found our resLevel
{
_resLevel = resLevel;
break;
}
}
//if we couldn't find a res level, it means the screen is bigger than the biggest one, so just choose the biggest
if(_resLevel == null)
{
_resLevel = _futileParams.resLevels.GetLastObject();
if(_resLevel == null)
{
throw new FutileException("You must add at least one FResolutionLevel!");
}
}
Futile.resourceSuffix = _resLevel.resourceSuffix;
//this is what helps us figure out the display scale if we're not at a specific resolution level
//it's relative to the next highest resolution level
float displayScaleModifier = 1.0f;
if(_futileParams.shouldLerpToNearestResolutionLevel)
{
displayScaleModifier = _screenLongLength/_resLevel.maxLength;
}
Futile.displayScale = _resLevel.displayScale * displayScaleModifier;
Futile.displayScaleInverse = 1.0f/Futile.displayScale;
Futile.resourceScale = _resLevel.resourceScale;
Futile.resourceScaleInverse = 1.0f/Futile.resourceScale;
width = pixelWidth*Futile.displayScaleInverse;
height = pixelHeight*Futile.displayScaleInverse;
if(Futile.isOpenGL)
{
Futile.screenPixelOffset = 0;
}
else //directX needs to be offset by half a pixel
{
Futile.screenPixelOffset = 0.5f * Futile.displayScaleInverse;
}
halfWidth = width/2.0f;
halfHeight = height/2.0f;
_originX = _futileParams.origin.x;
_originY = _futileParams.origin.y;
Debug.Log ("Futile: Display scale is " + Futile.displayScale);
Debug.Log ("Futile: Resource scale is " + Futile.resourceScale);
Debug.Log ("Futile: Resource suffix is " + _resLevel.resourceSuffix);
Debug.Log ("FScreen: Screen size in pixels is (" + pixelWidth +"px," + pixelHeight+"px)");
Debug.Log ("FScreen: Screen size in points is (" + width + "," + height+")");
Debug.Log ("FScreen: Origin is at (" + _originX*width + "," + _originY*height+")");
Debug.Log ("FScreen: Initial orientation is " + _currentOrientation);
_didJustResize = true;
}
public void Update()
{
if(Input.deviceOrientation == DeviceOrientation.LandscapeLeft && _currentOrientation != ScreenOrientation.LandscapeLeft && _futileParams.supportsLandscapeLeft)
{
SwitchOrientation(ScreenOrientation.LandscapeLeft);
}
else if(Input.deviceOrientation == DeviceOrientation.LandscapeRight && _currentOrientation != ScreenOrientation.LandscapeRight && _futileParams.supportsLandscapeRight)
{
SwitchOrientation(ScreenOrientation.LandscapeRight);
}
else if(Input.deviceOrientation == DeviceOrientation.Portrait && _currentOrientation != ScreenOrientation.Portrait && _futileParams.supportsPortrait)
{
SwitchOrientation(ScreenOrientation.Portrait);
}
else if(Input.deviceOrientation == DeviceOrientation.PortraitUpsideDown && _currentOrientation != ScreenOrientation.PortraitUpsideDown && _futileParams.supportsPortraitUpsideDown)
{
SwitchOrientation(ScreenOrientation.PortraitUpsideDown);
}
if(_didJustResize)
{
_didJustResize = false;
_oldWidth = Screen.width;
_oldHeight = Screen.height;
}
else if(_oldWidth != Screen.width || _oldHeight != Screen.height)
{
_oldWidth = Screen.width;
_oldHeight = Screen.height;
UpdateScreenDimensions();
if(SignalResize != null) SignalResize(false);
}
}
public void SwitchOrientation (ScreenOrientation newOrientation)
{
Debug.Log("Futile: Orientation changed to " + newOrientation);
if(_futileParams.singleOrientation != ScreenOrientation.Unknown) //if we're in single orientation mode, just broadcast the change, don't actually change anything
{
_currentOrientation = newOrientation;
if(SignalOrientationChange != null) SignalOrientationChange();
}
else
{
Screen.orientation = newOrientation;
_currentOrientation = newOrientation;
UpdateScreenDimensions(true);
Debug.Log ("Orientation switched to " + _currentOrientation + " screen is now: " + pixelWidth+"x"+pixelHeight+"px");
if(SignalOrientationChange != null) SignalOrientationChange();
if(SignalResize != null) SignalResize(true);
_didJustResize = true;
}
}
private void UpdateScreenDimensions(bool orientationChange=false)
{
if (orientationChange) {
_screenLongLength = Math.Max (Screen.width, Screen.height);
_screenShortLength = Math.Min (Screen.width, Screen.height);
if(_currentOrientation == ScreenOrientation.Portrait || _currentOrientation == ScreenOrientation.PortraitUpsideDown)
{
pixelWidth = _screenShortLength;
pixelHeight = _screenLongLength;
}
else //landscape
{
pixelWidth = _screenLongLength;
pixelHeight = _screenShortLength;
}
_fscreenWidthIsScreenWidth=(pixelWidth==Screen.width);
} else {
if (_fscreenWidthIsScreenWidth) {
pixelWidth=Screen.width;
pixelHeight=Screen.height;
} else {
pixelWidth=Screen.height;
pixelHeight=Screen.width;
}
//Check for an orientation change? Not sure it's appropriate to do so
//...
}
width = pixelWidth*Futile.displayScaleInverse;
height = pixelHeight*Futile.displayScaleInverse;
halfWidth = width/2.0f;
halfHeight = height/2.0f;
Futile.instance.UpdateCameraPosition();
}
public float originX
{
get {return _originX;}
set
{
if(_originX != value)
{
_originX = value;
Futile.instance.UpdateCameraPosition();
}
}
}
public float originY
{
get {return _originY;}
set
{
if(_originY != value)
{
_originY = value;
Futile.instance.UpdateCameraPosition();
}
}
}
public ScreenOrientation currentOrientation
{
get {return _currentOrientation;}
set
{
if(_currentOrientation != value)
{
SwitchOrientation(value);
}
}
}
public bool IsLandscape()
{
return _currentOrientation == ScreenOrientation.LandscapeLeft || _currentOrientation == ScreenOrientation.LandscapeRight;
}
}