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 formspec = "size[12,3.2]" ..
64 "textarea[1,1;10,2;;ERROR: " ..
65 core.formspec_escape(gamedata.errormessage) ..
67 "button[4.5,2.5;3,0.5;btn_error_confirm;" .. fgettext("Ok") .. "]"
69 local active_toplevel_ui_elements = 0
70 for key,value in pairs(ui.childlist) do
71 if (value.type == "toplevel") then
72 local retval = value:get_formspec()
74 if retval ~= nil and retval ~= "" then
75 active_toplevel_ui_elements = active_toplevel_ui_elements +1
76 formspec = formspec .. retval
81 -- no need to show addons if there ain't a toplevel element
82 if (active_toplevel_ui_elements > 0) then
83 for key,value in pairs(ui.childlist) do
84 if (value.type == "addon") then
85 local retval = value:get_formspec()
87 if retval ~= nil and retval ~= "" then
88 formspec = formspec .. retval
94 if (active_toplevel_ui_elements > 1) then
95 print("WARNING: ui manager detected more then one active ui element, self most likely isn't intended")
98 if (active_toplevel_ui_elements == 0) then
99 print("WARNING: not a single toplevel ui element active switching to default")
100 ui.childlist[ui.default]:show()
101 formspec = ui.childlist[ui.default]:get_formspec()
104 core.update_formspec(formspec)
107 --------------------------------------------------------------------------------
108 function ui.handle_buttons(fields)
110 if fields["btn_error_confirm"] then
111 gamedata.errormessage = nil
116 for key,value in pairs(ui.childlist) do
118 local retval = value:handle_buttons(fields)
128 --------------------------------------------------------------------------------
129 function ui.handle_events(event)
131 for key,value in pairs(ui.childlist) do
133 if value.handle_events ~= nil then
134 local retval = value:handle_events(event)
137 print("event handled by: " .. key)
144 --------------------------------------------------------------------------------
145 --------------------------------------------------------------------------------
146 -- initialize callbacks
147 --------------------------------------------------------------------------------
148 --------------------------------------------------------------------------------
149 core.button_handler = function(fields)
150 if fields["btn_error_confirm"] then
151 gamedata.errormessage = nil
156 if ui.handle_buttons(fields) then
161 --------------------------------------------------------------------------------
162 core.event_handler = function(event)
163 if ui.handle_events(event) then
168 if event == "Refresh" then