tabview.lua 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 or 6,
  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. local tsize = tab.tabsize or { width = self.width, height = self.height }
  61. if self.parent == nil and not prepend then
  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 end_button_size = 0.75
  69. local tab_header_size = { width = tsize.width, height = 0.85 }
  70. if self.end_button then
  71. tab_header_size.width = tab_header_size.width - end_button_size - 0.1
  72. end
  73. local formspec = (prepend or "") .. self:tab_header(tab_header_size) .. content
  74. if self.end_button then
  75. formspec = formspec ..
  76. ("style[%s;noclip=true;border=false]"):format(self.end_button.name) ..
  77. ("tooltip[%s;%s]"):format(self.end_button.name, self.end_button.label) ..
  78. ("image_button[%f,%f;%f,%f;%s;%s;]"):format(
  79. self.width - end_button_size,
  80. (-tab_header_size.height - end_button_size) / 2,
  81. end_button_size,
  82. end_button_size,
  83. core.formspec_escape(self.end_button.icon),
  84. self.end_button.name)
  85. end
  86. return formspec
  87. end
  88. --------------------------------------------------------------------------------
  89. local function handle_buttons(self,fields)
  90. if self.hidden then
  91. return false
  92. end
  93. if self:handle_tab_buttons(fields) then
  94. return true
  95. end
  96. if self.end_button and fields[self.end_button.name] then
  97. return self.end_button.on_click(self)
  98. end
  99. if self.glb_btn_handler ~= nil and
  100. self.glb_btn_handler(self, fields) then
  101. return true
  102. end
  103. local tab = self.tablist[self.last_tab_index]
  104. if tab.button_handler ~= nil then
  105. return tab.button_handler(self, fields, tab.name, tab.tabdata)
  106. end
  107. return false
  108. end
  109. --------------------------------------------------------------------------------
  110. local function handle_events(self,event)
  111. if self.hidden then
  112. return false
  113. end
  114. if self.glb_evt_handler ~= nil and
  115. self.glb_evt_handler(self,event) then
  116. return true
  117. end
  118. local tab = self.tablist[self.last_tab_index]
  119. if tab.evt_handler ~= nil then
  120. return tab.evt_handler(self, event, tab.name, tab.tabdata)
  121. end
  122. return false
  123. end
  124. --------------------------------------------------------------------------------
  125. local function tab_header(self, size)
  126. local toadd = ""
  127. for i = 1, #self.tablist do
  128. if toadd ~= "" then
  129. toadd = toadd .. ","
  130. end
  131. local caption = self.tablist[i].caption
  132. if type(caption) == "function" then
  133. caption = caption(self)
  134. end
  135. toadd = toadd .. caption
  136. end
  137. return string.format("tabheader[%f,%f;%f,%f;%s;%s;%i;true;false]",
  138. self.header_x, self.header_y, size.width, size.height, self.name, toadd, self.last_tab_index)
  139. end
  140. --------------------------------------------------------------------------------
  141. local function switch_to_tab(self, index)
  142. --first call on_change for tab to leave
  143. if self.tablist[self.last_tab_index].on_change ~= nil then
  144. self.tablist[self.last_tab_index].on_change("LEAVE",
  145. self.current_tab, self.tablist[index].name)
  146. end
  147. --update tabview data
  148. self.last_tab_index = index
  149. local old_tab = self.current_tab
  150. self.current_tab = self.tablist[index].name
  151. if (self.autosave_tab) then
  152. core.settings:set(self.name .. "_LAST",self.current_tab)
  153. end
  154. -- call for tab to enter
  155. if self.tablist[index].on_change ~= nil then
  156. self.tablist[index].on_change("ENTER",
  157. old_tab,self.current_tab)
  158. end
  159. end
  160. --------------------------------------------------------------------------------
  161. local function handle_tab_buttons(self,fields)
  162. --save tab selection to config file
  163. if fields[self.name] then
  164. local index = tonumber(fields[self.name])
  165. switch_to_tab(self, index)
  166. return true
  167. end
  168. return false
  169. end
  170. --------------------------------------------------------------------------------
  171. local function set_tab_by_name(self, name)
  172. for i=1,#self.tablist,1 do
  173. if self.tablist[i].name == name then
  174. switch_to_tab(self, i)
  175. return true
  176. end
  177. end
  178. return false
  179. end
  180. --------------------------------------------------------------------------------
  181. local function hide_tabview(self)
  182. self.hidden=true
  183. --call on_change as we're not gonna show self tab any longer
  184. if self.tablist[self.last_tab_index].on_change ~= nil then
  185. self.tablist[self.last_tab_index].on_change("LEAVE",
  186. self.current_tab, nil)
  187. end
  188. end
  189. --------------------------------------------------------------------------------
  190. local function show_tabview(self)
  191. self.hidden=false
  192. -- call for tab to enter
  193. if self.tablist[self.last_tab_index].on_change ~= nil then
  194. self.tablist[self.last_tab_index].on_change("ENTER",
  195. nil,self.current_tab)
  196. end
  197. end
  198. local tabview_metatable = {
  199. add = add_tab,
  200. handle_buttons = handle_buttons,
  201. handle_events = handle_events,
  202. get_formspec = get_formspec,
  203. show = show_tabview,
  204. hide = hide_tabview,
  205. delete = function(self) ui.delete(self) end,
  206. set_parent = function(self,parent) self.parent = parent end,
  207. set_autosave_tab =
  208. function(self,value) self.autosave_tab = value end,
  209. set_tab = set_tab_by_name,
  210. set_global_button_handler =
  211. function(self,handler) self.glb_btn_handler = handler end,
  212. set_global_event_handler =
  213. function(self,handler) self.glb_evt_handler = handler end,
  214. set_fixed_size =
  215. function(self,state) self.fixed_size = state end,
  216. set_end_button =
  217. function(self, v) self.end_button = v end,
  218. tab_header = tab_header,
  219. handle_tab_buttons = handle_tab_buttons
  220. }
  221. tabview_metatable.__index = tabview_metatable
  222. --------------------------------------------------------------------------------
  223. function tabview_create(name, size, tabheaderpos)
  224. local self = {}
  225. self.name = name
  226. self.type = "toplevel"
  227. self.width = size.x
  228. self.height = size.y
  229. self.header_x = tabheaderpos.x
  230. self.header_y = tabheaderpos.y
  231. setmetatable(self, tabview_metatable)
  232. self.fixed_size = true
  233. self.hidden = true
  234. self.current_tab = nil
  235. self.last_tab_index = 1
  236. self.tablist = {}
  237. self.autosave_tab = false
  238. ui.add(self)
  239. return self
  240. end