summaryrefslogtreecommitdiff
path: root/indra/newview/scripts/lua/require/UI.lua
blob: 969a2cbded3cb8bb4502dce7b67559ac9d99eb3d (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
-- Engage the viewer's UI

local leap = require 'leap'
local mapargs = require 'mapargs'
local Timer = (require 'timers').Timer
local util = require 'util'

-- Allow lazily accessing certain other modules on demand, e.g. a reference to
-- UI.Floater lazily loads the Floater module. Use of UI's __index metamethod
-- theoretically permits any other module you can require() to appear as a
-- submodule of UI, but it doesn't make sense to support (e.g.) UI.Queue.
local submods = { 'Floater', 'popup' }
local UI = util.setmetamethods{
    __index=function(t, key)
        if not table.find(submods, key) then
            error(`Invalid UI submodule {key}`, 2)
        end
        local mod = require(key)
        -- cache the submodule
        t[key] = mod
        return mod
    end
}                               

-- ***************************************************************************
--  registered menu actions
-- ***************************************************************************
function UI.call(func, parameter)
    -- 'call' is fire-and-forget
    leap.request('UI', {op='call', ['function']=func, parameter=parameter})
end

function UI.callables()
    return leap.request('UI', {op='callables'}).callables
end

function UI.getValue(path)
    return leap.request('UI', {op='getValue', path=path})['value']
end

-- ***************************************************************************
--  UI views
-- ***************************************************************************
-- Either:
-- wreq{op='Something', a=1, b=2, ...}
-- or:
-- (args should be local, as this wreq() call modifies it)
-- local args = {a=1, b=2, ...}
-- wreq('Something', args)
local function wreq(op_or_data, data_if_op)
    if data_if_op ~= nil then
        -- this is the wreq(op, data) form
        data_if_op.op = op_or_data
        op_or_data = data_if_op
    end
    return leap.request('LLWindow', op_or_data)
end

-- omit 'parent' to list all view paths
function UI.listviews(parent)
    return wreq{op='getPaths', under=parent}
end

function UI.viewinfo(path)
    return wreq{op='getInfo', path=path}
end

-- ***************************************************************************
--  mouse actions
-- ***************************************************************************
-- pass a table:
-- UI.click{path=path
--          [, button='LEFT' | 'CENTER' | 'RIGHT']
--          [, x=x, y=y]
--          [, hold=duration]}
function UI.click(...)
    local args = mapargs('path,button,x,y,hold', ...)
    args.button = args.button or 'LEFT'
    local hold = args.hold or 1.0
    wreq('mouseMove', args)
    wreq('mouseDown', args)
    Timer(hold, 'wait')
    wreq('mouseUp', args)
end

-- pass a table as for UI.click()
function UI.doubleclick(...)
    local args = mapargs('path,button,x,y', ...)
    args.button = args.button or 'LEFT'
    wreq('mouseDown', args)
    wreq('mouseUp',   args)
    wreq('mouseDown', args)
    wreq('mouseUp',   args)
end

-- UI.drag{path=, xoff=, yoff=}
function UI.drag(...)
    local args = mapargs('path,xoff,yoff', ...)
    -- query the specified path
    local rect = UI.viewinfo(args.path).rect
    local centerx = math.floor(rect.left   + (rect.right - rect.left)/2)
    local centery = math.floor(rect.bottom + (rect.top   - rect.bottom)/2)
    wreq{op='mouseMove', path=args.path, x=centerx, y=centery}
    wreq{op='mouseDown', path=args.path, button='LEFT'}
    wreq{op='mouseMove', path=args.path, x=centerx + args.xoff, y=centery + args.yoff}
    wreq{op='mouseUp',   path=args.path, button='LEFT'}
end

-- ***************************************************************************
--  keyboard actions
-- ***************************************************************************
-- pass a table:
-- UI.keypress{
--     [path=path]                      -- if omitted, default input field
--     [, char='x']                     -- requires one of char, keycode, keysym
--     [, keycode=120]
--     keysym per https://github.com/secondlife/viewer/blob/main/indra/llwindow/llkeyboard.cpp#L68-L124
--     [, keysym='Enter']
--     [, mask={'SHIFT', 'CTL', 'ALT', 'MAC_CONTROL'}]  -- some subset of these
--     }
function UI.keypress(...)
    local args = mapargs('path,char,keycode,keysym,mask', ...)
    if args.char == '\n' then
        args.char = nil
        args.keysym = 'Enter'
    end
    return wreq('keyDown', args)
end

-- UI.type{text=, path=}
function UI.type(...)
    local args = mapargs('text,path', ...)
    if #args.text > 0 then
        -- The caller's path may be specified in a way that requires recursively
        -- searching parts of the LLView tree. No point in doing that more than
        -- once. Capture the actual path found by that first call and use that for
        -- subsequent calls.
        local path = UI.keypress{path=args.path, char=string.sub(args.text, 1, 1)}.path
        for i = 2, #args.text do
            UI.keypress{path=path, char=string.sub(args.text, i, i)}
        end
    end
end

-- ***************************************************************************
--  Snapshot
-- ***************************************************************************
-- UI.snapshot{filename=filename            -- extension may be specified: bmp, jpeg, png
--          [, type='COLOR' | 'DEPTH']
--          [, width=width][, height=height]  -- uses current window size if not specified
--          [, showui=true][, showhud=true]
--          [, rebuild=false]}
function UI.snapshot(...)
    local args = mapargs('filename,width,height,showui,showhud,rebuild,type', ...)
    args.op = 'saveSnapshot'
    return leap.request('LLViewerWindow', args).result
end

-- ***************************************************************************
--  Top menu
-- ***************************************************************************

function UI.getTopMenus()
    return leap.request('UI', {op='getTopMenus'}).menus
end

function UI.addMenu(...)
    local args = mapargs('name,label', ...)
    args.op = 'addMenu'
    return leap.request('UI', args)
end

function UI.setMenuVisible(name, visible)
    return leap.request('UI', {op='setMenuVisible', name=name, visible=visible})
end

function UI.addMenuBranch(...)
    local args = mapargs('name,label,parent_menu', ...)
    args.op = 'addMenuBranch'
    return leap.request('UI', args)
end

-- see UI.callables() for valid values of 'func'
function UI.addMenuItem(...)
    local args = mapargs('name,label,parent_menu,func,param', ...)
    args.op = 'addMenuItem'
    return leap.request('UI', args)
end

function UI.addMenuSeparator(...)
    local args = mapargs('parent_menu', ...)
    args.op = 'addMenuSeparator'
    return leap.request('UI', args)
end

-- ***************************************************************************
--  Toolbar buttons
-- ***************************************************************************
-- Clears all buttons off the toolbars
function UI.clearAllToolbars()
    leap.send('UI', {op='clearAllToolbars'})
end

function UI.defaultToolbars()
    leap.send('UI', {op='defaultToolbars'})
end

-- UI.addToolbarBtn{btn_name=btn_name
--          [, toolbar= bottom] -- left, right, bottom -- default is bottom
--          [, rank=1]} -- position on the toolbar, starts at 0 (0 - first position, 1 - second position etc.)
function UI.addToolbarBtn(...)
    local args = mapargs('btn_name,toolbar,rank', ...)
    args.op = 'addToolbarBtn'
    return leap.request('UI', args)
end

-- Returns the rank(position) of the command in the original list
function UI.removeToolbarBtn(btn_name)
    return leap.request('UI', {op = 'removeToolbarBtn', btn_name=btn_name}).rank
end

function UI.getToolbarBtnNames()
    return leap.request('UI', {op = 'getToolbarBtnNames'}).cmd_names
end

-- ***************************************************************************
--  Floaters
-- ***************************************************************************
function UI.showFloater(floater_name)
    leap.send("LLFloaterReg", {op = "showInstance", name = floater_name})
end

function UI.hideFloater(floater_name)
    leap.send("LLFloaterReg", {op = "hideInstance", name = floater_name})
end

function UI.toggleFloater(floater_name)
    leap.send("LLFloaterReg", {op = "toggleInstance", name = floater_name})
end

function UI.isFloaterVisible(floater_name)
    return leap.request("LLFloaterReg", {op = "instanceVisible", name = floater_name}).visible
end

function UI.closeAllFloaters()
    return leap.send("UI", {op = "closeAllFloaters"})
end

function UI.getFloaterNames()
    return leap.request("LLFloaterReg", {op = "getFloaterNames"}).floaters
end

return UI