]> git.lizzy.rs Git - minetest.git/blob - builtin/fstk/ui.lua
Add flags and lacunarity as new noise parameters
[minetest.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                 formspec = "size[12,3.2]" ..
64                         "textarea[1,1;10,2;;ERROR: " ..
65                         core.formspec_escape(gamedata.errormessage) ..
66                         ";]"..
67                         "button[4.5,2.5;3,0.5;btn_error_confirm;" .. fgettext("Ok") .. "]"
68         else
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()
73
74                                 if retval ~= nil and retval ~= "" then
75                                         active_toplevel_ui_elements = active_toplevel_ui_elements +1
76                                         formspec = formspec .. retval
77                                 end
78                         end
79                 end
80                 
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()
86
87                                         if retval ~= nil and retval ~= "" then
88                                                 formspec = formspec .. retval
89                                         end
90                                 end
91                         end
92                 end
93
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")
96                 end
97
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()
102                 end
103         end
104         core.update_formspec(formspec)
105 end
106
107 --------------------------------------------------------------------------------
108 function ui.handle_buttons(fields)
109
110         if fields["btn_error_confirm"] then
111                 gamedata.errormessage = nil
112                 update_menu()
113                 return
114         end
115
116         for key,value in pairs(ui.childlist) do
117
118                 local retval = value:handle_buttons(fields)
119
120                 if retval then
121                         ui.update()
122                         return
123                 end
124         end
125 end
126
127
128 --------------------------------------------------------------------------------
129 function ui.handle_events(event)
130         
131         for key,value in pairs(ui.childlist) do
132
133                 if value.handle_events ~= nil then
134                         local retval = value:handle_events(event)
135
136                         if retval then
137                                 return retval
138                         end
139                 end
140         end
141 end
142
143 --------------------------------------------------------------------------------
144 --------------------------------------------------------------------------------
145 -- initialize callbacks
146 --------------------------------------------------------------------------------
147 --------------------------------------------------------------------------------
148 core.button_handler = function(fields)
149         if fields["btn_error_confirm"] then
150                 gamedata.errormessage = nil
151                 ui.update()
152                 return
153         end
154
155         if ui.handle_buttons(fields) then
156                 ui.update()
157         end
158 end
159
160 --------------------------------------------------------------------------------
161 core.event_handler = function(event)
162         if ui.handle_events(event) then
163                 ui.update()
164                 return
165         end
166
167         if event == "Refresh" then
168                 ui.update()
169                 return
170         end
171 end