buttonbar.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. self.buttons[#self.buttons + 1] = {
  122. name = name,
  123. caption = caption,
  124. image = image,
  125. tooltip = tooltip
  126. }
  127. if self.orientation == "horizontal" then
  128. if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
  129. > self.size.x ) then
  130. self.btn_initial_offset = self.btn_size * 0.05 + 0.5
  131. self.have_move_buttons = true
  132. end
  133. else
  134. if ((self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
  135. > self.size.y ) then
  136. self.btn_initial_offset = self.btn_size * 0.05 + 0.5
  137. self.have_move_buttons = true
  138. end
  139. end
  140. end,
  141. set_bgparams = function(self, bgcolor)
  142. if (type(bgcolor) == "string") then
  143. self.bgcolor = bgcolor
  144. end
  145. end,
  146. }
  147. buttonbar_metatable.__index = buttonbar_metatable
  148. function buttonbar_create(name, cbf_buttonhandler, pos, orientation, size)
  149. assert(name ~= nil)
  150. assert(cbf_buttonhandler ~= nil)
  151. assert(orientation == "vertical" or orientation == "horizontal")
  152. assert(pos ~= nil and type(pos) == "table")
  153. assert(size ~= nil and type(size) == "table")
  154. local self = {}
  155. self.name = name
  156. self.type = "addon"
  157. self.bgcolor = "#000000"
  158. self.pos = pos
  159. self.size = size
  160. self.orientation = orientation
  161. self.startbutton = 1
  162. self.have_move_buttons = false
  163. self.hidden = false
  164. if self.orientation == "horizontal" then
  165. self.btn_size = self.size.y
  166. else
  167. self.btn_size = self.size.x
  168. end
  169. if (self.btn_initial_offset == nil) then
  170. self.btn_initial_offset = self.btn_size * 0.05
  171. end
  172. self.userbuttonhandler = cbf_buttonhandler
  173. self.buttons = {}
  174. setmetatable(self,buttonbar_metatable)
  175. ui.add(self)
  176. return self
  177. end