Преглед изворни кода

Added cursor visibility and mouse capture on MacOSX

Sean Paul Taylor пре 13 година
родитељ
комит
daaa7190a0
4 измењених фајлова са 117 додато и 81 уклоњено
  1. 0 25
      gameplay/src/Game.cpp
  2. 41 47
      gameplay/src/Game.h
  3. 25 0
      gameplay/src/Game.inl
  4. 51 9
      gameplay/src/PlatformMacOSX.mm

+ 0 - 25
gameplay/src/Game.cpp

@@ -62,31 +62,6 @@ void Game::setVsync(bool enable)
     Platform::setVsync(enable);
 }
 
-bool Game::hasMouse()
-{
-    return Platform::hasMouse();
-}
-
-bool Game::isMouseCaptured()
-{
-    return Platform::isMouseCaptured();
-}
-
-void Game::setMouseCaptured(bool captured)
-{
-    Platform::setMouseCaptured(captured);
-}
-
-void Game::setCursorVisible(bool visible)
-{
-    Platform::setCursorVisible(visible);
-}
-
-bool Game::isCursorVisible()
-{
-    return Platform::isCursorVisible();
-}
-
 bool Game::isVsync()
 {
     return Platform::isVsync();

+ 41 - 47
gameplay/src/Game.h

@@ -75,53 +75,6 @@ public:
      */
     static void setVsync(bool enable);
 
-    /** 
-     * Gets whether the current platform supports mouse input.
-     *
-      * @return true if a mouse is supported, false otherwise.
-      */
-    static bool hasMouse();
-
-    /**
-     * Gets whether mouse input is currently captured.
-     *
-     * @see Platform::isMouseCaptured()
-     */
-    static bool isMouseCaptured();
-
-    /**
-     * Enables or disables mouse capture.
-     *
-     * On platforms that support a mouse, when mouse capture is enabled,
-     * the platform cursor will be hidden and the mouse will be warped
-     * to the center of the screen. While mouse capture is enabled,
-     * all mouse move events will then be delivered as deltas instead
-     * of absolute positions.
-     *
-     * @param captured true to enable mouse capture mode, false to disable it.
-     *
-     * @see Platform::setMouseCaptured(bool)
-     */
-    static void setMouseCaptured(bool captured);
-
-    /**
-     * Sets the visibility of the platform cursor.
-     *
-     * @param visible true to show the platform cursor, false to hide it.
-     *
-     * @see Platform::setCursorVisible(bool)
-     */
-    static void setCursorVisible(bool visible);
-
-    /**
-     * Determines whether the platform cursor is currently visible.
-     *
-     * @return true if the platform cursor is visible, false otherwise.
-     *
-     * @see Platform::isCursorVisible()
-     */
-    static bool isCursorVisible();
-
     /**
      * Gets the total absolute running time (in milliseconds) since Game::run().
      * 
@@ -321,6 +274,47 @@ public:
      * @see Mouse::MouseEvent
      */
     virtual bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
+    
+    /** 
+     * Gets whether the current platform supports mouse input.
+     *
+     * @return true if a mouse is supported, false otherwise.
+     */
+    inline bool hasMouse();
+    
+    /**
+     * Gets whether mouse input is currently captured.
+     *
+     * @return is the mouse captured.
+     */
+    inline bool isMouseCaptured();
+    
+    /**
+     * Enables or disables mouse capture.
+     *
+     * On platforms that support a mouse, when mouse capture is enabled,
+     * the platform cursor will be hidden and the mouse will be warped
+     * to the center of the screen. While mouse capture is enabled,
+     * all mouse move events will then be delivered as deltas instead
+     * of absolute positions.
+     *
+     * @param captured true to enable mouse capture mode, false to disable it.
+     */
+    inline void setMouseCaptured(bool captured);
+    
+    /**
+     * Sets the visibility of the platform cursor.
+     *
+     * @param visible true to show the platform cursor, false to hide it.
+     */
+    inline void setCursorVisible(bool visible);
+    
+    /**
+     * Determines whether the platform cursor is currently visible.
+     *
+     * @return true if the platform cursor is visible, false otherwise.
+     */
+    inline bool isCursorVisible();
 
     /**
      * Gamepad callback on gamepad events.

+ 25 - 0
gameplay/src/Game.inl

@@ -57,6 +57,31 @@ void Game::renderOnce(T* instance, void (T::*method)(void*), void* cookie)
     Platform::swapBuffers();
 }
 
+inline bool Game::hasMouse()
+{
+    return Platform::hasMouse();
+}
+
+inline bool Game::isMouseCaptured()
+{
+    return Platform::isMouseCaptured();
+}
+
+inline void Game::setMouseCaptured(bool captured)
+{
+    Platform::setMouseCaptured(captured);
+}
+
+inline void Game::setCursorVisible(bool visible)
+{
+    Platform::setCursorVisible(visible);
+}
+
+inline bool Game::isCursorVisible()
+{
+    return Platform::isCursorVisible();
+}
+
 inline void Game::setMultiTouch(bool enabled)
 {
     Platform::setMultiTouch(enabled);

+ 51 - 9
gameplay/src/PlatformMacOSX.mm

@@ -36,7 +36,9 @@ static bool __shiftDown = false;
 static char* __title = NULL;
 static bool __fullscreen = false;
 static void* __attachToWindow = NULL;
-
+static bool __mouseCaptured = false;
+static CGPoint __mouseCapturePoint;
+static bool __cursorVisible = true;
 
 double getMachTimeInMilliseconds()
 {
@@ -234,10 +236,24 @@ static CVReturn MyDisplayLinkCallback(CVDisplayLinkRef displayLink, const CVTime
     [self mouse: Mouse::MOUSE_RELEASE_LEFT_BUTTON orTouchEvent: Touch::TOUCH_RELEASE x: point.x y: __height - point.y s: 0];
 }
 
-- (void)mouseMoved:(NSEvent *) event 
+- (void)mouseMoved:(NSEvent*) event 
 {
     NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
-    gameplay::Platform::mouseEventInternal(Mouse::MOUSE_MOVE, point.x, __height - point.y, 0);
+    
+
+    if (__mouseCaptured)
+    {
+        point.x = [event deltaX];
+        point.y = [event deltaY];
+
+        NSWindow* window = __view.window;
+        NSRect rect = window.frame;
+        CGPoint centerPoint;
+        centerPoint.x = rect.origin.x + (rect.size.width / 2);
+        centerPoint.y = rect.origin.y + (rect.size.height / 2);
+        CGDisplayMoveCursorToPoint(CGDisplayPrimaryDisplay(NULL), centerPoint);
+    }
+    gameplay::Platform::mouseEventInternal(Mouse::MOUSE_MOVE, point.x, point.y, 0);
 }
 
 - (void) mouseDragged: (NSEvent*) event
@@ -765,24 +781,50 @@ bool Platform::hasMouse()
 
 void Platform::setMouseCaptured(bool captured)
 {
-    // TODO: not implemented
+    if (captured != __mouseCaptured)
+    {
+        if (captured)
+        {
+            [NSCursor hide];
+        }
+        else
+        {   
+            [NSCursor unhide];
+        }
+        NSWindow* window = __view.window;
+        NSRect rect = window.frame;
+        CGPoint centerPoint;
+        centerPoint.x = rect.origin.x + (rect.size.width / 2);
+        centerPoint.y = rect.origin.y + (rect.size.height / 2);
+        CGDisplayMoveCursorToPoint(CGDisplayPrimaryDisplay(NULL), centerPoint);
+        __mouseCaptured = captured;
+    }
 }
 
 bool Platform::isMouseCaptured()
 {
-    // TODO: not implemented
-    return false;
+    return __mouseCaptured;
 }
 
 void Platform::setCursorVisible(bool visible)
 {
-    // TODO: not implemented
+    if (visible != __cursorVisible)
+    {
+        if (visible)
+        {
+             [NSCursor unhide];
+        }
+        else 
+        {
+             [NSCursor hide];
+        }
+        __cursorVisible = visible;
+    }
 }
 
 bool Platform::isCursorVisible()
 {
-    // TODO: not implemented
-    return true;
+    return __cursorVisible;
 }
 
 void Platform::swapBuffers()