-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputs.py
163 lines (137 loc) · 5.16 KB
/
inputs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import logging
log = logging.getLogger(__name__)
from events import *
class InputFactory(object):
def __init__(self, event_func):
self.event_func = event_func
def StandardButton(self, key):
return StandardButton(self.event_func, key)
def TwoButtonAxis(self, threshold, pos_key, neg_key):
return TwoButtonAxis(self.event_func, threshold, pos_key, neg_key)
def MouseAxis(self, axis, _max, wrap):
return MouseAxis(self.event_func, axis, _max, wrap)
def MouseWheelButton(self, value):
return MouseWheelButton(self.event_func, value)
def MultiButton(self, value):
return MultiButton(self.event_func, value)
# def RepeatingButton(self, value):
# return StandardMultiButton(self.event_func, value)
def KeySequenceButton(self, value):
return KeySequenceButton(self.event_func, value)
class Input(object):
def __init__(self, event_func, key):
self.state = 0
self.key = key
self.event_func = event_func
pass
def set_state(self, state):
self.state = state
def get_state(self):
return self.state
def _emit_event(self, ev):
self.event_func(ev)
class StandardButton(Input):
"""Sends events when state changes"""
def set_state(self, state):
if state != self.state:
ev = PressEvent if state else ReleaseEvent
self._emit_event(ev(self.key))
self.state = state
class MultiButton(Input):
"""Sends a sequence of key presses when state changes"""
def __init__(self, event_func, key_list):
super(MultiButton, self).__init__(event_func, None)
self.key_list = key_list
def set_state(self, state):
if state != self.state:
ev = PressEvent if state else ReleaseEvent
for key in self.key_list:
self._emit_event(ev(key))
self.state = state
class TwoButtonAxis(Input):
"""Sends one event for positive, one event for negative"""
def __init__(self, event_func, threshold, pos_key, neg_key):
super(TwoButtonAxis, self).__init__(event_func, None)
self.tick_mod = 10 #every 10 ticks
self.pos_key = pos_key
self.neg_key = neg_key
self.threshold = threshold
def set_state(self, state):
if self.state == 0:
if state > self.threshold:
if self.pos_key == None:
return
self.state = 1
self._emit_event(PressEvent(key=self.pos_key))
elif state < (-1*self.threshold):
if self.neg_key == None:
return
self.state = -1
self._emit_event(PressEvent(key=self.neg_key))
elif self.state > 0:
if state < self.threshold:
self.state = 0
self._emit_event(ReleaseEvent(key=self.pos_key))
elif self.state < 0:
if state > -1*self.threshold:
self.state = 0
self._emit_event(ReleaseEvent(key=self.neg_key))
def tick(self, i):
pass
class MouseAxis(Input):
def __init__(self, event_func, axis_name, _max, wrap=True):
self.event_func = event_func
self.axis = axis_name
self.state = 0
self.pos = _max/2
self.max = _max
self.threshold = 0.3
self.pos_diff = 1
self.wrap = wrap
def set_state(self, state):
if state > self.threshold:
if self.state < 0:
self.state = 0
else:
self.state = state / self.threshold
elif state < (-1*self.threshold):
if self.state > 0:
self.state = 0
else:
self.state = state / self.threshold
else:
self.state = 0
def tick(self, x):
if (x % 1) == 0:
if self.state < 0:
self.pos -= self.pos_diff * (self.state*self.state)
if self.state > 0:
self.pos += self.pos_diff * (self.state*self.state)
if self.wrap:
self.pos = self.pos % self.max
else:
self.pos = min(self.pos, self.max)
self.pos = max(self.pos, 0)
if self.state != 0:
self._emit_event(MouseMoveEvent(axis=self.axis, value=self.pos))
class MouseWheelButton(Input):
"""Sends a click n event for down and that is it"""
def __init__(self, event_func, value):
self.value = value
super(MouseWheelButton, self).__init__(event_func, None)
def set_state(self, state):
if state:
self._emit_event(MouseWheelEvent(self.value))
class KeySequenceButton(Input):
"""Sends a sequence of key press/release events whenever the button is pressed.
Only fires once per press
In the config have a comma separated list of buttons
"""
def __init__(self, event_func, key_list):
super(KeySequenceButton, self).__init__(event_func, None)
self.key_list = key_list
def set_state(self, state):
if state:
for key in self.key_list:
self._emit_event(PressEvent(key))
self._emit_event(ReleaseEvent(key))