Patch #21789 - BGE Keyboard and Mouse Python types - by Mitchell Stokes(Moguri)
authorDalai Felinto <dfelinto@gmail.com>
Sat, 17 Apr 2010 06:52:14 +0000 (06:52 +0000)
committerDalai Felinto <dfelinto@gmail.com>
Sat, 17 Apr 2010 06:52:14 +0000 (06:52 +0000)
The patch exposes mouse and keyboard read-only properties in the GameLogic module
Also renames bge.keys to bge.events (* Note: name of bge submodules (logic, render, ...) may change before 2.5 final release [right Campbell?]).

"""
This patch adds two new types to the BGE:
SCA_PythonKeyboard
SCA_PythonMouse

These two types allow users to make use of the keyboard and mouse without the need for a keyboard or mouse sensor.

SCA_PythonKeyboard has an events property that acts just like SCA_KeyboardSensor.events.

SCA_PythonMouse also has an events property to check for mouse events. Further more it supports getting and setting normalized cursor position (from 0.0 to 1.0) with SCA_PythonMouse.position. The cursor can be shown/hidden using SCA_PythonMouse.visible.
"""

Its use is similar with current mouse and keyboard controllers. With the exception of mouse position being normalized and writable as well (replacing Rasterizer.setMousePosition).

Code Sample:
######
from bge import logic, events

mouse = logic.mouse
keyboard = logic.keyboard

for key,status in keyboard.events:
    if status == logic.KX_INPUT_JUST_ACTIVATED:
        if key == events.WKEY:
            print(mouse.position)
            # move_forward()

mouse.visible = True # turn cursor visible
mouse.position = 0.5,0.5  # centralize mouse - use tuple
######

* Important Note: mouse.position still will not work properly for Letterbox mode.
In order to fix letterboxing I may need to move the set x,y mouse function to inside the canvas code (to avoid duplicated code between mouse sensor and bge.logic.mouse). I'll leave this for another commit though.
Thanks Mitchell for the work on that.

13 files changed:
source/gameengine/BlenderRoutines/KX_BlenderCanvas.cpp
source/gameengine/GameLogic/SCA_PythonKeyboard.cpp [new file with mode: 0644]
source/gameengine/GameLogic/SCA_PythonKeyboard.h [new file with mode: 0644]
source/gameengine/GameLogic/SCA_PythonMouse.cpp [new file with mode: 0644]
source/gameengine/GameLogic/SCA_PythonMouse.h [new file with mode: 0644]
source/gameengine/GamePlayer/ghost/GPG_Canvas.cpp
source/gameengine/Ketsji/KX_KetsjiEngine.h
source/gameengine/Ketsji/KX_PythonInit.cpp
source/gameengine/Ketsji/KX_PythonInitTypes.cpp
source/gameengine/PyDoc/GameKeys.py
source/gameengine/PyDoc/GameLogic.py
source/gameengine/PyDoc/GameTypes.py
source/gameengine/Rasterizer/RAS_ICanvas.h

index 9139d6ea7294c7027293b40ff12a46c52f3fe8b5..d563a17fe06dadc6beb24b9bcb93f71105946b2e 100644 (file)
@@ -139,6 +139,8 @@ SetViewPort(
 
 void KX_BlenderCanvas::SetMouseState(RAS_MouseState mousestate)
 {
+       m_mousestate = mousestate;
+
        switch (mousestate)
        {
        case MOUSE_INVISIBLE:
diff --git a/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp b/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp
new file mode 100644 (file)
index 0000000..ef6d2ad
--- /dev/null
@@ -0,0 +1,115 @@
+/**
+ * Python Keyboard Object
+ *
+ * $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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 *****
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "SCA_PythonKeyboard.h"
+#include "SCA_IInputDevice.h"
+
+/* ------------------------------------------------------------------------- */
+/* Native functions                                                          */
+/* ------------------------------------------------------------------------- */
+
+SCA_PythonKeyboard::SCA_PythonKeyboard(SCA_IInputDevice* keyboard)
+: PyObjectPlus(),
+m_keyboard(keyboard)
+{
+}
+
+SCA_PythonKeyboard::~SCA_PythonKeyboard()
+{
+       /* intentionally empty */
+}
+
+#ifndef DISABLE_PYTHON
+
+/* ------------------------------------------------------------------------- */
+/* Python functions                                                          */
+/* ------------------------------------------------------------------------- */
+
+/* Integration hooks ------------------------------------------------------- */
+PyTypeObject SCA_PythonKeyboard::Type = {
+       PyVarObject_HEAD_INIT(NULL, 0)
+       "SCA_PythonKeyboard",
+       sizeof(PyObjectPlus_Proxy),
+       0,
+       py_base_dealloc,
+       0,
+       0,
+       0,
+       0,
+       py_base_repr,
+       0,0,0,0,0,0,0,0,0,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
+       &PyObjectPlus::Type,
+       0,0,0,0,0,0,
+       py_base_new
+};
+
+PyMethodDef SCA_PythonKeyboard::Methods[] = {
+       {NULL,NULL} //Sentinel
+};
+
+PyAttributeDef SCA_PythonKeyboard::Attributes[] = {
+       KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_PythonKeyboard, pyattr_get_events),
+       { NULL }        //Sentinel
+};
+
+PyObject* SCA_PythonKeyboard::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       SCA_PythonKeyboard* self = static_cast<SCA_PythonKeyboard*>(self_v);
+
+       PyObject* resultlist = PyList_New(0);
+       
+       for (int i=SCA_IInputDevice::KX_BEGINKEY; i<=SCA_IInputDevice::KX_ENDKEY; i++)
+       {
+               const SCA_InputEvent & inevent = self->m_keyboard->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
+               
+               
+               if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
+               {
+                       PyObject* keypair = PyList_New(2);
+                       PyList_SET_ITEM(keypair, 0, PyLong_FromSsize_t(i));
+                       PyList_SET_ITEM(keypair, 1, PyLong_FromSsize_t(inevent.m_status));
+                       PyList_Append(resultlist, keypair);
+               }
+       }
+
+       return resultlist;
+}
+
+#endif
diff --git a/source/gameengine/GameLogic/SCA_PythonKeyboard.h b/source/gameengine/GameLogic/SCA_PythonKeyboard.h
new file mode 100644 (file)
index 0000000..4c178d6
--- /dev/null
@@ -0,0 +1,51 @@
+/**
+ * SCA_PythonKeyboard.h
+ *
+ * $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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 *****
+ */
+
+#ifndef __KX_PYKEYBOARD
+#define __KX_PYKEYBOARD
+
+#include "PyObjectPlus.h"
+
+class SCA_PythonKeyboard : public PyObjectPlus
+{
+       Py_Header;
+       class SCA_IInputDevice *m_keyboard;
+public:
+       SCA_PythonKeyboard(class SCA_IInputDevice* keyboard);
+       virtual ~SCA_PythonKeyboard();
+
+#ifndef DISABLE_PYTHON
+       static PyObject*        pyattr_get_events(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+#endif
+};
+
+#endif //__KX_PYKEYBOARD
+
diff --git a/source/gameengine/GameLogic/SCA_PythonMouse.cpp b/source/gameengine/GameLogic/SCA_PythonMouse.cpp
new file mode 100644 (file)
index 0000000..405c411
--- /dev/null
@@ -0,0 +1,185 @@
+/**
+ * Python Mouse Object
+ *
+ * $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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 *****
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "SCA_PythonMouse.h"
+#include "SCA_IInputDevice.h"
+#include "RAS_ICanvas.h"
+
+/* ------------------------------------------------------------------------- */
+/* Native functions                                                          */
+/* ------------------------------------------------------------------------- */
+
+SCA_PythonMouse::SCA_PythonMouse(SCA_IInputDevice* mouse, RAS_ICanvas* canvas)
+: PyObjectPlus(),
+m_canvas(canvas),
+m_mouse(mouse)
+{
+}
+
+SCA_PythonMouse::~SCA_PythonMouse()
+{
+       /* intentionally empty */
+}
+
+#ifndef DISABLE_PYTHON
+
+/* ------------------------------------------------------------------------- */
+/* Python functions                                                          */
+/* ------------------------------------------------------------------------- */
+
+/* Integration hooks ------------------------------------------------------- */
+PyTypeObject SCA_PythonMouse::Type = {
+       PyVarObject_HEAD_INIT(NULL, 0)
+       "SCA_PythonMouse",
+       sizeof(PyObjectPlus_Proxy),
+       0,
+       py_base_dealloc,
+       0,
+       0,
+       0,
+       0,
+       py_base_repr,
+       0,0,0,0,0,0,0,0,0,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
+       &PyObjectPlus::Type,
+       0,0,0,0,0,0,
+       py_base_new
+};
+
+PyMethodDef SCA_PythonMouse::Methods[] = {
+//     KX_PYMETHODTABLE(SCA_PythonMouse, show),
+       {NULL,NULL} //Sentinel
+};
+
+PyAttributeDef SCA_PythonMouse::Attributes[] = {
+       KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_PythonMouse, pyattr_get_events),
+       KX_PYATTRIBUTE_RW_FUNCTION("position", SCA_PythonMouse, pyattr_get_position, pyattr_set_position),
+       KX_PYATTRIBUTE_RW_FUNCTION("visible", SCA_PythonMouse, pyattr_get_visible, pyattr_set_visible),
+       { NULL }        //Sentinel
+};     
+
+PyObject* SCA_PythonMouse::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
+
+       PyObject* resultlist = PyList_New(0);
+
+       for (int i=SCA_IInputDevice::KX_BEGINMOUSE; i<=SCA_IInputDevice::KX_ENDMOUSE; i++)
+       {
+               const SCA_InputEvent & inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
+               
+               
+               if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
+               {
+                       PyObject* keypair = PyList_New(2);
+                       PyList_SET_ITEM(keypair, 0, PyLong_FromSsize_t(i));
+                       PyList_SET_ITEM(keypair, 1, PyLong_FromSsize_t(inevent.m_status));
+                       PyList_Append(resultlist, keypair);
+               }
+       }
+
+       return resultlist;
+}
+
+PyObject* SCA_PythonMouse::pyattr_get_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
+       const SCA_InputEvent & xevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
+       const SCA_InputEvent & yevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
+
+       PyObject* resultlist = PyList_New(2);
+
+       PyList_SET_ITEM(resultlist, 0, PyFloat_FromDouble(float(xevent.m_eventval)/self->m_canvas->GetWidth()));
+       
+       PyList_SET_ITEM(resultlist, 1, PyFloat_FromDouble(float(yevent.m_eventval)/self->m_canvas->GetHeight()));
+
+       return resultlist;
+}
+
+int SCA_PythonMouse::pyattr_set_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
+       int x, y;
+       float pyx, pyy;
+       if (!PyArg_ParseTuple(value, "ff:position", &pyx, &pyy))
+               return PY_SET_ATTR_FAIL;
+
+       x = (int)(pyx*self->m_canvas->GetWidth());
+       y = (int)(pyy*self->m_canvas->GetHeight());
+
+       self->m_canvas->SetMousePosition(x, y);
+
+       return PY_SET_ATTR_SUCCESS;
+}
+
+PyObject* SCA_PythonMouse::pyattr_get_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
+
+       int visible;
+
+       if (self->m_canvas->GetMouseState() == RAS_ICanvas::MOUSE_INVISIBLE)
+               visible = 0;
+       else
+               visible = 1;
+
+       return PyBool_FromLong(visible);
+}
+
+int SCA_PythonMouse::pyattr_set_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
+{
+       SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
+
+       int visible = PyObject_IsTrue(value);
+
+       if (visible == -1)
+       {
+               PyErr_SetString(PyExc_AttributeError, "SCA_PythonMouse.visible = bool: SCA_PythonMouse, expected True or False");
+               return PY_SET_ATTR_FAIL;
+       }
+
+       if (visible)
+               self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
+       else
+               self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
+
+       return PY_SET_ATTR_SUCCESS;
+}
+
+#endif
diff --git a/source/gameengine/GameLogic/SCA_PythonMouse.h b/source/gameengine/GameLogic/SCA_PythonMouse.h
new file mode 100644 (file)
index 0000000..b3ce107
--- /dev/null
@@ -0,0 +1,60 @@
+/**
+ * SCA_PythonMouse.h
+ *
+ * $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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 *****
+ */
+
+#ifndef __KX_PYMOUSE
+#define __KX_PYMOUSE
+
+#include "PyObjectPlus.h"
+
+class SCA_PythonMouse : public PyObjectPlus
+{
+       Py_Header;
+       class SCA_IInputDevice *m_mouse;
+       class RAS_ICanvas *m_canvas;
+public:
+       SCA_PythonMouse(class SCA_IInputDevice* mouse, class RAS_ICanvas* canvas);
+       virtual ~SCA_PythonMouse();
+
+       void Show(bool visible);
+
+#ifndef DISABLE_PYTHON
+       KX_PYMETHOD_DOC(SCA_PythonMouse, show);
+
+       static PyObject*        pyattr_get_events(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static PyObject*        pyattr_get_position(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_position(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject* value);
+       static PyObject*        pyattr_get_visible(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_visible(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject* value);
+#endif
+};
+
+#endif //__KX_PYMOUSE
+
index 0a96fbbe50375c32ca8439a9e4a895581fce4988..4eb9a4cfcd78b7156dc6758a44a37b475c402fae 100644 (file)
@@ -79,6 +79,8 @@ void GPG_Canvas::SetMousePosition(int x, int y)
 
 void GPG_Canvas::SetMouseState(RAS_MouseState mousestate)
 {
+       m_mousestate = mousestate;
+
        if (m_window)
        {
                switch (mousestate)
index 36db8685afbe86e5fb86aa622f2816fb14e198a4..b3549c5fdabebc896ce3e03164a8bf4f49d82d06 100644 (file)
@@ -213,6 +213,8 @@ public:
        RAS_IRasterizer*                GetRasterizer(){return m_rasterizer;};
        RAS_ICanvas*                GetCanvas(){return m_canvas;};
        RAS_IRenderTools*           GetRenderTools(){return m_rendertools;};
+       SCA_IInputDevice*               GetKeyboardDevice(){return m_keyboarddevice;};
+       SCA_IInputDevice*               GetMouseDevice(){return m_mousedevice;};
 
        /// Dome functions
        void                    InitDome(short res, short mode, short angle, float resbuf, short tilt, struct Text* text); 
index 082f802010df7ff3028cd4e43eb244746f82be34..675cf3c09f6d046f412e1c1bfdb5477c91057ddf 100644 (file)
@@ -73,6 +73,8 @@ extern "C" {
 #include "SCA_PropertySensor.h"
 #include "SCA_RandomActuator.h"
 #include "SCA_KeyboardSensor.h" /* IsPrintable, ToCharacter */
+#include "SCA_PythonKeyboard.h"
+#include "SCA_PythonMouse.h"
 #include "KX_ConstraintActuator.h"
 #include "KX_IpoActuator.h"
 #include "KX_SoundActuator.h"
@@ -1280,6 +1282,13 @@ PyObject* initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack
        
        PyDict_SetItemString(d, "globalDict", item=PyDict_New()); Py_DECREF(item);
 
+       // Add keyboard and mouse attributes to this module
+       SCA_PythonKeyboard* pykeyb = new SCA_PythonKeyboard(gp_KetsjiEngine->GetKeyboardDevice());
+       PyDict_SetItemString(d, "keyboard", pykeyb->NewProxy(true));
+
+       SCA_PythonMouse* pymouse = new SCA_PythonMouse(gp_KetsjiEngine->GetMouseDevice(), gp_Canvas);
+       PyDict_SetItemString(d, "mouse", pymouse->NewProxy(true));
+
        ErrorObject = PyUnicode_FromString("GameLogic.error");
        PyDict_SetItemString(d, "error", ErrorObject);
        Py_DECREF(ErrorObject);
@@ -1978,7 +1987,7 @@ void setupGamePython(KX_KetsjiEngine* ketsjiengine, KX_Scene* startscene, Main *
 #endif
 
        /* could be done a lot more nicely, but for now a quick way to get bge.* working */
-       PyRun_SimpleString("__import__('sys').modules['bge']=[mod for mod in (type(__builtins__)('bge'), ) if mod.__dict__.update({'logic':__import__('GameLogic'), 'render':__import__('Rasterizer'), 'keys':__import__('GameKeys'), 'constraints':__import__('PhysicsConstraints'), 'types':__import__('GameTypes')}) is None][0]");
+       PyRun_SimpleString("__import__('sys').modules['bge']=[mod for mod in (type(__builtins__)('bge'), ) if mod.__dict__.update({'logic':__import__('GameLogic'), 'render':__import__('Rasterizer'), 'events':__import__('GameKeys'), 'constraints':__import__('PhysicsConstraints'), 'types':__import__('GameTypes')}) is None][0]");
 }
 
 static struct PyModuleDef Rasterizer_module_def = {
@@ -2257,6 +2266,15 @@ PyObject* initGameKeys()
        KX_MACRO_addTypesToDict(d, PAGEDOWNKEY, SCA_IInputDevice::KX_PAGEDOWNKEY);
        KX_MACRO_addTypesToDict(d, ENDKEY, SCA_IInputDevice::KX_ENDKEY);
 
+       // MOUSE
+       KX_MACRO_addTypesToDict(d, LEFTMOUSE, SCA_IInputDevice::KX_LEFTMOUSE);
+       KX_MACRO_addTypesToDict(d, MIDDLEMOUSE, SCA_IInputDevice::KX_MIDDLEMOUSE);
+       KX_MACRO_addTypesToDict(d, RIGHTMOUSE, SCA_IInputDevice::KX_RIGHTMOUSE);
+       KX_MACRO_addTypesToDict(d, WHEELUPMOUSE, SCA_IInputDevice::KX_WHEELUPMOUSE);
+       KX_MACRO_addTypesToDict(d, WHEELDOWNMOUSE, SCA_IInputDevice::KX_WHEELDOWNMOUSE);
+       KX_MACRO_addTypesToDict(d, MOUSEX, SCA_IInputDevice::KX_MOUSEX);
+       KX_MACRO_addTypesToDict(d, MOUSEY, SCA_IInputDevice::KX_MOUSEY);
+
        // Check for errors
        if (PyErr_Occurred())
     {
index 278e0236b2e15434f38ad7b1d253d5ca24e29d78..6b9d7a2cccf37f8a0c03ad5c24da13060de7ddc9 100644 (file)
@@ -79,6 +79,8 @@
 #include "SCA_RandomSensor.h"
 #include "SCA_XNORController.h"
 #include "SCA_XORController.h"
+#include "SCA_PythonKeyboard.h"
+#include "SCA_PythonMouse.h"
 #include "KX_IpoActuator.h"
 #include "KX_NearSensor.h"
 #include "KX_RadarSensor.h"
@@ -239,6 +241,8 @@ void initPyTypes(void)
                PyType_Ready_Attr(dict, SCA_XNORController, init_getset);
                PyType_Ready_Attr(dict, SCA_XORController, init_getset);
                PyType_Ready_Attr(dict, SCA_IController, init_getset);
+               PyType_Ready_Attr(dict, SCA_PythonKeyboard, init_getset);
+               PyType_Ready_Attr(dict, SCA_PythonMouse, init_getset);
        }
 
 
index e798f6c901b7a595a6e4b482973c3bffc01610fa..8e5c7d3ae2485ad2968053a183945293c41e67fe 100644 (file)
@@ -150,6 +150,15 @@ Example::
 @var SPACEKEY:
 @var TABKEY:
 
+@group Mouse Events: LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE, WHEELUPMOUSE, WHEELDOWNMOUSE, MOUSEX, MOUSEY
+@var LEFTMOUSE:
+@var MIDDLEMOUSE:
+@var RIGHTMOUSE:
+@var WHEELUPMOUSE:
+@var WHEELDOWNMOUSE:
+@var MOUSEX:
+@var MOUSEY:
+
 """
 
 def EventToString(event):
index 56a9ea0d4f496d568de6fcbade562bbf580ecebd..4bef65e42b3cc9d82f1c08b889549f57ddd3abaf 100644 (file)
@@ -308,6 +308,9 @@ Documentation for the GameLogic Module.
                                        later on with the game load/save actuators.
                                        note: only python built in types such as int/string/bool/float/tuples/lists
                                        can be saved, GameObjects, Actuators etc will not work as expectred.
+                                       
+@var keyboard:         The current keyboard wrapped in an SCA_PythonKeyboard object.
+@var mouse:                    The current mouse wrapped in an SCA_PythonMouse object.
 """
 
 import GameTypes
index 839f228c3d5a4bcbdb1309eca14a415e2d3896c7..45d08d2e96a7df3c06a9f20a447d802dbdb77ab9 100644 (file)
@@ -108,6 +108,43 @@ class SCA_ILogicBrick(CValue):
                """
 #}
 
+class SCA_PythonKeyboard(PyObjectPlus)
+       """
+       The current keyboard.   
+       @ivar events: a list of pressed keys that have either been pressed, or just released, or are active this frame. (read-only).
+
+                       - 'keycode' matches the values in L{GameKeys}.
+                       - 'status' uses...
+                               - L{GameLogic.KX_INPUT_NONE}
+                               - L{GameLogic.KX_INPUT_JUST_ACTIVATED}
+                               - L{GameLogic.KX_INPUT_ACTIVE}
+                               - L{GameLogic.KX_INPUT_JUST_RELEASED}
+                       
+       @type events: list [[keycode, status], ...]
+       """
+       pass
+       
+class SCA_PythonMouse(PyObjectPlus)
+       """
+       The current mouse.
+       
+       @ivar events: a list of pressed buttons that have either been pressed, or just released, or are active this frame. (read-only).
+
+                       - 'keycode' matches the values in L{GameKeys}.
+                       - 'status' uses...
+                               - L{GameLogic.KX_INPUT_NONE}
+                               - L{GameLogic.KX_INPUT_JUST_ACTIVATED}
+                               - L{GameLogic.KX_INPUT_ACTIVE}
+                               - L{GameLogic.KX_INPUT_JUST_RELEASED}
+                       
+       @type events: list [[keycode, status], ...]
+       @ivar position: The normalized x and y position of the mouse cursor.
+       @type position: list [x, y]
+       @ivar visible: The visibility of the mouse cursor
+       @type visible: boolean
+       """
+       pass
+
 class SCA_IObject(CValue):
        """
        This class has no python functions
index 2ab06be26e7da4d437d85499d2d5c9af93ae05f9..0821e369f75757c5bf1643b4380728e5c8ac6769 100644 (file)
@@ -172,12 +172,21 @@ public:
                int y
        )=0;
 
+       virtual
+               RAS_MouseState
+       GetMouseState()
+       {
+               return m_mousestate;
+       }
+
        virtual 
                void 
        MakeScreenShot(
                const char* filename
        )=0;
        
+protected:
+       RAS_MouseState m_mousestate;
        
 #ifdef WITH_CXX_GUARDEDALLOC
 public: