~fireclawthefox/panda3dcodecollection/trunk

« back to all changes in this revision

Viewing changes to character/extended Char controller/controlPlugins/plugWiiRemote.py

  • Committer: Fireclaw the Fox
  • Date: 2017-02-08 14:26:21 UTC
  • Revision ID: fireclawthefox@gmail.com-20170208142621-ljorcvore5ivwutx
Added gamepad support

- Panda3D's integrated gamepad support (preview, needs panda3d binaries build from input-overhaul branch)
- Added pyglet gamepad sample
- enhanced graphics for the extended Char controller demo

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from panda3d.core import Vec3, ButtonHandle, loadPrcFileData
 
2
#loadPrcFileData("", "notify-level-device debug")
 
3
try:
 
4
    from panda3d.core import InputDevice
 
5
    gamepadsupport = True
 
6
except:
 
7
    gamepadsupport = False
 
8
 
 
9
class PluginWiiRemote():
 
10
    """This plugin provides gamepad support for wii remotes plus nunchuk"""
 
11
    def __init__(self, parent):
 
12
        if not gamepadsupport: return
 
13
        if not self.hasGamepad(): return
 
14
 
 
15
        self.parent = parent
 
16
        self.deviceMap = self.parent.deviceMap
 
17
        self.usedGamepadID = self.parent.usedGamepadID
 
18
        self.deadzone_x = self.parent.deadzone_x
 
19
        self.deadzone_y = self.parent.deadzone_y
 
20
 
 
21
        self.checkGamepads()
 
22
 
 
23
        # set the center position of the control sticks
 
24
        # NOTE: here we assume, that the wheel is centered when the application get started.
 
25
        #       In real world applications, you should notice the user and give him enough time
 
26
        #       to center the wheel until you store the center position of the controler!
 
27
        self.lxcenter = self.nunchuck.findControl(self.deviceMap["axis-left-x"]).state
 
28
        self.lycenter = self.nunchuck.findControl(self.deviceMap["axis-left-y"]).state
 
29
 
 
30
        self.sprintState = False
 
31
 
 
32
    def checkGamepads(self):
 
33
        # list of connected gamepad devices
 
34
        self.gamepads = []
 
35
        self.nunchuck = None
 
36
        connectedDevices = base.devices.getDevices()
 
37
        for device in connectedDevices:
 
38
            if device.get_device_class() == InputDevice.DC_gamepad \
 
39
            and "Nintendo Wii Remote" in device.name \
 
40
            and "Nintendo Wii Remote Nunchuk" not in device.name:
 
41
                self.gamepads.append(device)
 
42
            elif "Nintendo Wii Remote Nunchuk" in device.name:
 
43
                self.gamepads.append(device)
 
44
                self.nunchuck = device
 
45
 
 
46
    def hasGamepad(self):
 
47
        if not gamepadsupport: return False
 
48
        if base.devices.get_devices(InputDevice.DC_gamepad):
 
49
            return True
 
50
        return False
 
51
 
 
52
    def getMovementVec(self):
 
53
        if not gamepadsupport: return Vec3()
 
54
        movementVec = Vec3()
 
55
        # Move left/Right
 
56
        axis_x = self.nunchuck.findControl(self.deviceMap["axis-left-x"]).state
 
57
        if abs(axis_x) > self.deadzone_x:
 
58
            movementVec.setX(axis_x - self.lxcenter)
 
59
        # Move forward/backward
 
60
        axis_y = self.nunchuck.findControl(self.deviceMap["axis-left-y"]).state
 
61
        if abs(axis_y) > self.deadzone_y:
 
62
            movementVec.setY(-(axis_y - self.lycenter))
 
63
        return movementVec
 
64
 
 
65
    def getRotationVec(self):
 
66
        if not gamepadsupport: return Vec3()
 
67
        rotationVec = Vec3()
 
68
        rotationVec.setX(self.gamepads[self.usedGamepadID].findControl(self.deviceMap["axis-right-x"]).state - self.rxcenter)
 
69
        rotationVec.setY(self.gamepads[self.usedGamepadID].findControl(self.deviceMap["axis-right-y"]).state - self.rycenter)
 
70
        return rotationVec
 
71
 
 
72
    def getCamButton(self, direction):
 
73
        if not gamepadsupport: return False
 
74
        if direction not in self.deviceMap: return False
 
75
 
 
76
        self.checkGamepads()
 
77
        return self.gamepads[self.usedGamepadID].findButton(self.deviceMap[direction]).state == InputDevice.S_down
 
78
 
 
79
    def getJumpState(self):
 
80
        if not gamepadsupport: return False
 
81
        return self.gamepads[self.usedGamepadID].findButton(self.deviceMap["jump"]).state == InputDevice.S_down
 
82
 
 
83
    def getCenterCamState(self):
 
84
        if not gamepadsupport: return False
 
85
        return self.gamepads[self.usedGamepadID].findButton(self.deviceMap["center-camera"]).state == InputDevice.S_down
 
86
 
 
87
    def getIntelActionState(self):
 
88
        if not gamepadsupport: return False
 
89
        return self.gamepads[self.usedGamepadID].findButton(self.deviceMap["intel-action"]).state == InputDevice.S_down
 
90
 
 
91
    def getSprintState(self):
 
92
        if not gamepadsupport: return False
 
93
        return self.nunchuck.findButton(self.deviceMap["sprint"]).state == InputDevice.S_down