MattiasBuelens

CCGUI tab

Jul 20th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. --[[
  2.  
  3.     ComputerCraft GUI
  4.     Tab container
  5.  
  6. --]]
  7.  
  8. ccgui = ccgui or {}
  9.  
  10. local TabContainer = common.newClass({
  11.     -- Current tab
  12.     currentTab = nil,
  13.     -- Spacing between tab buttons
  14.     tabSpacing = 0,
  15.     -- Stretch tab panes
  16.     tabStretch = true,
  17.     -- Style for tab buttons
  18.     tabStyle = ccgui.Button
  19. }, ccgui.FlowContainer)
  20. ccgui.TabContainer = TabContainer
  21.  
  22. function TabContainer:init()
  23.     ccgui.FlowContainer.init(self)
  24.  
  25.     self.tabBar = ccgui.FlowContainer:new{
  26.         horizontal = not self.horizontal,
  27.         spacing = self.tabSpacing
  28.     }
  29.     self.tabPane = ccgui.FlowContainer:new{
  30.         horizontal = not self.horizontal,
  31.         stretch = tabStretch
  32.     }
  33.     self:add(self.tabBar, self.tabPane)
  34.  
  35.     self:on("beforepaint", self.updateVisibleTab, self)
  36. end
  37.  
  38. function TabContainer:tabCount()
  39.     return #self.tabPane.children
  40. end
  41.  
  42. function TabContainer:addTab(label, tab)
  43.     if self.tabPane:find(tab, true) ~= nil then
  44.         -- Tab already contained
  45.         return tab
  46.     end
  47.    
  48.     -- Add tab pane
  49.     self.tabPane:add(tab)
  50.  
  51.     -- Add tab button
  52.     local tabButton = self:addButton(label)
  53.  
  54.     -- Link button and pane
  55.     tab.tabButton = tabButton
  56.     tabButton.tab = tab
  57.  
  58.     -- Bind button to pane
  59.     local container = self
  60.     tabButton:on("buttonpress", function(self)
  61.         container:setCurrentTab(self.tab)
  62.     end, tabButton)
  63.  
  64.     self:updateVisibleTab()
  65.  
  66.     return tab
  67. end
  68.  
  69. function TabContainer:addButton(label)
  70.     local tabButton = self.tabStyle:new{
  71.         text = label
  72.     }
  73.     self.tabBar:add(tabButton)
  74.     return tabButton
  75. end
  76.  
  77. function TabContainer:setCurrentTab(tab)
  78.     if type(tab) ~= "table" then return false end
  79.  
  80.     -- Set as current tab
  81.     self.currentTab = tab
  82.  
  83.     self:updateVisibleTab()
  84.     --self.tabPane:markRepaint()
  85.     return true
  86. end
  87.  
  88. function TabContainer:removeTab(tab)
  89.     if self.currentTab == tab then
  90.         -- Current tab removed
  91.         local i, n = self.tabPane:find(tab), self:tabCount()
  92.         if n > 1 then
  93.             -- Move to neighbour tab
  94.             if i == n then
  95.                 i = i-1
  96.             else
  97.                 i = i+1
  98.             end
  99.             self:setCurrentTab(self.tabPane.children[i])
  100.         else
  101.             -- No new current tab
  102.             self.currentTab = nil
  103.         end
  104.     end
  105.  
  106.     -- Remove tab
  107.     self.tabPane:remove(tab)
  108.  
  109.     -- Remove tab button
  110.     self.tabBar:remove(tab.tabButton)
  111.  
  112.     self:updateVisibleTab()
  113. end
  114.  
  115. function TabContainer:updateVisibleTab()
  116.     if self.currentTab == nil and self:tabCount() > 0 then
  117.         self.currentTab = self.tabPane.children[1]
  118.     end
  119.  
  120.     -- Show current tab and hide others
  121.     self.tabPane:each(function(child)
  122.         if child == self.currentTab then
  123.             child:show()
  124.         else
  125.             child:hide()
  126.         end
  127.     end)
  128. end
  129.  
  130. function TabContainer:sinkEventToCurrent(event)
  131.     self:on(event, function(...)
  132.         if self.isVisible and self.currentTab ~= nil then
  133.             self.currentTab:trigger(event, ...)
  134.         end
  135.     end, self)
  136. end
Advertisement
Add Comment
Please, Sign In to add comment