blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
Benoit Bolsee 70d239ef7d BGE logic update: new servo control motion actuator, new distance constraint actuator, new orientation constraint actuator, new actuator sensor.
General
=======
- Removal of Damp option in motion actuator (replaced by
  Servo control motion).
- No PyDoc at present, will be added soon.

Generalization of the Lvl option
================================
A sensor with the Lvl option selected will always produce an 
event at the start of the game or when entering a state or at 
object creation. The event will be positive or negative 
depending of the sensor condition. A negative pulse makes
sense when used with a NAND controller: it will be converted
into an actuator activation.

Servo control motion
====================
A new variant of the motion actuator allows to control speed 
with force. The control if of type "PID" (Propotional, Integral, 
Derivate): the force is automatically adapted to achieve the 
target speed. All the parameters of the servo controller are
configurable. The result is a great variety of motion style: 
anysotropic friction, flying, sliding, pseudo Dloc...
This actuator should be used in preference to Dloc and LinV
as it produces more fluid movements and avoids the collision 
problem with Dloc.
LinV : target speed as (X,Y,Z) vector in local or world 
       coordinates (mostly useful in local coordinates).
Limit: the force can be limited along each axis (in the same
       coordinates of LinV). No limitation means that the force
       will grow as large as necessary to achieve the target 
       speed along that axis. Set a max value to limit the 
       accelaration along an axis (slow start) and set a min
       value (negative) to limit the brake force.
P:     Proportional coefficient of servo controller, don't set
       directly unless you know what you're doing.
I:     Integral coefficient of servo controller. Use low value
       (<0.1) for slow reaction (sliding), high values (>0.5)
       for hard control. The P coefficient will be automatically
       set to 60 times the I coefficient (a reasonable value).
D:     Derivate coefficient. Leave to 0 unless you know what
       you're doing. High values create instability. 

Notes: - This actuator works perfectly in zero friction 
         environment: the PID controller will simulate friction
         by applying force as needed.
       - This actuator is compatible with simple Drot motion
         actuator but not with LinV and Dloc motion.
       - (0,0,0) is a valid target speed.
       - All parameters are accessible through Python.

Distance constraint actuator
============================
A new variant of the constraint actuator allows to set the
distance and orientation relative to a surface. The controller
uses a ray to detect the surface (or any object) and adapt the
distance and orientation parallel to the surface.
Damp:  Time constant (in nb of frames) of distance and 
       orientation control.
Dist:  Select to enable distance control and set target 
       distance. The object will be position at the given
       distance of surface along the ray direction.
Direction: chose a local axis as the ray direction.
Range: length of ray. Objecgt within this distance will be 
       detected.
N    : Select to enable orientation control. The actuator will
       change the orientation and the location of the object 
       so that it is parallel to the surface at the vertical
       of the point of contact of the ray.  
M/P  : Select to enable material detection. Default is property
       detection.
Property/Material: name of property/material that the target of
       ray must have to be detected. If not set, property/
       material filter is disabled and any collisioning object
       within range will be detected.
PER  : Select to enable persistent operation. Normally the 
       actuator disables itself automatically if the ray does
       not reach a valid target. 
time : Maximum activation time of actuator. 
       0 : unlimited.
       >0: number of frames before automatic deactivation.  
rotDamp: Time constant (in nb of frame) of orientation control.
       0 : use Damp parameter.
       >0: use a different time constant for orientation.

Notes: - If neither N nor Dist options are set, the actuator
         does not change the position and orientation of the
         object; it works as a ray sensor.
       - The ray has no "X-ray" capability: if the first object
         hit does not have the required property/material, it
         returns no hit and the actuator disables itself unless
         PER option is enabled.
       - This actuator changes the position and orientation but
         not the speed of the object. This has an important 
         implication in a gravity environment: the gravity will
         cause the speed to increase although the object seems
         to stay still (it is repositioned at each frame).
         The gravity must be compensated in one way or another.
         the new servo control motion actuator is the simplest 
         way: set the target speed along the ray axis to 0
         and the servo control will automatically compensate 
         the gravity.
       - This actuator changes the orientation of the object 
         and will conflict with Drot motion unless it is 
         placed BEFORE the Drot motion actuator (the order of 
         actuator is important)
       - All parameters are accessible through Python.

Orientation constraint 
======================
A new variant of the constraint actuator allows to align an
object axis along a global direction.
Damp : Time constant (in nb of frames) of orientation control.
X,Y,Z: Global coordinates of reference direction. 
time : Maximum activation time of actuator. 
       0 : unlimited.
       >0: number of frames before automatic deactivation.  

Notes: - (X,Y,Z) = (0,0,0) is not a valid direction
       - This actuator changes the orientation of the object
         and will conflict with Drot motion unless it is placed
         BEFORE the Drot motion actuator (the order of 
         actuator is important).
       - This actuator doesn't change the location and speed. 
         It is compatible with gravity.
       - All parameters are accessible through Python.

Actuator sensor 
===============
This sensor detects the activation and deactivation of actuators 
of the same object. The sensor generates a positive pulse when 
the corresponding sensor is activated and a negative pulse when 
it is deactivated (the contrary if the Inv option is selected). 
This is mostly useful to chain actions and to detect the loss of 
contact of the distance motion actuator.

Notes: - Actuators are disabled at the start of the game; if you
         want to detect the On-Off transition of an actuator 
         after it has been activated at least once, unselect the
         Lvl and Inv options and use a NAND controller.
       - Some actuators deactivates themselves immediately after 
         being activated. The sensor detects this situation as 
         an On-Off transition.
       - The actuator name can be set through Python.
2008-07-04 08:14:50 +00:00

733 lines
19 KiB
C++

/**
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
* Sensor for keyboard input
*/
#include "SCA_KeyboardSensor.h"
#include "SCA_KeyboardManager.h"
#include "SCA_LogicManager.h"
#include "StringValue.h"
#include "SCA_IInputDevice.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* ------------------------------------------------------------------------- */
/* Native functions */
/* ------------------------------------------------------------------------- */
SCA_KeyboardSensor::SCA_KeyboardSensor(SCA_KeyboardManager* keybdmgr,
short int hotkey,
short int qual,
short int qual2,
bool bAllKeys,
const STR_String& targetProp,
const STR_String& toggleProp,
SCA_IObject* gameobj,
PyTypeObject* T )
:SCA_ISensor(gameobj,keybdmgr,T),
m_pKeyboardMgr(keybdmgr),
m_hotkey(hotkey),
m_qual(qual),
m_qual2(qual2),
m_bAllKeys(bAllKeys),
m_targetprop(targetProp),
m_toggleprop(toggleProp)
{
if (hotkey == SCA_IInputDevice::KX_ESCKEY)
keybdmgr->GetInputDevice()->HookEscape();
// SetDrawColor(0xff0000ff);
Init();
}
SCA_KeyboardSensor::~SCA_KeyboardSensor()
{
}
void SCA_KeyboardSensor::Init()
{
// this function is used when the sensor is disconnected from all controllers
// by the state engine. It reinitializes the sensor as if it was just created.
// However, if the target key is pressed when the sensor is reactivated, it
// will not generated an event (see remark in Evaluate()).
m_val = (m_invert)?1:0;
m_reset = true;
}
CValue* SCA_KeyboardSensor::GetReplica()
{
SCA_KeyboardSensor* replica = new SCA_KeyboardSensor(*this);
// this will copy properties and so on...
CValue::AddDataToReplica(replica);
replica->Init();
return replica;
}
short int SCA_KeyboardSensor::GetHotkey()
{
return m_hotkey;
}
bool SCA_KeyboardSensor::IsPositiveTrigger()
{
bool result = (m_val != 0);
if (m_invert)
result = !result;
return result;
}
bool SCA_KeyboardSensor::TriggerOnAllKeys()
{
return m_bAllKeys;
}
bool SCA_KeyboardSensor::Evaluate(CValue* eventval)
{
bool result = false;
bool reset = m_reset && m_level;
SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
// cerr << "SCA_KeyboardSensor::Eval event, sensing for "<< m_hotkey << " at device " << inputdev << "\n";
/* See if we need to do logging: togPropState exists and is
* different from 0 */
CValue* myparent = GetParent();
CValue* togPropState = myparent->GetProperty(m_toggleprop);
if (togPropState &&
(((int)togPropState->GetNumber()) != 0) )
{
LogKeystrokes();
}
m_reset = false;
/* Now see whether events must be bounced. */
if (m_bAllKeys)
{
bool justactivated = false;
bool justreleased = false;
bool active = false;
for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++)
{
const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
switch (inevent.m_status)
{
case SCA_InputEvent::KX_JUSTACTIVATED:
justactivated = true;
break;
case SCA_InputEvent::KX_JUSTRELEASED:
justreleased = true;
break;
case SCA_InputEvent::KX_ACTIVE:
active = true;
break;
}
}
if (justactivated)
{
m_val=1;
result = true;
} else
{
if (justreleased)
{
m_val=(active)?1:0;
result = true;
} else
{
if (active)
{
if (m_val == 0)
{
m_val = 1;
if (m_level) {
result = true;
}
}
} else
{
if (m_val == 1)
{
m_val = 0;
result = true;
}
}
}
}
} else
{
// cerr << "======= SCA_KeyboardSensor::Evaluate:: peeking at key status" << endl;
const SCA_InputEvent & inevent = inputdev->GetEventValue(
(SCA_IInputDevice::KX_EnumInputs) m_hotkey);
// cerr << "======= SCA_KeyboardSensor::Evaluate:: status: " << inevent.m_status << endl;
if (inevent.m_status == SCA_InputEvent::KX_NO_INPUTSTATUS)
{
if (m_val == 1)
{
// this situation may occur after a scene suspend: the keyboard release
// event was not captured, produce now the event off
m_val = 0;
result = true;
}
} else
{
if (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED)
{
m_val=1;
result = true;
} else
{
if (inevent.m_status == SCA_InputEvent::KX_JUSTRELEASED)
{
m_val = 0;
result = true;
} else
{
if (inevent.m_status == SCA_InputEvent::KX_ACTIVE)
{
if (m_val == 0)
{
m_val = 1;
if (m_level)
{
result = true;
}
}
}
}
}
}
}
if (reset)
// force an event
result = true;
return result;
}
void SCA_KeyboardSensor::AddToTargetProp(int keyIndex)
{
if (IsPrintable(keyIndex)) {
CValue* tprop = GetParent()->GetProperty(m_targetprop);
if (tprop) {
/* overwrite the old property */
if (IsDelete(keyIndex)) {
/* strip one char, if possible */
STR_String newprop = tprop->GetText();
int oldlength = newprop.Length();
if (oldlength >= 1 ) {
newprop.SetLength(oldlength - 1);
CStringValue * newstringprop = new CStringValue(newprop, m_targetprop);
GetParent()->SetProperty(m_targetprop, newstringprop);
newstringprop->Release();
}
} else {
/* append */
char pchar = ToCharacter(keyIndex, IsShifted());
STR_String newprop = tprop->GetText() + pchar;
CStringValue * newstringprop = new CStringValue(newprop, m_targetprop);
GetParent()->SetProperty(m_targetprop, newstringprop);
newstringprop->Release();
}
} else {
if (!IsDelete(keyIndex)) {
/* Make a new property. Deletes can be ignored. */
char pchar = ToCharacter(keyIndex, IsShifted());
STR_String newprop = pchar;
CStringValue * newstringprop = new CStringValue(newprop, m_targetprop);
GetParent()->SetProperty(m_targetprop, newstringprop);
newstringprop->Release();
}
}
}
}
/**
* Determine whether this character can be printed. We cannot use
* the library functions here, because we need to test our own
* keycodes. */
bool SCA_KeyboardSensor::IsPrintable(int keyIndex)
{
/* only print
* - numerals: KX_ZEROKEY to KX_NINEKEY
* - alphas: KX_AKEY to KX_ZKEY.
* - specials: KX_RETKEY, KX_PADASTERKEY, KX_PADCOMMAKEY to KX_PERIODKEY,
* KX_TABKEY , KX_SEMICOLONKEY to KX_RIGHTBRACKETKEY,
* KX_PAD2 to KX_PADPLUSKEY
* - delete and backspace: also printable in the sense that they modify
* the string
* - retkey: should this be printable?
* - virgule: prints a space... don't know which key that's supposed
* to be...
*/
if ( ((keyIndex >= SCA_IInputDevice::KX_ZEROKEY)
&& (keyIndex <= SCA_IInputDevice::KX_NINEKEY))
|| ((keyIndex >= SCA_IInputDevice::KX_AKEY)
&& (keyIndex <= SCA_IInputDevice::KX_ZKEY))
|| (keyIndex == SCA_IInputDevice::KX_SPACEKEY)
/* || (keyIndex == KX_RETKEY) */
|| (keyIndex == SCA_IInputDevice::KX_PADASTERKEY)
|| (keyIndex == SCA_IInputDevice::KX_TABKEY)
|| ((keyIndex >= SCA_IInputDevice::KX_COMMAKEY)
&& (keyIndex <= SCA_IInputDevice::KX_PERIODKEY))
|| ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY)
&& (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY))
|| ((keyIndex >= SCA_IInputDevice::KX_PAD2)
&& (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY))
|| (keyIndex == SCA_IInputDevice::KX_DELKEY)
|| (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY)
)
{
return true;
} else {
return false;
}
}
// this code looks ugly, please use an ordinary hashtable
char SCA_KeyboardSensor::ToCharacter(int keyIndex, bool shifted)
{
/* numerals */
if ( (keyIndex >= SCA_IInputDevice::KX_ZEROKEY)
&& (keyIndex <= SCA_IInputDevice::KX_NINEKEY) ) {
if (shifted) {
char numshift[] = ")!@#$%^&*(";
return numshift[keyIndex - '0'];
} else {
return keyIndex - SCA_IInputDevice::KX_ZEROKEY + '0';
}
}
/* letters... always lowercase... is that desirable? */
if ( (keyIndex >= SCA_IInputDevice::KX_AKEY)
&& (keyIndex <= SCA_IInputDevice::KX_ZKEY) ) {
if (shifted) {
return keyIndex - SCA_IInputDevice::KX_AKEY + 'A';
} else {
return keyIndex - SCA_IInputDevice::KX_AKEY + 'a';
}
}
if (keyIndex == SCA_IInputDevice::KX_SPACEKEY) {
return ' ';
}
/* || (keyIndex == SCA_IInputDevice::KX_RETKEY) */
if (keyIndex == SCA_IInputDevice::KX_PADASTERKEY) {
return '*';
}
if (keyIndex == SCA_IInputDevice::KX_TABKEY) {
return '\t';
}
/* comma to period */
char commatoperiod[] = ",-.";
char commatoperiodshifted[] = "<_>";
if (keyIndex == SCA_IInputDevice::KX_COMMAKEY) {
if (shifted) {
return commatoperiodshifted[0];
} else {
return commatoperiod[0];
}
}
if (keyIndex == SCA_IInputDevice::KX_MINUSKEY) {
if (shifted) {
return commatoperiodshifted[1];
} else {
return commatoperiod[1];
}
}
if (keyIndex == SCA_IInputDevice::KX_PERIODKEY) {
if (shifted) {
return commatoperiodshifted[2];
} else {
return commatoperiod[2];
}
}
/* semicolon to rightbracket */
char semicolontorightbracket[] = ";\'` /\\=[]";
char semicolontorightbracketshifted[] = ":\"~ \?|+{}";
if ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY)
&& (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY)) {
if (shifted) {
return semicolontorightbracketshifted[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY];
} else {
return semicolontorightbracket[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY];
}
}
/* keypad2 to padplus */
char pad2topadplus[] = "246813579. 0- +";
if ((keyIndex >= SCA_IInputDevice::KX_PAD2)
&& (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY)) {
return pad2topadplus[keyIndex - SCA_IInputDevice::KX_PAD2];
}
return '!';
}
/**
* Tests whether this is a delete key.
*/
bool SCA_KeyboardSensor::IsDelete(int keyIndex)
{
if ( (keyIndex == SCA_IInputDevice::KX_DELKEY)
|| (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY) ) {
return true;
} else {
return false;
}
}
/**
* Tests whether shift is pressed
*/
bool SCA_KeyboardSensor::IsShifted(void)
{
SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
if ( (inputdev->GetEventValue(SCA_IInputDevice::KX_RIGHTSHIFTKEY).m_status
== SCA_InputEvent::KX_ACTIVE)
|| (inputdev->GetEventValue(SCA_IInputDevice::KX_RIGHTSHIFTKEY).m_status
== SCA_InputEvent::KX_JUSTACTIVATED)
|| (inputdev->GetEventValue(SCA_IInputDevice::KX_LEFTSHIFTKEY).m_status
== SCA_InputEvent::KX_ACTIVE)
|| (inputdev->GetEventValue(SCA_IInputDevice::KX_LEFTSHIFTKEY).m_status
== SCA_InputEvent::KX_JUSTACTIVATED)
) {
return true;
} else {
return false;
}
}
void SCA_KeyboardSensor::LogKeystrokes(void)
{
SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
int num = inputdev->GetNumActiveEvents();
/* weird loop, this one... */
if (num > 0)
{
int index = 0;
/* Check on all keys whether they were pushed. This does not
* untangle the ordering, so don't type too fast :) */
for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++)
{
const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
if (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED) //NO_INPUTSTATUS)
{
if (index < num)
{
AddToTargetProp(i);
index++;
}
}
}
}
}
/* ------------------------------------------------------------------------- */
/* Python functions : specific */
/* ------------------------------------------------------------------------- */
PyObject* SCA_KeyboardSensor::PySetAllMode(PyObject* self,
PyObject* args,
PyObject* kwds)
{
bool allkeys;
if (!PyArg_ParseTuple(args, "i", &allkeys))
{
return NULL;
}
m_bAllKeys = allkeys;
Py_Return
}
PyObject* SCA_KeyboardSensor::sPySetAllMode(PyObject* self,
PyObject* args,
PyObject* kwds)
{
// printf("sPyIsPositive\n");
return ((SCA_KeyboardSensor*) self)->PyIsPositive(self, args, kwds);
}
/** 1. GetKey : check which key this sensor looks at */
char SCA_KeyboardSensor::GetKey_doc[] =
"getKey()\n"
"\tReturn the code of the key this sensor is listening to.\n" ;
PyObject* SCA_KeyboardSensor::PyGetKey(PyObject* self, PyObject* args, PyObject* kwds)
{
return PyInt_FromLong(m_hotkey);
}
/** 2. SetKey: change the key to look at */
char SCA_KeyboardSensor::SetKey_doc[] =
"setKey(keycode)\n"
"\t- keycode: any code from GameKeys\n"
"\tSet the key this sensor should listen to.\n" ;
PyObject* SCA_KeyboardSensor::PySetKey(PyObject* self, PyObject* args, PyObject* kwds)
{
int keyCode;
if(!PyArg_ParseTuple(args, "i", &keyCode)) {
return NULL;
}
/* Since we have symbolic constants for this in Python, we don't guard */
/* anything. It's up to the user to provide a sensible number. */
m_hotkey = keyCode;
Py_Return;
}
/** 3. GetHold1 : set the first bucky bit */
char SCA_KeyboardSensor::GetHold1_doc[] =
"getHold1()\n"
"\tReturn the code of the first key modifier to the key this \n"
"\tsensor is listening to.\n" ;
PyObject* SCA_KeyboardSensor::PyGetHold1(PyObject* self, PyObject* args, PyObject* kwds)
{
return PyInt_FromLong(m_qual);
}
/** 4. SetHold1: change the first bucky bit */
char SCA_KeyboardSensor::SetHold1_doc[] =
"setHold1(keycode)\n"
"\t- keycode: any code from GameKeys\n"
"\tSet the first modifier to the key this sensor should listen to.\n" ;
PyObject* SCA_KeyboardSensor::PySetHold1(PyObject* self, PyObject* args, PyObject* kwds)
{
int keyCode;
if(!PyArg_ParseTuple(args, "i", &keyCode)) {
return NULL;
}
/* Since we have symbolic constants for this in Python, we don't guard */
/* anything. It's up to the user to provide a sensible number. */
m_qual = keyCode;
Py_Return;
}
/** 5. GetHold2 : get the second bucky bit */
char SCA_KeyboardSensor::GetHold2_doc[] =
"getHold2()\n"
"\tReturn the code of the second key modifier to the key this \n"
"\tsensor is listening to.\n" ;
PyObject* SCA_KeyboardSensor::PyGetHold2(PyObject* self, PyObject* args, PyObject* kwds)
{
return PyInt_FromLong(m_qual2);
}
/** 6. SetHold2: change the second bucky bit */
char SCA_KeyboardSensor::SetHold2_doc[] =
"setHold2(keycode)\n"
"\t- keycode: any code from GameKeys\n"
"\tSet the first modifier to the key this sensor should listen to.\n" ;
PyObject* SCA_KeyboardSensor::PySetHold2(PyObject* self, PyObject* args, PyObject* kwds)
{
int keyCode;
if(!PyArg_ParseTuple(args, "i", &keyCode)) {
return NULL;
}
/* Since we have symbolic constants for this in Python, we don't guard */
/* anything. It's up to the user to provide a sensible number. */
m_qual2 = keyCode;
Py_Return;
}
char SCA_KeyboardSensor::GetPressedKeys_doc[] =
"getPressedKeys()\n"
"\tGet a list of pressed keys that have either been pressed, or just released this frame.\n" ;
PyObject* SCA_KeyboardSensor::PyGetPressedKeys(PyObject* self, PyObject* args, PyObject* kwds)
{
SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
int num = inputdev->GetNumJustEvents();
PyObject* resultlist = PyList_New(num);
if (num > 0)
{
int index = 0;
for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++)
{
const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
if ((inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED)
|| (inevent.m_status == SCA_InputEvent::KX_JUSTRELEASED))
{
if (index < num)
{
PyObject* keypair = PyList_New(2);
PyList_SetItem(keypair,0,PyInt_FromLong(i));
PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status));
PyList_SetItem(resultlist,index,keypair);
index++;
}
}
}
if (index>0) return resultlist;
}
Py_Return;
}
char SCA_KeyboardSensor::GetCurrentlyPressedKeys_doc[] =
"getCurrentlyPressedKeys()\n"
"\tGet a list of keys that are currently pressed.\n" ;
PyObject* SCA_KeyboardSensor::PyGetCurrentlyPressedKeys(PyObject* self, PyObject* args, PyObject* kwds)
{
SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
int num = inputdev->GetNumActiveEvents();
PyObject* resultlist = PyList_New(num);
if (num > 0)
{
int index = 0;
for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++)
{
const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i);
if ( (inevent.m_status == SCA_InputEvent::KX_ACTIVE)
|| (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED))
{
if (index < num)
{
PyObject* keypair = PyList_New(2);
PyList_SetItem(keypair,0,PyInt_FromLong(i));
PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status));
PyList_SetItem(resultlist,index,keypair);
index++;
}
}
}
/* why?*/
if (index > 0) return resultlist;
}
Py_Return;
}
/* ------------------------------------------------------------------------- */
/* Python functions : integration hooks */
/* ------------------------------------------------------------------------- */
PyTypeObject SCA_KeyboardSensor::Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"SCA_KeyboardSensor",
sizeof(SCA_KeyboardSensor),
0,
PyDestructor,
0,
__getattr,
__setattr,
0, //&MyPyCompare,
__repr,
0, //&cvalue_as_number,
0,
0,
0,
0
};
PyParentObject SCA_KeyboardSensor::Parents[] = {
&SCA_KeyboardSensor::Type,
&SCA_ISensor::Type,
&SCA_ILogicBrick::Type,
&CValue::Type,
NULL
};
PyMethodDef SCA_KeyboardSensor::Methods[] = {
{"getKey", (PyCFunction) SCA_KeyboardSensor::sPyGetKey, METH_VARARGS, GetKey_doc},
{"setKey", (PyCFunction) SCA_KeyboardSensor::sPySetKey, METH_VARARGS, SetKey_doc},
{"getHold1", (PyCFunction) SCA_KeyboardSensor::sPyGetHold1, METH_VARARGS, GetHold1_doc},
{"setHold1", (PyCFunction) SCA_KeyboardSensor::sPySetHold1, METH_VARARGS, SetHold1_doc},
{"getHold2", (PyCFunction) SCA_KeyboardSensor::sPyGetHold2, METH_VARARGS, GetHold2_doc},
{"setHold2", (PyCFunction) SCA_KeyboardSensor::sPySetHold2, METH_VARARGS, SetHold2_doc},
// {"getUseAllKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetUseAllKeys, METH_VARARGS, GetUseAllKeys_doc},
// {"setUseAllKeys", (PyCFunction) SCA_KeyboardSensor::sPySetUseAllKeys, METH_VARARGS, SetUseAllKeys_doc},
{"getPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetPressedKeys, METH_VARARGS, GetPressedKeys_doc},
{"getCurrentlyPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetCurrentlyPressedKeys, METH_VARARGS, GetCurrentlyPressedKeys_doc},
// {"getKeyEvents", (PyCFunction) SCA_KeyboardSensor::sPyGetKeyEvents, METH_VARARGS, GetKeyEvents_doc},
{NULL,NULL} //Sentinel
};
PyObject*
SCA_KeyboardSensor::_getattr(const STR_String& attr)
{
_getattr_up(SCA_ISensor);
}