From c8d74466ee3f11b8ba846b43066cf0fed01a2522 Mon Sep 17 00:00:00 2001 From: Darren Ranalli Date: Tue, 13 Dec 2005 02:04:43 +0000 Subject: [PATCH] added InputState.watchWithModifiers to reduce code duplication --- direct/src/controls/ControlManager.py | 86 ++++----------------------- direct/src/controls/InputState.py | 6 ++ 2 files changed, 18 insertions(+), 74 deletions(-) diff --git a/direct/src/controls/ControlManager.py b/direct/src/controls/ControlManager.py index 61e8331f4b..8003af38e6 100755 --- a/direct/src/controls/ControlManager.py +++ b/direct/src/controls/ControlManager.py @@ -30,93 +30,31 @@ class ControlManager: #self.monitorTask = taskMgr.add(self.monitor, "ControlManager-%s"%(id(self)), priority=-1) inputState.watch("run", "running-on", "running-off") - inputState.watch("forward", "arrow_up", "arrow_up-up") - inputState.watch("forward", "control-arrow_up", "arrow_up-up") - inputState.watch("forward", "shift-control-arrow_up", "arrow_up-up") - inputState.watch("forward", "alt-arrow_up", "arrow_up-up") - inputState.watch("forward", "control-alt-arrow_up", "arrow_up-up") - inputState.watch("forward", "shift-arrow_up", "arrow_up-up") + inputState.watchWithModifiers("forward", "arrow_up") inputState.watch("forward", "force-forward", "force-forward-stop") - inputState.watch("reverse", "arrow_down", "arrow_down-up") - inputState.watch("reverse", "control-arrow_down", "arrow_down-up") - inputState.watch("reverse", "shift-control-arrow_down", "arrow_down-up") - inputState.watch("reverse", "alt-arrow_down", "arrow_down-up") - inputState.watch("reverse", "control-alt-arrow_down", "arrow_down-up") - inputState.watch("reverse", "shift-arrow_down", "arrow_down-up") - - inputState.watch("reverse", "mouse4", "mouse4-up") - inputState.watch("reverse", "control-mouse4", "mouse4-up") - inputState.watch("reverse", "shift-control-mouse4", "mouse4-up") - inputState.watch("reverse", "alt-mouse4", "mouse4-up") - inputState.watch("reverse", "control-alt-mouse4", "mouse4-up") - inputState.watch("reverse", "shift-mouse4", "mouse4-up") + inputState.watchWithModifiers("reverse", "arrow_down") + inputState.watchWithModifiers("reverse", "mouse4") - inputState.watch("turnLeft", "arrow_left", "arrow_left-up") - inputState.watch("turnLeft", "control-arrow_left", "arrow_left-up") - inputState.watch("turnLeft", "shift-control-arrow_left", "arrow_left-up") - inputState.watch("turnLeft", "alt-arrow_left", "alt-arrow_left-up") - inputState.watch("turnLeft", "control-alt-arrow_left", "alt-arrow_left-up") - inputState.watch("turnLeft", "shift-arrow_left", "arrow_left-up") + inputState.watchWithModifiers("turnLeft", "arrow_left") inputState.watch("turnLeft", "mouse-look_left", "mouse-look_left-done") inputState.watch("turnLeft", "force-turnLeft", "force-turnLeft-stop") - inputState.watch("turnRight", "arrow_right", "arrow_right-up") - inputState.watch("turnRight", "control-arrow_right", "arrow_right-up") - inputState.watch("turnRight", "shift-control-arrow_right", "arrow_right-up") - inputState.watch("turnRight", "alt-arrow_right", "arrow_right-up") - inputState.watch("turnRight", "control-alt-arrow_right", "arrow_right-up") - inputState.watch("turnRight", "shift-arrow_right", "arrow_right-up") + inputState.watchWithModifiers("turnRight", "arrow_right") inputState.watch("turnRight", "mouse-look_right", "mouse-look_right-done") inputState.watch("turnRight", "force-turnRight", "force-turnRight-stop") if self.wantWASD: - inputState.watch("forward", "w", "w-up") - inputState.watch("forward", "control-w", "w-up") - inputState.watch("forward", "shift-control-w", "w-up") - inputState.watch("forward", "alt-w", "w-up") - inputState.watch("forward", "control-alt-w", "w-up") - inputState.watch("forward", "shift-w", "w-up") - - inputState.watch("reverse", "s", "s-up") - inputState.watch("reverse", "control-s", "s-up") - inputState.watch("reverse", "shift-control-s", "s-up") - inputState.watch("reverse", "alt-s", "s-up") - inputState.watch("reverse", "control-alt-s", "s-up") - inputState.watch("reverse", "shift-s", "s-up") - - inputState.watch("slideLeft", "a", "a-up") - inputState.watch("slideLeft", "control-a", "a-up") - inputState.watch("slideLeft", "shift-control-a", "a-up") - inputState.watch("slideLeft", "alt-a", "alt-a-up") - inputState.watch("slideLeft", "control-alt-a", "alt-a-up") - inputState.watch("slideLeft", "shift-a", "a-up") - - inputState.watch("slideRight", "d", "d-up") - inputState.watch("slideRight", "control-d", "d-up") - inputState.watch("slideRight", "shift-control-d", "d-up") - inputState.watch("slideRight", "alt-d", "d-up") - inputState.watch("slideRight", "control-alt-d", "d-up") - inputState.watch("slideRight", "shift-d", "d-up") - - - inputState.watch("turnLeft", "q", "q-up") - inputState.watch("turnLeft", "control-q", "q-up") - inputState.watch("turnLeft", "shift-control-q", "q-up") - inputState.watch("turnLeft", "alt-q", "alt-q-up") - inputState.watch("turnLeft", "control-alt-q", "alt-q-up") - inputState.watch("turnLeft", "shift-q", "q-up") - - inputState.watch("turnRight", "e", "e-up") - inputState.watch("turnRight", "control-e", "e-up") - inputState.watch("turnRight", "shift-control-e", "e-up") - inputState.watch("turnRight", "alt-e", "e-up") - inputState.watch("turnRight", "control-alt-e", "e-up") - inputState.watch("turnRight", "shift-e", "e-up") + inputState.watchWithModifiers("forward", "w") + inputState.watchWithModifiers("reverse", "s") + inputState.watchWithModifiers("slideLeft", "a") + inputState.watchWithModifiers("slideRight", "d") + inputState.watchWithModifiers("turnLeft", "q") + inputState.watchWithModifiers("turnRight", "e") # Jump controls if self.wantWASD: - inputState.watch("jump", "space", "space-up") + inputState.watchWithModifiers("jump", "space") else: inputState.watch("jump", "control", "control-up") diff --git a/direct/src/controls/InputState.py b/direct/src/controls/InputState.py index b5ce45578a..7dfd2001b3 100755 --- a/direct/src/controls/InputState.py +++ b/direct/src/controls/InputState.py @@ -51,6 +51,12 @@ class InputState(DirectObject.DirectObject): else: # Start a new list self.watching[name] = [(eventOn, eventOff)] + + def watchWithModifiers(self, name, event, default = 0): + patterns = ('%s', 'control-%s', 'shift-control-%s', 'alt-%s', + 'control-alt-%s', 'shift-%s',) + for pattern in patterns: + self.watch(name, pattern % event, '%s-up' % event, default) def force(self, name, value): """