gl_view.mm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*************************************************************************/
  2. /* gl_view.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #import "gl_view.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/project_settings.h"
  33. #include "os_iphone.h"
  34. #include "servers/audio_server.h"
  35. #import <OpenGLES/EAGLDrawable.h>
  36. #import <QuartzCore/QuartzCore.h>
  37. /*
  38. @interface GLView (private)
  39. - (id)initGLES;
  40. - (BOOL)createFramebuffer;
  41. - (void)destroyFramebuffer;
  42. @end
  43. */
  44. bool gles3_available = true;
  45. int gl_view_base_fb;
  46. static String keyboard_text;
  47. static GLView *_instance = NULL;
  48. static bool video_found_error = false;
  49. static bool video_playing = false;
  50. static CMTime video_current_time;
  51. void _show_keyboard(String);
  52. void _hide_keyboard();
  53. bool _play_video(String, float, String, String);
  54. bool _is_video_playing();
  55. void _pause_video();
  56. void _focus_out_video();
  57. void _unpause_video();
  58. void _stop_video();
  59. CGFloat _points_to_pixels(CGFloat);
  60. void _show_keyboard(String p_existing) {
  61. keyboard_text = p_existing;
  62. printf("instance on show is %p\n", _instance);
  63. [_instance open_keyboard];
  64. };
  65. void _hide_keyboard() {
  66. printf("instance on hide is %p\n", _instance);
  67. [_instance hide_keyboard];
  68. keyboard_text = "";
  69. };
  70. Rect2 _get_ios_window_safe_area(float p_window_width, float p_window_height) {
  71. UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
  72. if (_instance != nil && [_instance respondsToSelector:@selector(safeAreaInsets)]) {
  73. insets = [_instance safeAreaInsets];
  74. }
  75. ERR_FAIL_COND_V(insets.left < 0 || insets.top < 0 || insets.right < 0 || insets.bottom < 0,
  76. Rect2(0, 0, p_window_width, p_window_height));
  77. UIEdgeInsets window_insets = UIEdgeInsetsMake(_points_to_pixels(insets.top), _points_to_pixels(insets.left), _points_to_pixels(insets.bottom), _points_to_pixels(insets.right));
  78. return Rect2(window_insets.left, window_insets.top, p_window_width - window_insets.right - window_insets.left, p_window_height - window_insets.bottom - window_insets.top);
  79. }
  80. bool _play_video(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
  81. p_path = ProjectSettings::get_singleton()->globalize_path(p_path);
  82. NSString *file_path = [[[NSString alloc] initWithUTF8String:p_path.utf8().get_data()] autorelease];
  83. _instance.avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:file_path]];
  84. _instance.avPlayerItem = [[AVPlayerItem alloc] initWithAsset:_instance.avAsset];
  85. [_instance.avPlayerItem addObserver:_instance forKeyPath:@"status" options:0 context:nil];
  86. _instance.avPlayer = [[AVPlayer alloc] initWithPlayerItem:_instance.avPlayerItem];
  87. _instance.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:_instance.avPlayer];
  88. [_instance.avPlayer addObserver:_instance forKeyPath:@"status" options:0 context:nil];
  89. [[NSNotificationCenter defaultCenter]
  90. addObserver:_instance
  91. selector:@selector(playerItemDidReachEnd:)
  92. name:AVPlayerItemDidPlayToEndTimeNotification
  93. object:[_instance.avPlayer currentItem]];
  94. [_instance.avPlayer addObserver:_instance forKeyPath:@"rate" options:NSKeyValueObservingOptionNew context:0];
  95. [_instance.avPlayerLayer setFrame:_instance.bounds];
  96. [_instance.layer addSublayer:_instance.avPlayerLayer];
  97. [_instance.avPlayer play];
  98. AVMediaSelectionGroup *audioGroup = [_instance.avAsset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
  99. NSMutableArray *allAudioParams = [NSMutableArray array];
  100. for (id track in audioGroup.options) {
  101. NSString *language = [[track locale] localeIdentifier];
  102. NSLog(@"subtitle lang: %@", language);
  103. if ([language isEqualToString:[NSString stringWithUTF8String:p_audio_track.utf8()]]) {
  104. AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
  105. [audioInputParams setVolume:p_volume atTime:kCMTimeZero];
  106. [audioInputParams setTrackID:[track trackID]];
  107. [allAudioParams addObject:audioInputParams];
  108. AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
  109. [audioMix setInputParameters:allAudioParams];
  110. [_instance.avPlayer.currentItem selectMediaOption:track inMediaSelectionGroup:audioGroup];
  111. [_instance.avPlayer.currentItem setAudioMix:audioMix];
  112. break;
  113. }
  114. }
  115. AVMediaSelectionGroup *subtitlesGroup = [_instance.avAsset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicLegible];
  116. NSArray *useableTracks = [AVMediaSelectionGroup mediaSelectionOptionsFromArray:subtitlesGroup.options withoutMediaCharacteristics:[NSArray arrayWithObject:AVMediaCharacteristicContainsOnlyForcedSubtitles]];
  117. for (id track in useableTracks) {
  118. NSString *language = [[track locale] localeIdentifier];
  119. NSLog(@"subtitle lang: %@", language);
  120. if ([language isEqualToString:[NSString stringWithUTF8String:p_subtitle_track.utf8()]]) {
  121. [_instance.avPlayer.currentItem selectMediaOption:track inMediaSelectionGroup:subtitlesGroup];
  122. break;
  123. }
  124. }
  125. video_playing = true;
  126. return true;
  127. }
  128. bool _is_video_playing() {
  129. if (_instance.avPlayer.error) {
  130. printf("Error during playback\n");
  131. }
  132. return (_instance.avPlayer.rate > 0 && !_instance.avPlayer.error);
  133. }
  134. void _pause_video() {
  135. video_current_time = _instance.avPlayer.currentTime;
  136. [_instance.avPlayer pause];
  137. video_playing = false;
  138. }
  139. void _focus_out_video() {
  140. printf("focus out pausing video\n");
  141. [_instance.avPlayer pause];
  142. };
  143. void _unpause_video() {
  144. [_instance.avPlayer play];
  145. video_playing = true;
  146. };
  147. void _stop_video() {
  148. [_instance.avPlayer pause];
  149. [_instance.avPlayerLayer removeFromSuperlayer];
  150. _instance.avPlayer = nil;
  151. video_playing = false;
  152. }
  153. CGFloat _points_to_pixels(CGFloat points) {
  154. float pixelPerInch;
  155. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  156. pixelPerInch = 132;
  157. } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  158. pixelPerInch = 163;
  159. } else {
  160. pixelPerInch = 160;
  161. }
  162. CGFloat pointsPerInch = 72.0;
  163. return (points / pointsPerInch * pixelPerInch);
  164. }
  165. @implementation GLView
  166. @synthesize animationInterval;
  167. static const int max_touches = 8;
  168. static UITouch *touches[max_touches];
  169. static void init_touches() {
  170. for (int i = 0; i < max_touches; i++) {
  171. touches[i] = NULL;
  172. };
  173. };
  174. static int get_touch_id(UITouch *p_touch) {
  175. int first = -1;
  176. for (int i = 0; i < max_touches; i++) {
  177. if (first == -1 && touches[i] == NULL) {
  178. first = i;
  179. continue;
  180. };
  181. if (touches[i] == p_touch)
  182. return i;
  183. };
  184. if (first != -1) {
  185. touches[first] = p_touch;
  186. return first;
  187. };
  188. return -1;
  189. };
  190. static int remove_touch(UITouch *p_touch) {
  191. int remaining = 0;
  192. for (int i = 0; i < max_touches; i++) {
  193. if (touches[i] == NULL)
  194. continue;
  195. if (touches[i] == p_touch)
  196. touches[i] = NULL;
  197. else
  198. ++remaining;
  199. };
  200. return remaining;
  201. };
  202. static void clear_touches() {
  203. for (int i = 0; i < max_touches; i++) {
  204. touches[i] = NULL;
  205. };
  206. };
  207. // Implement this to override the default layer class (which is [CALayer class]).
  208. // We do this so that our view will be backed by a layer that is capable of OpenGL ES rendering.
  209. + (Class)layerClass {
  210. return [CAEAGLLayer class];
  211. }
  212. //The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
  213. - (id)initWithCoder:(NSCoder *)coder {
  214. active = FALSE;
  215. if ((self = [super initWithCoder:coder])) {
  216. self = [self initGLES];
  217. }
  218. return self;
  219. }
  220. - (id)initGLES {
  221. // Get our backing layer
  222. CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
  223. // Configure it so that it is opaque, does not retain the contents of the backbuffer when displayed, and uses RGBA8888 color.
  224. eaglLayer.opaque = YES;
  225. eaglLayer.drawableProperties = [NSDictionary
  226. dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE],
  227. kEAGLDrawablePropertyRetainedBacking,
  228. kEAGLColorFormatRGBA8,
  229. kEAGLDrawablePropertyColorFormat,
  230. nil];
  231. // Create our EAGLContext, and if successful make it current and create our framebuffer.
  232. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
  233. if (!context || ![EAGLContext setCurrentContext:context] || ![self createFramebuffer]) {
  234. context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  235. gles3_available = false;
  236. if (!context || ![EAGLContext setCurrentContext:context] || ![self createFramebuffer]) {
  237. [self release];
  238. return nil;
  239. }
  240. }
  241. // Default the animation interval to 1/60th of a second.
  242. animationInterval = 1.0 / 60.0;
  243. return self;
  244. }
  245. - (id<GLViewDelegate>)delegate {
  246. return delegate;
  247. }
  248. // Update the delegate, and if it needs a -setupView: call, set our internal flag so that it will be called.
  249. - (void)setDelegate:(id<GLViewDelegate>)d {
  250. delegate = d;
  251. delegateSetup = ![delegate respondsToSelector:@selector(setupView:)];
  252. }
  253. @synthesize useCADisplayLink;
  254. // If our view is resized, we'll be asked to layout subviews.
  255. // This is the perfect opportunity to also update the framebuffer so that it is
  256. // the same size as our display area.
  257. - (void)layoutSubviews {
  258. //printf("HERE\n");
  259. [EAGLContext setCurrentContext:context];
  260. [self destroyFramebuffer];
  261. [self createFramebuffer];
  262. [self drawView];
  263. [self drawView];
  264. }
  265. - (BOOL)createFramebuffer {
  266. // Generate IDs for a framebuffer object and a color renderbuffer
  267. UIScreen *mainscr = [UIScreen mainScreen];
  268. printf("******** screen size %i, %i\n", (int)mainscr.currentMode.size.width, (int)mainscr.currentMode.size.height);
  269. self.contentScaleFactor = mainscr.nativeScale;
  270. glGenFramebuffersOES(1, &viewFramebuffer);
  271. glGenRenderbuffersOES(1, &viewRenderbuffer);
  272. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  273. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  274. // This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
  275. // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
  276. [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
  277. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  278. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  279. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  280. // For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer.
  281. glGenRenderbuffersOES(1, &depthRenderbuffer);
  282. glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  283. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
  284. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
  285. if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
  286. NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  287. return NO;
  288. }
  289. if (OS::get_singleton()) {
  290. OS::VideoMode vm;
  291. vm.fullscreen = true;
  292. vm.width = backingWidth;
  293. vm.height = backingHeight;
  294. vm.resizable = false;
  295. OS::get_singleton()->set_video_mode(vm);
  296. OSIPhone::get_singleton()->set_base_framebuffer(viewFramebuffer);
  297. };
  298. gl_view_base_fb = viewFramebuffer;
  299. return YES;
  300. }
  301. // Clean up any buffers we have allocated.
  302. - (void)destroyFramebuffer {
  303. glDeleteFramebuffersOES(1, &viewFramebuffer);
  304. viewFramebuffer = 0;
  305. glDeleteRenderbuffersOES(1, &viewRenderbuffer);
  306. viewRenderbuffer = 0;
  307. if (depthRenderbuffer) {
  308. glDeleteRenderbuffersOES(1, &depthRenderbuffer);
  309. depthRenderbuffer = 0;
  310. }
  311. }
  312. - (void)startAnimation {
  313. if (active)
  314. return;
  315. active = TRUE;
  316. printf("start animation!\n");
  317. if (useCADisplayLink) {
  318. // Approximate frame rate
  319. // assumes device refreshes at 60 fps
  320. int frameInterval = (int)floor(animationInterval * 60.0f);
  321. displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
  322. [displayLink setFrameInterval:frameInterval];
  323. // Setup DisplayLink in main thread
  324. [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
  325. } else {
  326. animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
  327. }
  328. if (video_playing) {
  329. _unpause_video();
  330. }
  331. }
  332. - (void)stopAnimation {
  333. if (!active)
  334. return;
  335. active = FALSE;
  336. printf("******** stop animation!\n");
  337. if (useCADisplayLink) {
  338. [displayLink invalidate];
  339. displayLink = nil;
  340. } else {
  341. [animationTimer invalidate];
  342. animationTimer = nil;
  343. }
  344. clear_touches();
  345. if (video_playing) {
  346. // save position
  347. }
  348. }
  349. - (void)setAnimationInterval:(NSTimeInterval)interval {
  350. animationInterval = interval;
  351. if ((useCADisplayLink && displayLink) || (!useCADisplayLink && animationTimer)) {
  352. [self stopAnimation];
  353. [self startAnimation];
  354. }
  355. }
  356. // Updates the OpenGL view when the timer fires
  357. - (void)drawView {
  358. if (useCADisplayLink) {
  359. // Pause the CADisplayLink to avoid recursion
  360. [displayLink setPaused:YES];
  361. // Process all input events
  362. while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, TRUE) == kCFRunLoopRunHandledSource)
  363. ;
  364. // We are good to go, resume the CADisplayLink
  365. [displayLink setPaused:NO];
  366. }
  367. if (!active) {
  368. printf("draw view not active!\n");
  369. return;
  370. };
  371. // Make sure that you are drawing to the current context
  372. [EAGLContext setCurrentContext:context];
  373. // If our drawing delegate needs to have the view setup, then call -setupView: and flag that it won't need to be called again.
  374. if (!delegateSetup) {
  375. [delegate setupView:self];
  376. delegateSetup = YES;
  377. }
  378. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  379. [delegate drawView:self];
  380. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  381. [context presentRenderbuffer:GL_RENDERBUFFER_OES];
  382. #ifdef DEBUG_ENABLED
  383. GLenum err = glGetError();
  384. if (err)
  385. NSLog(@"%x error", err);
  386. #endif
  387. }
  388. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  389. NSArray *tlist = [[event allTouches] allObjects];
  390. for (unsigned int i = 0; i < [tlist count]; i++) {
  391. if ([touches containsObject:[tlist objectAtIndex:i]]) {
  392. UITouch *touch = [tlist objectAtIndex:i];
  393. if (touch.phase != UITouchPhaseBegan)
  394. continue;
  395. int tid = get_touch_id(touch);
  396. ERR_FAIL_COND(tid == -1);
  397. CGPoint touchPoint = [touch locationInView:self];
  398. OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, true, touch.tapCount > 1);
  399. };
  400. };
  401. }
  402. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  403. NSArray *tlist = [[event allTouches] allObjects];
  404. for (unsigned int i = 0; i < [tlist count]; i++) {
  405. if ([touches containsObject:[tlist objectAtIndex:i]]) {
  406. UITouch *touch = [tlist objectAtIndex:i];
  407. if (touch.phase != UITouchPhaseMoved)
  408. continue;
  409. int tid = get_touch_id(touch);
  410. ERR_FAIL_COND(tid == -1);
  411. CGPoint touchPoint = [touch locationInView:self];
  412. CGPoint prev_point = [touch previousLocationInView:self];
  413. OSIPhone::get_singleton()->touch_drag(tid, prev_point.x * self.contentScaleFactor, prev_point.y * self.contentScaleFactor, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor);
  414. };
  415. };
  416. }
  417. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  418. NSArray *tlist = [[event allTouches] allObjects];
  419. for (unsigned int i = 0; i < [tlist count]; i++) {
  420. if ([touches containsObject:[tlist objectAtIndex:i]]) {
  421. UITouch *touch = [tlist objectAtIndex:i];
  422. if (touch.phase != UITouchPhaseEnded)
  423. continue;
  424. int tid = get_touch_id(touch);
  425. ERR_FAIL_COND(tid == -1);
  426. remove_touch(touch);
  427. CGPoint touchPoint = [touch locationInView:self];
  428. OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, false, false);
  429. };
  430. };
  431. }
  432. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
  433. OSIPhone::get_singleton()->touches_cancelled();
  434. clear_touches();
  435. };
  436. - (BOOL)canBecomeFirstResponder {
  437. return YES;
  438. };
  439. - (void)open_keyboard {
  440. //keyboard_text = p_existing;
  441. [self becomeFirstResponder];
  442. };
  443. - (void)hide_keyboard {
  444. //keyboard_text = p_existing;
  445. [self resignFirstResponder];
  446. };
  447. - (void)keyboardOnScreen:(NSNotification *)notification {
  448. NSDictionary *info = notification.userInfo;
  449. NSValue *value = info[UIKeyboardFrameEndUserInfoKey];
  450. CGRect rawFrame = [value CGRectValue];
  451. CGRect keyboardFrame = [self convertRect:rawFrame fromView:nil];
  452. OSIPhone::get_singleton()->set_virtual_keyboard_height(_points_to_pixels(keyboardFrame.size.height));
  453. }
  454. - (void)keyboardHidden:(NSNotification *)notification {
  455. OSIPhone::get_singleton()->set_virtual_keyboard_height(0);
  456. }
  457. - (void)deleteBackward {
  458. if (keyboard_text.length())
  459. keyboard_text.erase(keyboard_text.length() - 1, 1);
  460. OSIPhone::get_singleton()->key(KEY_BACKSPACE, true);
  461. };
  462. - (BOOL)hasText {
  463. return keyboard_text.length() ? YES : NO;
  464. };
  465. - (void)insertText:(NSString *)p_text {
  466. String character;
  467. character.parse_utf8([p_text UTF8String]);
  468. keyboard_text = keyboard_text + character;
  469. OSIPhone::get_singleton()->key(character[0] == 10 ? KEY_ENTER : character[0], true);
  470. printf("inserting text with character %lc\n", (CharType)character[0]);
  471. };
  472. - (void)audioRouteChangeListenerCallback:(NSNotification *)notification {
  473. printf("*********** route changed!\n");
  474. NSDictionary *interuptionDict = notification.userInfo;
  475. NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
  476. switch (routeChangeReason) {
  477. case AVAudioSessionRouteChangeReasonNewDeviceAvailable: {
  478. NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");
  479. NSLog(@"Headphone/Line plugged in");
  480. }; break;
  481. case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: {
  482. NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
  483. NSLog(@"Headphone/Line was pulled. Resuming video play....");
  484. if (_is_video_playing()) {
  485. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  486. [_instance.avPlayer play]; // NOTE: change this line according your current player implementation
  487. NSLog(@"resumed play");
  488. });
  489. };
  490. }; break;
  491. case AVAudioSessionRouteChangeReasonCategoryChange: {
  492. // called at start - also when other audio wants to play
  493. NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");
  494. }; break;
  495. }
  496. }
  497. // When created via code however, we get initWithFrame
  498. - (id)initWithFrame:(CGRect)frame {
  499. self = [super initWithFrame:frame];
  500. _instance = self;
  501. printf("after init super %p\n", self);
  502. if (self != nil) {
  503. self = [self initGLES];
  504. printf("after init gles %p\n", self);
  505. }
  506. init_touches();
  507. self.multipleTouchEnabled = YES;
  508. self.autocorrectionType = UITextAutocorrectionTypeNo;
  509. printf("******** adding observer for sound routing changes\n");
  510. [[NSNotificationCenter defaultCenter]
  511. addObserver:self
  512. selector:@selector(audioRouteChangeListenerCallback:)
  513. name:AVAudioSessionRouteChangeNotification
  514. object:nil];
  515. printf("******** adding observer for keyboard show/hide\n");
  516. [[NSNotificationCenter defaultCenter]
  517. addObserver:self
  518. selector:@selector(keyboardOnScreen:)
  519. name:UIKeyboardDidShowNotification
  520. object:nil];
  521. [[NSNotificationCenter defaultCenter]
  522. addObserver:self
  523. selector:@selector(keyboardHidden:)
  524. name:UIKeyboardDidHideNotification
  525. object:nil];
  526. //self.autoresizesSubviews = YES;
  527. //[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth];
  528. return self;
  529. }
  530. //- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
  531. // return YES;
  532. //}
  533. //- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
  534. // return YES;
  535. //}
  536. // Stop animating and release resources when they are no longer needed.
  537. - (void)dealloc {
  538. [self stopAnimation];
  539. if ([EAGLContext currentContext] == context) {
  540. [EAGLContext setCurrentContext:nil];
  541. }
  542. [context release];
  543. context = nil;
  544. [super dealloc];
  545. }
  546. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  547. if (object == _instance.avPlayerItem && [keyPath isEqualToString:@"status"]) {
  548. if (_instance.avPlayerItem.status == AVPlayerStatusFailed || _instance.avPlayer.status == AVPlayerStatusFailed) {
  549. _stop_video();
  550. video_found_error = true;
  551. }
  552. if (_instance.avPlayer.status == AVPlayerStatusReadyToPlay &&
  553. _instance.avPlayerItem.status == AVPlayerItemStatusReadyToPlay &&
  554. CMTIME_COMPARE_INLINE(video_current_time, ==, kCMTimeZero)) {
  555. //NSLog(@"time: %@", video_current_time);
  556. [_instance.avPlayer seekToTime:video_current_time];
  557. video_current_time = kCMTimeZero;
  558. }
  559. }
  560. if (object == _instance.avPlayer && [keyPath isEqualToString:@"rate"]) {
  561. NSLog(@"Player playback rate changed: %.5f", _instance.avPlayer.rate);
  562. if (_is_video_playing() && _instance.avPlayer.rate == 0.0 && !_instance.avPlayer.error) {
  563. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  564. [_instance.avPlayer play]; // NOTE: change this line according your current player implementation
  565. NSLog(@"resumed play");
  566. });
  567. NSLog(@" . . . PAUSED (or just started)");
  568. }
  569. }
  570. }
  571. - (void)playerItemDidReachEnd:(NSNotification *)notification {
  572. _stop_video();
  573. }
  574. /*
  575. - (void)moviePlayBackDidFinish:(NSNotification*)notification {
  576. NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
  577. switch ([reason intValue]) {
  578. case MPMovieFinishReasonPlaybackEnded:
  579. //NSLog(@"Playback Ended");
  580. break;
  581. case MPMovieFinishReasonPlaybackError:
  582. //NSLog(@"Playback Error");
  583. video_found_error = true;
  584. break;
  585. case MPMovieFinishReasonUserExited:
  586. //NSLog(@"User Exited");
  587. video_found_error = true;
  588. break;
  589. default:
  590. //NSLog(@"Unsupported reason!");
  591. break;
  592. }
  593. MPMoviePlayerController *player = [notification object];
  594. [[NSNotificationCenter defaultCenter]
  595. removeObserver:self
  596. name:MPMoviePlayerPlaybackDidFinishNotification
  597. object:player];
  598. [_instance.moviePlayerController stop];
  599. [_instance.moviePlayerController.view removeFromSuperview];
  600. video_playing = false;
  601. }
  602. */
  603. @end