-
Notifications
You must be signed in to change notification settings - Fork 5
/
command_wizard.py
237 lines (187 loc) · 6.97 KB
/
command_wizard.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
Actions related to building voice commands.
"""
import os
import datetime
from talon import Module, Context, actions, ui, imgui
from .overlays import ImageSelectorOverlay, BlobBoxOverlay
from .mouse_helper import get_image_template_directory
mod = Module()
mod.tag("command_wizard_showing", desc="The command wizard is showing")
ctx = Context()
def save_image_template(image):
"""
Saves the given image to the image templates folder and returns the generated name.58
"""
unique_filename = \
str(datetime.datetime.now().date()) + '_' + \
str(datetime.datetime.now().time()).replace(':', '.') + \
".png"
templates_directory = get_image_template_directory()
full_filename = os.path.join(templates_directory, unique_filename)
if not os.path.exists(templates_directory):
os.mkdir(templates_directory)
image.write_file(full_filename)
return unique_filename
def handle_image_click_builder(result):
"""
Result handler for the image click command builder.
"""
if result is None:
return
filename = save_image_template(result["image"])
index = result["index"]
offset_bit = ""
if result["offset"]:
offset_bit = ", ".join([""] + list(map(lambda x: str(int(x)), result["offset"])))
command = "\n".join([
"",
":",
" user.mouse_helper_position_save()",
f' user.mouse_helper_move_image_relative("{filename}", {index}{offset_bit})',
" sleep(0.05)",
" mouse_click(0)",
" sleep(0.05)",
" user.mouse_helper_position_restore()",
])
actions.clip.set_text(command)
actions.app.notify("Copied new command to clipboard")
def handle_multi_image_builder(result):
"""
Result handler for the multi image marker command builder.
"""
if result is None:
return
filename = save_image_template(result["image"])
offset_bit = ""
if result["offset"]:
offset_bit = ", ".join([""] + list(map(lambda x: str(int(x)), result["offset"])))
command = "\n".join([
"",
":",
f' matches = user.mouse_helper_find_template_relative("{filename}"{offset_bit})',
" user.marker_ui_show(matches)",
])
actions.clip.set_text(command)
actions.app.notify("Copied new command to clipboard")
def handle_blob_detect_builder(result):
"""
Result handler for the blob detect command builder.
"""
if result is None:
return
active_rectangle = active_rectangle_before_overlay
def calculate_offset(position, minimum, width):
# Split each axis into two to determine which side of the screen
# the coordinate is offset from
maximum = minimum + width
split = (minimum + maximum) / 2
if position > split:
return str(position - maximum)
else:
return str(position - minimum)
offsets = " ".join([
calculate_offset(result.x, active_rectangle.x, active_rectangle.width),
calculate_offset(result.y, active_rectangle.y, active_rectangle.height),
calculate_offset(result.x + result.width, active_rectangle.x, active_rectangle.width),
calculate_offset(result.y + result.height, active_rectangle.y, active_rectangle.height),
])
command = "\n".join([
"",
":",
f' bounding_rectangle = user.mouse_helper_calculate_relative_rect("{offsets}", "active_window")',
f' user.mouse_helper_blob_picker(bounding_rectangle)',
])
actions.clip.set_text(command)
actions.app.notify("Copied new command to clipboard")
command_wizards = [
(
"Click a single image on the screen",
ImageSelectorOverlay,
handle_image_click_builder,
(
"Select a region of the screen as an image to find in your voice command "
"then press enter to confirm your selection. Press escape to cancel.\n\n"
"After selecting and before enter, optionally right click to define an "
"offset from the selected region. This will be clicked instead of the "
"center of the region."
)
),
(
"Show markers on all image matches on screen",
ImageSelectorOverlay,
handle_multi_image_builder,
(
"Select a region of the screen as an image to find in your voice command "
"then press enter to confirm your selection. Press escape to cancel.\n\n"
"After selecting and before enter, optionally right click to define an "
"offset from the selected region. This will be clicked instead of the "
"center of the region."
)
),
(
"Find items in a box to click",
BlobBoxOverlay,
handle_blob_detect_builder,
(
"Select a region of the active window to use in your voice command then press "
"enter to confirm your selection. Press escape to cancel.\n\n"
"When drawing a box wider than tall, the first row of pixels will be "
"considered background color. When the box is taller than wide the first "
"column of pixels will be considered background color.\n\n"
"The background color will be used to detect clickable regions in the area "
"you selected. "
)
)
]
existing_overlay = None
# On OSX the overlay window itself shows up as the ui.active_window() after the overlay is nominally
# closed. So we save what the active window rect was just before the overlay is shown.
active_rectangle_before_overlay = None
def open_overlay(index):
global existing_overlay
global active_rectangle_before_overlay
active_rectangle_before_overlay = ui.active_window().rect
builder_picker_toggle(False)
if existing_overlay:
existing_overlay.destroy()
_, overlay, handler, help = command_wizards[index]
existing_overlay = overlay(handler, text=help)
@imgui.open(y=0)
def builder_picker(gui: imgui.GUI):
t = gui.text("Choose the command type that you would like to build:")
gui.spacer()
for i, (text, _, _, _) in enumerate(command_wizards):
gui.text(text)
if gui.button(f"Choose {i+1}"):
open_overlay(i)
gui.spacer()
if gui.button("Command wizard hide"):
builder_picker_toggle(False)
def builder_picker_toggle(visible: bool):
if visible:
builder_picker.show()
ctx.tags = ["user.command_wizard_showing"]
else:
builder_picker.hide()
ctx.tags = []
@mod.action_class
class CommandWizardActions:
"""
Actions related to the command builder wizard.
"""
def command_wizard_show():
"""
Brings up the command wizard UI
"""
builder_picker_toggle(True)
def command_wizard_hide():
"""
Closes the command wizard UI
"""
builder_picker_toggle(False)
def command_wizard_choose_option(option: int):
"""
Chooses one of the command wizards
"""
open_overlay(option - 1)