SDL_uikitviewcontroller.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_UIKIT
  20. #include "SDL_video.h"
  21. #include "SDL_assert.h"
  22. #include "SDL_hints.h"
  23. #include "../SDL_sysvideo.h"
  24. #include "../../events/SDL_events_c.h"
  25. #include "SDL_uikitviewcontroller.h"
  26. #include "SDL_uikitvideo.h"
  27. #include "SDL_uikitmodes.h"
  28. #include "SDL_uikitwindow.h"
  29. @implementation SDL_uikitviewcontroller
  30. @synthesize window;
  31. - (id)initWithSDLWindow:(SDL_Window *)_window
  32. {
  33. if (self = [super initWithNibName:nil bundle:nil]) {
  34. self.window = _window;
  35. }
  36. return self;
  37. }
  38. - (void)loadView
  39. {
  40. /* do nothing. */
  41. }
  42. - (void)viewDidLayoutSubviews
  43. {
  44. const CGSize size = self.view.bounds.size;
  45. int w = (int) size.width;
  46. int h = (int) size.height;
  47. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, w, h);
  48. }
  49. - (NSUInteger)supportedInterfaceOrientations
  50. {
  51. NSUInteger orientationMask = 0;
  52. const char *hint = SDL_GetHint(SDL_HINT_ORIENTATIONS);
  53. if (hint != NULL) {
  54. NSArray *orientations = [@(hint) componentsSeparatedByString:@" "];
  55. if ([orientations containsObject:@"LandscapeLeft"]) {
  56. orientationMask |= UIInterfaceOrientationMaskLandscapeLeft;
  57. }
  58. if ([orientations containsObject:@"LandscapeRight"]) {
  59. orientationMask |= UIInterfaceOrientationMaskLandscapeRight;
  60. }
  61. if ([orientations containsObject:@"Portrait"]) {
  62. orientationMask |= UIInterfaceOrientationMaskPortrait;
  63. }
  64. if ([orientations containsObject:@"PortraitUpsideDown"]) {
  65. orientationMask |= UIInterfaceOrientationMaskPortraitUpsideDown;
  66. }
  67. }
  68. if (orientationMask == 0 && (window->flags & SDL_WINDOW_RESIZABLE)) {
  69. orientationMask = UIInterfaceOrientationMaskAll; /* any orientation is okay. */
  70. }
  71. if (orientationMask == 0) {
  72. if (window->w >= window->h) {
  73. orientationMask |= UIInterfaceOrientationMaskLandscape;
  74. }
  75. if (window->h >= window->w) {
  76. orientationMask |= (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
  77. }
  78. }
  79. /* Don't allow upside-down orientation on the phone, so answering calls is in the natural orientation */
  80. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
  81. orientationMask &= ~UIInterfaceOrientationMaskPortraitUpsideDown;
  82. }
  83. return orientationMask;
  84. }
  85. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orient
  86. {
  87. NSUInteger orientationMask = [self supportedInterfaceOrientations];
  88. return (orientationMask & (1 << orient));
  89. }
  90. - (BOOL)prefersStatusBarHidden
  91. {
  92. if (window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_BORDERLESS)) {
  93. return YES;
  94. } else {
  95. return NO;
  96. }
  97. }
  98. - (UIStatusBarStyle)preferredStatusBarStyle
  99. {
  100. /* We assume most games don't have a bright white background. */
  101. return UIStatusBarStyleLightContent;
  102. }
  103. @end
  104. #endif /* SDL_VIDEO_DRIVER_UIKIT */
  105. /* vi: set ts=4 sw=4 expandtab: */