]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/fstk/ui.lua
976659ed364bda029f57496eae169727225d57a2
[dragonfireclient.git] / builtin / fstk / ui.lua
1 --Minetest
2 --Copyright (C) 2014 sapier
3 --
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.
8 --
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.
13 --
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.
17
18 ui = {}
19 ui.childlist = {}
20 ui.default = nil
21 -- Whether fstk is currently showing its own formspec instead of active ui elements.
22 ui.overridden = false
23
24 --------------------------------------------------------------------------------
25 function ui.add(child)
26         --TODO check child
27         ui.childlist[child.name] = child
28
29         return child.name
30 end
31
32 --------------------------------------------------------------------------------
33 function ui.delete(child)
34
35         if ui.childlist[child.name] == nil then
36                 return false
37         end
38
39         ui.childlist[child.name] = nil
40         return true
41 end
42
43 --------------------------------------------------------------------------------
44 function ui.set_default(name)
45         ui.default = name
46 end
47
48 --------------------------------------------------------------------------------
49 function ui.find_by_name(name)
50         return ui.childlist[name]
51 end
52
53 --------------------------------------------------------------------------------
54 --------------------------------------------------------------------------------
55 -- Internal functions not to be called from user
56 --------------------------------------------------------------------------------
57 --------------------------------------------------------------------------------
58
59 function ui.update()
60         ui.overridden = false
61         local formspec = {}
62
63         -- handle errors
64         if gamedata ~= nil and gamedata.reconnect_requested then
65                 local error_message = core.formspec_escape(
66                                 gamedata.errormessage or "<none available>")
67                 formspec = {
68                         "size[14,8]",
69                         "real_coordinates[true]",
70                         "set_focus[btn_reconnect_yes;true]",
71                         "box[0.5,1.2;13,5;#000]",
72                         ("textarea[0.5,1.2;13,5;;%s;%s]"):format(
73                                 fgettext("The server has requested a reconnect:"), error_message),
74                         "button[2,6.6;4,1;btn_reconnect_yes;" .. fgettext("Reconnect") .. "]",
75                         "button[8,6.6;4,1;btn_reconnect_no;" .. fgettext("Main menu") .. "]"
76                 }
77                 ui.overridden = true
78         elseif gamedata ~= nil and gamedata.errormessage ~= nil then
79                 local error_message = core.formspec_escape(gamedata.errormessage)
80
81                 local error_title
82                 if string.find(gamedata.errormessage, "ModError") then
83                         error_title = fgettext("An error occurred in a Lua script:")
84                 else
85                         error_title = fgettext("An error occurred:")
86                 end
87                 formspec = {
88                         "size[14,8]",
89                         "real_coordinates[true]",
90                         "set_focus[btn_error_confirm;true]",
91                         "box[0.5,1.2;13,5;#000]",
92                         ("textarea[0.5,1.2;13,5;;%s;%s]"):format(
93                                 error_title, error_message),
94                         "button[5,6.6;4,1;btn_error_confirm;" .. fgettext("OK") .. "]"
95                 }
96                 ui.overridden = true
97         else
98                 local active_toplevel_ui_elements = 0
99                 for key,value in pairs(ui.childlist) do
100                         if (value.type == "toplevel") then
101                                 local retval = value:get_formspec()
102
103                                 if retval ~= nil and retval ~= "" then
104                                         active_toplevel_ui_elements = active_toplevel_ui_elements + 1
105                                         table.insert(formspec, retval)
106                                 end
107                         end
108                 end
109
110                 -- no need to show addons if there ain't a toplevel element
111                 if (active_toplevel_ui_elements > 0) then
112                         for key,value in pairs(ui.childlist) do
113                                 if (value.type == "addon") then
114                                         local retval = value:get_formspec()
115
116                                         if retval ~= nil and retval ~= "" then
117                                                 table.insert(formspec, retval)
118                                         end
119                                 end
120                         end
121                 end
122
123                 if (active_toplevel_ui_elements > 1) then
124                         core.log("warning", "more than one active ui "..
125                                 "element, self most likely isn't intended")
126                 end
127
128                 if (active_toplevel_ui_elements == 0) then
129                         core.log("warning", "no toplevel ui element "..
130                                         "active; switching to default")
131                         ui.childlist[ui.default]:show()
132                         formspec = {ui.childlist[ui.default]:get_formspec()}
133                 end
134         end
135         core.update_formspec(table.concat(formspec))
136 end
137
138 --------------------------------------------------------------------------------
139 function ui.handle_buttons(fields)
140         for key,value in pairs(ui.childlist) do
141
142                 local retval = value:handle_buttons(fields)
143
144                 if retval then
145                         ui.update()
146                         return
147                 end
148         end
149 end
150
151
152 --------------------------------------------------------------------------------
153 function ui.handle_events(event)
154
155         for key,value in pairs(ui.childlist) do
156
157                 if value.handle_events ~= nil then
158                         local retval = value:handle_events(event)
159
160                         if retval then
161                                 return retval
162                         end
163                 end
164         end
165 end
166
167 --------------------------------------------------------------------------------
168 --------------------------------------------------------------------------------
169 -- initialize callbacks
170 --------------------------------------------------------------------------------
171 --------------------------------------------------------------------------------
172 core.button_handler = function(fields)
173         if fields["btn_reconnect_yes"] then
174                 gamedata.reconnect_requested = false
175                 gamedata.errormessage = nil
176                 gamedata.do_reconnect = true
177                 core.start()
178                 return
179         elseif fields["btn_reconnect_no"] or fields["btn_error_confirm"] then
180                 gamedata.errormessage = nil
181                 gamedata.reconnect_requested = false
182                 ui.update()
183                 return
184         end
185
186         if ui.handle_buttons(fields) then
187                 ui.update()
188         end
189 end
190
191 --------------------------------------------------------------------------------
192 core.event_handler = function(event)
193         -- Handle error messages
194         if ui.overridden then
195                 if event == "MenuQuit" then
196                         gamedata.errormessage = nil
197                         gamedata.reconnect_requested = false
198                         ui.update()
199                 end
200                 return
201         end
202
203         if ui.handle_events(event) then
204                 ui.update()
205                 return
206         end
207
208         if event == "Refresh" then
209                 ui.update()
210                 return
211         end
212 end