diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2018-12-26 07:03:10 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2018-12-26 07:03:10 +0000 |
| commit | 8f9c8d11488b376f31598b120a2dbf055975dc5e (patch) | |
| tree | a7c77472321299e0c1b1690e4551b51ff192708c /Common/NativeApp/Apple/Source | |
| parent | Improved implementation of Vulkan mode in Mac native app (diff) | |
| download | DiligentEngine-8f9c8d11488b376f31598b120a2dbf055975dc5e.tar.gz DiligentEngine-8f9c8d11488b376f31598b120a2dbf055975dc5e.zip | |
Imprved MacOS app implementation: unified functionality common for GL and Vulkan
Diffstat (limited to 'Common/NativeApp/Apple/Source')
13 files changed, 324 insertions, 85 deletions
diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/AppDelegate.m b/Common/NativeApp/Apple/Source/Classes/OSX/AppDelegate.m index 3eb1f46..4e8e0f9 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/AppDelegate.m +++ b/Common/NativeApp/Apple/Source/Classes/OSX/AppDelegate.m @@ -7,7 +7,8 @@ */ #import "AppDelegate.h" -#import "GLView.h" +#import "GLViewController.h" +#import "MetalViewController.h" @interface AppDelegate () @@ -18,7 +19,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSWindow* mainWindow = [[NSApplication sharedApplication]mainWindow]; - NSString* error = [[mainWindow contentView] getError]; + NSString* error = [[mainWindow contentViewController] getError]; if(error != nil) { NSAlert *alert = [[NSAlert alloc] init]; @@ -31,8 +32,11 @@ } [mainWindow setAcceptsMouseMovedEvents:YES]; - NSString *Name = [[mainWindow contentView] getAppName]; - [mainWindow setTitle:Name]; + NSString *name = [[mainWindow contentViewController] getAppName]; + if(name != nil) + { + [mainWindow setTitle:name]; + } } - (void)applicationWillTerminate:(NSNotification *)aNotification { diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/GLView.h b/Common/NativeApp/Apple/Source/Classes/OSX/GLView.h index 7ef94fb..6430751 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/GLView.h +++ b/Common/NativeApp/Apple/Source/Classes/OSX/GLView.h @@ -9,6 +9,7 @@ #import <Cocoa/Cocoa.h> #import <QuartzCore/CVDisplayLink.h> +#include "NativeAppBase.h" @interface GLView : NSOpenGLView { CVDisplayLinkRef displayLink; @@ -16,7 +17,7 @@ -(void)stopDisplayLink; -(void)startDisplayLink; --(NSString*)getAppName; -(NSString*)getError; +-(NativeAppBase*)getApp; @end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/GLView.m b/Common/NativeApp/Apple/Source/Classes/OSX/GLView.m index fac04b8..aafc32d 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/GLView.m +++ b/Common/NativeApp/Apple/Source/Classes/OSX/GLView.m @@ -10,7 +10,6 @@ #include <string> #import "GLView.h" -#include "NativeAppBase.h" #ifndef SUPPORT_RETINA_RESOLUTION #define SUPPORT_RETINA_RESOLUTION 1 @@ -267,64 +266,8 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, [super dealloc]; } -- (void)mouseMove:(NSEvent *)theEvent { - NSPoint curPoint = [self convertPoint:[theEvent locationInWindow] fromView:nil]; -#if SUPPORT_RETINA_RESOLUTION - curPoint = [self convertPointToBacking:curPoint]; -#endif - if(_theApp) - _theApp->OnMouseMove(curPoint.x, _viewRectPixels.size.height-1 - curPoint.y); -} - -- (void)mouseDown:(NSEvent *)theEvent { - [self mouseMove: theEvent]; - if(_theApp) - _theApp->OnMouseDown(1); -} - -- (void)mouseUp:(NSEvent *)theEvent { - [self mouseMove: theEvent]; - if(_theApp) - _theApp->OnMouseUp(1); -} - -- (void)rightMouseDown:(NSEvent *)theEvent { - [self mouseMove: theEvent]; - if(_theApp) - _theApp->OnMouseDown(3); -} - -- (void)rightMouseUp:(NSEvent *)theEvent { - [self mouseMove: theEvent]; - if(_theApp) - _theApp->OnMouseUp(3); -} - -- (void)mouseMoved:(NSEvent *)theEvent { - [self mouseMove: theEvent]; -} - -- (void)mouseDragged:(NSEvent *)theEvent { - [self mouseMove: theEvent]; -} - -- (void)keyDown:(NSEvent *)theEvent { - unichar c = [[theEvent charactersIgnoringModifiers] characterAtIndex:0]; - int key = 0; - switch(c){ - case NSLeftArrowFunctionKey: key = 260; break; - case NSRightArrowFunctionKey: key = 262; break; - case 0x7F: key = '\b'; break; - default: key = c; - } - if(_theApp) - _theApp->OnKeyPressed(key); - - [super keyDown:theEvent]; -} - - (BOOL)acceptsFirstResponder { - return YES; + return YES; // To make keyboard events work } - (void)stopDisplayLink @@ -337,14 +280,14 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, CVDisplayLinkStart(displayLink); } -- (NSString*)getAppName +- (NSString*)getError { - return [NSString stringWithFormat:@"%s", _theApp ? _theApp->GetAppTitle() : ""]; + return _error.empty() ? nil : [NSString stringWithFormat:@"%s", _error.c_str()]; } -- (NSString*)getError +- (NativeAppBase*)getApp { - return _error.empty() ? nil : [NSString stringWithFormat:@"%s", _error.c_str()]; + return _theApp.get(); } @end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/GLViewController.h b/Common/NativeApp/Apple/Source/Classes/OSX/GLViewController.h new file mode 100644 index 0000000..e709ffe --- /dev/null +++ b/Common/NativeApp/Apple/Source/Classes/OSX/GLViewController.h @@ -0,0 +1,31 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#import "ViewControllerBase.h" + +@interface GLViewController : ViewControllerBase + +-(NSString*)getAppName; +-(NSString*)getError; + +@end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/GLViewController.mm b/Common/NativeApp/Apple/Source/Classes/OSX/GLViewController.mm new file mode 100644 index 0000000..430616f --- /dev/null +++ b/Common/NativeApp/Apple/Source/Classes/OSX/GLViewController.mm @@ -0,0 +1,47 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#import "GLViewController.h" +#import "GLView.h" + +@implementation GLViewController + +- (void)viewDidLoad +{ + [super viewDidLoad]; + + _theApp = [(GLView*)self.view getApp]; +} + +-(NSString*)getAppName +{ + return [NSString stringWithFormat:@"%s (OpenGL)", _theApp ? _theApp->GetAppTitle() : ""]; +} + +-(NSString*)getError +{ + return [(GLView*)self.view getError]; +} + +@end + diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.h b/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.h index 53b2a63..d62dc81 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.h +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.h @@ -23,11 +23,13 @@ #import <AppKit/AppKit.h> #import <MetalKit/MetalKit.h> +#include "NativeAppBase.h" // Implementation of MTKViewDelegate @interface MTKRenderer : NSObject<MTKViewDelegate> - (nonnull instancetype)initWithMetalKitView:(nonnull MTKView *)mtkView; +- (NativeAppBase*)getApp; @end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.mm b/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.mm index 175c46d..75500b6 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.mm +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MTKRenderer.mm @@ -41,7 +41,7 @@ _theApp.reset(CreateApplication()); _theApp->Initialize(mtkView); } - + return self; } @@ -50,7 +50,7 @@ { NSRect viewRectPoints = [view bounds]; NSRect viewRectPixels = [view convertRectToBacking:viewRectPoints]; - + _theApp->WindowResize(viewRectPixels.size.width, viewRectPixels.size.height); } @@ -62,5 +62,10 @@ _theApp->Present(); } +- (NativeAppBase*)getApp +{ + return _theApp.get(); +} + @end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.h b/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.h new file mode 100644 index 0000000..fedef45 --- /dev/null +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.h @@ -0,0 +1,32 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + + +#import <MetalKit/MetalKit.h> +#include "NativeAppBase.h" + +@interface MetalView : MTKView +{ +} + +@end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.m b/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.m new file mode 100644 index 0000000..0fd4cb7 --- /dev/null +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MetalView.m @@ -0,0 +1,36 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#import "MetalView.h" + +@interface MetalView () + +@end + +@implementation MetalView + +- (BOOL)acceptsFirstResponder { + return YES; // To make keyboard events work +} + +@end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.h b/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.h index bfe4a01..4c31feb 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.h +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.h @@ -21,15 +21,11 @@ * of the possibility of such damages. */ -#if defined(TARGET_IOS) -#import <UIKit/UIKit.h> -#define PlatformViewController UIViewController -#else -#import <AppKit/AppKit.h> -#define PlatformViewController NSViewController -#endif +#import "ViewControllerBase.h" -// The main view controller for the app storyboard. -@interface MetalViewController : NSViewController +@interface MetalViewController : ViewControllerBase + +-(NSString*)getAppName; +-(NSString*)getError; @end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.mm b/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.mm index a3d821f..985f442 100644 --- a/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.mm +++ b/Common/NativeApp/Apple/Source/Classes/OSX/MetalViewController.mm @@ -21,6 +21,7 @@ * of the possibility of such damages. */ +#import "MetalView.h" #import "MetalViewController.h" #import "MTKRenderer.h" @@ -33,23 +34,32 @@ - (void)viewDidLoad { [super viewDidLoad]; - - // Set the view to use the default device + _view = (MTKView *)self.view; - + _renderer = [[MTKRenderer alloc] initWithMetalKitView:_view]; - + _theApp = [_renderer getApp]; + if(!_renderer) { NSLog(@"Renderer failed initialization"); return; } - + // Initialize our renderer with the view size [_renderer mtkView:_view drawableSizeWillChange:_view.drawableSize]; - + _view.delegate = _renderer; } -@end +-(NSString*)getAppName +{ + return [NSString stringWithFormat:@"%s (Vulkan)", _theApp ? _theApp->GetAppTitle() : ""]; +} + +-(NSString*)getError +{ + return nil; +} +@end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/ViewControllerBase.h b/Common/NativeApp/Apple/Source/Classes/OSX/ViewControllerBase.h new file mode 100644 index 0000000..55341b0 --- /dev/null +++ b/Common/NativeApp/Apple/Source/Classes/OSX/ViewControllerBase.h @@ -0,0 +1,32 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + + +#import <AppKit/AppKit.h> +#include "NativeAppBase.h" + +@interface ViewControllerBase : NSViewController +{ + NativeAppBase* _theApp; +} +@end diff --git a/Common/NativeApp/Apple/Source/Classes/OSX/ViewControllerBase.mm b/Common/NativeApp/Apple/Source/Classes/OSX/ViewControllerBase.mm new file mode 100644 index 0000000..9eedb10 --- /dev/null +++ b/Common/NativeApp/Apple/Source/Classes/OSX/ViewControllerBase.mm @@ -0,0 +1,100 @@ +/* Copyright 2015-2018 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#import "ViewControllerBase.h" +#import "GLView.h" +#import "MetalView.h" + +#ifndef SUPPORT_RETINA_RESOLUTION +#define SUPPORT_RETINA_RESOLUTION 1 +#endif + +@implementation ViewControllerBase +{ + +} + +- (void)mouseMove:(NSEvent *)theEvent { + NSPoint curPoint = [self.view convertPoint:[theEvent locationInWindow] fromView:nil]; + + NSRect viewRectPoints = [self.view bounds]; +#if SUPPORT_RETINA_RESOLUTION + NSRect viewRectPixels = [self.view convertRectToBacking:viewRectPoints]; + curPoint = [self.view convertPointToBacking:curPoint]; +#else + // Points:Pixels is always 1:1 when not supporting retina resolutions + NSRect viewRectPixels = viewRectPoints; +#endif + + if(_theApp) + _theApp->OnMouseMove(curPoint.x, viewRectPixels.size.height-1 - curPoint.y); +} + +- (void)mouseDown:(NSEvent *)theEvent { + [self mouseMove: theEvent]; + if(_theApp) + _theApp->OnMouseDown(1); +} + +- (void)mouseUp:(NSEvent *)theEvent { + [self mouseMove: theEvent]; + if(_theApp) + _theApp->OnMouseUp(1); +} + +- (void)rightMouseDown:(NSEvent *)theEvent { + [self mouseMove: theEvent]; + if(_theApp) + _theApp->OnMouseDown(3); +} + +- (void)rightMouseUp:(NSEvent *)theEvent { + [self mouseMove: theEvent]; + if(_theApp) + _theApp->OnMouseUp(3); +} + +- (void)mouseMoved:(NSEvent *)theEvent { + [self mouseMove: theEvent]; +} + +- (void)mouseDragged:(NSEvent *)theEvent { + [self mouseMove: theEvent]; +} + +- (void)keyDown:(NSEvent *)theEvent { + unichar c = [[theEvent charactersIgnoringModifiers] characterAtIndex:0]; + int key = 0; + switch(c){ + case NSLeftArrowFunctionKey: key = 260; break; + case NSRightArrowFunctionKey: key = 262; break; + case 0x7F: key = '\b'; break; + default: key = c; + } + if(_theApp) + _theApp->OnKeyPressed(key); + + [super keyDown:theEvent]; +} + +@end |
