From 8ed6de22a4575c04339212ca6ca8e318552814d7 Mon Sep 17 00:00:00 2001
From: Jonas Kulla <Nyocurio@gmail.com>
Date: Sun, 5 Jan 2014 01:15:33 +0100
Subject: [PATCH] Input: Return const -20 mouse position when outside window

---
 src/eventthread.cpp | 4 +++-
 src/eventthread.h   | 1 +
 src/input.cpp       | 8 ++++++++
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/src/eventthread.cpp b/src/eventthread.cpp
index ecb9d10..f4db40d 100644
--- a/src/eventthread.cpp
+++ b/src/eventthread.cpp
@@ -42,7 +42,7 @@ EventThread::JoyState EventThread::joyState =
 
 EventThread::MouseState EventThread::mouseState =
 {
-	0, 0, { false }
+	0, 0, false, { false }
 };
 
 /* User event codes */
@@ -129,12 +129,14 @@ void EventThread::process(RGSSThreadData &rtData)
 
 			case SDL_WINDOWEVENT_ENTER :
 				cursorInWindow = true;
+				mouseState.inWindow = true;
 				updateCursorState(cursorInWindow && windowFocused);
 
 				break;
 
 			case SDL_WINDOWEVENT_LEAVE :
 				cursorInWindow = false;
+				mouseState.inWindow = false;
 				updateCursorState(cursorInWindow && windowFocused);
 
 				break;
diff --git a/src/eventthread.h b/src/eventthread.h
index 6611983..5ed27c1 100644
--- a/src/eventthread.h
+++ b/src/eventthread.h
@@ -56,6 +56,7 @@ public:
 	struct MouseState
 	{
 		int x, y;
+		bool inWindow;
 		bool buttons[32];
 	};
 
diff --git a/src/input.cpp b/src/input.cpp
index 7dd76d6..4c23659 100644
--- a/src/input.cpp
+++ b/src/input.cpp
@@ -614,12 +614,20 @@ int Input::dir8Value()
 int Input::mouseX()
 {
 	RGSSThreadData &rtData = shState->rtData();
+
+	if (!EventThread::mouseState.inWindow)
+		return -20;
+
 	return (EventThread::mouseState.x - rtData.screenOffset.x) * rtData.sizeResoRatio.x;
 }
 
 int Input::mouseY()
 {
 	RGSSThreadData &rtData = shState->rtData();
+
+	if (!EventThread::mouseState.inWindow)
+		return -20;
+
 	return (EventThread::mouseState.y - rtData.screenOffset.y) * rtData.sizeResoRatio.y;
 }