tabview.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. formspec_version = tab.formspec_version,
  42. on_change = tab.on_change,
  43. tabdata = {},
  44. }
  45. self.tablist[#self.tablist + 1] = newtab
  46. if self.last_tab_index == #self.tablist then
  47. self.current_tab = tab.name
  48. if tab.on_activate ~= nil then
  49. tab.on_activate(nil,tab.name)
  50. end
  51. end
  52. end
  53. --------------------------------------------------------------------------------
  54. local function get_formspec(self)
  55. if self.hidden or (self.parent ~= nil and self.parent.hidden) then
  56. return ""
  57. end
  58. local tab = self.tablist[self.last_tab_index]
  59. local content, prepend = tab.get_formspec(self, tab.name, tab.tabdata, tab.tabsize)
  60. if self.parent == nil and not prepend then
  61. local tsize = tab.tabsize or {width=self.width, height=self.height}
  62. prepend = string.format("size[%f,%f,%s]", tsize.width, tsize.height,
  63. dump(self.fixed_size))
  64. if tab.formspec_version then
  65. prepend = ("formspec_version[%d]"):format(tab.formspec_version) .. prepend
  66. end
  67. end
  68. local formspec = (prepend or "") .. self:tab_header() .. content
  69. return formspec
  70. end
  71. --------------------------------------------------------------------------------
  72. local function handle_buttons(self,fields)
  73. if self.hidden then
  74. return false
  75. end
  76. if self:handle_tab_buttons(fields) then
  77. return true
  78. end
  79. if self.glb_btn_handler ~= nil and
  80. self.glb_btn_handler(self,fields) then
  81. return true
  82. end
  83. local tab = self.tablist[self.last_tab_index]
  84. if tab.button_handler ~= nil then
  85. return tab.button_handler(self, fields, tab.name, tab.tabdata)
  86. end
  87. return false
  88. end
  89. --------------------------------------------------------------------------------
  90. local function handle_events(self,event)
  91. if self.hidden then
  92. return false
  93. end
  94. if self.glb_evt_handler ~= nil and
  95. self.glb_evt_handler(self,event) then
  96. return true
  97. end
  98. local tab = self.tablist[self.last_tab_index]
  99. if tab.evt_handler ~= nil then
  100. return tab.evt_handler(self, event, tab.name, tab.tabdata)
  101. end
  102. return false
  103. end
  104. --------------------------------------------------------------------------------
  105. local function tab_header(self)
  106. local toadd = ""
  107. for i=1,#self.tablist,1 do
  108. if toadd ~= "" then
  109. toadd = toadd .. ","
  110. end
  111. toadd = toadd .. self.tablist[i].caption
  112. end
  113. return string.format("tabheader[%f,%f;%s;%s;%i;true;false]",
  114. self.header_x, self.header_y, self.name, toadd, self.last_tab_index);
  115. end
  116. --------------------------------------------------------------------------------
  117. local function switch_to_tab(self, index)
  118. --first call on_change for tab to leave
  119. if self.tablist[self.last_tab_index].on_change ~= nil then
  120. self.tablist[self.last_tab_index].on_change("LEAVE",
  121. self.current_tab, self.tablist[index].name)
  122. end
  123. --update tabview data
  124. self.last_tab_index = index
  125. local old_tab = self.current_tab
  126. self.current_tab = self.tablist[index].name
  127. if (self.autosave_tab) then
  128. core.settings:set(self.name .. "_LAST",self.current_tab)
  129. end
  130. -- call for tab to enter
  131. if self.tablist[index].on_change ~= nil then
  132. self.tablist[index].on_change("ENTER",
  133. old_tab,self.current_tab)
  134. end
  135. end
  136. --------------------------------------------------------------------------------
  137. local function handle_tab_buttons(self,fields)
  138. --save tab selection to config file
  139. if fields[self.name] then
  140. local index = tonumber(fields[self.name])
  141. switch_to_tab(self, index)
  142. return true
  143. end
  144. return false
  145. end
  146. --------------------------------------------------------------------------------
  147. local function set_tab_by_name(self, name)
  148. for i=1,#self.tablist,1 do
  149. if self.tablist[i].name == name then
  150. switch_to_tab(self, i)
  151. return true
  152. end
  153. end
  154. return false
  155. end
  156. --------------------------------------------------------------------------------
  157. local function hide_tabview(self)
  158. self.hidden=true
  159. --call on_change as we're not gonna show self tab any longer
  160. if self.tablist[self.last_tab_index].on_change ~= nil then
  161. self.tablist[self.last_tab_index].on_change("LEAVE",
  162. self.current_tab, nil)
  163. end
  164. end
  165. --------------------------------------------------------------------------------
  166. local function show_tabview(self)
  167. self.hidden=false
  168. -- call for tab to enter
  169. if self.tablist[self.last_tab_index].on_change ~= nil then
  170. self.tablist[self.last_tab_index].on_change("ENTER",
  171. nil,self.current_tab)
  172. end
  173. end
  174. local tabview_metatable = {
  175. add = add_tab,
  176. handle_buttons = handle_buttons,
  177. handle_events = handle_events,
  178. get_formspec = get_formspec,
  179. show = show_tabview,
  180. hide = hide_tabview,
  181. delete = function(self) ui.delete(self) end,
  182. set_parent = function(self,parent) self.parent = parent end,
  183. set_autosave_tab =
  184. function(self,value) self.autosave_tab = value end,
  185. set_tab = set_tab_by_name,
  186. set_global_button_handler =
  187. function(self,handler) self.glb_btn_handler = handler end,
  188. set_global_event_handler =
  189. function(self,handler) self.glb_evt_handler = handler end,
  190. set_fixed_size =
  191. function(self,state) self.fixed_size = state end,
  192. tab_header = tab_header,
  193. handle_tab_buttons = handle_tab_buttons
  194. }
  195. tabview_metatable.__index = tabview_metatable
  196. --------------------------------------------------------------------------------
  197. function tabview_create(name, size, tabheaderpos)
  198. local self = {}
  199. self.name = name
  200. self.type = "toplevel"
  201. self.width = size.x
  202. self.height = size.y
  203. self.header_x = tabheaderpos.x
  204. self.header_y = tabheaderpos.y
  205. setmetatable(self, tabview_metatable)
  206. self.fixed_size = true
  207. self.hidden = true
  208. self.current_tab = nil
  209. self.last_tab_index = 1
  210. self.tablist = {}
  211. self.autosave_tab = false
  212. ui.add(self)
  213. return self
  214. end