Skip to content

Commit c398456

Browse files
sapiersapier
sapier
authored and
sapier
committedMay 16, 2014
Add formspec toolkit and refactor mainmenu to use it
Fix crash on using cursor keys in client menu without selected server Add support for non fixed size tabviews
1 parent 34d8726 commit c398456

27 files changed

+3514
-2496
lines changed
 

‎builtin/mainmenu/filterlist.lua ‎builtin/common/filterlist.lua

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

1818
--------------------------------------------------------------------------------
19+
-- TODO improve doc --
20+
-- TODO code cleanup --
1921
-- Generic implementation of a filter/sortable list --
2022
-- Usage: --
2123
-- Filterlist needs to be initialized on creation. To achieve this you need to --
@@ -62,6 +64,20 @@ function filterlist.create(raw_fct,compare_fct,uid_match_fct,filter_fct,fetch_pa
6264
this.m_processed_list = nil
6365
this.m_raw_list = this.m_raw_list_fct(this.m_fetch_param)
6466

67+
this.add_sort_mechanism = filterlist.add_sort_mechanism
68+
this.set_filtercriteria = filterlist.set_filtercriteria
69+
this.get_filtercriteria = filterlist.get_filtercriteria
70+
this.set_sortmode = filterlist.set_sortmode
71+
this.get_list = filterlist.get_list
72+
this.get_raw_list = filterlist.get_raw_list
73+
this.get_raw_element = filterlist.get_raw_element
74+
this.get_raw_index = filterlist.get_raw_index
75+
this.get_current_index = filterlist.get_current_index
76+
this.size = filterlist.size
77+
this.uid_exists_raw = filterlist.uid_exists_raw
78+
this.raw_index_by_uid = filterlist.raw_index_by_uid
79+
this.refresh = filterlist.refresh
80+
6581
filterlist.process(this)
6682

6783
return this

‎builtin/fstk/buttonbar.lua

+209
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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

‎builtin/fstk/dialog.lua

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
local function dialog_event_handler(self,event)
19+
if self.user_eventhandler == nil or
20+
self.user_eventhandler(event) == false then
21+
22+
--close dialog on esc
23+
if event == "MenuQuit" then
24+
self:delete()
25+
return true
26+
end
27+
end
28+
end
29+
30+
local dialog_metatable = {
31+
eventhandler = dialog_event_handler,
32+
get_formspec = function(self)
33+
if not self.hidden then return self.formspec(self.data) end
34+
end,
35+
handle_buttons = function(self,fields)
36+
if not self.hidden then return self.buttonhandler(self,fields) end
37+
end,
38+
handle_events = function(self,event)
39+
if not self.hidden then return self.eventhandler(self,event) end
40+
end,
41+
hide = function(self) self.hidden = true end,
42+
show = function(self) self.hidden = false end,
43+
delete = function(self)
44+
if self.parent ~= nil then
45+
self.parent:show()
46+
end
47+
ui.delete(self)
48+
end,
49+
set_parent = function(self,parent) self.parent = parent end
50+
}
51+
dialog_metatable.__index = dialog_metatable
52+
53+
function dialog_create(name,get_formspec,buttonhandler,eventhandler)
54+
local self = {}
55+
56+
self.name = name
57+
self.type = "toplevel"
58+
self.hidden = true
59+
self.data = {}
60+
61+
self.formspec = get_formspec
62+
self.buttonhandler = buttonhandler
63+
self.user_eventhandler = eventhandler
64+
65+
setmetatable(self,dialog_metatable)
66+
67+
ui.add(self)
68+
return self
69+
end

0 commit comments

Comments
 (0)
Please sign in to comment.