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)
commit795b438bf5565a87765edb880318b7e241714270
treec76292cfa5e11b1eab41484342fb04b5e9fb2a06
parent6562e6a4d1c12166b53322de131ed65867eb026d
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.
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