2012-06-26 00:13:30 +02:00
|
|
|
-- @docclass
|
2014-06-06 18:10:14 +02:00
|
|
|
UITabBar = extends(UIWidget, "UITabBar")
|
2012-01-13 00:47:31 +01:00
|
|
|
|
|
|
|
-- private functions
|
2012-01-14 02:37:15 +01:00
|
|
|
local function onTabClick(tab)
|
|
|
|
tab.tabBar:selectTab(tab)
|
2012-01-13 00:47:31 +01:00
|
|
|
end
|
|
|
|
|
2013-01-21 22:36:53 +01:00
|
|
|
local function onTabMouseRelease(tab, mousePos, mouseButton)
|
|
|
|
if mouseButton == MouseRightButton and tab:containsPoint(mousePos) then
|
|
|
|
signalcall(tab.tabBar.onTabLeftClick, tab.tabBar, tab)
|
2012-10-10 22:20:32 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-13 00:47:31 +01:00
|
|
|
-- public functions
|
|
|
|
function UITabBar.create()
|
|
|
|
local tabbar = UITabBar.internalCreate()
|
2012-01-13 21:37:44 +01:00
|
|
|
tabbar:setFocusable(false)
|
2012-01-13 00:47:31 +01:00
|
|
|
tabbar.tabs = {}
|
|
|
|
return tabbar
|
|
|
|
end
|
|
|
|
|
2013-01-21 22:36:53 +01:00
|
|
|
function UITabBar:onSetup()
|
|
|
|
self.buttonsPanel = self:getChildById('buttonsPanel')
|
|
|
|
end
|
|
|
|
|
2012-01-13 00:47:31 +01:00
|
|
|
function UITabBar:setContentWidget(widget)
|
|
|
|
self.contentWidget = widget
|
|
|
|
if #self.tabs > 0 then
|
|
|
|
self.contentWidget:addChild(self.tabs[1].tabPanel)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-21 22:36:53 +01:00
|
|
|
function UITabBar:addTab(text, panel, icon)
|
2012-01-13 00:47:31 +01:00
|
|
|
if panel == nil then
|
2012-06-26 00:13:30 +02:00
|
|
|
panel = g_ui.createWidget(self:getStyleName() .. 'Panel')
|
2012-04-27 06:54:14 +02:00
|
|
|
panel:setId('tabPanel')
|
2012-01-13 00:47:31 +01:00
|
|
|
end
|
|
|
|
|
2013-01-21 22:36:53 +01:00
|
|
|
local tab = g_ui.createWidget(self:getStyleName() .. 'Button', self.buttonsPanel)
|
2015-01-20 14:34:45 +01:00
|
|
|
|
2012-03-25 19:10:19 +02:00
|
|
|
panel.isTab = true
|
2012-01-14 02:37:15 +01:00
|
|
|
tab.tabPanel = panel
|
|
|
|
tab.tabBar = self
|
2012-04-27 06:54:14 +02:00
|
|
|
tab:setId('tab')
|
2012-01-14 02:37:15 +01:00
|
|
|
tab:setText(text)
|
|
|
|
tab:setWidth(tab:getTextSize().width + tab:getPaddingLeft() + tab:getPaddingRight())
|
2012-03-18 14:34:39 +01:00
|
|
|
tab.onClick = onTabClick
|
2013-01-21 22:36:53 +01:00
|
|
|
tab.onMouseRelease = onTabMouseRelease
|
2012-03-18 14:34:39 +01:00
|
|
|
tab.onDestroy = function() tab.tabPanel:destroy() end
|
2012-01-13 00:47:31 +01:00
|
|
|
|
2012-01-14 02:37:15 +01:00
|
|
|
table.insert(self.tabs, tab)
|
2012-01-13 00:47:31 +01:00
|
|
|
if #self.tabs == 1 then
|
2012-01-14 02:37:15 +01:00
|
|
|
self:selectTab(tab)
|
2012-01-13 00:47:31 +01:00
|
|
|
end
|
|
|
|
|
2013-01-21 22:36:53 +01:00
|
|
|
local tabStyle = {}
|
|
|
|
tabStyle['icon-source'] = icon
|
|
|
|
tab:mergeStyle(tabStyle)
|
2012-10-10 22:20:32 +02:00
|
|
|
|
2013-01-21 22:36:53 +01:00
|
|
|
return tab
|
2012-10-10 22:20:32 +02:00
|
|
|
end
|
|
|
|
|
2013-02-06 20:35:59 +01:00
|
|
|
function UITabBar:addButton(text, func, icon)
|
|
|
|
local button = g_ui.createWidget(self:getStyleName() .. 'Button', self.buttonsPanel)
|
|
|
|
button:setText(text)
|
|
|
|
|
|
|
|
local style = {}
|
|
|
|
style['icon-source'] = icon
|
|
|
|
button:mergeStyle(style)
|
|
|
|
|
|
|
|
button.onClick = func
|
|
|
|
return button
|
|
|
|
end
|
|
|
|
|
2012-02-03 05:32:39 +01:00
|
|
|
function UITabBar:removeTab(tab)
|
|
|
|
local index = table.find(self.tabs, tab)
|
|
|
|
if index == nil then return end
|
|
|
|
if self.currentTab == tab then
|
|
|
|
self:selectPrevTab()
|
|
|
|
end
|
|
|
|
table.remove(self.tabs, index)
|
|
|
|
tab:destroy()
|
|
|
|
end
|
|
|
|
|
2012-01-14 02:37:15 +01:00
|
|
|
function UITabBar:getTab(text)
|
|
|
|
for k,tab in pairs(self.tabs) do
|
2012-02-03 10:59:04 +01:00
|
|
|
if tab:getText():lower() == text:lower() then
|
2012-01-14 02:37:15 +01:00
|
|
|
return tab
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function UITabBar:selectTab(tab)
|
|
|
|
if self.currentTab == tab then return end
|
2012-01-13 00:47:31 +01:00
|
|
|
if self.contentWidget then
|
2012-03-25 19:10:19 +02:00
|
|
|
local selectedWidget = self.contentWidget:getLastChild()
|
|
|
|
if selectedWidget and selectedWidget.isTab then
|
2012-01-13 00:47:31 +01:00
|
|
|
self.contentWidget:removeChild(selectedWidget)
|
|
|
|
end
|
2012-01-14 02:37:15 +01:00
|
|
|
self.contentWidget:addChild(tab.tabPanel)
|
|
|
|
tab.tabPanel:fill('parent')
|
2012-01-13 00:47:31 +01:00
|
|
|
end
|
|
|
|
|
2012-01-14 02:37:15 +01:00
|
|
|
if self.currentTab then
|
|
|
|
self.currentTab:setChecked(false)
|
2012-01-13 00:47:31 +01:00
|
|
|
end
|
2012-02-03 05:32:39 +01:00
|
|
|
signalcall(self.onTabChange, self, tab)
|
2012-01-14 02:37:15 +01:00
|
|
|
self.currentTab = tab
|
|
|
|
tab:setChecked(true)
|
|
|
|
tab:setOn(false)
|
2013-01-16 17:20:17 +01:00
|
|
|
|
|
|
|
local parent = tab:getParent()
|
2013-03-16 01:59:10 +01:00
|
|
|
if parent then
|
|
|
|
parent:focusChild(tab, MouseFocusReason)
|
|
|
|
end
|
2012-01-13 21:37:44 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function UITabBar:selectNextTab()
|
2012-01-14 02:37:15 +01:00
|
|
|
if self.currentTab == nil then return end
|
|
|
|
local index = table.find(self.tabs, self.currentTab)
|
2012-01-13 21:37:44 +01:00
|
|
|
if index == nil then return end
|
|
|
|
local nextTab = self.tabs[index + 1] or self.tabs[1]
|
|
|
|
if not nextTab then return end
|
|
|
|
self:selectTab(nextTab)
|
|
|
|
end
|
|
|
|
|
|
|
|
function UITabBar:selectPrevTab()
|
2012-01-14 02:37:15 +01:00
|
|
|
if self.currentTab == nil then return end
|
|
|
|
local index = table.find(self.tabs, self.currentTab)
|
2012-01-13 21:37:44 +01:00
|
|
|
if index == nil then return end
|
|
|
|
local prevTab = self.tabs[index - 1] or self.tabs[#self.tabs]
|
|
|
|
if not prevTab then return end
|
|
|
|
self:selectTab(prevTab)
|
2012-01-13 00:47:31 +01:00
|
|
|
end
|
|
|
|
|
2012-01-14 02:37:15 +01:00
|
|
|
function UITabBar:getTabPanel(tab)
|
|
|
|
return tab.tabPanel
|
2012-01-13 00:47:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function UITabBar:getCurrentTabPanel()
|
2012-01-14 02:37:15 +01:00
|
|
|
if self.currentTab then
|
|
|
|
return self.currentTab.tabPanel
|
2012-01-13 00:47:31 +01:00
|
|
|
end
|
|
|
|
end
|
2012-01-14 02:37:15 +01:00
|
|
|
|
|
|
|
function UITabBar:getCurrentTab()
|
|
|
|
return self.currentTab
|
2013-01-03 09:24:07 +01:00
|
|
|
end
|
2013-03-02 21:01:29 +01:00
|
|
|
|
|
|
|
function UITabBar:getTabs()
|
|
|
|
return self.tabs
|
|
|
|
end
|
|
|
|
|
|
|
|
function UITabBar:getTabsPanel()
|
|
|
|
return table.collect(self.tabs, function(_,tab) return tab.tabPanel end)
|
|
|
|
end
|