2 --Copyright (C) 2014 sapier
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.
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.
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.
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 --------------------------------------------------------------------------------
62 if gamedata ~= nil and gamedata.errormessage ~= nil then
63 local ar = gamedata.errormessage:split("\n")
66 -- Hack to add word wrapping.
67 -- TODO: Add engine support for wrapping in formspecs
69 if formspec ~= "" then
70 formspec = formspec .. ","
72 formspec = formspec .. core.formspec_escape(string.sub(text, 1, 79))
73 text = string.sub(text, 80, #text)
75 if formspec ~= "" then
76 formspec = formspec .. ","
78 formspec = formspec .. core.formspec_escape(text)
81 if string.find(gamedata.errormessage, "ModError") then
82 error_title = fgettext("An error occured in a Lua script, such as a mod:")
84 error_title = fgettext("An error occured:")
86 formspec = "size[12,5]" ..
87 "label[0.5,0;" .. error_title ..
88 "]textlist[0.2,0.8;11.5,3.5;;" .. formspec ..
89 "]button[4.5,4.6;3,0.5;btn_error_confirm;" .. fgettext("Ok") .. "]"
91 local active_toplevel_ui_elements = 0
92 for key,value in pairs(ui.childlist) do
93 if (value.type == "toplevel") then
94 local retval = value:get_formspec()
96 if retval ~= nil and retval ~= "" then
97 active_toplevel_ui_elements = active_toplevel_ui_elements +1
98 formspec = formspec .. retval
103 -- no need to show addons if there ain't a toplevel element
104 if (active_toplevel_ui_elements > 0) then
105 for key,value in pairs(ui.childlist) do
106 if (value.type == "addon") then
107 local retval = value:get_formspec()
109 if retval ~= nil and retval ~= "" then
110 formspec = formspec .. retval
116 if (active_toplevel_ui_elements > 1) then
117 print("WARNING: ui manager detected more then one active ui element, self most likely isn't intended")
120 if (active_toplevel_ui_elements == 0) then
121 print("WARNING: not a single toplevel ui element active switching to default")
122 ui.childlist[ui.default]:show()
123 formspec = ui.childlist[ui.default]:get_formspec()
126 core.update_formspec(formspec)
129 --------------------------------------------------------------------------------
130 function ui.handle_buttons(fields)
132 if fields["btn_error_confirm"] then
133 gamedata.errormessage = nil
138 for key,value in pairs(ui.childlist) do
140 local retval = value:handle_buttons(fields)
150 --------------------------------------------------------------------------------
151 function ui.handle_events(event)
153 for key,value in pairs(ui.childlist) do
155 if value.handle_events ~= nil then
156 local retval = value:handle_events(event)
165 --------------------------------------------------------------------------------
166 --------------------------------------------------------------------------------
167 -- initialize callbacks
168 --------------------------------------------------------------------------------
169 --------------------------------------------------------------------------------
170 core.button_handler = function(fields)
171 if fields["btn_error_confirm"] then
172 gamedata.errormessage = nil
177 if ui.handle_buttons(fields) then
182 --------------------------------------------------------------------------------
183 core.event_handler = function(event)
184 if ui.handle_events(event) then
189 if event == "Refresh" then