Permalink
Browse files

bear::input handles touch events.

There is a "finger" controller which dispatches instances of finger_event.

bear::mouse_detector process the touch event as the mouse events. This
behavior can be disabled.
  • Loading branch information...
Julien Jorge
Julien Jorge committed Sep 1, 2013
1 parent d5467a8 commit 487680fa40fe7791a1078109eca718a1a352e1bf
@@ -3,6 +3,9 @@ set( INPUT_TARGET_NAME bear_input )
#-------------------------------------------------------------------------------
set( INPUT_SOURCE_FILES
code/controller_button.cpp
+ code/finger.cpp
+ code/finger_event.cpp
+ code/finger_status.cpp
code/input_listener.cpp
code/input_status.cpp
code/joystick.cpp
@@ -0,0 +1,107 @@
+/*
+ Copyright (C) 2012 Stuffomatic Ltd. <contact@stuff-o-matic.com>
+
+ All rights reserved.
+
+ See the accompanying license file for details about usage, modification and
+ distribution of this file.
+*/
+/**
+ * \file
+ * \brief Implementation of the bear::input::finger class.
+ * \author Julien Jorge
+ */
+#include "input/finger.hpp"
+
+#include <SDL/SDL.h>
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Returns the last events.
+ */
+const bear::input::finger::event_list& bear::input::finger::get_events() const
+{
+ return m_events;
+} // finger::get_events()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Re-read the status of all keys.
+ * \pre The caller is an instance of bear::input::system.
+ */
+void bear::input::finger::refresh()
+{
+ m_events.clear();
+
+ SDL_Event e;
+
+ // The range of events to process. It includes button up and button down.
+ const SDL_EventType event_min( SDL_FINGERDOWN );
+ const SDL_EventType event_max( SDL_FINGERMOTION );
+
+ while ( SDL_PeepEvents(&e, 1, SDL_GETEVENT, event_min, event_max ) == 1 )
+ {
+ const SDL_TouchFingerEvent* const evt =
+ reinterpret_cast<SDL_TouchFingerEvent*>(&e);
+ const position_type position( convert_position( evt->x, evt->y ) );
+
+ if ( e.type == SDL_FINGERDOWN )
+ m_events.push_back
+ ( finger_event::create_pressed_event( position ) );
+ else if ( e.type == SDL_FINGERUP )
+ m_events.push_back
+ ( finger_event::create_released_event( position ) );
+ else if ( e.type == SDL_FINGERMOTION )
+ m_events.push_back
+ ( finger_event::create_motion_event
+ ( position, convert_delta( evt->dx, evt->dy ) ) );
+ }
+} // finger::refresh()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Converts SDL's finger position into the coordinates of the engine.
+ */
+bear::input::position_type
+bear::input::finger::convert_position( double x, double y ) const
+{
+ SDL_Window* const window( SDL_GetMouseFocus() );
+
+ position_type result(0, 0);
+
+ if ( window != NULL )
+ {
+ int w;
+ int h;
+ SDL_GetWindowSize( window, &w, &h );
+
+ result.x = x * w;
+ result.y = (1 - y) * h;
+ }
+
+ return result;
+} // finger::convert_position()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Converts SDL's finger movement into the coordinates of the engine.
+ */
+bear::input::position_type
+bear::input::finger::convert_delta( double x, double y ) const
+{
+ SDL_Window* const window( SDL_GetMouseFocus() );
+
+ position_type result(0, 0);
+
+ if ( window != NULL )
+ {
+ int w;
+ int h;
+ SDL_GetWindowSize( window, &w, &h );
+
+ result.x = x * w;
+ result.y = - y * h;
+ }
+
+ return result;
+} // finger::convert_delta()
@@ -0,0 +1,100 @@
+/*
+ Copyright (C) 2012 Stuffomatic Ltd. <contact@stuff-o-matic.com>
+
+ All rights reserved.
+
+ See the accompanying license file for details about usage, modification and
+ distribution of this file.
+*/
+/**
+ * \file
+ * \brief Implementation of the input::finger_event class.
+ * \author Julien Jorge
+ */
+#include "input/finger_event.hpp"
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Creates a pressed event.
+ * \param p The position where the finger has been pressed.
+ */
+bear::input::finger_event
+bear::input::finger_event::create_pressed_event( const position_type& p )
+{
+ finger_event e( finger_event_pressed );
+ e.m_position = p;
+
+ return e;
+} // finger_event::create_pressed_event()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Creates a released event.
+ * \param p The position where the finger has been released.
+ */
+bear::input::finger_event
+bear::input::finger_event::create_released_event( const position_type& p )
+{
+ finger_event e( finger_event_released );
+ e.m_position = p;
+
+ return e;
+} // finger_event::create_released_event()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Creates a motion event.
+ * \param p The position where the event occurred.
+ * \param d The distance of the motion.
+ */
+bear::input::finger_event
+bear::input::finger_event::create_motion_event
+( const position_type& p, const position_type& d )
+{
+ finger_event e( finger_event_motion );
+ e.m_position = p;
+ e.m_distance = d;
+
+ return e;
+} // finger_event::create_motion_event()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Returns the type of the event.
+ */
+bear::input::finger_event::event_type
+bear::input::finger_event::get_type() const
+{
+ return m_type;
+} // finger_event::get_type()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Returns the position where the event occurred.
+ */
+const bear::input::position_type&
+bear::input::finger_event::get_position() const
+{
+ return m_position;
+} // finger_event::get_position()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Returns the distance of the motion.
+ */
+const bear::input::position_type&
+bear::input::finger_event::get_distance() const
+{
+ return m_distance;
+} // finger_event::get_distance()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Constructs an event of an unknown type.
+ * \param t The type of the event.
+ */
+bear::input::finger_event::finger_event( event_type t )
+ : m_type(t), m_position(0, 0), m_distance(0, 0)
+{
+
+} // finger_event::finger_event()
@@ -0,0 +1,40 @@
+/*
+ Copyright (C) 2012 Stuffomatic Ltd. <contact@stuff-o-matic.com>
+
+ All rights reserved.
+
+ See the accompanying license file for details about usage, modification and
+ distribution of this file.
+*/
+/**
+ * \file
+ * \brief Implementation of the bear::input::finger_status class.
+ * \author Julien Jorge
+ */
+#include "input/finger_status.hpp"
+
+#include "input/input_listener.hpp"
+#include "input/system.hpp"
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Read the status of the finger.
+ */
+void bear::input::finger_status::read()
+{
+ const finger& f = system::get_instance().get_finger();
+
+ m_events = f.get_events();
+} // finger_status::read()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Send the status of the finger to an input_listener.
+ * \param listener The listener to pass informations to.
+ */
+void bear::input::finger_status::scan_inputs( input_listener& listener ) const
+{
+ for ( event_list::const_iterator it = m_events.begin(); it != m_events.end();
+ ++it )
+ listener.finger_action( *it );
+} // finger_status::scan_inputs()
@@ -164,3 +164,14 @@ bool bear::input::input_listener::mouse_move
{
return false;
} // input_listener::mouse_move()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Method called a finger related event occurs.
+ * \param event The event.
+ * \return true if the event has been processed.
+ */
+bool bear::input::input_listener::finger_action( const finger_event& event )
+{
+ return false;
+} // input_listener::finger_action()
@@ -22,6 +22,7 @@ void bear::input::input_status::read()
m_keyboard.read();
m_joystick.read();
m_mouse.read();
+ m_finger.read();
} // input_status::read()
/*----------------------------------------------------------------------------*/
@@ -34,4 +35,5 @@ void bear::input::input_status::scan_inputs( input_listener& listener ) const
m_keyboard.scan_inputs( listener );
m_joystick.scan_inputs( listener );
m_mouse.scan_inputs( listener );
+ m_finger.scan_inputs( listener );
} // input_status::scan_inputs()
@@ -249,7 +249,7 @@ bear::input::keyboard::const_iterator bear::input::keyboard::end() const
/*----------------------------------------------------------------------------*/
/**
- * \brief Get the last events.
+ * \brief Returns the last events.
*/
const bear::input::keyboard::event_list&
bear::input::keyboard::get_events() const
@@ -13,6 +13,7 @@
*/
#include "input/system.hpp"
+#include "input/finger.hpp"
#include "input/joystick.hpp"
#include "input/keyboard.hpp"
#include "input/mouse.hpp"
@@ -37,6 +38,9 @@ void bear::input::system::initialize()
SDL_EventState( SDL_KEYDOWN, SDL_ENABLE );
SDL_EventState( SDL_MOUSEWHEEL, SDL_ENABLE );
+ SDL_EventState( SDL_FINGERDOWN, SDL_ENABLE );
+ SDL_EventState( SDL_FINGERUP, SDL_ENABLE );
+ SDL_EventState( SDL_FINGERMOTION, SDL_ENABLE );
// force the creation of the instance
get_instance().refresh();
@@ -77,7 +81,16 @@ void bear::input::system::refresh()
/*----------------------------------------------------------------------------*/
/**
- * \brief Create an instance of the bear::input::keyboard class.
+ * \brief Returns the instance of the bear::input::finger class.
+ */
+bear::input::finger& bear::input::system::get_finger()
+{
+ return *m_finger;
+} // system::get_finger()
+
+/*----------------------------------------------------------------------------*/
+/**
+ * \brief Returns the instance of the bear::input::keyboard class.
*/
bear::input::keyboard& bear::input::system::get_keyboard()
{
@@ -86,7 +99,7 @@ bear::input::keyboard& bear::input::system::get_keyboard()
/*----------------------------------------------------------------------------*/
/**
- * \brief Create an instance of the bear::input::mouse class.
+ * \brief Returns the instance of the bear::input::mouse class.
*/
bear::input::mouse& bear::input::system::get_mouse()
{
@@ -95,7 +108,8 @@ bear::input::mouse& bear::input::system::get_mouse()
/*----------------------------------------------------------------------------*/
/**
- * \brief Create an instance of the bear::input::joystick class.
+ * \brief Returns the instance of the bear::input::joystick class for a given
+ * joystick.
* \param joy_id Joystick identifier.
* \pre joy_id < bear::input::joystick::number_of_joysticks()
*/
@@ -117,6 +131,8 @@ bear::input::system::system()
for (unsigned int i=0; i!=joystick::number_of_joysticks(); ++i)
m_joystick.push_back( new joystick(i) );
+
+ m_finger = new finger;
} // system::system()
/*----------------------------------------------------------------------------*/
@@ -139,6 +155,8 @@ void bear::input::system::refresh_alone()
for (unsigned int i=0; i!=m_joystick.size(); ++i)
m_joystick[i]->refresh();
+
+ m_finger->refresh();
} // system::refresh_alone()
/*----------------------------------------------------------------------------*/
@@ -147,20 +165,17 @@ void bear::input::system::refresh_alone()
*/
void bear::input::system::clear()
{
- if (m_keyboard != NULL)
- {
- delete m_keyboard;
- m_keyboard = NULL;
- }
-
- if (m_mouse != NULL)
- {
- delete m_mouse;
- m_mouse = NULL;
- }
+ delete m_keyboard;
+ m_keyboard = NULL;
+
+ delete m_mouse;
+ m_mouse = NULL;
for (unsigned int i=0; i!=m_joystick.size(); ++i)
delete m_joystick[i];
m_joystick.clear();
+
+ delete m_finger;
+ m_finger = NULL;
} // system::clear()
Oops, something went wrong.

0 comments on commit 487680f

Please sign in to comment.