mouse.H

00001 /*
00002  * This file is part of the "Archon" framework.
00003  * (http://files3d.sourceforge.net)
00004  *
00005  * Copyright © 2002 by Kristian Spangsege and Brian Kristiansen.
00006  *
00007  * Permission to use, copy, modify, and distribute this software and
00008  * its documentation under the terms of the GNU General Public License is
00009  * hereby granted. No representations are made about the suitability of
00010  * this software for any purpose. It is provided "as is" without express
00011  * or implied warranty. See the GNU General Public License
00012  * (http://www.gnu.org/copyleft/gpl.html) for more details.
00013  *
00014  * The characters in this file are ISO8859-1 encoded.
00015  *
00016  * The documentation in this file is in "Doxygen" style
00017  * (http://www.doxygen.org).
00018  */
00019 
00020 #ifndef ARCHON_UTILITIES_MOUSE_H
00021 #define ARCHON_UTILITIES_MOUSE_H
00022 
00023 #include <string>
00024 
00025 #include <archon/util/window.H>
00026 
00027 #include <archon/util/exception.H>
00028 
00029 namespace Archon
00030 {
00031   namespace Utilities
00032   {
00036     namespace MouseEvents
00037     {
00038       using namespace std;
00039 
00040       struct ButtonHandler
00041       {
00042         virtual void press(unsigned x, unsigned y) = 0;
00043         virtual void release(unsigned x, unsigned y) = 0;
00044         virtual ~ButtonHandler() {}
00045       };
00046 
00047       struct ButtonFunc: ButtonHandler
00048       {
00053         ButtonFunc(void (*pressFunc)(unsigned x, unsigned y),
00054                    void (*releaseFunc)(unsigned x, unsigned y)):
00055           p(pressFunc), r(releaseFunc) {}
00056 
00057       private:
00058         void (*p)(unsigned x, unsigned y);
00059         void (*r)(unsigned x, unsigned y);
00060         virtual void press(unsigned x, unsigned y)   { if(p) (*p)(x, y); }
00061         virtual void release(unsigned x, unsigned y) { if(r) (*r)(x, y); }
00062       };
00063 
00064       template<class T>
00065       struct ButtonFuncCookie: ButtonHandler
00066       {
00071         ButtonFuncCookie(void (*pressFunc)(unsigned x, unsigned y, T),
00072                          void (*releaseFunc)(unsigned x, unsigned y, T),
00073                          T cookie):
00074           p(pressFunc), r(releaseFunc), cookie(cookie) {}
00075 
00076       private:
00077         void (*p)(unsigned x, unsigned y, T);
00078         void (*r)(unsigned x, unsigned y, T);
00079         T cookie;
00080         virtual void press(unsigned x, unsigned y)   { if(p) (*p)(x, y, cookie); }
00081         virtual void release(unsigned x, unsigned y) { if(r) (*r)(x, y, cookie); }
00082       };
00083 
00084       template<class T>
00085       struct ButtonMethod: ButtonHandler
00086       {
00087         ButtonMethod(T *object, void (T::*method)(unsigned x, unsigned y, bool press)):
00088           o(object), m(method) {}
00089 
00090       private:
00091         T *o;
00092         void (T::*m)(unsigned x, unsigned y, bool press);
00093         virtual void press(unsigned x, unsigned y)   { (o->*m)(x, y, true);  }
00094         virtual void release(unsigned x, unsigned y) { (o->*m)(x, y, false); }
00095       };
00096 
00101       template<class T>
00102       struct ButtonVarAssign: ButtonHandler
00103       {
00104         ButtonVarAssign(T &variable, T pressValue, T releaseValue):
00105           var(variable), pVal(pressValue), rVal(releaseValue), rEnable(true) {}
00106 
00111         ButtonVarAssign(T &variable, T pressValue):
00112           var(variable), pVal(pressValue), rEnable(false) {}
00113 
00114       private:
00115         T &var;
00116         const T pVal;
00117         const T rVal;
00118         const bool rEnable;
00119         virtual void press(unsigned, unsigned)   { var = pVal; }
00120         virtual void release(unsigned, unsigned) { if(rEnable) var = rVal; }
00121       };
00122 
00127       template<class T>
00128       struct ButtonVarAdd: ButtonHandler
00129       {
00130         ButtonVarAdd(T &variable, T value, bool ignoreRelease = false):
00131           var(variable), val(value), rEnable(!ignoreRelease) {}
00132 
00133       private:
00134         T &var;
00135         const T val;
00136         virtual void press(unsigned, unsigned)   { var += val; }
00137         virtual void release(unsigned, unsigned) { if(rEnable) var -= val; }
00138         const bool rEnable;
00139       };
00140 
00141       struct MotionHandler
00142       {
00143         virtual void move(unsigned xAbs, unsigned yAbs,
00144                           int xRel, int yRel) = 0;
00145         virtual ~MotionHandler() {}
00146       };
00147 
00148       struct MotionFunc: MotionHandler
00149       {
00150         MotionFunc(void (*moveFunc)(unsigned xAbs, unsigned yAbs,
00151                                     int xRel, int yRel)):
00152           m(moveFunc) {}
00153 
00154       private:
00155         void (*m)(unsigned xAbs, unsigned yAbs, int xRel, int yRel);
00156         virtual void move(unsigned xAbs, unsigned yAbs, int xRel, int yRel)
00157         {
00158           (*m)(xAbs, yAbs, xRel, yRel);
00159         }
00160       };
00161 
00162       template<class T>
00163       struct MotionFuncCookie: MotionHandler
00164       {
00165         MotionFuncCookie(void (*moveFunc)(unsigned xAbs, unsigned yAbs,
00166                                           int xRel, int yRel, T), T cookie):
00167           m(moveFunc), cookie(cookie) {}
00168 
00169       private:
00170         void (*m)(unsigned xAbs, unsigned yAbs, int xRel, int yRel, T);
00171         T cookie;
00172         virtual void move(unsigned xAbs, unsigned yAbs, int xRel, int yRel)
00173         {
00174           (*m)(xAbs, yAbs, xRel, yRel, cookie);
00175         }
00176       };
00177 
00178       template<class T> struct MotionMethod: MotionHandler
00179       {
00180         MotionMethod(T *object,
00181                      void (T::*moveMethod)(unsigned xAbs, unsigned yAbs,
00182                                            int xRel, int yRel)):
00183           o(object), m(moveMethod) {}
00184 
00185       private:
00186         T *o;
00187         void (T::*m)(unsigned xAbs, unsigned yAbs, int xRel, int yRel);
00188         virtual void move(unsigned xAbs, unsigned yAbs, int xRel, int yRel)
00189         {
00190           (o->*m)(xAbs, yAbs, xRel, yRel);
00191         }
00192       };
00193 
00217       struct Dispatcher: Window::MouseHandler
00218       {
00225         Dispatcher(unsigned numberOfButtons = 3,
00226                    unsigned numberOfModes = 1,
00227                    unsigned maxMulticlickLimit = 1,
00228                    unsigned long multiclickMaxDelay = 200);
00229         virtual ~Dispatcher();
00230 
00234         void setMulticlickLimit(unsigned button, unsigned limit);
00235 
00236         void setButtonHandler(unsigned button, ButtonHandler *,
00237                               unsigned mode = 0,
00238                               unsigned multiclickCardinality = 1);
00239 
00240         void setMotionHandler(MotionHandler *, unsigned mode = 0);
00241 
00242         void setMode(unsigned);
00243         unsigned getMode() const { return currentMode; }
00244 
00245       private:
00246         const unsigned numberOfButtons;
00247         const unsigned numberOfModes;
00248         const unsigned maxMulticlickLimit;
00249         const unsigned long multiclickMaxDelay; // In milliseconds
00250 
00251         struct ButtonAccount
00252         {
00253           unsigned long lastClick;
00254           unsigned multiclickLimit;
00255           unsigned multiclickCounter;
00256           unsigned clickMode;
00257           ButtonAccount(): lastClick(0), multiclickLimit(1),
00258                            multiclickCounter(0), clickMode(0) {}
00259         };
00260 
00261         ButtonAccount *buttonAccounts;
00262         ButtonHandler **buttonHandlers;
00263         MotionHandler **motionHandlers;
00264 
00265         unsigned currentMode;
00266 
00267         virtual void press(unsigned button, unsigned x, unsigned y,
00268                            unsigned long milliSecondTime);
00269         virtual void release(unsigned button, unsigned x, unsigned y);
00270         virtual void motion(unsigned xAbs, unsigned yAbs, int xRel, int yRel);
00271       };
00272     }
00273   }
00274 }
00275 
00276 #endif // ARCHON_UTILITIES_KEYBOARD_H

Generated on Sun Jul 30 22:55:45 2006 for Archon by  doxygen 1.4.4