Explorar o código

Switch to new enum style

Daniele Bartolini %!s(int64=12) %!d(string=hai) anos
pai
achega
01983f8814
Modificáronse 5 ficheiros con 217 adicións e 226 borrados
  1. 0 2
      engine/input/Accelerometer.h
  2. 129 131
      engine/input/KeyCode.h
  3. 30 35
      engine/input/Keyboard.h
  4. 58 56
      engine/input/Mouse.h
  5. 0 2
      engine/input/Touch.h

+ 0 - 2
engine/input/Accelerometer.h

@@ -51,8 +51,6 @@ struct Accelerometer
 public:
 public:
 
 
 	Vec3			m_orientation;
 	Vec3			m_orientation;
-
-	friend class	Device;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 129 - 131
engine/input/KeyCode.h

@@ -29,138 +29,136 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 namespace crown
 {
 {
 
 
-const uint16_t MAX_KEYCODES = 256;
-
-typedef uint8_t Key;
-
-/// A KeyCode is the number which uniquely identifies a key
-/// on the keyboard.
-enum KeyCode
+/// Uniquely identifies a button on the keyboard.
+struct KeyboardButton
 {
 {
-	KC_NOKEY		= 0x00,
-
-	KC_TAB			= 0x09,		// ASCII value
-	KC_ENTER		= 0x0D,		// ASCII value
-	KC_ESCAPE		= 0x1B,		// ASCII value
-	KC_SPACE		= 0x20,		// ASCII value
-	KC_BACKSPACE	= 0x7F,		// ASCII value
-
-	/* KeyPad */
-	KC_KP_0			= 0x80,
-	KC_KP_1			= 0x81,
-	KC_KP_2			= 0x82,
-	KC_KP_3			= 0x83,
-	KC_KP_4			= 0x84,
-	KC_KP_5			= 0x85,
-	KC_KP_6			= 0x86,
-	KC_KP_7			= 0x87,
-	KC_KP_8			= 0x88,
-	KC_KP_9			= 0x89,
-
-	/* Function keys */
-	KC_F1			= 0x90,
-	KC_F2			= 0x91,
-	KC_F3			= 0x92,
-	KC_F4			= 0x93,
-	KC_F5			= 0x94,
-	KC_F6			= 0x95,
-	KC_F7			= 0x96,
-	KC_F8			= 0x97,
-	KC_F9			= 0x98,
-	KC_F10			= 0x99,
-	KC_F11			= 0x9A,
-	KC_F12			= 0x9B,
-
-	/* Other keys */
-	KC_HOME			= 0xA0,
-	KC_LEFT			= 0xA1,
-	KC_UP			= 0xA2,
-	KC_RIGHT		= 0xA3,
-	KC_DOWN			= 0xA4,
-	KC_PAGE_UP		= 0xA5,
-	KC_PAGE_DOWN	= 0xA6,
-
-	/* Modifier keys */
-	KC_LCONTROL		= 0xB0,
-	KC_RCONTROL		= 0xB1,
-	KC_LSHIFT		= 0xB2,
-	KC_RSHIFT		= 0xB3,
-	KC_CAPS_LOCK	= 0xB4,
-	KC_LALT			= 0xB5,
-	KC_RALT			= 0xB6,
-	KC_LSUPER		= 0xB7,
-	KC_RSUPER		= 0xB8,
-
-	/* [0x30, 0x39] reserved for ASCII digits */
-	KC_0			= 0x30,
-	KC_1			= 0x31,
-	KC_2			= 0x32,
-	KC_3			= 0x33,
-	KC_4			= 0x34,
-	KC_5			= 0x35,
-	KC_6			= 0x36,
-	KC_7			= 0x37,
-	KC_8			= 0x38,
-	KC_9			= 0x39,
-
-	/* [0x41, 0x5A] reserved for ASCII alphabet */
-	KC_A			= 0x41,
-	KC_B			= 0x42,
-	KC_C			= 0x43,
-	KC_D			= 0x44,
-	KC_E			= 0x45,
-	KC_F			= 0x46,
-	KC_G			= 0x47,
-	KC_H			= 0x48,
-	KC_I			= 0x49,
-	KC_J			= 0x4A,
-	KC_K			= 0x4B,
-	KC_L			= 0x4C,
-	KC_M			= 0x4D,
-	KC_N			= 0x4E,
-	KC_O			= 0x4F,
-	KC_P			= 0x50,
-	KC_Q			= 0x51,
-	KC_R			= 0x52,
-	KC_S			= 0x53,
-	KC_T			= 0x54,
-	KC_U			= 0x55,
-	KC_V			= 0x56,
-	KC_W			= 0x57,
-	KC_X			= 0x58,
-	KC_Y			= 0x59,
-	KC_Z			= 0x5A,
-
-	/* [0x61, 0x7A] reserved for ASCII alphabet */
-	KC_a			= 0x61,
-	KC_b			= 0x62,
-	KC_c			= 0x63,
-	KC_d			= 0x64,
-	KC_e			= 0x65,
-	KC_f			= 0x66,
-	KC_g			= 0x67,
-	KC_h			= 0x68,
-	KC_i			= 0x69,
-	KC_j			= 0x6A,
-	KC_k			= 0x6B,
-	KC_l			= 0x6C,
-	KC_m			= 0x6D,
-	KC_n			= 0x6E,
-	KC_o			= 0x6F,
-	KC_p			= 0x70,
-	KC_q			= 0x71,
-	KC_r			= 0x72,
-	KC_s			= 0x73,
-	KC_t			= 0x74,
-	KC_u			= 0x75,
-	KC_v			= 0x76,
-	KC_w			= 0x77,
-	KC_x			= 0x78,
-	KC_y			= 0x79,
-	KC_z			= 0x7A,
-
-	// The last key _must_ be <= 0xFF
-	KC_COUNT		= 0xFF
+	enum Enum
+	{
+		NONE		= 0x00,
+
+		TAB			= 0x09,		// ASCII value
+		ENTER		= 0x0D,		// ASCII value
+		ESCAPE		= 0x1B,		// ASCII value
+		SPACE		= 0x20,		// ASCII value
+		BACKSPACE	= 0x7F,		// ASCII value
+
+		/* 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 */
+		a			= 0x61,
+		b			= 0x62,
+		c			= 0x63,
+		d			= 0x64,
+		e			= 0x65,
+		f			= 0x66,
+		g			= 0x67,
+		h			= 0x68,
+		i			= 0x69,
+		j			= 0x6A,
+		k			= 0x6B,
+		l			= 0x6C,
+		m			= 0x6D,
+		n			= 0x6E,
+		o			= 0x6F,
+		p			= 0x70,
+		q			= 0x71,
+		r			= 0x72,
+		s			= 0x73,
+		t			= 0x74,
+		u			= 0x75,
+		v			= 0x76,
+		w			= 0x77,
+		x			= 0x78,
+		y			= 0x79,
+		z			= 0x7A,
+
+		// The last key _must_ be <= 0xFF
+		COUNT		= 0xFF
+	};
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 30 - 35
engine/input/Keyboard.h

@@ -37,23 +37,26 @@ namespace crown
 {
 {
 
 
 /// Enumerates modifier keys.
 /// Enumerates modifier keys.
-enum ModifierKey
+struct ModifierButton
 {
 {
-	MK_SHIFT	= 1,
-	MK_CTRL		= 2,
-	MK_ALT		= 4
+	enum Enum
+	{
+		SHIFT	= 1,
+		CTRL	= 2,
+		ALT		= 4
+	};
 };
 };
 
 
 /// Interface for accessing keyboard input device.
 /// Interface for accessing keyboard input device.
 struct Keyboard
 struct Keyboard
 {
 {
 	Keyboard()
 	Keyboard()
-		: m_modifier(0), m_current_frame(0), m_last_key(KC_NOKEY)
+		: m_modifier(0), m_any_pressed(false)
 	{
 	{
-		for (uint32_t i = 0; i < MAX_KEYCODES; i++)
+		for (uint32_t i = 0; i < KeyboardButton::COUNT; i++)
 		{
 		{
-			m_keys[i] = ~0;
-			m_state[i] = false;
+			m_last_state[i] = false;
+			m_current_state[i] = false;
 		}
 		}
 	}
 	}
 
 
@@ -63,61 +66,53 @@ struct Keyboard
 	/// of another key when the two are pressed in combination. (Thanks wikipedia.)
 	/// of another key when the two are pressed in combination. (Thanks wikipedia.)
 	/// @note
 	/// @note
 	/// Crown currently supports three different modifier keys: Shift, Ctrl and Alt.
 	/// Crown currently supports three different modifier keys: Shift, Ctrl and Alt.
-	bool modifier_pressed(ModifierKey modifier) const
+	bool modifier_pressed(ModifierButton::Enum modifier)
 	{
 	{
-		return (m_modifier & modifier) == modifier;
+		return (m_modifier & (uint8_t) modifier) == modifier;
 	}
 	}
 
 
 	/// Returns whether the specified @a key is pressed in the current frame.
 	/// Returns whether the specified @a key is pressed in the current frame.
-	bool key_pressed(KeyCode key) const
+	bool button_pressed(KeyboardButton::Enum b)
 	{
 	{
-		CE_ASSERT(key >= 0 && key < MAX_KEYCODES, "KeyCode out of range: %d", key);
+		bool pressed = (m_current_state[b] == true && m_last_state[b] == false);
 
 
-		return (m_state[key] == true);
+		return pressed;
 	}
 	}
 
 
 	/// Returns whether the specified @a key is released in the current frame.
 	/// Returns whether the specified @a key is released in the current frame.
-	bool key_released(KeyCode key) const
+	bool button_released(KeyboardButton::Enum b)
 	{
 	{
-		CE_ASSERT(key >= 0 && key < MAX_KEYCODES, "KeyCode out of range: %d", key);
+		bool released = (m_current_state[b] == false && m_last_state[b] == true);
 
 
-		return (m_state[key] == false) && (m_keys[key] == m_current_frame);
+		return released;
 	}
 	}
 
 
 	/// Returns wheter any key is pressed in the current frame.
 	/// Returns wheter any key is pressed in the current frame.
-	bool any_pressed() const
+	bool any_pressed()
 	{
 	{
-		return key_pressed(m_last_key);
+		return m_any_pressed;
 	}
 	}
 
 
 	/// Returns whether any key is released in the current frame.
 	/// Returns whether any key is released in the current frame.
-	bool any_released() const
+	bool any_released()
 	{
 	{
-		return key_pressed(m_last_key);
+		return false;
 	}
 	}
 
 
-	void update(uint64_t frame, KeyCode k, bool state)
+	void set_button_state(KeyboardButton::Enum b, bool state)
 	{
 	{
-		CE_ASSERT(k >= 0 && k < MAX_KEYCODES, "KeyCode out of range: %d", k);
-
-		m_last_key = k;
-		m_keys[k] = frame;
-		m_state[k] = state;
+		m_last_state[b] = m_current_state[b];
+		m_current_state[b] = state;
 	}
 	}
 
 
 public:
 public:
 
 
-	uint8_t			m_modifier;
-
-	// The current frame number
-	uint64_t		m_current_frame;
+	uint8_t m_modifier;
 
 
 	// Last key updated
 	// Last key updated
-	KeyCode			m_last_key;
-	uint64_t		m_keys[MAX_KEYCODES];
-	bool			m_state[MAX_KEYCODES];
-
-	friend class	Device;
+	bool m_any_pressed;
+	bool m_last_state[KeyboardButton::COUNT];
+	bool m_current_state[KeyboardButton::COUNT];
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 58 - 56
engine/input/Mouse.h

@@ -38,11 +38,15 @@ namespace crown
 const uint32_t MAX_MOUSE_BUTTONS = 3;
 const uint32_t MAX_MOUSE_BUTTONS = 3;
 
 
 /// Enumerates mouse buttons.
 /// Enumerates mouse buttons.
-enum MouseButton
+struct MouseButton
 {
 {
-	MB_LEFT		= 0,
-	MB_MIDDLE	= 1,
-	MB_RIGHT	= 2
+	enum Enum
+	{
+		LEFT,
+		MIDDLE,
+		RIGHT,
+		COUNT
+	};
 };
 };
 
 
 /// Interface for accessing mouse input device.
 /// Interface for accessing mouse input device.
@@ -52,41 +56,50 @@ public:
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	Mouse()
 	Mouse()
-		: m_current_frame(0), m_last_button(MB_LEFT)
+		: m_last_button(MouseButton::COUNT)
 	{
 	{
-		m_buttons[MB_LEFT] = ~0;
-		m_buttons[MB_MIDDLE] = ~0;
-		m_buttons[MB_RIGHT] = ~0;
-
-		m_state[MB_LEFT] = false;
-		m_state[MB_MIDDLE] = false;
-		m_state[MB_RIGHT] = false;
+		m_last_state[MouseButton::LEFT] = false;
+		m_last_state[MouseButton::MIDDLE] = false;
+		m_last_state[MouseButton::RIGHT] = false;
+		m_current_state[MouseButton::LEFT] = false;
+		m_current_state[MouseButton::MIDDLE] = false;
+		m_current_state[MouseButton::RIGHT] = false;
 	}
 	}
 
 
 	/// Returns whether @a button is pressed in the current frame.
 	/// Returns whether @a button is pressed in the current frame.
-	bool button_pressed(MouseButton button) const
+	bool button_pressed(MouseButton::Enum button)
 	{
 	{
-		CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
+		bool pressed = (m_current_state[button] == true && m_last_state[button] == false);
 
 
-		return (m_state[button] == true) && (m_buttons[button] == m_current_frame);
+		if (pressed)
+		{
+			m_last_state[button] = m_current_state[button];
+		}
+
+		return pressed;
 	}
 	}
 
 
 	/// Returns whether @a button is released in the current frame.
 	/// Returns whether @a button is released in the current frame.
-	bool button_released(MouseButton button) const
+	bool button_released(MouseButton::Enum button)
 	{
 	{
-		CE_ASSERT(button >= 0 && button < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", button);
+		bool released = (m_current_state[button] == false && m_last_state[button] == true);
+
+		if (released)
+		{
+			m_last_state[button] = m_current_state[button];
+		}
 
 
-		return (m_state[button] == false) && (m_buttons[button] == m_current_frame);
+		return released;
 	}
 	}
 
 
 	/// Returns wheter any button is pressed in the current frame.
 	/// Returns wheter any button is pressed in the current frame.
-	bool any_pressed() const
+	bool any_pressed()
 	{
 	{
 		return button_pressed(m_last_button);
 		return button_pressed(m_last_button);
 	}
 	}
 
 
 	/// Returns whether any button is released in the current frame.
 	/// Returns whether any button is released in the current frame.
-	bool any_released() const
+	bool any_released()
 	{
 	{
 		return button_released(m_last_button);
 		return button_released(m_last_button);
 	}
 	}
@@ -96,13 +109,9 @@ public:
 	/// Coordinates in window space have the origin at the
 	/// Coordinates in window space have the origin at the
 	/// upper-left corner of the window. +X extends from left
 	/// upper-left corner of the window. +X extends from left
 	/// to right and +Y extends from top to bottom.
 	/// to right and +Y extends from top to bottom.
-	Vec2 cursor_xy() const
+	Vec2 cursor_xy()
 	{
 	{
-		int32_t x, y;
-
-		device()->window()->get_cursor_xy(x, y);
-
-		return Vec2(x, y);
+		return Vec2(m_x, m_y);
 	}
 	}
 
 
 	/// Sets the position of the cursor in window space.
 	/// Sets the position of the cursor in window space.
@@ -112,7 +121,8 @@ public:
 	/// to right and +Y extends from top to bottom.
 	/// to right and +Y extends from top to bottom.
 	void set_cursor_xy(const Vec2& position)
 	void set_cursor_xy(const Vec2& position)
 	{
 	{
-		device()->window()->set_cursor_xy((int32_t) position.x, (int32_t) position.y);
+		m_x = (uint16_t) position.x;
+		m_y = (uint16_t) position.y;
 	}
 	}
 
 
 	/// Returns the relative position of the cursor in window space.
 	/// Returns the relative position of the cursor in window space.
@@ -124,19 +134,9 @@ public:
 	/// Relative coordinates are mapped to a float varying
 	/// Relative coordinates are mapped to a float varying
 	/// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
 	/// from 0.0 to 1.0 where 0.0 is the origin and 1.0 the
 	/// maximum extent of the cosidered axis.
 	/// maximum extent of the cosidered axis.
-	Vec2 cursor_relative_xy() const
+	Vec2 cursor_relative_xy()
 	{
 	{
-		uint32_t window_width;
-		uint32_t window_height;
-
-		device()->window()->get_size(window_width, window_height);
-
-		Vec2 pos = cursor_xy();
-
-		pos.x = pos.x / (float) window_width;
-		pos.y = pos.y / (float) window_height;
-
-		return pos;
+		return Vec2(m_x / m_width, m_y / m_height);
 	}
 	}
 
 
 	/// Sets the relative position of the cursor in window space.
 	/// Sets the relative position of the cursor in window space.
@@ -150,36 +150,38 @@ public:
 	/// maximum extent of the cosidered axis.
 	/// maximum extent of the cosidered axis.
 	void set_cursor_relative_xy(const Vec2& position)
 	void set_cursor_relative_xy(const Vec2& position)
 	{
 	{
-		uint32_t window_width;
-		uint32_t window_height;
-
-		device()->window()->get_size(window_width, window_height);
-
-		set_cursor_xy(Vec2(position.x * (float) window_width, position.y * (float) window_height));
+		set_cursor_xy(Vec2(position.x * (float) m_width, position.y * (float) m_height));
 	}
 	}
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
-	void update(uint64_t frame, MouseButton b, bool state)
+	void set_metrics(uint16_t width, uint16_t height)
 	{
 	{
-		CE_ASSERT(b >= 0 && b < MAX_MOUSE_BUTTONS, "MouseButton out of range: %d", b);
+		m_width = width;
+		m_height = height;
+	}
 
 
+	//-----------------------------------------------------------------------------
+	void set_button_state(MouseButton::Enum b, bool state)
+	{
+		m_last_state[b] = m_current_state[b];
+		m_current_state[b] = state;
 		m_last_button = b;
 		m_last_button = b;
-		m_buttons[b] = frame;
-		m_state[b] = state;
 	}
 	}
 
 
 public:
 public:
 
 
-	// The current frame number
-	uint64_t		m_current_frame;
-
 	// Last button updated
 	// Last button updated
-	MouseButton		m_last_button;
+	MouseButton::Enum m_last_button;
+	bool m_last_state[MouseButton::COUNT];
+	bool m_current_state[MouseButton::COUNT];
 
 
-	uint64_t		m_buttons[MAX_MOUSE_BUTTONS];
-	bool			m_state[MAX_MOUSE_BUTTONS];
+	// Position within the window
+	uint16_t m_x;
+	uint16_t m_y;
 
 
-	friend class	Device;
+	// Window size
+	uint16_t m_width;
+	uint16_t m_height;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 0 - 2
engine/input/Touch.h

@@ -89,8 +89,6 @@ struct Touch
 public:
 public:
 
 
 	PointerData		m_pointers[MAX_POINTER_IDS];
 	PointerData		m_pointers[MAX_POINTER_IDS];
-
-	friend class	Device;
 };
 };
 
 
 }
 }