Blender  V2.59
GPC_MouseDevice.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: GPC_MouseDevice.cpp 35170 2011-02-25 13:35:11Z jesterking $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): none yet.
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00035 #include "GPC_MouseDevice.h"
00036 
00037 GPC_MouseDevice::GPC_MouseDevice()
00038 {
00039 
00040 }
00041 GPC_MouseDevice::~GPC_MouseDevice()
00042 {
00043 
00044 }
00045 
00049 bool GPC_MouseDevice::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
00050 {
00051         const SCA_InputEvent & inevent =  m_eventStatusTables[m_currentTable][inputcode];
00052         bool pressed = (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 
00053                 inevent.m_status == SCA_InputEvent::KX_ACTIVE);
00054         return pressed;
00055 }
00056 
00057 
00063 void GPC_MouseDevice::NextFrame()
00064 {
00065         SCA_IInputDevice::NextFrame();
00066         
00067         // Convert just pressed events into regular (active) events
00068         int previousTable = 1-m_currentTable;
00069         for (int mouseevent= KX_BEGINMOUSE; mouseevent< KX_ENDMOUSEBUTTONS; mouseevent++) {
00070                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mouseevent];
00071                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
00072                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE) {
00073                         m_eventStatusTables[m_currentTable][mouseevent] = oldevent;
00074                         m_eventStatusTables[m_currentTable][mouseevent].m_status = SCA_InputEvent::KX_ACTIVE;
00075                 }
00076         }
00077         for (int mousemove= KX_ENDMOUSEBUTTONS; mousemove< KX_ENDMOUSE; mousemove++) {
00078                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mousemove];
00079                 m_eventStatusTables[m_currentTable][mousemove] = oldevent;
00080                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
00081                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE) {       
00082                         m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_JUSTRELEASED;
00083                 }
00084                 else {
00085                         if (oldevent.m_status == SCA_InputEvent::KX_JUSTRELEASED) {
00086                                 m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS ;
00087                         }
00088                 }
00089         }
00090 }
00091 
00092 
00093 bool GPC_MouseDevice::ConvertButtonEvent(TButtonId button, bool isDown)
00094 {
00095         bool result = false;
00096 
00097         switch (button)
00098         {
00099         case buttonLeft:
00100                 result = ConvertEvent(KX_LEFTMOUSE, isDown);
00101                 break;
00102         case buttonMiddle:
00103                 result = ConvertEvent(KX_MIDDLEMOUSE, isDown);
00104                 break;
00105         case buttonRight:
00106                 result = ConvertEvent(KX_RIGHTMOUSE, isDown);
00107                 break;
00108         case buttonWheelUp:
00109                 result = ConvertEvent(KX_WHEELUPMOUSE, isDown);
00110                 break;
00111         case buttonWheelDown:
00112                 result = ConvertEvent(KX_WHEELDOWNMOUSE, isDown);
00113                 break;
00114         default:
00115                 // Should not happen!
00116                 break;
00117         }
00118 
00119         return result;
00120 }
00121 
00126 bool GPC_MouseDevice::ConvertButtonEvent(TButtonId button, bool isDown, int x, int y)
00127 {
00128         // First update state tables for cursor move.
00129         bool result = ConvertMoveEvent(x, y);
00130 
00131         // Now update for button state.
00132         if (result) {
00133                 result = ConvertButtonEvent(button, isDown);
00134         }
00135 
00136         return result;
00137 }
00138 
00142 bool GPC_MouseDevice::ConvertMoveEvent(int x, int y)
00143 {
00144         bool result;
00145 
00146         // Convert to local coordinates?
00147         result = ConvertEvent(KX_MOUSEX, x);
00148         if (result) {
00149                 result = ConvertEvent(KX_MOUSEY, y);
00150         }
00151 
00152         return result;
00153 }
00154 
00155 
00156 bool GPC_MouseDevice::ConvertEvent(KX_EnumInputs kxevent, int eventval)
00157 {
00158         bool result = true;
00159         
00160         // Only process it, if it's a mouse event
00161         if (kxevent > KX_BEGINMOUSE && kxevent < KX_ENDMOUSE) {
00162                 int previousTable = 1-m_currentTable;
00163 
00164                 if (eventval > 0) {
00165                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = eventval;
00166 
00167                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
00168                         {
00169                         case SCA_InputEvent::KX_ACTIVE:
00170                         case SCA_InputEvent::KX_JUSTACTIVATED:
00171                                 {
00172                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
00173                                         break;
00174                                 }
00175                         case SCA_InputEvent::KX_JUSTRELEASED:
00176                                 {
00177                                         if ( kxevent > KX_BEGINMOUSEBUTTONS && kxevent < KX_ENDMOUSEBUTTONS)
00178                                         {
00179                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
00180                                         } else
00181                                         {
00182                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
00183                                                 
00184                                         }
00185                                         break;
00186                                 }
00187                         default:
00188                                 {
00189                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
00190                                 }
00191                         }
00192                         
00193                 } 
00194                 else {
00195                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
00196                         {
00197                         case SCA_InputEvent::KX_JUSTACTIVATED:
00198                         case SCA_InputEvent::KX_ACTIVE:
00199                                 {
00200                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
00201                                         break;
00202                                 }
00203                         default:
00204                                 {
00205                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
00206                                 }
00207                         }
00208                 }
00209         }
00210         else {
00211                 result = false;
00212         }
00213         return result;
00214 }