mirror of
https://github.com/AxioDL/boo.git
synced 2025-06-09 16:13:33 +00:00
Reformat Objective-C code with new style
This commit is contained in:
parent
9658d1372d
commit
2135f4e4dc
File diff suppressed because it is too large
Load Diff
@ -18,187 +18,174 @@
|
|||||||
* in CoreAnimation warnings). */
|
* in CoreAnimation warnings). */
|
||||||
#define COCOA_TERMINATE 1
|
#define COCOA_TERMINATE 1
|
||||||
|
|
||||||
namespace boo {class ApplicationCocoa;}
|
namespace boo { class ApplicationCocoa; }
|
||||||
@interface AppDelegate : NSObject <NSApplicationDelegate>
|
@interface AppDelegate : NSObject <NSApplicationDelegate> {
|
||||||
{
|
boo::ApplicationCocoa* m_app;
|
||||||
boo::ApplicationCocoa* m_app;
|
@public
|
||||||
@public
|
|
||||||
}
|
}
|
||||||
- (id)initWithApp:(boo::ApplicationCocoa*)app;
|
- (id)initWithApp:(boo::ApplicationCocoa*)app;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
namespace boo
|
namespace boo {
|
||||||
{
|
|
||||||
static logvisor::Module Log("boo::ApplicationCocoa");
|
static logvisor::Module Log("boo::ApplicationCocoa");
|
||||||
|
|
||||||
std::shared_ptr<IWindow> _WindowCocoaNew(SystemStringView title, MetalContext* metalCtx);
|
std::shared_ptr<IWindow> _WindowCocoaNew(SystemStringView title, MetalContext* metalCtx);
|
||||||
|
|
||||||
class ApplicationCocoa : public IApplication
|
class ApplicationCocoa : public IApplication {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
IApplicationCallback& m_callback;
|
IApplicationCallback& m_callback;
|
||||||
AppDelegate* m_appDelegate;
|
AppDelegate* m_appDelegate;
|
||||||
private:
|
private:
|
||||||
const SystemString m_uniqueName;
|
const SystemString m_uniqueName;
|
||||||
const SystemString m_friendlyName;
|
const SystemString m_friendlyName;
|
||||||
const SystemString m_pname;
|
const SystemString m_pname;
|
||||||
const std::vector<SystemString> m_args;
|
const std::vector<SystemString> m_args;
|
||||||
|
|
||||||
NSPanel* aboutPanel;
|
NSPanel* aboutPanel;
|
||||||
|
|
||||||
/* All windows */
|
/* All windows */
|
||||||
std::unordered_map<uintptr_t, std::weak_ptr<IWindow>> m_windows;
|
std::unordered_map<uintptr_t, std::weak_ptr<IWindow>> m_windows;
|
||||||
|
|
||||||
MetalContext m_metalCtx;
|
MetalContext m_metalCtx;
|
||||||
#if 0
|
#if 0
|
||||||
GLContext m_glCtx;
|
GLContext m_glCtx;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void _deletedWindow(IWindow* window)
|
void _deletedWindow(IWindow* window) {
|
||||||
{
|
m_windows.erase(window->getPlatformHandle());
|
||||||
m_windows.erase(window->getPlatformHandle());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ApplicationCocoa(IApplicationCallback& callback,
|
ApplicationCocoa(IApplicationCallback& callback,
|
||||||
SystemStringView uniqueName,
|
SystemStringView uniqueName,
|
||||||
SystemStringView friendlyName,
|
SystemStringView friendlyName,
|
||||||
SystemStringView pname,
|
SystemStringView pname,
|
||||||
const std::vector<SystemString>& args,
|
const std::vector<SystemString>& args,
|
||||||
std::string_view gfxApi,
|
std::string_view gfxApi,
|
||||||
uint32_t samples,
|
uint32_t samples,
|
||||||
uint32_t anisotropy,
|
uint32_t anisotropy,
|
||||||
bool deepColor)
|
bool deepColor)
|
||||||
: m_callback(callback),
|
: m_callback(callback),
|
||||||
m_uniqueName(uniqueName),
|
m_uniqueName(uniqueName),
|
||||||
m_friendlyName(friendlyName),
|
m_friendlyName(friendlyName),
|
||||||
m_pname(pname),
|
m_pname(pname),
|
||||||
m_args(args)
|
m_args(args) {
|
||||||
{
|
m_metalCtx.m_sampleCount = samples;
|
||||||
m_metalCtx.m_sampleCount = samples;
|
m_metalCtx.m_anisotropy = anisotropy;
|
||||||
m_metalCtx.m_anisotropy = anisotropy;
|
m_metalCtx.m_pixelFormat = deepColor ? MTLPixelFormatRGBA16Float : MTLPixelFormatBGRA8Unorm;
|
||||||
m_metalCtx.m_pixelFormat = deepColor ? MTLPixelFormatRGBA16Float : MTLPixelFormatBGRA8Unorm;
|
|
||||||
#if 0
|
#if 0
|
||||||
m_glCtx.m_sampleCount = samples;
|
m_glCtx.m_sampleCount = samples;
|
||||||
m_glCtx.m_anisotropy = anisotropy;
|
m_glCtx.m_anisotropy = anisotropy;
|
||||||
m_glCtx.m_deepColor = deepColor;
|
m_glCtx.m_deepColor = deepColor;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
[[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyRegular];
|
[[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyRegular];
|
||||||
|
|
||||||
/* Delegate (OS X callbacks) */
|
/* Delegate (OS X callbacks) */
|
||||||
m_appDelegate = [[AppDelegate alloc] initWithApp:this];
|
m_appDelegate = [[AppDelegate alloc] initWithApp:this];
|
||||||
[[NSApplication sharedApplication] setDelegate:m_appDelegate];
|
[[NSApplication sharedApplication] setDelegate:m_appDelegate];
|
||||||
|
|
||||||
/* App menu */
|
/* App menu */
|
||||||
NSMenu* rootMenu = [[NSMenu alloc] initWithTitle:@"main"];
|
NSMenu* rootMenu = [[NSMenu alloc] initWithTitle:@"main"];
|
||||||
NSMenu* appMenu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:m_friendlyName.c_str()]];
|
NSMenu* appMenu = [[NSMenu alloc] initWithTitle:[NSString stringWithUTF8String:m_friendlyName.c_str()]];
|
||||||
NSMenuItem* fsItem = [appMenu addItemWithTitle:@"Toggle Full Screen"
|
NSMenuItem* fsItem = [appMenu addItemWithTitle:@"Toggle Full Screen"
|
||||||
action:@selector(toggleFs:)
|
action:@selector(toggleFs:)
|
||||||
keyEquivalent:@"f"];
|
keyEquivalent:@"f"];
|
||||||
[fsItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
|
[fsItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
|
||||||
[appMenu addItem:[NSMenuItem separatorItem]];
|
[appMenu addItem:[NSMenuItem separatorItem]];
|
||||||
NSMenuItem* quitItem = [appMenu addItemWithTitle:[NSString stringWithFormat:@"Quit %s", m_friendlyName.c_str()]
|
NSMenuItem* quitItem = [appMenu addItemWithTitle:[NSString stringWithFormat:@"Quit %s", m_friendlyName.c_str()]
|
||||||
action:@selector(quitApp:)
|
action:@selector(quitApp:)
|
||||||
keyEquivalent:@"q"];
|
keyEquivalent:@"q"];
|
||||||
[quitItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
|
[quitItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
|
||||||
[[rootMenu addItemWithTitle:[NSString stringWithUTF8String:m_friendlyName.c_str()]
|
[[rootMenu addItemWithTitle:[NSString stringWithUTF8String:m_friendlyName.c_str()]
|
||||||
action:nil keyEquivalent:@""] setSubmenu:appMenu];
|
action:nil keyEquivalent:@""] setSubmenu:appMenu];
|
||||||
[[NSApplication sharedApplication] setMainMenu:rootMenu];
|
[[NSApplication sharedApplication] setMainMenu:rootMenu];
|
||||||
|
|
||||||
m_metalCtx.m_dev = MTLCreateSystemDefaultDevice();
|
m_metalCtx.m_dev = MTLCreateSystemDefaultDevice();
|
||||||
if (!m_metalCtx.m_dev)
|
if (!m_metalCtx.m_dev)
|
||||||
Log.report(logvisor::Fatal, "Unable to create metal device");
|
Log.report(logvisor::Fatal, "Unable to create metal device");
|
||||||
m_metalCtx.m_q = [m_metalCtx.m_dev newCommandQueue];
|
m_metalCtx.m_q = [m_metalCtx.m_dev newCommandQueue];
|
||||||
while (![m_metalCtx.m_dev supportsTextureSampleCount:m_metalCtx.m_sampleCount])
|
while (![m_metalCtx.m_dev supportsTextureSampleCount:m_metalCtx.m_sampleCount])
|
||||||
m_metalCtx.m_sampleCount = flp2(m_metalCtx.m_sampleCount - 1);
|
m_metalCtx.m_sampleCount = flp2(m_metalCtx.m_sampleCount - 1);
|
||||||
Log.report(logvisor::Info, "using Metal renderer");
|
Log.report(logvisor::Info, "using Metal renderer");
|
||||||
}
|
}
|
||||||
|
|
||||||
EPlatformType getPlatformType() const
|
EPlatformType getPlatformType() const {
|
||||||
{
|
return EPlatformType::Cocoa;
|
||||||
return EPlatformType::Cocoa;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
std::thread m_clientThread;
|
std::thread m_clientThread;
|
||||||
int m_clientReturn = 0;
|
int m_clientReturn = 0;
|
||||||
bool m_terminateNow = false;
|
bool m_terminateNow = false;
|
||||||
int run()
|
|
||||||
{
|
|
||||||
/* Spawn client thread */
|
|
||||||
m_clientThread = std::thread([&]()
|
|
||||||
{
|
|
||||||
std::string thrName = std::string(getFriendlyName()) + " Client Thread";
|
|
||||||
logvisor::RegisterThreadName(thrName.c_str());
|
|
||||||
|
|
||||||
/* Run app */
|
int run() {
|
||||||
m_clientReturn = m_callback.appMain(this);
|
/* Spawn client thread */
|
||||||
|
m_clientThread = std::thread([&]() {
|
||||||
|
std::string thrName = std::string(getFriendlyName()) + " Client Thread";
|
||||||
|
logvisor::RegisterThreadName(thrName.c_str());
|
||||||
|
|
||||||
/* Cleanup */
|
/* Run app */
|
||||||
for (auto& w : m_windows)
|
m_clientReturn = m_callback.appMain(this);
|
||||||
if (std::shared_ptr<IWindow> window = w.second.lock())
|
|
||||||
window->closeWindow();
|
/* Cleanup */
|
||||||
|
for (auto& w : m_windows)
|
||||||
|
if (std::shared_ptr<IWindow> window = w.second.lock())
|
||||||
|
window->closeWindow();
|
||||||
|
|
||||||
#if COCOA_TERMINATE
|
#if COCOA_TERMINATE
|
||||||
/* Continue termination */
|
/* Continue termination */
|
||||||
dispatch_sync(dispatch_get_main_queue(),
|
dispatch_sync(dispatch_get_main_queue(),
|
||||||
^{
|
^{
|
||||||
/* Ends modal run loop and continues Cocoa termination */
|
/* Ends modal run loop and continues Cocoa termination */
|
||||||
[NSApp replyToApplicationShouldTerminate:YES];
|
[NSApp replyToApplicationShouldTerminate:YES];
|
||||||
|
|
||||||
/* If this is reached, application didn't spawn any windows
|
/* If this is reached, application didn't spawn any windows
|
||||||
* and must be explicitly terminated */
|
* and must be explicitly terminated */
|
||||||
m_terminateNow = true;
|
m_terminateNow = true;
|
||||||
[NSApp terminate:nil];
|
[NSApp terminate:nil];
|
||||||
});
|
});
|
||||||
#else
|
#else
|
||||||
/* Return control to main() */
|
/* Return control to main() */
|
||||||
dispatch_sync(dispatch_get_main_queue(),
|
dispatch_sync(dispatch_get_main_queue(),
|
||||||
^{
|
^{
|
||||||
[[NSApplication sharedApplication] stop:nil];
|
[[NSApplication sharedApplication] stop:nil];
|
||||||
});
|
});
|
||||||
#endif
|
#endif
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Already in Cocoa's event loop; return now */
|
/* Already in Cocoa's event loop; return now */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void quit()
|
void quit() {
|
||||||
{
|
[NSApp terminate:nil];
|
||||||
[NSApp terminate:nil];
|
}
|
||||||
}
|
|
||||||
|
|
||||||
SystemStringView getUniqueName() const
|
SystemStringView getUniqueName() const {
|
||||||
{
|
return m_uniqueName;
|
||||||
return m_uniqueName;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
SystemStringView getFriendlyName() const
|
SystemStringView getFriendlyName() const {
|
||||||
{
|
return m_friendlyName;
|
||||||
return m_friendlyName;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
SystemStringView getProcessName() const
|
SystemStringView getProcessName() const {
|
||||||
{
|
return m_pname;
|
||||||
return m_pname;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const std::vector<SystemString>& getArgs() const
|
const std::vector<SystemString>& getArgs() const {
|
||||||
{
|
return m_args;
|
||||||
return m_args;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<IWindow> newWindow(std::string_view title)
|
std::shared_ptr<IWindow> newWindow(std::string_view title) {
|
||||||
{
|
auto newWindow = _WindowCocoaNew(title, &m_metalCtx);
|
||||||
auto newWindow = _WindowCocoaNew(title, &m_metalCtx);
|
m_windows[newWindow->getPlatformHandle()] = newWindow;
|
||||||
m_windows[newWindow->getPlatformHandle()] = newWindow;
|
return newWindow;
|
||||||
return newWindow;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Last GL context */
|
/* Last GL context */
|
||||||
#if 0
|
#if 0
|
||||||
NSOpenGLContext* m_lastGLCtx = nullptr;
|
NSOpenGLContext* m_lastGLCtx = nullptr;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -210,6 +197,7 @@ void _CocoaUpdateLastGLCtx(NSOpenGLContext* lastGLCtx)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
IApplication* APP = nullptr;
|
IApplication* APP = nullptr;
|
||||||
|
|
||||||
int ApplicationRun(IApplication::EPlatformType platform,
|
int ApplicationRun(IApplication::EPlatformType platform,
|
||||||
IApplicationCallback& cb,
|
IApplicationCallback& cb,
|
||||||
SystemStringView uniqueName,
|
SystemStringView uniqueName,
|
||||||
@ -220,56 +208,54 @@ int ApplicationRun(IApplication::EPlatformType platform,
|
|||||||
uint32_t samples,
|
uint32_t samples,
|
||||||
uint32_t anisotropy,
|
uint32_t anisotropy,
|
||||||
bool deepColor,
|
bool deepColor,
|
||||||
bool singleInstance)
|
bool singleInstance) {
|
||||||
{
|
std::string thrName = std::string(friendlyName) + " Main Thread";
|
||||||
std::string thrName = std::string(friendlyName) + " Main Thread";
|
logvisor::RegisterThreadName(thrName.c_str());
|
||||||
logvisor::RegisterThreadName(thrName.c_str());
|
@autoreleasepool {
|
||||||
@autoreleasepool
|
if (!APP) {
|
||||||
{
|
if (platform != IApplication::EPlatformType::Cocoa &&
|
||||||
if (!APP)
|
platform != IApplication::EPlatformType::Auto)
|
||||||
{
|
return 1;
|
||||||
if (platform != IApplication::EPlatformType::Cocoa &&
|
/* Never deallocated to ensure window destructors have access */
|
||||||
platform != IApplication::EPlatformType::Auto)
|
APP = new ApplicationCocoa(cb, uniqueName, friendlyName, pname, args,
|
||||||
return 1;
|
gfxApi, samples, anisotropy, deepColor);
|
||||||
/* Never deallocated to ensure window destructors have access */
|
|
||||||
APP = new ApplicationCocoa(cb, uniqueName, friendlyName, pname, args,
|
|
||||||
gfxApi, samples, anisotropy, deepColor);
|
|
||||||
}
|
|
||||||
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
|
|
||||||
if ([NSApp respondsToSelector:@selector(setAppearance:)])
|
|
||||||
[NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
|
|
||||||
#endif
|
|
||||||
[NSApp run];
|
|
||||||
ApplicationCocoa* appCocoa = static_cast<ApplicationCocoa*>(APP);
|
|
||||||
if (appCocoa->m_clientThread.joinable())
|
|
||||||
appCocoa->m_clientThread.join();
|
|
||||||
return appCocoa->m_clientReturn;
|
|
||||||
}
|
}
|
||||||
|
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
|
||||||
|
if ([NSApp respondsToSelector:@selector(setAppearance:)])
|
||||||
|
[NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
|
||||||
|
#endif
|
||||||
|
[NSApp run];
|
||||||
|
ApplicationCocoa* appCocoa = static_cast<ApplicationCocoa*>(APP);
|
||||||
|
if (appCocoa->m_clientThread.joinable())
|
||||||
|
appCocoa->m_clientThread.join();
|
||||||
|
return appCocoa->m_clientReturn;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@implementation AppDelegate
|
@implementation AppDelegate
|
||||||
- (id)initWithApp:(boo::ApplicationCocoa*)app
|
- (id)initWithApp:(boo::ApplicationCocoa*)app {
|
||||||
{
|
self = [super init];
|
||||||
self = [super init];
|
m_app = app;
|
||||||
m_app = app;
|
return self;
|
||||||
return self;
|
|
||||||
}
|
}
|
||||||
- (void)applicationDidFinishLaunching:(NSNotification*)notification
|
|
||||||
{
|
- (void)applicationDidFinishLaunching:(NSNotification*)notification {
|
||||||
(void)notification;
|
(void) notification;
|
||||||
m_app->run();
|
m_app->run();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if COCOA_TERMINATE
|
#if COCOA_TERMINATE
|
||||||
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app
|
|
||||||
{
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app {
|
||||||
(void)app;
|
(void) app;
|
||||||
if (m_app->m_terminateNow)
|
if (m_app->m_terminateNow)
|
||||||
return NSTerminateNow;
|
return NSTerminateNow;
|
||||||
m_app->m_callback.appQuitting(m_app);
|
m_app->m_callback.appQuitting(m_app);
|
||||||
return NSTerminateLater;
|
return NSTerminateLater;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app
|
||||||
{
|
{
|
||||||
@ -278,35 +264,35 @@ int ApplicationRun(IApplication::EPlatformType platform,
|
|||||||
return NSTerminateCancel;
|
return NSTerminateCancel;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
- (BOOL)application:(NSApplication*)sender openFile:(NSString*)filename
|
|
||||||
{
|
- (BOOL)application:(NSApplication*)sender openFile:(NSString*)filename {
|
||||||
std::vector<boo::SystemString> strVec;
|
std::vector<boo::SystemString> strVec;
|
||||||
strVec.push_back(boo::SystemString([filename UTF8String]));
|
strVec.push_back(boo::SystemString([filename UTF8String]));
|
||||||
m_app->m_callback.appFilesOpen(boo::APP, strVec);
|
m_app->m_callback.appFilesOpen(boo::APP, strVec);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
- (void)application:(NSApplication*)sender openFiles:(NSArray*)filenames
|
|
||||||
{
|
- (void)application:(NSApplication*)sender openFiles:(NSArray*)filenames {
|
||||||
std::vector<boo::SystemString> strVec;
|
std::vector<boo::SystemString> strVec;
|
||||||
strVec.reserve([filenames count]);
|
strVec.reserve([filenames count]);
|
||||||
for (NSString* str in filenames)
|
for (NSString* str in filenames)
|
||||||
strVec.push_back(boo::SystemString([str UTF8String]));
|
strVec.push_back(boo::SystemString([str UTF8String]));
|
||||||
m_app->m_callback.appFilesOpen(boo::APP, strVec);
|
m_app->m_callback.appFilesOpen(boo::APP, strVec);
|
||||||
}
|
}
|
||||||
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender
|
|
||||||
{
|
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender {
|
||||||
(void)sender;
|
(void) sender;
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
- (IBAction)toggleFs:(id)sender
|
|
||||||
{
|
- (IBAction)toggleFs:(id)sender {
|
||||||
(void)sender;
|
(void) sender;
|
||||||
[[NSApp keyWindow] toggleFullScreen:nil];
|
[[NSApp keyWindow] toggleFullScreen:nil];
|
||||||
}
|
}
|
||||||
- (IBAction)quitApp:(id)sender
|
|
||||||
{
|
- (IBAction)quitApp:(id)sender {
|
||||||
(void)sender;
|
(void) sender;
|
||||||
[NSApp terminate:nil];
|
[NSApp terminate:nil];
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user