/home/jpf/code/cc/project/cmil/include/InputData.h

Go to the documentation of this file.
00001 // =====================================================================================
00002 //       Filename:  InputData.h 
00003 //    Description:  
00004 //        Created:  02/15/2007 08:47:46 PM EST
00005 //       Revision:  none
00006 //       Compiler:  gcc 3.4.6
00007 // 
00008 //         Author:  John P. Feltz 
00009 //          Email:  jfeltz@gmail.com
00010 //        License:  Copyright (c) 2006-2007, John P. Feltz 
00011 //                  This program is free software; you can redistribute it and/or
00012 //                  modify it under the terms of the GNU General Public License as
00013 //                  published by the Free Software Foundation, version 2 of the 
00014 //                  License.
00015 //                  This program is distributed in the hope that it will be
00016 //                  useful, but WITHOUT ANY WARRANTY; without even the implied
00017 //                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00018 //                  PURPOSE.
00019 //                  See the GNU General Public License version 2 for more details.
00020 // 
00021 // =====================================================================================
00022 
00023 namespace cmil {
00024   class Ev;
00025   class InputDevice;
00026   class InputIdentifiers;
00027   class InputRelay;
00028 
00029   const bool pressed = true;
00030   const bool released = false;
00031 
00032 
00033   namespace SDL {
00034     class Relay;  
00035   }
00036   
00039   class InputData : public Logger {
00040     public:
00041       InputData(const std::string& name) : m_state(true), m_name(name), m_id(0) {}; 
00042 
00043       virtual ~InputData() {}
00044 
00045       bool operator==(const InputData& other) {
00046         if(m_name == other.m_name) {
00047           return true;
00048         }
00049         else  { return false; }
00050       } 
00051 
00052 
00053       std::string getName() const;
00054       bool getState() const { return m_state; }
00055       unsigned long getTime() const { return m_time; };
00056       unsigned int getID() const { return m_id; }
00057 
00058       virtual Ev createEv() const;
00059 
00060       virtual InputData* clone() = 0;
00061       virtual bool isActionCompatible(InputAction* const action) const = 0 ;
00062       virtual void run(InputAction* const action) const = 0;
00063       
00064     protected:  
00065       friend class DeviceRegistry;
00066       friend class DeviceInputs;
00067       
00071 
00072       bool m_state;
00073 
00074       void setDevice(const std::string& device) { m_device = device; }
00075 
00076       void setName(const std::string& name) { 
00077         m_name = name; 
00078       }
00079 
00080       void setWhichDevice(int num_device) { m_num_device = num_device; }
00081 
00082       void setID(const int id) { m_id = id; }
00083 
00084     private:
00085       //FIXME this is wrong, InputData should have no notion of any kind of relay.
00086       friend class SDL::Relay;
00087       void setTime(unsigned long time) { m_time = time; };
00088 
00090       std::string m_name;
00091       unsigned long m_time;
00092       
00094       unsigned int m_id;
00095 
00097       std::string m_device; 
00098       unsigned int m_num_device;  
00099     
00100       InputData& operator=(const InputData& );
00101   };
00102 
00104   template <typename ActionType> class tInputData : public InputData {
00105     public:
00106       tInputData(const std::string& name) : InputData(name) {} 
00107 
00108       bool isActionCompatible(InputAction* const action) const {
00109         log_debug( std::string("template isActionCompatible() typeid(*action).name() : ")
00110             .append(typeid(*action).name()));
00111 
00112         if(action) {
00113             log_debug( "template isActionCompatible() running cast test" );
00114           if(dynamic_cast<ActionType*>(action) != 0) {
00115             log_debug( "template isActionCompatible() finished cast test" );
00116             return true;
00117           }
00118         }
00119 
00120         log_debug( "template isActionCompatible() returning false" );
00121         return false;
00122       }
00123 
00124       virtual void run(InputAction* const action) const {
00125         ActionType* tmp_action_type;
00126 
00127         if(action) {
00128           if((tmp_action_type = dynamic_cast<ActionType*> (action)) != 0) {
00129             do_run(tmp_action_type);
00130           }
00131           else 
00132             log_debug("run()  unable to run action due to incompatible action type. " );
00133         }
00134         else  
00135           log_error("run() called on a null action");
00136       }
00137 
00138     private:
00139       virtual void do_run(ActionType* action) const = 0;
00140   };
00141 
00142   class ModAction : public virtual InputAction {
00143     public:
00144       ModAction(const std::string& name) : InputAction(name) {}
00145 
00146       void Movement(bool movement) {
00147         if (movement == pressed) {
00148           doPress();
00149         }
00150         else  {
00151           doRelease();
00152         }
00153       }
00154 
00155     virtual void doPress() = 0; 
00156     virtual void doRelease() {}; 
00157   };
00158 
00161   class ModData : public tInputData<ModAction> {
00162     public:
00163       ModData(const std::string& ident) : tInputData<ModAction>(ident) {};
00164 
00165       virtual InputData* clone() = 0;
00166 
00167       virtual void do_run(ModAction* action) const = 0; 
00168 
00169       void setState(const bool state) { m_state = state; }
00170   };
00171 
00172   class Press : public ModData {
00173     public:
00174       Press(const std::string& ident) : ModData(ident) {};
00175 
00176       virtual InputData* clone() {
00177         return new Press(*this);
00178       };
00179 
00180       void do_run(ModAction* action) const {
00181         action->Movement(m_state);
00182       }
00183   };
00184 
00185   class AxisAction : public virtual InputAction {
00186     public:
00187       AxisAction(const std::string& name) : InputAction(name) {}
00188       virtual void runAxis(signed int axis) = 0; 
00189   };
00190 
00192   class Axis : public tInputData<AxisAction> {
00193     public:
00194       Axis(const std::string& ident) : tInputData<AxisAction>(ident), m_axis(0) {}
00195 
00196       virtual InputData* clone() {
00197         return new Axis(*this);
00198       };
00199 
00200       virtual void setAxis(signed int axis) {
00201         m_axis = axis;
00202       }
00203         
00204       void do_run(AxisAction* action) const {
00205         action->runAxis(m_axis);
00206       }
00207       
00208     private:
00209       signed int m_axis;
00210   };
00211 
00212   class MotionAction : public virtual InputAction {
00213     public:
00214       MotionAction(const std::string& name) : InputAction(name) {}
00215       virtual void runMotion(unsigned int xrel, unsigned long int yrel) = 0; 
00216   };
00217   
00219   class Motion : public tInputData<MotionAction> {
00220     public:
00221       Motion(const std::string& ident) : tInputData<MotionAction>(ident), m_xrel(0), m_yrel(0) {}
00222 
00223       virtual InputData* clone() {
00224         return new Motion(*this);
00225       };
00226 
00227 
00228       std::string getName() const {
00229         return "Motion";
00230       }; 
00231 
00232       void do_run(MotionAction* action) const {
00233         action->runMotion(m_xrel, m_yrel);
00234       }
00235 
00236       void setMotion(int xrel, int yrel) {
00237         if( ((xrel * yrel) <= 1) )  {
00238           m_xrel = xrel;
00239           m_yrel = yrel;
00240         }
00241         else 
00242           log_error("setMotion() unable to to set motion due to relative bounds exceeded");
00243       }
00244         
00245     private:
00246       unsigned int m_xrel; 
00247       unsigned int m_yrel; 
00248   };
00249 
00250   class CoordAction : public virtual InputAction {
00251     public:
00252       CoordAction(const std::string& name) : InputAction(name) {}
00253       virtual void runCoord(unsigned long int x, unsigned long int y, int xrel, int yrel) = 0; 
00254   };
00255   
00258   class Coord : public tInputData<CoordAction> {
00259     public:
00260       Coord(const std::string& ident) : tInputData<CoordAction>(ident), m_x(0), m_y(0), m_xrel(0), m_yrel(0) {}
00261 
00262       virtual InputData* clone() {
00263         return new Coord(*this);
00264       };
00265 
00266 
00267       virtual void setCoord(int x, int y, int xrel, int yrel) {
00268         m_x=x;
00269         m_y=y;
00270         m_xrel=xrel;
00271         m_yrel=yrel;
00272       }; 
00273 
00274       void do_run(CoordAction* action) const {
00275         action->runCoord(m_x, m_y, m_xrel, m_yrel);
00276       };
00277 
00278     private:
00279       //These values store the screen position.
00280       int m_x,m_y;
00281       
00282       //These values store relative movement. 
00283       int m_xrel,m_yrel;
00284   };
00285 
00286   class HatAction : public virtual InputAction {
00287     public:
00288       enum Position {
00289         Centered, 
00290         Up,
00291         Right,
00292         Down,
00293         Left,
00294         RightUp,  
00295         RightDown,
00296         LeftUp, 
00297         LeftDown
00298       };
00299 
00300       HatAction(const std::string& name) : InputAction(name) {}
00301       virtual void Movement(Position pos) = 0; 
00302   };
00303 
00306   class Hat : public tInputData<HatAction> {
00307     public:
00308       Hat(const std::string& ident) : tInputData<HatAction>(ident) {}
00309 
00310       virtual InputData* clone() {
00311         return new Hat(*this);
00312       };
00313 
00314       virtual void setPosition(HatAction::Position pos) {
00315         m_pos = pos;
00316       }; 
00317       const HatAction::Position getPosition() const {
00318         return m_pos; 
00319       }; 
00320 
00321       void do_run(HatAction* action) const {
00322         action->Movement(m_pos);
00323       };
00324     private:
00325       HatAction::Position m_pos;
00326   };
00327 }

(c) 2006-2007 John P. Feltz
Generated on Wed Jul 25 16:08:02 2007 for Common Media Input Layer by doxygen 1.4.7

SourceForge.net Logo