[aseprite] 143/250: Move OSXView to its own file

Tobias Hansen thansen at moszumanska.debian.org
Sun Dec 20 15:27:22 UTC 2015


This is an automated email from the git hooks/post-receive script.

thansen pushed a commit to branch master
in repository aseprite.

commit 8edb0c0a6736c9dd2a25bc73ae58b7fc48a9e708
Author: David Capello <davidcapello at gmail.com>
Date:   Wed Oct 7 16:10:52 2015 -0300

    Move OSXView to its own file
    
    This new OSXView contains some basic mouse event handlers to generate
    some she events.
---
 src/she/osx/view.h    | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/she/osx/window.mm |  13 +----
 2 files changed, 144 insertions(+), 12 deletions(-)

diff --git a/src/she/osx/view.h b/src/she/osx/view.h
new file mode 100644
index 0000000..05d73f6
--- /dev/null
+++ b/src/she/osx/view.h
@@ -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;
+}
+
+ at 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;
+ at end
+
+ at 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;
+}
+
+ at end
diff --git a/src/she/osx/window.mm b/src/she/osx/window.mm
index 15d6969..aa0d949 100644
--- a/src/she/osx/window.mm
+++ b/src/she/osx/window.mm
@@ -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
 
- at interface OSXView : NSView
-- (void)mouseDragged:(NSEvent*)theEvent;
- at end
-
 @implementation OSXWindowDelegate
 
 - (OSXWindowDelegate*)initWithWindow:(OSXWindow*)window
@@ -59,14 +56,6 @@
 
 @end
 
- at implementation OSXView
-
-- (void)mouseDragged:(NSEvent*)theEvent
-{
-}
-
- at end
-
 @implementation OSXWindow
 
 - (OSXWindow*)init

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/aseprite.git



More information about the Pkg-games-commits mailing list