2
0

tabview.lua 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. --Minetest
  2. --Copyright (C) 2014 sapier
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. --------------------------------------------------------------------------------
  18. -- A tabview implementation --
  19. -- Usage: --
  20. -- tabview.create: returns initialized tabview raw element --
  21. -- element.add(tab): add a tab declaration --
  22. -- element.handle_buttons() --
  23. -- element.handle_events() --
  24. -- element.getFormspec() returns formspec of tabview --
  25. --------------------------------------------------------------------------------
  26. --------------------------------------------------------------------------------
  27. local function add_tab(self,tab)
  28. assert(tab.size == nil or (type(tab.size) == table and
  29. tab.size.x ~= nil and tab.size.y ~= nil))
  30. assert(tab.cbf_formspec ~= nil and type(tab.cbf_formspec) == "function")
  31. assert(tab.cbf_button_handler == nil or
  32. type(tab.cbf_button_handler) == "function")
  33. assert(tab.cbf_events == nil or type(tab.cbf_events) == "function")
  34. local newtab = {
  35. name = tab.name,
  36. caption = tab.caption,
  37. button_handler = tab.cbf_button_handler,
  38. event_handler = tab.cbf_events,
  39. get_formspec = tab.cbf_formspec,
  40. tabsize = tab.tabsize,
  41. on_change = tab.on_change,
  42. tabdata = {},
  43. }
  44. self.tablist[#self.tablist + 1] = newtab
  45. if self.last_tab_index == #self.tablist then
  46. self.current_tab = tab.name
  47. if tab.on_activate ~= nil then
  48. tab.on_activate(nil,tab.name)
  49. end
  50. end
  51. end
  52. --------------------------------------------------------------------------------
  53. local function get_formspec(self)
  54. if self.hidden or (self.parent ~= nil and self.parent.hidden) then
  55. return ""
  56. end
  57. local tab = self.tablist[self.last_tab_index]
  58. local content, prepend = tab.get_formspec(self, tab.name, tab.tabdata, tab.tabsize)
  59. if self.parent == nil and not prepend then
  60. local tsize = tab.tabsize or {width=self.width, height=self.height}
  61. prepend = string.format("size[%f,%f,%s]", tsize.width, tsize.height,
  62. dump(self.fixed_size))
  63. end
  64. local formspec = (prepend or "") .. self:tab_header() .. content
  65. return formspec
  66. end
  67. --------------------------------------------------------------------------------
  68. local function handle_buttons(self,fields)
  69. if self.hidden then
  70. return false
  71. end
  72. if self:handle_tab_buttons(fields) then
  73. return true
  74. end
  75. if self.glb_btn_handler ~= nil and
  76. self.glb_btn_handler(self,fields) then
  77. return true
  78. end
  79. local tab = self.tablist[self.last_tab_index]
  80. if tab.button_handler ~= nil then
  81. return tab.button_handler(self, fields, tab.name, tab.tabdata)
  82. end
  83. return false
  84. end
  85. --------------------------------------------------------------------------------
  86. local function handle_events(self,event)
  87. if self.hidden then
  88. return false
  89. end
  90. if self.glb_evt_handler ~= nil and
  91. self.glb_evt_handler(self,event) then
  92. return true
  93. end
  94. local tab = self.tablist[self.last_tab_index]
  95. if tab.evt_handler ~= nil then
  96. return tab.evt_handler(self, event, tab.name, tab.tabdata)
  97. end
  98. return false
  99. end
  100. --------------------------------------------------------------------------------
  101. local function tab_header(self)
  102. local toadd = ""
  103. for i=1,#self.tablist,1 do
  104. if toadd ~= "" then
  105. toadd = toadd .. ","
  106. end
  107. toadd = toadd .. self.tablist[i].caption
  108. end
  109. return string.format("tabheader[%f,%f;%s;%s;%i;true;false]",
  110. self.header_x, self.header_y, self.name, toadd, self.last_tab_index);
  111. end
  112. --------------------------------------------------------------------------------
  113. local function switch_to_tab(self, index)
  114. --first call on_change for tab to leave
  115. if self.tablist[self.last_tab_index].on_change ~= nil then
  116. self.tablist[self.last_tab_index].on_change("LEAVE",
  117. self.current_tab, self.tablist[index].name)
  118. end
  119. --update tabview data
  120. self.last_tab_index = index
  121. local old_tab = self.current_tab
  122. self.current_tab = self.tablist[index].name
  123. if (self.autosave_tab) then
  124. core.settings:set(self.name .. "_LAST",self.current_tab)
  125. end
  126. -- call for tab to enter
  127. if self.tablist[index].on_change ~= nil then
  128. self.tablist[index].on_change("ENTER",
  129. old_tab,self.current_tab)
  130. end
  131. end
  132. --------------------------------------------------------------------------------
  133. local function handle_tab_buttons(self,fields)
  134. --save tab selection to config file
  135. if fields[self.name] then
  136. local index = tonumber(fields[self.name])
  137. switch_to_tab(self, index)
  138. return true
  139. end
  140. return false
  141. end
  142. --------------------------------------------------------------------------------
  143. local function set_tab_by_name(self, name)
  144. for i=1,#self.tablist,1 do
  145. if self.tablist[i].name == name then
  146. switch_to_tab(self, i)
  147. return true
  148. end
  149. end
  150. return false
  151. end
  152. --------------------------------------------------------------------------------
  153. local function hide_tabview(self)
  154. self.hidden=true
  155. --call on_change as we're not gonna show self tab any longer
  156. if self.tablist[self.last_tab_index].on_change ~= nil then
  157. self.tablist[self.last_tab_index].on_change("LEAVE",
  158. self.current_tab, nil)
  159. end
  160. end
  161. --------------------------------------------------------------------------------
  162. local function show_tabview(self)
  163. self.hidden=false
  164. -- call for tab to enter
  165. if self.tablist[self.last_tab_index].on_change ~= nil then
  166. self.tablist[self.last_tab_index].on_change("ENTER",
  167. nil,self.current_tab)
  168. end
  169. end
  170. local tabview_metatable = {
  171. add = add_tab,
  172. handle_buttons = handle_buttons,
  173. handle_events = handle_events,
  174. get_formspec = get_formspec,
  175. show = show_tabview,
  176. hide = hide_tabview,
  177. delete = function(self) ui.delete(self) end,
  178. set_parent = function(self,parent) self.parent = parent end,
  179. set_autosave_tab =
  180. function(self,value) self.autosave_tab = value end,
  181. set_tab = set_tab_by_name,
  182. set_global_button_handler =
  183. function(self,handler) self.glb_btn_handler = handler end,
  184. set_global_event_handler =
  185. function(self,handler) self.glb_evt_handler = handler end,
  186. set_fixed_size =
  187. function(self,state) self.fixed_size = state end,
  188. tab_header = tab_header,
  189. handle_tab_buttons = handle_tab_buttons
  190. }
  191. tabview_metatable.__index = tabview_metatable
  192. --------------------------------------------------------------------------------
  193. function tabview_create(name, size, tabheaderpos)
  194. local self = {}
  195. self.name = name
  196. self.type = "toplevel"
  197. self.width = size.x
  198. self.height = size.y
  199. self.header_x = tabheaderpos.x
  200. self.header_y = tabheaderpos.y
  201. setmetatable(self, tabview_metatable)
  202. self.fixed_size = true
  203. self.hidden = true
  204. self.current_tab = nil
  205. self.last_tab_index = 1
  206. self.tablist = {}
  207. self.autosave_tab = false
  208. ui.add(self)
  209. return self
  210. end