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.
22 --------------------------------------------------------------------------------
23 function ui.add(child)
25 ui.childlist[child.name] = child
30 --------------------------------------------------------------------------------
31 function ui.delete(child)
33 if ui.childlist[child.name] == nil then
37 ui.childlist[child.name] = nil
41 --------------------------------------------------------------------------------
42 function ui.set_default(name)
46 --------------------------------------------------------------------------------
47 function ui.find_by_name(name)
48 return ui.childlist[name]
51 --------------------------------------------------------------------------------
52 --------------------------------------------------------------------------------
53 -- Internal functions not to be called from user
54 --------------------------------------------------------------------------------
55 --------------------------------------------------------------------------------
61 if gamedata ~= nil and gamedata.reconnect_requested then
62 local error_message = core.formspec_escape(
63 gamedata.errormessage or "<none available>")
66 "real_coordinates[true]",
67 "set_focus[btn_reconnect_yes;true]",
68 "box[0.5,1.2;13,5;#000]",
69 ("textarea[0.5,1.2;13,5;;%s;%s]"):format(
70 fgettext("The server has requested a reconnect:"), error_message),
71 "button[2,6.6;4,1;btn_reconnect_yes;" .. fgettext("Reconnect") .. "]",
72 "button[8,6.6;4,1;btn_reconnect_no;" .. fgettext("Main menu") .. "]"
74 elseif gamedata ~= nil and gamedata.errormessage ~= nil then
75 local error_message = core.formspec_escape(gamedata.errormessage)
78 if string.find(gamedata.errormessage, "ModError") then
79 error_title = fgettext("An error occurred in a Lua script:")
81 error_title = fgettext("An error occurred:")
85 "real_coordinates[true]",
86 "set_focus[btn_error_confirm;true]",
87 "box[0.5,1.2;13,5;#000]",
88 ("textarea[0.5,1.2;13,5;;%s;%s]"):format(
89 error_title, error_message),
90 "button[5,6.6;4,1;btn_error_confirm;" .. fgettext("OK") .. "]"
93 local active_toplevel_ui_elements = 0
94 for key,value in pairs(ui.childlist) do
95 if (value.type == "toplevel") then
96 local retval = value:get_formspec()
98 if retval ~= nil and retval ~= "" then
99 active_toplevel_ui_elements = active_toplevel_ui_elements + 1
100 table.insert(formspec, retval)
105 -- no need to show addons if there ain't a toplevel element
106 if (active_toplevel_ui_elements > 0) then
107 for key,value in pairs(ui.childlist) do
108 if (value.type == "addon") then
109 local retval = value:get_formspec()
111 if retval ~= nil and retval ~= "" then
112 table.insert(formspec, retval)
118 if (active_toplevel_ui_elements > 1) then
119 core.log("warning", "more than one active ui "..
120 "element, self most likely isn't intended")
123 if (active_toplevel_ui_elements == 0) then
124 core.log("warning", "no toplevel ui element "..
125 "active; switching to default")
126 ui.childlist[ui.default]:show()
127 formspec = {ui.childlist[ui.default]:get_formspec()}
130 core.update_formspec(table.concat(formspec))
133 --------------------------------------------------------------------------------
134 function ui.handle_buttons(fields)
135 for key,value in pairs(ui.childlist) do
137 local retval = value:handle_buttons(fields)
147 --------------------------------------------------------------------------------
148 function ui.handle_events(event)
150 for key,value in pairs(ui.childlist) do
152 if value.handle_events ~= nil then
153 local retval = value:handle_events(event)
162 --------------------------------------------------------------------------------
163 --------------------------------------------------------------------------------
164 -- initialize callbacks
165 --------------------------------------------------------------------------------
166 --------------------------------------------------------------------------------
167 core.button_handler = function(fields)
168 if fields["btn_reconnect_yes"] then
169 gamedata.reconnect_requested = false
170 gamedata.errormessage = nil
171 gamedata.do_reconnect = true
174 elseif fields["btn_reconnect_no"] or fields["btn_error_confirm"] then
175 gamedata.errormessage = nil
176 gamedata.reconnect_requested = false
181 if ui.handle_buttons(fields) then
186 --------------------------------------------------------------------------------
187 core.event_handler = function(event)
188 if ui.handle_events(event) then
193 if event == "Refresh" then