2
0

buttonbar.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. local function buttonbar_formspec(self)
  18. if self.hidden then
  19. return ""
  20. end
  21. local formspec = string.format("box[%f,%f;%f,%f;%s]",
  22. self.pos.x,self.pos.y ,self.size.x,self.size.y,self.bgcolor)
  23. for i=self.startbutton,#self.buttons,1 do
  24. local btn_name = self.buttons[i].name
  25. local btn_pos = {}
  26. if self.orientation == "horizontal" then
  27. btn_pos.x = self.pos.x + --base pos
  28. (i - self.startbutton) * self.btn_size + --button offset
  29. self.btn_initial_offset
  30. else
  31. btn_pos.x = self.pos.x + (self.btn_size * 0.05)
  32. end
  33. if self.orientation == "vertical" then
  34. btn_pos.y = self.pos.y + --base pos
  35. (i - self.startbutton) * self.btn_size + --button offset
  36. self.btn_initial_offset
  37. else
  38. btn_pos.y = self.pos.y + (self.btn_size * 0.05)
  39. end
  40. if (self.orientation == "vertical" and
  41. (btn_pos.y + self.btn_size <= self.pos.y + self.size.y)) or
  42. (self.orientation == "horizontal" and
  43. (btn_pos.x + self.btn_size <= self.pos.x + self.size.x)) then
  44. local borders="true"
  45. if self.buttons[i].image ~= nil then
  46. borders="false"
  47. end
  48. formspec = formspec ..
  49. string.format("image_button[%f,%f;%f,%f;%s;%s;%s;true;%s]tooltip[%s;%s]",
  50. btn_pos.x, btn_pos.y, self.btn_size, self.btn_size,
  51. self.buttons[i].image, btn_name, self.buttons[i].caption,
  52. borders, btn_name, self.buttons[i].tooltip)
  53. else
  54. --print("end of displayable buttons: orientation: " .. self.orientation)
  55. --print( "button_end: " .. (btn_pos.y + self.btn_size - (self.btn_size * 0.05)))
  56. --print( "bar_end: " .. (self.pos.x + self.size.x))
  57. break
  58. end
  59. end
  60. if (self.have_move_buttons) then
  61. local btn_dec_pos = {}
  62. btn_dec_pos.x = self.pos.x + (self.btn_size * 0.05)
  63. btn_dec_pos.y = self.pos.y + (self.btn_size * 0.05)
  64. local btn_inc_pos = {}
  65. local btn_size = {}
  66. if self.orientation == "horizontal" then
  67. btn_size.x = 0.5
  68. btn_size.y = self.btn_size
  69. btn_inc_pos.x = self.pos.x + self.size.x - 0.5
  70. btn_inc_pos.y = self.pos.y + (self.btn_size * 0.05)
  71. else
  72. btn_size.x = self.btn_size
  73. btn_size.y = 0.5
  74. btn_inc_pos.x = self.pos.x + (self.btn_size * 0.05)
  75. btn_inc_pos.y = self.pos.y + self.size.y - 0.5
  76. end
  77. local text_dec = "<"
  78. local text_inc = ">"
  79. if self.orientation == "vertical" then
  80. text_dec = "^"
  81. text_inc = "v"
  82. end
  83. formspec = formspec ..
  84. string.format("image_button[%f,%f;%f,%f;;btnbar_dec_%s;%s;true;true]",
  85. btn_dec_pos.x, btn_dec_pos.y, btn_size.x, btn_size.y,
  86. self.name, text_dec)
  87. formspec = formspec ..
  88. string.format("image_button[%f,%f;%f,%f;;btnbar_inc_%s;%s;true;true]",
  89. btn_inc_pos.x, btn_inc_pos.y, btn_size.x, btn_size.y,
  90. self.name, text_inc)
  91. end
  92. return formspec
  93. end
  94. local function buttonbar_buttonhandler(self, fields)
  95. if fields["btnbar_inc_" .. self.name] ~= nil and
  96. self.startbutton < #self.buttons then
  97. self.startbutton = self.startbutton + 1
  98. return true
  99. end
  100. if fields["btnbar_dec_" .. self.name] ~= nil and self.startbutton > 1 then
  101. self.startbutton = self.startbutton - 1
  102. return true
  103. end
  104. for i=1,#self.buttons,1 do
  105. if fields[self.buttons[i].name] ~= nil then
  106. return self.userbuttonhandler(fields)
  107. end
  108. end
  109. end
  110. local buttonbar_metatable = {
  111. handle_buttons = buttonbar_buttonhandler,
  112. handle_events = function(self, event) end,
  113. get_formspec = buttonbar_formspec,
  114. hide = function(self) self.hidden = true end,
  115. show = function(self) self.hidden = false end,
  116. delete = function(self) ui.delete(self) end,
  117. add_button = function(self, name, caption, image, tooltip)
  118. if caption == nil then caption = "" end
  119. if image == nil then image = "" end
  120. if tooltip == nil then tooltip = "" end
  121. table.insert(self.buttons,{ name=name, caption=caption, image=image, tooltip=tooltip})
  122. if self.orientation == "horizontal" then
  123. if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
  124. > self.size.x ) then
  125. self.btn_initial_offset = self.btn_size * 0.05 + 0.5
  126. self.have_move_buttons = true
  127. end
  128. else
  129. if ((self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
  130. > self.size.y ) then
  131. self.btn_initial_offset = self.btn_size * 0.05 + 0.5
  132. self.have_move_buttons = true
  133. end
  134. end
  135. end,
  136. set_bgparams = function(self, bgcolor)
  137. if (type(bgcolor) == "string") then
  138. self.bgcolor = bgcolor
  139. end
  140. end,
  141. }
  142. buttonbar_metatable.__index = buttonbar_metatable
  143. function buttonbar_create(name, cbf_buttonhandler, pos, orientation, size)
  144. assert(name ~= nil)
  145. assert(cbf_buttonhandler ~= nil)
  146. assert(orientation == "vertical" or orientation == "horizontal")
  147. assert(pos ~= nil and type(pos) == "table")
  148. assert(size ~= nil and type(size) == "table")
  149. local self = {}
  150. self.name = name
  151. self.type = "addon"
  152. self.bgcolor = "#000000"
  153. self.pos = pos
  154. self.size = size
  155. self.orientation = orientation
  156. self.startbutton = 1
  157. self.have_move_buttons = false
  158. self.hidden = false
  159. if self.orientation == "horizontal" then
  160. self.btn_size = self.size.y
  161. else
  162. self.btn_size = self.size.x
  163. end
  164. if (self.btn_initial_offset == nil) then
  165. self.btn_initial_offset = self.btn_size * 0.05
  166. end
  167. self.userbuttonhandler = cbf_buttonhandler
  168. self.buttons = {}
  169. setmetatable(self,buttonbar_metatable)
  170. ui.add(self)
  171. return self
  172. end