Blender V2.61 - r43446

SCA_MouseSensor.cpp

Go to the documentation of this file.
00001 /*
00002  * Sensor for mouse input
00003  *
00004  *
00005  *
00006  * ***** BEGIN GPL LICENSE BLOCK *****
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00023  * All rights reserved.
00024  *
00025  * The Original Code is: all of this file.
00026  *
00027  * Contributor(s): José I. Romero (cleanup and fixes)
00028  *
00029  * ***** END GPL LICENSE BLOCK *****
00030  */
00031 
00037 #include <stddef.h>
00038 
00039 #include "SCA_MouseSensor.h"
00040 #include "SCA_EventManager.h"
00041 #include "SCA_MouseManager.h"
00042 #include "SCA_LogicManager.h"
00043 #include "SCA_IInputDevice.h"
00044 #include "ConstExpr.h"
00045 #include <iostream>
00046 
00047 /* ------------------------------------------------------------------------- */
00048 /* Native functions                                                          */
00049 /* ------------------------------------------------------------------------- */
00050 
00051 SCA_MouseSensor::SCA_MouseSensor(SCA_MouseManager* eventmgr, 
00052                                  int startx,int starty,
00053                                  short int mousemode,
00054                                  SCA_IObject* gameobj)
00055     : SCA_ISensor(gameobj,eventmgr),
00056       m_x(startx),
00057       m_y(starty)
00058 {
00059     m_mousemode   = mousemode;
00060     m_triggermode = true;
00061 
00062     UpdateHotkey(this);
00063     Init();
00064 }
00065 
00066 void SCA_MouseSensor::Init()
00067 {
00068     m_val = (m_invert)?1:0; /* stores the latest attribute */
00069     m_reset = true;
00070 }
00071 
00072 SCA_MouseSensor::~SCA_MouseSensor() 
00073 {
00074     /* Nothing to be done here. */
00075 }
00076 
00077 void SCA_MouseSensor::UpdateHotkey(void *self)
00078 {
00079     // gosh, this function is so damn stupid
00080     // its here because of a design mistake in the mouse sensor, it should only
00081     // have 3 trigger modes (button, wheel, move), and let the user set the 
00082     // hotkey separately, like the other sensors. but instead it has a mode for 
00083     // each friggin key and i have to update the hotkey based on it... genius!
00084     SCA_MouseSensor* sensor = reinterpret_cast<SCA_MouseSensor*>(self);
00085 
00086     switch (sensor->m_mousemode) {
00087     case KX_MOUSESENSORMODE_LEFTBUTTON:
00088         sensor->m_hotkey = SCA_IInputDevice::KX_LEFTMOUSE;
00089         break;
00090     case KX_MOUSESENSORMODE_MIDDLEBUTTON:
00091         sensor->m_hotkey = SCA_IInputDevice::KX_MIDDLEMOUSE;
00092         break;
00093     case KX_MOUSESENSORMODE_RIGHTBUTTON:
00094         sensor->m_hotkey = SCA_IInputDevice::KX_RIGHTMOUSE;
00095         break;
00096     case KX_MOUSESENSORMODE_WHEELUP:
00097         sensor->m_hotkey = SCA_IInputDevice::KX_WHEELUPMOUSE;
00098         break;
00099     case KX_MOUSESENSORMODE_WHEELDOWN:
00100         sensor->m_hotkey = SCA_IInputDevice::KX_WHEELDOWNMOUSE;
00101         break;
00102     default:
00103         ; /* ignore, no hotkey */
00104     }
00105 }
00106 
00107 CValue* SCA_MouseSensor::GetReplica()
00108 {
00109     SCA_MouseSensor* replica = new SCA_MouseSensor(*this);
00110     // this will copy properties and so on...
00111     replica->ProcessReplica();
00112     replica->Init();
00113 
00114     return replica;
00115 }
00116 
00117 
00118 
00119 bool SCA_MouseSensor::IsPositiveTrigger()
00120 {
00121     bool result = (m_val != 0);
00122     if (m_invert)
00123         result = !result;
00124         
00125     return result;
00126 }
00127 
00128 
00129 
00130 short int SCA_MouseSensor::GetModeKey()
00131 { 
00132     return m_mousemode;
00133 }
00134 
00135 
00136 
00137 SCA_IInputDevice::KX_EnumInputs SCA_MouseSensor::GetHotKey()
00138 { 
00139     return m_hotkey;
00140 }
00141 
00142 
00143 
00144 bool SCA_MouseSensor::Evaluate()
00145 {
00146     bool result = false;
00147     bool reset = m_reset && m_level;
00148     SCA_IInputDevice* mousedev = ((SCA_MouseManager *)m_eventmgr)->GetInputDevice();
00149 
00150     m_reset = false;
00151     switch (m_mousemode) {
00152     case KX_MOUSESENSORMODE_LEFTBUTTON:
00153     case KX_MOUSESENSORMODE_MIDDLEBUTTON:
00154     case KX_MOUSESENSORMODE_RIGHTBUTTON:
00155     case KX_MOUSESENSORMODE_WHEELUP:
00156     case KX_MOUSESENSORMODE_WHEELDOWN:
00157         {
00158             const SCA_InputEvent& mevent = mousedev->GetEventValue(m_hotkey);
00159             switch (mevent.m_status){   
00160             case SCA_InputEvent::KX_JUSTACTIVATED:
00161                 m_val = 1;
00162                 result = true;
00163                 break;
00164             case SCA_InputEvent::KX_JUSTRELEASED:
00165                 m_val = 0;
00166                 result = true;
00167                 break;
00168             case SCA_InputEvent::KX_ACTIVE:
00169                 if (m_val == 0)
00170                 {
00171                     m_val = 1;
00172                     if (m_level)
00173                         result = true;
00174                 }
00175                 break;
00176             default:
00177                 if (m_val == 1)
00178                 {
00179                     m_val = 0;
00180                     result = true;
00181                 }
00182                 break;
00183             }
00184             break;
00185         }
00186     case KX_MOUSESENSORMODE_MOVEMENT:
00187         {
00188             const SCA_InputEvent& eventX = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
00189             const SCA_InputEvent& eventY = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
00190 
00191             if (eventX.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
00192                 eventY.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
00193                 eventX.m_status == SCA_InputEvent::KX_ACTIVE ||
00194                 eventY.m_status == SCA_InputEvent::KX_ACTIVE)   
00195             {
00196                 m_val = 1;
00197                 result = true;
00198             } 
00199             else if (eventX.m_status == SCA_InputEvent::KX_JUSTRELEASED ||
00200                     eventY.m_status == SCA_InputEvent::KX_JUSTRELEASED )
00201             {
00202                 m_val = 0;
00203                 result = true;
00204             } 
00205             else //KX_NO_IMPUTSTATUS
00206             { 
00207                 if (m_val == 1)
00208                 {
00209                     m_val = 0;
00210                     result = true;
00211                 }
00212             }
00213             
00214             break;
00215         }
00216     default:
00217         ; /* error */
00218     }
00219 
00220     if (reset)
00221         // force an event
00222         result = true;
00223     return result;
00224 }
00225 
00226 void SCA_MouseSensor::setX(short x)
00227 {
00228     m_x = x;
00229 }
00230 
00231 void SCA_MouseSensor::setY(short y)
00232 {
00233     m_y = y;
00234 }
00235 
00236 bool SCA_MouseSensor::isValid(SCA_MouseSensor::KX_MOUSESENSORMODE m)
00237 {
00238     return ((m > KX_MOUSESENSORMODE_NODEF) && (m < KX_MOUSESENSORMODE_MAX));
00239 }
00240 
00241 #ifdef WITH_PYTHON
00242 
00243 /* ------------------------------------------------------------------------- */
00244 /* Python functions                                                          */
00245 /* ------------------------------------------------------------------------- */
00246 
00247 KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus,
00248 "getButtonStatus(button)\n"
00249 "\tGet the given button's status (KX_INPUT_NONE, KX_INPUT_NONE, KX_INPUT_JUST_ACTIVATED, KX_INPUT_ACTIVE, KX_INPUT_JUST_RELEASED).\n")
00250 {
00251     if (PyLong_Check(value))
00252     {
00253         int button = PyLong_AsSsize_t(value);
00254         
00255         if ((button < SCA_IInputDevice::KX_LEFTMOUSE)
00256             || (button > SCA_IInputDevice::KX_RIGHTMOUSE)){
00257             PyErr_SetString(PyExc_ValueError, "sensor.getButtonStatus(int): Mouse Sensor, invalid button specified!");
00258             return NULL;
00259         }
00260         
00261         SCA_IInputDevice* mousedev = ((SCA_MouseManager *)m_eventmgr)->GetInputDevice();
00262         const SCA_InputEvent& event = mousedev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) button);
00263         return PyLong_FromSsize_t(event.m_status);
00264     }
00265     
00266     Py_RETURN_NONE;
00267 }
00268 
00269 /* ------------------------------------------------------------------------- */
00270 /* Python Integration Hooks                                                  */
00271 /* ------------------------------------------------------------------------- */
00272 
00273 PyTypeObject SCA_MouseSensor::Type = {
00274     PyVarObject_HEAD_INIT(NULL, 0)
00275     "SCA_MouseSensor",
00276     sizeof(PyObjectPlus_Proxy),
00277     0,
00278     py_base_dealloc,
00279     0,
00280     0,
00281     0,
00282     0,
00283     py_base_repr,
00284     0,0,0,0,0,0,0,0,0,
00285     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00286     0,0,0,0,0,0,0,
00287     Methods,
00288     0,
00289     0,
00290     &SCA_ISensor::Type,
00291     0,0,0,0,0,0,
00292     py_base_new
00293 };
00294 
00295 PyMethodDef SCA_MouseSensor::Methods[] = {
00296     KX_PYMETHODTABLE_O(SCA_MouseSensor, getButtonStatus),
00297     {NULL,NULL} //Sentinel
00298 };
00299 
00300 int SCA_MouseSensor::UpdateHotkeyPy(void *self, const PyAttributeDef*)
00301 {
00302     UpdateHotkey(self);
00303     // return value is used in py_setattro(),
00304     // 0=attribute checked ok (see Attributes array definition)
00305     return 0;
00306 }
00307 
00308 PyAttributeDef SCA_MouseSensor::Attributes[] = {
00309     KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",KX_MOUSESENSORMODE_NODEF,KX_MOUSESENSORMODE_MAX-1,true,SCA_MouseSensor,m_mousemode,UpdateHotkeyPy),
00310     KX_PYATTRIBUTE_SHORT_LIST_RO("position",SCA_MouseSensor,m_x,2),
00311     { NULL }    //Sentinel
00312 };
00313 
00314 #endif // WITH_PYTHON
00315 
00316 /* eof */