Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | Related Pages

joystick.h

00001 /*
00002   libwftk - Worldforge Toolkit - a widget library
00003   Copyright (C) 2003 Ron Steinke <rsteinke@w-link.net>
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009   
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014   
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the
00017   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018   Boston, MA  02111-1307, SA.
00019 */
00020 
00021 #ifndef _WFTK_JOYSTICK_H
00022 #define _WFTK_JOYSTICK_H
00023 
00024 #include <vector>
00025 #include <sigc++/object.h>
00026 #if SIGC_MAJOR_VERSION == 1 && SIGC_MINOR_VERSION == 0
00027 #include <sigc++/signal_system.h>
00028 #else
00029 #include <sigc++/signal.h>
00030 #endif
00031 
00032 #include <SDL/SDL_joystick.h>
00033 #include <SDL/SDL_events.h>
00034 
00035 #include <wftk/marshal.h>
00036 #include <wftk/point.h>
00037 
00038 namespace wftk {
00039 
00041 class Joystick : virtual public SigC::Object
00042 {
00043  public:
00045   Joystick(unsigned);
00046   ~Joystick();
00047 
00049   static unsigned number() {if(!init_) init(); return joysticks_.size();}
00051   static Joystick* instance(unsigned n = 0);
00052 
00054   const char* name() {return SDL_JoystickName((int) num_);}
00055 
00057   Uint8 numAxes() {return nAxes_;}
00059   Uint8 numBalls() {return nBalls_;}
00061   Uint8 numHats() {return nHats_;}
00063   Uint8 numButtons() {return nButtons_;}
00064 
00066   enum Hat {
00067     CENTERED = SDL_HAT_CENTERED, // == 0
00068     LEFT = SDL_HAT_LEFT,
00069     RIGHT = SDL_HAT_RIGHT,
00070     UP = SDL_HAT_UP,
00071     DOWN = SDL_HAT_DOWN,
00072     RIGHTUP = RIGHT | UP, // == SDL_HAT_RIGHTUP
00073     RIGHTDOWN = RIGHT | DOWN, // == SDL_HAT_RIGHTDOWN
00074     LEFTUP = LEFT | UP, // == SDL_HAT_LEFTUP
00075     LEFTDOWN = LEFT | DOWN // == SDL_HAT_LEFTDOWN
00076   };
00077 
00079   Sint16 getAxis(Uint8 axis) {return (axis < nAxes_) ?
00080     SDL_JoystickGetAxis(stick_, (int) axis) : 0;}
00082   unsigned getHat(Uint8 hat) {return (hat < nHats_) ?
00083     SDL_JoystickGetHat(stick_, (int) hat) : 0;}
00085   Point getBall(Uint8 ball) {Point p; if(ball < nBalls_)
00086     SDL_JoystickGetBall(stick_, (int) ball, &p.x, &p.y); return p;}
00088   bool getButton(Uint8 button) {return (button < nButtons_) ?
00089     (SDL_JoystickGetButton(stick_, (int) button) != 0) : false;}
00090 
00092   SigC::Signal2<bool,Uint8,Sint16,BoolMarshal> axisEvent;
00094   SigC::Signal2<bool,Uint8,unsigned,BoolMarshal> hatEvent;
00096   SigC::Signal2<bool,Uint8,const Point&,BoolMarshal> ballEvent;
00098   SigC::Signal2<bool,Uint8,bool,BoolMarshal> buttonEvent;
00099 
00101   static bool handleEvent(const SDL_Event*);
00102 
00103  private:
00104   Joystick(const Joystick&);
00105   Joystick& operator=(const Joystick&);
00106 
00107   unsigned num_;
00108   SDL_Joystick* stick_;
00109   Uint8 nAxes_, nHats_, nBalls_, nButtons_;
00110 
00111   static void init();
00112   static void shutdown();
00113   static bool init_;
00114   static std::vector<Joystick*> joysticks_;
00115 };
00116 
00117 } // namespace wftk
00118 
00119 #endif // _WFTK_JOYSTICK_H

Generated Tue Aug 9 18:40:26 2005.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.