| 1 |
gui.widgets.ScrollArea = { |
| 2 |
new: func(parent, style = nil, cfg = nil) |
| 3 |
{ |
| 4 |
style = style or canvas.style; |
| 5 |
var cfg = Config.new(cfg); |
| 6 |
var m = gui.Widget.new(gui.widgets.ScrollArea, cfg); |
| 7 |
m._focus_policy = m.NoFocus; |
| 8 |
m._content_pos = [0, 0]; |
| 9 |
m._scroller_pos = [0, 0]; |
| 10 |
m._max_scroll = [0, 0]; |
| 11 |
m._layout = nil; |
| 12 |
|
| 13 |
if( style != nil ) |
| 14 |
m._setView( style.createWidget(parent, "scroll-area", cfg) ); |
| 15 |
|
| 16 |
m.setMinimumSize([32, 32]); |
| 17 |
|
| 18 |
return m; |
| 19 |
}, |
| 20 |
setLayout: func(l) |
| 21 |
{ |
| 22 |
me._layout = l; |
| 23 |
l.setParent(me); |
| 24 |
return me.update(); |
| 25 |
}, |
| 26 |
getContent: func() |
| 27 |
{ |
| 28 |
return me._view.content; |
| 29 |
}, |
| 30 |
# Set the background color for the content area. |
| 31 |
# |
| 32 |
# @param color Vector of 3 or 4 values in [0, 1] |
| 33 |
setColorBackground: func |
| 34 |
{ |
| 35 |
if( size(arg) == 1 ) |
| 36 |
var arg = arg[0]; |
| 37 |
me._view.setColorBackground(arg); |
| 38 |
return me; |
| 39 |
}, |
| 40 |
# Reset the size of the content area, e.g. on window resize. |
| 41 |
# |
| 42 |
# @param sz Vector of [x,y] values. |
| 43 |
setSize: func |
| 44 |
{ |
| 45 |
if( size(arg) == 1 ) |
| 46 |
var arg = arg[0]; |
| 47 |
var (x,y) = arg; |
| 48 |
me._size = [x,y]; |
| 49 |
return me.update(); |
| 50 |
}, |
| 51 |
# Move contents to the coordinates x,y (or as far as possible) |
| 52 |
# |
| 53 |
# @param x The x coordinate (positive is right) |
| 54 |
# @param y The y coordinate (positive is down) |
| 55 |
scrollTo: func(x, y) |
| 56 |
{ |
| 57 |
me._content_pos[0] = x; |
| 58 |
me._content_pos[1] = y; |
| 59 |
|
| 60 |
return me.update(); |
| 61 |
}, |
| 62 |
# Move the scrollable area to the top-most position |
| 63 |
scrollToTop: func me.scrollTo( me._content_pos[0], 0 ), |
| 64 |
# Move the scrollable area to the bottom-most position |
| 65 |
scrollToBottom: func me.scrollTo( me._content_pos[0], me._max_scroll[1] ), |
| 66 |
# Move the scrollable area to the left-most position |
| 67 |
scrollToLeft: func me.scrollTo( 0, me._content_pos[1] ), |
| 68 |
# Move the scrollable area to the right-most position |
| 69 |
scrollToRight: func me.scrollTo( me._max_scroll[0], me._content_pos[1] ), |
| 70 |
# Move content by given delta |
| 71 |
scrollBy: func(x, y) |
| 72 |
{ |
| 73 |
return me.scrollTo( me._content_pos[0] + x, |
| 74 |
me._content_pos[1] + y ); |
| 75 |
}, |
| 76 |
# Set horizontal scrollbar position |
| 77 |
horizScrollBarTo: func(x) |
| 78 |
{ |
| 79 |
if( me._scroller_delta[0] < 1 ) |
| 80 |
return me; |
| 81 |
|
| 82 |
me.scrollTo( me._max_scroll[0] * (x / me._scroller_delta[0]), |
| 83 |
me._content_pos[1] ); |
| 84 |
}, |
| 85 |
# Set vertical scrollbar position |
| 86 |
vertScrollBarTo: func(y) |
| 87 |
{ |
| 88 |
if( me._scroller_delta[1] < 1 ) |
| 89 |
return me; |
| 90 |
|
| 91 |
me.scrollTo( me._content_pos[0], |
| 92 |
me._max_scroll[1] * (y / me._scroller_delta[1]) ); |
| 93 |
}, |
| 94 |
# Move horizontal scrollbar by given offset |
| 95 |
horizScrollBarBy: func(dx) |
| 96 |
{ |
| 97 |
me.horizScrollBarTo(me._scroller_pos[0] + dx); |
| 98 |
}, |
| 99 |
# Move vertical scrollbar by given offset |
| 100 |
vertScrollBarBy: func(dy) |
| 101 |
{ |
| 102 |
me.vertScrollBarTo(me._scroller_pos[1] + dy); |
| 103 |
}, |
| 104 |
# Update scroll bar and content area. |
| 105 |
# |
| 106 |
# Needs to be called when the size of the content changes. |
| 107 |
update: func(bb=nil) |
| 108 |
{ |
| 109 |
if (bb == nil) bb = me._updateBB(); |
| 110 |
if (bb == nil) return me; |
| 111 |
|
| 112 |
var offset = [ me._content_offset[0] - me._content_pos[0], |
| 113 |
me._content_offset[1] - me._content_pos[1] ]; |
| 114 |
me.getContent().setTranslation(offset); |
| 115 |
|
| 116 |
me._view.update(me); |
| 117 |
me.getContent().update(); |
| 118 |
|
| 119 |
return me; |
| 120 |
}, |
| 121 |
# protected: |
| 122 |
_setView: func(view) |
| 123 |
{ |
| 124 |
call(gui.Widget._setView, [view], me); |
| 125 |
|
| 126 |
view.vert.addEventListener("mousedown", func(e) me._dragStart(e)); |
| 127 |
view.horiz.addEventListener("mousedown", func(e) me._dragStart(e)); |
| 128 |
view._root.addEventListener("mousedown", func(e) |
| 129 |
{ |
| 130 |
me._drag_offsetX = me._content_pos[0] + e.clientX; |
| 131 |
me._drag_offsetY = me._content_pos[1] + e.clientY; |
| 132 |
}); |
| 133 |
|
| 134 |
view.vert.addEventListener |
| 135 |
( |
| 136 |
"drag", |
| 137 |
func(e) |
| 138 |
{ |
| 139 |
if( !me._enabled ) |
| 140 |
return; |
| 141 |
|
| 142 |
me.vertScrollBarTo(me._drag_offsetY + e.clientY); |
| 143 |
e.stopPropagation(); |
| 144 |
} |
| 145 |
); |
| 146 |
view.horiz.addEventListener |
| 147 |
( |
| 148 |
"drag", |
| 149 |
func(e) |
| 150 |
{ |
| 151 |
if( !me._enabled ) |
| 152 |
return; |
| 153 |
|
| 154 |
me.horizScrollBarTo(me._drag_offsetX + e.clientX); |
| 155 |
e.stopPropagation(); |
| 156 |
} |
| 157 |
); |
| 158 |
|
| 159 |
view._root.addEventListener |
| 160 |
( |
| 161 |
"drag", |
| 162 |
func(e) |
| 163 |
{ |
| 164 |
if( !me._enabled ) |
| 165 |
return; |
| 166 |
|
| 167 |
me.scrollTo( me._drag_offsetX - e.clientX, |
| 168 |
me._drag_offsetY - e.clientY ); |
| 169 |
e.stopPropagation(); |
| 170 |
} |
| 171 |
); |
| 172 |
view._root.addEventListener |
| 173 |
( |
| 174 |
"wheel", |
| 175 |
func(e) |
| 176 |
{ |
| 177 |
if( !me._enabled ) |
| 178 |
return; |
| 179 |
|
| 180 |
me.scrollBy(0, 30 * -e.deltaY); # TODO make step size configurable |
| 181 |
e.stopPropagation(); |
| 182 |
} |
| 183 |
); |
| 184 |
}, |
| 185 |
_dragStart: func(e) |
| 186 |
{ |
| 187 |
me._drag_offsetX = me._scroller_pos[0] - e.clientX; |
| 188 |
me._drag_offsetY = me._scroller_pos[1] - e.clientY; |
| 189 |
e.stopPropagation(); |
| 190 |
}, |
| 191 |
_updateBB: func() |
| 192 |
{ |
| 193 |
# TODO only update on content resize |
| 194 |
if( me._layout == nil ) |
| 195 |
{ |
| 196 |
var bb = me.getContent().getTightBoundingBox(); |
| 197 |
|
| 198 |
if( bb[2] < bb[0] or bb[3] < bb[1] ) |
| 199 |
return nil; |
| 200 |
var w = bb[2] - bb[0]; |
| 201 |
var h = bb[3] - bb[1]; |
| 202 |
|
| 203 |
var cur_offset = me.getContent().getTranslation(); |
| 204 |
me._content_offset = [cur_offset[0] - bb[0], cur_offset[1] - bb[1]]; |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
var min_size = me._layout.minimumSize(); |
| 209 |
var max_size = me._layout.maximumSize(); |
| 210 |
var size_hint = me._layout.sizeHint(); |
| 211 |
var w = math.min(max_size[0], math.max(size_hint[0], me._size[0])); |
| 212 |
var h = math.max( |
| 213 |
math.min(max_size[1], math.max(size_hint[1], me._size[1])), |
| 214 |
me._layout.heightForWidth(w) |
| 215 |
); |
| 216 |
|
| 217 |
me._layout.setGeometry([0, 0, w, h]); |
| 218 |
|
| 219 |
# Layout always has the origin at (0, 0) |
| 220 |
me._content_offset = [0, 0]; |
| 221 |
} |
| 222 |
|
| 223 |
me._max_scroll[0] = math.max(0, w - me._size[0]); |
| 224 |
me._max_scroll[1] = math.max(0, h - me._size[1]); |
| 225 |
me._content_size = [w, h]; |
| 226 |
|
| 227 |
# keep position within limit and only integer (to prevent artifacts on text, |
| 228 |
# lines, etc. not alligned with pixel grid) |
| 229 |
me._content_pos[0] = |
| 230 |
math.max(0, math.min( math.round(me._content_pos[0]), me._max_scroll[0])); |
| 231 |
me._content_pos[1] = |
| 232 |
math.max(0, math.min( math.round(me._content_pos[1]), me._max_scroll[1])); |
| 233 |
|
| 234 |
me._scroller_size = [0, 0]; # scroller size |
| 235 |
me._scroller_offset = [0, 0]; # scroller minimum pos (eg. add offset for |
| 236 |
# scrolling with buttons) |
| 237 |
me._scroller_delta = [0, 0]; # scroller max travel distance |
| 238 |
|
| 239 |
# update scroller size/offset/max delta |
| 240 |
me._view._updateScrollMetrics(me, 0); |
| 241 |
me._view._updateScrollMetrics(me, 1); |
| 242 |
|
| 243 |
# update current scrollbar positions |
| 244 |
me._scroller_pos[0] = |
| 245 |
me._max_scroll[0] > 0 |
| 246 |
? (me._content_pos[0] / me._max_scroll[0]) * me._scroller_delta[0] |
| 247 |
: 0; |
| 248 |
me._scroller_pos[1] = |
| 249 |
me._max_scroll[1] > 0 |
| 250 |
? (me._content_pos[1] / me._max_scroll[1]) * me._scroller_delta[1] |
| 251 |
: 0; |
| 252 |
} |
| 253 |
}; |