]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/fstk/ui.lua
Optional reconnect functionality
[dragonfireclient.git] / builtin / fstk / ui.lua
1 --Minetest
2 --Copyright (C) 2014 sapier
3 --
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.
8 --
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.
13 --
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.
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 local function wordwrap_quickhack(str)
58         local res = ""
59         local ar = str:split("\n")
60         for i = 1, #ar do
61                 local text = ar[i]
62                 -- Hack to add word wrapping.
63                 -- TODO: Add engine support for wrapping in formspecs
64                 while #text > 80 do
65                         if res ~= "" then
66                                 res = res .. ","
67                         end
68                         res = res .. core.formspec_escape(string.sub(text, 1, 79))
69                         text = string.sub(text, 80, #text)
70                 end
71                 if res ~= "" then
72                         res = res .. ","
73                 end
74                 res = res .. core.formspec_escape(text)
75         end
76         return res
77 end
78
79 --------------------------------------------------------------------------------
80 function ui.update()
81         local formspec = ""
82
83         -- handle errors
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)
93                 local error_title
94                 if string.find(gamedata.errormessage, "ModError") then
95                         error_title = fgettext("An error occured in a Lua script, such as a mod:")
96                 else
97                         error_title = fgettext("An error occured:")
98                 end
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") .. "]"
103         else
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()
108
109                                 if retval ~= nil and retval ~= "" then
110                                         active_toplevel_ui_elements = active_toplevel_ui_elements +1
111                                         formspec = formspec .. retval
112                                 end
113                         end
114                 end
115
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()
121
122                                         if retval ~= nil and retval ~= "" then
123                                                 formspec = formspec .. retval
124                                         end
125                                 end
126                         end
127                 end
128
129                 if (active_toplevel_ui_elements > 1) then
130                         print("WARNING: ui manager detected more then one active ui element, self most likely isn't intended")
131                 end
132
133                 if (active_toplevel_ui_elements == 0) then
134                         print("WARNING: not a single toplevel ui element active switching to default")
135                         ui.childlist[ui.default]:show()
136                         formspec = ui.childlist[ui.default]:get_formspec()
137                 end
138         end
139         core.update_formspec(formspec)
140 end
141
142 --------------------------------------------------------------------------------
143 function ui.handle_buttons(fields)
144         for key,value in pairs(ui.childlist) do
145
146                 local retval = value:handle_buttons(fields)
147
148                 if retval then
149                         ui.update()
150                         return
151                 end
152         end
153 end
154
155
156 --------------------------------------------------------------------------------
157 function ui.handle_events(event)
158
159         for key,value in pairs(ui.childlist) do
160
161                 if value.handle_events ~= nil then
162                         local retval = value:handle_events(event)
163
164                         if retval then
165                                 return retval
166                         end
167                 end
168         end
169 end
170
171 --------------------------------------------------------------------------------
172 --------------------------------------------------------------------------------
173 -- initialize callbacks
174 --------------------------------------------------------------------------------
175 --------------------------------------------------------------------------------
176 core.button_handler = function(fields)
177         if fields["btn_reconnect_yes"] then
178                 gamedata.reconnect_requested = false
179                 gamedata.errormessage = nil
180                 gamedata.do_reconnect = true
181                 core.start()
182                 return
183         elseif fields["btn_reconnect_no"] or fields["btn_error_confirm"] then
184                 gamedata.errormessage = nil
185                 gamedata.reconnect_requested = false
186                 ui.update()
187                 return
188         end
189
190         if ui.handle_buttons(fields) then
191                 ui.update()
192         end
193 end
194
195 --------------------------------------------------------------------------------
196 core.event_handler = function(event)
197         if ui.handle_events(event) then
198                 ui.update()
199                 return
200         end
201
202         if event == "Refresh" then
203                 ui.update()
204                 return
205         end
206 end