buttonbar.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. --Minetest
  2. --Copyright (C) 2014 sapier
  3. --Copyright (C) 2023 Gregor Parzefall
  4. --
  5. --This program is free software; you can redistribute it and/or modify
  6. --it under the terms of the GNU Lesser General Public License as published by
  7. --the Free Software Foundation; either version 2.1 of the License, or
  8. --(at your option) any later version.
  9. --
  10. --This program is distributed in the hope that it will be useful,
  11. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. --GNU Lesser General Public License for more details.
  14. --
  15. --You should have received a copy of the GNU Lesser General Public License along
  16. --with this program; if not, write to the Free Software Foundation, Inc.,
  17. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. local BASE_SPACING = 0.1
  19. local function get_scroll_btn_width()
  20. return core.settings:get_bool("enable_touch") and 0.8 or 0.5
  21. end
  22. local function buttonbar_formspec(self)
  23. if self.hidden then
  24. return ""
  25. end
  26. local formspec = {
  27. "style_type[box;noclip=true]",
  28. string.format("box[%f,%f;%f,%f;%s]", self.pos.x, self.pos.y, self.size.x,
  29. self.size.y, self.bgcolor),
  30. "style_type[box;noclip=false]",
  31. }
  32. local btn_size = self.size.y - 2*BASE_SPACING
  33. -- Spacing works like CSS Flexbox with `justify-content: space-evenly;`.
  34. -- `BASE_SPACING` is used as the minimum spacing, like `gap` in CSS Flexbox.
  35. -- The number of buttons per page is always calculated as if the scroll
  36. -- buttons were visible.
  37. local avail_space = self.size.x - 2*BASE_SPACING - 2*get_scroll_btn_width()
  38. local btns_per_page = math.floor((avail_space - BASE_SPACING) / (btn_size + BASE_SPACING))
  39. self.num_pages = math.ceil(#self.buttons / btns_per_page)
  40. self.cur_page = math.min(self.cur_page, self.num_pages)
  41. local first_btn = (self.cur_page - 1) * btns_per_page + 1
  42. local show_scroll_btns = self.num_pages > 1
  43. -- In contrast, the button spacing calculation takes hidden scroll buttons
  44. -- into account.
  45. local real_avail_space = show_scroll_btns and avail_space or self.size.x
  46. local btn_spacing = (real_avail_space - btns_per_page * btn_size) / (btns_per_page + 1)
  47. local btn_start_x = self.pos.x + btn_spacing
  48. if show_scroll_btns then
  49. btn_start_x = btn_start_x + BASE_SPACING + get_scroll_btn_width()
  50. end
  51. for i = first_btn, first_btn + btns_per_page - 1 do
  52. local btn = self.buttons[i]
  53. if btn == nil then
  54. break
  55. end
  56. local btn_pos = {
  57. x = btn_start_x + (i - first_btn) * (btn_size + btn_spacing),
  58. y = self.pos.y + BASE_SPACING,
  59. }
  60. table.insert(formspec, string.format("image_button[%f,%f;%f,%f;%s;%s;%s;true;false]tooltip[%s;%s]",
  61. btn_pos.x, btn_pos.y, btn_size, btn_size, btn.image, btn.name,
  62. btn.caption, btn.name, btn.tooltip))
  63. end
  64. if show_scroll_btns then
  65. local btn_prev_pos = {
  66. x = self.pos.x + BASE_SPACING,
  67. y = self.pos.y + BASE_SPACING,
  68. }
  69. local btn_next_pos = {
  70. x = self.pos.x + self.size.x - BASE_SPACING - get_scroll_btn_width(),
  71. y = self.pos.y + BASE_SPACING,
  72. }
  73. table.insert(formspec, string.format("style[%s,%s;noclip=true]",
  74. self.btn_prev_name, self.btn_next_name))
  75. table.insert(formspec, string.format("button[%f,%f;%f,%f;%s;<]",
  76. btn_prev_pos.x, btn_prev_pos.y, get_scroll_btn_width(), btn_size,
  77. self.btn_prev_name))
  78. table.insert(formspec, string.format("button[%f,%f;%f,%f;%s;>]",
  79. btn_next_pos.x, btn_next_pos.y, get_scroll_btn_width(), btn_size,
  80. self.btn_next_name))
  81. end
  82. return table.concat(formspec)
  83. end
  84. local function buttonbar_buttonhandler(self, fields)
  85. if fields[self.btn_prev_name] and self.cur_page > 1 then
  86. self.cur_page = self.cur_page - 1
  87. return true
  88. end
  89. if fields[self.btn_next_name] and self.cur_page < self.num_pages then
  90. self.cur_page = self.cur_page + 1
  91. return true
  92. end
  93. for _, btn in ipairs(self.buttons) do
  94. if fields[btn.name] then
  95. return self.userbuttonhandler(fields)
  96. end
  97. end
  98. end
  99. local buttonbar_metatable = {
  100. handle_buttons = buttonbar_buttonhandler,
  101. handle_events = function(self, event) end,
  102. get_formspec = buttonbar_formspec,
  103. hide = function(self) self.hidden = true end,
  104. show = function(self) self.hidden = false end,
  105. delete = function(self) ui.delete(self) end,
  106. add_button = function(self, name, caption, image, tooltip)
  107. if caption == nil then caption = "" end
  108. if image == nil then image = "" end
  109. if tooltip == nil then tooltip = "" end
  110. table.insert(self.buttons, {
  111. name = name,
  112. caption = caption,
  113. image = image,
  114. tooltip = tooltip,
  115. })
  116. end,
  117. }
  118. buttonbar_metatable.__index = buttonbar_metatable
  119. function buttonbar_create(name, pos, size, bgcolor, cbf_buttonhandler)
  120. assert(type(name) == "string" )
  121. assert(type(pos) == "table" )
  122. assert(type(size) == "table" )
  123. assert(type(bgcolor) == "string" )
  124. assert(type(cbf_buttonhandler) == "function")
  125. local self = {}
  126. self.type = "addon"
  127. self.name = name
  128. self.pos = pos
  129. self.size = size
  130. self.bgcolor = bgcolor
  131. self.userbuttonhandler = cbf_buttonhandler
  132. self.hidden = false
  133. self.buttons = {}
  134. self.num_pages = 1
  135. self.cur_page = 1
  136. self.btn_prev_name = "btnbar_prev_" .. self.name
  137. self.btn_next_name = "btnbar_next_" .. self.name
  138. setmetatable(self, buttonbar_metatable)
  139. ui.add(self)
  140. return self
  141. end