forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialogs.lua
More file actions
411 lines (362 loc) · 10.6 KB
/
Copy pathdialogs.lua
File metadata and controls
411 lines (362 loc) · 10.6 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
-- Some simple dialog screens
local _ENV = mkmodule('gui.dialogs')
local gui = require('gui')
local widgets = require('gui.widgets')
---------------------------------
-- DialogWindow and DialogScreen
--
DialogWindow = defclass(DialogWindow, widgets.Window)
DialogWindow.ATTRS{
frame={w=20, h=10},
min_width=30,
extra_height=0,
message_label_attrs={},
accept_hotkey_label_attrs={},
on_accept=DEFAULT_NIL,
on_cancel=DEFAULT_NIL,
}
function DialogWindow:init()
local message_label_attrs = {
view_id='label',
auto_height=false,
frame={t=0, l=0, b=2},
}
for k,v in pairs(self.message_label_attrs) do message_label_attrs[k] = v end
local accept_hotkey_label_attrs = {
frame={b=0, l=0, r=0},
key='SELECT',
label='Ok',
auto_width=true,
on_activate=self:callback('accept'),
}
for k,v in pairs(self.accept_hotkey_label_attrs) do accept_hotkey_label_attrs[k] = v end
self:addviews{
widgets.Label(message_label_attrs),
widgets.HotkeyLabel(accept_hotkey_label_attrs),
}
end
function DialogWindow:accept()
if self.on_accept then
self.on_accept()
end
self.parent_view:dismiss()
end
function DialogWindow:cancel()
if self.on_cancel then
self.on_cancel()
end
self.parent_view:dismiss()
end
function DialogWindow:computeFrame()
local min_width = math.max(self.frame.w or 0, 20, self.min_width, #(self.frame_title or '') + 4)
local label = self.subviews.label
local text_area_width = label:getTextWidth() + 1
local text_height = label:getTextHeight()
local sw, sh = dfhack.screen.getWindowSize()
if text_height >= sh - (6 + self.extra_height) then
-- account for scrollbar
text_area_width = text_area_width + 2
end
local fw, fh = math.max(min_width, text_area_width), text_height + 3 + self.extra_height
return gui.compute_frame_body(sw, sh, {w=fw, h=fh}, 1, 1, true)
end
function DialogWindow:onInput(keys)
if (keys.LEAVESCREEN or keys._MOUSE_R) and self.on_cancel then
self:cancel()
end
return DialogWindow.super.onInput(self, keys)
end
DialogScreen = defclass(DialogScreen, gui.ZScreenModal)
DialogScreen.ATTRS{
focus_path='MessageBox',
title=DEFAULT_NIL,
min_width=DEFAULT_NIL,
extra_height=DEFAULT_NIL,
message_label_attrs=DEFAULT_NIL,
accept_hotkey_label_attrs=DEFAULT_NIL,
on_accept=DEFAULT_NIL,
on_cancel=DEFAULT_NIL,
on_close=DEFAULT_NIL,
}
function DialogScreen:init(args)
local window = DialogWindow{
frame_title=self.title,
min_width=self.min_width,
extra_height=self.extra_height,
message_label_attrs=self.message_label_attrs,
accept_hotkey_label_attrs=self.accept_hotkey_label_attrs,
on_accept=self.on_accept,
on_cancel=self.on_cancel,
subviews=self.subviews,
}
window:addviews(args.subviews)
self:addviews{window}
end
function DialogScreen:onDismiss()
if self.on_close then
self.on_close()
end
end
------------------------
-- Convenience methods
--
function showMessage(title, text, tcolor, on_close)
return DialogScreen{
title=title,
message_label_attrs={
text=text,
text_pen=tcolor,
},
on_close=on_close,
}:show()
end
function showYesNoPrompt(title, text, tcolor, on_accept, on_cancel, on_pause, on_settings)
local dialog
local subviews = {
widgets.HotkeyLabel{
frame={b=2, r=0},
label='Settings',
key='CUSTOM_SHIFT_S',
auto_width=true,
on_activate=function()
dialog:dismiss()
on_settings()
end,
visible=not not on_settings,
},
widgets.HotkeyLabel{
frame={b=2, l=0},
label='Pause this confirmation',
key='CUSTOM_SHIFT_P',
auto_width=true,
on_activate=function()
dialog:dismiss()
on_pause()
end,
visible=not not on_pause,
},
}
dialog = DialogScreen{
title=title,
message_label_attrs={
frame=(on_pause or on_settings) and {t=0, l=0, b=4} or nil,
text=text,
text_pen=tcolor,
},
accept_hotkey_label_attrs={
label='Yes, proceed',
},
on_accept=on_accept,
on_cancel=on_cancel,
min_width=on_settings and 39 or nil,
extra_height=(on_pause or on_settings) and 2 or 0,
subviews=subviews,
}
return dialog:show()
end
------------------------
-- Legacy classes
--
MessageBox = defclass(MessageBox, gui.FramedScreen)
MessageBox.focus_path = 'MessageBox'
MessageBox.ATTRS{
frame_style = gui.WINDOW_FRAME,
frame_inset = {l=1, t=1, b=1, r=0},
-- new attrs
on_accept = DEFAULT_NIL,
on_cancel = DEFAULT_NIL,
on_close = DEFAULT_NIL,
}
function MessageBox:init(info)
self:addviews{
widgets.Label{
view_id = 'label',
text = info.text,
text_pen = info.text_pen,
frame = { l = 0, t = 0 },
auto_height = true
}
}
end
function MessageBox:getWantedFrameSize()
local label = self.subviews.label
local width = math.max(self.frame_width or 0, 20, #(self.frame_title or '') + 4)
local text_area_width = label:getTextWidth() + 1
local text_height = label:getTextHeight()
local sw, sh = dfhack.screen.getWindowSize()
if text_height > sh - 4 then
-- account for scrollbar
text_area_width = text_area_width + 2
end
return math.max(width, text_area_width), text_height
end
function MessageBox:onRenderFrame(dc,rect)
MessageBox.super.onRenderFrame(self,dc,rect)
if self.on_accept then
dc:seek(rect.x1+2,rect.y2):key('LEAVESCREEN'):string('/'):key('SELECT')
end
end
function MessageBox:onDestroy()
if self.on_close then
self.on_close()
end
end
function MessageBox:onInput(keys)
if keys.SELECT or keys.LEAVESCREEN or keys._MOUSE_R then
self:dismiss()
if keys.SELECT and self.on_accept then
self.on_accept()
elseif (keys.LEAVESCREEN or keys._MOUSE_R) and self.on_cancel then
self.on_cancel()
end
return true
end
self:inputToSubviews(keys)
return true
end
InputBox = defclass(InputBox, MessageBox)
InputBox.focus_path = 'InputBox'
InputBox.ATTRS{
on_input = DEFAULT_NIL,
}
function InputBox:preinit(info)
info.on_accept = nil
end
function InputBox:init(info)
self:addviews{
widgets.EditField{
view_id = 'edit',
label_text = info.label_text,
text = info.input,
text_pen = info.input_pen,
frame = { l = 0, r = 0, h = 1 },
}
}
end
function InputBox:getWantedFrameSize()
local mw, mh = InputBox.super.getWantedFrameSize(self)
self.subviews.edit.frame.t = mh+1
return mw, mh+2
end
function InputBox:onInput(keys)
if keys.SELECT then
self:dismiss()
if self.on_input then
self.on_input(self.subviews.edit.text)
end
return true
elseif keys.LEAVESCREEN or keys._MOUSE_R then
self:dismiss()
if self.on_cancel then
self.on_cancel()
end
return true
end
self:inputToSubviews(keys)
return true
end
function showInputPrompt(title, text, tcolor, input, on_input, on_cancel, min_width)
local ib = InputBox{
frame_title = title,
text = text,
text_pen = tcolor,
input = input,
on_input = on_input,
on_cancel = on_cancel,
frame_width = min_width,
}
ib:show()
return ib
end
ListBox = defclass(ListBox, MessageBox)
ListBox.focus_path = 'ListBox'
ListBox.ATTRS{
with_filter = false,
dismiss_on_select = true,
dismiss_on_select2 = true,
cursor_pen = DEFAULT_NIL,
select_pen = DEFAULT_NIL,
on_select = DEFAULT_NIL,
on_select2 = DEFAULT_NIL,
select2_hint = DEFAULT_NIL,
row_height = DEFAULT_NIL,
list_frame_inset = DEFAULT_NIL,
}
function ListBox:preinit(info)
info.on_accept = nil
end
function ListBox:init(info)
local spen = dfhack.pen.parse(COLOR_CYAN, self.select_pen, nil, false)
local cpen = dfhack.pen.parse(COLOR_LIGHTCYAN, self.cursor_pen or self.select_pen, nil, true)
local list_widget = widgets.List
if self.with_filter then
list_widget = widgets.FilteredList
end
local on_submit2
if self.select2_hint or self.on_select2 then
on_submit2 = function(sel, obj)
if self.dismiss_on_select2 then self:dismiss() end
if self.on_select2 then self.on_select2(sel, obj) end
local cb = obj.on_select2
if cb then cb(obj, sel) end
end
end
self:addviews{
list_widget{
view_id = 'list',
selected = info.selected,
choices = info.choices,
icon_width = info.icon_width,
text_pen = spen,
cursor_pen = cpen,
on_submit = function(sel,obj)
if self.dismiss_on_select then self:dismiss() end
if self.on_select then self.on_select(sel, obj) end
local cb = obj.on_select or obj[2]
if cb then cb(obj, sel) end
end,
on_submit2 = on_submit2,
frame = { l = 0, r = 0},
frame_inset = self.list_frame_inset,
row_height = self.row_height,
}
}
end
function ListBox:onRenderFrame(dc,rect)
ListBox.super.onRenderFrame(self,dc,rect)
if self.select2_hint then
dc:seek(rect.x1+2,rect.y2):string('Shift-Click', COLOR_LIGHTGREEN):string(': '..self.select2_hint, COLOR_GREY)
end
end
function ListBox:getWantedFrameSize()
local mw, mh = ListBox.super.getWantedFrameSize(self)
local list = self.subviews.list
list.frame.t = mh+1
return math.max(mw, list:getContentWidth()), mh+3+math.min(18,list:getContentHeight())
end
function ListBox:onInput(keys)
if keys.LEAVESCREEN or keys._MOUSE_R then
self:dismiss()
if self.on_cancel then
self.on_cancel()
end
return true
end
self:inputToSubviews(keys)
return true
end
function showListPrompt(title, text, tcolor, choices, on_select, on_cancel, min_width, filter)
local lb = ListBox{
frame_title = title,
text = text,
text_pen = tcolor,
choices = choices,
on_select = on_select,
on_cancel = on_cancel,
frame_width = min_width,
with_filter = filter,
}
lb:show()
return lb
end
return _ENV