glutinput.cpp

00001 /*      _______   __   __   __   ______   __   __   _______   __   __
00002  *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
00003  *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
00004  *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
00005  *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
00006  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
00007  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
00008  *
00009  * Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson
00010  *
00011  *                                                         Js_./
00012  * Per Larsson a.k.a finalman                          _RqZ{a<^_aa
00013  * Olof Naessén a.k.a jansem/yakslem                _asww7!uY`>  )\a//
00014  *                                                 _Qhm`] _f "'c  1!5m
00015  * Visit: http://guichan.darkbits.org             )Qk<P ` _: :+' .'  "{[
00016  *                                               .)j(] .d_/ '-(  P .   S
00017  * License: (BSD)                                <Td/Z <fP"5(\"??"\a.  .L
00018  * Redistribution and use in source and          _dV>ws?a-?'      ._/L  #'
00019  * binary forms, with or without                 )4d[#7r, .   '     )d`)[
00020  * modification, are permitted provided         _Q-5'5W..j/?'   -?!\)cam'
00021  * that the following conditions are met:       j<<WP+k/);.        _W=j f
00022  * 1. Redistributions of source code must       .$%w\/]Q  . ."'  .  mj$
00023  *    retain the above copyright notice,        ]E.pYY(Q]>.   a     J@\
00024  *    this list of conditions and the           j(]1u<sE"L,. .   ./^ ]{a
00025  *    following disclaimer.                     4'_uomm\.  )L);-4     (3=
00026  * 2. Redistributions in binary form must        )_]X{Z('a_"a7'<a"a,  ]"[
00027  *    reproduce the above copyright notice,       #}<]m7`Za??4,P-"'7. ).m
00028  *    this list of conditions and the            ]d2e)Q(<Q(  ?94   b-  LQ/
00029  *    following disclaimer in the                <B!</]C)d_, '(<' .f. =C+m
00030  *    documentation and/or other materials      .Z!=J ]e []('-4f _ ) -.)m]'
00031  *    provided with the distribution.          .w[5]' _[ /.)_-"+?   _/ <W"
00032  * 3. Neither the name of Guichan nor the      :$we` _! + _/ .        j?
00033  *    names of its contributors may be used     =3)= _f  (_yQmWW$#(    "
00034  *    to endorse or promote products derived     -   W,  sQQQQmZQ#Wwa]..
00035  *    from this software without specific        (js, \[QQW$QWW#?!V"".
00036  *    prior written permission.                    ]y:.<\..          .
00037  *                                                 -]n w/ '         [.
00038  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT       )/ )/           !
00039  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY         <  (; sac    ,    '
00040  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,               ]^ .-  %
00041  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            c <   r
00042  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR            aga<  <La
00043  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          5%  )P'-3L
00044  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR        _bQf` y`..)a
00045  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          ,J?4P'.P"_(\?d'.,
00046  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES               _Pa,)!f/<[]/  ?"
00047  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT      _2-..:. .r+_,.. .
00048  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     ?a.<%"'  " -'.a_ _,
00049  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                     ^
00050  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00051  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00052  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00053  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00054  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00055  */
00056 
00057 /*
00058  * For comments regarding functions please see the header file.
00059  */
00060 
00061 #include "guichan/glut/glutinput.hpp"
00062 #include "guichan/exception.hpp"
00063 
00064 namespace gcn
00065 {
00066 
00067     GLUTInput::GLUTInput()
00068     {
00069     }
00070 
00071     bool GLUTInput::isKeyQueueEmpty()
00072     {
00073         return mKeyInputQueue.empty();
00074     }
00075 
00076     KeyInput GLUTInput::dequeueKeyInput()
00077     {
00078         KeyInput keyInput;
00079 
00080         if (mKeyInputQueue.empty())
00081         {
00082             throw GCN_EXCEPTION("The queue is empty.");
00083         }
00084 
00085         keyInput = mKeyInputQueue.front();
00086         mKeyInputQueue.pop();
00087 
00088         return keyInput;
00089     }
00090 
00091     bool GLUTInput::isMouseQueueEmpty()
00092     {
00093         return mMouseInputQueue.empty();
00094     }
00095 
00096     MouseInput GLUTInput::dequeueMouseInput()
00097     {
00098         MouseInput mouseInput;
00099 
00100         if (mMouseInputQueue.empty())
00101         {
00102             throw GCN_EXCEPTION("The queue is empty.");
00103         }
00104 
00105         mouseInput = mMouseInputQueue.front();
00106         mMouseInputQueue.pop();
00107 
00108         return mouseInput;
00109     }
00110 
00111     void GLUTInput::pushKeyDownInput(unsigned char key)
00112     {
00113         KeyInput keyInput;
00114         keyInput.setKey(convertKeyCharacter(key));
00115         keyInput.setType(KeyInput::PRESS);
00116         mKeyInputQueue.push(keyInput);
00117     }
00118 
00119     void GLUTInput::pushKeyUpInput(unsigned char key)
00120     {
00121         KeyInput keyInput;
00122         keyInput.setKey(convertKeyCharacter(key));
00123         keyInput.setType(KeyInput::RELEASE);
00124         mKeyInputQueue.push(keyInput);
00125     }
00126 
00127     void GLUTInput::pushSpecialDownInput(unsigned char key)
00128     {
00129         KeyInput keyInput;
00130         keyInput.setKey(convertSpecialCharacter(key));
00131         keyInput.setType(KeyInput::PRESS);
00132         mKeyInputQueue.push(keyInput);
00133 
00134     }
00135 
00136     void GLUTInput::pushSpecialUpInput(unsigned char key)
00137     {
00138         KeyInput keyInput;
00139         keyInput.setKey(convertSpecialCharacter(key));
00140         keyInput.setType(KeyInput::RELEASE);
00141         mKeyInputQueue.push(keyInput);
00142     }
00143 
00144     void GLUTInput::pushMouseInput(int button, int state, int x, int y)
00145     {
00146         MouseInput mouseInput;
00147         mouseInput.x = x;
00148         mouseInput.y = y;
00149         mouseInput.setButton(convertMouseButton(button));
00150 
00151         if (state == GLUT_DOWN)
00152         {
00153             mouseInput.setType(MouseInput::PRESS);
00154         }
00155         else
00156         {
00157             mouseInput.setType(MouseInput::RELEASE);
00158         }
00159 
00160         mMouseInputQueue.push(mouseInput);
00161     }
00162 
00163     void GLUTInput::pushMotionInput(int x, int y)
00164     {
00165         MouseInput mouseInput;
00166         mouseInput.x = x;
00167         mouseInput.y = y;
00168         mouseInput.setButton(MouseInput::EMPTY);
00169         mouseInput.setType(MouseInput::MOTION);
00170         mMouseInputQueue.push(mouseInput);
00171     }
00172 
00173     void GLUTInput::pushPassiveMotionInput(int x, int y)
00174     {
00175         pushMotionInput(x, y);
00176     }
00177 
00178     Key GLUTInput::convertKeyCharacter(unsigned char key)
00179     {
00180         int value = 0;
00181         Key gcnKey;
00182 
00183         value = (int)key;
00184 
00185         switch(key)
00186         {
00187             case 8:
00188                 value = Key::BACKSPACE;
00189                 break;
00190             case 13:
00191                 value = Key::ENTER;
00192                 break;
00193             case 27:
00194                 value = Key::ESCAPE;
00195                 break;
00196             case 127:
00197                 value = Key::DELETE;
00198                 break;
00199         }
00200 
00201         int modifiers = glutGetModifiers();
00202         gcnKey.setShiftPressed(modifiers & GLUT_ACTIVE_SHIFT);
00203         gcnKey.setControlPressed(modifiers & GLUT_ACTIVE_CTRL);
00204         gcnKey.setAltPressed(modifiers & GLUT_ACTIVE_ALT);
00205 
00206         gcnKey.setValue(value);
00207 
00208         return gcnKey;
00209     }
00210 
00211     Key GLUTInput::convertSpecialCharacter(unsigned char key)
00212     {
00213         int value = 0;
00214         Key gcnKey;
00215 
00216         value = (int)key;
00217 
00218         switch(key)
00219         {
00220             case GLUT_KEY_UP:
00221                 value = Key::UP;
00222                 break;
00223             case GLUT_KEY_DOWN:
00224                 value = Key::DOWN;
00225                 break;
00226             case GLUT_KEY_RIGHT:
00227                 value = Key::RIGHT;
00228                 break;
00229             case GLUT_KEY_LEFT:
00230                 value = Key::LEFT;
00231                 break;
00232             case GLUT_KEY_F1:
00233                 value = Key::F1;
00234                 break;
00235             case GLUT_KEY_F2:
00236                 value = Key::F2;
00237                 break;
00238             case GLUT_KEY_F3:
00239                 value = Key::F3;
00240                 break;
00241             case GLUT_KEY_F4:
00242                 value = Key::F4;
00243                 break;
00244             case GLUT_KEY_F5:
00245                 value = Key::F5;
00246                 break;
00247             case GLUT_KEY_F6:
00248                 value = Key::F6;
00249                 break;
00250             case GLUT_KEY_F7:
00251                 value = Key::F7;
00252                 break;
00253             case GLUT_KEY_F8:
00254                 value = Key::F8;
00255                 break;
00256             case GLUT_KEY_F9:
00257                 value = Key::F9;
00258                 break;
00259             case GLUT_KEY_F10:
00260                 value = Key::F10;
00261                 break;
00262             case GLUT_KEY_F11:
00263                 value = Key::F11;
00264                 break;
00265             case GLUT_KEY_F12:
00266                 value = Key::F12;
00267                 break;
00268             case GLUT_KEY_PAGE_UP:
00269                 value = Key::PAGE_UP;
00270                 break;
00271             case GLUT_KEY_PAGE_DOWN:
00272                 value = Key::PAGE_DOWN;
00273                 break;
00274             case GLUT_KEY_HOME:
00275                 value = Key::HOME;
00276                 break;
00277             case GLUT_KEY_END:
00278                 value = Key::END;
00279                 break;
00280             case GLUT_KEY_INSERT:
00281                 value = Key::INSERT;
00282                 break;
00283             default:
00284                 break;
00285         }
00286 
00287         int modifiers = glutGetModifiers();
00288         gcnKey.setShiftPressed(modifiers & GLUT_ACTIVE_SHIFT);
00289         gcnKey.setControlPressed(modifiers & GLUT_ACTIVE_CTRL);
00290         gcnKey.setAltPressed(modifiers & GLUT_ACTIVE_ALT);
00291 
00292         gcnKey.setValue(value);
00293 
00294         return gcnKey;
00295     }
00296 
00297     int GLUTInput::convertMouseButton(int button)
00298     {
00299         switch (button)
00300         {
00301             case GLUT_LEFT_BUTTON:
00302                 return MouseInput::LEFT;
00303                 break;
00304             case GLUT_RIGHT_BUTTON:
00305                 return MouseInput::RIGHT;
00306                 break;
00307             case GLUT_MIDDLE_BUTTON:
00308                 return MouseInput::MIDDLE;
00309                 break;
00310         }
00311 
00312         throw GCN_EXCEPTION("Unknown GLUT mouse type.");
00313 
00314         return 0;
00315     }
00316 }

Generated on Sat Jul 29 19:38:48 2006 for Guichan by  doxygen 1.4.7