keyboard.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_KEYBOARD_H
00021 #define ARCHON_UTILITIES_KEYBOARD_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 KeyboardEvents
00037     {
00038       using namespace std;
00039 
00040       struct Handler
00041       {
00042         virtual void press() = 0;
00043         virtual void release() = 0;
00044         virtual ~Handler() {}
00045       };
00046 
00047       struct FuncCall: Handler
00048       {
00053         FuncCall(void (*pressFunc)(), void (*releaseFunc)()):
00054           p(pressFunc), r(releaseFunc) {}
00055 
00056       private:
00057         void (*p)();
00058         void (*r)();
00059         virtual void press()   { if(p) (*p)(); }
00060         virtual void release() { if(r) (*r)(); }
00061 
00062       };
00063 
00064       template<class T> struct MethodCall: Handler
00065       {
00070         MethodCall(T *object,
00071                    void (T::*pressMethod)(),
00072                    void (T::*releaseMethod)()):
00073           o(object), p(pressMethod), r(releaseMethod) {}
00074 
00075       private:
00076         T *o;
00077         void (T::*p)();
00078         void (T::*r)();
00079         virtual void press()   { if(p) (o->*p)(); }
00080         virtual void release() { if(r) (o->*r)(); }
00081       };
00082 
00083       struct Toggle: Handler
00084       {
00085         Toggle(bool &var): var(var) {}
00086 
00087       private:
00088         bool &var;
00089         virtual void press()   { var = !var; }
00090         virtual void release() {}
00091       };
00092 
00093       template<class T> struct VarAssign: Handler
00094       {
00095         VarAssign(T &variable, T pressValue, T releaseValue):
00096           var(variable), pVal(pressValue), rVal(releaseValue), rEnable(true) {}
00097 
00102         VarAssign(T &variable, T pressValue):
00103           var(variable), pVal(pressValue), rVal(pVal), rEnable(false) {}
00104 
00105       private:
00106         T &var;
00107         const T pVal;
00108         const T rVal;
00109         const bool rEnable;
00110         virtual void press()   { var = pVal; }
00111         virtual void release() { if(rEnable) var = rVal; }
00112       };
00113 
00114       template<class T> struct VarAdd: Handler
00115       {
00116         VarAdd(T &variable, T value, bool ignoreRelease = false):
00117           var(variable), val(value), rEnable(!ignoreRelease) {}
00118 
00119       private:
00120         T &var;
00121         const T val;
00122         virtual void press()   { var += val; }
00123         virtual void release() { if(rEnable) var -= val; }
00124         const bool rEnable;
00125       };
00126 
00127       template<class T> struct VarMultiply: Handler
00128       {
00129         VarMultiply(T &variable, T value, bool ignoreRelease = false):
00130           var(variable), val(value), rEnable(!ignoreRelease) {}
00131 
00132       private:
00133         T &var;
00134         const T val;
00135         virtual void press()   { var *= val; }
00136         virtual void release() { if(rEnable) var /= val; }
00137         const bool rEnable;
00138       };
00139 
00165       struct Dispatcher: Window::KeyboardHandler
00166       {
00173         Dispatcher(unsigned numberOfModes = 1,
00174                    unsigned maxMultipressLimit = 1,
00175                    unsigned long multipressMaxDelay = 200);
00176         virtual ~Dispatcher();
00177 
00181         void setMultipressLimit(Key, unsigned limit);
00182 
00183         void setHandler(Key, Handler *,
00184                         unsigned mode = 0,
00185                         unsigned multipressCardinality = 1);
00186 
00187         void setMode(unsigned);
00188         unsigned getMode() const { return currentMode; }
00189 
00190       private:
00191         const unsigned numberOfModes;
00192         const unsigned maxMultipressLimit;
00193         const unsigned long multipressMaxDelay; // In milliseconds
00194 
00195         struct KeyAccount
00196         {
00197           unsigned long lastPress;
00198           unsigned multipressLimit;
00199           unsigned multipressCounter;
00200           unsigned pressMode;
00201           KeyAccount(): lastPress(0), multipressLimit(1),
00202                         multipressCounter(0), pressMode(0) {}
00203         };
00204 
00205         KeyAccount *keyAccounts;
00206         Handler **handlers;
00207 
00208         unsigned currentMode;
00209 
00210         virtual void press(Key, unsigned long milliSecondTime);
00211         virtual void release(Key);
00212       };
00213     }
00214   }
00215 }
00216 
00217 #endif // ARCHON_UTILITIES_KEYBOARD_H

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