2014-03-19 22:20:09 +00:00
|
|
|
gui.widgets.ScrollArea = {
|
|
|
|
new: func(parent, style, cfg)
|
|
|
|
{
|
|
|
|
var cfg = Config.new(cfg);
|
|
|
|
var m = gui.Widget.new(gui.widgets.ScrollArea);
|
2014-06-08 22:37:14 +00:00
|
|
|
m._focus_policy = m.NoFocus;
|
2014-06-25 22:30:33 +00:00
|
|
|
m._content_pos = [0, 0];
|
|
|
|
m._scroller_pos = [0, 0];
|
2014-04-18 21:49:11 +00:00
|
|
|
m._max_scroll = [0, 0];
|
2014-06-10 16:46:52 +00:00
|
|
|
m._layout = nil;
|
2014-03-19 22:20:09 +00:00
|
|
|
|
|
|
|
if( style != nil )
|
2014-06-08 22:37:14 +00:00
|
|
|
m._setView( style.createWidget(parent, "scroll-area", cfg) );
|
|
|
|
|
|
|
|
m.setMinimumSize([32, 32]);
|
2014-03-19 22:20:09 +00:00
|
|
|
|
|
|
|
return m;
|
|
|
|
},
|
2014-06-10 16:46:52 +00:00
|
|
|
setLayout: func(l)
|
|
|
|
{
|
|
|
|
me._layout = l;
|
|
|
|
l.setParent(me);
|
|
|
|
return me.update();
|
|
|
|
},
|
2014-03-19 22:20:09 +00:00
|
|
|
getContent: func()
|
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
return me._view.content;
|
2014-03-19 22:20:09 +00:00
|
|
|
},
|
2014-04-18 21:49:11 +00:00
|
|
|
# Set the background color for the content area.
|
|
|
|
#
|
|
|
|
# @param color Vector of 3 or 4 values in [0, 1]
|
|
|
|
setColorBackground: func
|
|
|
|
{
|
|
|
|
if( size(arg) == 1 )
|
|
|
|
var arg = arg[0];
|
2014-06-08 22:37:14 +00:00
|
|
|
me._view.setColorBackground(arg);
|
2014-04-18 21:49:11 +00:00
|
|
|
},
|
|
|
|
# Reset the size of the content area, e.g. on window resize.
|
|
|
|
#
|
|
|
|
# @param sz Vector of [x,y] values.
|
|
|
|
setSize: func
|
|
|
|
{
|
|
|
|
if( size(arg) == 1 )
|
|
|
|
var arg = arg[0];
|
|
|
|
var (x,y) = arg;
|
|
|
|
me._size = [x,y];
|
|
|
|
me.update();
|
2014-06-16 19:54:26 +00:00
|
|
|
|
|
|
|
if( me._layout != nil )
|
|
|
|
me._layout.setGeometry(me._layout.geometry());
|
2014-04-18 21:49:11 +00:00
|
|
|
},
|
2014-06-25 22:30:33 +00:00
|
|
|
# Move contents to the coordinates x,y (or as far as possible)
|
2014-04-18 21:49:11 +00:00
|
|
|
#
|
|
|
|
# @param x The x coordinate (positive is right)
|
|
|
|
# @param y The y coordinate (positive is down)
|
2014-06-25 22:30:33 +00:00
|
|
|
scrollTo: func(x, y)
|
2014-03-19 22:20:09 +00:00
|
|
|
{
|
2014-06-25 22:30:33 +00:00
|
|
|
me._content_pos[0] = x;
|
|
|
|
me._content_pos[1] = y;
|
2014-04-18 21:49:11 +00:00
|
|
|
|
2014-03-19 22:20:09 +00:00
|
|
|
me.update();
|
|
|
|
},
|
2014-06-25 22:30:33 +00:00
|
|
|
# Move the scrollable area to the top-most position
|
|
|
|
scrollToTop: func me.scrollTo( me._content_pos[0], 0 ),
|
|
|
|
# Move the scrollable area to the bottom-most position
|
|
|
|
scrollToBottom: func me.scrollTo( me._content_pos[0], me._max_scroll[1] ),
|
|
|
|
# Move the scrollable area to the left-most position
|
|
|
|
scrollToLeft: func me.scrollTo( 0, me._content_pos[1] ),
|
|
|
|
# Move the scrollable area to the right-most position
|
|
|
|
scrollToRight: func me.scrollTo( me._max_scroll[0], me._content_pos[1] ),
|
|
|
|
# Move content by given delta
|
|
|
|
scrollBy: func(x, y)
|
|
|
|
{
|
|
|
|
return me.scrollTo( me._content_pos[0] + x,
|
|
|
|
me._content_pos[1] + y );
|
2014-04-18 21:49:11 +00:00
|
|
|
},
|
2014-06-25 22:30:33 +00:00
|
|
|
# Set horizontal scrollbar position
|
|
|
|
horizScrollBarTo: func(x)
|
2014-04-18 21:49:11 +00:00
|
|
|
{
|
2014-06-25 22:30:33 +00:00
|
|
|
if( me._scroller_delta[0] < 1 )
|
|
|
|
return me;
|
2014-03-19 22:20:09 +00:00
|
|
|
|
2014-06-25 22:30:33 +00:00
|
|
|
me.scrollTo( me._max_scroll[0] * (x / me._scroller_delta[0]),
|
|
|
|
me._content_pos[1] );
|
2014-04-18 21:49:11 +00:00
|
|
|
},
|
2014-06-25 22:30:33 +00:00
|
|
|
# Set vertical scrollbar position
|
|
|
|
vertScrollBarTo: func(y)
|
2014-04-18 21:49:11 +00:00
|
|
|
{
|
2014-06-25 22:30:33 +00:00
|
|
|
if( me._scroller_delta[1] < 1 )
|
|
|
|
return me;
|
2014-03-19 22:20:09 +00:00
|
|
|
|
2014-06-25 22:30:33 +00:00
|
|
|
me.scrollTo( me._content_pos[0],
|
|
|
|
me._max_scroll[1] * (y / me._scroller_delta[1]) );
|
2014-04-18 21:49:11 +00:00
|
|
|
},
|
2014-06-25 22:30:33 +00:00
|
|
|
# Move horizontal scrollbar by given offset
|
|
|
|
horizScrollBarBy: func(dx)
|
2014-06-13 10:27:31 +00:00
|
|
|
{
|
2014-06-25 22:30:33 +00:00
|
|
|
me.horizScrollBarTo(me._scroller_pos[0] + dx);
|
2014-06-13 10:27:31 +00:00
|
|
|
},
|
2014-06-25 22:30:33 +00:00
|
|
|
# Move vertical scrollbar by given offset
|
|
|
|
vertScrollBarBy: func(dy)
|
2014-06-13 10:27:31 +00:00
|
|
|
{
|
2014-06-25 22:30:33 +00:00
|
|
|
me.vertScrollBarTo(me._scroller_pos[1] + dy);
|
2014-06-13 10:27:31 +00:00
|
|
|
},
|
2014-04-18 21:49:11 +00:00
|
|
|
# Update scroll bar and content area.
|
|
|
|
#
|
|
|
|
# Needs to be called when the size of the content changes.
|
|
|
|
update: func(bb=nil)
|
|
|
|
{
|
|
|
|
if (bb == nil) bb = me._updateBB();
|
|
|
|
if (bb == nil) return me;
|
2014-03-19 22:20:09 +00:00
|
|
|
|
2014-06-25 22:30:33 +00:00
|
|
|
var offset = [ me._content_offset[0] - me._content_pos[0],
|
|
|
|
me._content_offset[1] - me._content_pos[1] ];
|
2014-03-19 22:20:09 +00:00
|
|
|
me.getContent().setTranslation(offset);
|
|
|
|
|
2014-06-08 22:37:14 +00:00
|
|
|
me._view.update(me);
|
2014-03-19 22:20:09 +00:00
|
|
|
me.getContent().update();
|
2014-04-18 21:49:11 +00:00
|
|
|
|
|
|
|
return me;
|
2014-03-19 22:20:09 +00:00
|
|
|
},
|
|
|
|
# protected:
|
2014-06-08 22:37:14 +00:00
|
|
|
_setView: func(view)
|
2014-03-19 22:20:09 +00:00
|
|
|
{
|
2014-06-08 22:37:14 +00:00
|
|
|
view.vert.addEventListener("mousedown", func(e) me._dragStart(e));
|
|
|
|
view.horiz.addEventListener("mousedown", func(e) me._dragStart(e));
|
2014-06-13 10:27:31 +00:00
|
|
|
view._root.addEventListener("mousedown", func(e)
|
|
|
|
{
|
2014-06-25 22:30:33 +00:00
|
|
|
me._drag_offsetX = me._content_pos[0] + e.clientX;
|
|
|
|
me._drag_offsetY = me._content_pos[1] + e.clientY;
|
2014-06-13 10:27:31 +00:00
|
|
|
});
|
2014-06-08 22:37:14 +00:00
|
|
|
|
|
|
|
view.vert.addEventListener
|
|
|
|
(
|
|
|
|
"drag",
|
2014-06-10 22:00:55 +00:00
|
|
|
func(e)
|
|
|
|
{
|
2014-06-13 10:27:31 +00:00
|
|
|
if( !me._enabled )
|
|
|
|
return;
|
|
|
|
|
2014-06-25 22:30:33 +00:00
|
|
|
me.vertScrollBarTo(me._drag_offsetY + e.clientY);
|
2014-06-13 10:27:31 +00:00
|
|
|
e.stopPropagation();
|
2014-06-10 22:00:55 +00:00
|
|
|
}
|
2014-06-08 22:37:14 +00:00
|
|
|
);
|
|
|
|
view.horiz.addEventListener
|
|
|
|
(
|
|
|
|
"drag",
|
2014-06-10 22:00:55 +00:00
|
|
|
func(e)
|
|
|
|
{
|
2014-06-13 10:27:31 +00:00
|
|
|
if( !me._enabled )
|
|
|
|
return;
|
|
|
|
|
2014-06-25 22:30:33 +00:00
|
|
|
me.horizScrollBarTo(me._drag_offsetX + e.clientX);
|
2014-06-13 10:27:31 +00:00
|
|
|
e.stopPropagation();
|
2014-06-10 22:00:55 +00:00
|
|
|
}
|
2014-06-08 22:37:14 +00:00
|
|
|
);
|
|
|
|
|
2014-06-13 10:27:31 +00:00
|
|
|
view._root.addEventListener
|
|
|
|
(
|
|
|
|
"drag",
|
|
|
|
func(e)
|
|
|
|
{
|
|
|
|
if( !me._enabled )
|
|
|
|
return;
|
|
|
|
|
2014-06-25 22:30:33 +00:00
|
|
|
me.scrollTo( me._drag_offsetX - e.clientX,
|
|
|
|
me._drag_offsetY - e.clientY );
|
2014-06-13 10:27:31 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
);
|
2014-06-08 22:37:14 +00:00
|
|
|
view._root.addEventListener
|
|
|
|
(
|
|
|
|
"wheel",
|
2014-06-10 22:00:55 +00:00
|
|
|
func(e)
|
|
|
|
{
|
2014-06-13 10:27:31 +00:00
|
|
|
if( !me._enabled )
|
|
|
|
return;
|
|
|
|
|
2014-06-25 22:30:33 +00:00
|
|
|
me.scrollBy(0, 30 * -e.deltaY); # TODO make step size configurable
|
2014-06-13 10:27:31 +00:00
|
|
|
e.stopPropagation();
|
2014-06-10 22:00:55 +00:00
|
|
|
}
|
2014-06-08 22:37:14 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
call(gui.Widget._setView, [view], me);
|
2014-03-19 22:20:09 +00:00
|
|
|
},
|
|
|
|
_dragStart: func(e)
|
|
|
|
{
|
2014-06-25 22:30:33 +00:00
|
|
|
me._drag_offsetX = me._scroller_pos[0] - e.clientX;
|
|
|
|
me._drag_offsetY = me._scroller_pos[1] - e.clientY;
|
2014-06-13 10:27:31 +00:00
|
|
|
e.stopPropagation();
|
2014-04-18 21:49:11 +00:00
|
|
|
},
|
2014-06-08 22:37:14 +00:00
|
|
|
_updateBB: func()
|
|
|
|
{
|
2014-04-18 21:49:11 +00:00
|
|
|
# TODO only update on content resize
|
2014-06-10 16:46:52 +00:00
|
|
|
if( me._layout == nil )
|
|
|
|
{
|
|
|
|
var bb = me.getContent().getTightBoundingBox();
|
2014-04-18 21:49:11 +00:00
|
|
|
|
2014-06-10 16:46:52 +00:00
|
|
|
if( bb[2] < bb[0] or bb[3] < bb[1] )
|
|
|
|
return nil;
|
|
|
|
var w = bb[2] - bb[0];
|
|
|
|
var h = bb[3] - bb[1];
|
|
|
|
|
|
|
|
var cur_offset = me.getContent().getTranslation();
|
|
|
|
me._content_offset = [cur_offset[0] - bb[0], cur_offset[1] - bb[1]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var min_size = me._layout.minimumSize();
|
|
|
|
var max_size = me._layout.maximumSize();
|
|
|
|
var size_hint = me._layout.sizeHint();
|
2014-06-12 22:15:30 +00:00
|
|
|
var w = math.min(max_size[0], math.max(size_hint[0], me._size[0]));
|
|
|
|
var h = math.max(
|
|
|
|
math.min(max_size[1], math.max(size_hint[1], me._size[1])),
|
|
|
|
me._layout.heightForWidth(w)
|
|
|
|
);
|
|
|
|
|
2014-06-10 16:46:52 +00:00
|
|
|
me._layout.setGeometry([0, 0, w, h]);
|
|
|
|
|
|
|
|
# Layout always has the origin at (0, 0)
|
|
|
|
me._content_offset = [0, 0];
|
|
|
|
}
|
2014-04-18 21:49:11 +00:00
|
|
|
|
2014-06-25 22:30:33 +00:00
|
|
|
me._max_scroll[0] = math.max(0, w - me._size[0]);
|
|
|
|
me._max_scroll[1] = math.max(0, h - me._size[1]);
|
|
|
|
me._content_size = [w, h];
|
|
|
|
|
|
|
|
# keep position within limit and only integer (to prevent artifacts on text,
|
|
|
|
# lines, etc. not alligned with pixel grid)
|
|
|
|
me._content_pos[0] =
|
|
|
|
math.max(0, math.min( math.round(me._content_pos[0]), me._max_scroll[0]));
|
|
|
|
me._content_pos[1] =
|
|
|
|
math.max(0, math.min( math.round(me._content_pos[1]), me._max_scroll[1]));
|
|
|
|
|
|
|
|
me._scroller_size = [0, 0]; # scroller size
|
|
|
|
me._scroller_offset = [0, 0]; # scroller minimum pos (eg. add offset for
|
|
|
|
# scrolling with buttons)
|
|
|
|
me._scroller_delta = [0, 0]; # scroller max travel distance
|
|
|
|
|
|
|
|
# update scroller size/offset/max delta
|
|
|
|
me._view._updateScrollMetrics(me, 0);
|
|
|
|
me._view._updateScrollMetrics(me, 1);
|
|
|
|
|
|
|
|
# update current scrollbar positions
|
|
|
|
me._scroller_pos[0] =
|
|
|
|
me._max_scroll[0] > 0
|
|
|
|
? (me._content_pos[0] / me._max_scroll[0]) * me._scroller_delta[0]
|
|
|
|
: 0;
|
|
|
|
me._scroller_pos[1] =
|
|
|
|
me._max_scroll[1] > 0
|
|
|
|
? (me._content_pos[1] / me._max_scroll[1]) * me._scroller_delta[1]
|
|
|
|
: 0;
|
2014-06-08 22:37:14 +00:00
|
|
|
}
|
2014-03-19 22:20:09 +00:00
|
|
|
};
|