|
@@ -29,7 +29,6 @@
|
|
/*************************************************************************/
|
|
/*************************************************************************/
|
|
|
|
|
|
#import "godot_view.h"
|
|
#import "godot_view.h"
|
|
-#import "godot_view_gesture_recognizer.h"
|
|
|
|
|
|
|
|
#include "core/os/keyboard.h"
|
|
#include "core/os/keyboard.h"
|
|
#include "core/project_settings.h"
|
|
#include "core/project_settings.h"
|
|
@@ -39,699 +38,451 @@
|
|
#import <OpenGLES/EAGLDrawable.h>
|
|
#import <OpenGLES/EAGLDrawable.h>
|
|
#import <QuartzCore/QuartzCore.h>
|
|
#import <QuartzCore/QuartzCore.h>
|
|
|
|
|
|
-/*
|
|
|
|
-@interface GodotView (private)
|
|
|
|
-
|
|
|
|
-- (id)initGLES;
|
|
|
|
-- (BOOL)createFramebuffer;
|
|
|
|
-- (void)destroyFramebuffer;
|
|
|
|
-@end
|
|
|
|
-*/
|
|
|
|
-
|
|
|
|
-bool gles3_available = true;
|
|
|
|
-int gl_view_base_fb;
|
|
|
|
-static String keyboard_text;
|
|
|
|
-static GodotView *_instance = NULL;
|
|
|
|
-
|
|
|
|
-static bool video_found_error = false;
|
|
|
|
-static bool video_playing = false;
|
|
|
|
-static CMTime video_current_time;
|
|
|
|
-
|
|
|
|
-void _show_keyboard(String);
|
|
|
|
-void _hide_keyboard();
|
|
|
|
-bool _play_video(String, float, String, String);
|
|
|
|
-bool _is_video_playing();
|
|
|
|
-void _pause_video();
|
|
|
|
-void _focus_out_video();
|
|
|
|
-void _unpause_video();
|
|
|
|
-void _stop_video();
|
|
|
|
-CGFloat _points_to_pixels(CGFloat);
|
|
|
|
-
|
|
|
|
-void _show_keyboard(String p_existing) {
|
|
|
|
- keyboard_text = p_existing;
|
|
|
|
- printf("instance on show is %p\n", _instance);
|
|
|
|
- [_instance open_keyboard];
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-void _hide_keyboard() {
|
|
|
|
- printf("instance on hide is %p\n", _instance);
|
|
|
|
- [_instance hide_keyboard];
|
|
|
|
- keyboard_text = "";
|
|
|
|
-};
|
|
|
|
|
|
+#import "display_layer.h"
|
|
|
|
+#import "godot_view_gesture_recognizer.h"
|
|
|
|
+#import "godot_view_renderer.h"
|
|
|
|
|
|
-Rect2 _get_ios_window_safe_area(float p_window_width, float p_window_height) {
|
|
|
|
- UIEdgeInsets insets = UIEdgeInsetsZero;
|
|
|
|
|
|
+#import <CoreMotion/CoreMotion.h>
|
|
|
|
|
|
- if (@available(iOS 11.0, *)) {
|
|
|
|
- insets = [_instance safeAreaInsets];
|
|
|
|
- }
|
|
|
|
|
|
+static const int max_touches = 8;
|
|
|
|
|
|
- ERR_FAIL_COND_V(insets.left < 0 || insets.top < 0 || insets.right < 0 || insets.bottom < 0,
|
|
|
|
- Rect2(0, 0, p_window_width, p_window_height));
|
|
|
|
- UIEdgeInsets window_insets = UIEdgeInsetsMake(_points_to_pixels(insets.top), _points_to_pixels(insets.left), _points_to_pixels(insets.bottom), _points_to_pixels(insets.right));
|
|
|
|
- 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);
|
|
|
|
|
|
+@interface GodotView () {
|
|
|
|
+ UITouch *godot_touches[max_touches];
|
|
|
|
+ String keyboard_text;
|
|
}
|
|
}
|
|
|
|
|
|
-bool _play_video(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
|
|
|
|
- p_path = ProjectSettings::get_singleton()->globalize_path(p_path);
|
|
|
|
|
|
+@property(assign, nonatomic) BOOL isActive;
|
|
|
|
|
|
- NSString *file_path = [[[NSString alloc] initWithUTF8String:p_path.utf8().get_data()] autorelease];
|
|
|
|
|
|
+// CADisplayLink available on 3.1+ synchronizes the animation timer & drawing with the refresh rate of the display, only supports animation intervals of 1/60 1/30 & 1/15
|
|
|
|
+@property(strong, nonatomic) CADisplayLink *displayLink;
|
|
|
|
|
|
- _instance.avAsset = [AVAsset assetWithURL:[NSURL fileURLWithPath:file_path]];
|
|
|
|
|
|
+// An animation timer that, when animation is started, will periodically call -drawView at the given rate.
|
|
|
|
+// Only used if CADisplayLink is not
|
|
|
|
+@property(strong, nonatomic) NSTimer *animationTimer;
|
|
|
|
|
|
- _instance.avPlayerItem = [[AVPlayerItem alloc] initWithAsset:_instance.avAsset];
|
|
|
|
- [_instance.avPlayerItem addObserver:_instance forKeyPath:@"status" options:0 context:nil];
|
|
|
|
|
|
+@property(strong, nonatomic) CALayer<DisplayLayer> *renderingLayer;
|
|
|
|
|
|
- _instance.avPlayer = [[AVPlayer alloc] initWithPlayerItem:_instance.avPlayerItem];
|
|
|
|
- _instance.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:_instance.avPlayer];
|
|
|
|
|
|
+@property(strong, nonatomic) CMMotionManager *motionManager;
|
|
|
|
|
|
- [_instance.avPlayer addObserver:_instance forKeyPath:@"status" options:0 context:nil];
|
|
|
|
- [[NSNotificationCenter defaultCenter]
|
|
|
|
- addObserver:_instance
|
|
|
|
- selector:@selector(playerItemDidReachEnd:)
|
|
|
|
- name:AVPlayerItemDidPlayToEndTimeNotification
|
|
|
|
- object:[_instance.avPlayer currentItem]];
|
|
|
|
|
|
+@property(strong, nonatomic) GodotViewGestureRecognizer *delayGestureRecognizer;
|
|
|
|
|
|
- [_instance.avPlayer addObserver:_instance forKeyPath:@"rate" options:NSKeyValueObservingOptionNew context:0];
|
|
|
|
-
|
|
|
|
- [_instance.avPlayerLayer setFrame:_instance.bounds];
|
|
|
|
- [_instance.layer addSublayer:_instance.avPlayerLayer];
|
|
|
|
- [_instance.avPlayer play];
|
|
|
|
-
|
|
|
|
- AVMediaSelectionGroup *audioGroup = [_instance.avAsset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicAudible];
|
|
|
|
-
|
|
|
|
- NSMutableArray *allAudioParams = [NSMutableArray array];
|
|
|
|
- for (id track in audioGroup.options) {
|
|
|
|
- NSString *language = [[track locale] localeIdentifier];
|
|
|
|
- NSLog(@"subtitle lang: %@", language);
|
|
|
|
-
|
|
|
|
- if ([language isEqualToString:[NSString stringWithUTF8String:p_audio_track.utf8()]]) {
|
|
|
|
- AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters];
|
|
|
|
- [audioInputParams setVolume:p_volume atTime:kCMTimeZero];
|
|
|
|
- [audioInputParams setTrackID:[track trackID]];
|
|
|
|
- [allAudioParams addObject:audioInputParams];
|
|
|
|
-
|
|
|
|
- AVMutableAudioMix *audioMix = [AVMutableAudioMix audioMix];
|
|
|
|
- [audioMix setInputParameters:allAudioParams];
|
|
|
|
|
|
+@end
|
|
|
|
|
|
- [_instance.avPlayer.currentItem selectMediaOption:track inMediaSelectionGroup:audioGroup];
|
|
|
|
- [_instance.avPlayer.currentItem setAudioMix:audioMix];
|
|
|
|
|
|
+@implementation GodotView
|
|
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
|
|
+// Implement this to override the default layer class (which is [CALayer class]).
|
|
|
|
+// We do this so that our view will be backed by a layer that is capable of OpenGL ES rendering.
|
|
|
|
+- (CALayer<DisplayLayer> *)initializeRendering {
|
|
|
|
+ if (self.renderingLayer) {
|
|
|
|
+ return self.renderingLayer;
|
|
}
|
|
}
|
|
|
|
|
|
- AVMediaSelectionGroup *subtitlesGroup = [_instance.avAsset mediaSelectionGroupForMediaCharacteristic:AVMediaCharacteristicLegible];
|
|
|
|
- NSArray *useableTracks = [AVMediaSelectionGroup mediaSelectionOptionsFromArray:subtitlesGroup.options withoutMediaCharacteristics:[NSArray arrayWithObject:AVMediaCharacteristicContainsOnlyForcedSubtitles]];
|
|
|
|
|
|
+ CALayer<DisplayLayer> *layer = [GodotOpenGLLayer layer];
|
|
|
|
|
|
- for (id track in useableTracks) {
|
|
|
|
- NSString *language = [[track locale] localeIdentifier];
|
|
|
|
- NSLog(@"subtitle lang: %@", language);
|
|
|
|
|
|
+ layer.frame = self.bounds;
|
|
|
|
+ layer.contentsScale = self.contentScaleFactor;
|
|
|
|
|
|
- if ([language isEqualToString:[NSString stringWithUTF8String:p_subtitle_track.utf8()]]) {
|
|
|
|
- [_instance.avPlayer.currentItem selectMediaOption:track inMediaSelectionGroup:subtitlesGroup];
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ [self.layer addSublayer:layer];
|
|
|
|
+ self.renderingLayer = layer;
|
|
|
|
|
|
- video_playing = true;
|
|
|
|
|
|
+ [layer initializeDisplayLayer];
|
|
|
|
|
|
- return true;
|
|
|
|
|
|
+ return self.renderingLayer;
|
|
}
|
|
}
|
|
|
|
|
|
-bool _is_video_playing() {
|
|
|
|
- if (_instance.avPlayer.error) {
|
|
|
|
- printf("Error during playback\n");
|
|
|
|
|
|
+- (instancetype)initWithCoder:(NSCoder *)coder {
|
|
|
|
+ self = [super initWithCoder:coder];
|
|
|
|
+
|
|
|
|
+ if (self) {
|
|
|
|
+ [self godot_commonInit];
|
|
}
|
|
}
|
|
- return (_instance.avPlayer.rate > 0 && !_instance.avPlayer.error);
|
|
|
|
-}
|
|
|
|
|
|
|
|
-void _pause_video() {
|
|
|
|
- video_current_time = _instance.avPlayer.currentTime;
|
|
|
|
- [_instance.avPlayer pause];
|
|
|
|
- video_playing = false;
|
|
|
|
|
|
+ return self;
|
|
}
|
|
}
|
|
|
|
|
|
-void _focus_out_video() {
|
|
|
|
- printf("focus out pausing video\n");
|
|
|
|
- [_instance.avPlayer pause];
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-void _unpause_video() {
|
|
|
|
-
|
|
|
|
- [_instance.avPlayer play];
|
|
|
|
- video_playing = true;
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-void _stop_video() {
|
|
|
|
- [_instance.avPlayer pause];
|
|
|
|
- [_instance.avPlayerLayer removeFromSuperlayer];
|
|
|
|
- _instance.avPlayer = nil;
|
|
|
|
- video_playing = false;
|
|
|
|
-}
|
|
|
|
|
|
+- (instancetype)initWithFrame:(CGRect)frame {
|
|
|
|
+ self = [super initWithFrame:frame];
|
|
|
|
|
|
-CGFloat _points_to_pixels(CGFloat points) {
|
|
|
|
- float pixelPerInch;
|
|
|
|
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
|
|
|
|
- pixelPerInch = 132;
|
|
|
|
- } else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
|
|
|
|
- pixelPerInch = 163;
|
|
|
|
- } else {
|
|
|
|
- pixelPerInch = 160;
|
|
|
|
|
|
+ if (self) {
|
|
|
|
+ [self godot_commonInit];
|
|
}
|
|
}
|
|
- CGFloat pointsPerInch = 72.0;
|
|
|
|
- return (points / pointsPerInch * pixelPerInch);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-@implementation GodotView
|
|
|
|
-
|
|
|
|
-@synthesize animationInterval;
|
|
|
|
-
|
|
|
|
-static const int max_touches = 8;
|
|
|
|
-static UITouch *touches[max_touches];
|
|
|
|
-
|
|
|
|
-static void init_touches() {
|
|
|
|
-
|
|
|
|
- for (int i = 0; i < max_touches; i++) {
|
|
|
|
- touches[i] = NULL;
|
|
|
|
- };
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-static int get_touch_id(UITouch *p_touch) {
|
|
|
|
-
|
|
|
|
- int first = -1;
|
|
|
|
- for (int i = 0; i < max_touches; i++) {
|
|
|
|
- if (first == -1 && touches[i] == NULL) {
|
|
|
|
- first = i;
|
|
|
|
- continue;
|
|
|
|
- };
|
|
|
|
- if (touches[i] == p_touch)
|
|
|
|
- return i;
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- if (first != -1) {
|
|
|
|
- touches[first] = p_touch;
|
|
|
|
- return first;
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- return -1;
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-static int remove_touch(UITouch *p_touch) {
|
|
|
|
-
|
|
|
|
- int remaining = 0;
|
|
|
|
- for (int i = 0; i < max_touches; i++) {
|
|
|
|
-
|
|
|
|
- if (touches[i] == NULL)
|
|
|
|
- continue;
|
|
|
|
- if (touches[i] == p_touch)
|
|
|
|
- touches[i] = NULL;
|
|
|
|
- else
|
|
|
|
- ++remaining;
|
|
|
|
- };
|
|
|
|
- return remaining;
|
|
|
|
-};
|
|
|
|
|
|
|
|
-static void clear_touches() {
|
|
|
|
|
|
+ return self;
|
|
|
|
+}
|
|
|
|
|
|
- for (int i = 0; i < max_touches; i++) {
|
|
|
|
|
|
+// Stop animating and release resources when they are no longer needed.
|
|
|
|
+- (void)dealloc {
|
|
|
|
+ [self stopRendering];
|
|
|
|
|
|
- touches[i] = NULL;
|
|
|
|
- };
|
|
|
|
-};
|
|
|
|
|
|
+ self.renderer = nil;
|
|
|
|
|
|
-// Implement this to override the default layer class (which is [CALayer class]).
|
|
|
|
-// We do this so that our view will be backed by a layer that is capable of OpenGL ES rendering.
|
|
|
|
-+ (Class)layerClass {
|
|
|
|
- return [CAEAGLLayer class];
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-//The GL view is stored in the nib file. When it's unarchived it's sent -initWithCoder:
|
|
|
|
-- (id)initWithCoder:(NSCoder *)coder {
|
|
|
|
- active = FALSE;
|
|
|
|
- if ((self = [super initWithCoder:coder])) {
|
|
|
|
- self = [self initGLES];
|
|
|
|
- [self initGestureRecognizer];
|
|
|
|
|
|
+ if (self.renderingLayer) {
|
|
|
|
+ [self.renderingLayer removeFromSuperlayer];
|
|
|
|
+ self.renderingLayer = nil;
|
|
}
|
|
}
|
|
- return self;
|
|
|
|
-}
|
|
|
|
|
|
|
|
-- (id)initGLES {
|
|
|
|
- // Get our backing layer
|
|
|
|
- CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
|
|
|
|
-
|
|
|
|
- // Configure it so that it is opaque, does not retain the contents of the backbuffer when displayed, and uses RGBA8888 color.
|
|
|
|
- eaglLayer.opaque = YES;
|
|
|
|
- eaglLayer.drawableProperties = [NSDictionary
|
|
|
|
- dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:FALSE],
|
|
|
|
- kEAGLDrawablePropertyRetainedBacking,
|
|
|
|
- kEAGLColorFormatRGBA8,
|
|
|
|
- kEAGLDrawablePropertyColorFormat,
|
|
|
|
- nil];
|
|
|
|
- bool fallback_gl2 = false;
|
|
|
|
- // Create a GL ES 3 context based on the gl driver from project settings
|
|
|
|
- if (GLOBAL_GET("rendering/quality/driver/driver_name") == "GLES3") {
|
|
|
|
- context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES3];
|
|
|
|
- NSLog(@"Setting up an OpenGL ES 3.0 context. Based on Project Settings \"rendering/quality/driver/driver_name\"");
|
|
|
|
- if (!context && GLOBAL_GET("rendering/quality/driver/fallback_to_gles2")) {
|
|
|
|
- gles3_available = false;
|
|
|
|
- fallback_gl2 = true;
|
|
|
|
- NSLog(@"Failed to create OpenGL ES 3.0 context. Falling back to OpenGL ES 2.0");
|
|
|
|
- }
|
|
|
|
|
|
+ if (self.motionManager) {
|
|
|
|
+ [self.motionManager stopDeviceMotionUpdates];
|
|
|
|
+ self.motionManager = nil;
|
|
}
|
|
}
|
|
|
|
|
|
- // Create GL ES 2 context
|
|
|
|
- if (GLOBAL_GET("rendering/quality/driver/driver_name") == "GLES2" || fallback_gl2) {
|
|
|
|
- context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
|
|
|
|
- NSLog(@"Setting up an OpenGL ES 2.0 context.");
|
|
|
|
- if (!context) {
|
|
|
|
- NSLog(@"Failed to create OpenGL ES 2.0 context!");
|
|
|
|
- return nil;
|
|
|
|
- }
|
|
|
|
|
|
+ if (self.displayLink) {
|
|
|
|
+ [self.displayLink invalidate];
|
|
|
|
+ self.displayLink = nil;
|
|
}
|
|
}
|
|
|
|
|
|
- if (![EAGLContext setCurrentContext:context]) {
|
|
|
|
- NSLog(@"Failed to set EAGLContext!");
|
|
|
|
- return nil;
|
|
|
|
- }
|
|
|
|
- if (![self createFramebuffer]) {
|
|
|
|
- NSLog(@"Failed to create frame buffer!");
|
|
|
|
- return nil;
|
|
|
|
|
|
+ if (self.animationTimer) {
|
|
|
|
+ [self.animationTimer invalidate];
|
|
|
|
+ self.animationTimer = nil;
|
|
}
|
|
}
|
|
|
|
|
|
- // Default the animation interval to 1/60th of a second.
|
|
|
|
- animationInterval = 1.0 / 60.0;
|
|
|
|
- return self;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-- (void)initGestureRecognizer {
|
|
|
|
- delayGestureRecognizer = [[GodotViewGestureRecognizer alloc] init];
|
|
|
|
- [self addGestureRecognizer:delayGestureRecognizer];
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-- (id<GodotViewDelegate>)delegate {
|
|
|
|
- return delegate;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// Update the delegate, and if it needs a -setupView: call, set our internal flag so that it will be called.
|
|
|
|
-- (void)setDelegate:(id<GodotViewDelegate>)d {
|
|
|
|
- delegate = d;
|
|
|
|
- delegateSetup = ![delegate respondsToSelector:@selector(setupView:)];
|
|
|
|
|
|
+ if (self.delayGestureRecognizer) {
|
|
|
|
+ self.delayGestureRecognizer = nil;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-@synthesize useCADisplayLink;
|
|
|
|
|
|
+- (void)godot_commonInit {
|
|
|
|
+ self.contentScaleFactor = [UIScreen mainScreen].nativeScale;
|
|
|
|
|
|
-// If our view is resized, we'll be asked to layout subviews.
|
|
|
|
-// This is the perfect opportunity to also update the framebuffer so that it is
|
|
|
|
-// the same size as our display area.
|
|
|
|
|
|
+ [self initTouches];
|
|
|
|
|
|
-- (void)layoutSubviews {
|
|
|
|
- [EAGLContext setCurrentContext:context];
|
|
|
|
- [self destroyFramebuffer];
|
|
|
|
- [self createFramebuffer];
|
|
|
|
- [self drawView];
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-- (BOOL)createFramebuffer {
|
|
|
|
- // Generate IDs for a framebuffer object and a color renderbuffer
|
|
|
|
- UIScreen *mainscr = [UIScreen mainScreen];
|
|
|
|
- printf("******** screen size %i, %i\n", (int)mainscr.currentMode.size.width, (int)mainscr.currentMode.size.height);
|
|
|
|
- self.contentScaleFactor = mainscr.nativeScale;
|
|
|
|
-
|
|
|
|
- glGenFramebuffersOES(1, &viewFramebuffer);
|
|
|
|
- glGenRenderbuffersOES(1, &viewRenderbuffer);
|
|
|
|
-
|
|
|
|
- glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
|
|
|
|
- glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
|
|
|
|
- // This call associates the storage for the current render buffer with the EAGLDrawable (our CAEAGLLayer)
|
|
|
|
- // allowing us to draw into a buffer that will later be rendered to screen wherever the layer is (which corresponds with our view).
|
|
|
|
- [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(id<EAGLDrawable>)self.layer];
|
|
|
|
- glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
|
|
|
|
-
|
|
|
|
- glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
|
|
|
|
- glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
|
|
|
|
-
|
|
|
|
- // For this sample, we also need a depth buffer, so we'll create and attach one via another renderbuffer.
|
|
|
|
- glGenRenderbuffersOES(1, &depthRenderbuffer);
|
|
|
|
- glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
|
|
|
|
- glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
|
|
|
|
- glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
|
|
|
|
-
|
|
|
|
- if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
|
|
|
|
- NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
|
|
|
|
- return NO;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (OS::get_singleton()) {
|
|
|
|
- OS::VideoMode vm;
|
|
|
|
- vm.fullscreen = true;
|
|
|
|
- vm.width = backingWidth;
|
|
|
|
- vm.height = backingHeight;
|
|
|
|
- vm.resizable = false;
|
|
|
|
- OS::get_singleton()->set_video_mode(vm);
|
|
|
|
- OSIPhone::get_singleton()->set_base_framebuffer(viewFramebuffer);
|
|
|
|
- };
|
|
|
|
- gl_view_base_fb = viewFramebuffer;
|
|
|
|
|
|
+ // Configure and start accelerometer
|
|
|
|
+ if (!self.motionManager) {
|
|
|
|
+ self.motionManager = [[CMMotionManager alloc] init];
|
|
|
|
+ if (self.motionManager.deviceMotionAvailable) {
|
|
|
|
+ self.motionManager.deviceMotionUpdateInterval = 1.0 / 70.0;
|
|
|
|
+ [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical];
|
|
|
|
+ } else {
|
|
|
|
+ self.motionManager = nil;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- return YES;
|
|
|
|
|
|
+ // Initialize delay gesture recognizer
|
|
|
|
+ GodotViewGestureRecognizer *gestureRecognizer = [[GodotViewGestureRecognizer alloc] init];
|
|
|
|
+ self.delayGestureRecognizer = gestureRecognizer;
|
|
|
|
+ [self addGestureRecognizer:self.delayGestureRecognizer];
|
|
}
|
|
}
|
|
|
|
|
|
-// Clean up any buffers we have allocated.
|
|
|
|
-- (void)destroyFramebuffer {
|
|
|
|
- glDeleteFramebuffersOES(1, &viewFramebuffer);
|
|
|
|
- viewFramebuffer = 0;
|
|
|
|
- glDeleteRenderbuffersOES(1, &viewRenderbuffer);
|
|
|
|
- viewRenderbuffer = 0;
|
|
|
|
-
|
|
|
|
- if (depthRenderbuffer) {
|
|
|
|
- glDeleteRenderbuffersOES(1, &depthRenderbuffer);
|
|
|
|
- depthRenderbuffer = 0;
|
|
|
|
|
|
+- (void)startRendering {
|
|
|
|
+ if (self.isActive) {
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
|
|
-- (void)startAnimation {
|
|
|
|
- if (active)
|
|
|
|
- return;
|
|
|
|
- active = TRUE;
|
|
|
|
|
|
+ self.isActive = YES;
|
|
|
|
+
|
|
printf("start animation!\n");
|
|
printf("start animation!\n");
|
|
- if (useCADisplayLink) {
|
|
|
|
|
|
+
|
|
|
|
+ if (self.useCADisplayLink) {
|
|
|
|
+ self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
|
|
|
|
|
|
// Approximate frame rate
|
|
// Approximate frame rate
|
|
// assumes device refreshes at 60 fps
|
|
// assumes device refreshes at 60 fps
|
|
- int displayFPS = (NSInteger)(1.0 / animationInterval);
|
|
|
|
|
|
+ int displayFPS = (NSInteger)(1.0 / self.renderingInterval);
|
|
|
|
|
|
- displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawView)];
|
|
|
|
- displayLink.preferredFramesPerSecond = displayFPS;
|
|
|
|
|
|
+ self.displayLink.preferredFramesPerSecond = displayFPS;
|
|
|
|
|
|
// Setup DisplayLink in main thread
|
|
// Setup DisplayLink in main thread
|
|
- [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
|
|
|
|
|
+ [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
|
} else {
|
|
} else {
|
|
- animationTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (video_playing) {
|
|
|
|
- _unpause_video();
|
|
|
|
|
|
+ self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:self.renderingInterval target:self selector:@selector(drawView) userInfo:nil repeats:YES];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-- (void)stopAnimation {
|
|
|
|
- if (!active)
|
|
|
|
|
|
+- (void)stopRendering {
|
|
|
|
+ if (!self.isActive) {
|
|
return;
|
|
return;
|
|
- active = FALSE;
|
|
|
|
- printf("******** stop animation!\n");
|
|
|
|
-
|
|
|
|
- if (useCADisplayLink) {
|
|
|
|
- [displayLink invalidate];
|
|
|
|
- displayLink = nil;
|
|
|
|
- } else {
|
|
|
|
- [animationTimer invalidate];
|
|
|
|
- animationTimer = nil;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- clear_touches();
|
|
|
|
|
|
+ self.isActive = NO;
|
|
|
|
|
|
- if (video_playing) {
|
|
|
|
- // save position
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+ printf("******** stop animation!\n");
|
|
|
|
|
|
-- (void)setAnimationInterval:(NSTimeInterval)interval {
|
|
|
|
- animationInterval = interval;
|
|
|
|
- if ((useCADisplayLink && displayLink) || (!useCADisplayLink && animationTimer)) {
|
|
|
|
- [self stopAnimation];
|
|
|
|
- [self startAnimation];
|
|
|
|
|
|
+ if (self.useCADisplayLink) {
|
|
|
|
+ [self.displayLink invalidate];
|
|
|
|
+ self.displayLink = nil;
|
|
|
|
+ } else {
|
|
|
|
+ [self.animationTimer invalidate];
|
|
|
|
+ self.animationTimer = nil;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ [self clearTouches];
|
|
}
|
|
}
|
|
|
|
|
|
// Updates the OpenGL view when the timer fires
|
|
// Updates the OpenGL view when the timer fires
|
|
- (void)drawView {
|
|
- (void)drawView {
|
|
-
|
|
|
|
- if (!active) {
|
|
|
|
|
|
+ if (!self.isActive) {
|
|
printf("draw view not active!\n");
|
|
printf("draw view not active!\n");
|
|
return;
|
|
return;
|
|
- };
|
|
|
|
- if (useCADisplayLink) {
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (self.useCADisplayLink) {
|
|
// Pause the CADisplayLink to avoid recursion
|
|
// Pause the CADisplayLink to avoid recursion
|
|
- [displayLink setPaused:YES];
|
|
|
|
|
|
+ [self.displayLink setPaused:YES];
|
|
|
|
|
|
// Process all input events
|
|
// Process all input events
|
|
while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, TRUE) == kCFRunLoopRunHandledSource)
|
|
while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, TRUE) == kCFRunLoopRunHandledSource)
|
|
;
|
|
;
|
|
|
|
|
|
// We are good to go, resume the CADisplayLink
|
|
// We are good to go, resume the CADisplayLink
|
|
- [displayLink setPaused:NO];
|
|
|
|
|
|
+ [self.displayLink setPaused:NO];
|
|
}
|
|
}
|
|
|
|
|
|
- // Make sure that you are drawing to the current context
|
|
|
|
- [EAGLContext setCurrentContext:context];
|
|
|
|
|
|
+ [self.renderingLayer startRenderDisplayLayer];
|
|
|
|
|
|
- // If our drawing delegate needs to have the view setup, then call -setupView: and flag that it won't need to be called again.
|
|
|
|
- if (!delegateSetup) {
|
|
|
|
- [delegate setupView:self];
|
|
|
|
- delegateSetup = YES;
|
|
|
|
|
|
+ if (!self.renderer) {
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
|
|
- glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
|
|
|
|
-
|
|
|
|
- [delegate drawView:self];
|
|
|
|
|
|
+ if ([self.renderer setupView:self]) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
|
|
|
|
- [context presentRenderbuffer:GL_RENDERBUFFER_OES];
|
|
|
|
|
|
+ [self handleMotion];
|
|
|
|
+ [self.renderer renderOnView:self];
|
|
|
|
|
|
-#ifdef DEBUG_ENABLED
|
|
|
|
- GLenum err = glGetError();
|
|
|
|
- if (err)
|
|
|
|
- NSLog(@"DrawView: %x error", err);
|
|
|
|
-#endif
|
|
|
|
|
|
+ [self.renderingLayer stopRenderDisplayLayer];
|
|
}
|
|
}
|
|
|
|
|
|
-- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
- NSArray *tlist = [[event allTouches] allObjects];
|
|
|
|
- for (unsigned int i = 0; i < [tlist count]; i++) {
|
|
|
|
-
|
|
|
|
- if ([touches containsObject:[tlist objectAtIndex:i]]) {
|
|
|
|
-
|
|
|
|
- UITouch *touch = [tlist objectAtIndex:i];
|
|
|
|
- int tid = get_touch_id(touch);
|
|
|
|
- ERR_FAIL_COND(tid == -1);
|
|
|
|
- CGPoint touchPoint = [touch locationInView:self];
|
|
|
|
- OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, true, touch.tapCount > 1);
|
|
|
|
- };
|
|
|
|
- };
|
|
|
|
|
|
+- (BOOL)canRender {
|
|
|
|
+ if (self.useCADisplayLink) {
|
|
|
|
+ return self.displayLink != nil;
|
|
|
|
+ } else {
|
|
|
|
+ return self.animationTimer != nil;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
|
|
+- (void)setRenderingInterval:(NSTimeInterval)renderingInterval {
|
|
|
|
+ _renderingInterval = renderingInterval;
|
|
|
|
|
|
- NSArray *tlist = [[event allTouches] allObjects];
|
|
|
|
- for (unsigned int i = 0; i < [tlist count]; i++) {
|
|
|
|
-
|
|
|
|
- if ([touches containsObject:[tlist objectAtIndex:i]]) {
|
|
|
|
-
|
|
|
|
- UITouch *touch = [tlist objectAtIndex:i];
|
|
|
|
- int tid = get_touch_id(touch);
|
|
|
|
- ERR_FAIL_COND(tid == -1);
|
|
|
|
- CGPoint touchPoint = [touch locationInView:self];
|
|
|
|
- CGPoint prev_point = [touch previousLocationInView:self];
|
|
|
|
- OSIPhone::get_singleton()->touch_drag(tid, prev_point.x * self.contentScaleFactor, prev_point.y * self.contentScaleFactor, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor);
|
|
|
|
- };
|
|
|
|
- };
|
|
|
|
|
|
+ if (self.canRender) {
|
|
|
|
+ [self stopRendering];
|
|
|
|
+ [self startRendering];
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
- NSArray *tlist = [[event allTouches] allObjects];
|
|
|
|
- for (unsigned int i = 0; i < [tlist count]; i++) {
|
|
|
|
|
|
+// If our view is resized, we'll be asked to layout subviews.
|
|
|
|
+// This is the perfect opportunity to also update the framebuffer so that it is
|
|
|
|
+// the same size as our display area.
|
|
|
|
|
|
- if ([touches containsObject:[tlist objectAtIndex:i]]) {
|
|
|
|
|
|
+- (void)layoutSubviews {
|
|
|
|
+ if (self.renderingLayer) {
|
|
|
|
+ self.renderingLayer.frame = self.bounds;
|
|
|
|
+ [self.renderingLayer layoutDisplayLayer];
|
|
|
|
+ }
|
|
|
|
|
|
- UITouch *touch = [tlist objectAtIndex:i];
|
|
|
|
- int tid = get_touch_id(touch);
|
|
|
|
- ERR_FAIL_COND(tid == -1);
|
|
|
|
- remove_touch(touch);
|
|
|
|
- CGPoint touchPoint = [touch locationInView:self];
|
|
|
|
- OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, false, false);
|
|
|
|
- };
|
|
|
|
- };
|
|
|
|
|
|
+ [super layoutSubviews];
|
|
}
|
|
}
|
|
|
|
|
|
-- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
|
|
+// MARK: - Input
|
|
|
|
|
|
- OSIPhone::get_singleton()->touches_cancelled();
|
|
|
|
- clear_touches();
|
|
|
|
-};
|
|
|
|
|
|
+// MARK: Keyboard
|
|
|
|
|
|
- (BOOL)canBecomeFirstResponder {
|
|
- (BOOL)canBecomeFirstResponder {
|
|
return YES;
|
|
return YES;
|
|
-};
|
|
|
|
-
|
|
|
|
-- (void)open_keyboard {
|
|
|
|
- //keyboard_text = p_existing;
|
|
|
|
- [self becomeFirstResponder];
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-- (void)hide_keyboard {
|
|
|
|
- //keyboard_text = p_existing;
|
|
|
|
- [self resignFirstResponder];
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-- (void)keyboardOnScreen:(NSNotification *)notification {
|
|
|
|
- NSDictionary *info = notification.userInfo;
|
|
|
|
- NSValue *value = info[UIKeyboardFrameEndUserInfoKey];
|
|
|
|
-
|
|
|
|
- CGRect rawFrame = [value CGRectValue];
|
|
|
|
- CGRect keyboardFrame = [self convertRect:rawFrame fromView:nil];
|
|
|
|
|
|
+}
|
|
|
|
|
|
- OSIPhone::get_singleton()->set_virtual_keyboard_height(_points_to_pixels(keyboardFrame.size.height));
|
|
|
|
|
|
+- (BOOL)becomeFirstResponderWithString:(String)p_existing {
|
|
|
|
+ keyboard_text = p_existing;
|
|
|
|
+ return [self becomeFirstResponder];
|
|
}
|
|
}
|
|
|
|
|
|
-- (void)keyboardHidden:(NSNotification *)notification {
|
|
|
|
- OSIPhone::get_singleton()->set_virtual_keyboard_height(0);
|
|
|
|
|
|
+- (BOOL)resignFirstResponder {
|
|
|
|
+ keyboard_text = String();
|
|
|
|
+ return [super resignFirstResponder];
|
|
}
|
|
}
|
|
|
|
|
|
- (void)deleteBackward {
|
|
- (void)deleteBackward {
|
|
- if (keyboard_text.length())
|
|
|
|
|
|
+ if (keyboard_text.length()) {
|
|
keyboard_text.erase(keyboard_text.length() - 1, 1);
|
|
keyboard_text.erase(keyboard_text.length() - 1, 1);
|
|
|
|
+ }
|
|
OSIPhone::get_singleton()->key(KEY_BACKSPACE, true);
|
|
OSIPhone::get_singleton()->key(KEY_BACKSPACE, true);
|
|
-};
|
|
|
|
|
|
+}
|
|
|
|
|
|
- (BOOL)hasText {
|
|
- (BOOL)hasText {
|
|
- return keyboard_text.length() ? YES : NO;
|
|
|
|
-};
|
|
|
|
|
|
+ return keyboard_text.length() > 0;
|
|
|
|
+}
|
|
|
|
|
|
- (void)insertText:(NSString *)p_text {
|
|
- (void)insertText:(NSString *)p_text {
|
|
String character;
|
|
String character;
|
|
character.parse_utf8([p_text UTF8String]);
|
|
character.parse_utf8([p_text UTF8String]);
|
|
keyboard_text = keyboard_text + character;
|
|
keyboard_text = keyboard_text + character;
|
|
OSIPhone::get_singleton()->key(character[0] == 10 ? KEY_ENTER : character[0], true);
|
|
OSIPhone::get_singleton()->key(character[0] == 10 ? KEY_ENTER : character[0], true);
|
|
- printf("inserting text with character %lc\n", (CharType)character[0]);
|
|
|
|
-};
|
|
|
|
|
|
+}
|
|
|
|
|
|
-- (void)audioRouteChangeListenerCallback:(NSNotification *)notification {
|
|
|
|
- printf("*********** route changed!\n");
|
|
|
|
- NSDictionary *interuptionDict = notification.userInfo;
|
|
|
|
|
|
+// MARK: Touches
|
|
|
|
|
|
- NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
|
|
|
|
|
|
+- (void)initTouches {
|
|
|
|
+ for (int i = 0; i < max_touches; i++) {
|
|
|
|
+ godot_touches[i] = NULL;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
- switch (routeChangeReason) {
|
|
|
|
|
|
+- (int)getTouchIDForTouch:(UITouch *)p_touch {
|
|
|
|
+ int first = -1;
|
|
|
|
+ for (int i = 0; i < max_touches; i++) {
|
|
|
|
+ if (first == -1 && godot_touches[i] == NULL) {
|
|
|
|
+ first = i;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (godot_touches[i] == p_touch) {
|
|
|
|
+ return i;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- case AVAudioSessionRouteChangeReasonNewDeviceAvailable: {
|
|
|
|
- NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");
|
|
|
|
- NSLog(@"Headphone/Line plugged in");
|
|
|
|
- }; break;
|
|
|
|
|
|
+ if (first != -1) {
|
|
|
|
+ godot_touches[first] = p_touch;
|
|
|
|
+ return first;
|
|
|
|
+ }
|
|
|
|
|
|
- case AVAudioSessionRouteChangeReasonOldDeviceUnavailable: {
|
|
|
|
- NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
|
|
|
|
- NSLog(@"Headphone/Line was pulled. Resuming video play....");
|
|
|
|
- if (_is_video_playing()) {
|
|
|
|
|
|
+ return -1;
|
|
|
|
+}
|
|
|
|
|
|
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
|
|
|
- [_instance.avPlayer play]; // NOTE: change this line according your current player implementation
|
|
|
|
- NSLog(@"resumed play");
|
|
|
|
- });
|
|
|
|
- };
|
|
|
|
- }; break;
|
|
|
|
|
|
+- (int)removeTouch:(UITouch *)p_touch {
|
|
|
|
+ int remaining = 0;
|
|
|
|
+ for (int i = 0; i < max_touches; i++) {
|
|
|
|
+ if (godot_touches[i] == NULL) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (godot_touches[i] == p_touch) {
|
|
|
|
+ godot_touches[i] = NULL;
|
|
|
|
+ } else {
|
|
|
|
+ ++remaining;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return remaining;
|
|
|
|
+}
|
|
|
|
|
|
- case AVAudioSessionRouteChangeReasonCategoryChange: {
|
|
|
|
- // called at start - also when other audio wants to play
|
|
|
|
- NSLog(@"AVAudioSessionRouteChangeReasonCategoryChange");
|
|
|
|
- }; break;
|
|
|
|
|
|
+- (void)clearTouches {
|
|
|
|
+ for (int i = 0; i < max_touches; i++) {
|
|
|
|
+ godot_touches[i] = NULL;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-// When created via code however, we get initWithFrame
|
|
|
|
-- (id)initWithFrame:(CGRect)frame {
|
|
|
|
- self = [super initWithFrame:frame];
|
|
|
|
- _instance = self;
|
|
|
|
- printf("after init super %p\n", self);
|
|
|
|
- if (self != nil) {
|
|
|
|
- self = [self initGLES];
|
|
|
|
- printf("after init gles %p\n", self);
|
|
|
|
- [self initGestureRecognizer];
|
|
|
|
- }
|
|
|
|
- init_touches();
|
|
|
|
- self.multipleTouchEnabled = YES;
|
|
|
|
- self.autocorrectionType = UITextAutocorrectionTypeNo;
|
|
|
|
-
|
|
|
|
- printf("******** adding observer for sound routing changes\n");
|
|
|
|
- [[NSNotificationCenter defaultCenter]
|
|
|
|
- addObserver:self
|
|
|
|
- selector:@selector(audioRouteChangeListenerCallback:)
|
|
|
|
- name:AVAudioSessionRouteChangeNotification
|
|
|
|
- object:nil];
|
|
|
|
-
|
|
|
|
- printf("******** adding observer for keyboard show/hide\n");
|
|
|
|
- [[NSNotificationCenter defaultCenter]
|
|
|
|
- addObserver:self
|
|
|
|
- selector:@selector(keyboardOnScreen:)
|
|
|
|
- name:UIKeyboardDidShowNotification
|
|
|
|
- object:nil];
|
|
|
|
- [[NSNotificationCenter defaultCenter]
|
|
|
|
- addObserver:self
|
|
|
|
- selector:@selector(keyboardHidden:)
|
|
|
|
- name:UIKeyboardDidHideNotification
|
|
|
|
- object:nil];
|
|
|
|
-
|
|
|
|
- //self.autoresizesSubviews = YES;
|
|
|
|
- //[self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleWidth];
|
|
|
|
|
|
+- (void)touchesBegan:(NSSet *)touchesSet withEvent:(UIEvent *)event {
|
|
|
|
+ NSArray *tlist = [event.allTouches allObjects];
|
|
|
|
+ for (unsigned int i = 0; i < [tlist count]; i++) {
|
|
|
|
+ if ([touchesSet containsObject:[tlist objectAtIndex:i]]) {
|
|
|
|
+ UITouch *touch = [tlist objectAtIndex:i];
|
|
|
|
+ int tid = [self getTouchIDForTouch:touch];
|
|
|
|
+ ERR_FAIL_COND(tid == -1);
|
|
|
|
+ CGPoint touchPoint = [touch locationInView:self];
|
|
|
|
+ OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, true, touch.tapCount > 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
- return self;
|
|
|
|
|
|
+- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
+ NSArray *tlist = [event.allTouches allObjects];
|
|
|
|
+ for (unsigned int i = 0; i < [tlist count]; i++) {
|
|
|
|
+ if ([touches containsObject:[tlist objectAtIndex:i]]) {
|
|
|
|
+ UITouch *touch = [tlist objectAtIndex:i];
|
|
|
|
+ int tid = [self getTouchIDForTouch:touch];
|
|
|
|
+ ERR_FAIL_COND(tid == -1);
|
|
|
|
+ CGPoint touchPoint = [touch locationInView:self];
|
|
|
|
+ CGPoint prev_point = [touch previousLocationInView:self];
|
|
|
|
+ OSIPhone::get_singleton()->touch_drag(tid, prev_point.x * self.contentScaleFactor, prev_point.y * self.contentScaleFactor, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-//- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
|
|
|
|
-// return YES;
|
|
|
|
-//}
|
|
|
|
|
|
+- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
+ NSArray *tlist = [event.allTouches allObjects];
|
|
|
|
+ for (unsigned int i = 0; i < [tlist count]; i++) {
|
|
|
|
+ if ([touches containsObject:[tlist objectAtIndex:i]]) {
|
|
|
|
+ UITouch *touch = [tlist objectAtIndex:i];
|
|
|
|
+ int tid = [self getTouchIDForTouch:touch];
|
|
|
|
+ ERR_FAIL_COND(tid == -1);
|
|
|
|
+ [self removeTouch:touch];
|
|
|
|
+ CGPoint touchPoint = [touch locationInView:self];
|
|
|
|
+ OSIPhone::get_singleton()->touch_press(tid, touchPoint.x * self.contentScaleFactor, touchPoint.y * self.contentScaleFactor, false, false);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
-//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
|
|
|
|
-// return YES;
|
|
|
|
-//}
|
|
|
|
|
|
+- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
|
|
|
|
+ NSArray *tlist = [event.allTouches allObjects];
|
|
|
|
+ for (unsigned int i = 0; i < [tlist count]; i++) {
|
|
|
|
+ if ([touches containsObject:[tlist objectAtIndex:i]]) {
|
|
|
|
+ UITouch *touch = [tlist objectAtIndex:i];
|
|
|
|
+ int tid = [self getTouchIDForTouch:touch];
|
|
|
|
+ ERR_FAIL_COND(tid == -1);
|
|
|
|
+ OSIPhone::get_singleton()->touches_cancelled(tid);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ [self clearTouches];
|
|
|
|
+}
|
|
|
|
|
|
-// Stop animating and release resources when they are no longer needed.
|
|
|
|
-- (void)dealloc {
|
|
|
|
- [self stopAnimation];
|
|
|
|
|
|
+// MARK: Motion
|
|
|
|
|
|
- if ([EAGLContext currentContext] == context) {
|
|
|
|
- [EAGLContext setCurrentContext:nil];
|
|
|
|
|
|
+- (void)handleMotion {
|
|
|
|
+ if (!self.motionManager) {
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
|
|
- [context release];
|
|
|
|
- context = nil;
|
|
|
|
-
|
|
|
|
- [super dealloc];
|
|
|
|
-}
|
|
|
|
|
|
+ // Just using polling approach for now, we can set this up so it sends
|
|
|
|
+ // data to us in intervals, might be better. See Apple reference pages
|
|
|
|
+ // for more details:
|
|
|
|
+ // https://developer.apple.com/reference/coremotion/cmmotionmanager?language=objc
|
|
|
|
|
|
-- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
|
|
|
|
|
+ // Apple splits our accelerometer date into a gravity and user movement
|
|
|
|
+ // component. We add them back together
|
|
|
|
+ CMAcceleration gravity = self.motionManager.deviceMotion.gravity;
|
|
|
|
+ CMAcceleration acceleration = self.motionManager.deviceMotion.userAcceleration;
|
|
|
|
|
|
- if (object == _instance.avPlayerItem && [keyPath isEqualToString:@"status"]) {
|
|
|
|
- if (_instance.avPlayerItem.status == AVPlayerItemStatusFailed || _instance.avPlayer.status == AVPlayerStatusFailed) {
|
|
|
|
- _stop_video();
|
|
|
|
- video_found_error = true;
|
|
|
|
- }
|
|
|
|
|
|
+ ///@TODO We don't seem to be getting data here, is my device broken or
|
|
|
|
+ /// is this code incorrect?
|
|
|
|
+ CMMagneticField magnetic = self.motionManager.deviceMotion.magneticField.field;
|
|
|
|
|
|
- if (_instance.avPlayer.status == AVPlayerStatusReadyToPlay &&
|
|
|
|
- _instance.avPlayerItem.status == AVPlayerItemStatusReadyToPlay &&
|
|
|
|
- CMTIME_COMPARE_INLINE(video_current_time, ==, kCMTimeZero)) {
|
|
|
|
|
|
+ ///@TODO we can access rotationRate as a CMRotationRate variable
|
|
|
|
+ ///(processed date) or CMGyroData (raw data), have to see what works
|
|
|
|
+ /// best
|
|
|
|
+ CMRotationRate rotation = self.motionManager.deviceMotion.rotationRate;
|
|
|
|
|
|
- //NSLog(@"time: %@", video_current_time);
|
|
|
|
|
|
+ // Adjust for screen orientation.
|
|
|
|
+ // [[UIDevice currentDevice] orientation] changes even if we've fixed
|
|
|
|
+ // our orientation which is not a good thing when you're trying to get
|
|
|
|
+ // your user to move the screen in all directions and want consistent
|
|
|
|
+ // output
|
|
|
|
|
|
- [_instance.avPlayer seekToTime:video_current_time];
|
|
|
|
- video_current_time = kCMTimeZero;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ ///@TODO Using [[UIApplication sharedApplication] statusBarOrientation]
|
|
|
|
+ /// is a bit of a hack. Godot obviously knows the orientation so maybe
|
|
|
|
+ /// we
|
|
|
|
+ // can use that instead? (note that left and right seem swapped)
|
|
|
|
|
|
- if (object == _instance.avPlayer && [keyPath isEqualToString:@"rate"]) {
|
|
|
|
- NSLog(@"Player playback rate changed: %.5f", _instance.avPlayer.rate);
|
|
|
|
- if (_is_video_playing() && _instance.avPlayer.rate == 0.0 && !_instance.avPlayer.error) {
|
|
|
|
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
|
|
|
|
- [_instance.avPlayer play]; // NOTE: change this line according your current player implementation
|
|
|
|
- NSLog(@"resumed play");
|
|
|
|
- });
|
|
|
|
|
|
+ UIInterfaceOrientation interfaceOrientation = UIInterfaceOrientationUnknown;
|
|
|
|
|
|
- NSLog(@" . . . PAUSED (or just started)");
|
|
|
|
- }
|
|
|
|
|
|
+ if (@available(iOS 13, *)) {
|
|
|
|
+ interfaceOrientation = [UIApplication sharedApplication].delegate.window.windowScene.interfaceOrientation;
|
|
|
|
+ } else {
|
|
|
|
+ interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
|
|
-- (void)playerItemDidReachEnd:(NSNotification *)notification {
|
|
|
|
- _stop_video();
|
|
|
|
|
|
+ switch (interfaceOrientation) {
|
|
|
|
+ case UIInterfaceOrientationLandscapeLeft: {
|
|
|
|
+ OSIPhone::get_singleton()->update_gravity(-gravity.y, gravity.x, gravity.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_accelerometer(-(acceleration.y + gravity.y), (acceleration.x + gravity.x), acceleration.z + gravity.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_magnetometer(-magnetic.y, magnetic.x, magnetic.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_gyroscope(-rotation.y, rotation.x, rotation.z);
|
|
|
|
+ } break;
|
|
|
|
+ case UIInterfaceOrientationLandscapeRight: {
|
|
|
|
+ OSIPhone::get_singleton()->update_gravity(gravity.y, -gravity.x, gravity.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_accelerometer((acceleration.y + gravity.y), -(acceleration.x + gravity.x), acceleration.z + gravity.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_magnetometer(magnetic.y, -magnetic.x, magnetic.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_gyroscope(rotation.y, -rotation.x, rotation.z);
|
|
|
|
+ } break;
|
|
|
|
+ case UIInterfaceOrientationPortraitUpsideDown: {
|
|
|
|
+ OSIPhone::get_singleton()->update_gravity(-gravity.x, gravity.y, gravity.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_accelerometer(-(acceleration.x + gravity.x), (acceleration.y + gravity.y), acceleration.z + gravity.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_magnetometer(-magnetic.x, magnetic.y, magnetic.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_gyroscope(-rotation.x, rotation.y, rotation.z);
|
|
|
|
+ } break;
|
|
|
|
+ default: { // assume portrait
|
|
|
|
+ OSIPhone::get_singleton()->update_gravity(gravity.x, gravity.y, gravity.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_accelerometer(acceleration.x + gravity.x, acceleration.y + gravity.y, acceleration.z + gravity.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_magnetometer(magnetic.x, magnetic.y, magnetic.z);
|
|
|
|
+ OSIPhone::get_singleton()->update_gyroscope(rotation.x, rotation.y, rotation.z);
|
|
|
|
+ } break;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
@end
|
|
@end
|