2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
2012-03-25 16:10:15 +02:00
|
|
|
UIScrollArea = extends(UIWidget)
|
|
|
|
|
2012-03-25 19:10:19 +02:00
|
|
|
-- public functions
|
2012-03-25 16:10:15 +02:00
|
|
|
function UIScrollArea.create()
|
|
|
|
local scrollarea = UIScrollArea.internalCreate()
|
|
|
|
scrollarea:setClipping(true)
|
2012-03-25 19:10:19 +02:00
|
|
|
scrollarea.inverted = false
|
2012-08-10 02:36:52 +02:00
|
|
|
scrollarea.alwaysScrollMaximum = false
|
2012-03-25 16:10:15 +02:00
|
|
|
return scrollarea
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIScrollArea:onStyleApply(styleName, styleNode)
|
|
|
|
for name,value in pairs(styleNode) do
|
2012-03-25 19:10:19 +02:00
|
|
|
if name == 'vertical-scrollbar' then
|
|
|
|
addEvent(function()
|
2013-01-30 21:23:26 +01:00
|
|
|
local parent = self:getParent()
|
|
|
|
if parent then
|
|
|
|
self:setVerticalScrollBar(parent:getChildById(value))
|
|
|
|
end
|
2012-03-25 19:10:19 +02:00
|
|
|
end)
|
|
|
|
elseif name == 'horizontal-scrollbar' then
|
|
|
|
addEvent(function()
|
2013-01-30 21:23:26 +01:00
|
|
|
local parent = self:getParent()
|
|
|
|
if parent then
|
|
|
|
self:setHorizontalScrollBar(self:getParent():getChildById(value))
|
|
|
|
end
|
2012-03-25 19:10:19 +02:00
|
|
|
end)
|
|
|
|
elseif name == 'inverted-scroll' then
|
|
|
|
self:setInverted(value)
|
2012-08-10 02:36:52 +02:00
|
|
|
elseif name == 'always-scroll-maximum' then
|
|
|
|
self:setAlwaysScrollMaximum(value)
|
2012-03-25 16:10:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-03-25 19:10:19 +02:00
|
|
|
|
|
|
|
function UIScrollArea:updateScrollBars()
|
2012-08-10 02:36:52 +02:00
|
|
|
local scrollWidth = math.max(self:getChildrenRect().width - self:getPaddingRect().width, 0)
|
|
|
|
local scrollHeight = math.max(self:getChildrenRect().height - self:getPaddingRect().height, 0)
|
2012-03-25 19:10:19 +02:00
|
|
|
|
|
|
|
local scrollbar = self.verticalScrollBar
|
|
|
|
if scrollbar then
|
|
|
|
if self.inverted then
|
2012-08-10 02:36:52 +02:00
|
|
|
scrollbar:setMinimum(-scrollHeight)
|
2012-03-26 15:34:43 +02:00
|
|
|
scrollbar:setMaximum(0)
|
2012-03-25 19:10:19 +02:00
|
|
|
else
|
2012-03-26 15:34:43 +02:00
|
|
|
scrollbar:setMinimum(0)
|
2012-08-10 02:36:52 +02:00
|
|
|
scrollbar:setMaximum(scrollHeight)
|
2012-03-25 19:10:19 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local scrollbar = self.horizontalScrollBar
|
|
|
|
if scrollbar then
|
|
|
|
if self.inverted then
|
2012-08-10 02:36:52 +02:00
|
|
|
scrollbar:setMinimum(-scrollWidth)
|
2012-08-31 06:56:10 +02:00
|
|
|
scrollbar:setMaximum(0)
|
2012-03-25 19:10:19 +02:00
|
|
|
else
|
2012-08-31 06:56:10 +02:00
|
|
|
scrollbar:setMinimum(0)
|
2012-08-10 02:36:52 +02:00
|
|
|
scrollbar:setMaximum(scrollWidth)
|
2012-03-25 19:10:19 +02:00
|
|
|
end
|
|
|
|
end
|
2012-08-10 02:36:52 +02:00
|
|
|
|
|
|
|
if self.lastScrollWidth ~= scrollWidth then
|
|
|
|
self:onScrollWidthChange()
|
|
|
|
end
|
|
|
|
if self.lastScrollHeight ~= scrollHeight then
|
|
|
|
self:onScrollHeightChange()
|
|
|
|
end
|
|
|
|
|
|
|
|
self.lastScrollWidth = scrollWidth
|
|
|
|
self.lastScrollHeight = scrollHeight
|
2012-03-25 19:10:19 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function UIScrollArea:setVerticalScrollBar(scrollbar)
|
|
|
|
self.verticalScrollBar = scrollbar
|
|
|
|
self.verticalScrollBar.onValueChange = function(scrollbar, value)
|
|
|
|
local virtualOffset = self:getVirtualOffset()
|
|
|
|
virtualOffset.y = value
|
|
|
|
self:setVirtualOffset(virtualOffset)
|
2012-10-06 05:56:38 +02:00
|
|
|
if self.onScrollbarChange then self:onScrollbarChange(value) end
|
2012-03-25 19:10:19 +02:00
|
|
|
end
|
|
|
|
self:updateScrollBars()
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIScrollArea:setHorizontalScrollBar(scrollbar)
|
|
|
|
self.horizontalScrollBar = scrollbar
|
2012-08-31 06:56:10 +02:00
|
|
|
self.horizontalScrollBar.onValueChange = function(scrollbar, value)
|
|
|
|
local virtualOffset = self:getVirtualOffset()
|
|
|
|
virtualOffset.x = value
|
|
|
|
self:setVirtualOffset(virtualOffset)
|
2012-10-06 05:56:38 +02:00
|
|
|
if self.onScrollbarChange then self:onScrollbarChange(value) end
|
2012-08-31 06:56:10 +02:00
|
|
|
end
|
2012-03-25 19:10:19 +02:00
|
|
|
self:updateScrollBars()
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIScrollArea:setInverted(inverted)
|
|
|
|
self.inverted = inverted
|
|
|
|
end
|
|
|
|
|
2012-08-10 02:36:52 +02:00
|
|
|
function UIScrollArea:setAlwaysScrollMaximum(value)
|
|
|
|
self.alwaysScrollMaximum = value
|
|
|
|
end
|
|
|
|
|
2012-03-25 19:10:19 +02:00
|
|
|
function UIScrollArea:onLayoutUpdate()
|
|
|
|
self:updateScrollBars()
|
|
|
|
end
|
2012-03-26 15:34:43 +02:00
|
|
|
|
|
|
|
function UIScrollArea:onMouseWheel(mousePos, mouseWheel)
|
|
|
|
if self.verticalScrollBar then
|
|
|
|
if mouseWheel == MouseWheelUp then
|
|
|
|
self.verticalScrollBar:decrement()
|
|
|
|
else
|
|
|
|
self.verticalScrollBar:increment()
|
|
|
|
end
|
2012-08-31 06:56:10 +02:00
|
|
|
elseif self.horizontalScrollBar then
|
|
|
|
if mouseWheel == MouseWheelUp then
|
|
|
|
self.horizontalScrollBar:increment()
|
|
|
|
else
|
|
|
|
self.horizontalScrollBar:decrement()
|
|
|
|
end
|
2012-03-26 15:34:43 +02:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2012-06-02 02:38:26 +02:00
|
|
|
|
2013-01-28 19:33:10 +01:00
|
|
|
function UIScrollArea:ensureChildVisible(child)
|
|
|
|
if child then
|
2012-06-02 02:38:26 +02:00
|
|
|
local paddingRect = self:getPaddingRect()
|
2012-08-31 06:56:10 +02:00
|
|
|
if self.verticalScrollBar then
|
2013-01-28 19:33:10 +01:00
|
|
|
local deltaY = paddingRect.y - child:getY()
|
2012-08-31 06:56:10 +02:00
|
|
|
if deltaY > 0 then
|
|
|
|
self.verticalScrollBar:decrement(deltaY)
|
|
|
|
end
|
|
|
|
|
2013-01-28 19:33:10 +01:00
|
|
|
deltaY = (child:getY() + child:getHeight()) - (paddingRect.y + paddingRect.height)
|
2012-08-31 06:56:10 +02:00
|
|
|
if deltaY > 0 then
|
|
|
|
self.verticalScrollBar:increment(deltaY)
|
|
|
|
end
|
2013-02-28 03:45:03 +01:00
|
|
|
elseif self.horizontalScrollBar then
|
2013-01-28 19:33:10 +01:00
|
|
|
local deltaX = paddingRect.x - child:getX()
|
2012-08-31 06:56:10 +02:00
|
|
|
if deltaX > 0 then
|
|
|
|
self.horizontalScrollBar:decrement(deltaX)
|
|
|
|
end
|
|
|
|
|
2013-01-28 19:33:10 +01:00
|
|
|
deltaX = (child:getX() + child:getWidth()) - (paddingRect.x + paddingRect.width)
|
2012-08-31 06:56:10 +02:00
|
|
|
if deltaX > 0 then
|
|
|
|
self.horizontalScrollBar:increment(deltaX)
|
|
|
|
end
|
2012-06-02 02:38:26 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-08-10 02:36:52 +02:00
|
|
|
|
2013-01-28 19:33:10 +01:00
|
|
|
function UIScrollArea:onChildFocusChange(focusedChild, oldFocused, reason)
|
|
|
|
if focusedChild and (reason == MouseFocusReason or reason == KeyboardFocusReason) then
|
|
|
|
self:ensureChildVisible(focusedChild)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-10 02:36:52 +02:00
|
|
|
function UIScrollArea:onScrollWidthChange()
|
|
|
|
if self.alwaysScrollMaximum and self.horizontalScrollBar then
|
|
|
|
self.horizontalScrollBar:setValue(self.horizontalScrollBar:getMaximum())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UIScrollArea:onScrollHeightChange()
|
|
|
|
if self.alwaysScrollMaximum and self.verticalScrollBar then
|
|
|
|
self.verticalScrollBar:setValue(self.verticalScrollBar:getMaximum())
|
|
|
|
end
|
|
|
|
end
|