Просмотр исходного кода

Implement mouse management in InputManager

Daniele Bartolini 13 лет назад
Родитель
Сommit
aed89fdb95

+ 0 - 2
src/CMakeLists.txt

@@ -157,13 +157,11 @@ set (STREAMS_HEADERS
 )
 
 set (MEM_SRC
-	core/mem/GarbageBin.cpp
 	#core/mem/MallocAllocator.cpp
 )
 
 set (MEM_HEADERS
 	core/mem/Auto.h
-	core/mem/GarbageBin.h
 	core/mem/Shared.h
 	#core/mem/Allocator.h
 	#core/mem/MallocAllocator.h

+ 0 - 1
src/Crown.h

@@ -70,7 +70,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Generic.h"
 
 // Core/Mem
-#include "GarbageBin.h"
 
 // Core/Streams
 #include "Stream.h"

+ 2 - 6
src/Device.cpp

@@ -23,19 +23,15 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "Types.h"
 #include "Config.h"
 #include "Device.h"
 #include "Filesystem.h"
-#include "GarbageBin.h"
 #include "InputManager.h"
 #include "Log.h"
+#include "OS.h"
 #include "Renderer.h"
-#include "GarbageBin.h"
-#include "Config.h"
-#include "Filesystem.h"
 #include "Timer.h"
-#include "OS.h"
+#include "Types.h"
 #include <cstdlib>
 
 namespace crown

+ 0 - 1
src/Device.h

@@ -29,7 +29,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Config.h"
 #include "Timer.h"
 #include "Str.h"
-#include "EventLoop.h"
 
 namespace crown
 {

+ 7 - 9
src/MovableCamera.cpp

@@ -48,7 +48,7 @@ MovableCamera::MovableCamera(const Vec3& position, const Angles& axis,
 	mAngleX = 0.0f;
 	mAngleY = 0.0f;
 
-	//GetInputManager()->GetMouse()->SetCursorRelativeXY(Vec2(0.5f, 0.5f));
+	GetInputManager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
 }
 
 MovableCamera::~MovableCamera()
@@ -63,7 +63,7 @@ float MovableCamera::GetMouseSensibility() const
 void MovableCamera::SetActive(bool active)
 {
 	Camera::SetActive(active);
-	//GetInputManager()->GetMouse()->SetCursorRelativeXY(Vec2(0.5f, 0.5f));
+	GetInputManager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
 }
 
 void MovableCamera::SetMouseSensibility(float sensibility)
@@ -205,11 +205,9 @@ void MovableCamera::StrafeRight()
 
 void MovableCamera::SetViewByMouse()
 {
-	//Mouse* mouse = GetInputManager()->GetMouse();
-
-	static Vec2 lastPos = Vec2::ZERO;//mouse->GetCursorRelativeXY();
-	Vec2 currentPos = Vec2::ZERO;//mouse->GetCursorRelativeXY();
-	//mouse->SetCursorRelativeXY(Vec2(0.5f, 0.5f));
+	static Vec2 lastPos = GetInputManager()->get_cursor_relative_xy();
+	Vec2 currentPos = GetInputManager()->get_cursor_relative_xy();
+	GetInputManager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
 
 	if (lastPos == currentPos)
 	{
@@ -218,8 +216,8 @@ void MovableCamera::SetViewByMouse()
 
 	Vec2 delta = lastPos - currentPos;
 
-	//mouse->SetCursorRelativeXY(Vec2(0.5f, 0.5f));
-	lastPos = Vec2::ZERO;//mouse->GetCursorRelativeXY();
+	GetInputManager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
+	lastPos = GetInputManager()->get_cursor_relative_xy();
 
 	mAngleX += delta.y * mMouseSensibility;
 	mAngleY += delta.x * mMouseSensibility;

+ 2 - 4
src/core/Delegate.h

@@ -25,9 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #pragma once
 
-//#include "GarbageBin.h"
 #include "Weak.h"
-#include "GarbageBin.h"
 
 namespace crown
 {
@@ -35,7 +33,7 @@ namespace crown
 /* Delegate with 0 arguments */
 
 template<typename TResult>
-class IDelegate: public virtual IGarbageable
+class IDelegate
 {
 public:
 	virtual TResult Invoke() = 0;
@@ -44,7 +42,7 @@ public:
 };
 
 template<typename TClass, typename TResult>
-class Delegate: public IDelegate<TResult>, public virtual IGarbageable
+class Delegate : public IDelegate<TResult>
 {
 public:
 	Delegate(TClass* object):

+ 16 - 13
src/input/EventDispatcher.cpp

@@ -24,41 +24,35 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "EventDispatcher.h"
-#include "Exceptions.h"
 
 namespace crown
 {
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::AddMouseListener(MouseListener* listener)
 {
-	if (listener == 0)
-	{
-		throw ArgumentException("EventDispatcher::AddMouseListener: listener == NULL.");
-	}
+	assert(listener != NULL);
 
 	mMouseListenerList.push_back(listener);
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::AddKeyboardListener(KeyboardListener* listener)
 {
-	if (listener == 0)
-	{
-		throw ArgumentException("EventDispatcher::AddKeyboardListener: listener == NULL.");
-	}
+	assert(listener != NULL);
 
 	mKeyboardListenerList.push_back(listener);
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::AddTouchListener(TouchListener* listener)
 {
-	if (listener == 0)
-	{
-		throw ArgumentException("EventDispatcher::AddTouchListener: listener == NULL.");
-	}
+	assert(listener != NULL);
 
 	mTouchListenerList.push_back(listener);
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::ButtonPressed(const MouseEvent& event)
 {
 	for (uint i = 0; i < mMouseListenerList.size(); i++)
@@ -67,6 +61,7 @@ void EventDispatcher::ButtonPressed(const MouseEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::ButtonReleased(const MouseEvent& event)
 {
 	for (uint i = 0; i < mMouseListenerList.size(); i++)
@@ -75,6 +70,7 @@ void EventDispatcher::ButtonReleased(const MouseEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::CursorMoved(const MouseEvent& event)
 {
 	for (uint i = 0; i < mMouseListenerList.size(); i++)
@@ -83,6 +79,7 @@ void EventDispatcher::CursorMoved(const MouseEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::KeyPressed(const KeyboardEvent& event)
 {
 	for (uint i = 0; i < mKeyboardListenerList.size(); i++)
@@ -91,6 +88,7 @@ void EventDispatcher::KeyPressed(const KeyboardEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::KeyReleased(const KeyboardEvent& event)
 {
 	for (uint i = 0; i < mKeyboardListenerList.size(); i++)
@@ -99,6 +97,7 @@ void EventDispatcher::KeyReleased(const KeyboardEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::TextInput(const KeyboardEvent& event)
 {
 	for (uint i = 0; i < mKeyboardListenerList.size(); i++)
@@ -107,6 +106,7 @@ void EventDispatcher::TextInput(const KeyboardEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::TouchDown(const TouchEvent& event)
 {
 	for (uint i = 0; i < mTouchListenerList.size(); i++)
@@ -115,6 +115,7 @@ void EventDispatcher::TouchDown(const TouchEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::TouchUp(const TouchEvent& event)
 {
 	for (uint i = 0; i < mTouchListenerList.size(); i++)
@@ -123,6 +124,7 @@ void EventDispatcher::TouchUp(const TouchEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::TouchMove(const TouchEvent& event)
 {
 	for (uint i = 0; i < mTouchListenerList.size(); i++)
@@ -131,6 +133,7 @@ void EventDispatcher::TouchMove(const TouchEvent& event)
 	}
 }
 
+//-----------------------------------------------------------------------------
 void EventDispatcher::TouchCancel(const TouchEvent& event)
 {
 	for (uint i = 0; i < mTouchListenerList.size(); i++)

+ 71 - 11
src/input/InputManager.cpp

@@ -25,16 +25,15 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "InputManager.h"
 #include "OS.h"
+#include "Log.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-InputManager::InputManager()
+InputManager::InputManager() :
+	m_cursor_visible(true)
 {
-//	mMouse = new Mouse();
-//	mKeyboard = new Keyboard();
-//	mTouch = new Touch();
 }
 
 //-----------------------------------------------------------------------------
@@ -51,15 +50,12 @@ void InputManager::EventLoop()
 	{
 		event = os::pop_event();
 
-		if (event.type == os::OSET_NONE)
-		{
-			return;
-		}
-
-		os::printf("OS Event: %d, %d, %d, %d, %d\n", event.type, event.data_a, event.data_b, event.data_c, event.data_d);
-
 		switch (event.type)
 		{
+			case os::OSET_NONE:
+			{
+				return;
+			}
 			case os::OSET_BUTTON_PRESS:
 			case os::OSET_BUTTON_RELEASE:
 			{
@@ -105,6 +101,70 @@ void InputManager::EventLoop()
 	}
 }
 
+//-----------------------------------------------------------------------------
+bool InputManager::is_cursor_visible() const
+{
+	return m_cursor_visible;
+}
+
+//-----------------------------------------------------------------------------
+void InputManager::set_cursor_visible(bool visible)
+{
+	if (visible)
+	{
+		os::hide_cursor();
+	}
+	else
+	{
+		os::show_cursor();
+	}
+
+	m_cursor_visible = visible;
+}
+
+//-----------------------------------------------------------------------------
+Point2 InputManager::get_cursor_xy() const
+{
+	Point2 xy;
+
+	os::get_cursor_xy(xy.x, xy.y);
+
+	return xy;
+}
+
+//-----------------------------------------------------------------------------
+void InputManager::set_cursor_xy(const Point2& position)
+{
+	os::set_cursor_xy(position.x, position.y);
+}
+
+//-----------------------------------------------------------------------------
+Vec2 InputManager::get_cursor_relative_xy() const
+{
+	uint window_width;
+	uint window_height;
+
+	os::get_render_window_metrics(window_width, window_height);
+
+	Vec2 pos = get_cursor_xy().to_vec2();
+
+	pos.x = pos.x / (float) window_width;
+	pos.y = pos.y / (float) window_height;
+
+	return pos;
+}
+
+//-----------------------------------------------------------------------------
+void InputManager::set_cursor_relative_xy(const Vec2& position)
+{
+	uint window_width;
+	uint window_height;
+
+	os::get_render_window_metrics(window_width, window_height);
+
+	set_cursor_xy(Point2((int)(position.x * (float) window_width), (int)(position.y * (float) window_height)));
+}
+
 //-----------------------------------------------------------------------------
 InputManager inputManager;
 InputManager* GetInputManager()

+ 55 - 52
src/input/InputManager.h

@@ -26,76 +26,31 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "EventDispatcher.h"
+#include "Point2.h"
+#include "Vec2.h"
 
 namespace crown
 {
 
-class Mouse;
-class Keyboard;
-class Touch;
 class MouseListener;
 class KeyboardListener;
 class TouchListener;
 
-
 class InputManager
 {
 
 public:
 
-	/**
-		Constructor.
-	*/
 	InputManager();
-
-	/**
-		Destructor.
-	*/
 	~InputManager();
 
-	/**
-		Initializes the input manager.
-	*/
+	//! Initializes the input manager.
 	void Init();
 
-	/**
-		Returns whether the mouse is available.
-	*/
 	bool IsMouseAvailable() {}
-
-	/**
-		Returns whether the keyboard is available.
-	*/
 	bool IsKeyboardAvailable() {}
-
-	/**
-		Returns whether the touch is available.
-	*/
 	bool IsTouchAvailable() {}
 
-	/**
-		Returns the handle to the mouse input device.
-	*/
-	inline Mouse* GetMouse()
-	{
-		//return mMouse;
-	}
-
-	/**
-		Returns the handle to the keyboard input device.
-	*/
-	inline Keyboard* GetKeyboard()
-	{
-		//return mKeyboard;
-	}
-
-	/**
-		Return the handle to the touch input device.
-	*/
-	inline Touch* GetTouch()
-	{
-		//return mTouch;
-	}
 
 	inline void RegisterMouseListener(MouseListener* listener)
 	{
@@ -119,13 +74,61 @@ public:
 
 	void EventLoop();
 
-protected:
+	//! Returns whether the cursor is visible.
+	bool is_cursor_visible() const;
+
+	//! Sets whether the cursor is visible.
+	void set_cursor_visible(bool visible);
+
+	/**
+		Returns the position of the cursor in window space.
+	@note
+		Coordinates in window space have the origin at the
+		upper-left corner of the window. +X extends from left
+		to right and +Y extends from top to bottom.
+	*/
+	Point2 get_cursor_xy() const;
+
+	/**
+		Sets the position of the cursor in window space.
+	@note
+		Coordinates in window space have the origin at the
+		upper-left corner of the window. +X extends from left
+		to right and +Y extends from top to bottom.
+	*/
+	void set_cursor_xy(const Point2& position);
+
+	/**
+		Returns the relative position of the cursor in window space.
+	@note
+		Coordinates in window space have the origin at the
+		upper-left corner of the window. +X extends from left
+		to right and +Y extends from top to bottom.
+	@note
+		Relative coordinates are mapped to a real varying
+		from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
+		maximum extent of the cosidered axis.
+	*/
+	Vec2 get_cursor_relative_xy() const;
+
+	/**
+		Sets the relative position of the cursor in window space.
+	@note
+		Coordinates in window space have the origin at the
+		upper-left corner of the window. +X extends from left
+		to right and +Y extends from top to bottom.
+	@note
+		Relative coordinates are mapped to a real varying
+		from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
+		maximum extent of the cosidered axis.
+	*/
+	void set_cursor_relative_xy(const Vec2& position);
+
+private:
 
 	EventDispatcher		mEventDispatcher;
 
-//	Mouse*				mMouse;
-//	Keyboard*			mKeyboard;
-//	Touch*				mTouch;
+	bool				m_cursor_visible;
 };
 
 InputManager* GetInputManager();

+ 0 - 82
src/input/Mouse.h

@@ -61,87 +61,5 @@ public:
 	virtual void CursorMoved(const MouseEvent& event) { (void)event; }
 };
 
-/**
-	Interface for accessing mouse input device.
-*/
-class Mouse
-{
-
-public:
-
-	/**
-		Constructor.
-	*/
-	Mouse() : mListener(NULL) {}
-
-	/**
-		Destructor.
-	*/
-	virtual ~Mouse() {}
-
-	/**
-		Returns whether the cursor is visible.
-	*/
-	virtual bool IsCursorVisible() const = 0;
-
-	/**
-		Sets whether the cursor is visible.
-	*/
-	virtual void SetCursorVisible(bool visible) = 0;
-
-	/**
-		Returns the position of the cursor in window space.
-	@note
-		Coordinates in window space have the origin at the
-		upper-left corner of the window. +X extends from left
-		to right and +Y extends from top to bottom.
-	*/
-	virtual Point2 GetCursorXY() const = 0;
-
-	/**
-		Sets the position of the cursor in window space.
-	@note
-		Coordinates in window space have the origin at the
-		upper-left corner of the window. +X extends from left
-		to right and +Y extends from top to bottom.
-	*/
-	virtual void SetCursorXY(const Point2& position) = 0;
-
-	/**
-		Returns the relative position of the cursor in window space.
-	@note
-		Coordinates in window space have the origin at the
-		upper-left corner of the window. +X extends from left
-		to right and +Y extends from top to bottom.
-	@note
-		Relative coordinates are mapped to a real varying
-		from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
-		maximum extent of the cosidered axis.
-	*/
-	virtual Vec2 GetCursorRelativeXY() const = 0;
-
-	/**
-		Sets the relative position of the cursor in window space.
-	@note
-		Coordinates in window space have the origin at the
-		upper-left corner of the window. +X extends from left
-		to right and +Y extends from top to bottom.
-	@note
-		Relative coordinates are mapped to a real varying
-		from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
-		maximum extent of the cosidered axis.
-	*/
-	virtual void SetCursorRelativeXY(const Vec2& position) = 0;
-
-	/**
-		Sets the listener for this device.
-	*/
-	inline void SetListener(MouseListener* listener) { mListener = listener; }
-
-protected:
-
-	MouseListener* mListener;
-};
-
 } // namespace crown
 

+ 4 - 1
src/os/OS.h

@@ -83,11 +83,14 @@ void			init_os();
 
 bool			create_render_window(uint x, uint y, uint width, uint height, bool fullscreen);
 bool			destroy_render_window();
+void			get_render_window_metrics(uint& width, uint& height);
 void			swap_buffers();
 
 void			event_loop();
 
 void			init_input();
+void			get_cursor_xy(int& x, int& y);
+void			set_cursor_xy(int x, int y);
 
 void			hide_cursor();
 void			show_cursor();
@@ -102,7 +105,7 @@ enum OSEventType
 
 	OSET_BUTTON_PRESS		= 3,
 	OSET_BUTTON_RELEASE		= 4,
-	OSET_MOTION_NOTIFY		= 5,
+	OSET_MOTION_NOTIFY		= 5
 };
 
 struct OSEvent

+ 12 - 0
src/os/linux/GLXRenderWindow.cpp

@@ -167,6 +167,18 @@ void swap_buffers()
 	glXSwapBuffers(display, glx_window);
 }
 
+//-----------------------------------------------------------------------------
+void get_render_window_metrics(uint& width, uint& height)
+{
+	XWindowAttributes attribs;
+	XGetWindowAttributes(display, window, &attribs);
+
+	XFlush(display);
+
+	width = attribs.width;
+	height = attribs.height;
+}
+
 ////-----------------------------------------------------------------------------
 //void GLXRenderWindow::Move(uint x, uint y)
 //{

+ 36 - 0
src/os/linux/Input.cpp

@@ -127,6 +127,42 @@ void init_input()
 	x11_create_hidden_cursor();
 }
 
+//-----------------------------------------------------------------------------
+void get_cursor_xy(int& x, int& y)
+{
+	Window unused;
+	int pointer_x, pointer_y, dummy;
+	uint dummy2;
+
+	XQueryPointer(display, window, &unused, &unused, &dummy, &dummy, &pointer_x, &pointer_y, &dummy2);
+
+	x = pointer_x;
+	y = pointer_y;
+}
+
+//-----------------------------------------------------------------------------
+void set_cursor_xy(int x, int y)
+{
+	uint width;
+	uint height;
+
+	get_render_window_metrics(width, height);
+
+	XWarpPointer(display, None, window, 0, 0, width, height, x, y);
+
+	XFlush(display);
+}
+
+//-----------------------------------------------------------------------------
+void hide_cursor()
+{
+}
+
+//-----------------------------------------------------------------------------
+void show_cursor()
+{
+}
+
 //-----------------------------------------------------------------------------
 void event_loop()
 {