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 --------------------------------------------------------------------------------
57 local function wordwrap_quickhack(str)
59 local ar = str:split("\n")
62 -- Hack to add word wrapping.
63 -- TODO: Add engine support for wrapping in formspecs
68 res = res .. core.formspec_escape(string.sub(text, 1, 79))
69 text = string.sub(text, 80, #text)
74 res = res .. core.formspec_escape(text)
79 --------------------------------------------------------------------------------
84 if gamedata ~= nil and gamedata.reconnect_requested then
85 formspec = wordwrap_quickhack(gamedata.errormessage or "")
86 formspec = "size[12,5]" ..
87 "label[0.5,0;" .. fgettext("The server has requested a reconnect:") ..
88 "]textlist[0.2,0.8;11.5,3.5;;" .. formspec ..
89 "]button[6,4.6;3,0.5;btn_reconnect_no;" .. fgettext("Main menu") .. "]" ..
90 "button[3,4.6;3,0.5;btn_reconnect_yes;" .. fgettext("Reconnect") .. "]"
91 elseif gamedata ~= nil and gamedata.errormessage ~= nil then
92 formspec = wordwrap_quickhack(gamedata.errormessage)
94 if string.find(gamedata.errormessage, "ModError") then
95 error_title = fgettext("An error occured in a Lua script, such as a mod:")
97 error_title = fgettext("An error occured:")
99 formspec = "size[12,5]" ..
100 "label[0.5,0;" .. error_title ..
101 "]textlist[0.2,0.8;11.5,3.5;;" .. formspec ..
102 "]button[4.5,4.6;3,0.5;btn_error_confirm;" .. fgettext("Ok") .. "]"
104 local active_toplevel_ui_elements = 0
105 for key,value in pairs(ui.childlist) do
106 if (value.type == "toplevel") then
107 local retval = value:get_formspec()
109 if retval ~= nil and retval ~= "" then
110 active_toplevel_ui_elements = active_toplevel_ui_elements +1
111 formspec = formspec .. retval
116 -- no need to show addons if there ain't a toplevel element
117 if (active_toplevel_ui_elements > 0) then
118 for key,value in pairs(ui.childlist) do
119 if (value.type == "addon") then
120 local retval = value:get_formspec()
122 if retval ~= nil and retval ~= "" then
123 formspec = formspec .. retval
129 if (active_toplevel_ui_elements > 1) then
130 print("WARNING: ui manager detected more then one active ui element, self most likely isn't intended")
133 if (active_toplevel_ui_elements == 0) then
134 print("WARNING: not a single toplevel ui element active switching to default")
135 ui.childlist[ui.default]:show()
136 formspec = ui.childlist[ui.default]:get_formspec()
139 core.update_formspec(formspec)
142 --------------------------------------------------------------------------------
143 function ui.handle_buttons(fields)
144 for key,value in pairs(ui.childlist) do
146 local retval = value:handle_buttons(fields)
156 --------------------------------------------------------------------------------
157 function ui.handle_events(event)
159 for key,value in pairs(ui.childlist) do
161 if value.handle_events ~= nil then
162 local retval = value:handle_events(event)
171 --------------------------------------------------------------------------------
172 --------------------------------------------------------------------------------
173 -- initialize callbacks
174 --------------------------------------------------------------------------------
175 --------------------------------------------------------------------------------
176 core.button_handler = function(fields)
177 if fields["btn_reconnect_yes"] then
178 gamedata.reconnect_requested = false
179 gamedata.errormessage = nil
180 gamedata.do_reconnect = true
183 elseif fields["btn_reconnect_no"] or fields["btn_error_confirm"] then
184 gamedata.errormessage = nil
185 gamedata.reconnect_requested = false
190 if ui.handle_buttons(fields) then
195 --------------------------------------------------------------------------------
196 core.event_handler = function(event)
197 if ui.handle_events(event) then
202 if event == "Refresh" then