Move OSXView to its own file

This new OSXView contains some basic mouse event handlers to generate
some she events.
This commit is contained in:
David Capello 2015-10-07 16:10:52 -03:00
parent 0db4c1c664
commit 8edb0c0a67
2 changed files with 144 additions and 12 deletions

143
src/she/osx/view.h Normal file
View File

@ -0,0 +1,143 @@
// SHE library
// Copyright (C) 2015 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
inline gfx::Point get_local_mouse_pos(NSView* view, NSEvent* event)
{
NSPoint point = [view convertPoint:[event locationInWindow]
fromView:nil];
// "she" layer coordinates expect (X,Y) origin at the top-left corner.
return gfx::Point(point.x,
view.bounds.size.height - point.y);
}
inline she::Event::MouseButton get_mouse_buttons(NSEvent* event)
{
switch ([event buttonNumber]) {
case 0: return she::Event::LeftButton; break;
case 1: return she::Event::RightButton; break;
case 2: return she::Event::MiddleButton; break;
// TODO add support for other buttons
}
return she::Event::MouseButton::NoneButton;
}
@interface OSXView : NSView
{
NSTrackingArea* m_trackingArea;
}
- (id)initWithFrame:(NSRect)frameRect;
- (void)dealloc;
- (void)mouseDown:(NSEvent*)event;
- (void)mouseUp:(NSEvent*)event;
- (void)mouseEntered:(NSEvent*)event;
- (void)mouseMoved:(NSEvent*)event;
- (void)mouseExited:(NSEvent*)event;
- (void)mouseDragged:(NSEvent*)event;
- (void)setFrameSize:(NSSize)newSize;
- (void)createMouseTrackingArea;
- (void)destroyMouseTrackingArea;
@end
@implementation OSXView
- (id)initWithFrame:(NSRect)frameRect
{
self = [super initWithFrame:frameRect];
if (self != nil) {
[self createMouseTrackingArea];
}
return self;
}
- (void)dealloc
{
[self destroyMouseTrackingArea];
[super dealloc];
}
- (void)mouseDown:(NSEvent*)event
{
she::Event ev;
ev.setType(she::Event::MouseDown);
ev.setPosition(get_local_mouse_pos(self, event));
ev.setButton(she::Event::LeftButton);
she::queue_event(ev);
}
- (void)mouseUp:(NSEvent*)event
{
she::Event ev;
ev.setType(she::Event::MouseUp);
ev.setPosition(get_local_mouse_pos(self, event));
ev.setButton(she::Event::LeftButton);
she::queue_event(ev);
}
- (void)mouseEntered:(NSEvent*)event
{
she::Event ev;
ev.setType(she::Event::MouseEnter);
ev.setPosition(get_local_mouse_pos(self, event));
she::queue_event(ev);
}
- (void)mouseMoved:(NSEvent*)event
{
she::Event ev;
ev.setType(she::Event::MouseMove);
ev.setPosition(get_local_mouse_pos(self, event));
she::queue_event(ev);
}
- (void)mouseExited:(NSEvent*)event
{
she::Event ev;
ev.setType(she::Event::MouseLeave);
ev.setPosition(get_local_mouse_pos(self, event));
she::queue_event(ev);
}
- (void)mouseDragged:(NSEvent*)event
{
she::Event ev;
ev.setType(she::Event::MouseMove);
ev.setPosition(get_local_mouse_pos(self, event));
ev.setButton(get_mouse_buttons(event));
she::queue_event(ev);
}
- (void)setFrameSize:(NSSize)newSize
{
[super setFrameSize:newSize];
// Re-create the mouse tracking area
[self destroyMouseTrackingArea];
[self createMouseTrackingArea];
}
- (void)createMouseTrackingArea
{
// Create a tracking area to receive mouseMoved events
m_trackingArea =
[[NSTrackingArea alloc]
initWithRect:self.bounds
options:(NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingActiveAlways |
NSTrackingEnabledDuringMouseDrag)
owner:self
userInfo:nil];
[self addTrackingArea:m_trackingArea];
}
- (void)destroyMouseTrackingArea
{
[self removeTrackingArea:m_trackingArea];
[m_trackingArea release];
m_trackingArea = nil;
}
@end

View File

@ -12,6 +12,7 @@
#include "she/event.h"
#include "she/event_queue.h"
#include "she/osx/view.h"
#include "she/system.h"
@interface OSXWindowDelegate : NSObject
@ -26,10 +27,6 @@
- (void)windowDidMiniaturize:(NSNotification*)notification;
@end
@interface OSXView : NSView
- (void)mouseDragged:(NSEvent*)theEvent;
@end
@implementation OSXWindowDelegate
- (OSXWindowDelegate*)initWithWindow:(OSXWindow*)window
@ -59,14 +56,6 @@
@end
@implementation OSXView
- (void)mouseDragged:(NSEvent*)theEvent
{
}
@end
@implementation OSXWindow
- (OSXWindow*)init