Advertisement
melzneni

Turti Lib Scroll View

Sep 19th, 2023 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. local scrollViews = {}
  2.  
  3. local function scrollListener()
  4. while true do
  5. local _, scrollDirection, x, y = os.pullEvent("mouse_scroll")
  6. printPoly(scrollDirection, x, y)
  7. end
  8. end
  9.  
  10. local function registerScrollView(view)
  11. local context = getClassContext(view)
  12.  
  13. for _, registeredView in ipairs(scrollViews) do
  14. if rectsOverlap(context.x, context.y, context.w, context.h,
  15. registeredView.x, registeredView.y, registeredView.w, registeredView.h) then
  16. error("scroll view overlaps with another scroll view")
  17. end
  18. end
  19.  
  20. table.insert(scrollViews, {
  21. view = view,
  22. x = context.x,
  23. y = context.y,
  24. w = context.w,
  25. h = context.h
  26. })
  27. end
  28.  
  29. local function disposeScrollView(view)
  30. for i = 0, #scrollViews do
  31. if scrollViews[i] == view then
  32. table.remove(scrollViews, i)
  33. return
  34. end
  35. end
  36. end
  37.  
  38. local ScrollViewClass
  39. ScrollViewClass = {
  40. key = "ScrollView",
  41. methods = {
  42. dispose = function(view, context)
  43. disposeScrollView(view)
  44. end,
  45. print = function(view, context, ...)
  46. printPolyTo(function(txt)
  47. ScrollViewClass.privateMethods.write(view, context, txt)
  48. end, ...)
  49. ScrollViewClass.privateMethods.render(context)
  50. end,
  51. clear = function(view, context)
  52. context.lines = { "" }
  53. context.currentLine = 1
  54. context.viewOffset = 0
  55. end
  56. },
  57. privateMethods = {
  58. write = function(view, context, txt)
  59. local first = true
  60. local pts = splitText(txt, "\n\r", false, false, true)
  61. for _, pt in ipairs(pts) do
  62. if first then
  63. first = false
  64. else
  65. table.insert(context.lines, "")
  66. context.currentLine = context.currentLine + 1
  67. end
  68. local line = context.lines[context.currentLine]
  69. local newLine = line .. pt
  70. while #newLine > context.w do
  71. context.lines[context.currentLine] = newLine:sub(1, context.w)
  72. newLine = newLine:sub(context.w + 1)
  73. table.insert(context.lines, "")
  74. context.currentLine = context.currentLine + 1
  75. end
  76. context.lines[context.currentLine] = newLine
  77. end
  78. end,
  79. render = function(context)
  80. if not test then
  81. local x, y = term.getCursorPos()
  82. for i = 1, context.h do
  83. local screenLine = context.viewOffset + i
  84. term.setCursorPos(context.x + 1, screenLine)
  85. term.clearLine()
  86. term.setCursorPos(context.x + 1, screenLine)
  87. if i <= context.currentLine then
  88. write(context.lines[i])
  89. end
  90. end
  91. term.setCursorPos(x, y)
  92. else
  93. for i = 1, context.h do
  94. if i <= context.currentLine then
  95. print("render line [" .. context.lines[i] .. "]")
  96. end
  97. end
  98. print("")
  99. end
  100. end
  101. },
  102. construct = function(view, context, x, y, w, h)
  103. if not x or not y or not w or not h then
  104. error("missing arguments (x, y, w, h expected)")
  105. end
  106. context.x = x
  107. context.y = y
  108. context.w = w
  109. context.h = h
  110. ScrollViewClass.methods.clear(view, context)
  111. registerScrollView(view)
  112. if not test then
  113. local _, y1 = term.getCursorPos()
  114. if context.y <= y1 - 1 and y1 - 1 < context.y + context.h then
  115. term.setCursorPos(1, context.y + context.h)
  116. end
  117. end
  118. end
  119. }
  120.  
  121. return {
  122. name = "scrollView",
  123. onSetup = function()
  124. ThreadManager.startThread(
  125. scrollListener, true, "scroll view listener"
  126. )
  127. end,
  128. classes = {
  129. ScrollViewClass
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement