boo/lib/mac/WindowCocoa.mm

1209 lines
34 KiB
Plaintext
Raw Normal View History

2015-11-01 00:06:56 +00:00
#include "boo/graphicsdev/GL.hpp"
#include "boo/graphicsdev/glew.h"
#include "boo/graphicsdev/Metal.hpp"
2015-11-09 02:24:45 +00:00
#include "CocoaCommon.hpp"
#import <AppKit/AppKit.h>
2015-11-01 00:06:56 +00:00
#import <CoreVideo/CVDisplayLink.h>
2015-09-02 19:09:13 +00:00
#include "boo/IApplication.hpp"
#include "boo/IWindow.hpp"
#include "boo/IGraphicsContext.hpp"
2015-11-01 00:06:56 +00:00
#include <LogVisor/LogVisor.hpp>
2015-09-02 19:09:13 +00:00
namespace boo {class WindowCocoa;}
@interface WindowCocoaInternal : NSWindow
{
2015-09-02 19:09:13 +00:00
boo::WindowCocoa* booWindow;
}
2015-09-02 19:09:13 +00:00
- (id)initWithBooWindow:(boo::WindowCocoa*)bw title:(const std::string&)title;
2015-05-06 00:50:57 +00:00
- (void)setFrameDefault;
- (NSRect)genFrameDefault;
@end
2015-09-02 19:09:13 +00:00
/* AppKit applies OpenGL much differently than other platforms
* the NSOpenGLView class composes together all necessary
* OGL context members and provides the necessary event hooks
* for KB/Mouse/Touch events
*/
static const NSOpenGLPixelFormatAttribute PF_RGBA8_ATTRS[] =
{
NSOpenGLPFAAccelerated,
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
};
2015-09-02 19:09:13 +00:00
static const NSOpenGLPixelFormatAttribute PF_RGBA8_Z24_ATTRS[] =
{
NSOpenGLPFAAccelerated,
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAColorSize, 24,
NSOpenGLPFAAlphaSize, 8,
NSOpenGLPFADepthSize, 24,
};
static const NSOpenGLPixelFormatAttribute PF_RGBAF32_ATTRS[] =
{
NSOpenGLPFAAccelerated,
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAColorFloat,
NSOpenGLPFAColorSize, 96,
NSOpenGLPFAAlphaSize, 32,
};
static const NSOpenGLPixelFormatAttribute PF_RGBAF32_Z24_ATTRS[] =
{
NSOpenGLPFAAccelerated,
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
NSOpenGLPFADoubleBuffer,
NSOpenGLPFAColorFloat,
NSOpenGLPFAColorSize, 96,
NSOpenGLPFAAlphaSize, 32,
NSOpenGLPFADepthSize, 24,
};
static const NSOpenGLPixelFormatAttribute* PF_TABLE[] =
{
NULL,
PF_RGBA8_ATTRS,
PF_RGBA8_Z24_ATTRS,
PF_RGBAF32_ATTRS,
PF_RGBAF32_Z24_ATTRS
};
namespace boo
{
class GraphicsContextCocoa : public IGraphicsContext
{
protected:
EGraphicsAPI m_api;
EPixelFormat m_pf;
IWindow* m_parentWindow;
CVDisplayLinkRef m_dispLink = nullptr;
GraphicsContextCocoa(EGraphicsAPI api, EPixelFormat pf, IWindow* parentWindow)
: m_api(api), m_pf(pf), m_parentWindow(parentWindow) {}
std::mutex m_dlmt;
std::condition_variable m_dlcv;
2015-11-16 22:03:46 +00:00
static CVReturn DLCallback(CVDisplayLinkRef displayLink,
const CVTimeStamp * inNow,
const CVTimeStamp * inOutputTime,
CVOptionFlags flagsIn,
2015-11-16 22:03:46 +00:00
CVOptionFlags * flagsOut,
GraphicsContextCocoa* ctx)
{
ctx->m_dlcv.notify_one();
return kCVReturnSuccess;
}
~GraphicsContextCocoa()
{
if (m_dispLink)
{
CVDisplayLinkStop(m_dispLink);
CVDisplayLinkRelease(m_dispLink);
}
}
public:
IWindowCallback* m_callback = nullptr;
void waitForRetrace()
{
std::unique_lock<std::mutex> lk(m_dlmt);
m_dlcv.wait(lk);
}
};
class GraphicsContextCocoaGL;
class GraphicsContextCocoaMetal;
}
@interface BooCocoaResponder : NSResponder
2015-09-02 19:09:13 +00:00
{
@public
2015-09-02 19:09:13 +00:00
NSUInteger lastModifiers;
boo::GraphicsContextCocoa* booContext;
NSView* parentView;
2015-09-02 19:09:13 +00:00
}
- (id)initWithBooContext:(boo::GraphicsContextCocoa*)bctx View:(NSView*)view;
@end
@interface GraphicsContextCocoaGLInternal : NSOpenGLView
{
BooCocoaResponder* resp;
}
- (id)initWithBooContext:(boo::GraphicsContextCocoaGL*)bctx;
@end
@interface GraphicsContextCocoaMetalInternal : NSView
{
BooCocoaResponder* resp;
2015-11-09 02:24:45 +00:00
boo::MetalContext* m_ctx;
}
- (id)initWithBooContext:(boo::GraphicsContextCocoaMetal*)bctx;
2015-09-02 19:09:13 +00:00
@end
namespace boo
{
2015-11-01 00:06:56 +00:00
static LogVisor::LogModule Log("boo::WindowCocoa");
IGraphicsCommandQueue* _NewGLCommandQueue(IGraphicsContext* parent);
2015-11-09 02:24:45 +00:00
IGraphicsCommandQueue* _NewMetalCommandQueue(MetalContext* ctx, IWindow* parentWindow,
IGraphicsContext* parent);
2015-11-01 00:06:56 +00:00
void _CocoaUpdateLastGLCtx(NSOpenGLContext* lastGLCtx);
class GraphicsContextCocoaGL : public GraphicsContextCocoa
2015-09-02 19:09:13 +00:00
{
GraphicsContextCocoaGLInternal* m_nsContext = nullptr;
2015-11-01 00:06:56 +00:00
IGraphicsCommandQueue* m_commandQueue = nullptr;
IGraphicsDataFactory* m_dataFactory = nullptr;
2015-11-17 20:33:12 +00:00
NSOpenGLContext* m_mainCtx = nullptr;
2015-11-01 00:06:56 +00:00
NSOpenGLContext* m_loadCtx = nullptr;
2015-09-02 19:09:13 +00:00
public:
2015-11-01 00:06:56 +00:00
NSOpenGLContext* m_lastCtx = nullptr;
2015-09-02 19:09:13 +00:00
GraphicsContextCocoaGL(EGraphicsAPI api, IWindow* parentWindow, NSOpenGLContext* lastGLCtx)
: GraphicsContextCocoa(api, PF_RGBA8, parentWindow),
m_lastCtx(lastGLCtx)
{
m_dataFactory = new GLDataFactory(this);
}
2015-09-02 19:09:13 +00:00
~GraphicsContextCocoaGL()
2015-09-02 19:09:13 +00:00
{
2015-11-01 00:06:56 +00:00
delete m_dataFactory;
delete m_commandQueue;
2015-09-02 19:09:13 +00:00
[m_nsContext release];
2015-11-01 00:06:56 +00:00
[m_loadCtx release];
2015-09-02 19:09:13 +00:00
}
void _setCallback(IWindowCallback* cb)
{
m_callback = cb;
}
EGraphicsAPI getAPI() const
{
return m_api;
}
EPixelFormat getPixelFormat() const
{
return m_pf;
}
void setPixelFormat(EPixelFormat pf)
{
if (pf > PF_RGBAF32_Z24)
return;
m_pf = pf;
}
void initializeContext()
{
m_nsContext = [[GraphicsContextCocoaGLInternal alloc] initWithBooContext:this];
2015-11-01 00:06:56 +00:00
if (!m_nsContext)
Log.report(LogVisor::FatalError, "unable to make new NSOpenGLView");
2015-09-02 19:09:13 +00:00
[(NSWindow*)m_parentWindow->getPlatformHandle() setContentView:m_nsContext];
2015-11-01 00:06:56 +00:00
CVDisplayLinkCreateWithActiveCGDisplays(&m_dispLink);
CVDisplayLinkSetOutputCallback(m_dispLink, (CVDisplayLinkOutputCallback)DLCallback, this);
CVDisplayLinkStart(m_dispLink);
m_commandQueue = _NewGLCommandQueue(this);
2015-09-02 19:09:13 +00:00
}
2015-11-01 00:06:56 +00:00
void makeCurrent()
2015-09-02 19:09:13 +00:00
{
2015-11-01 00:06:56 +00:00
[[m_nsContext openGLContext] makeCurrentContext];
2015-09-02 19:09:13 +00:00
}
2015-11-01 00:06:56 +00:00
void postInit()
{
}
IGraphicsCommandQueue* getCommandQueue()
2015-09-02 19:09:13 +00:00
{
2015-11-01 00:06:56 +00:00
return m_commandQueue;
2015-09-02 19:09:13 +00:00
}
2015-11-01 00:06:56 +00:00
IGraphicsDataFactory* getDataFactory()
2015-09-02 19:09:13 +00:00
{
2015-11-01 00:06:56 +00:00
return m_dataFactory;
2015-09-02 19:09:13 +00:00
}
2015-11-17 20:33:12 +00:00
IGraphicsDataFactory* getMainContextDataFactory()
{
if (!m_mainCtx)
{
NSOpenGLPixelFormat* nspf = [[NSOpenGLPixelFormat alloc] initWithAttributes:PF_TABLE[m_pf]];
m_mainCtx = [[NSOpenGLContext alloc] initWithFormat:nspf shareContext:[m_nsContext openGLContext]];
[nspf release];
if (!m_mainCtx)
Log.report(LogVisor::FatalError, "unable to make main NSOpenGLContext");
[m_mainCtx makeCurrentContext];
}
return m_dataFactory;
}
2015-11-01 00:06:56 +00:00
IGraphicsDataFactory* getLoadContextDataFactory()
{
if (!m_loadCtx)
{
NSOpenGLPixelFormat* nspf = [[NSOpenGLPixelFormat alloc] initWithAttributes:PF_TABLE[m_pf]];
m_loadCtx = [[NSOpenGLContext alloc] initWithFormat:nspf shareContext:[m_nsContext openGLContext]];
[nspf release];
if (!m_loadCtx)
Log.report(LogVisor::FatalError, "unable to make load NSOpenGLContext");
[m_loadCtx makeCurrentContext];
}
return m_dataFactory;
2015-11-01 00:06:56 +00:00
}
void present()
2015-09-02 19:09:13 +00:00
{
[[m_nsContext openGLContext] flushBuffer];
}
};
IGraphicsContext* _GraphicsContextCocoaGLNew(IGraphicsContext::EGraphicsAPI api,
IWindow* parentWindow, NSOpenGLContext* lastGLCtx)
2015-09-02 19:09:13 +00:00
{
if (api != IGraphicsContext::API_OPENGL_3_3 && api != IGraphicsContext::API_OPENGL_4_2)
return NULL;
/* Create temporary context to query GL version */
NSOpenGLPixelFormat* nspf = [[NSOpenGLPixelFormat alloc] initWithAttributes:PF_RGBA8_ATTRS];
if (!nspf)
return NULL;
NSOpenGLContext* nsctx = [[NSOpenGLContext alloc] initWithFormat:nspf shareContext:nil];
[nspf release];
if (!nsctx)
return NULL;
[nsctx makeCurrentContext];
const char* glVersion = (char*)glGetString(GL_VERSION);
unsigned major = 0;
unsigned minor = 0;
if (glVersion)
{
major = glVersion[0] - '0';
minor = glVersion[2] - '0';
}
[NSOpenGLContext clearCurrentContext];
[nsctx release];
if (!glVersion)
return NULL;
if (major > 4 || (major == 4 && minor >= 2))
api = IGraphicsContext::API_OPENGL_4_2;
else if (major == 3 && minor >= 3)
if (api == IGraphicsContext::API_OPENGL_4_2)
return NULL;
return new GraphicsContextCocoaGL(api, parentWindow, lastGLCtx);
2015-09-02 19:09:13 +00:00
}
2015-11-16 22:03:46 +00:00
#if BOO_HAS_METAL
class GraphicsContextCocoaMetal : public GraphicsContextCocoa
2015-09-02 19:09:13 +00:00
{
GraphicsContextCocoaMetalInternal* m_nsContext = nullptr;
IGraphicsCommandQueue* m_commandQueue = nullptr;
IGraphicsDataFactory* m_dataFactory = nullptr;
public:
2015-11-09 02:24:45 +00:00
MetalContext* m_metalCtx;
GraphicsContextCocoaMetal(EGraphicsAPI api, IWindow* parentWindow,
2015-11-09 02:24:45 +00:00
MetalContext* metalCtx)
: GraphicsContextCocoa(api, PF_RGBA8, parentWindow),
2015-11-09 02:24:45 +00:00
m_metalCtx(metalCtx)
2015-11-01 00:06:56 +00:00
{
2015-11-09 02:24:45 +00:00
m_dataFactory = new MetalDataFactory(this, metalCtx);
2015-11-01 00:06:56 +00:00
}
~GraphicsContextCocoaMetal()
{
delete m_dataFactory;
delete m_commandQueue;
[m_nsContext release];
2015-11-09 02:24:45 +00:00
m_metalCtx->m_windows.erase(m_parentWindow);
}
void _setCallback(IWindowCallback* cb)
{
m_callback = cb;
}
EGraphicsAPI getAPI() const
{
return m_api;
}
EPixelFormat getPixelFormat() const
{
return m_pf;
}
void setPixelFormat(EPixelFormat pf)
{
if (pf > PF_RGBAF32_Z24)
return;
m_pf = pf;
}
void initializeContext()
{
2015-11-09 02:24:45 +00:00
MetalContext::Window& w = m_metalCtx->m_windows[m_parentWindow];
m_nsContext = [[GraphicsContextCocoaMetalInternal alloc] initWithBooContext:this];
if (!m_nsContext)
Log.report(LogVisor::FatalError, "unable to make new NSView for Metal");
2015-11-09 02:24:45 +00:00
w.m_metalLayer = (CAMetalLayer*)m_nsContext.layer;
[(NSWindow*)m_parentWindow->getPlatformHandle() setContentView:m_nsContext];
CVDisplayLinkCreateWithActiveCGDisplays(&m_dispLink);
CVDisplayLinkSetOutputCallback(m_dispLink, (CVDisplayLinkOutputCallback)DLCallback, this);
CVDisplayLinkStart(m_dispLink);
2015-11-09 02:24:45 +00:00
m_commandQueue = _NewMetalCommandQueue(m_metalCtx, m_parentWindow, this);
}
void makeCurrent()
{
}
void postInit()
{
}
IGraphicsCommandQueue* getCommandQueue()
{
return m_commandQueue;
}
IGraphicsDataFactory* getDataFactory()
{
return m_dataFactory;
}
IGraphicsDataFactory* getLoadContextDataFactory()
{
return m_dataFactory;
}
void present()
{
}
};
2015-09-02 19:09:13 +00:00
IGraphicsContext* _GraphicsContextCocoaMetalNew(IGraphicsContext::EGraphicsAPI api,
IWindow* parentWindow,
2015-11-09 02:24:45 +00:00
MetalContext* metalCtx)
{
if (api != IGraphicsContext::API_METAL)
return nullptr;
2015-11-09 02:24:45 +00:00
return new GraphicsContextCocoaMetal(api, parentWindow, metalCtx);
}
2015-11-16 22:03:46 +00:00
#endif
}
@implementation BooCocoaResponder
- (id)initWithBooContext:(boo::GraphicsContextCocoa*)bctx View:(NSView*)view
2015-09-02 19:09:13 +00:00
{
lastModifiers = 0;
booContext = bctx;
parentView = view;
return self;
2015-09-02 19:09:13 +00:00
}
static inline boo::EModifierKey getMod(NSUInteger flags)
2015-09-02 19:09:13 +00:00
{
int ret = boo::MKEY_NONE;
2015-09-02 19:09:13 +00:00
if (flags & NSControlKeyMask)
ret |= boo::MKEY_CTRL;
2015-09-02 19:09:13 +00:00
if (flags & NSAlternateKeyMask)
ret |= boo::MKEY_ALT;
2015-09-02 19:09:13 +00:00
if (flags & NSShiftKeyMask)
ret |= boo::MKEY_SHIFT;
2015-09-02 19:09:13 +00:00
if (flags & NSCommandKeyMask)
ret |= boo::MKEY_COMMAND;
return static_cast<boo::EModifierKey>(ret);
2015-09-02 19:09:13 +00:00
}
static inline boo::EMouseButton getButton(NSEvent* event)
2015-09-02 19:09:13 +00:00
{
NSInteger buttonNumber = event.buttonNumber;
if (buttonNumber == 3)
return boo::BUTTON_MIDDLE;
2015-09-02 19:09:13 +00:00
else if (buttonNumber == 4)
return boo::BUTTON_AUX1;
2015-09-02 19:09:13 +00:00
else if (buttonNumber == 5)
return boo::BUTTON_AUX2;
return boo::BUTTON_NONE;
2015-09-02 19:09:13 +00:00
}
2015-09-02 19:09:13 +00:00
- (void)mouseDown:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil];
float pixelFactor = [[parentView window] backingScaleFactor];
NSRect frame = [parentView frame];
boo::SWindowCoord coord =
2015-09-02 19:09:13 +00:00
{
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
{(unsigned)liw.x, (unsigned)liw.y},
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
};
booContext->m_callback->mouseDown(coord, boo::BUTTON_PRIMARY,
2015-09-02 19:09:13 +00:00
getMod([theEvent modifierFlags]));
}
- (void)mouseUp:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil];
float pixelFactor = [[parentView window] backingScaleFactor];
NSRect frame = [parentView frame];
boo::SWindowCoord coord =
2015-09-02 19:09:13 +00:00
{
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
{(unsigned)liw.x, (unsigned)liw.y},
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
};
booContext->m_callback->mouseUp(coord, boo::BUTTON_PRIMARY,
2015-09-02 19:09:13 +00:00
getMod([theEvent modifierFlags]));
}
- (void)rightMouseDown:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil];
float pixelFactor = [[parentView window] backingScaleFactor];
NSRect frame = [parentView frame];
boo::SWindowCoord coord =
2015-09-02 19:09:13 +00:00
{
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
{(unsigned)liw.x, (unsigned)liw.y},
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
};
booContext->m_callback->mouseDown(coord, boo::BUTTON_SECONDARY,
2015-09-02 19:09:13 +00:00
getMod([theEvent modifierFlags]));
}
- (void)rightMouseUp:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil];
float pixelFactor = [[parentView window] backingScaleFactor];
NSRect frame = [parentView frame];
boo::SWindowCoord coord =
2015-09-02 19:09:13 +00:00
{
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
{(unsigned)liw.x, (unsigned)liw.y},
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
};
booContext->m_callback->mouseUp(coord, boo::BUTTON_SECONDARY,
2015-09-02 19:09:13 +00:00
getMod([theEvent modifierFlags]));
}
- (void)otherMouseDown:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
boo::EMouseButton button = getButton(theEvent);
2015-09-02 19:09:13 +00:00
if (!button)
return;
NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil];
float pixelFactor = [[parentView window] backingScaleFactor];
NSRect frame = [parentView frame];
boo::SWindowCoord coord =
2015-09-02 19:09:13 +00:00
{
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
{(unsigned)liw.x, (unsigned)liw.y},
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
};
booContext->m_callback->mouseDown(coord, button, getMod([theEvent modifierFlags]));
}
- (void)otherMouseUp:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
boo::EMouseButton button = getButton(theEvent);
2015-09-02 19:09:13 +00:00
if (!button)
return;
NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil];
float pixelFactor = [[parentView window] backingScaleFactor];
NSRect frame = [parentView frame];
boo::SWindowCoord coord =
2015-09-02 19:09:13 +00:00
{
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
{(unsigned)liw.x, (unsigned)liw.y},
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
};
booContext->m_callback->mouseUp(coord, button, getMod([theEvent modifierFlags]));
}
- (void)mouseMoved:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil];
if (theEvent.window == [parentView window] && NSPointInRect(liw, parentView.frame))
2015-09-02 19:09:13 +00:00
{
float pixelFactor = [[parentView window] backingScaleFactor];
NSRect frame = [parentView frame];
boo::SWindowCoord coord =
{
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
{(unsigned)liw.x, (unsigned)liw.y},
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
};
booContext->m_callback->mouseMove(coord);
}
2015-09-02 19:09:13 +00:00
}
2015-09-02 19:09:13 +00:00
- (void)mouseDragged:(NSEvent*)theEvent
{
[self mouseMoved:theEvent];
}
2015-09-02 19:09:13 +00:00
- (void)rightMouseDragged:(NSEvent*)theEvent
{
[self mouseMoved:theEvent];
}
2015-09-02 19:09:13 +00:00
- (void)otherMouseDragged:(NSEvent*)theEvent
{
[self mouseMoved:theEvent];
}
- (void)scrollWheel:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
NSPoint liw = [parentView convertPoint:[theEvent locationInWindow] fromView:nil];
float pixelFactor = [[parentView window] backingScaleFactor];
NSRect frame = [parentView frame];
boo::SWindowCoord coord =
2015-09-02 19:09:13 +00:00
{
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
{(unsigned)liw.x, (unsigned)liw.y},
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
};
boo::SScrollDelta scroll =
2015-09-02 19:09:13 +00:00
{
{(float)[theEvent scrollingDeltaX], (float)[theEvent scrollingDeltaY]},
(bool)[theEvent hasPreciseScrollingDeltas]
};
booContext->m_callback->scroll(coord, scroll);
}
- (void)touchesBeganWithEvent:(NSEvent*)event
{
if (!booContext->m_callback)
return;
for (NSTouch* touch in [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil])
{
NSPoint pos = touch.normalizedPosition;
boo::STouchCoord coord =
2015-09-02 19:09:13 +00:00
{
{(float)pos.x, (float)pos.y}
};
booContext->m_callback->touchDown(coord, (uintptr_t)touch.identity);
}
}
- (void)touchesEndedWithEvent:(NSEvent*)event
{
if (!booContext->m_callback)
return;
for (NSTouch* touch in [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil])
{
NSPoint pos = touch.normalizedPosition;
boo::STouchCoord coord =
2015-09-02 19:09:13 +00:00
{
{(float)pos.x, (float)pos.y}
};
booContext->m_callback->touchUp(coord, (uintptr_t)touch.identity);
}
}
- (void)touchesMovedWithEvent:(NSEvent*)event
{
if (!booContext->m_callback)
return;
for (NSTouch* touch in [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil])
{
NSPoint pos = touch.normalizedPosition;
boo::STouchCoord coord =
2015-09-02 19:09:13 +00:00
{
{(float)pos.x, (float)pos.y}
};
booContext->m_callback->touchMove(coord, (uintptr_t)touch.identity);
}
}
- (void)touchesCancelledWithEvent:(NSEvent*)event
{
if (!booContext->m_callback)
return;
for (NSTouch* touch in [event touchesMatchingPhase:NSTouchPhaseCancelled inView:nil])
{
NSPoint pos = touch.normalizedPosition;
boo::STouchCoord coord =
2015-09-02 19:09:13 +00:00
{
{(float)pos.x, (float)pos.y}
};
booContext->m_callback->touchUp(coord, (uintptr_t)touch.identity);
}
}
/* keycodes for keys that are independent of keyboard layout*/
enum
{
kVK_Return = 0x24,
kVK_Tab = 0x30,
kVK_Space = 0x31,
kVK_Delete = 0x33,
kVK_Escape = 0x35,
kVK_Command = 0x37,
kVK_Shift = 0x38,
kVK_CapsLock = 0x39,
kVK_Option = 0x3A,
kVK_Control = 0x3B,
kVK_RightShift = 0x3C,
kVK_RightOption = 0x3D,
kVK_RightControl = 0x3E,
kVK_Function = 0x3F,
kVK_F17 = 0x40,
kVK_VolumeUp = 0x48,
kVK_VolumeDown = 0x49,
kVK_Mute = 0x4A,
kVK_F18 = 0x4F,
kVK_F19 = 0x50,
kVK_F20 = 0x5A,
kVK_F5 = 0x60,
kVK_F6 = 0x61,
kVK_F7 = 0x62,
kVK_F3 = 0x63,
kVK_F8 = 0x64,
kVK_F9 = 0x65,
kVK_F11 = 0x67,
kVK_F13 = 0x69,
kVK_F16 = 0x6A,
kVK_F14 = 0x6B,
kVK_F10 = 0x6D,
kVK_F12 = 0x6F,
kVK_F15 = 0x71,
kVK_Help = 0x72,
kVK_Home = 0x73,
kVK_PageUp = 0x74,
kVK_ForwardDelete = 0x75,
kVK_F4 = 0x76,
kVK_End = 0x77,
kVK_F2 = 0x78,
kVK_PageDown = 0x79,
kVK_F1 = 0x7A,
kVK_LeftArrow = 0x7B,
kVK_RightArrow = 0x7C,
kVK_DownArrow = 0x7D,
kVK_UpArrow = 0x7E
};
static boo::ESpecialKey translateKeycode(short code)
2015-09-02 19:09:13 +00:00
{
switch (code) {
case kVK_F1:
return boo::KEY_F1;
2015-09-02 19:09:13 +00:00
case kVK_F2:
return boo::KEY_F2;
2015-09-02 19:09:13 +00:00
case kVK_F3:
return boo::KEY_F3;
2015-09-02 19:09:13 +00:00
case kVK_F4:
return boo::KEY_F4;
2015-09-02 19:09:13 +00:00
case kVK_F5:
return boo::KEY_F5;
2015-09-02 19:09:13 +00:00
case kVK_F6:
return boo::KEY_F6;
2015-09-02 19:09:13 +00:00
case kVK_F7:
return boo::KEY_F7;
2015-09-02 19:09:13 +00:00
case kVK_F8:
return boo::KEY_F8;
2015-09-02 19:09:13 +00:00
case kVK_F9:
return boo::KEY_F9;
2015-09-02 19:09:13 +00:00
case kVK_F10:
return boo::KEY_F10;
2015-09-02 19:09:13 +00:00
case kVK_F11:
return boo::KEY_F11;
2015-09-02 19:09:13 +00:00
case kVK_F12:
return boo::KEY_F12;
2015-09-02 19:09:13 +00:00
case kVK_Escape:
return boo::KEY_ESC;
2015-09-02 19:09:13 +00:00
case kVK_Return:
return boo::KEY_ENTER;
2015-09-02 19:09:13 +00:00
case kVK_Delete:
return boo::KEY_BACKSPACE;
2015-09-02 19:09:13 +00:00
case kVK_ForwardDelete:
return boo::KEY_DELETE;
2015-09-02 19:09:13 +00:00
case kVK_Home:
return boo::KEY_HOME;
2015-09-02 19:09:13 +00:00
case kVK_End:
return boo::KEY_END;
2015-09-02 19:09:13 +00:00
case kVK_PageUp:
return boo::KEY_PGUP;
2015-09-02 19:09:13 +00:00
case kVK_PageDown:
return boo::KEY_PGDOWN;
2015-09-02 19:09:13 +00:00
case kVK_LeftArrow:
return boo::KEY_LEFT;
2015-09-02 19:09:13 +00:00
case kVK_RightArrow:
return boo::KEY_RIGHT;
2015-09-02 19:09:13 +00:00
case kVK_UpArrow:
return boo::KEY_UP;
2015-09-02 19:09:13 +00:00
case kVK_DownArrow:
return boo::KEY_DOWN;
2015-09-02 19:09:13 +00:00
default:
return boo::KEY_NONE;
2015-09-02 19:09:13 +00:00
}
}
- (void)keyDown:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
NSString* chars = theEvent.characters;
if ([chars length] == 0 ||
[chars characterAtIndex:0] == '\n' ||
[chars characterAtIndex:0] == '\r')
2015-09-02 19:09:13 +00:00
booContext->m_callback->specialKeyDown(translateKeycode(theEvent.keyCode),
getMod(theEvent.modifierFlags),
theEvent.isARepeat);
else
booContext->m_callback->charKeyDown([chars characterAtIndex:0],
getMod(theEvent.modifierFlags),
theEvent.isARepeat);
}
- (void)keyUp:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
NSString* chars = theEvent.characters;
if ([chars length] == 0)
booContext->m_callback->specialKeyUp(translateKeycode(theEvent.keyCode),
getMod(theEvent.modifierFlags));
else
booContext->m_callback->charKeyUp([chars characterAtIndex:0],
getMod(theEvent.modifierFlags));
}
- (void)flagsChanged:(NSEvent*)theEvent
{
if (!booContext->m_callback)
return;
NSUInteger modFlags = theEvent.modifierFlags;
if (modFlags != lastModifiers)
{
NSUInteger changedFlags = modFlags ^ lastModifiers;
NSUInteger downFlags = changedFlags & modFlags;
if (downFlags & NSControlKeyMask)
booContext->m_callback->modKeyDown(boo::MKEY_CTRL, false);
2015-09-02 19:09:13 +00:00
if (downFlags & NSAlternateKeyMask)
booContext->m_callback->modKeyDown(boo::MKEY_ALT, false);
2015-09-02 19:09:13 +00:00
if (downFlags & NSShiftKeyMask)
booContext->m_callback->modKeyDown(boo::MKEY_SHIFT, false);
2015-09-02 19:09:13 +00:00
if (downFlags & NSCommandKeyMask)
booContext->m_callback->modKeyDown(boo::MKEY_COMMAND, false);
2015-09-02 19:09:13 +00:00
NSUInteger upFlags = changedFlags & ~modFlags;
if (upFlags & NSControlKeyMask)
booContext->m_callback->modKeyUp(boo::MKEY_CTRL);
2015-09-02 19:09:13 +00:00
if (upFlags & NSAlternateKeyMask)
booContext->m_callback->modKeyUp(boo::MKEY_ALT);
2015-09-02 19:09:13 +00:00
if (upFlags & NSShiftKeyMask)
booContext->m_callback->modKeyUp(boo::MKEY_SHIFT);
2015-09-02 19:09:13 +00:00
if (upFlags & NSCommandKeyMask)
booContext->m_callback->modKeyUp(boo::MKEY_COMMAND);
2015-09-02 19:09:13 +00:00
lastModifiers = modFlags;
}
}
- (BOOL)acceptsTouchEvents
{
return YES;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
@end
@implementation GraphicsContextCocoaGLInternal
- (id)initWithBooContext:(boo::GraphicsContextCocoaGL*)bctx
{
resp = [[BooCocoaResponder alloc] initWithBooContext:bctx View:self];
boo::IGraphicsContext::EPixelFormat pf = bctx->getPixelFormat();
NSOpenGLPixelFormat* nspf = [[NSOpenGLPixelFormat alloc] initWithAttributes:PF_TABLE[pf]];
self = [self initWithFrame:NSMakeRect(0, 0, 100, 100) pixelFormat:nspf];
if (bctx->m_lastCtx)
{
NSOpenGLContext* sharedCtx = [[NSOpenGLContext alloc] initWithFormat:nspf shareContext:bctx->m_lastCtx];
[self setOpenGLContext:sharedCtx];
[sharedCtx setView:self];
}
[nspf release];
return self;
}
- (void)dealloc
{
[resp release];
[super dealloc];
}
- (void)reshape
{
boo::SWindowRect rect = {{int(self.frame.origin.x), int(self.frame.origin.y)},
{int(self.frame.size.width), int(self.frame.size.height)}};
resp->booContext->m_callback->resized(rect);
[super reshape];
}
- (BOOL)acceptsTouchEvents
{
return YES;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (NSResponder*)nextResponder
{
return resp;
}
@end
2015-11-16 22:03:46 +00:00
#if BOO_HAS_METAL
@implementation GraphicsContextCocoaMetalInternal
- (id)initWithBooContext:(boo::GraphicsContextCocoaMetal*)bctx
{
2015-11-09 02:24:45 +00:00
m_ctx = bctx->m_metalCtx;
2015-11-09 06:45:14 +00:00
self = [self initWithFrame:NSMakeRect(0, 0, 100, 100)];
[self setWantsLayer:YES];
resp = [[BooCocoaResponder alloc] initWithBooContext:bctx View:self];
return self;
}
- (void)dealloc
{
[resp release];
[super dealloc];
}
2015-11-09 02:24:45 +00:00
- (CALayer*)makeBackingLayer
{
CAMetalLayer* layer = [CAMetalLayer new];
layer.device = m_ctx->m_dev.get();
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
layer.framebufferOnly = NO;
return layer;
}
- (BOOL)acceptsTouchEvents
{
return YES;
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (NSResponder*)nextResponder
{
return resp;
}
2015-09-02 19:09:13 +00:00
@end
2015-11-16 22:03:46 +00:00
#endif
2015-09-02 19:09:13 +00:00
namespace boo
{
class WindowCocoa : public IWindow
{
2015-09-02 19:09:13 +00:00
WindowCocoaInternal* m_nsWindow;
IGraphicsContext* m_gfxCtx;
public:
WindowCocoa(const std::string& title, NSOpenGLContext* lastGLCtx, MetalContext* metalCtx)
{
2015-11-01 00:06:56 +00:00
dispatch_sync(dispatch_get_main_queue(),
^{
m_nsWindow = [[WindowCocoaInternal alloc] initWithBooWindow:this title:title];
2015-11-16 22:03:46 +00:00
#if BOO_HAS_METAL
2015-11-09 02:24:45 +00:00
if (metalCtx->m_dev)
m_gfxCtx = _GraphicsContextCocoaMetalNew(IGraphicsContext::API_METAL, this, metalCtx);
else
2015-11-16 22:03:46 +00:00
#endif
2015-11-09 02:24:45 +00:00
m_gfxCtx = _GraphicsContextCocoaGLNew(IGraphicsContext::API_OPENGL_3_3, this, lastGLCtx);
2015-11-01 00:06:56 +00:00
m_gfxCtx->initializeContext();
});
}
2015-05-06 00:50:57 +00:00
void _clearWindow()
{
2015-11-01 00:06:56 +00:00
m_nsWindow = nullptr;
2015-05-06 00:50:57 +00:00
}
2015-09-02 19:09:13 +00:00
~WindowCocoa()
{
[m_nsWindow orderOut:nil];
delete m_gfxCtx;
2015-11-01 00:06:56 +00:00
[m_nsWindow release];
2015-09-02 19:09:13 +00:00
APP->_deletedWindow(this);
}
void setCallback(IWindowCallback* cb)
{
m_gfxCtx->_setCallback(cb);
}
void showWindow()
{
2015-11-01 00:06:56 +00:00
dispatch_sync(dispatch_get_main_queue(),
^{
[m_nsWindow makeKeyAndOrderFront:nil];
});
}
void hideWindow()
{
2015-11-01 00:06:56 +00:00
dispatch_sync(dispatch_get_main_queue(),
^{
[m_nsWindow orderOut:nil];
});
}
std::string getTitle()
{
return [[m_nsWindow title] UTF8String];
}
void setTitle(const std::string& title)
{
2015-11-01 00:06:56 +00:00
dispatch_sync(dispatch_get_main_queue(),
^{
[m_nsWindow setTitle:[[NSString stringWithUTF8String:title.c_str()] autorelease]];
});
}
void setWindowFrameDefault()
{
2015-11-01 00:06:56 +00:00
dispatch_sync(dispatch_get_main_queue(),
^{
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 getWindowFrame(int& xOut, int& yOut, int& wOut, int& 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)
{
2015-11-01 00:06:56 +00:00
dispatch_sync(dispatch_get_main_queue(),
^{
NSRect wFrame = NSMakeRect(x, y, w, h);
[m_nsWindow setFrame:wFrame display:NO];
});
}
void setWindowFrame(int x, int y, int w, int h)
{
dispatch_sync(dispatch_get_main_queue(),
^{
NSRect wFrame = NSMakeRect(x, y, w, h);
[m_nsWindow setFrame:wFrame display:NO];
});
}
float getVirtualPixelFactor() const
{
return [m_nsWindow backingScaleFactor];
}
bool isFullscreen() const
{
return ([m_nsWindow styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask;
}
void setFullscreen(bool fs)
{
if ((fs && !isFullscreen()) || (!fs && isFullscreen()))
2015-11-01 00:06:56 +00:00
dispatch_sync(dispatch_get_main_queue(),
^{
[m_nsWindow toggleFullScreen:nil];
});
}
ETouchType getTouchType() const
{
return TOUCH_TRACKPAD;
}
void setStyle(EWindowStyle style)
{
2015-11-16 22:03:46 +00:00
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
if (style & STYLE_TITLEBAR)
m_nsWindow.titleVisibility = NSWindowTitleVisible;
else
m_nsWindow.titleVisibility = NSWindowTitleHidden;
2015-11-16 22:03:46 +00:00
#endif
if (style & STYLE_CLOSE)
m_nsWindow.styleMask |= NSClosableWindowMask;
else
m_nsWindow.styleMask &= ~NSClosableWindowMask;
if (style & STYLE_RESIZE)
m_nsWindow.styleMask |= NSResizableWindowMask;
else
m_nsWindow.styleMask &= ~NSResizableWindowMask;
}
EWindowStyle getStyle() const
{
int retval = 0;
2015-11-16 22:03:46 +00:00
#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101000
retval |= m_nsWindow.titleVisibility == NSWindowTitleVisible ? STYLE_TITLEBAR : 0;
2015-11-16 22:03:46 +00:00
#else
retval |= STYLE_TITLEBAR;
#endif
retval |= (m_nsWindow.styleMask & NSClosableWindowMask) ? STYLE_CLOSE : 0;
retval |= (m_nsWindow.styleMask & NSResizableWindowMask) ? STYLE_RESIZE: 0;
return EWindowStyle(retval);
}
2015-09-02 19:09:13 +00:00
void waitForRetrace()
{
2015-11-01 00:06:56 +00:00
static_cast<GraphicsContextCocoa*>(m_gfxCtx)->waitForRetrace();
2015-09-02 19:09:13 +00:00
}
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-11-01 00:06:56 +00:00
IGraphicsCommandQueue* getCommandQueue()
{
return m_gfxCtx->getCommandQueue();
}
IGraphicsDataFactory* getDataFactory()
{
return m_gfxCtx->getDataFactory();
}
2015-11-17 20:33:12 +00:00
IGraphicsDataFactory* getMainContextDataFactory()
{
return m_gfxCtx->getMainContextDataFactory();
}
2015-11-01 00:06:56 +00:00
IGraphicsDataFactory* getLoadContextDataFactory()
{
return m_gfxCtx->getLoadContextDataFactory();
}
};
IWindow* _WindowCocoaNew(const SystemString& title, NSOpenGLContext* lastGLCtx, MetalContext* metalCtx)
{
return new WindowCocoa(title, lastGLCtx, metalCtx);
}
}
2015-09-02 19:09:13 +00:00
@implementation WindowCocoaInternal
- (id)initWithBooWindow:(boo::WindowCocoa *)bw title:(const boo::SystemString&)title
{
2015-05-06 00:50:57 +00:00
self = [self initWithContentRect:[self genFrameDefault]
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];
booWindow = bw;
return self;
}
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;
}
@end