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

Add support to input polling

Daniele Bartolini пре 12 година
родитељ
комит
e116fcd21b

+ 4 - 0
src/CMakeLists.txt

@@ -189,6 +189,10 @@ set (SETTINGS_HEADERS
 set (INPUT_SRC
 set (INPUT_SRC
 	input/EventDispatcher.cpp
 	input/EventDispatcher.cpp
 	input/InputManager.cpp
 	input/InputManager.cpp
+	input/Keyboard.cpp
+	input/Mouse.cpp
+	input/Touch.cpp
+	input/Accelerometer.cpp
 )
 )
 
 
 set (INPUT_HEADERS
 set (INPUT_HEADERS

+ 43 - 0
src/input/Accelerometer.cpp

@@ -0,0 +1,43 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "Accelerometer.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+Accelerometer::Accelerometer() :
+	m_orientation(0.0f, 0.0f, 0.0f)
+{
+}
+
+//-----------------------------------------------------------------------------
+const Vec3& Accelerometer::orientation() const
+{
+	return m_orientation;
+}
+
+} // namespace crown

+ 37 - 5
src/input/Accelerometer.h

@@ -1,5 +1,33 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
 #pragma once
 #pragma once
 
 
+#include "Types.h"
+#include "Vec3.h"
+
 namespace crown
 namespace crown
 {
 {
 
 
@@ -15,22 +43,26 @@ struct AccelerometerEvent
 class AccelerometerListener
 class AccelerometerListener
 {
 {
 public:
 public:
+
 	virtual void accelerometer_changed(const AccelerometerEvent& event) { (void)event; }
 	virtual void accelerometer_changed(const AccelerometerEvent& event) { (void)event; }
 };
 };
 
 
+/// Interface for accessing accelerometer input device.
 class Accelerometer
 class Accelerometer
 {
 {
 public:
 public:
 	
 	
-	Accelerometer() : m_listener(NULL) {}
+					Accelerometer();
 
 
-	virtual ~Accelerometer() {}
-
-	inline void set_listener(AccelerometerListener* listener) { m_listener = listener; }
+	/// Returns the orientation of the accelerometer.
+	/// FIXME NEED MORE DOCUMENTATION
+	const Vec3&		orientation() const;
 
 
 private:
 private:
 
 
-	AccelerometerListener* m_listener;
+	Vec3			m_orientation;
+
+	friend class	InputManager;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 61 - 45
src/input/InputManager.cpp

@@ -41,6 +41,36 @@ InputManager::~InputManager()
 {
 {
 }
 }
 
 
+//-----------------------------------------------------------------------------
+void InputManager::register_mouse_listener(MouseListener* listener)
+{
+	m_event_dispatcher.add_mouse_listener(listener);
+}
+
+//-----------------------------------------------------------------------------
+void InputManager::register_keyboard_listener(KeyboardListener* listener)
+{
+	m_event_dispatcher.add_keyboard_listener(listener);
+}
+
+//-----------------------------------------------------------------------------
+void InputManager::register_touch_listener(TouchListener* listener)
+{
+	m_event_dispatcher.add_touch_listener(listener);
+}
+
+//-----------------------------------------------------------------------------
+void InputManager::register_accelerometer_listener(AccelerometerListener* listener)
+{
+	m_event_dispatcher.add_accelerometer_listener(listener);
+}
+
+//-----------------------------------------------------------------------------
+EventDispatcher* InputManager::get_event_dispatcher()
+{
+	return &m_event_dispatcher;
+}
+
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void InputManager::event_loop()
 void InputManager::event_loop()
 {
 {
@@ -67,10 +97,12 @@ void InputManager::event_loop()
 
 
 				if (event.type == os::OSET_BUTTON_PRESS)
 				if (event.type == os::OSET_BUTTON_PRESS)
 				{
 				{
+					m_mouse.m_buttons[mouse_event.button] = true;
 					m_event_dispatcher.button_pressed(mouse_event);
 					m_event_dispatcher.button_pressed(mouse_event);
 				}
 				}
 				else
 				else
 				{
 				{
+					m_mouse.m_buttons[mouse_event.button] = false;
 					m_event_dispatcher.button_released(mouse_event);
 					m_event_dispatcher.button_released(mouse_event);
 				}
 				}
 
 
@@ -84,10 +116,12 @@ void InputManager::event_loop()
 
 
 				if (event.type == os::OSET_KEY_PRESS)
 				if (event.type == os::OSET_KEY_PRESS)
 				{
 				{
+					m_keyboard.m_keys[keyboard_event.key] = true;
 					m_event_dispatcher.key_pressed(keyboard_event);
 					m_event_dispatcher.key_pressed(keyboard_event);
 				}
 				}
 				else
 				else
 				{
 				{
+					m_keyboard.m_keys[keyboard_event.key] = false;
 					m_event_dispatcher.key_released(keyboard_event);
 					m_event_dispatcher.key_released(keyboard_event);
 				}
 				}
 
 
@@ -100,14 +134,25 @@ void InputManager::event_loop()
 				touch_event.pointer_id = event.data_a.int_value;
 				touch_event.pointer_id = event.data_a.int_value;
 				touch_event.x = event.data_b.int_value;
 				touch_event.x = event.data_b.int_value;
 				touch_event.y = event.data_c.int_value;
 				touch_event.y = event.data_c.int_value;
+
+				m_touch.m_pointers[touch_event.pointer_id].x = touch_event.x;
+				m_touch.m_pointers[touch_event.pointer_id].y = touch_event.y;
+
+				// FIXME
+				m_touch.m_pointers[touch_event.pointer_id].relative_x = 0.0f;
+				m_touch.m_pointers[touch_event.pointer_id].relative_y = 0.0f;
+
 				if (event.type == os::OSET_TOUCH_DOWN)
 				if (event.type == os::OSET_TOUCH_DOWN)
 				{
 				{
+					m_touch.m_pointers[touch_event.pointer_id].up = false;
 					m_event_dispatcher.touch_down(touch_event);
 					m_event_dispatcher.touch_down(touch_event);
 				}
 				}
 				else
 				else
 				{
 				{
+					m_touch.m_pointers[touch_event.pointer_id].up = true;
 					m_event_dispatcher.touch_up(touch_event);
 					m_event_dispatcher.touch_up(touch_event);
 				}
 				}
+
 				break;
 				break;
 			}
 			}
 			case os::OSET_TOUCH_MOVE:
 			case os::OSET_TOUCH_MOVE:
@@ -115,8 +160,17 @@ void InputManager::event_loop()
 				TouchEvent touch_event;
 				TouchEvent touch_event;
 				touch_event.pointer_id = event.data_a.int_value;
 				touch_event.pointer_id = event.data_a.int_value;
 				touch_event.x = event.data_b.int_value;
 				touch_event.x = event.data_b.int_value;
-				touch_event.y = event.data_c.int_value;	
-				m_event_dispatcher.touch_move(touch_event);			
+				touch_event.y = event.data_c.int_value;
+
+				m_touch.m_pointers[touch_event.pointer_id].x = touch_event.x;
+				m_touch.m_pointers[touch_event.pointer_id].y = touch_event.y;
+
+				// FIXME
+				m_touch.m_pointers[touch_event.pointer_id].relative_x = 0.0f;
+				m_touch.m_pointers[touch_event.pointer_id].relative_y = 0.0f;
+
+				m_event_dispatcher.touch_move(touch_event);
+
 				break;
 				break;
 			}
 			}
 			case os::OSET_ACCELEROMETER:
 			case os::OSET_ACCELEROMETER:
@@ -125,6 +179,11 @@ void InputManager::event_loop()
 				sensor_event.x = event.data_a.float_value;
 				sensor_event.x = event.data_a.float_value;
 				sensor_event.y = event.data_b.float_value;
 				sensor_event.y = event.data_b.float_value;
 				sensor_event.z = event.data_c.float_value;
 				sensor_event.z = event.data_c.float_value;
+
+				m_accelerometer.m_orientation.x = sensor_event.x;
+				m_accelerometer.m_orientation.y = sensor_event.y;
+				m_accelerometer.m_orientation.z = sensor_event.z;
+
 				m_event_dispatcher.accelerometer_changed(sensor_event);
 				m_event_dispatcher.accelerometer_changed(sensor_event);
 				break;
 				break;
 			}
 			}
@@ -157,48 +216,5 @@ void InputManager::set_cursor_visible(bool visible)
 	m_cursor_visible = visible;
 	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
-{
-	uint32_t window_width;
-	uint32_t 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)
-{
-	uint32_t window_width;
-	uint32_t window_height;
-
-	os::get_render_window_metrics(window_width, window_height);
-
-	set_cursor_xy(Point2((int32_t)(position.x * (float) window_width), (int32_t)(position.y * (float) window_height)));
-}
-
 } // namespace crown
 } // namespace crown
 
 

+ 26 - 92
src/input/InputManager.h

@@ -26,9 +26,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 #pragma once
 
 
 #include "EventDispatcher.h"
 #include "EventDispatcher.h"
-#include "Point2.h"
-#include "Vec2.h"
-#include "Log.h"
+#include "Mouse.h"
+#include "Keyboard.h"
+#include "Touch.h"
+#include "Accelerometer.h"
 
 
 namespace crown
 namespace crown
 {
 {
@@ -39,102 +40,35 @@ class TouchListener;
 
 
 class InputManager
 class InputManager
 {
 {
-
 public:
 public:
 
 
-	InputManager();
-	~InputManager();
-
-	//! Initializes the input manager.
-	void init();
-
-	bool is_mouse_available() { return true; }
-	bool is_keyboard_available() { return true; }
-	bool is_touch_available() { return true; }
-	bool is_accelerometer_available() { return true; }
-
-
-	inline void register_mouse_listener(MouseListener* listener)
-	{
-		m_event_dispatcher.add_mouse_listener(listener);
-	}
-
-	inline void register_keyboard_listener(KeyboardListener* listener)
-	{
-		m_event_dispatcher.add_keyboard_listener(listener);
-	}
-
-	inline void register_touch_listener(TouchListener* listener)
-	{
-		m_event_dispatcher.add_touch_listener(listener);
-	}
-
-	inline void register_accelerometer_listener(AccelerometerListener* listener)
-	{
-		m_event_dispatcher.add_accelerometer_listener(listener);
-	}
-
-	inline EventDispatcher* get_event_dispatcher()
-	{
-		return &m_event_dispatcher;
-	}
-
-	void event_loop();
-
-	//! 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);
+						InputManager();
+						~InputManager();
+
+	void				register_mouse_listener(MouseListener* listener);
+	void				register_keyboard_listener(KeyboardListener* listener);
+	void				register_touch_listener(TouchListener* listener);
+	void				register_accelerometer_listener(AccelerometerListener* listener);
+
+	EventDispatcher*	get_event_dispatcher();
+
+	/// Returns whether the cursor is visible.
+	bool				is_cursor_visible() const;
+
+	/// Sets whether the cursor is visible.
+	void				set_cursor_visible(bool visible);
+
+	void				event_loop();
 
 
 private:
 private:
 
 
 	EventDispatcher		m_event_dispatcher;
 	EventDispatcher		m_event_dispatcher;
 
 
+	Keyboard			m_keyboard;
+	Mouse				m_mouse;
+	Touch				m_touch;
+	Accelerometer		m_accelerometer;
+
 	bool				m_cursor_visible;
 	bool				m_cursor_visible;
 };
 };
 
 

+ 6 - 5
src/input/KeyCode.h

@@ -28,10 +28,10 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 namespace crown
 {
 {
 
 
-/**
-	A KeyCode is the number which uniquely identifies a key
-	on the keyboard.
-*/
+const uint16_t MAX_KEYCODES = 256;
+
+/// A KeyCode is the number which uniquely identifies a key
+/// on the keyboard.
 enum KeyCode
 enum KeyCode
 {
 {
 	KC_NOKEY		= 0x00,
 	KC_NOKEY		= 0x00,
@@ -91,7 +91,8 @@ enum KeyCode
 	/* [0x30, 0x39] reserved for ASCII digits */
 	/* [0x30, 0x39] reserved for ASCII digits */
 	/* [0x41, 0x5A] and [0x61, 0x7A] reserved for ASCII alphabet */
 	/* [0x41, 0x5A] and [0x61, 0x7A] reserved for ASCII alphabet */
 
 
-	KC_COUNT		= 0xFF		// The last key must be <= 0xFF
+	// The last key _must_ be <= 0xFF
+	KC_COUNT		= 0xFF
 };
 };
 
 
 typedef uint8_t Key;
 typedef uint8_t Key;

+ 58 - 0
src/input/Keyboard.cpp

@@ -0,0 +1,58 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "Keyboard.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+Keyboard::Keyboard()
+{
+	for (uint32_t i = 0; i < MAX_KEYCODES; i++)
+	{
+		m_keys[i] = false;
+	}
+}
+
+//-----------------------------------------------------------------------------
+bool Keyboard::modifier_pressed(ModifierKey modifier) const
+{
+	return (m_modifier & modifier) == modifier;
+}
+
+//-----------------------------------------------------------------------------
+bool Keyboard::key_pressed(KeyCode key) const
+{
+	return m_keys[key] == true;
+}
+
+//-----------------------------------------------------------------------------
+bool Keyboard::key_released(KeyCode key) const
+{
+	return m_keys[key] == false;
+}
+
+} // namespace crown

+ 27 - 46
src/input/Keyboard.h

@@ -28,16 +28,13 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Types.h"
 #include "Types.h"
 #include "KeyCode.h"
 #include "KeyCode.h"
 
 
-#define MAX_KEYCODES 256
-
 namespace crown
 namespace crown
 {
 {
 
 
 class InputManager;
 class InputManager;
 
 
-/**
-	Enumerates modifier keys.
-*/
+
+/// Enumerates modifier keys.
 enum ModifierKey
 enum ModifierKey
 {
 {
 	MK_SHIFT	= 1,
 	MK_SHIFT	= 1,
@@ -61,51 +58,35 @@ public:
 	virtual void text_input(const KeyboardEvent& event) { (void)event; }
 	virtual void text_input(const KeyboardEvent& event) { (void)event; }
 };
 };
 
 
-/**
-	Interface for accessing keyboard input device.
-*/
+/// Interface for accessing keyboard input device.
 class Keyboard
 class Keyboard
 {
 {
-
 public:
 public:
 
 
-	/**
-		Constructor.
-	*/
-	Keyboard() : m_listener(NULL) {}
-
-	/**
-		Destructor.
-	*/
-	virtual ~Keyboard() {}
-
-	/**
-		Returns whether the specified modifier is pressed.
-	@note
-		A modifier is a special key that modifies the normal action
-		of another key when the two are pressed in combination. (Thanks wikipedia.)
-		Crown currently supports three different modifier keys: Shift, Ctrl and Alt.
-	*/
-	virtual bool is_modifier_pressed(ModifierKey modifier) const = 0;
-
-	/**
-		Returns whether the specified key is pressed.
-	*/
-	virtual bool is_key_pressed(KeyCode key) const = 0;
-
-	/**
-		Returns whether the specified key is released.
-	*/
-	virtual bool is_key_released(KeyCode key) const = 0;
-
-	/**
-		Sets the listener for this device.
-	*/
-	inline void set_listener(KeyboardListener* listener) { m_listener = listener; }
-
-protected:
-
-	KeyboardListener* m_listener;
+					Keyboard();
+
+	/// Returns whether the specified @modifier is pressed.
+	/// @note
+	/// A modifier is a special key that modifies the normal action
+	/// of another key when the two are pressed in combination. (Thanks wikipedia.)
+	/// @note
+	/// Crown currently supports three different modifier keys: Shift, Ctrl and Alt.
+	bool			modifier_pressed(ModifierKey modifier) const;
+
+	/// Returns whether the specified @key is pressed.
+	bool			key_pressed(KeyCode key) const;
+
+	/// Returns whether the specified @key is released.
+	bool			key_released(KeyCode key) const;
+
+private:
+
+	ModifierKey		m_modifier;
+
+	// True if key pressed, false otherwise.
+	bool			m_keys[MAX_KEYCODES];
+
+	friend class	InputManager;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 95 - 0
src/input/Mouse.cpp

@@ -0,0 +1,95 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "Mouse.h"
+#include "OS.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+Mouse::Mouse()
+{
+	m_buttons[MB_LEFT] = false;
+	m_buttons[MB_MIDDLE] = false;
+	m_buttons[MB_RIGHT] = false;
+}
+
+//-----------------------------------------------------------------------------
+bool Mouse::button_pressed(MouseButton button) const
+{
+	return m_buttons[button] == true;
+}
+
+//-----------------------------------------------------------------------------
+bool Mouse::button_released(MouseButton button) const
+{
+	return m_buttons[button] == false;
+}
+
+//-----------------------------------------------------------------------------
+Point2 Mouse::cursor_xy() const
+{
+	Point2 xy;
+
+	os::get_cursor_xy(xy.x, xy.y);
+
+	return xy;
+}
+
+//-----------------------------------------------------------------------------
+void Mouse::set_cursor_xy(const Point2& position)
+{
+	os::set_cursor_xy(position.x, position.y);
+}
+
+//-----------------------------------------------------------------------------
+Vec2 Mouse::cursor_relative_xy() const
+{
+	uint32_t window_width;
+	uint32_t window_height;
+
+	os::get_render_window_metrics(window_width, window_height);
+
+	Vec2 pos = cursor_xy().to_vec2();
+
+	pos.x = pos.x / (float) window_width;
+	pos.y = pos.y / (float) window_height;
+
+	return pos;
+}
+
+//-----------------------------------------------------------------------------
+void Mouse::set_cursor_relative_xy(const Vec2& position)
+{
+	uint32_t window_width;
+	uint32_t window_height;
+
+	os::get_render_window_metrics(window_width, window_height);
+
+	set_cursor_xy(Point2((int32_t)(position.x * (float) window_width), (int32_t)(position.y * (float) window_height)));
+}
+
+} // namespace crown

+ 63 - 7
src/input/Mouse.h

@@ -26,15 +26,15 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 #pragma once
 
 
 #include "Types.h"
 #include "Types.h"
+#include "Vec2.h"
+#include "Point2.h"
 
 
 namespace crown
 namespace crown
 {
 {
 
 
-class InputManager;
+const uint32_t MAX_MOUSE_BUTTONS = 3;
 
 
-/**
-	Enumerates mouse buttons.
-*/
+/// Enumerates mouse buttons.
 enum MouseButton
 enum MouseButton
 {
 {
 	MB_LEFT		= 0,
 	MB_LEFT		= 0,
@@ -50,9 +50,7 @@ struct MouseEvent
 	float wheel;
 	float wheel;
 };
 };
 
 
-/**
-	Interface for managing mouse input
-*/
+/// Interface for managing mouse input.
 class MouseListener
 class MouseListener
 {
 {
 
 
@@ -63,5 +61,63 @@ public:
 	virtual void cursor_moved(const MouseEvent& event) { (void)event; }
 	virtual void cursor_moved(const MouseEvent& event) { (void)event; }
 };
 };
 
 
+/// Interface for accessing mouse input device.
+class Mouse
+{
+public:
+
+			Mouse();
+
+
+	/// Returns whether @button is pressed.
+	bool	button_pressed(MouseButton button) const;
+
+	/// Returns whether @button is released.
+	bool	button_released(MouseButton button) const;
+
+	/// 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	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	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:
+
+	// True if correspondig button is pressed, false otherwise.
+	bool	m_buttons[MAX_MOUSE_BUTTONS];
+
+	friend class	InputManager;
+};
+
 } // namespace crown
 } // namespace crown
 
 

+ 65 - 0
src/input/Touch.cpp

@@ -0,0 +1,65 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "Touch.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+Touch::Touch()
+{
+
+}
+
+//-----------------------------------------------------------------------------
+bool Touch::touch_up(uint16_t id) const
+{
+	return m_pointers[id].up == true;
+}
+
+//-----------------------------------------------------------------------------
+bool Touch::touch_down(uint16_t id) const
+{
+	return m_pointers[id].up == false;
+}
+
+//----------------------------------------------------------------------------- 
+Point2 Touch::touch_xy(uint16_t id) const
+{
+	const PointerData& data = m_pointers[id];
+
+	return Point2(data.x, data.y);
+}
+
+//-----------------------------------------------------------------------------
+Vec2 Touch::touch_relative_xy(uint16_t id)
+{
+	const PointerData& data = m_pointers[id];
+
+	return Vec2(data.relative_x, data.relative_y);
+}
+
+} // namespace crown

+ 34 - 27
src/input/Touch.h

@@ -25,21 +25,32 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 
 #pragma once
 #pragma once
 
 
+#include "Types.h"
+#include "Vec2.h"
+#include "Point2.h"
+
 namespace crown
 namespace crown
 {
 {
 
 
-class InputManager;
+const uint32_t MAX_POINTER_IDS = 4;
+
+struct PointerData
+{
+	uint32_t	x;
+	uint32_t	y;
+	float		relative_x;
+	float		relative_y;
+	bool		up;
+};
 
 
 struct TouchEvent
 struct TouchEvent
 {
 {
-	int32_t pointer_id;
-	int32_t x;
-	int32_t y;
+	uint32_t	pointer_id;
+	uint32_t	x;
+	uint32_t	y;
 };
 };
 
 
-/**
-	Interface for managing touch input device.	
-*/
+/// Interface for managing touch input device.
 class TouchListener
 class TouchListener
 {
 {
 
 
@@ -51,34 +62,30 @@ public:
 	virtual void touch_cancel(const TouchEvent& event) { (void)event; }
 	virtual void touch_cancel(const TouchEvent& event) { (void)event; }
 };
 };
 
 
-/**
-	Interface for accessing touch input device.
-*/
+/// Interface for accessing touch input device.
 class Touch
 class Touch
 {
 {
-
 public:
 public:
 
 
-	/**
-		Constructor.
-	*/
-	Touch() : m_listener(NULL) {}
+					Touch();
+
+	/// Returns whether the touch pointer @id is up.
+	bool			touch_up(uint16_t id) const;
+
+	/// Returns whether the touch pointer @id is down.
+	bool			touch_down(uint16_t id) const;
+
+	/// Returns the position of the pointer @id in windows space. 
+	Point2			touch_xy(uint16_t id) const;
 
 
-	/**
-		Destructor.
-	*/
-	virtual ~Touch() {}
+	/// Returns the relative position of the pointer @id in window space.
+	Vec2			touch_relative_xy(uint16_t id);
 
 
-	/**
-		Sets the listener for this device.
-	@param listener
-		The listener
-	*/
-	inline void set_listener(TouchListener* listener) { m_listener = listener; }
+private:
 
 
-protected:
+	PointerData		m_pointers[MAX_POINTER_IDS];
 
 
-	TouchListener* m_listener;
+	friend class	InputManager;
 };
 };
 
 
 }
 }