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