forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab_bar.lua
More file actions
438 lines (380 loc) · 12.9 KB
/
Copy pathtab_bar.lua
File metadata and controls
438 lines (380 loc) · 12.9 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
local Widget = require('gui.widgets.widget')
local ResizingPanel = require('gui.widgets.containers.resizing_panel')
local Label = require('gui.widgets.labels.label')
local Panel = require('gui.widgets.containers.panel')
local utils = require('utils')
local to_pen = dfhack.pen.parse
---@class widgets.TabPens
---@field text_mode_tab_pen dfhack.pen
---@field text_mode_label_pen dfhack.pen
---@field lt dfhack.pen
---@field lt2 dfhack.pen
---@field t dfhack.pen
---@field rt2 dfhack.pen
---@field rt dfhack.pen
---@field lb dfhack.pen
---@field lb2 dfhack.pen
---@field b dfhack.pen
---@field rb2 dfhack.pen
---@field rb dfhack.pen
local TSO = df.global.init.tabs_texpos[0] -- tab spritesheet offset
local DEFAULT_ACTIVE_TAB_PENS = {
text_mode_tab_pen=to_pen{fg=COLOR_YELLOW},
text_mode_label_pen=to_pen{fg=COLOR_WHITE},
lt=to_pen{tile=TSO+5, write_to_lower=true},
lt2=to_pen{tile=TSO+6, write_to_lower=true},
t=to_pen{tile=TSO+7, fg=COLOR_BLACK, write_to_lower=true, top_of_text=true},
rt2=to_pen{tile=TSO+8, write_to_lower=true},
rt=to_pen{tile=TSO+9, write_to_lower=true},
lb=to_pen{tile=TSO+15, write_to_lower=true},
lb2=to_pen{tile=TSO+16, write_to_lower=true},
b=to_pen{tile=TSO+17, fg=COLOR_BLACK, write_to_lower=true, bottom_of_text=true},
rb2=to_pen{tile=TSO+18, write_to_lower=true},
rb=to_pen{tile=TSO+19, write_to_lower=true},
}
local DEFAULT_INACTIVE_TAB_PENS = {
text_mode_tab_pen=to_pen{fg=COLOR_BROWN},
text_mode_label_pen=to_pen{fg=COLOR_DARKGREY},
lt=to_pen{tile=TSO+0, write_to_lower=true},
lt2=to_pen{tile=TSO+1, write_to_lower=true},
t=to_pen{tile=TSO+2, fg=COLOR_WHITE, write_to_lower=true, top_of_text=true},
rt2=to_pen{tile=TSO+3, write_to_lower=true},
rt=to_pen{tile=TSO+4, write_to_lower=true},
lb=to_pen{tile=TSO+10, write_to_lower=true},
lb2=to_pen{tile=TSO+11, write_to_lower=true},
b=to_pen{tile=TSO+12, fg=COLOR_WHITE, write_to_lower=true, bottom_of_text=true},
rb2=to_pen{tile=TSO+13, write_to_lower=true},
rb=to_pen{tile=TSO+14, write_to_lower=true},
}
---------
-- Tab --
---------
---@class widgets.Tab.attrs: widgets.Widget.attrs
---@field id? string|integer
---@field label string
---@field on_select? function
---@field get_pens? fun(): widgets.TabPens
---@class widgets.Tab.attrs.partial: widgets.Tab.attrs
---@class widgets.Tab.initTable: widgets.Tab.attrs
---@field label string
---@class widgets.Tab: widgets.Widget, widgets.Tab.attrs
---@field super widgets.Widget
---@field ATTRS widgets.Tab.attrs|fun(attributes: widgets.Tab.attrs.partial)
---@overload fun(init_table: widgets.Tab.initTable): self
Tab = defclass(Tabs, Widget)
Tab.ATTRS{
id=DEFAULT_NIL,
label=DEFAULT_NIL,
on_select=DEFAULT_NIL,
get_pens=DEFAULT_NIL,
}
function Tab:preinit(init_table)
init_table.frame = init_table.frame or {}
init_table.frame.w = #init_table.label + 4
init_table.frame.h = 2
end
function Tab:onRenderBody(dc)
local pens = self.get_pens()
dc:seek(0, 0)
if dfhack.screen.inGraphicsMode() then
dc:char(nil, pens.lt):char(nil, pens.lt2)
for i=1,#self.label do
dc:char(self.label:sub(i,i), pens.t)
end
dc:char(nil, pens.rt2):char(nil, pens.rt)
dc:seek(0, 1)
dc:char(nil, pens.lb):char(nil, pens.lb2)
for i=1,#self.label do
dc:char(self.label:sub(i,i), pens.b)
end
dc:char(nil, pens.rb2):char(nil, pens.rb)
else
local tp = pens.text_mode_tab_pen
dc:char(' ', tp):char('/', tp)
for i=1,#self.label do
dc:char('-', tp)
end
dc:char('\\', tp):char(' ', tp)
dc:seek(0, 1)
dc:char('/', tp):char('-', tp)
dc:string(self.label, pens.text_mode_label_pen)
dc:char('-', tp):char('\\', tp)
end
end
function Tab:onInput(keys)
if Tab.super.onInput(self, keys) then return true end
if keys._MOUSE_L and self:getMousePos() then
self.on_select(self.id)
return true
end
end
-------------
-- Tab Bar --
-------------
---@class widgets.TabBar.attrs: widgets.ResizingPanel.attrs
---@field labels string[]
---@field on_select? function
---@field get_cur_page? function
---@field active_tab_pens widgets.TabPens
---@field inactive_tab_pens widgets.TabPens
---@field get_pens? fun(index: integer, tabbar: self): widgets.TabPens
---@field key string
---@field key_back string
---@field wrap boolean
---@field scroll_step integer
---@field scroll_key string
---@field scroll_key_back string
---@field fast_scroll_modifier integer
---@field scroll_into_view_offset integer
---@field scroll_label_text_pen dfhack.pen
---@field scroll_label_text_hpen dfhack.pen
---@class widgets.TabBar.attrs.partial: widgets.TabBar.attrs
---@class widgets.TabBar.initTable: widgets.TabBar.attrs
---@field labels string[]
---@class widgets.TabBar: widgets.ResizingPanel, widgets.TabBar.attrs
---@field super widgets.ResizingPanel
---@field ATTRS widgets.TabBar.attrs|fun(attribute: widgets.TabBar.attrs.partial)
---@overload fun(init_table: widgets.TabBar.initTable): self
TabBar = defclass(TabBar, ResizingPanel)
TabBar.ATTRS{
labels=DEFAULT_NIL,
on_select=DEFAULT_NIL,
get_cur_page=DEFAULT_NIL,
active_tab_pens=DEFAULT_ACTIVE_TAB_PENS,
inactive_tab_pens=DEFAULT_INACTIVE_TAB_PENS,
get_pens=DEFAULT_NIL,
key='CUSTOM_CTRL_T',
key_back='CUSTOM_CTRL_Y',
wrap = true,
scroll_step = 10,
scroll_key = 'CUSTOM_ALT_T',
scroll_key_back = 'CUSTOM_ALT_Y',
fast_scroll_modifier = 3,
scroll_into_view_offset = 5,
scroll_label_text_pen = DEFAULT_NIL,
scroll_label_text_hpen = DEFAULT_NIL,
}
local TO_THE_RIGHT = string.char(16)
local TO_THE_LEFT = string.char(17)
---@param self widgets.TabBar
function TabBar:init()
self.scrollable = false
self.scroll_offset = 0
self.first_render = true
local panel = Panel{
view_id='TabBar__tabs',
frame={t=0, l=0, h=2},
frame_inset={l=0},
}
for idx,label in ipairs(self.labels) do
panel:addviews{
Tab{
frame={t=0, l=0},
id=idx,
label=label,
on_select=function()
self.scrollTabIntoView(self, idx)
self.on_select(idx)
end,
get_pens=self.get_pens and function()
return self.get_pens(idx, self)
end or function()
if self.get_cur_page() == idx then
return self.active_tab_pens
end
return self.inactive_tab_pens
end,
}
}
end
self:addviews{panel}
if not self.wrap then
self:addviews{
Label{
view_id='TabBar__scroll_left',
frame={t=0, l=0, w=1},
text_pen=self.scroll_label_text_pen,
text_hpen=self.scroll_label_text_hpen,
text=TO_THE_LEFT,
visible = false,
on_click=function()
self:scrollLeft()
end,
},
Label{
view_id='TabBar__scroll_right',
frame={t=0, l=0, w=1},
text_pen=self.scroll_label_text_pen,
text_hpen=self.scroll_label_text_hpen,
text=TO_THE_RIGHT,
visible = false,
on_click=function()
self:scrollRight()
end,
},
}
end
end
function TabBar:updateScrollElements()
self:showScrollLeft()
self:showScrollRight()
self:updateTabPanelPosition()
end
function TabBar:leftScrollVisible()
return self.scroll_offset < 0
end
function TabBar:showScrollLeft()
if self.wrap then return end
self:scrollLeftElement().visible = self:leftScrollVisible()
end
function TabBar:rightScrollVisible()
return self.scroll_offset > self.offset_to_show_last_tab
end
function TabBar:showScrollRight()
if self.wrap then return end
self:scrollRightElement().visible = self:rightScrollVisible()
end
function TabBar:updateTabPanelPosition()
self:tabsElement().frame_inset.l = self.scroll_offset
self:tabsElement():updateLayout(self.frame_body)
end
function TabBar:tabsElement()
return self.subviews.TabBar__tabs
end
function TabBar:scrollLeftElement()
return self.subviews.TabBar__scroll_left
end
function TabBar:scrollRightElement()
return self.subviews.TabBar__scroll_right
end
function TabBar:scrollTabIntoView(idx)
if self.wrap or not self.scrollable then return end
local tab = self:tabsElement().subviews[idx]
local tab_l = tab.frame.l
local tab_r = tab.frame.l + tab.frame.w
local tabs_l = self:tabsElement().frame.l
local tabs_r = tabs_l + self.frame_body.width
local scroll_offset = self.scroll_offset
if tab_l < tabs_l - scroll_offset then
self.scroll_offset = tabs_l - tab_l + self.scroll_into_view_offset
elseif tab_r > tabs_r - scroll_offset then
self.scroll_offset = self.scroll_offset - (tab_r - tabs_r) - self.scroll_into_view_offset
end
self:capScrollOffset()
self:updateScrollElements()
end
function TabBar:capScrollOffset()
if self.scroll_offset > 0 then
self.scroll_offset = 0
elseif self.scroll_offset < self.offset_to_show_last_tab then
self.scroll_offset = self.offset_to_show_last_tab
end
end
function TabBar:scrollRight(alternate_step)
if not self:scrollRightElement().visible then return end
self.scroll_offset = self.scroll_offset - (alternate_step and alternate_step or self.scroll_step)
self:capScrollOffset()
self:updateScrollElements()
end
function TabBar:scrollLeft(alternate_step)
if not self:scrollLeftElement().visible then return end
self.scroll_offset = self.scroll_offset + (alternate_step and alternate_step or self.scroll_step)
self:capScrollOffset()
self:updateScrollElements()
end
function TabBar:isMouseOver()
for _, sv in ipairs(self:tabsElement().subviews) do
if utils.getval(sv.visible) and sv:getMouseFramePos() then return true end
end
end
function TabBar:postComputeFrame(body)
local all_tabs_width = 0
local t, l, width = 0, 0, body.width
self.scrollable = false
self.last_post_compute_width = self.post_compute_width or 0
self.post_compute_width = width
local tab_rows = 1
for _,tab in ipairs(self:tabsElement().subviews) do
tab.visible = true
if l > 0 and l + tab.frame.w > width then
if self.wrap then
t = t + 2
l = 0
tab_rows = tab_rows + 1
else
self.scrollable = true
end
end
tab.frame.t = t
tab.frame.l = l
l = l + tab.frame.w
all_tabs_width = all_tabs_width + tab.frame.w
end
self.offset_to_show_last_tab = -(all_tabs_width - self.post_compute_width)
if self.scrollable and not self.wrap then
self:scrollRightElement().frame.l = width - 1
if self.last_post_compute_width ~= self.post_compute_width then
self.scroll_offset = 0
end
end
if self.first_render then
self.first_render = false
if not self.wrap then
self:scrollTabIntoView(self.get_cur_page())
end
end
-- we have to calculate the height of this based on the number of tab rows we will have
-- so that autoarrange_subviews will work correctly
self:tabsElement().frame.h = tab_rows * 2
self:updateScrollElements()
end
function TabBar:fastStep()
return self.scroll_step * self.fast_scroll_modifier
end
function TabBar:onInput(keys)
if TabBar.super.onInput(self, keys) then return true end
if not self.wrap then
if self:isMouseOver() then
if keys.CONTEXT_SCROLL_UP then
self:scrollLeft()
return true
end
if keys.CONTEXT_SCROLL_DOWN then
self:scrollRight()
return true
end
if keys.CONTEXT_SCROLL_PAGEUP then
self:scrollLeft(self:fastStep())
return true
end
if keys.CONTEXT_SCROLL_PAGEDOWN then
self:scrollRight(self:fastStep())
return true
end
end
if self.scroll_key and keys[self.scroll_key] then
self:scrollRight()
return true
end
if self.scroll_key_back and keys[self.scroll_key_back] then
self:scrollLeft()
return true
end
end
if self.key and keys[self.key] then
local zero_idx = self.get_cur_page() - 1
local next_zero_idx = (zero_idx + 1) % #self.labels
self.scrollTabIntoView(self, next_zero_idx + 1)
self.on_select(next_zero_idx + 1)
return true
end
if self.key_back and keys[self.key_back] then
local zero_idx = self.get_cur_page() - 1
local prev_zero_idx = (zero_idx - 1) % #self.labels
self.scrollTabIntoView(self, prev_zero_idx + 1)
self.on_select(prev_zero_idx + 1)
return true
end
end
TabBar.Tab = Tab
return TabBar