Created
March 15, 2016 20:05
-
-
Save u8sand/ef0894faff700e943de6 to your computer and use it in GitHub Desktop.
A prototype of the baka input handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Input(Data): | |
data = ['key', 'mouse'] | |
class Key(dict, Data): | |
data = ('data', 'input',) | |
def __init__(self, parent): | |
dict.__init__(self) | |
self.input = parent | |
def verbose(self, text): | |
if self.input.config.verbose: | |
# print('[input.key]: %s' % (text)) | |
pass | |
return text | |
def press(self, event): | |
# get the actual input binding | |
key = self.get(self.verbose(QKeySequence( | |
event.modifiers() | event.key()).toString())) | |
if key: | |
# execute the attached function | |
self.input.eval(key[0]) | |
event.accept() | |
class Mouse(dict, Data): | |
data = ('data', 'input', 'buttonString', 'gestureHandler',) | |
timer_threshold = 100 | |
gesture_threshold = 15 | |
class GestureHandler: | |
def __init__(self, startPos): | |
self.startPos = startPos | |
self.action = None | |
self.timer = QElapsedTimer() | |
self.timer.start() | |
QApplication.setOverrideCursor(QCursor(Qt.PointingHandCursor)) | |
def __init__(self, parent): | |
dict.__init__(self) | |
self.input = parent | |
self.buttonString = { | |
Qt.LeftButton: 'Left', | |
Qt.RightButton: 'Right', | |
Qt.MiddleButton: 'Middle', | |
} | |
self.gestureHandler = { | |
Qt.LeftButton: None, | |
Qt.RightButton: None, | |
Qt.MiddleButton: None, | |
} | |
def verbose(self, text): | |
if self.input.config.verbose: | |
print('[input.mouse]: %s' % (text)) | |
return text | |
def press(self, event): | |
self.gestureHandler[event.button()] = Input.Mouse.GestureHandler( | |
event.globalPos()) | |
event.accept() | |
def move(self, event): | |
for button, handler in filter(lambda h: h[1], self.gestureHandler.items()): | |
if handler.timer.elapsed() > self.timer_threshold: | |
delta = event.globalPos() - handler.startPos | |
if handler.action == None: | |
if abs(delta.x()) >= abs(delta.y()) + self.gesture_threshold: | |
handler.action = self.get(self.verbose( | |
'%sHDrag' % (self.buttonString.get(button)))) | |
elif abs(delta.y()) >= abs(delta.x()) + self.gesture_threshold: | |
handler.action = self.get(self.verbose( | |
'%sVDrag' % (self.buttonString.get(button)))) | |
# todo: DDrag | |
else: | |
self.input.eval(action[0])(delta) | |
event.accept() | |
def release(self, event): | |
button = event.button() | |
handler = self.gestureHandler.get(button) | |
click = True | |
if handler: | |
click = handler.action == None | |
self.gestureHandler[button] = None | |
QApplication.restoreOverrideCursor() | |
if click: | |
action = self.get(self.verbose( | |
'%sClick' % (self.buttonString.get(button)))) | |
if action: | |
self.input.eval(action[0]) | |
event.accept() | |
def doubleClick(self, event): | |
action = self.get(self.verbose( | |
'%sDoubleClick' % (self.buttonString.get(event.button())))) | |
if action: | |
self.input.eval(action[0]) | |
event.accept() | |
def wheel(self, event): | |
angle = event.angleDelta() | |
action = self.get(self.verbose( | |
'Wheel%s' % ('Up' if angle.y() > 0 else 'Down'))) | |
if action: | |
self.input.eval(action[0]) | |
event.accept() | |
def __init__(self, ui): | |
''' Initialize input bindings. ''' | |
self.key, self.mouse = Input.Key(self), Input.Mouse(self) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment