2019-06-28 17:50:02 +00:00
|
|
|
local class = require('opus.class')
|
|
|
|
local UI = require('opus.ui')
|
2019-02-06 04:03:57 +00:00
|
|
|
|
|
|
|
UI.Viewport = class(UI.Window)
|
|
|
|
UI.Viewport.defaults = {
|
|
|
|
UIElement = 'Viewport',
|
|
|
|
accelerators = {
|
|
|
|
down = 'scroll_down',
|
|
|
|
up = 'scroll_up',
|
|
|
|
home = 'scroll_top',
|
2020-03-31 15:57:23 +00:00
|
|
|
left = 'scroll_left',
|
|
|
|
right = 'scroll_right',
|
2019-02-06 04:03:57 +00:00
|
|
|
[ 'end' ] = 'scroll_bottom',
|
|
|
|
pageUp = 'scroll_pageUp',
|
|
|
|
[ 'control-b' ] = 'scroll_pageUp',
|
|
|
|
pageDown = 'scroll_pageDown',
|
|
|
|
[ 'control-f' ] = 'scroll_pageDown',
|
|
|
|
},
|
|
|
|
}
|
2020-03-31 15:57:23 +00:00
|
|
|
function UI.Viewport:postInit()
|
|
|
|
if self.showScrollBar then
|
|
|
|
self.scrollBar = UI.ScrollBar()
|
2019-02-09 00:21:20 +00:00
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
|
2020-03-31 15:57:23 +00:00
|
|
|
function UI.Viewport:setScrollPosition(offy, offx) -- argh - reverse
|
|
|
|
local oldOffy = self.offy
|
|
|
|
self.offy = math.max(offy, 0)
|
|
|
|
self.offy = math.min(self.offy, math.max(#self.lines, self.height) - self.height)
|
|
|
|
if self.offy ~= oldOffy then
|
2019-02-06 04:03:57 +00:00
|
|
|
if self.scrollBar then
|
|
|
|
self.scrollBar:draw()
|
|
|
|
end
|
2020-03-31 15:57:23 +00:00
|
|
|
self.offy = offy
|
|
|
|
self:dirty(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
local oldOffx = self.offx
|
|
|
|
self.offx = math.max(offx or 0, 0)
|
|
|
|
self.offx = math.min(self.offx, math.max(#self.lines[1], self.width) - self.width)
|
|
|
|
if self.offx ~= oldOffx then
|
|
|
|
if self.scrollBar then
|
|
|
|
--self.scrollBar:draw()
|
|
|
|
end
|
|
|
|
self.offx = offx or 0
|
|
|
|
self:dirty(true)
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-17 05:13:19 +00:00
|
|
|
function UI.Viewport:blit(x, y, text, bg, fg)
|
2020-03-31 15:57:23 +00:00
|
|
|
if y > #self.lines then
|
|
|
|
self:resizeBuffer(self.width, y)
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
2020-04-17 05:13:19 +00:00
|
|
|
return UI.Window.blit(self, x, y, text, bg, fg)
|
|
|
|
end
|
|
|
|
|
|
|
|
function UI.Viewport:write(x, y, text, bg, fg)
|
|
|
|
if y > #self.lines then
|
|
|
|
self:resizeBuffer(self.width, y)
|
|
|
|
end
|
|
|
|
return UI.Window.write(self, x, y, text, bg, fg)
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
|
2020-03-31 15:57:23 +00:00
|
|
|
function UI.Viewport:setViewHeight(h)
|
|
|
|
if h > #self.lines then
|
|
|
|
self:resizeBuffer(self.width, h)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-06 04:03:57 +00:00
|
|
|
function UI.Viewport:reset()
|
|
|
|
self.offy = 0
|
2020-03-31 15:57:23 +00:00
|
|
|
for i = self.height + 1, #self.lines do
|
|
|
|
self.lines[i] = nil
|
2019-02-06 04:03:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UI.Viewport:getViewArea()
|
|
|
|
return {
|
|
|
|
y = (self.offy or 0) + 1,
|
|
|
|
height = self.height,
|
2020-03-31 15:57:23 +00:00
|
|
|
totalHeight = #self.lines,
|
2019-02-06 04:03:57 +00:00
|
|
|
offsetY = self.offy or 0,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function UI.Viewport:eventHandler(event)
|
2020-04-01 05:43:45 +00:00
|
|
|
if #self.lines <= self.height then
|
|
|
|
return
|
|
|
|
end
|
2019-02-06 04:03:57 +00:00
|
|
|
if event.type == 'scroll_down' then
|
2020-03-31 15:57:23 +00:00
|
|
|
self:setScrollPosition(self.offy + 1, self.offx)
|
2019-02-06 04:03:57 +00:00
|
|
|
elseif event.type == 'scroll_up' then
|
2020-03-31 15:57:23 +00:00
|
|
|
self:setScrollPosition(self.offy - 1, self.offx)
|
|
|
|
elseif event.type == 'scroll_left' then
|
|
|
|
self:setScrollPosition(self.offy, self.offx - 1)
|
|
|
|
elseif event.type == 'scroll_right' then
|
|
|
|
self:setScrollPosition(self.offy, self.offx + 1)
|
2019-02-06 04:03:57 +00:00
|
|
|
elseif event.type == 'scroll_top' then
|
2020-03-31 15:57:23 +00:00
|
|
|
self:setScrollPosition(0, 0)
|
2019-02-06 04:03:57 +00:00
|
|
|
elseif event.type == 'scroll_bottom' then
|
2020-03-31 15:57:23 +00:00
|
|
|
self:setScrollPosition(10000000, 0)
|
2019-02-06 04:03:57 +00:00
|
|
|
elseif event.type == 'scroll_pageUp' then
|
2020-03-31 15:57:23 +00:00
|
|
|
self:setScrollPosition(self.offy - self.height, self.offx)
|
2019-02-06 04:03:57 +00:00
|
|
|
elseif event.type == 'scroll_pageDown' then
|
2020-03-31 15:57:23 +00:00
|
|
|
self:setScrollPosition(self.offy + self.height, self.offx)
|
2019-02-06 04:03:57 +00:00
|
|
|
elseif event.type == 'scroll_to' then
|
2020-03-31 15:57:23 +00:00
|
|
|
self:setScrollPosition(event.offset, 0)
|
2019-02-06 04:03:57 +00:00
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|