Daniele Bartolini 10 лет назад
Родитель
Сommit
0bb819d563

+ 2 - 2
src/core/os_event_queue.h

@@ -6,8 +6,8 @@
 #pragma once
 
 #include "types.h"
-#include "mouse.h"
-#include "keyboard.h"
+#include "input_types.h"
+#include "math_types.h"
 #include "atomic_int.h"
 
 namespace crown

+ 0 - 7
src/crown.cpp

@@ -5,15 +5,11 @@
 
 #include "crown.h"
 #include "memory.h"
-#include "input.h"
 #include "console_server.h"
 #include "bundle_compiler.h"
 #include "device.h"
 #include "command_line.h"
 #include "json_parser.h"
-#include "keyboard.h"
-#include "mouse.h"
-#include "touch.h"
 #include "main.h"
 #include "audio.h"
 #include "physics.h"
@@ -187,7 +183,6 @@ void parse_config_file(Filesystem& fs, ConfigSettings& cs)
 bool init(Filesystem& fs, const ConfigSettings& cs)
 {
 	profiler_globals::init();
-	input_globals::init();
 	audio_globals::init();
 	physics_globals::init();
 	bgfx::init();
@@ -204,7 +199,6 @@ void update()
 		console_server_globals::update();
 		device()->update();
 		bgfx::frame();
-		input_globals::update();
 		profiler_globals::flush();
 	}
 }
@@ -216,7 +210,6 @@ void shutdown()
 	bgfx::shutdown();
 	physics_globals::shutdown();
 	audio_globals::shutdown();
-	input_globals::shutdown();
 	profiler_globals::shutdown();
 }
 } // namespace crown

+ 17 - 4
src/device.cpp

@@ -3,18 +3,20 @@
  * License: https://github.com/taylor001/crown/blob/master/LICENSE
  */
 
+#include "device.h"
+#include "array.h"
 #include "config.h"
 #include "debug_line.h"
-#include "device.h"
+#include "input_manager.h"
 #include "log.h"
 #include "lua_environment.h"
 #include "material_manager.h"
+#include "memory.h"
+#include "os.h"
 #include "resource_manager.h"
 #include "resource_package.h"
 #include "types.h"
 #include "world.h"
-#include "memory.h"
-#include "os.h"
 
 #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
 
@@ -39,6 +41,7 @@ Device::Device(const ConfigSettings& cs, Filesystem& fs)
 	, _boot_package(NULL)
 	, _lua_environment(NULL)
 	, _resource_manager(NULL)
+	, _input_manager(NULL)
 	, _worlds(default_allocator())
 {
 }
@@ -59,6 +62,8 @@ void Device::init()
 	_lua_environment = CE_NEW(_allocator, LuaEnvironment)();
 	_lua_environment->load_libs();
 
+	_input_manager = CE_NEW(_allocator, InputManager)();
+
 	CE_LOGD("Crown Engine initialized.");
 	CE_LOGD("Initializing Game...");
 
@@ -78,6 +83,8 @@ void Device::shutdown()
 {
 	CE_ASSERT(_is_init, "Engine is not initialized");
 
+	CE_DELETE(_allocator, _input_manager);
+
 	// Shutdowns the game
 	_lua_environment->call_global("shutdown", 0);
 
@@ -107,6 +114,11 @@ LuaEnvironment* Device::lua_environment()
 	return _lua_environment;
 }
 
+InputManager* Device::input_manager()
+{
+	return _input_manager;
+}
+
 void Device::quit()
 {
 	_is_running = false;
@@ -160,8 +172,9 @@ void Device::update()
 		_lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
 	}
 
-	_frame_count++;
+	_input_manager->update();
 
+	_frame_count++;
 	_lua_environment->clear_temporaries();
 }
 

+ 6 - 1
src/device.h

@@ -12,7 +12,8 @@
 #include "resource_types.h"
 #include "lua_types.h"
 #include "filesystem_types.h"
-#include "array.h"
+#include "container_types.h"
+#include "input_types.h"
 #include "crown.h"
 
 namespace crown
@@ -107,6 +108,9 @@ struct Device
 	/// Returns the lua environment.
 	LuaEnvironment* lua_environment();
 
+	/// Returns the input manager.
+	InputManager* input_manager();
+
 private:
 
 	// Used to allocate all subsystems
@@ -133,6 +137,7 @@ private:
 
 	LuaEnvironment* _lua_environment;
 	ResourceManager* _resource_manager;
+	InputManager* _input_manager;
 
 	Array<World*> _worlds;
 

+ 0 - 65
src/input/input.cpp

@@ -1,65 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#include "input.h"
-#include "keyboard.h"
-#include "mouse.h"
-#include "touch.h"
-#include "memory.h"
-
-namespace crown
-{
-namespace input_globals
-{
-	const size_t BUFFER_SIZE = 0 +
-		+ sizeof(Keyboard)
-		+ sizeof(Mouse)
-		+ sizeof(Touch);
-
-	char _buffer[BUFFER_SIZE];
-	Keyboard* _keyboard = NULL;
-	Mouse* _mouse = NULL;
-	Touch* _touch = NULL;
-
-	void init()
-	{
-		_keyboard = new (_buffer) Keyboard();
-		_mouse = new (_keyboard + 1) Mouse();
-		_touch = new (_mouse + 1) Touch();
-	}
-
-	void shutdown()
-	{
-		_keyboard->~Keyboard();
-		_keyboard = NULL;
-		_mouse->~Mouse();
-		_mouse = NULL;
-		_touch->~Touch();
-		_touch = NULL;
-	}
-
-	void update()
-	{
-		_keyboard->update();
-		_mouse->update();
-		_touch->update();
-	}
-
-	Keyboard& keyboard()
-	{
-		return *_keyboard;
-	}
-
-	Mouse& mouse()
-	{
-		return *_mouse;
-	}
-
-	Touch& touch()
-	{
-		return *_touch;
-	}
-} // namespace input_globals
-} // namespace crown

+ 0 - 37
src/input/input.h

@@ -1,37 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#pragma once
-
-#include "input_types.h"
-
-namespace crown
-{
-/// @defgroup Input Input
-
-/// Global input-related functions.
-///
-/// @ingroup Input
-namespace input_globals
-{
-	/// Initializes the input system.
-	void init();
-
-	/// Shutdowns the input system.
-	void shutdown();
-
-	/// Updates input devices.
-	void update();
-
-	/// Returns the default keyboard.
-	Keyboard& keyboard();
-
-	/// Returns the default mouse.
-	Mouse& mouse();
-
-	/// Rettuns the default touch panel.
-	Touch& touch();
-} // namespace input_globals
-} // namespace crown

+ 74 - 0
src/input/input_device.cpp

@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "input_device.h"
+#include "error.h"
+#include <string.h> // memcpy
+
+namespace crown
+{
+
+const char* InputDevice::name() const
+{
+	return _name;
+}
+
+uint8_t InputDevice::num_buttons() const
+{
+	return _num_buttons;
+}
+
+uint8_t InputDevice::num_axes() const
+{
+	return _num_axes;
+}
+
+bool InputDevice::pressed(uint8_t i) const
+{
+	CE_ASSERT(i < _num_buttons, "Index out of bounds");
+	return (~_last_state[i] & _current_state[i]) != 0;
+}
+
+bool InputDevice::released(uint8_t i) const
+{
+	CE_ASSERT(i < _num_buttons, "Index out of bounds");
+	return (_last_state[i] & ~_current_state[i]) != 0;
+}
+
+bool InputDevice::any_pressed() const
+{
+	return pressed(_last_button);
+}
+
+bool InputDevice::any_released() const
+{
+	return released(_last_button);
+}
+
+Vector3 InputDevice::axis(uint8_t i) const
+{
+	CE_ASSERT(i < _num_axes, "Index out of bounds");
+	return _axis[i];
+}
+
+void InputDevice::set_button_state(uint8_t i, bool state)
+{
+	CE_ASSERT(i < _num_buttons, "Index out of bounds");
+	_last_button = i;
+	_current_state[i] = state;
+}
+
+void InputDevice::set_axis(uint8_t i, const Vector3& value)
+{
+	CE_ASSERT(i < _num_axes, "Index out of bounds");
+	_axis[i] = value;
+}
+
+void InputDevice::update()
+{
+	memcpy(_last_state, _current_state, sizeof(uint8_t)*_num_buttons);
+}
+
+} // namespace crown

+ 56 - 0
src/input/input_device.h

@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#pragma once
+
+#include "types.h"
+#include "math_types.h"
+
+namespace crown
+{
+
+struct InputDevice
+{
+	/// Returns the name of the input device.
+	const char* name() const;
+
+	/// Returns the number of buttons of the input device.
+	uint8_t num_buttons() const;
+
+	/// Returns the number of axes of the input devices.
+	uint8_t num_axes() const;
+
+	/// Returns whether the specified @a b button is pressed in the current frame.
+	bool pressed(uint8_t i) const;
+
+	/// Returns whether the specified @a b button is released in the current frame.
+	bool released(uint8_t i) const;
+
+	/// Returns wheter any button is pressed in the current frame.
+	bool any_pressed() const;
+
+	/// Returns whether any button is released in the current frame.
+	bool any_released() const;
+
+	/// Returns the value of the axis @a i.
+	Vector3 axis(uint8_t i) const;
+
+	void set_button_state(uint8_t i, bool state);
+
+	void set_axis(uint8_t i, const Vector3& value);
+
+	void update();
+
+	uint8_t _num_buttons;
+	uint8_t _num_axes;
+	uint8_t _last_button;
+
+	uint8_t* _last_state;    // num_buttons
+	uint8_t* _current_state; // num_buttons
+	Vector3* _axis;          // num_axes
+	char* _name;             // strlen(name) + 1
+};
+
+} // namespace crown

+ 81 - 0
src/input/input_manager.cpp

@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "input_manager.h"
+#include "input_device.h"
+#include "memory.h"
+#include "vector3.h"
+#include <string.h> // strlen, strcpy, memset
+
+namespace crown
+{
+
+InputManager::InputManager()
+	: _keyboard(NULL)
+	, _mouse(NULL)
+	, _touch(NULL)
+{
+	_keyboard = create_input_device("Keyboard", KeyboardButton::COUNT, 0);
+	_mouse = create_input_device("Mouse", MouseButton::COUNT, 2);
+	_touch = create_input_device("Touch", TouchButton::COUNT, TouchButton::COUNT);
+}
+
+InputManager::~InputManager()
+{
+	default_allocator().deallocate(_touch);
+	default_allocator().deallocate(_mouse);
+	default_allocator().deallocate(_keyboard);
+}
+
+InputDevice* InputManager::create_input_device(const char* name, uint8_t num_buttons, uint8_t num_axes)
+{
+	const uint32_t size = 0
+		+ sizeof(InputDevice)
+		+ sizeof(uint8_t)*num_buttons*2
+		+ sizeof(Vector3)*num_axes
+		+ strlen(name) + 1;
+
+	InputDevice* id = (InputDevice*)default_allocator().allocate(size);
+
+	id->_num_buttons = num_buttons;
+	id->_num_axes = num_axes;
+	id->_last_button = 0;
+
+	id->_last_state = (uint8_t*)&id[1];
+	id->_current_state = (uint8_t*)(id->_last_state + num_buttons);
+	id->_axis = (Vector3*)(id->_current_state + num_buttons);
+	id->_name = (char*)(id->_axis + num_axes);
+
+	memset(id->_last_state, 0, sizeof(uint8_t)*num_buttons);
+	memset(id->_current_state, 0, sizeof(uint8_t)*num_buttons);
+	memset(id->_axis, 0, sizeof(Vector3)*num_axes);
+	strcpy(id->_name, name);
+
+	return id;
+}
+
+InputDevice* InputManager::keyboard()
+{
+	return _keyboard;
+}
+
+InputDevice* InputManager::mouse()
+{
+	return _mouse;
+}
+
+InputDevice* InputManager::touch()
+{
+	return _touch;
+}
+
+void InputManager::update()
+{
+	_keyboard->update();
+	_mouse->update();
+	_touch->update();
+}
+
+} // namespace crown

+ 43 - 0
src/input/input_manager.h

@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#pragma once
+
+#include "types.h"
+#include "input_types.h"
+
+namespace crown
+{
+
+class InputManager
+{
+public:
+
+	InputManager();
+	~InputManager();
+
+	/// Creates a new input device and returns it.
+	InputDevice* create_input_device(const char* name, uint8_t num_buttons, uint8_t num_axes);
+
+	/// Returns the default keyboard input device.
+	InputDevice* keyboard();
+
+	/// Returns the default mouse input device.
+	InputDevice* mouse();
+
+	/// Returns the default touch input device.
+	InputDevice* touch();
+
+	/// Updates the input devices
+	void update();
+
+private:
+
+	InputDevice* _keyboard;
+	InputDevice* _mouse;
+	InputDevice* _touch;
+};
+
+} // namespace crown

+ 141 - 3
src/input/input_types.h

@@ -7,7 +7,145 @@
 
 namespace crown
 {
-	struct Keyboard;
-	struct Mouse;
-	struct Touch;
+
+class InputManager;
+struct InputDevice;
+
+/// Enumerates keyboard buttons.
+///
+/// @ingroup Input
+struct KeyboardButton
+{
+	enum Enum
+	{
+		NONE      = 0x00,
+
+		TAB       = 0x09,
+		ENTER     = 0x0d,
+		ESCAPE    = 0x1b,
+		SPACE     = 0x20,
+		BACKSPACE = 0x7f,
+
+		/* KeyPad */
+		KP_0      = 0x80,
+		KP_1      = 0x81,
+		KP_2      = 0x82,
+		KP_3      = 0x83,
+		KP_4      = 0x84,
+		KP_5      = 0x85,
+		KP_6      = 0x86,
+		KP_7      = 0x87,
+		KP_8      = 0x88,
+		KP_9      = 0x89,
+
+		/* Function keys */
+		F1        = 0x90,
+		F2        = 0x91,
+		F3        = 0x92,
+		F4        = 0x93,
+		F5        = 0x94,
+		F6        = 0x95,
+		F7        = 0x96,
+		F8        = 0x97,
+		F9        = 0x98,
+		F10       = 0x99,
+		F11       = 0x9a,
+		F12       = 0x9b,
+
+		/* Other keys */
+		HOME      = 0xa0,
+		LEFT      = 0xa1,
+		UP        = 0xa2,
+		RIGHT     = 0xa3,
+		DOWN      = 0xa4,
+		PAGE_UP   = 0xa5,
+		PAGE_DOWN = 0xa6,
+
+		/* Modifier keys */
+		LCONTROL  = 0xb0,
+		RCONTROL  = 0xb1,
+		LSHIFT    = 0xb2,
+		RSHIFT    = 0xb3,
+		CAPS_LOCK = 0xb4,
+		LALT      = 0xb5,
+		RALT      = 0xb6,
+		LSUPER    = 0xb7,
+		RSUPER    = 0xb8,
+
+		/* [0x30, 0x39] reserved for ASCII digits */
+		NUM_0     = 0x30,
+		NUM_1     = 0x31,
+		NUM_2     = 0x32,
+		NUM_3     = 0x33,
+		NUM_4     = 0x34,
+		NUM_5     = 0x35,
+		NUM_6     = 0x36,
+		NUM_7     = 0x37,
+		NUM_8     = 0x38,
+		NUM_9     = 0x39,
+
+		/* [0x41, 0x5a] reserved for ASCII alphabet */
+		A         = 0x41,
+		B         = 0x42,
+		C         = 0x43,
+		D         = 0x44,
+		E         = 0x45,
+		F         = 0x46,
+		G         = 0x47,
+		H         = 0x48,
+		I         = 0x49,
+		J         = 0x4a,
+		K         = 0x4b,
+		L         = 0x4c,
+		M         = 0x4d,
+		N         = 0x4e,
+		O         = 0x4f,
+		P         = 0x50,
+		Q         = 0x51,
+		R         = 0x52,
+		S         = 0x53,
+		T         = 0x54,
+		U         = 0x55,
+		V         = 0x56,
+		W         = 0x57,
+		X         = 0x58,
+		Y         = 0x59,
+		Z         = 0x5a,
+
+		/* [0x61, 0x7a] reserved for ASCII alphabet */
+
+		// The last key _must_ be <= 0xff
+		COUNT     = 0xff
+	};
+};
+
+/// Enumerates mouse buttons.
+///
+/// @ingroup Input
+struct MouseButton
+{
+	enum Enum
+	{
+		LEFT,
+		MIDDLE,
+		RIGHT,
+		EXTRA_1,
+		EXTRA_2,
+		COUNT
+	};
+};
+
+/// Enumerates touch panel buttons
+struct TouchButton
+{
+	enum Enum
+	{
+		POINTER_0,
+		POINTER_1,
+		POINTER_2,
+		POINTER_3,
+		COUNT
+	};
+};
+
 } // namespace crown

+ 0 - 119
src/input/key_code.h

@@ -1,119 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#pragma once
-
-namespace crown
-{
-
-/// Enumerates keyboard buttons.
-///
-/// @ingroup Input
-struct KeyboardButton
-{
-	enum Enum
-	{
-		NONE      = 0x00,
-
-		TAB       = 0x09,
-		ENTER     = 0x0d,
-		ESCAPE    = 0x1b,
-		SPACE     = 0x20,
-		BACKSPACE = 0x7f,
-
-		/* KeyPad */
-		KP_0      = 0x80,
-		KP_1      = 0x81,
-		KP_2      = 0x82,
-		KP_3      = 0x83,
-		KP_4      = 0x84,
-		KP_5      = 0x85,
-		KP_6      = 0x86,
-		KP_7      = 0x87,
-		KP_8      = 0x88,
-		KP_9      = 0x89,
-
-		/* Function keys */
-		F1        = 0x90,
-		F2        = 0x91,
-		F3        = 0x92,
-		F4        = 0x93,
-		F5        = 0x94,
-		F6        = 0x95,
-		F7        = 0x96,
-		F8        = 0x97,
-		F9        = 0x98,
-		F10       = 0x99,
-		F11       = 0x9a,
-		F12       = 0x9b,
-
-		/* Other keys */
-		HOME      = 0xa0,
-		LEFT      = 0xa1,
-		UP        = 0xa2,
-		RIGHT     = 0xa3,
-		DOWN      = 0xa4,
-		PAGE_UP   = 0xa5,
-		PAGE_DOWN = 0xa6,
-
-		/* Modifier keys */
-		LCONTROL  = 0xb0,
-		RCONTROL  = 0xb1,
-		LSHIFT    = 0xb2,
-		RSHIFT    = 0xb3,
-		CAPS_LOCK = 0xb4,
-		LALT      = 0xb5,
-		RALT      = 0xb6,
-		LSUPER    = 0xb7,
-		RSUPER    = 0xb8,
-
-		/* [0x30, 0x39] reserved for ASCII digits */
-		NUM_0     = 0x30,
-		NUM_1     = 0x31,
-		NUM_2     = 0x32,
-		NUM_3     = 0x33,
-		NUM_4     = 0x34,
-		NUM_5     = 0x35,
-		NUM_6     = 0x36,
-		NUM_7     = 0x37,
-		NUM_8     = 0x38,
-		NUM_9     = 0x39,
-
-		/* [0x41, 0x5a] reserved for ASCII alphabet */
-		A         = 0x41,
-		B         = 0x42,
-		C         = 0x43,
-		D         = 0x44,
-		E         = 0x45,
-		F         = 0x46,
-		G         = 0x47,
-		H         = 0x48,
-		I         = 0x49,
-		J         = 0x4a,
-		K         = 0x4b,
-		L         = 0x4c,
-		M         = 0x4d,
-		N         = 0x4e,
-		O         = 0x4f,
-		P         = 0x50,
-		Q         = 0x51,
-		R         = 0x52,
-		S         = 0x53,
-		T         = 0x54,
-		U         = 0x55,
-		V         = 0x56,
-		W         = 0x57,
-		X         = 0x58,
-		Y         = 0x59,
-		Z         = 0x5a,
-
-		/* [0x61, 0x7a] reserved for ASCII alphabet */
-
-		// The last key _must_ be <= 0xff
-		COUNT     = 0xff
-	};
-};
-
-} // namespace crown

+ 0 - 69
src/input/keyboard.h

@@ -1,69 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#pragma once
-
-#include "types.h"
-#include "key_code.h"
-#include <cstring> // mem*
-
-namespace crown
-{
-
-/// Interface for accessing keyboard input device.
-///
-/// @ingroup Input
-struct Keyboard
-{
-	Keyboard()
-		: _last_button(KeyboardButton::NONE)
-	{
-		memset(_last_state, 0, KeyboardButton::COUNT);
-		memset(_current_state, 0, KeyboardButton::COUNT);
-	}
-
-	/// Returns whether the specified @a b button is pressed in the current frame.
-	bool button_pressed(KeyboardButton::Enum b) const
-	{
-		return (~_last_state[b] & _current_state[b]) != 0;
-	}
-
-	/// Returns whether the specified @a b button is released in the current frame.
-	bool button_released(KeyboardButton::Enum b) const
-	{
-		return (_last_state[b] & ~_current_state[b]) != 0;
-	}
-
-	/// Returns wheter any button is pressed in the current frame.
-	bool any_pressed()
-	{
-		return button_pressed(_last_button);
-	}
-
-	/// Returns whether any button is released in the current frame.
-	bool any_released()
-	{
-		return button_released(_last_button);
-	}
-
-	void set_button_state(KeyboardButton::Enum b, bool state)
-	{
-		_last_button = b;
-		_current_state[b] = state;
-	}
-
-	void update()
-	{
-		memcpy(_last_state, _current_state, KeyboardButton::COUNT);
-	}
-
-public:
-
-	KeyboardButton::Enum _last_button;
-	uint8_t _last_state[KeyboardButton::COUNT];
-	uint8_t _current_state[KeyboardButton::COUNT];
-};
-
-} // namespace crown

+ 0 - 172
src/input/mouse.h

@@ -1,172 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#pragma once
-
-#include "types.h"
-#include "vector2.h"
-#include <cstring> // mem*
-
-namespace crown
-{
-
-/// Enumerates mouse buttons.
-///
-/// @ingroup Input
-struct MouseButton
-{
-	enum Enum
-	{
-		NONE,
-		LEFT,
-		MIDDLE,
-		RIGHT,
-		COUNT
-	};
-};
-
-/// Interface for accessing mouse input device.
-///
-/// @ingroup Input
-struct Mouse
-{
-	Mouse()
-		: _last_button(MouseButton::NONE)
-		, _wheel(0.0f)
-	{
-		memset(_last_state, 0, MouseButton::COUNT);
-		memset(_current_state, 0, MouseButton::COUNT);
-	}
-
-	/// Returns whether the @a b button is pressed in the current frame.
-	bool button_pressed(MouseButton::Enum b)
-	{
-		return (~_last_state[b] & _current_state[b]) != 0;
-	}
-
-	/// Returns whether the @a b button is released in the current frame.
-	bool button_released(MouseButton::Enum b)
-	{
-		return (_last_state[b] & ~_current_state[b]) != 0;
-	}
-
-	/// Returns wheter any button is pressed in the current frame.
-	bool any_pressed()
-	{
-		return button_pressed(_last_button);
-	}
-
-	/// Returns whether any button is released in the current frame.
-	bool any_released()
-	{
-		return button_released(_last_button);
-	}
-
-	/// 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.
-	Vector2 cursor_xy()
-	{
-		return vector2(_x, _y);
-	}
-
-	/// 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 Vector2& position)
-	{
-		_x = (uint16_t) position.x;
-		_y = (uint16_t) position.y;
-	}
-
-	/// 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 float varying
-	/// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
-	/// maximum extent of the cosidered axis.
-	Vector2 cursor_relative_xy()
-	{
-		return vector2((float) _x / _width, (float) _y / _height);
-	}
-
-	/// 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 float 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 Vector2& position)
-	{
-		set_cursor_xy(vector2(position.x * (float) _width, position.y * (float) _height));
-	}
-
-	/// Returns the mouse wheel state in the current frame.
-	/// A positive or negative value is returned when the wheel is up or down
-	/// respectively, 0.0 otherwise.
-	float wheel()
-	{
-		return _wheel;
-	}
-
-	void set_position(uint16_t x, uint16_t y)
-	{
-		_x = x;
-		_y = y;
-	}
-
-	void set_metrics(uint16_t width, uint16_t height)
-	{
-		_width = width;
-		_height = height;
-	}
-
-	void set_button_state(uint16_t x, uint16_t y, MouseButton::Enum b, bool state)
-	{
-		set_position(x, y);
-		_last_button = b;
-		_current_state[b] = state;
-	}
-
-	void set_wheel(uint16_t x, uint16_t y, float wheel)
-	{
-		set_position(x, y);
-		_wheel = wheel;
-	}
-
-	void update()
-	{
-		_wheel = 0.0f;
-		memcpy(_last_state, _current_state, MouseButton::COUNT);
-	}
-
-public:
-
-	MouseButton::Enum _last_button;
-	float _wheel;
-	uint8_t _last_state[MouseButton::COUNT];
-	uint8_t _current_state[MouseButton::COUNT];
-
-	// Position within the window
-	int16_t _x;
-	int16_t _y;
-
-	// Window size
-	uint16_t _width;
-	uint16_t _height;
-};
-
-} // namespace crown
-

+ 0 - 109
src/input/touch.h

@@ -1,109 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#pragma once
-
-#include "types.h"
-#include "vector2.h"
-#include <cstring> // mem*
-
-namespace crown
-{
-
-/// Maximum number of pointers supported by Touch.
-///
-/// @ingroup Input
-const uint32_t MAX_POINTER_IDS = 4;
-
-/// Interface for accessing touch panel input device.
-///
-/// @ingroup Input
-struct Touch
-{
-	Touch()
-		: _last_pointer(0xff)
-	{
-		memset(_last_state, 0, MAX_POINTER_IDS);
-		memset(_current_state, 0, MAX_POINTER_IDS);
-	}
-
-	/// Returns whether the @a p pointer is pressed in the current frame.
-	bool pointer_down(uint8_t p)
-	{
-		if (p >= MAX_POINTER_IDS) return false;
-		return (~_last_state[p] & _current_state[p]) != 0;
-	}
-
-	/// Returns whether the @a p pointer is released in the current frame.
-	bool pointer_up(uint8_t p)
-	{
-		if (p >= MAX_POINTER_IDS) return false;
-		return (_last_state[p] & ~_current_state[p]) != 0;
-	}
-
-	/// Returns wheter any pointer is pressed in the current frame.
-	bool any_down()
-	{
-		return pointer_down(_last_pointer);
-	}
-
-	/// Returns whether any pointer is released in the current frame.
-	bool any_up()
-	{
-		return pointer_up(_last_pointer);
-	}
-
-	/// Returns the position of the pointer @a p 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.
-	Vector2 pointer_xy(uint8_t p)
-	{
-		if (p >= MAX_POINTER_IDS) return VECTOR2_ZERO;
-		return vector2(_x[p], _y[p]);
-	}
-
-	void set_position(uint8_t p, uint16_t x, uint16_t y)
-	{
-		if (p >= MAX_POINTER_IDS) return;
-		_x[p] = x;
-		_y[p] = y;
-	}
-
-	void set_metrics(uint16_t width, uint16_t height)
-	{
-		_width = width;
-		_height = height;
-	}
-
-	void set_pointer_state(uint16_t x, uint16_t y, uint8_t p, bool state)
-	{
-		set_position(p, x, y);
-
-		_last_pointer = p;
-		_current_state[p] = state;
-	}
-
-	void update()
-	{
-		memcpy(_last_state, _current_state, MAX_POINTER_IDS);
-	}
-
-public:
-
-	uint8_t _last_pointer;
-	uint8_t _last_state[MAX_POINTER_IDS];
-	uint8_t _current_state[MAX_POINTER_IDS];
-
-	uint16_t _x[MAX_POINTER_IDS];
-	uint16_t _y[MAX_POINTER_IDS];
-
-	// Window size
-	uint16_t _width;
-	uint16_t _height;
-};
-
-} // namespace crown

+ 37 - 13
src/lua/lua_keyboard.cpp

@@ -5,47 +5,71 @@
 
 #include "lua_stack.h"
 #include "lua_environment.h"
-#include "input.h"
-#include "keyboard.h"
+#include "device.h"
+#include "input_manager.h"
+#include "input_device.h"
 
 namespace crown
 {
-using namespace input_globals;
 
-static int keyboard_button_pressed(lua_State* L)
+static int keyboard_name(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(keyboard().button_pressed((KeyboardButton::Enum) stack.get_int(1)));
+	stack.push_string(device()->input_manager()->keyboard()->name());
 	return 1;
 }
 
-static int keyboard_button_released(lua_State* L)
+static int keyboard_num_buttons(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(keyboard().button_released((KeyboardButton::Enum) stack.get_int(1)));
+	stack.push_uint32(device()->input_manager()->keyboard()->num_buttons());
+	return 1;
+}
+
+static int keyboard_num_axes(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_uint32(device()->input_manager()->keyboard()->num_axes());
+	return 1;
+}
+
+static int keyboard_pressed(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_bool(device()->input_manager()->keyboard()->pressed((KeyboardButton::Enum) stack.get_int(1)));
+	return 1;
+}
+
+static int keyboard_released(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_bool(device()->input_manager()->keyboard()->released((KeyboardButton::Enum) stack.get_int(1)));
 	return 1;
 }
 
 static int keyboard_any_pressed(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(keyboard().any_pressed());
+	stack.push_bool(device()->input_manager()->keyboard()->any_pressed());
 	return 1;
 }
 
 static int keyboard_any_released(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(keyboard().any_released());
+	stack.push_bool(device()->input_manager()->keyboard()->any_released());
 	return 1;
 }
 
 void load_keyboard(LuaEnvironment& env)
 {
-	env.load_module_function("Keyboard", "button_pressed",   keyboard_button_pressed);
-	env.load_module_function("Keyboard", "button_released",  keyboard_button_released);
-	env.load_module_function("Keyboard", "any_pressed",      keyboard_any_pressed);
-	env.load_module_function("Keyboard", "any_released",     keyboard_any_released);
+	env.load_module_function("Keyboard", "name",         keyboard_name);
+	env.load_module_function("Keyboard", "num_buttons",  keyboard_num_buttons);
+	env.load_module_function("Keyboard", "num_axes",     keyboard_num_axes);
+	env.load_module_function("Keyboard", "pressed",      keyboard_pressed);
+	env.load_module_function("Keyboard", "released",     keyboard_released);
+	env.load_module_function("Keyboard", "any_pressed",  keyboard_any_pressed);
+	env.load_module_function("Keyboard", "any_released", keyboard_any_released);
 
 	env.load_module_enum("Keyboard", "TAB",       KeyboardButton::TAB);
 	env.load_module_enum("Keyboard", "ENTER",     KeyboardButton::ENTER);

+ 28 - 36
src/lua/lua_mouse.cpp

@@ -5,87 +5,79 @@
 
 #include "lua_stack.h"
 #include "lua_environment.h"
-#include "input.h"
-#include "mouse.h"
+#include "device.h"
+#include "input_manager.h"
+#include "input_device.h"
 
 namespace crown
 {
-using namespace input_globals;
 
-static int mouse_button_pressed(lua_State* L)
+static int mouse_name(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(mouse().button_pressed((MouseButton::Enum) stack.get_int(1)));
+	stack.push_string(device()->input_manager()->mouse()->name());
 	return 1;
 }
 
-static int mouse_button_released(lua_State* L)
+static int mouse_num_buttons(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(mouse().button_released((MouseButton::Enum) stack.get_int(1)));
+	stack.push_uint32(device()->input_manager()->mouse()->num_buttons());
 	return 1;
 }
 
-static int mouse_any_pressed(lua_State* L)
+static int mouse_num_axes(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(mouse().any_pressed());
+	stack.push_uint32(device()->input_manager()->mouse()->num_axes());
 	return 1;
 }
 
-static int mouse_any_released(lua_State* L)
+static int mouse_pressed(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(mouse().any_released());
+	stack.push_bool(device()->input_manager()->mouse()->pressed((MouseButton::Enum) stack.get_int(1)));
 	return 1;
 }
 
-static int mouse_cursor_xy(lua_State* L)
+static int mouse_released(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_vector2(mouse().cursor_xy());
+	stack.push_bool(device()->input_manager()->mouse()->released((MouseButton::Enum) stack.get_int(1)));
 	return 1;
 }
 
-static int mouse_set_cursor_xy(lua_State* L)
-{
-	LuaStack stack(L);
-	mouse().set_cursor_xy(stack.get_vector2(1));
-	return 0;
-}
-
-static int mouse_cursor_relative_xy(lua_State* L)
+static int mouse_any_pressed(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_vector2(mouse().cursor_relative_xy());
+	stack.push_bool(device()->input_manager()->mouse()->any_pressed());
 	return 1;
 }
 
-static int mouse_set_cursor_relative_xy(lua_State* L)
+static int mouse_any_released(lua_State* L)
 {
 	LuaStack stack(L);
-	mouse().set_cursor_relative_xy(stack.get_vector2(1));
-	return 0;
+	stack.push_bool(device()->input_manager()->mouse()->any_released());
+	return 1;
 }
 
-static int mouse_wheel(lua_State* L)
+static int mouse_axis(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_float(mouse().wheel());
+	stack.push_vector3(device()->input_manager()->mouse()->axis(stack.get_int(1)));
 	return 1;
 }
 
 void load_mouse(LuaEnvironment& env)
 {
-	env.load_module_function("Mouse", "button_pressed",         mouse_button_pressed);
-	env.load_module_function("Mouse", "button_released",        mouse_button_released);
-	env.load_module_function("Mouse", "any_pressed",            mouse_any_pressed);
-	env.load_module_function("Mouse", "any_released",           mouse_any_released);
-	env.load_module_function("Mouse", "cursor_xy",              mouse_cursor_xy);
-	env.load_module_function("Mouse", "set_cursor_xy",          mouse_set_cursor_xy);
-	env.load_module_function("Mouse", "cursor_relative_xy",     mouse_cursor_relative_xy);
-	env.load_module_function("Mouse", "set_cursor_relative_xy", mouse_set_cursor_relative_xy);
-	env.load_module_function("Mouse", "wheel",                  mouse_wheel);
+	env.load_module_function("Mouse", "name",         mouse_name);
+	env.load_module_function("Mouse", "num_buttons",  mouse_num_buttons);
+	env.load_module_function("Mouse", "num_axes",     mouse_num_axes);
+	env.load_module_function("Mouse", "pressed",      mouse_pressed);
+	env.load_module_function("Mouse", "released",     mouse_released);
+	env.load_module_function("Mouse", "any_pressed",  mouse_any_pressed);
+	env.load_module_function("Mouse", "any_released", mouse_any_released);
+	env.load_module_function("Mouse", "axis",         mouse_axis);
 
 	env.load_module_enum("Mouse", "LEFT",   MouseButton::LEFT);
 	env.load_module_enum("Mouse", "MIDDLE", MouseButton::MIDDLE);

+ 42 - 18
src/lua/lua_touch.cpp

@@ -5,55 +5,79 @@
 
 #include "lua_stack.h"
 #include "lua_environment.h"
-#include "input.h"
-#include "touch.h"
+#include "device.h"
+#include "input_manager.h"
+#include "input_device.h"
 
 namespace crown
 {
-using namespace input_globals;
 
-static int touch_pointer_down(lua_State* L)
+static int touch_name(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(touch().pointer_down((uint8_t) stack.get_int(1)));
+	stack.push_string(device()->input_manager()->touch()->name());
 	return 1;
 }
 
-static int touch_pointer_up(lua_State* L)
+static int touch_num_buttons(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(touch().pointer_up((uint8_t) stack.get_int(1)));
+	stack.push_uint32(device()->input_manager()->touch()->num_buttons());
 	return 1;
 }
 
-static int touch_any_down(lua_State* L)
+static int touch_num_axes(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(touch().any_down());
+	stack.push_uint32(device()->input_manager()->touch()->num_axes());
 	return 1;
 }
 
-static int touch_any_up(lua_State* L)
+static int touch_pressed(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_bool(touch().any_up());
+	stack.push_bool(device()->input_manager()->touch()->pressed(stack.get_int(1)));
 	return 1;
 }
 
-static int touch_pointer_xy(lua_State* L)
+static int touch_released(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.push_vector2(touch().pointer_xy((uint8_t) stack.get_int(1)));
+	stack.push_bool(device()->input_manager()->touch()->released(stack.get_int(1)));
+	return 1;
+}
+
+static int touch_any_pressed(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_bool(device()->input_manager()->touch()->any_pressed());
+	return 1;
+}
+
+static int touch_any_released(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_bool(device()->input_manager()->touch()->any_released());
+	return 1;
+}
+
+static int touch_axis(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_vector3(device()->input_manager()->touch()->axis(stack.get_int(1)));
 	return 1;
 }
 
 void load_touch(LuaEnvironment& env)
 {
-	env.load_module_function("Touch", "pointer_down", touch_pointer_down);
-	env.load_module_function("Touch", "pointer_up",   touch_pointer_up);
-	env.load_module_function("Touch", "any_down",     touch_any_down);
-	env.load_module_function("Touch", "any_up",       touch_any_up);
-	env.load_module_function("Touch", "pointer_xy",   touch_pointer_xy);
+	env.load_module_function("Touch", "name",         touch_name);
+	env.load_module_function("Touch", "num_buttons",  touch_num_buttons);
+	env.load_module_function("Touch", "num_axes",     touch_num_axes);
+	env.load_module_function("Touch", "pressed",      touch_pressed);
+	env.load_module_function("Touch", "released",     touch_released);
+	env.load_module_function("Touch", "any_pressed",  touch_any_pressed);
+	env.load_module_function("Touch", "any_released", touch_any_released);
+	env.load_module_function("Touch", "axis",         touch_axis);
 }
 
 } // namespace crown

+ 27 - 15
src/main/main.cpp

@@ -4,12 +4,12 @@
  */
 
 #include "main.h"
-#include "mouse.h"
-#include "keyboard.h"
-#include "touch.h"
 #include "device.h"
 #include "os_event_queue.h"
-#include "input.h"
+#include "input_manager.h"
+#include "input_device.h"
+#include "error.h"
+#include "vector3.h"
 #include <bgfx.h>
 
 namespace crown
@@ -17,10 +17,9 @@ namespace crown
 
 bool process_events()
 {
-	using namespace input_globals;
-
 	OsEvent event;
 	bool exit = false;
+	InputManager* im = device()->input_manager();
 
 	while(next_event(event))
 	{
@@ -33,9 +32,15 @@ bool process_events()
 				const OsTouchEvent& ev = event.touch;
 				switch (ev.type)
 				{
-					case OsTouchEvent::POINTER: touch().set_pointer_state(ev.x, ev.y, ev.pointer_id, ev.pressed); break;
-					case OsTouchEvent::MOVE: touch().set_position(ev.pointer_id, ev.x, ev.y); break;
-					default: CE_FATAL("Oops, unknown touch event type"); break;
+					case OsTouchEvent::POINTER:
+						im->touch()->set_button_state(ev.pointer_id, ev.pressed);
+						break;
+					case OsTouchEvent::MOVE:
+						im->touch()->set_axis(ev.pointer_id, vector3(ev.x, ev.y, 0.0f));
+						break;
+					default:
+						CE_FATAL("Oops, unknown touch event type");
+						break;
 				}
 				break;
 			}
@@ -44,23 +49,30 @@ bool process_events()
 				const OsMouseEvent& ev = event.mouse;
 				switch (ev.type)
 				{
-					case OsMouseEvent::BUTTON: mouse().set_button_state(ev.x, ev.y, ev.button, ev.pressed); break;
-					case OsMouseEvent::WHEEL: mouse().set_wheel(ev.x, ev.y, ev.wheel); break;
-					case OsMouseEvent::MOVE: mouse().set_position(ev.x, ev.y); break;
-					default: CE_FATAL("Oops, unknown mouse event type"); break;
+					case OsMouseEvent::BUTTON:
+						im->mouse()->set_button_state(ev.button, ev.pressed);
+						break;
+					case OsMouseEvent::MOVE:
+						im->mouse()->set_axis(0, vector3(ev.x, ev.y, 0.0f));
+						break;
+					case OsMouseEvent::WHEEL:
+						im->mouse()->set_axis(1, vector3(ev.wheel, 0.0f, 0.0f));
+						break;
+					default:
+						CE_FATAL("Oops, unknown mouse event type");
+						break;
 				}
 				break;
 			}
 			case OsEvent::KEYBOARD:
 			{
 				const OsKeyboardEvent& ev = event.keyboard;
-				keyboard().set_button_state(ev.button, ev.pressed);
+				im->keyboard()->set_button_state(ev.button, ev.pressed);
 				break;
 			}
 			case OsEvent::METRICS:
 			{
 				const OsMetricsEvent& ev = event.metrics;
-				mouse().set_metrics(ev.width, ev.height);
 				device()->update_resolution(ev.width, ev.height);
 				bgfx::reset(ev.width, ev.height, BGFX_RESET_VSYNC);
 				break;

+ 16 - 11
src/main/main_linux.cpp

@@ -308,15 +308,17 @@ struct LinuxDevice
 				case ClientMessage:
 				{
 					if ((Atom)event.xclient.data.l[0] == _wm_delete_message)
-					{
 						_queue.push_exit_event(0);
-					}
+
 					break;
 				}
 				case ConfigureNotify:
 				{
-					_queue.push_metrics_event(event.xconfigure.x, event.xconfigure.y,
-						event.xconfigure.width, event.xconfigure.height);
+					_queue.push_metrics_event(event.xconfigure.x
+						, event.xconfigure.y
+						, event.xconfigure.width
+						, event.xconfigure.height);
+
 					break;
 				}
 				case ButtonPress:
@@ -324,8 +326,10 @@ struct LinuxDevice
 				{
 					if (event.xbutton.button == Button4 || event.xbutton.button == Button5)
 					{
-						_queue.push_mouse_event(event.xbutton.x, event.xbutton.y,
-							event.xbutton.button == Button4 ? 1.0f : -1.0f);
+						_queue.push_mouse_event(event.xbutton.x
+							, event.xbutton.y
+							, event.xbutton.button == Button4 ? 1.0f : -1.0f);
+
 						break;
 					}
 
@@ -335,13 +339,14 @@ struct LinuxDevice
 						case Button1: mb = MouseButton::LEFT; break;
 						case Button2: mb = MouseButton::MIDDLE; break;
 						case Button3: mb = MouseButton::RIGHT; break;
-						default: mb = MouseButton::NONE; break;
+						default: mb = MouseButton::COUNT; break;
 					}
 
-					if (mb != MouseButton::NONE)
-					{
-						_queue.push_mouse_event(event.xbutton.x, event.xbutton.y, mb, event.type == ButtonPress);
-					}
+					if (mb != MouseButton::COUNT)
+						_queue.push_mouse_event(event.xbutton.x
+							, event.xbutton.y
+							, mb
+							, event.type == ButtonPress);
 
 					break;
 				}