2 --Copyright (C) 2014 sapier
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.
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.
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.
19 local function buttonbar_formspec(self)
25 local formspec = string.format("box[%f,%f;%f,%f;%s]",
26 self.pos.x,self.pos.y ,self.size.x,self.size.y,self.bgcolor)
28 for i=self.startbutton,#self.buttons,1 do
29 local btn_name = self.buttons[i].name
32 if self.orientation == "horizontal" then
33 btn_pos.x = self.pos.x + --base pos
34 (i - self.startbutton) * self.btn_size + --button offset
35 self.btn_initial_offset
37 btn_pos.x = self.pos.x + (self.btn_size * 0.05)
40 if self.orientation == "vertical" then
41 btn_pos.y = self.pos.y + --base pos
42 (i - self.startbutton) * self.btn_size + --button offset
43 self.btn_initial_offset
45 btn_pos.y = self.pos.y + (self.btn_size * 0.05)
48 if (self.orientation == "vertical" and
49 (btn_pos.y + self.btn_size <= self.pos.y + self.size.y)) or
50 (self.orientation == "horizontal" and
51 (btn_pos.x + self.btn_size <= self.pos.x + self.size.x)) then
55 if self.buttons[i].image ~= nil then
59 formspec = formspec ..
60 string.format("image_button[%f,%f;%f,%f;%s;%s;%s;true;%s]tooltip[%s;%s]",
61 btn_pos.x, btn_pos.y, self.btn_size, self.btn_size,
62 self.buttons[i].image, btn_name, self.buttons[i].caption,
63 borders, btn_name, self.buttons[i].tooltip)
65 --print("end of displayable buttons: orientation: " .. self.orientation)
66 --print( "button_end: " .. (btn_pos.y + self.btn_size - (self.btn_size * 0.05)))
67 --print( "bar_end: " .. (self.pos.x + self.size.x))
72 if (self.have_move_buttons) then
73 local btn_dec_pos = {}
74 btn_dec_pos.x = self.pos.x + (self.btn_size * 0.05)
75 btn_dec_pos.y = self.pos.y + (self.btn_size * 0.05)
76 local btn_inc_pos = {}
79 if self.orientation == "horizontal" then
81 btn_size.y = self.btn_size
82 btn_inc_pos.x = self.pos.x + self.size.x - 0.5
83 btn_inc_pos.y = self.pos.y + (self.btn_size * 0.05)
85 btn_size.x = self.btn_size
87 btn_inc_pos.x = self.pos.x + (self.btn_size * 0.05)
88 btn_inc_pos.y = self.pos.y + self.size.y - 0.5
93 if self.orientation == "vertical" then
98 formspec = formspec ..
99 string.format("image_button[%f,%f;%f,%f;;btnbar_dec_%s;%s;true;true]",
100 btn_dec_pos.x, btn_dec_pos.y, btn_size.x, btn_size.y,
103 formspec = formspec ..
104 string.format("image_button[%f,%f;%f,%f;;btnbar_inc_%s;%s;true;true]",
105 btn_inc_pos.x, btn_inc_pos.y, btn_size.x, btn_size.y,
112 local function buttonbar_buttonhandler(self, fields)
114 if fields["btnbar_inc_" .. self.name] ~= nil and
115 self.startbutton < #self.buttons then
117 self.startbutton = self.startbutton + 1
121 if fields["btnbar_dec_" .. self.name] ~= nil and self.startbutton > 1 then
122 self.startbutton = self.startbutton - 1
126 for i=1,#self.buttons,1 do
127 if fields[self.buttons[i].name] ~= nil then
128 return self.userbuttonhandler(fields)
133 local buttonbar_metatable = {
134 handle_buttons = buttonbar_buttonhandler,
135 handle_events = function(self, event) end,
136 get_formspec = buttonbar_formspec,
138 hide = function(self) self.hidden = true end,
139 show = function(self) self.hidden = false end,
141 delete = function(self) ui.delete(self) end,
143 add_button = function(self, name, caption, image, tooltip)
144 if caption == nil then caption = "" end
145 if image == nil then image = "" end
146 if tooltip == nil then tooltip = "" end
148 self.buttons[#self.buttons + 1] = {
154 if self.orientation == "horizontal" then
155 if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
158 self.btn_initial_offset = self.btn_size * 0.05 + 0.5
159 self.have_move_buttons = true
162 if ((self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
165 self.btn_initial_offset = self.btn_size * 0.05 + 0.5
166 self.have_move_buttons = true
171 set_bgparams = function(self, bgcolor)
172 if (type(bgcolor) == "string") then
173 self.bgcolor = bgcolor
178 buttonbar_metatable.__index = buttonbar_metatable
180 function buttonbar_create(name, cbf_buttonhandler, pos, orientation, size)
182 assert(cbf_buttonhandler ~= nil)
183 assert(orientation == "vertical" or orientation == "horizontal")
184 assert(pos ~= nil and type(pos) == "table")
185 assert(size ~= nil and type(size) == "table")
190 self.bgcolor = "#000000"
193 self.orientation = orientation
195 self.have_move_buttons = false
198 if self.orientation == "horizontal" then
199 self.btn_size = self.size.y
201 self.btn_size = self.size.x
204 if (self.btn_initial_offset == nil) then
205 self.btn_initial_offset = self.btn_size * 0.05
208 self.userbuttonhandler = cbf_buttonhandler
211 setmetatable(self,buttonbar_metatable)