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.
21 -- Whether fstk is currently showing its own formspec instead of active ui elements.
24 --------------------------------------------------------------------------------
25 function ui.add(child)
27 ui.childlist[child.name] = child
32 --------------------------------------------------------------------------------
33 function ui.delete(child)
35 if ui.childlist[child.name] == nil then
39 ui.childlist[child.name] = nil
43 --------------------------------------------------------------------------------
44 function ui.set_default(name)
48 --------------------------------------------------------------------------------
49 function ui.find_by_name(name)
50 return ui.childlist[name]
53 --------------------------------------------------------------------------------
54 --------------------------------------------------------------------------------
55 -- Internal functions not to be called from user
56 --------------------------------------------------------------------------------
57 --------------------------------------------------------------------------------
64 if gamedata ~= nil and gamedata.reconnect_requested then
65 local error_message = core.formspec_escape(
66 gamedata.errormessage or fgettext("<none available>"))
69 "real_coordinates[true]",
70 "set_focus[btn_reconnect_yes;true]",
71 "box[0.5,1.2;13,5;#000]",
72 ("textarea[0.5,1.2;13,5;;%s;%s]"):format(
73 fgettext("The server has requested a reconnect:"), error_message),
74 "button[2,6.6;4,1;btn_reconnect_yes;" .. fgettext("Reconnect") .. "]",
75 "button[8,6.6;4,1;btn_reconnect_no;" .. fgettext("Main menu") .. "]"
78 elseif gamedata ~= nil and gamedata.errormessage ~= nil then
79 local error_message = core.formspec_escape(gamedata.errormessage)
82 if string.find(gamedata.errormessage, "ModError") then
83 error_title = fgettext("An error occurred in a Lua script:")
85 error_title = fgettext("An error occurred:")
89 "real_coordinates[true]",
90 "set_focus[btn_error_confirm;true]",
91 "box[0.5,1.2;13,5;#000]",
92 ("textarea[0.5,1.2;13,5;;%s;%s]"):format(
93 error_title, error_message),
94 "button[5,6.6;4,1;btn_error_confirm;" .. fgettext("OK") .. "]"
98 local active_toplevel_ui_elements = 0
99 for key,value in pairs(ui.childlist) do
100 if (value.type == "toplevel") then
101 local retval = value:get_formspec()
103 if retval ~= nil and retval ~= "" then
104 active_toplevel_ui_elements = active_toplevel_ui_elements + 1
105 table.insert(formspec, retval)
110 -- no need to show addons if there ain't a toplevel element
111 if (active_toplevel_ui_elements > 0) then
112 for key,value in pairs(ui.childlist) do
113 if (value.type == "addon") then
114 local retval = value:get_formspec()
116 if retval ~= nil and retval ~= "" then
117 table.insert(formspec, retval)
123 if (active_toplevel_ui_elements > 1) then
124 core.log("warning", "more than one active ui "..
125 "element, self most likely isn't intended")
128 if (active_toplevel_ui_elements == 0) then
129 core.log("warning", "no toplevel ui element "..
130 "active; switching to default")
131 ui.childlist[ui.default]:show()
132 formspec = {ui.childlist[ui.default]:get_formspec()}
135 core.update_formspec(table.concat(formspec))
138 --------------------------------------------------------------------------------
139 function ui.handle_buttons(fields)
140 for key,value in pairs(ui.childlist) do
142 local retval = value:handle_buttons(fields)
152 --------------------------------------------------------------------------------
153 function ui.handle_events(event)
155 for key,value in pairs(ui.childlist) do
157 if value.handle_events ~= nil then
158 local retval = value:handle_events(event)
167 --------------------------------------------------------------------------------
168 --------------------------------------------------------------------------------
169 -- initialize callbacks
170 --------------------------------------------------------------------------------
171 --------------------------------------------------------------------------------
172 core.button_handler = function(fields)
173 if fields["btn_reconnect_yes"] then
174 gamedata.reconnect_requested = false
175 gamedata.errormessage = nil
176 gamedata.do_reconnect = true
179 elseif fields["btn_reconnect_no"] or fields["btn_error_confirm"] then
180 gamedata.errormessage = nil
181 gamedata.reconnect_requested = false
186 if ui.handle_buttons(fields) then
191 --------------------------------------------------------------------------------
192 core.event_handler = function(event)
193 -- Handle error messages
194 if ui.overridden then
195 if event == "MenuQuit" then
196 gamedata.errormessage = nil
197 gamedata.reconnect_requested = false
203 if ui.handle_events(event) then
208 if event == "Refresh" then