OrientationSupport.mm 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "OrientationSupport.h"
  2. #include <math.h>
  3. CGAffineTransform TransformForOrientation(ScreenOrientation orient)
  4. {
  5. switch (orient)
  6. {
  7. case portrait: return CGAffineTransformIdentity;
  8. case portraitUpsideDown: return CGAffineTransformMakeRotation(M_PI);
  9. case landscapeLeft: return CGAffineTransformMakeRotation(M_PI_2);
  10. case landscapeRight: return CGAffineTransformMakeRotation(-M_PI_2);
  11. default: return CGAffineTransformIdentity;
  12. }
  13. return CGAffineTransformIdentity;
  14. }
  15. CGAffineTransform TransformBetweenOrientations(ScreenOrientation fromOrient, ScreenOrientation toOrient)
  16. {
  17. CGAffineTransform fromTransform = TransformForOrientation(fromOrient);
  18. CGAffineTransform toTransform = TransformForOrientation(toOrient);
  19. return CGAffineTransformConcat(CGAffineTransformInvert(fromTransform), toTransform);
  20. }
  21. #if !PLATFORM_TVOS
  22. UIInterfaceOrientation ConvertToIosScreenOrientation(ScreenOrientation orient)
  23. {
  24. switch (orient)
  25. {
  26. case portrait: return UIInterfaceOrientationPortrait;
  27. case portraitUpsideDown: return UIInterfaceOrientationPortraitUpsideDown;
  28. // landscape left/right have switched values in device/screen orientation
  29. // though unity docs are adjusted with device orientation values, so swap here
  30. case landscapeLeft: return UIInterfaceOrientationLandscapeRight;
  31. case landscapeRight: return UIInterfaceOrientationLandscapeLeft;
  32. case orientationUnknown: return (UIInterfaceOrientation)UIInterfaceOrientationUnknown;
  33. default: return UIInterfaceOrientationPortrait;
  34. }
  35. return UIInterfaceOrientationPortrait;
  36. }
  37. ScreenOrientation ConvertToUnityScreenOrientation(UIInterfaceOrientation orient)
  38. {
  39. switch (orient)
  40. {
  41. case UIInterfaceOrientationPortrait: return portrait;
  42. case UIInterfaceOrientationPortraitUpsideDown: return portraitUpsideDown;
  43. // landscape left/right have switched values in device/screen orientation
  44. // though unity docs are adjusted with device orientation values, so swap here
  45. case UIInterfaceOrientationLandscapeLeft: return landscapeRight;
  46. case UIInterfaceOrientationLandscapeRight: return landscapeLeft;
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wswitch"
  49. case UIInterfaceOrientationUnknown: return orientationUnknown;
  50. #pragma clang diagnostic pop
  51. default: return portrait;
  52. }
  53. }
  54. // Replacement for UIViewController.interfaceOrientation which is obsolete since iOS 8.0
  55. UIInterfaceOrientation UIViewControllerInterfaceOrientation(UIViewController* c)
  56. {
  57. // [UIScreen coordinateSpace] is only available on iOS 8+
  58. // [UIViewController interfaceOrientation] is obsolete in iOS 8+
  59. if (!_ios80orNewer)
  60. return c.interfaceOrientation;
  61. CGPoint fixedPoint = [c.view.window.screen.coordinateSpace convertPoint: CGPointMake(0.0, 0.0) toCoordinateSpace: c.view.window.screen.fixedCoordinateSpace];
  62. if (fabs(fixedPoint.x) < FLT_EPSILON)
  63. {
  64. if (fabs(fixedPoint.y) < FLT_EPSILON)
  65. return UIInterfaceOrientationPortrait;
  66. else
  67. return UIInterfaceOrientationLandscapeLeft;
  68. }
  69. else
  70. {
  71. if (fabs(fixedPoint.y) < FLT_EPSILON)
  72. return UIInterfaceOrientationLandscapeRight;
  73. else
  74. return UIInterfaceOrientationPortraitUpsideDown;
  75. }
  76. }
  77. #endif
  78. ScreenOrientation UIViewControllerOrientation(UIViewController* controller)
  79. {
  80. #if PLATFORM_TVOS
  81. return UNITY_TVOS_ORIENTATION;
  82. #else
  83. return ConvertToUnityScreenOrientation(UIViewControllerInterfaceOrientation(controller));
  84. #endif
  85. }
  86. ScreenOrientation OrientationAfterTransform(ScreenOrientation curOrient, CGAffineTransform transform)
  87. {
  88. int rotDeg = (int)::roundf(::atan2f(transform.b, transform.a) * (180 / M_PI));
  89. assert(rotDeg == 0 || rotDeg == 90 || rotDeg == -90 || rotDeg == 180 || rotDeg == -180);
  90. if (rotDeg == 0)
  91. {
  92. return curOrient;
  93. }
  94. else if ((rotDeg == 180) || (rotDeg == -180))
  95. {
  96. if (curOrient == portrait)
  97. return portraitUpsideDown;
  98. else if (curOrient == portraitUpsideDown)
  99. return portrait;
  100. else if (curOrient == landscapeRight)
  101. return landscapeLeft;
  102. else if (curOrient == landscapeLeft)
  103. return landscapeRight;
  104. }
  105. else if (rotDeg == 90)
  106. {
  107. if (curOrient == portrait)
  108. return landscapeLeft;
  109. else if (curOrient == portraitUpsideDown)
  110. return landscapeRight;
  111. else if (curOrient == landscapeRight)
  112. return portrait;
  113. else if (curOrient == landscapeLeft)
  114. return portraitUpsideDown;
  115. }
  116. else if (rotDeg == -90)
  117. {
  118. if (curOrient == portrait)
  119. return landscapeRight;
  120. else if (curOrient == portraitUpsideDown)
  121. return landscapeLeft;
  122. else if (curOrient == landscapeRight)
  123. return portraitUpsideDown;
  124. else if (curOrient == landscapeLeft)
  125. return portrait;
  126. }
  127. ::printf("rotation unhandled: %d\n", rotDeg);
  128. return curOrient;
  129. }
  130. void OrientView(UIViewController* host, UIView* view, ScreenOrientation to)
  131. {
  132. ScreenOrientation fromController = UIViewControllerOrientation(host);
  133. // before ios8 view transform is relative to portrait, while on ios8 it is relative to window/controller
  134. const bool newRotationLogic = _ios80orNewer;
  135. CGAffineTransform transform = newRotationLogic ? TransformBetweenOrientations(fromController, to) : TransformForOrientation(to);
  136. // this is for unity-inited orientation. In that case we need to manually adjust bounds if changing portrait/landscape
  137. // the easiest way would be to manually rotate current bounds (to acknowledge the fact that we do NOT rotate controller itself)
  138. // NB: as we use current view bounds we need to use view transform to properly adjust them
  139. CGRect rect = view.bounds;
  140. CGSize ext = CGSizeApplyAffineTransform(rect.size, CGAffineTransformConcat(CGAffineTransformInvert(view.transform), transform));
  141. view.transform = transform;
  142. view.bounds = CGRectMake(0, 0, ::fabs(ext.width), ::fabs(ext.height));
  143. }