|
| 1 | +--Minetest |
| 2 | +--Copyright (C) 2014 sapier |
| 3 | +-- |
| 4 | +--self 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 | +--self 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 self program; if not, write to the Free Software Foundation, Inc., |
| 16 | +--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | + |
| 18 | + |
| 19 | +local function buttonbar_formspec(self) |
| 20 | + |
| 21 | + if self.hidden then |
| 22 | + return "" |
| 23 | + end |
| 24 | + |
| 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) |
| 27 | + |
| 28 | + for i=self.startbutton,#self.buttons,1 do |
| 29 | + local btn_name = self.buttons[i].name |
| 30 | + local btn_pos = {} |
| 31 | + |
| 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 |
| 36 | + else |
| 37 | + btn_pos.x = self.pos.x + (self.btn_size * 0.05) |
| 38 | + end |
| 39 | + |
| 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 |
| 44 | + else |
| 45 | + btn_pos.y = self.pos.y + (self.btn_size * 0.05) |
| 46 | + end |
| 47 | + |
| 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 |
| 52 | + |
| 53 | + local borders="true" |
| 54 | + |
| 55 | + if self.buttons[i].image ~= nil then |
| 56 | + borders="false" |
| 57 | + end |
| 58 | + |
| 59 | + formspec = formspec .. |
| 60 | + string.format("image_button[%f,%f;%f,%f;%s;%s;%s;true;%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) |
| 64 | + else |
| 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)) |
| 68 | + break |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 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 = {} |
| 77 | + local btn_size = {} |
| 78 | + |
| 79 | + if self.orientation == "horizontal" then |
| 80 | + btn_size.x = 0.5 |
| 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) |
| 84 | + else |
| 85 | + btn_size.x = self.btn_size |
| 86 | + btn_size.y = 0.5 |
| 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 |
| 89 | + end |
| 90 | + |
| 91 | + local text_dec = "<" |
| 92 | + local text_inc = ">" |
| 93 | + if self.orientation == "vertical" then |
| 94 | + text_dec = "^" |
| 95 | + text_inc = "v" |
| 96 | + end |
| 97 | + |
| 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, |
| 101 | + self.name, text_dec) |
| 102 | + |
| 103 | + formspec = formspec .. |
| 104 | + string.format("image_button[%f,%f;%f,%f;;btnbar_dec_%s;%s;true;true]", |
| 105 | + btn_inc_pos.x, btn_inc_pos.y, btn_size.x, btn_size.y, |
| 106 | + self.name, text_inc) |
| 107 | + end |
| 108 | + |
| 109 | + return formspec |
| 110 | +end |
| 111 | + |
| 112 | +local function buttonbar_buttonhandler(self, fields) |
| 113 | + |
| 114 | + if fields["btnbar_inc_" .. self.name] ~= nil and |
| 115 | + self.startbutton < #self.buttons then |
| 116 | + |
| 117 | + self.startbutton = self.startbutton + 1 |
| 118 | + return true |
| 119 | + end |
| 120 | + |
| 121 | + if fields["btnbar_dec_" .. self.name] ~= nil and self.startbutton > 1 then |
| 122 | + self.startbutton = self.startbutton - 1 |
| 123 | + return true |
| 124 | + end |
| 125 | + |
| 126 | + for i=1,#self.buttons,1 do |
| 127 | + if fields[self.buttons[i].name] ~= nil then |
| 128 | + return self.userbuttonhandler(fields) |
| 129 | + end |
| 130 | + end |
| 131 | +end |
| 132 | + |
| 133 | +local buttonbar_metatable = { |
| 134 | + handle_buttons = buttonbar_buttonhandler, |
| 135 | + handle_events = function(self, event) end, |
| 136 | + get_formspec = buttonbar_formspec, |
| 137 | + |
| 138 | + hide = function(self) self.hidden = true end, |
| 139 | + show = function(self) self.hidden = false end, |
| 140 | + |
| 141 | + delete = function(self) ui.delete(self) end, |
| 142 | + |
| 143 | + add_button = function(self, name, caption, image) |
| 144 | + if caption == nil then caption = "" end |
| 145 | + if image == nil then image = "" end |
| 146 | + |
| 147 | + table.insert(self.buttons,{ name=name, caption=caption, image=image}) |
| 148 | + if self.orientation == "horizontal" then |
| 149 | + if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2) |
| 150 | + > self.size.x ) then |
| 151 | + |
| 152 | + self.btn_initial_offset = self.btn_size * 0.05 + 0.5 |
| 153 | + self.have_move_buttons = true |
| 154 | + end |
| 155 | + else |
| 156 | + if ((self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2) |
| 157 | + > self.size.y ) then |
| 158 | + |
| 159 | + self.btn_initial_offset = self.btn_size * 0.05 + 0.5 |
| 160 | + self.have_move_buttons = true |
| 161 | + end |
| 162 | + end |
| 163 | + end, |
| 164 | + |
| 165 | + set_bgparams = function(self, bgcolor) |
| 166 | + if (type(bgcolor) == "string") then |
| 167 | + self.bgcolor = bgcolor |
| 168 | + end |
| 169 | + end, |
| 170 | +} |
| 171 | + |
| 172 | +buttonbar_metatable.__index = buttonbar_metatable |
| 173 | + |
| 174 | +function buttonbar_create(name, cbf_buttonhandler, pos, orientation, size) |
| 175 | + assert(name ~= nil) |
| 176 | + assert(cbf_buttonhandler ~= nil) |
| 177 | + assert(orientation == "vertical" or orientation == "horizontal") |
| 178 | + assert(pos ~= nil and type(pos) == "table") |
| 179 | + assert(size ~= nil and type(size) == "table") |
| 180 | + |
| 181 | + local self = {} |
| 182 | + self.name = name |
| 183 | + self.type = "addon" |
| 184 | + self.bgcolor = "#000000" |
| 185 | + self.pos = pos |
| 186 | + self.size = size |
| 187 | + self.orientation = orientation |
| 188 | + self.startbutton = 1 |
| 189 | + self.have_move_buttons = false |
| 190 | + self.hidden = false |
| 191 | + |
| 192 | + if self.orientation == "horizontal" then |
| 193 | + self.btn_size = self.size.y |
| 194 | + else |
| 195 | + self.btn_size = self.size.x |
| 196 | + end |
| 197 | + |
| 198 | + if (self.btn_initial_offset == nil) then |
| 199 | + self.btn_initial_offset = self.btn_size * 0.05 |
| 200 | + end |
| 201 | + |
| 202 | + self.userbuttonhandler = cbf_buttonhandler |
| 203 | + self.buttons = {} |
| 204 | + |
| 205 | + setmetatable(self,buttonbar_metatable) |
| 206 | + |
| 207 | + ui.add(self) |
| 208 | + return self |
| 209 | +end |
0 commit comments