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
19local 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
110end
111
112local 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
131end
132
133local 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			table.insert(self.buttons,{ name=name, caption=caption, image=image, tooltip=tooltip})
149			if self.orientation == "horizontal" then
150				if ( (self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
151					> self.size.x ) then
152
153					self.btn_initial_offset = self.btn_size * 0.05 + 0.5
154					self.have_move_buttons = true
155				end
156			else
157				if ((self.btn_size * #self.buttons) + (self.btn_size * 0.05 *2)
158					> self.size.y ) then
159
160					self.btn_initial_offset = self.btn_size * 0.05 + 0.5
161					self.have_move_buttons = true
162				end
163			end
164		end,
165
166	set_bgparams = function(self, bgcolor)
167			if (type(bgcolor) == "string") then
168				self.bgcolor = bgcolor
169			end
170		end,
171}
172
173buttonbar_metatable.__index = buttonbar_metatable
174
175function buttonbar_create(name, cbf_buttonhandler, pos, orientation, size)
176	assert(name ~= nil)
177	assert(cbf_buttonhandler ~= nil)
178	assert(orientation == "vertical" or orientation == "horizontal")
179	assert(pos ~= nil and type(pos) == "table")
180	assert(size ~= nil and type(size) == "table")
181
182	local self = {}
183	self.name = name
184	self.type = "addon"
185	self.bgcolor = "#000000"
186	self.pos = pos
187	self.size = size
188	self.orientation = orientation
189	self.startbutton = 1
190	self.have_move_buttons = false
191	self.hidden = false
192
193	if self.orientation == "horizontal" then
194			self.btn_size = self.size.y
195	else
196			self.btn_size = self.size.x
197	end
198
199	if (self.btn_initial_offset == nil) then
200		self.btn_initial_offset = self.btn_size * 0.05
201	end
202
203	self.userbuttonhandler = cbf_buttonhandler
204	self.buttons = {}
205
206	setmetatable(self,buttonbar_metatable)
207
208	ui.add(self)
209	return self
210end
211