boo/lib/mac/ApplicationCocoa.mm

257 lines
8.0 KiB
Plaintext
Raw Normal View History

2015-05-06 00:50:57 +00:00
#include <AppKit/AppKit.h>
2015-11-01 00:06:56 +00:00
#include <thread>
2015-05-06 00:50:57 +00:00
2015-09-02 19:09:13 +00:00
#include "boo/IApplication.hpp"
#include "boo/graphicsdev/Metal.hpp"
2015-11-09 02:24:45 +00:00
#include "CocoaCommon.hpp"
2015-05-06 00:50:57 +00:00
2015-11-01 00:06:56 +00:00
namespace boo {class ApplicationCocoa;}
2015-05-06 00:50:57 +00:00
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
2015-11-01 00:06:56 +00:00
boo::ApplicationCocoa* m_app;
2015-05-06 01:03:49 +00:00
@public
2015-05-06 00:50:57 +00:00
NSPanel* aboutPanel;
}
2015-11-01 00:06:56 +00:00
- (id)initWithApp:(boo::ApplicationCocoa*)app;
2015-05-06 00:50:57 +00:00
@end
namespace boo
{
IWindow* _WindowCocoaNew(const SystemString& title, NSOpenGLContext* lastGLCtx, MetalContext* metalCtx);
2015-05-06 00:50:57 +00:00
2015-09-02 19:09:13 +00:00
class ApplicationCocoa : public IApplication
2015-05-06 00:50:57 +00:00
{
2015-11-01 00:06:56 +00:00
public:
NSApplication* m_app = nullptr;
2015-05-06 00:50:57 +00:00
IApplicationCallback& m_callback;
2015-11-01 00:06:56 +00:00
private:
2015-09-02 19:09:13 +00:00
const SystemString m_uniqueName;
const SystemString m_friendlyName;
const SystemString m_pname;
const std::vector<SystemString> m_args;
2015-05-06 00:50:57 +00:00
NSPanel* aboutPanel;
2015-11-01 00:06:56 +00:00
/* All windows */
std::unordered_map<NSWindow*, IWindow*> m_windows;
MetalContext m_metalCtx;
2015-05-06 00:50:57 +00:00
void _deletedWindow(IWindow* window)
{
2015-11-01 00:06:56 +00:00
m_windows.erase((NSWindow*)window->getPlatformHandle());
2015-05-06 00:50:57 +00:00
}
public:
2015-09-02 19:09:13 +00:00
ApplicationCocoa(IApplicationCallback& callback,
const SystemString& uniqueName,
const SystemString& friendlyName,
const SystemString& pname,
const std::vector<SystemString>& args)
2015-05-06 00:50:57 +00:00
: m_callback(callback),
2015-09-02 19:09:13 +00:00
m_uniqueName(uniqueName),
2015-05-06 00:50:57 +00:00
m_friendlyName(friendlyName),
m_pname(pname),
m_args(args)
{
2015-11-01 00:06:56 +00:00
m_app = [NSApplication sharedApplication];
[m_app setActivationPolicy:NSApplicationActivationPolicyRegular];
/* Delegate (OS X callbacks) */
AppDelegate* appDelegate = [[AppDelegate alloc] initWithApp:this];
[m_app setDelegate:appDelegate];
/* App menu */
NSMenu* appMenu = [[NSMenu alloc] initWithTitle:@"main"];
NSMenu* rwkMenu = [[NSMenu alloc] initWithTitle:[[NSString stringWithUTF8String:m_friendlyName.c_str()] autorelease]];
[rwkMenu addItemWithTitle:[[NSString stringWithFormat:@"About %s", m_friendlyName.c_str()] autorelease]
action:@selector(aboutApp:)
keyEquivalent:@""];
NSMenuItem* fsItem = [rwkMenu addItemWithTitle:@"Toggle Full Screen"
action:@selector(toggleFs:)
keyEquivalent:@"f"];
[fsItem setKeyEquivalentModifierMask:NSCommandKeyMask];
[rwkMenu addItem:[NSMenuItem separatorItem]];
NSMenuItem* quit_item = [rwkMenu addItemWithTitle:[[NSString stringWithFormat:@"Quit %s", m_friendlyName.c_str()] autorelease]
action:@selector(quitApp:)
keyEquivalent:@"q"];
[quit_item setKeyEquivalentModifierMask:NSCommandKeyMask];
[[appMenu addItemWithTitle:[[NSString stringWithUTF8String:m_friendlyName.c_str()] autorelease]
action:nil keyEquivalent:@""] setSubmenu:rwkMenu];
[[NSApplication sharedApplication] setMainMenu:appMenu];
/* About panel */
NSRect aboutCr = NSMakeRect(0, 0, 300, 220);
aboutPanel = [[NSPanel alloc] initWithContentRect:aboutCr
styleMask:NSUtilityWindowMask|NSTitledWindowMask|NSClosableWindowMask
backing:NSBackingStoreBuffered defer:YES];
[aboutPanel setTitle:[[NSString stringWithFormat:@"About %s", m_friendlyName.c_str()] autorelease]];
NSText* aboutText = [[NSText alloc] initWithFrame:aboutCr];
[aboutText setEditable:NO];
[aboutText setAlignment:NSCenterTextAlignment];
[aboutText setString:@"\nBoo Authors\n\nJackoalan\nAntidote\n"];
2015-11-01 00:06:56 +00:00
[aboutPanel setContentView:aboutText];
appDelegate->aboutPanel = aboutPanel;
2015-11-09 02:24:45 +00:00
/* Determine which graphics API to use */
for (const SystemString& arg : args)
if (!arg.compare("--metal"))
{
m_metalCtx.m_dev = MTLCreateSystemDefaultDevice();
m_metalCtx.m_q = [m_metalCtx.m_dev.get() newCommandQueue];
break;
}
2015-05-06 00:50:57 +00:00
}
2015-09-02 19:09:13 +00:00
EPlatformType getPlatformType() const
{
return PLAT_COCOA;
}
2015-11-01 00:06:56 +00:00
std::thread m_clientThread;
int m_clientReturn = 0;
int run()
2015-09-02 19:09:13 +00:00
{
2015-11-01 00:06:56 +00:00
/* Spawn client thread */
m_clientThread = std::thread([&]()
{
/* Run app */
m_clientReturn = m_callback.appMain(this);
/* Cleanup here */
for (auto& window : m_windows)
delete window.second;
});
/* Already in Cocoa's event loop; return now */
return 0;
2015-09-02 19:09:13 +00:00
}
2015-05-06 00:50:57 +00:00
void quit()
{
[NSApp terminate:nil];
}
2015-09-02 19:09:13 +00:00
const SystemString& getUniqueName() const
{
return m_uniqueName;
}
const SystemString& getFriendlyName() const
{
return m_friendlyName;
}
const SystemString& getProcessName() const
2015-05-06 00:50:57 +00:00
{
return m_pname;
}
2015-09-02 19:09:13 +00:00
const std::vector<SystemString>& getArgs() const
2015-05-06 00:50:57 +00:00
{
return m_args;
}
IWindow* newWindow(const std::string& title)
{
IWindow* newWindow = _WindowCocoaNew(title, m_lastGLCtx, &m_metalCtx);
2015-11-01 00:06:56 +00:00
m_windows[(NSWindow*)newWindow->getPlatformHandle()] = newWindow;
return newWindow;
2015-05-06 00:50:57 +00:00
}
2015-11-01 00:06:56 +00:00
/* Last GL context */
NSOpenGLContext* m_lastGLCtx = nullptr;
2015-05-06 00:50:57 +00:00
};
2015-11-01 00:06:56 +00:00
void _CocoaUpdateLastGLCtx(NSOpenGLContext* lastGLCtx)
{
static_cast<ApplicationCocoa*>(APP)->m_lastGLCtx = lastGLCtx;
}
2015-05-06 00:50:57 +00:00
2015-11-01 00:06:56 +00:00
IApplication* APP = nullptr;
int ApplicationRun(IApplication::EPlatformType platform,
IApplicationCallback& cb,
const SystemString& uniqueName,
const SystemString& friendlyName,
const SystemString& pname,
const std::vector<SystemString>& args,
bool singleInstance)
2015-05-06 00:50:57 +00:00
{
2015-11-01 00:06:56 +00:00
@autoreleasepool
2015-05-06 00:50:57 +00:00
{
2015-11-01 00:06:56 +00:00
if (!APP)
{
if (platform != IApplication::PLAT_COCOA &&
platform != IApplication::PLAT_AUTO)
return 1;
APP = new ApplicationCocoa(cb, uniqueName, friendlyName, pname, args);
}
[static_cast<ApplicationCocoa*>(APP)->m_app run];
return static_cast<ApplicationCocoa*>(APP)->m_clientReturn;
2015-05-06 00:50:57 +00:00
}
}
}
2015-11-01 00:06:56 +00:00
@implementation AppDelegate
- (id)initWithApp:(boo::ApplicationCocoa*)app
{
self = [super init];
m_app = app;
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
(void)notification;
m_app->run();
}
- (void)applicationWillTerminate:(NSNotification*)notification
{
(void)notification;
m_app->m_callback.appQuitting(m_app);
m_app->m_clientThread.join();
}
- (BOOL)application:(NSApplication*)sender openFile:(NSString*)filename
{
std::vector<boo::SystemString> strVec;
strVec.push_back(boo::SystemString([filename UTF8String]));
m_app->m_callback.appFilesOpen(boo::APP, strVec);
return true;
}
- (void)application:(NSApplication*)sender openFiles:(NSArray*)filenames
{
std::vector<boo::SystemString> strVec;
strVec.reserve([filenames count]);
for (NSString* str in filenames)
strVec.push_back(boo::SystemString([str UTF8String]));
m_app->m_callback.appFilesOpen(boo::APP, strVec);
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender
{
(void)sender;
return YES;
}
- (IBAction)aboutApp:(id)sender
{
(void)sender;
NSRect screenFrame = [[aboutPanel screen] frame];
CGFloat xPos = NSWidth(screenFrame)/2 - 300/2;
CGFloat yPos = NSHeight(screenFrame)/2 - 220/2;
NSRect aboutCr = NSMakeRect(xPos, yPos, 300, 220);
[aboutPanel setFrame:aboutCr display:NO];
[aboutPanel makeKeyAndOrderFront:self];
}
- (IBAction)toggleFs:(id)sender
{
(void)sender;
[[NSApp keyWindow] toggleFullScreen:nil];
}
- (IBAction)quitApp:(id)sender
{
(void)sender;
[NSApp terminate:nil];
}
@end