tabview.lua 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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,1 do
  128. if toadd ~= "" then
  129. toadd = toadd .. ","
  130. end
  131. toadd = toadd .. self.tablist[i].caption
  132. end
  133. return string.format("tabheader[%f,%f;%f,%f;%s;%s;%i;true;false]",
  134. self.header_x, self.header_y, size.width, size.height, self.name, toadd, self.last_tab_index)
  135. end
  136. --------------------------------------------------------------------------------
  137. local function switch_to_tab(self, index)
  138. --first call on_change for tab to leave
  139. if self.tablist[self.last_tab_index].on_change ~= nil then
  140. self.tablist[self.last_tab_index].on_change("LEAVE",
  141. self.current_tab, self.tablist[index].name)
  142. end
  143. --update tabview data
  144. self.last_tab_index = index
  145. local old_tab = self.current_tab
  146. self.current_tab = self.tablist[index].name
  147. if (self.autosave_tab) then
  148. core.settings:set(self.name .. "_LAST",self.current_tab)
  149. end
  150. -- call for tab to enter
  151. if self.tablist[index].on_change ~= nil then
  152. self.tablist[index].on_change("ENTER",
  153. old_tab,self.current_tab)
  154. end
  155. end
  156. --------------------------------------------------------------------------------
  157. local function handle_tab_buttons(self,fields)
  158. --save tab selection to config file
  159. if fields[self.name] then
  160. local index = tonumber(fields[self.name])
  161. switch_to_tab(self, index)
  162. return true
  163. end
  164. return false
  165. end
  166. --------------------------------------------------------------------------------
  167. local function set_tab_by_name(self, name)
  168. for i=1,#self.tablist,1 do
  169. if self.tablist[i].name == name then
  170. switch_to_tab(self, i)
  171. return true
  172. end
  173. end
  174. return false
  175. end
  176. --------------------------------------------------------------------------------
  177. local function hide_tabview(self)
  178. self.hidden=true
  179. --call on_change as we're not gonna show self tab any longer
  180. if self.tablist[self.last_tab_index].on_change ~= nil then
  181. self.tablist[self.last_tab_index].on_change("LEAVE",
  182. self.current_tab, nil)
  183. end
  184. end
  185. --------------------------------------------------------------------------------
  186. local function show_tabview(self)
  187. self.hidden=false
  188. -- call for tab to enter
  189. if self.tablist[self.last_tab_index].on_change ~= nil then
  190. self.tablist[self.last_tab_index].on_change("ENTER",
  191. nil,self.current_tab)
  192. end
  193. end
  194. local tabview_metatable = {
  195. add = add_tab,
  196. handle_buttons = handle_buttons,
  197. handle_events = handle_events,
  198. get_formspec = get_formspec,
  199. show = show_tabview,
  200. hide = hide_tabview,
  201. delete = function(self) ui.delete(self) end,
  202. set_parent = function(self,parent) self.parent = parent end,
  203. set_autosave_tab =
  204. function(self,value) self.autosave_tab = value end,
  205. set_tab = set_tab_by_name,
  206. set_global_button_handler =
  207. function(self,handler) self.glb_btn_handler = handler end,
  208. set_global_event_handler =
  209. function(self,handler) self.glb_evt_handler = handler end,
  210. set_fixed_size =
  211. function(self,state) self.fixed_size = state end,
  212. set_end_button =
  213. function(self, v) self.end_button = v end,
  214. tab_header = tab_header,
  215. handle_tab_buttons = handle_tab_buttons
  216. }
  217. tabview_metatable.__index = tabview_metatable
  218. --------------------------------------------------------------------------------
  219. function tabview_create(name, size, tabheaderpos)
  220. local self = {}
  221. self.name = name
  222. self.type = "toplevel"
  223. self.width = size.x
  224. self.height = size.y
  225. self.header_x = tabheaderpos.x
  226. self.header_y = tabheaderpos.y
  227. setmetatable(self, tabview_metatable)
  228. self.fixed_size = true
  229. self.hidden = true
  230. self.current_tab = nil
  231. self.last_tab_index = 1
  232. self.tablist = {}
  233. self.autosave_tab = false
  234. ui.add(self)
  235. return self
  236. end