]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/fstk/buttonbar.lua
core.formspec_escape: Restore backwards compat
[dragonfireclient.git] / builtin / fstk / buttonbar.lua
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
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]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)
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_inc_%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, tooltip)
144                         if caption == nil then caption = "" end
145                         if image == nil then image = "" end
146                         if tooltip == nil then tooltip = "" end
147
148                         self.buttons[#self.buttons + 1] = {
149                                 name = name,
150                                 caption = caption,
151                                 image = image,
152                                 tooltip = tooltip
153                         }
154                         if self.orientation == "horizontal" then
155                                 if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
156                                         > self.size.x ) then
157
158                                         self.btn_initial_offset = self.btn_size * 0.05 + 0.5
159                                         self.have_move_buttons = true
160                                 end
161                         else
162                                 if ((self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
163                                         > self.size.y ) then
164
165                                         self.btn_initial_offset = self.btn_size * 0.05 + 0.5
166                                         self.have_move_buttons = true
167                                 end
168                         end
169                 end,
170
171         set_bgparams = function(self, bgcolor)
172                         if (type(bgcolor) == "string") then
173                                 self.bgcolor = bgcolor
174                         end
175                 end,
176 }
177
178 buttonbar_metatable.__index = buttonbar_metatable
179
180 function buttonbar_create(name, cbf_buttonhandler, pos, orientation, size)
181         assert(name ~= nil)
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")
186
187         local self = {}
188         self.name = name
189         self.type = "addon"
190         self.bgcolor = "#000000"
191         self.pos = pos
192         self.size = size
193         self.orientation = orientation
194         self.startbutton = 1
195         self.have_move_buttons = false
196         self.hidden = false
197
198         if self.orientation == "horizontal" then
199                         self.btn_size = self.size.y
200         else
201                         self.btn_size = self.size.x
202         end
203
204         if (self.btn_initial_offset == nil) then
205                 self.btn_initial_offset = self.btn_size * 0.05
206         end
207
208         self.userbuttonhandler = cbf_buttonhandler
209         self.buttons = {}
210
211         setmetatable(self,buttonbar_metatable)
212
213         ui.add(self)
214         return self
215 end