Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

SCA_KeyboardSensor.cpp

Go to the documentation of this file.
00001 
00006 
00007 #include "SCA_KeyboardSensor.h"
00008 #include "SCA_KeyboardManager.h"
00009 #include "SCA_LogicManager.h"
00010 #include "SCA_ISystem.h"
00011 
00012 /* ------------------------------------------------------------------------- */
00013 /* Python functions : integration hooks                                      */
00014 /* ------------------------------------------------------------------------- */
00015 
00016 PyTypeObject SCA_KeyboardSensor::Type = {
00017         PyObject_HEAD_INIT(&PyType_Type)
00018         0,
00019         "SCA_KeyboardSensor",
00020         sizeof(SCA_KeyboardSensor),
00021         0,
00022         PyDestructor,
00023         0,
00024         __getattr,
00025         __setattr,
00026         0, //&MyPyCompare,
00027         __repr,
00028         0, //&cvalue_as_number,
00029         0,
00030         0,
00031         0,
00032         0
00033 };
00034 
00035 PyParentObject SCA_KeyboardSensor::Parents[] = {
00036         &SCA_KeyboardSensor::Type,
00037         &SCA_ISensor::Type,
00038         &SCA_ILogicBrick::Type,
00039         &CValue::Type,
00040         NULL
00041 };
00042 
00043 PyMethodDef SCA_KeyboardSensor::Methods[] = {
00044   {"getKey", (PyCFunction) SCA_KeyboardSensor::sPyGetKey, METH_VARARGS, GetKey_doc},
00045   {"setKey", (PyCFunction) SCA_KeyboardSensor::sPySetKey, METH_VARARGS, SetKey_doc},
00046   {"getHold1", (PyCFunction) SCA_KeyboardSensor::sPyGetHold1, METH_VARARGS, GetHold1_doc},
00047   {"setHold1", (PyCFunction) SCA_KeyboardSensor::sPySetHold1, METH_VARARGS, SetHold1_doc},
00048   {"getHold2", (PyCFunction) SCA_KeyboardSensor::sPyGetHold2, METH_VARARGS, GetHold2_doc},
00049   {"setHold2", (PyCFunction) SCA_KeyboardSensor::sPySetHold2, METH_VARARGS, SetHold2_doc},
00050 //  {"getUseAllKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetUseAllKeys, METH_VARARGS, GetUseAllKeys_doc},
00051 //  {"setUseAllKeys", (PyCFunction) SCA_KeyboardSensor::sPySetUseAllKeys, METH_VARARGS, SetUseAllKeys_doc},
00052   {"getPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetPressedKeys, METH_VARARGS, GetPressedKeys_doc},
00053 //  {"getKeyEvents", (PyCFunction) SCA_KeyboardSensor::sPyGetKeyEvents, METH_VARARGS, GetKeyEvents_doc},
00054   {NULL,NULL} //Sentinel
00055 };
00056 
00057 PyObject*
00058 SCA_KeyboardSensor::_getattr(char* attr)

00059 {
00060   _getattr_up(SCA_ISensor);
00061 }
00062 
00063 
00064 /* ------------------------------------------------------------------------- */
00065 /* Native functions                                                          */
00066 /* ------------------------------------------------------------------------- */
00067 
00068 SCA_KeyboardSensor::SCA_KeyboardSensor(SCA_KeyboardManager* keybdmgr,short int hotkey,short int qual,short int qual2,bool bAllKeys,SCA_IObject* gameobj,PyTypeObject* T )
00069 :SCA_ISensor(gameobj,keybdmgr,T),
00070 m_pKeyboardMgr(keybdmgr),
00071 m_hotkey(hotkey),
00072 m_qual(qual),
00073 m_qual2(qual2),
00074 m_bAllKeys(bAllKeys)

00075 

00076 

00077 {
00078         
00079 //      SetDrawColor(0xff0000ff);
00080         m_val=0;
00081 }
00082 
00083 
00084 bool SCA_KeyboardSensor::Evaluate(CValue* eventval)

00085 {
00086         bool result = false;
00087         SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
00088 
00089         if (m_bAllKeys)
00090         {
00091                 bool justactivated = false;
00092                 bool justreleased = false;
00093 
00094                 for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++)
00095                 {
00096                         const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
00097                         if (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED)
00098                         {
00099                                 justactivated = true;
00100                         }
00101                         if (inevent.m_status == SCA_InputEvent::KX_JUSTRELEASED)
00102                         {
00103                                 justreleased = true;
00104                         }
00105                 }
00106 
00107                 if (justactivated)
00108                 {
00109                         m_val=1;
00110                         result = true;
00111                 } else
00112                 {
00113                         if (justreleased)
00114                         {
00115                                 m_val=0;
00116                                 result = true;
00117                         }
00118                 }
00119 
00120 
00121         } else
00122         {
00123 
00124                 
00125                 const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) m_hotkey);
00126         
00127                 if (inevent.m_status == SCA_InputEvent::KX_NO_INPUTSTATUS)
00128                 {
00129                         int i=4;
00130                 } else
00131                 {
00132                         if (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED)
00133                         {
00134                                 m_val=1;
00135                                 result = true;
00136                         } else
00137                         {
00138                                 if (inevent.m_status == SCA_InputEvent::KX_JUSTRELEASED)
00139                                 {
00140                                         m_val = 0;
00141                                         result = true;
00142                                 }
00143                         }
00144                 }
00145         }
00146 
00147         return result;
00148 
00149 }
00150 
00151 
00152 /* ------------------------------------------------------------------------- */
00153 /* Python functions : specific                                               */
00154 /* ------------------------------------------------------------------------- */
00155 
00157 char SCA_KeyboardSensor::GetKey_doc[] = 
00158 "getKey()\n"
00159 "\tReturn the code of the key this sensor is listening to.\n" ;
00160 PyObject* SCA_KeyboardSensor::PyGetKey(PyObject* self, PyObject* args, PyObject* kwds)

00161 {
00162         return PyInt_FromLong(m_hotkey);
00163 }
00164 
00166 char SCA_KeyboardSensor::SetKey_doc[] = 
00167 "setKey(keycode)\n"
00168 "\t- keycode: any code from GameKeys\n"
00169 "\tSet the key this sensor should listen to.\n" ;
00170 PyObject* SCA_KeyboardSensor::PySetKey(PyObject* self, PyObject* args, PyObject* kwds)

00171 {
00172         int keyCode;
00173         
00174         if(!PyArg_ParseTuple(args, "i", &keyCode)) {
00175                 return NULL;
00176         }
00177 
00178         /* Since we have symbolic constants for this in Python, we don't guard   */
00179         /* anything. It's up to the user to provide a sensible number.           */
00180         m_hotkey = keyCode;
00181 
00182         Py_Return;
00183 }
00184 
00186 char SCA_KeyboardSensor::GetHold1_doc[] = 
00187 "getHold1()\n"
00188 "\tReturn the code of the first key modifier to the key this \n"
00189 "\tsensor is listening to.\n" ;
00190 PyObject* SCA_KeyboardSensor::PyGetHold1(PyObject* self, PyObject* args, PyObject* kwds)

00191 {
00192         return PyInt_FromLong(m_qual);
00193 }
00194 
00196 char SCA_KeyboardSensor::SetHold1_doc[] = 
00197 "setHold1(keycode)\n"
00198 "\t- keycode: any code from GameKeys\n"
00199 "\tSet the first modifier to the key this sensor should listen to.\n" ;
00200 PyObject* SCA_KeyboardSensor::PySetHold1(PyObject* self, PyObject* args, PyObject* kwds)

00201 {
00202         int keyCode;
00203 
00204         if(!PyArg_ParseTuple(args, "i", &keyCode)) {
00205                 return NULL;
00206         }
00207         
00208         /* Since we have symbolic constants for this in Python, we don't guard   */
00209         /* anything. It's up to the user to provide a sensible number.           */
00210         m_qual = keyCode;
00211 
00212         Py_Return;
00213 }
00214         
00216 char SCA_KeyboardSensor::GetHold2_doc[] = 
00217 "getHold2()\n"
00218 "\tReturn the code of the second key modifier to the key this \n"
00219 "\tsensor is listening to.\n" ;
00220 PyObject* SCA_KeyboardSensor::PyGetHold2(PyObject* self, PyObject* args, PyObject* kwds)

00221 {
00222         return PyInt_FromLong(m_qual2);
00223 }
00224 
00226 char SCA_KeyboardSensor::SetHold2_doc[] = 
00227 "setHold2(keycode)\n"
00228 "\t- keycode: any code from GameKeys\n"
00229 "\tSet the first modifier to the key this sensor should listen to.\n" ;
00230 PyObject* SCA_KeyboardSensor::PySetHold2(PyObject* self, PyObject* args, PyObject* kwds)

00231 {
00232         int keyCode;
00233 
00234         if(!PyArg_ParseTuple(args, "i", &keyCode)) {
00235                 return NULL;
00236         }
00237         
00238         /* Since we have symbolic constants for this in Python, we don't guard   */
00239         /* anything. It's up to the user to provide a sensible number.           */
00240         m_qual2 = keyCode;
00241 
00242         Py_Return;
00243 }
00244 
00245         
00247 char SCA_KeyboardSensor::GetPressedKeys_doc[] = 
00248 "getPressedKeys()\n"
00249 "\tGet a list of pressed keys that have either been pressed, or just released this frame.\n" ;
00250 
00251 PyObject* SCA_KeyboardSensor::PyGetPressedKeys(PyObject* self, PyObject* args, PyObject* kwds)

00252 {
00253         SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
00254 
00255         int num = inputdev->GetNumActiveEvents();
00256         PyObject* resultlist = PyList_New(num);
00257 
00258         if (num > 0)
00259         {
00260                 
00261                 int index = 0;
00262                 
00263                 for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++)
00264                 {
00265                         const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
00266                         if (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED) //NO_INPUTSTATUS)
00267                         {
00268                                 if (index < num)
00269                                 {
00270                                         PyObject* keypair = PyList_New(2);
00271                                         PyList_SetItem(keypair,0,PyInt_FromLong(i));
00272                                         PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status));
00273                                         PyList_SetItem(resultlist,index,keypair);
00274                                         index++;
00275                                 }
00276                         }
00277                 }
00278 
00279                 if (num == index)
00280                 {
00281                         return resultlist;
00282                 } else
00283                 {
00284                         int i=0;
00285                 }
00286         }
00287 
00288         Py_Return;
00289 }
00290 

Generated at Thu Feb 1 13:03:10 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000