-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui_scrollbox.lua
693 lines (552 loc) · 17.3 KB
/
ui_scrollbox.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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
--Scrollbar and Scrollbox Widgets.
--Written by Cosmin Apreutesei. Public Domain.
local ui = require'ui'()
local box2d = require'box2d'
local glue = require'glue'
local noop = glue.noop
local clamp = glue.clamp
local lerp = glue.lerp
local scrollbar = ui.layer:subclass'scrollbar'
ui.scrollbar = scrollbar
scrollbar.iswidget = true
local grip = ui.layer:subclass'scrollbar_grip'
scrollbar.grip_class = grip
--default geometry
scrollbar.w = 12
scrollbar.h = 12
grip.min_w = 20
ui:style('scrollbar autohide, scrollbar autohide > scrollbar_grip', {
corner_radius = 100,
})
--default colors
scrollbar.background_color = '#222'
grip.background_color = '#999'
ui:style('scrollbar_grip :hot', {
background_color = '#bbb',
})
ui:style('scrollbar_grip :active', {
background_color = '#fff',
})
--default initial state
scrollbar.content_length = 0
scrollbar.view_length = 0
scrollbar.offset = 0 --in 0..content_length range
--default behavior
scrollbar.vertical = true --scrollbar is rotated 90deg to make it vertical
scrollbar.step = false --no snapping
scrollbar.autohide = false --hide when mouse is not near the scrollbar
scrollbar.autohide_empty = true --hide when content is smaller than the view
scrollbar.autohide_distance = 20 --distance around the scrollbar
scrollbar.click_scroll_length = 300
--^how much to scroll when clicking on the track (area around the grip)
--fade animation
scrollbar.opacity = 0 --prevent fade out on init
ui:style('scrollbar', {
opacity = 1,
transition_opacity = false,
})
--fade out
ui:style('scrollbar autohide', {
opacity = 0,
transition_opacity = true,
transition_delay_opacity = .5,
transition_duration_opacity = 1,
transition_blend_opacity = 'wait',
})
ui:style('scrollbar :empty', {
opacity = 0,
transition_opacity = false,
})
--fade in
ui:style('scrollbar autohide :near', {
opacity = .5,
transition_opacity = true,
transition_delay_opacity = 0,
transition_duration_opacity = .5,
transition_blend_opacity = 'replace',
})
--smooth scrolling
ui:style('scrollbar', {
transition_offset = true,
transition_duration_offset = .2,
})
--vertical property and tags
scrollbar:stored_property'vertical'
function scrollbar:after_set_vertical(vertical)
self:settag('vertical', vertical)
self:settag('horizontal', not vertical)
end
--grip geometry
local function snap_offset(i, step)
return step and i - i % step or i
end
local function grip_offset(x, bar_w, grip_w, content_length, view_length, step)
local offset = x / (bar_w - grip_w) * (content_length - view_length)
local offset = snap_offset(offset, step)
return offset ~= offset and 0 or offset
end
local function grip_segment(content_length, view_length, offset, bar_w, min_w)
local w = clamp(bar_w * view_length / content_length, min_w, bar_w)
local x = offset * (bar_w - w) / (content_length - view_length)
local x = clamp(x, 0, bar_w - w)
return x, w
end
function scrollbar:grip_rect()
local x, w = grip_segment(
self.content_length, self.view_length, self.offset,
self.cw, self.grip.min_w
)
local y, h = 0, self.ch
return x, y, w, h
end
function scrollbar:grip_offset()
return grip_offset(self.grip.x, self.cw, self.grip.w,
self.content_length, self.view_length, self.step)
end
function scrollbar:create_grip()
local grip = self:grip_class(self.grip)
function grip.drag(grip, dx, dy)
grip.x = clamp(0, grip.x + dx, self.cw - grip.w)
self:transition('offset', self:grip_offset(), 0)
end
return grip
end
--scroll state
scrollbar:stored_property'content_length'
scrollbar:stored_property'view_length'
scrollbar:stored_property'offset'
function scrollbar:clamp_and_snap_offset(offset)
local max_offset = self.content_length - self.view_length
offset = clamp(offset, 0, math.max(max_offset, 0))
return snap_offset(offset, self.step)
end
function scrollbar:set_offset(offset)
local old_offset = self._offset
offset = self:clamp_and_snap_offset(offset)
self._offset = offset
self:settag(':empty', self:empty(), true)
if offset ~= old_offset then
self:fire('offset_changed', offset, old_offset)
end
end
function scrollbar:after_set_content_length() self.offset = self.offset end
function scrollbar:after_set_view_length() self.offset = self.offset end
function scrollbar:reset(content_length, view_length, offset)
self._content_length = content_length
self._view_length = view_length
self.offset = offset
end
function scrollbar:empty()
return self.content_length <= self.view_length
end
scrollbar:init_ignore{content_length=1, view_length=1, offset=1}
function scrollbar:after_init(t)
self:reset(t.content_length, t.view_length, t.offset)
self.grip = self:create_grip()
end
--visibility state
function scrollbar:check_visible(...)
return self.visible
and (not self.autohide_empty or not self:empty())
and (not self.autohide or self:check_visible_autohide(...))
end
--scroll API
function scrollbar:scroll_to(offset, duration)
if self:check_visible() == 'hit_test' then
self:settag(':near', true)
self:sync()
self:settag(':near', false)
end
offset = self:clamp_and_snap_offset(offset)
--^we want to animate the clamped length!
self:transition('offset', offset, duration)
end
function scrollbar:scroll_to_view(x, w, duration)
local sx = self:end_value'offset'
local sw = self.view_length
self:scroll_to(clamp(sx, x + w - sw, x), duration)
end
function scrollbar:scroll(delta, duration)
self:scroll_to(self:end_value'offset' + delta, duration)
end
function scrollbar:scroll_pages(pages, duration)
self:scroll(self.view_length * (pages or 1), duration)
end
--mouse interaction: grip dragging
grip.mousedown_activate = true
grip.draggable = true
--mouse interaction: clicking on the track
scrollbar.mousedown_activate = true
--TODO: mousepress or mousehold
function scrollbar:mousedown(mx, my)
local delta = self.click_scroll_length * (mx < self.grip.x and -1 or 1)
self:transition('offset', self:end_value'offset' + delta)
end
--autohide feature
scrollbar:stored_property'autohide'
function scrollbar:after_set_autohide(autohide)
self:settag('autohide', autohide)
end
function scrollbar:hit_test_near(mx, my) --mx,my in window space
if not mx then
return 'hit_test'
end
mx, my = self:from_window(mx, my)
return box2d.hit(mx, my,
box2d.offset(self.autohide_distance, self:client_rect()))
end
function scrollbar:check_visible_autohide(mx, my)
return self.grip.active
or (not self.ui.active_widget and self:hit_test_near(mx, my))
end
function scrollbar:after_set_parent()
if not self.window then return end
self.window:on({'mousemove', self}, function(win, mx, my)
self:settag(':near', self:check_visible(mx, my))
end)
self.window:on({'mouseleave', self}, function(win)
local visible = self:check_visible()
if visible == 'hit_test' then visible = false end
self:settag(':near', visible)
end)
end
--drawing: rotate matrix for vertical scrollbar
function scrollbar:override_rel_matrix(inherited)
local mt = inherited(self)
if self._vertical then
mt:rotate(math.rad(90)):translate(0, -self.h)
end
return mt
end
--drawing: sync grip geometry; sync :near tag.
function scrollbar:before_sync_layout_children()
local g = self.grip
g.x, g.y, g.w, g.h = self:grip_rect()
local visible = self:check_visible()
if visible ~= 'hit_test' then
self:settag(':near', visible, true)
end
end
--scrollbox ------------------------------------------------------------------
local scrollbox = ui.layer:subclass'scrollbox'
ui.scrollbox = scrollbox
scrollbox.iswidget = true
scrollbox.view_class = ui.layer
scrollbox.content_class = ui.layer
scrollbox.vscrollbar_class = scrollbar
scrollbox.hscrollbar_class = scrollbar
function scrollbox:after_init(t)
self.vscrollbar = self:vscrollbar_class({
tags = 'vscrollbar',
scrollbox = self,
vertical = true,
iswidget = false,
}, self.scrollbar, self.vscrollbar)
self.hscrollbar = self:hscrollbar_class({
tags = 'hscrollbar',
scrollbox = self,
vertical = false,
iswidget = false,
}, self.scrollbar, self.hscrollbar)
--make autohide scrollbars to show and hide in sync.
--TODO: remove the brk anti-recursion barrier hack.
local vs = self.vscrollbar
local hs = self.hscrollbar
function vs:override_check_visible_autohide(inherited, mx, my, brk)
return inherited(self, mx, my)
or (not brk and hs.autohide and hs:check_visible(mx, my, true))
end
function hs:override_check_visible_autohide(inherited, mx, my, brk)
return inherited(self, mx, my)
or (not brk and vs.autohide and vs:check_visible(mx, my, true))
end
--NOTE: the view is created last so it is freed first, so that the
--content can still access the scrollbox on its dying breath!
self.view = self:view_class({
tags = 'scrollbox_view',
clip_content = true, --we want to pad the content, but not clip it
sync_layout = noop, --prevent auto-sync'ing content's layout
}, self.view)
if not self.content or not self.content.islayer then
self.content = self.content_class(self.ui, {
tags = 'scrollbox_content',
parent = self.view,
}, self.content)
elseif self.content then
self.content.parent = self.view
end
end
--mouse interaction: wheel scrolling
scrollbox.vscrollable = true
scrollbox.hscrollable = true
scrollbox.wheel_scroll_length = 50 --pixels per scroll wheel notch
function scrollbox:mousewheel(delta)
self.vscrollbar:scroll(-delta * self.wheel_scroll_length)
end
--drawing
scrollbox:forward_properties('view', 'view_', {
padding=1,
padding_left=1,
padding_right=1,
padding_top=1,
padding_bottom=1,
})
function scrollbox:after_init(t)
if t.padding ~= nil then self.view.padding = t.padding end
if t.padding_left ~= nil then self.view.padding_left = t.padding_left end
if t.padding_right ~= nil then self.view.padding_right = t.padding_right end
if t.padding_top ~= nil then self.view.padding_top = t.padding_top end
if t.padding_bottom ~= nil then self.view.padding_bottom = t.padding_bottom end
end
--stretch content to the view size to avoid scrolling on that dimension.
scrollbox.auto_h = false
scrollbox.auto_w = false
function scrollbox:sync_layout_children()
local vs = self.vscrollbar
local hs = self.hscrollbar
local view = self.view
local content = self.content
content.parent = view
local w, h = self:client_size()
local vs_margin = vs.margin or 0
local hs_margin = hs.margin or 0
local vs_overlap = vs.autohide or vs.overlap or not vs.visible
local hs_overlap = hs.autohide or hs.overlap or not hs.visible
local sw = vs.h + vs_margin
local sh = hs.h + hs_margin
--for `auto_w`, lay out the content with `min_w` set to view's cw, and then
--get its size. if the content overflows vertically, another layout pass
--is necessary, this time with a smaller `min_w`, making room for the
--needed vertical scrollbar, under the assumption that the content will
--still overflow vertically under the smaller `min_w`. the same logic
--applies symmetrically for `auto_h`.
local cw0 = self.auto_w and w - ((vs_overlap or vs.autohide_empty) and 0 or sw)
local ch0 = self.auto_h and h - ((hs_overlap or hs.autohide_empty) and 0 or sh)
if cw0 and ch0 then
self.ui:warn'both auto_w and auto_h specified. auto_h ignored.'
ch0 = nil
end
::reflow::
if cw0 or ch0 then
content:sync_layout_separate_axes(cw0 and 'xy' or 'yx', cw0, ch0)
else
content:sync_layout()
end
local cw, ch = content:size()
--compute view dimensions by deciding which scrollbar is either hidden
--or is overlapping the view box so it takes no space of its own.
local vs_nospace, hs_nospace
local vs_nospace = vs_overlap
or (vs.autohide_empty and ch <= h and (ch <= h - sh or 'depends'))
local hs_nospace = hs_overlap
or (hs.autohide_empty and cw <= w and (cw <= w - sw or 'depends'))
if (vs_nospace == 'depends' and not hs_nospace)
or (hs_nospace == 'depends' and not vs_nospace)
then
vs_nospace = false
hs_nospace = false
end
view.w = w - (vs_nospace and 0 or sw)
view.h = h - (hs_nospace and 0 or sh)
--if the view's `cw` is smaller than the preliminary `w` on which content
--reflowing was based on for `auto_w`, then do it again with the real `cw`.
--the same applies for `ch` for `auto_h`.
if cw0 and view.cw < cw0 then
cw0 = view.cw
goto reflow
elseif ch0 and view.ch < ch0 then
ch0 = view.ch
goto reflow
end
--reset the scrollbars state.
hs:reset(cw, view.cw, hs.offset)
vs:reset(ch, view.ch, vs.offset)
--scroll the content layer.
content.x = -hs.offset * content.w / cw -- content.pw1
content.y = -vs.offset * content.h / ch -- content.ph1
--compute scrollbar dimensions.
vs.w = view.h - 2 * vs_margin --.w is its height!
hs.w = view.w - 2 * hs_margin
--check which scrollbars are visible and actually overlapping the view.
--NOTE: scrollbars state must already be set here since we call `empty()`.
local hs_overlapping = hs.visible and hs_overlap
and (not hs.autohide_empty or not hs:empty())
local vs_overlapping = vs.visible and vs_overlap
and (not vs.autohide_empty or not vs:empty())
--shorten the ends of scrollbars so they don't overlap each other.
vs.w = vs.w - (vs_overlap and hs_overlapping and sh or 0)
hs.w = hs.w - (hs_overlap and vs_overlapping and sw or 0)
--compute scrollbar positions.
vs.x = view.w - (vs_nospace and sw or 0)
hs.y = view.h - (hs_nospace and sh or 0)
vs.y = vs_margin
hs.x = hs_margin
for _,layer in ipairs(self) do
if layer ~= content then
layer:sync_layout() --recurse
end
end
end
--scroll API
--x, y is in content's content space.
function scrollbox:scroll_to_view(x, y, w, h)
x, y = self.content:from_content(x, y)
self.hscrollbar:scroll_to_view(x, w)
self.vscrollbar:scroll_to_view(y, h)
end
--x, y, w, h is in own content space.
function scrollbox:make_visible(x, y, w, h)
x, y = self:to_other(self.content, x, y)
self:scroll_to_view(x, y, w, h)
end
--multi-line editbox ---------------------------------------------------------
local textarea = scrollbox:subclass'textarea'
ui.textarea = textarea
textarea.tags = 'standalone'
textarea.auto_w = true
textarea.view_padding_left = 0
textarea.view_padding_right = 6
local editbox = ui.layer:subclass'textarea_content'
textarea.content_class = editbox
editbox.layout = 'textbox'
editbox.text_align_x = 'auto'
editbox.text_align_y = 'top'
editbox.focusable = true
editbox.text_selectable = true
editbox.text_editable = true
editbox.clip_content = false
function textarea:get_value() return self.editbox.value end
function textarea:set_value(val) self.editbox.value = val end
textarea:init_ignore{value=1}
function textarea:after_init(t)
self.editbox = self.content
self.value = t.value
end
--demo -----------------------------------------------------------------------
if not ... then require('ui_demo')(function(ui, win)
win.view.item_align_x = 'center'
win.view.item_align_y = 'center'
ui:style('scrollbox', {
border_width = 1,
border_color = '#f00',
})
local function mkcontent(w, h)
return ui:layer{
w = w or 2000,
h = h or 32000,
border_width = 20,
border_color = '#ff0',
background_type = 'gradient',
background_colors = {'#ff0', 0.5, '#00f'},
background_x2 = 100,
background_y2 = 100,
background_extend = 'repeat',
}
end
local s = [[
Lorem ipsum dolor sit amet, quod oblique vivendum ex sed. Impedit nominavi maluisset sea ut. Utroque apeirian maluisset cum ut. Nihil appellantur at his, fugit noluisse eu vel, mazim mandamus ex quo.
Mei malis eruditi ne. Movet volumus instructior ea nec. Vel cu minimum molestie atomorum, pro iudico facilisi et, sea elitr partiendo at. An has fugit assum accumsan.]]
--not autohide, custom bar metrics
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(),
vscrollbar = {h = 20, margin = 20},
hscrollbar = {h = 30, margin = 10},
}
--overlap, custom bar metrics
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(),
vscrollbar = {h = 20, margin = 20},
hscrollbar = {h = 30, margin = 10},
scrollbar = {overlap = true},
}
--not autohide, autohide_empty vertical
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(nil, 165),
}
--not autohide, autohide_empty horizontal
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(165),
autohide = true,
}
--not autohide, autohide_empty horizontal -> vertical
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(185, 175),
autohide = true,
}
--not autohide, autohide_empty vertical -> horizontal
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(175, 185),
autohide = true,
}
--autohide_empty case
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(180, 180),
}
--autohide
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(),
scrollbar = {
autohide = true,
},
}
--autohide, autohide_empty vertical
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(nil, 175),
scrollbar = {
autohide = true,
}
}
--autohide, autohide_empty horizontal
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(175),
scrollbar = {
autohide = true,
}
}
--autohide horizontal only
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
content = mkcontent(175),
hscrollbar = {
autohide = true,
}
}
--auto_w
ui:scrollbox{
parent = win,
min_cw = 180, min_ch = 180,
auto_w = true,
content = {
layout = 'textbox',
text_align_x = 'left',
text_align_y = 'top',
text = s,
},
}
ui:textarea{
parent = win,
min_cw = 180, min_ch = 180,
value = s,
}
end) end