]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/fstk/ui.lua
478a78ad54c608b0379bdd16c59d02ff9dd71ac8
[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 --------------------------------------------------------------------------------
58 function ui.update()
59         local formspec = ""
60
61         -- handle errors
62         if gamedata ~= nil and gamedata.errormessage ~= nil then
63                 local ar = gamedata.errormessage:split("\n")
64                 for i = 1, #ar do
65                         local text = ar[i]
66                         -- Hack to add word wrapping.
67                         -- TODO: Add engine support for wrapping in formspecs
68                         while #text > 80 do
69                                 if formspec ~= "" then
70                                         formspec = formspec .. ","
71                                 end
72                                 formspec = formspec .. core.formspec_escape(string.sub(text, 1, 79))
73                                 text = string.sub(text, 80, #text)
74                         end
75                         if formspec ~= "" then
76                                 formspec = formspec .. ","
77                         end
78                         formspec = formspec .. core.formspec_escape(text)
79                 end
80                 local error_title
81                 if string.find(gamedata.errormessage, "ModError") then
82                         error_title = fgettext("An error occured in a Lua script, such as a mod:")
83                 else
84                         error_title = fgettext("An error occured:")
85                 end
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") .. "]"
90         else
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()
95
96                                 if retval ~= nil and retval ~= "" then
97                                         active_toplevel_ui_elements = active_toplevel_ui_elements +1
98                                         formspec = formspec .. retval
99                                 end
100                         end
101                 end
102
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()
108
109                                         if retval ~= nil and retval ~= "" then
110                                                 formspec = formspec .. retval
111                                         end
112                                 end
113                         end
114                 end
115
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")
118                 end
119
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()
124                 end
125         end
126         core.update_formspec(formspec)
127 end
128
129 --------------------------------------------------------------------------------
130 function ui.handle_buttons(fields)
131
132         if fields["btn_error_confirm"] then
133                 gamedata.errormessage = nil
134                 update_menu()
135                 return
136         end
137
138         for key,value in pairs(ui.childlist) do
139
140                 local retval = value:handle_buttons(fields)
141
142                 if retval then
143                         ui.update()
144                         return
145                 end
146         end
147 end
148
149
150 --------------------------------------------------------------------------------
151 function ui.handle_events(event)
152
153         for key,value in pairs(ui.childlist) do
154
155                 if value.handle_events ~= nil then
156                         local retval = value:handle_events(event)
157
158                         if retval then
159                                 return retval
160                         end
161                 end
162         end
163 end
164
165 --------------------------------------------------------------------------------
166 --------------------------------------------------------------------------------
167 -- initialize callbacks
168 --------------------------------------------------------------------------------
169 --------------------------------------------------------------------------------
170 core.button_handler = function(fields)
171         if fields["btn_error_confirm"] then
172                 gamedata.errormessage = nil
173                 ui.update()
174                 return
175         end
176
177         if ui.handle_buttons(fields) then
178                 ui.update()
179         end
180 end
181
182 --------------------------------------------------------------------------------
183 core.event_handler = function(event)
184         if ui.handle_events(event) then
185                 ui.update()
186                 return
187         end
188
189         if event == "Refresh" then
190                 ui.update()
191                 return
192         end
193 end