Add OpenVulkanoOrientationLockableViewController and make openVulkanoView accesible from swift

This commit is contained in:
Georg Hagen
2024-06-05 16:57:11 +02:00
parent cd414c352e
commit 9965a5ba1f
3 changed files with 77 additions and 14 deletions

View File

@@ -0,0 +1,59 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#import "OpenVulkanoViewController.h"
#pragma mark -
#pragma mark OpenVulkanoViewController
@implementation OpenVulkanoOrientationLockableViewController
-(id)init {
[super init];
_rotationLockedView = nil;
return self;
}
-(void) viewDidLoad {
[super viewDidLoad];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
if (_rotationLockedView != nil)
{
_rotationLockedView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));
}
}
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
if (_rotationLockedView != nil)
{
[coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) {
CGAffineTransform deltaTransform = coordinator.targetTransform;
CGFloat deltaAngle = atan2f(deltaTransform.b, deltaTransform.a);
CGFloat currentRotation = [[_rotationLockedView.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
// Adding a small value to the rotation angle forces the animation to occur in a the desired direction, preventing an issue where the view would appear to rotate 2PI radians during a rotation from LandscapeRight -> LandscapeLeft.
currentRotation += -1 * deltaAngle + 0.0001;
[_rotationLockedView.layer setValue:@(currentRotation) forKeyPath:@"transform.rotation.z"];
} completion:^(id <UIViewControllerTransitionCoordinatorContext> context) {
// Integralize the transform to undo the extra 0.0001 added to the rotation angle.
CGAffineTransform currentTransform = _rotationLockedView.transform;
currentTransform.a = round(currentTransform.a);
currentTransform.b = round(currentTransform.b);
currentTransform.c = round(currentTransform.c);
currentTransform.d = round(currentTransform.d);
_rotationLockedView.transform = currentTransform;
}];
}
}
@end

View File

@@ -13,10 +13,15 @@
#pragma mark OpenVulkanoViewController
@interface OpenVulkanoViewController : UIViewController
{
@public OpenVulkanoView* openVulkanoView;
@public BOOL fixOpenVulkanoViewOrientation;
}
@property (nonatomic, strong) OpenVulkanoView* openVulkanoView;
-(void*) makeGraphicsApp;
@end
@interface OpenVulkanoOrientationLockableViewController : OpenVulkanoViewController
@property (nonatomic, strong) UIView* rotationLockedView;
@end

View File

@@ -30,8 +30,7 @@ using namespace OpenVulkano;
-(id)init {
[super init];
openVulkanoView = nil;
fixOpenVulkanoViewOrientation = false;
_openVulkanoView = nil;
return self;
}
@@ -44,40 +43,40 @@ using namespace OpenVulkano;
-(void) viewDidLoad {
[super viewDidLoad];
if (openVulkanoView == nil && [self.view isKindOfClass:[OpenVulkanoView class]]) {
openVulkanoView = (OpenVulkanoView*)self.view;
if (_openVulkanoView == nil && [self.view isKindOfClass:[OpenVulkanoView class]]) {
_openVulkanoView = (OpenVulkanoView*)self.view;
}
//TODO check if type is correct
IGraphicsApp* appPtr = static_cast<IGraphicsApp*>(self.makeGraphicsApp);
if (!appPtr) throw std::runtime_error("Failed to create graphics app");
app = std::unique_ptr<IGraphicsApp>(appPtr);
manager = new GraphicsAppManager(app.get(), (IVulkanWindow*)[openVulkanoView GetWindow]);
manager = new GraphicsAppManager(app.get(), (IVulkanWindow*)[_openVulkanoView GetWindow]);
manager->StartUp();
}
-(void) viewWillAppear:(BOOL)animated {
if (openVulkanoView != nil) { [openVulkanoView willAppear]; }
if (_openVulkanoView != nil) { [_openVulkanoView WillAppear]; }
[super viewWillAppear:animated];
}
-(void) viewDidAppear:(BOOL)animated {
if (openVulkanoView != nil) { [openVulkanoView DidAppear]; }
if (_openVulkanoView != nil) { [_openVulkanoView DidAppear]; }
[super viewDidAppear:animated];
}
-(void) viewWillDisappear:(BOOL)animated {
if (openVulkanoView != nil) { [openVulkanoView WillDisappear]; }
if (_openVulkanoView != nil) { [_openVulkanoView WillDisappear]; }
[super viewWillDisappear:animated];
}
-(void) viewDidDisappear:(BOOL)animated {
if (openVulkanoView != nil) { [openVulkanoView DidDisappear]; }
if (_openVulkanoView != nil) { [_openVulkanoView DidDisappear]; }
[super viewDidDisappear:animated];
}
-(void) viewDidUnload {
if (openVulkanoView != nil) { [openVulkanoView DidUnload]; }
if (_openVulkanoView != nil) { [_openVulkanoView DidUnload]; }
[super viewDidUnload];
}