MattiasBuelens

CCGUI scroll

Jul 13th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. --[[
  2.  
  3.     ComputerCraft GUI
  4.     Scrollable element
  5.  
  6. --]]
  7.  
  8. ccgui = ccgui or {}
  9.  
  10. local ScrollSlider = common.newClass(ccgui.Slider)
  11. function ScrollSlider:init()
  12.     ccgui.Slider.init(self)
  13.  
  14.     self:on("beforepaint", self.sliderLayout, self, -1)
  15. end
  16. function ScrollSlider:sliderLayout()
  17.     error("ScrollSlider:sliderLayout() not implemented")
  18. end
  19. function ScrollSlider:getStep()
  20.     return 1
  21. end
  22. function ScrollSlider:getMinimum()
  23.     return 0
  24. end
  25.  
  26. local HorizontalSlider = common.newClass({
  27.     horizontal  = true,
  28.     arrowLabels = { "<", ">" }
  29. }, ScrollSlider)
  30. function HorizontalSlider:sliderLayout()
  31.     -- Place below inner box of scroll element
  32.     local sbox = self.parent:inner(self.parent.bbox)
  33.     self.bbox = ccgui.newRectangle(sbox.x, sbox.y + sbox.h, sbox.w, 1)
  34. end
  35. function HorizontalSlider:getValue()
  36.     return self.parent.scrollPosition.x
  37. end
  38. function HorizontalSlider:rawSetValue(newValue)
  39.     self.parent.scrollPosition.x = newValue
  40.     --self.parent:markRepaint()
  41. end
  42. function HorizontalSlider:getSpan()
  43.     return self.parent:scrollVisible().x
  44. end
  45. function HorizontalSlider:getMaximum()
  46.     return self.parent:scrollTotal().x
  47. end
  48.  
  49. local VerticalSlider = common.newClass({
  50.     horizontal = false,
  51.     arrowLabels = { "^", "v" }
  52. }, ScrollSlider)
  53. function VerticalSlider:sliderLayout()
  54.     -- Place at right of inner box of scroll element
  55.     local sbox = self.parent:inner(self.parent.bbox)
  56.     self.bbox = ccgui.newRectangle(sbox.x + sbox.w, sbox.y, 1, sbox.h)
  57. end
  58. function VerticalSlider:getValue()
  59.     return self.parent.scrollPosition.y
  60. end
  61. function VerticalSlider:rawSetValue(newValue)
  62.     self.parent.scrollPosition.y = newValue
  63.     --self.parent:markRepaint()
  64. end
  65. function VerticalSlider:getSpan()
  66.     return self.parent:scrollVisible().y
  67. end
  68. function VerticalSlider:getMaximum()
  69.     return self.parent:scrollTotal().y
  70. end
  71.  
  72. local ScrollElement = common.newClass({
  73.     -- Orientation
  74.     horizontal      = false,
  75.     vertical        = true,
  76.     -- Show scroll bars
  77.     showScrollBars  = true,
  78.     -- Mouse scroll
  79.     mouseScroll     = false,
  80.     -- Colors
  81.     colorForeground = colours.grey,
  82.     colorBar        = colours.grey,
  83.     colorButton     = colours.lightGrey,
  84.     -- Relative scroll position
  85.     scrollPosition  = nil,
  86.     -- Sliders
  87.     sliderHoriz     = nil,
  88.     sliderVerti     = nil
  89. }, ccgui.Element)
  90. ccgui.ScrollElement = ScrollElement
  91.  
  92. function ScrollElement:init()
  93.     ccgui.Element.init(self)
  94.  
  95.     -- Scroll position
  96.     self.scrollPosition = vector.new(0, 0)
  97.  
  98.     -- Orientation
  99.     self.horizontal = not not self.horizontal
  100.     self.vertical = not not self.vertical
  101.  
  102.     -- Sliders
  103.     self.sliderHoriz = HorizontalSlider:new({
  104.         parent          = self,
  105.         colorForeground = self.colorForeground,
  106.         colorBar        = self.colorBar,
  107.         colorButton     = self.colorButton
  108.     })
  109.     self.sliderVerti = VerticalSlider:new({
  110.         parent          = self,
  111.         colorForeground = self.colorForeground,
  112.         colorBar        = self.colorBar,
  113.         colorButton     = self.colorButton
  114.     })
  115.  
  116.     -- Mouse
  117.     self:sinkEvent("mouse_click")
  118.     self:sinkEvent("mouse_drag")
  119.     self:on("mouse_scroll", self.scrollMouse, self)
  120.  
  121.     -- Paint
  122.     self:on("paint", self.scrollPaint, self)
  123. end
  124.  
  125. function ScrollElement:canScroll()
  126.     return true
  127. end
  128.  
  129. -- Relative visible scroll region
  130. function ScrollElement:scrollVisible()
  131.     error("ScrollElement:scrollVisible() not implemented")
  132. end
  133.  
  134. -- Relative total scroll size
  135. function ScrollElement:scrollTotal()
  136.     error("ScrollElement:scrollTotal() not implemented")
  137. end
  138.  
  139. -- Extra margins due to scroll bar
  140. function ScrollElement:scrollBarMargins()
  141.     if self.showScrollBars then
  142.         local right, bottom = 0, 0
  143.         if self.horizontal then
  144.             bottom = 1
  145.         end
  146.         if self.vertical then
  147.             right = 1
  148.         end
  149.         return ccgui.newMargins(0, right, bottom, 0)
  150.     end
  151.  
  152.     return ccgui.newMargins(0)
  153. end
  154.  
  155. function ScrollElement:inner(bbox)
  156.     return ccgui.Element.inner(self, bbox):contract(self:scrollBarMargins())
  157. end
  158.  
  159. function ScrollElement:outer(bbox)
  160.     return ccgui.Element.outer(self, bbox:expand(self:scrollBarMargins()))
  161. end
  162.  
  163. function ScrollElement:scrollPaint()
  164.     if self.showScrollBars then
  165.         if self.horizontal then
  166.             self.sliderHoriz:paint()
  167.         end
  168.         if self.vertical then
  169.             self.sliderVerti:paint()
  170.         end
  171.     end
  172. end
  173.  
  174. function ScrollElement:scrollMouse(dir, x, y)
  175.     if self.isVisible and self.mouseScroll and self:contains(x, y) then
  176.         -- Prefer vertical over horizontal scrolling
  177.         local slider = self.vertical and self.sliderVerti or self.sliderHorzi
  178.         -- Step in scroll direction
  179.         if dir < 0 then
  180.             slider:prevStep()
  181.         else
  182.             slider:nextStep()
  183.         end
  184.     end
  185. end
  186.  
  187. --[[
  188.  
  189.     Event sinking
  190.  
  191. ]]--
  192.  
  193. function ScrollElement:sinkEvent(event)
  194.     self:on(event, function(self, ...)
  195.         if self.isVisible and self.showScrollBars then
  196.             if self.horizontal then
  197.                 self.sliderHoriz:trigger(event, ...)
  198.             end
  199.             if self.vertical then
  200.                 self.sliderVerti:trigger(event, ...)
  201.             end
  202.         end
  203.     end, self)
  204. end
Advertisement
Add Comment
Please, Sign In to add comment