2015-05-04 23:37:28 +00:00
|
|
|
#import <AppKit/AppKit.h>
|
2015-05-06 00:50:57 +00:00
|
|
|
#include "IApplication.hpp"
|
2015-05-04 23:37:28 +00:00
|
|
|
#include "windowsys/IWindow.hpp"
|
|
|
|
#include "windowsys/IGraphicsContext.hpp"
|
|
|
|
|
2015-05-05 07:16:06 +00:00
|
|
|
namespace boo {class CWindowCocoa;}
|
2015-05-04 23:37:28 +00:00
|
|
|
@interface CWindowCocoaInternal : NSWindow
|
2015-05-05 07:16:06 +00:00
|
|
|
{
|
|
|
|
boo::CWindowCocoa* booWindow;
|
|
|
|
}
|
2015-05-06 00:50:57 +00:00
|
|
|
- (id)initWithBooWindow:(boo::CWindowCocoa*)bw title:(const std::string&)title;
|
|
|
|
- (void)setFrameDefault;
|
|
|
|
- (NSRect)genFrameDefault;
|
2015-05-04 23:37:28 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
namespace boo
|
|
|
|
{
|
2015-05-06 00:50:57 +00:00
|
|
|
|
|
|
|
IGraphicsContext* _CGraphicsContextCocoaNew(IGraphicsContext::EGraphicsAPI api,
|
|
|
|
IWindow* parentWindow);
|
2015-05-04 23:37:28 +00:00
|
|
|
|
|
|
|
class CWindowCocoa final : public IWindow
|
|
|
|
{
|
|
|
|
|
|
|
|
CWindowCocoaInternal* m_nsWindow;
|
|
|
|
IGraphicsContext* m_gfxCtx;
|
2015-05-05 07:16:06 +00:00
|
|
|
|
2015-05-04 23:37:28 +00:00
|
|
|
public:
|
2015-05-05 07:16:06 +00:00
|
|
|
|
2015-05-06 00:50:57 +00:00
|
|
|
CWindowCocoa(const std::string& title)
|
2015-05-04 23:37:28 +00:00
|
|
|
{
|
2015-05-06 00:50:57 +00:00
|
|
|
m_nsWindow = [[CWindowCocoaInternal alloc] initWithBooWindow:this title:title];
|
|
|
|
m_gfxCtx = _CGraphicsContextCocoaNew(IGraphicsContext::API_OPENGL_3_3, this);
|
2015-05-04 23:37:28 +00:00
|
|
|
m_gfxCtx->initializeContext();
|
|
|
|
}
|
|
|
|
|
2015-05-06 00:50:57 +00:00
|
|
|
void _clearWindow()
|
|
|
|
{
|
|
|
|
m_nsWindow = NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-04 23:37:28 +00:00
|
|
|
~CWindowCocoa()
|
|
|
|
{
|
|
|
|
[m_nsWindow orderOut:nil];
|
|
|
|
[m_nsWindow release];
|
|
|
|
delete m_gfxCtx;
|
2015-05-06 00:50:57 +00:00
|
|
|
IApplicationInstance()->_deletedWindow(this);
|
2015-05-04 23:37:28 +00:00
|
|
|
}
|
|
|
|
|
2015-05-05 07:16:06 +00:00
|
|
|
void setCallback(IWindowCallback* cb)
|
|
|
|
{
|
|
|
|
m_gfxCtx->_setCallback(cb);
|
|
|
|
}
|
|
|
|
|
2015-05-04 23:37:28 +00:00
|
|
|
void showWindow()
|
|
|
|
{
|
|
|
|
[m_nsWindow makeKeyAndOrderFront:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
void hideWindow()
|
|
|
|
{
|
|
|
|
[m_nsWindow orderOut:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getTitle()
|
|
|
|
{
|
|
|
|
return [[m_nsWindow title] UTF8String];
|
|
|
|
}
|
|
|
|
|
|
|
|
void setTitle(const std::string& title)
|
|
|
|
{
|
2015-05-06 00:50:57 +00:00
|
|
|
[m_nsWindow setTitle:[[NSString stringWithUTF8String:title.c_str()] autorelease]];
|
2015-05-04 23:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setWindowFrameDefault()
|
|
|
|
{
|
|
|
|
NSScreen* mainScreen = [NSScreen mainScreen];
|
|
|
|
NSRect scrFrame = mainScreen.frame;
|
|
|
|
float x_off = scrFrame.size.width / 3.0;
|
|
|
|
float y_off = scrFrame.size.height / 3.0;
|
|
|
|
[m_nsWindow setFrame:NSMakeRect(x_off, y_off, x_off * 2.0, y_off * 2.0) display:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
void getWindowFrame(float& xOut, float& yOut, float& wOut, float& hOut) const
|
|
|
|
{
|
|
|
|
NSRect wFrame = m_nsWindow.frame;
|
|
|
|
xOut = wFrame.origin.x;
|
|
|
|
yOut = wFrame.origin.y;
|
|
|
|
wOut = wFrame.size.width;
|
|
|
|
hOut = wFrame.size.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setWindowFrame(float x, float y, float w, float h)
|
|
|
|
{
|
|
|
|
NSRect wFrame = NSMakeRect(x, y, w, h);
|
|
|
|
[m_nsWindow setFrame:wFrame display:NO];
|
|
|
|
}
|
|
|
|
|
2015-05-05 07:16:06 +00:00
|
|
|
float getVirtualPixelFactor() const
|
|
|
|
{
|
|
|
|
return [m_nsWindow backingScaleFactor];
|
|
|
|
}
|
|
|
|
|
2015-05-04 23:37:28 +00:00
|
|
|
bool isFullscreen() const
|
|
|
|
{
|
|
|
|
return ([m_nsWindow styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFullscreen(bool fs)
|
|
|
|
{
|
|
|
|
if ((fs && !isFullscreen()) || (!fs && isFullscreen()))
|
|
|
|
[m_nsWindow toggleFullScreen:nil];
|
|
|
|
}
|
|
|
|
|
2015-05-05 07:16:06 +00:00
|
|
|
ETouchType getTouchType() const
|
|
|
|
{
|
|
|
|
return TOUCH_TRACKPAD;
|
|
|
|
}
|
|
|
|
|
2015-05-09 05:33:48 +00:00
|
|
|
uintptr_t getPlatformHandle() const
|
2015-05-06 00:50:57 +00:00
|
|
|
{
|
2015-05-09 05:33:48 +00:00
|
|
|
return (uintptr_t)m_nsWindow;
|
2015-05-06 00:50:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-04 23:37:28 +00:00
|
|
|
};
|
|
|
|
|
2015-05-06 00:50:57 +00:00
|
|
|
IWindow* _CWindowCocoaNew(const std::string& title)
|
2015-05-04 23:37:28 +00:00
|
|
|
{
|
2015-05-06 00:50:57 +00:00
|
|
|
return new CWindowCocoa(title);
|
2015-05-04 23:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation CWindowCocoaInternal
|
2015-05-06 00:50:57 +00:00
|
|
|
- (id)initWithBooWindow:(boo::CWindowCocoa *)bw title:(const std::string&)title
|
2015-05-04 23:37:28 +00:00
|
|
|
{
|
2015-05-06 00:50:57 +00:00
|
|
|
self = [self initWithContentRect:[self genFrameDefault]
|
2015-05-05 07:16:06 +00:00
|
|
|
styleMask:NSTitledWindowMask|
|
|
|
|
NSClosableWindowMask|
|
|
|
|
NSMiniaturizableWindowMask|
|
|
|
|
NSResizableWindowMask
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:YES];
|
2015-05-06 00:50:57 +00:00
|
|
|
self.title = [[NSString stringWithUTF8String:title.c_str()] autorelease];
|
2015-05-05 07:16:06 +00:00
|
|
|
booWindow = bw;
|
|
|
|
return self;
|
2015-05-04 23:37:28 +00:00
|
|
|
}
|
2015-05-06 00:50:57 +00:00
|
|
|
- (void)setFrameDefault
|
|
|
|
{
|
|
|
|
[self setFrame:[self genFrameDefault] display:NO];
|
|
|
|
}
|
|
|
|
- (NSRect)genFrameDefault
|
|
|
|
{
|
|
|
|
NSScreen* mainScreen = [NSScreen mainScreen];
|
|
|
|
NSRect scrFrame = mainScreen.frame;
|
|
|
|
float width = scrFrame.size.width * 2.0 / 3.0;
|
|
|
|
float height = scrFrame.size.height * 2.0 / 3.0;
|
|
|
|
return NSMakeRect((scrFrame.size.width - width) / 2.0,
|
|
|
|
(scrFrame.size.height - height) / 2.0,
|
|
|
|
width, height);
|
|
|
|
}
|
|
|
|
- (void)close
|
|
|
|
{
|
|
|
|
booWindow->_clearWindow();
|
|
|
|
[super close];
|
|
|
|
}
|
|
|
|
- (BOOL)acceptsFirstResponder
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
- (BOOL)acceptsMouseMovedEvents
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
- (NSWindowCollectionBehavior)collectionBehavior
|
|
|
|
{
|
|
|
|
return NSWindowCollectionBehaviorFullScreenPrimary;
|
|
|
|
}
|
2015-05-04 23:37:28 +00:00
|
|
|
@end
|
|
|
|
|