OrientationSupport.mm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. CGPoint fixedPoint = [c.view.window.screen.coordinateSpace convertPoint: CGPointMake(0.0, 0.0) toCoordinateSpace: c.view.window.screen.fixedCoordinateSpace];
  58. if (fabs(fixedPoint.x) < FLT_EPSILON)
  59. {
  60. if (fabs(fixedPoint.y) < FLT_EPSILON)
  61. return UIInterfaceOrientationPortrait;
  62. else
  63. return UIInterfaceOrientationLandscapeLeft;
  64. }
  65. else
  66. {
  67. if (fabs(fixedPoint.y) < FLT_EPSILON)
  68. return UIInterfaceOrientationLandscapeRight;
  69. else
  70. return UIInterfaceOrientationPortraitUpsideDown;
  71. }
  72. }
  73. #endif
  74. ScreenOrientation UIViewControllerOrientation(UIViewController* controller)
  75. {
  76. #if PLATFORM_TVOS
  77. return UNITY_TVOS_ORIENTATION;
  78. #else
  79. return ConvertToUnityScreenOrientation(UIViewControllerInterfaceOrientation(controller));
  80. #endif
  81. }
  82. ScreenOrientation OrientationAfterTransform(ScreenOrientation curOrient, CGAffineTransform transform)
  83. {
  84. int rotDeg = (int)::roundf(::atan2f(transform.b, transform.a) * (180 / M_PI));
  85. assert(rotDeg == 0 || rotDeg == 90 || rotDeg == -90 || rotDeg == 180 || rotDeg == -180);
  86. if (rotDeg == 0)
  87. {
  88. return curOrient;
  89. }
  90. else if ((rotDeg == 180) || (rotDeg == -180))
  91. {
  92. if (curOrient == portrait)
  93. return portraitUpsideDown;
  94. else if (curOrient == portraitUpsideDown)
  95. return portrait;
  96. else if (curOrient == landscapeRight)
  97. return landscapeLeft;
  98. else if (curOrient == landscapeLeft)
  99. return landscapeRight;
  100. }
  101. else if (rotDeg == 90)
  102. {
  103. if (curOrient == portrait)
  104. return landscapeLeft;
  105. else if (curOrient == portraitUpsideDown)
  106. return landscapeRight;
  107. else if (curOrient == landscapeRight)
  108. return portrait;
  109. else if (curOrient == landscapeLeft)
  110. return portraitUpsideDown;
  111. }
  112. else if (rotDeg == -90)
  113. {
  114. if (curOrient == portrait)
  115. return landscapeRight;
  116. else if (curOrient == portraitUpsideDown)
  117. return landscapeLeft;
  118. else if (curOrient == landscapeRight)
  119. return portraitUpsideDown;
  120. else if (curOrient == landscapeLeft)
  121. return portrait;
  122. }
  123. ::printf("rotation unhandled: %d\n", rotDeg);
  124. return curOrient;
  125. }
  126. void OrientView(UIViewController* host, UIView* view, ScreenOrientation to)
  127. {
  128. ScreenOrientation fromController = UIViewControllerOrientation(host);
  129. CGAffineTransform transform = TransformBetweenOrientations(fromController, to);
  130. // this is for unity-inited orientation. In that case we need to manually adjust bounds if changing portrait/landscape
  131. // the easiest way would be to manually rotate current bounds (to acknowledge the fact that we do NOT rotate controller itself)
  132. // NB: as we use current view bounds we need to use view transform to properly adjust them
  133. CGRect rect = view.bounds;
  134. CGSize ext = CGSizeApplyAffineTransform(rect.size, CGAffineTransformConcat(CGAffineTransformInvert(view.transform), transform));
  135. view.transform = transform;
  136. view.bounds = CGRectMake(0, 0, ::fabs(ext.width), ::fabs(ext.height));
  137. }