OrientationSupport.mm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #if !PLATFORM_VISIONOS
  58. CGPoint fixedPoint = [c.view.window.screen.coordinateSpace convertPoint: CGPointMake(0.0, 0.0) toCoordinateSpace: c.view.window.screen.fixedCoordinateSpace];
  59. if (fabs(fixedPoint.x) < FLT_EPSILON)
  60. {
  61. if (fabs(fixedPoint.y) < FLT_EPSILON)
  62. return UIInterfaceOrientationPortrait;
  63. else
  64. return UIInterfaceOrientationLandscapeLeft;
  65. }
  66. else
  67. {
  68. if (fabs(fixedPoint.y) < FLT_EPSILON)
  69. return UIInterfaceOrientationLandscapeRight;
  70. else
  71. return UIInterfaceOrientationPortraitUpsideDown;
  72. }
  73. #else
  74. return UIInterfaceOrientationLandscapeLeft;
  75. #endif
  76. }
  77. #endif
  78. ScreenOrientation UIViewControllerOrientation(UIViewController* controller)
  79. {
  80. #if PLATFORM_TVOS
  81. return UNITY_TVOS_ORIENTATION;
  82. #elif PLATFORM_VISIONOS
  83. return UNITY_VISIONOS_ORIENTATION;
  84. #else
  85. return ConvertToUnityScreenOrientation(UIViewControllerInterfaceOrientation(controller));
  86. #endif
  87. }
  88. ScreenOrientation OrientationAfterTransform(ScreenOrientation curOrient, CGAffineTransform transform)
  89. {
  90. int rotDeg = (int)::roundf(::atan2f(transform.b, transform.a) * (180 / M_PI));
  91. assert(rotDeg == 0 || rotDeg == 90 || rotDeg == -90 || rotDeg == 180 || rotDeg == -180);
  92. if (rotDeg == 0)
  93. {
  94. return curOrient;
  95. }
  96. else if ((rotDeg == 180) || (rotDeg == -180))
  97. {
  98. if (curOrient == portrait)
  99. return portraitUpsideDown;
  100. else if (curOrient == portraitUpsideDown)
  101. return portrait;
  102. else if (curOrient == landscapeRight)
  103. return landscapeLeft;
  104. else if (curOrient == landscapeLeft)
  105. return landscapeRight;
  106. }
  107. else if (rotDeg == 90)
  108. {
  109. if (curOrient == portrait)
  110. return landscapeLeft;
  111. else if (curOrient == portraitUpsideDown)
  112. return landscapeRight;
  113. else if (curOrient == landscapeRight)
  114. return portrait;
  115. else if (curOrient == landscapeLeft)
  116. return portraitUpsideDown;
  117. }
  118. else if (rotDeg == -90)
  119. {
  120. if (curOrient == portrait)
  121. return landscapeRight;
  122. else if (curOrient == portraitUpsideDown)
  123. return landscapeLeft;
  124. else if (curOrient == landscapeRight)
  125. return portraitUpsideDown;
  126. else if (curOrient == landscapeLeft)
  127. return portrait;
  128. }
  129. ::printf("rotation unhandled: %d\n", rotDeg);
  130. return curOrient;
  131. }
  132. void OrientView(UIViewController* host, UIView* view, ScreenOrientation to)
  133. {
  134. ScreenOrientation fromController = UIViewControllerOrientation(host);
  135. CGAffineTransform transform = TransformBetweenOrientations(fromController, 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. }