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 core.log("warning", "more than one active ui "..
131 "element, self most likely isn't intended")
134 if (active_toplevel_ui_elements == 0) then
135 core.log("warning", "no toplevel ui element "..
136 "active; switching to default")
137 ui.childlist[ui.default]:show()
138 formspec = ui.childlist[ui.default]:get_formspec()
141 core.update_formspec(formspec)
144 --------------------------------------------------------------------------------
145 function ui.handle_buttons(fields)
146 for key,value in pairs(ui.childlist) do
148 local retval = value:handle_buttons(fields)
158 --------------------------------------------------------------------------------
159 function ui.handle_events(event)
161 for key,value in pairs(ui.childlist) do
163 if value.handle_events ~= nil then
164 local retval = value:handle_events(event)
173 --------------------------------------------------------------------------------
174 --------------------------------------------------------------------------------
175 -- initialize callbacks
176 --------------------------------------------------------------------------------
177 --------------------------------------------------------------------------------
178 core.button_handler = function(fields)
179 if fields["btn_reconnect_yes"] then
180 gamedata.reconnect_requested = false
181 gamedata.errormessage = nil
182 gamedata.do_reconnect = true
185 elseif fields["btn_reconnect_no"] or fields["btn_error_confirm"] then
186 gamedata.errormessage = nil
187 gamedata.reconnect_requested = false
192 if ui.handle_buttons(fields) then
197 --------------------------------------------------------------------------------
198 core.event_handler = function(event)
199 if ui.handle_events(event) then
204 if event == "Refresh" then