Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local scrollViews = {}
- local function scrollListener()
- while true do
- local _, scrollDirection, x, y = os.pullEvent("mouse_scroll")
- printPoly(scrollDirection, x, y)
- end
- end
- local function registerScrollView(view)
- local context = getClassContext(view)
- for _, registeredView in ipairs(scrollViews) do
- if rectsOverlap(context.x, context.y, context.w, context.h,
- registeredView.x, registeredView.y, registeredView.w, registeredView.h) then
- error("scroll view overlaps with another scroll view")
- end
- end
- table.insert(scrollViews, {
- view = view,
- x = context.x,
- y = context.y,
- w = context.w,
- h = context.h
- })
- end
- local function disposeScrollView(view)
- for i = 0, #scrollViews do
- if scrollViews[i] == view then
- table.remove(scrollViews, i)
- return
- end
- end
- end
- local ScrollViewClass
- ScrollViewClass = {
- key = "ScrollView",
- methods = {
- dispose = function(view, context)
- disposeScrollView(view)
- end,
- print = function(view, context, ...)
- printPolyTo(function(txt)
- ScrollViewClass.privateMethods.write(view, context, txt)
- end, ...)
- ScrollViewClass.privateMethods.render(context)
- end,
- clear = function(view, context)
- context.lines = { "" }
- context.currentLine = 1
- context.viewOffset = 0
- end
- },
- privateMethods = {
- write = function(view, context, txt)
- local first = true
- local pts = splitText(txt, "\n\r", false, false, true)
- for _, pt in ipairs(pts) do
- if first then
- first = false
- else
- table.insert(context.lines, "")
- context.currentLine = context.currentLine + 1
- end
- local line = context.lines[context.currentLine]
- local newLine = line .. pt
- while #newLine > context.w do
- context.lines[context.currentLine] = newLine:sub(1, context.w)
- newLine = newLine:sub(context.w + 1)
- table.insert(context.lines, "")
- context.currentLine = context.currentLine + 1
- end
- context.lines[context.currentLine] = newLine
- end
- end,
- render = function(context)
- if not test then
- local x, y = term.getCursorPos()
- for i = 1, context.h do
- local screenLine = context.viewOffset + i
- term.setCursorPos(context.x + 1, screenLine)
- term.clearLine()
- term.setCursorPos(context.x + 1, screenLine)
- if i <= context.currentLine then
- write(context.lines[i])
- end
- end
- term.setCursorPos(x, y)
- else
- for i = 1, context.h do
- if i <= context.currentLine then
- print("render line [" .. context.lines[i] .. "]")
- end
- end
- print("")
- end
- end
- },
- construct = function(view, context, x, y, w, h)
- if not x or not y or not w or not h then
- error("missing arguments (x, y, w, h expected)")
- end
- context.x = x
- context.y = y
- context.w = w
- context.h = h
- ScrollViewClass.methods.clear(view, context)
- registerScrollView(view)
- if not test then
- local _, y1 = term.getCursorPos()
- if context.y <= y1 - 1 and y1 - 1 < context.y + context.h then
- term.setCursorPos(1, context.y + context.h)
- end
- end
- end
- }
- return {
- name = "scrollView",
- onSetup = function()
- ThreadManager.startThread(
- scrollListener, true, "scroll view listener"
- )
- end,
- classes = {
- ScrollViewClass
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement