-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzones.py
200 lines (158 loc) · 6.54 KB
/
zones.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from talon.skia import Paint
import time
from .helpers import rgba2hex,TriggerType
from .settings import *
class Zone:
def __init__(self,color,centre,name,ttype,action,warmup,repeatTime,modifiers) -> None:
self.color=color
self.centre=centre
self.name=name
self.triggerType=ttype
self.action=action
self.warmup=warmup
self.repeatTime=repeatTime
self.modifiers = modifiers
self.modifierStartAwake = "start awake" in modifiers
# at present the below modifier has no effect, for now all zones block input, makes it easier to use zoom mouse
# 'allow input' might be a better modifier
self.modifierBlockInput = "block input" in modifiers
self.modifierTriggerOffOnExit = "deactivate on exit" in modifiers
self.wasHovering = False
self.startTimer = 0
if self.modifierStartAwake:
self.repeatTimer=0
else:
self.repeatTimer=float("inf")
self.textColor=rgba2hex(255,255,255,ZONES_TEXT_ALPHA)
self.sinceInteractedTimer = 0
def interact(self):
self.sinceInteractedTimer = time.time()
def fire_interaction(self, action:str):
from .master import primative_interaction
primative_interaction(action)
def update(self,isHovering:bool):
if self.wasHovering!=isHovering:
self.on_hover_change(isHovering)
self.textColor = rgba2hex(255,255,255,ZONES_TEXT_ALPHA)
if time.time() - self.sinceInteractedTimer <= min(0.5, max(self.repeatTime,0.15))-0.05:
self.textColor = rgba2hex(255,0,0,ZONES_TEXT_ALPHA)
self.wasHovering=isHovering
def on_hover_change(self,isHovering:bool):
if self.triggerType != TriggerType.HOVER:
return
if self.modifierTriggerOffOnExit and not isHovering:
self.deactivate()
pass
def click(self):
return
if not self.configured:
return
# immediately set up the conditions for interaction to occur in the update method
if self.type==TriggerType.CLICK:
self.dirtyTrigger = True
self.startTimer = time.time()
self.repeatTimer = 0
pass
def draw(self, canvas):
paint = canvas.paint
paint.text_align = canvas.paint.TextAlign.LEFT
paint.textsize = 25
paint.color = self.textColor
paint.style = Paint.Style.FILL
text = self.name
tr = paint.measure_text(text)[1]
x=self.centre[0]
y=self.centre[1]
canvas.draw_text(text,x,y-tr.y)
pass
def deactivate(self):
self.repeatTimer=float("inf")
self.startTimer=float("inf")
pass
def clear_timers(self):
self.repeatTimer=float("inf")
self.startTimer=float("inf")
def start_timers(self):
self.startTimer = time.time()
self.repeatTimer = 0
class TriggerZone(Zone):
def __init__(self, color, centre, name, ttype, action, warmup, repeatTime,modifiers:str,action2) -> None:
super().__init__(color, centre, name, ttype, action, warmup, repeatTime,modifiers)
self.action2 = action2
self.triggerValue = False
self.dirtyTrigger=False
self.modifierForceOffRepeat = "force off repeat" in modifiers.lower()
self.modifierForceOnRepeat = "force on repeat" in modifiers.lower()
def interact(self):
super().interact()
if self.dirtyTrigger:
self.triggerValue = not self.triggerValue
self.dirtyTrigger = False
if self.triggerValue:
self.fire_interaction(self.action)
else:
self.fire_interaction(self.action2)
def update(self, isHovering: bool):
super().update(isHovering)
if (self.triggerValue):
self.textColor = rgba2hex(255,0,0,255)
if (time.time()-self.repeatTimer>=self.repeatTime and time.time()-self.startTimer>=self.warmup):
self.interact()
if self.triggerValue==True:
if (self.repeatTime>0 or self.modifierForceOnRepeat):
self.repeatTimer = time.time()
else:
self.clear_timers()
if self.triggerValue==False:
if (not self.modifierForceOffRepeat):
self.clear_timers()
def on_hover_change(self,isHovering: bool):
super().on_hover_change(isHovering)
if self.triggerType != TriggerType.HOVER:
return
if isHovering:
self.dirtyTrigger=True
self.start_timers()
else:
if time.time()-self.startTimer<self.warmup:
self.dirtyTrigger=False
if not self.modifierForceOffRepeat and not self.triggerValue:
self.clear_timers()
def click(self):
super().click()
if self.triggerType!=TriggerType.CLICK:
return
self.dirtyTrigger=True
self.start_timers()
def deactivate(self):
super().deactivate()
if self.triggerValue == True:
self.dirtyTrigger = True
self.interact()
class SimpleZone(Zone):
def __init__(self, color, centre, name, ttype, action, warmup, repeatTime,modifiers:str) -> None:
super().__init__(color, centre, name, ttype, action, warmup, repeatTime,modifiers)
def interact(self):
super().interact()
self.fire_interaction(self.action)
def update(self, isHovering: bool):
super().update(isHovering)
if (time.time()-self.repeatTimer>=self.repeatTime and time.time()-self.startTimer>=self.warmup):
self.interact()
if self.triggerType==TriggerType.HOVER:
self.repeatTimer = time.time()
else:
self.clear_timers()
def on_hover_change(self,isHovering: bool):
super().on_hover_change(isHovering)
if self.triggerType != TriggerType.HOVER:
return
if isHovering:
self.start_timers()
else:
self.clear_timers()
def click(self):
super().click()
if self.triggerType!=TriggerType.CLICK:
return
self.start_timers()