[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [28240] trunk/blender/source/gameengine: Patch #21789 - BGE Keyboard and Mouse Python types - by Mitchell Stokes( Moguri)

Dalai Felinto dfelinto at gmail.com
Sat Apr 17 08:52:14 CEST 2010


Revision: 28240
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=28240
Author:   dfelinto
Date:     2010-04-17 08:52:14 +0200 (Sat, 17 Apr 2010)

Log Message:
-----------
Patch #21789 - BGE Keyboard and Mouse Python types - by Mitchell Stokes(Moguri)
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.

Modified Paths:
--------------
    trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderCanvas.cpp
    trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Canvas.cpp
    trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PythonInitTypes.cpp
    trunk/blender/source/gameengine/PyDoc/GameKeys.py
    trunk/blender/source/gameengine/PyDoc/GameLogic.py
    trunk/blender/source/gameengine/PyDoc/GameTypes.py
    trunk/blender/source/gameengine/Rasterizer/RAS_ICanvas.h

Added Paths:
-----------
    trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.h
    trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.h

Modified: trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderCanvas.cpp
===================================================================
--- trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderCanvas.cpp	2010-04-16 23:58:12 UTC (rev 28239)
+++ trunk/blender/source/gameengine/BlenderRoutines/KX_BlenderCanvas.cpp	2010-04-17 06:52:14 UTC (rev 28240)
@@ -139,6 +139,8 @@
 
 void KX_BlenderCanvas::SetMouseState(RAS_MouseState mousestate)
 {
+	m_mousestate = mousestate;
+
 	switch (mousestate)
 	{
 	case MOUSE_INVISIBLE:

Added: trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp	                        (rev 0)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp	2010-04-17 06:52:14 UTC (rev 28240)
@@ -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

Added: trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.h	                        (rev 0)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.h	2010-04-17 06:52:14 UTC (rev 28240)
@@ -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
+

Added: trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.h	                        (rev 0)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.h	2010-04-17 06:52:14 UTC (rev 28240)
@@ -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
+

Modified: trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Canvas.cpp
===================================================================
--- trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Canvas.cpp	2010-04-16 23:58:12 UTC (rev 28239)
+++ trunk/blender/source/gameengine/GamePlayer/ghost/GPG_Canvas.cpp	2010-04-17 06:52:14 UTC (rev 28240)
@@ -79,6 +79,8 @@
 
 void GPG_Canvas::SetMouseState(RAS_MouseState mousestate)
 {
+	m_mousestate = mousestate;
+
 	if (m_window)
 	{
 		switch (mousestate)

Modified: trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h	2010-04-16 23:58:12 UTC (rev 28239)
+++ trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h	2010-04-17 06:52:14 UTC (rev 28240)
@@ -213,6 +213,8 @@
 	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); 

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2010-04-16 23:58:12 UTC (rev 28239)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2010-04-17 06:52:14 UTC (rev 28240)
@@ -73,6 +73,8 @@
 #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 @@
 	
 	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());

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list