-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathclick-monitor.lua
77 lines (66 loc) · 1.79 KB
/
click-monitor.lua
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
-- Displays the mouse (grid) coordinates when the mouse is clicked
--@ enable = true
--[====[
devel/click-monitor
===================
Displays the grid coordinates of mouse clicks in the console.
Useful for plugin/script development.
Usage: ``devel/click-monitor start|stop``
]====]
VERSION = '0.2'
if active == nil then active = false end
function usage()
print [[
Usage:
click-monitor enable|start: Begin monitoring
click-monitor disable|stop: End monitoring
]]
end
function set_timeout()
dfhack.timeout(1, 'frames', check_click)
end
function log(s, color)
-- prevent duplicate output
if s ~= last_msg then
dfhack.color(color)
print(s)
last_msg = s
end
end
function check_click()
local s = ''
local color = COLOR_RESET
for _, attr in pairs({'mouse_lbut', 'mouse_rbut', 'mouse_lbut_down',
'mouse_rbut_down', 'mouse_lbut_lift', 'mouse_rbut_lift'}) do
if df.global.enabler[attr] ~= 0 then
s = s .. '[' .. attr:sub(7) .. '] '
end
end
if s ~= '' then
s = ('x = %2i, y = %2i '):format(df.global.gps.mouse_x, df.global.gps.mouse_y) .. s
if s:find('rbut') then
color = (s:find('lbut') and COLOR_GREEN) or COLOR_BLUE
end
if df.global.gps.mouse_x == -1 then color = COLOR_RED end
log(s, color)
else
last_msg = nil
end
if active then set_timeout() end
end
args = {...}
if dfhack_flags and dfhack_flags.enable then
table.insert(args, dfhack_flags.enable_state and 'enable' or 'disable')
end
if #args == 1 then
if args[1] == 'start' or args[1] == 'enable' then
active = true
set_timeout()
elseif args[1] == 'stop' or args[1] == 'disable' then
active = false
else
usage()
end
else
usage()
end