]> git.lizzy.rs Git - minetest.git/blob - games/devtest/mods/testhud/init.lua
Add keybind to swap items between hands
[minetest.git] / games / devtest / mods / testhud / init.lua
1 local player_font_huds = {}
2
3 local font_states = {
4         {0, "Normal font"},
5         {1, "Bold font"},
6         {2, "Italic font"},
7         {3, "Bold and italic font"},
8         {4, "Monospace font"},
9         {5, "Bold and monospace font"},
10         {7, "ZOMG all the font styles"},
11 }
12
13
14 local font_default_def = {
15         hud_elem_type = "text",
16         position = {x = 0.5, y = 0.5},
17         scale = {x = 2, y = 2},
18         alignment = { x = 0, y = 0 },
19         number = 0xFFFFFF,
20 }
21
22 local function add_font_hud(player, state)
23         local def = table.copy(font_default_def)
24         local statetbl = font_states[state]
25         def.offset = {x = 0, y = 32 * state}
26         def.style = statetbl[1]
27         def.text = statetbl[2]
28         return player:hud_add(def)
29 end
30
31 local font_etime = 0
32 local font_state = 0
33
34 minetest.register_globalstep(function(dtime)
35         font_etime = font_etime + dtime
36         if font_etime < 1 then
37                 return
38         end
39         font_etime = 0
40         for _, player in ipairs(minetest.get_connected_players()) do
41                 local huds = player_font_huds[player:get_player_name()]
42                 if huds then
43                         for i, hud_id in ipairs(huds) do
44                                 local statetbl = font_states[(font_state + i) % #font_states + 1]
45                                 player:hud_change(hud_id, "style", statetbl[1])
46                                 player:hud_change(hud_id, "text", statetbl[2])
47                         end
48                 end
49         end
50         font_state = font_state + 1
51 end)
52
53 minetest.register_chatcommand("hudfonts", {
54         params = "[<HUD elements>]",
55         description = "Show/Hide some text on the HUD with various font options",
56         func = function(name, param)
57                 local player = minetest.get_player_by_name(name)
58                 local param = tonumber(param) or 0
59                 param = math.min(math.max(param, 1), #font_states)
60                 if player_font_huds[name] == nil then
61                         player_font_huds[name] = {}
62                         for i = 1, param do
63                                 table.insert(player_font_huds[name], add_font_hud(player, i))
64                         end
65                         minetest.chat_send_player(name, ("%d text HUD element(s) added."):format(param))
66                 else
67                         local huds = player_font_huds[name]
68                         if huds then
69                                 for _, hud_id in ipairs(huds) do
70                                         player:hud_remove(hud_id)
71                                 end
72                                 minetest.chat_send_player(name, "All text HUD elements removed.")
73                         end
74                         player_font_huds[name] = nil
75                 end
76                 return true
77         end,
78 })
79
80 -- Testing waypoint capabilities
81
82 local player_waypoints = {}
83 minetest.register_chatcommand("hudwaypoints", {
84         params = "[ add | add_change | remove ]",
85         description = "Create HUD waypoints at your position for testing (add: Add waypoints and change them after 0.5s (default). add_change: Add waypoints and change immediately. remove: Remove all waypoints)",
86         func = function(name, params)
87                 local player = minetest.get_player_by_name(name)
88                 if not player then
89                         return false, "No player."
90                 end
91                 if params == "remove" then
92                         if player_waypoints[name] then
93                                 for i=1, #player_waypoints[name] do
94                                         player:hud_remove(player_waypoints[name][i])
95                                 end
96                                 player_waypoints[name] = {}
97                         end
98                         return true, "All waypoint HUD elements removed."
99                 end
100                 if not (params == "add_change" or params == "add" or params == "") then
101                         -- Incorrect syntax
102                         return false
103                 end
104                 local regular = player:hud_add {
105                         hud_elem_type = "waypoint",
106                         name = "regular waypoint",
107                         text = "m",
108                         number = 0xFFFFFF,
109                         world_pos = vector.add(player:get_pos(), {x = 0, y = 1.5, z = 0})
110                 }
111                 local reduced_precision = player:hud_add {
112                         hud_elem_type = "waypoint",
113                         name = "imprecise waypoint",
114                         text = "m (0.1 steps, precision = 10)",
115                         precision = 10,
116                         number = 0xFFFFFF,
117                         world_pos = vector.add(player:get_pos(), {x = 0, y = 1, z = 0})
118                 }
119                 local hidden_distance = player:hud_add {
120                         hud_elem_type = "waypoint",
121                         name = "waypoint with hidden distance",
122                         text = "this text is hidden as well (precision = 0)",
123                         precision = 0,
124                         number = 0xFFFFFF,
125                         world_pos = vector.add(player:get_pos(), {x = 0, y = 0.5, z = 0})
126                 }
127                 local function change(chplayer)
128                         if not (chplayer and chplayer:is_player()) then
129                                 return
130                         end
131                         if regular then
132                                 chplayer:hud_change(regular, "world_pos", vector.add(player:get_pos(), {x = 0, y = 3, z = 0}))
133                                 chplayer:hud_change(regular, "number", 0xFF0000)
134                         end
135                         if reduced_precision then
136                                 chplayer:hud_change(reduced_precision, "precision", 2)
137                                 chplayer:hud_change(reduced_precision, "text", "m (0.5 steps, precision = 2)")
138                                 chplayer:hud_change(reduced_precision, "number", 0xFFFF00)
139                         end
140                         if hidden_distance then
141                                 chplayer:hud_change(hidden_distance, "number", 0x0000FF)
142                         end
143                         minetest.chat_send_player(chplayer:get_player_name(), "Waypoints changed.")
144                 end
145                 if params == "add_change" then
146                         -- change immediate
147                         change(player)
148                 else
149                         minetest.after(0.5, change, player)
150                 end
151                 local image_waypoint = player:hud_add {
152                         hud_elem_type = "image_waypoint",
153                         text = "testhud_waypoint.png",
154                         world_pos = player:get_pos(),
155                         scale = {x = 3, y = 3},
156                         offset = {x = 0, y = -32}
157                 }
158                 if not player_waypoints[name] then
159                         player_waypoints[name] = {}
160                 end
161                 if regular then
162                         table.insert(player_waypoints[name], regular)
163                 end
164                 if reduced_precision then
165                         table.insert(player_waypoints[name], reduced_precision)
166                 end
167                 if hidden_distance then
168                         table.insert(player_waypoints[name], hidden_distance)
169                 end
170                 if image_waypoint then
171                         table.insert(player_waypoints[name], image_waypoint)
172                 end
173                 regular = regular or "error"
174                 reduced_precision = reduced_precision or "error"
175                 hidden_distance = hidden_distance or "error"
176                 image_waypoint = image_waypoint or "error"
177                 return true, "Waypoints added. IDs: regular: " .. regular .. ", reduced precision: " .. reduced_precision ..
178                         ", hidden distance: " .. hidden_distance .. ", image waypoint: " .. image_waypoint
179         end
180 })
181
182 minetest.register_on_joinplayer(function(player)
183         player:set_properties({zoom_fov = 15})
184 end)
185
186 minetest.register_chatcommand("zoomfov", {
187         params = "[<FOV>]",
188         description = "Set or display your zoom_fov",
189         func = function(name, param)
190                 local player = minetest.get_player_by_name(name)
191                 if not player then
192                         return false, "No player."
193                 end
194                 if param == "" then
195                         local fov = player:get_properties().zoom_fov
196                         return true, "zoom_fov = "..tostring(fov)
197                 end
198                 local fov = tonumber(param)
199                 if not fov then
200                         return false, "Missing or incorrect zoom_fov parameter!"
201                 end
202                 player:set_properties({zoom_fov = fov})
203                 fov = player:get_properties().zoom_fov
204                 return true, "zoom_fov = "..tostring(fov)
205         end,
206 })
207
208 minetest.register_on_leaveplayer(function(player)
209         player_font_huds[player:get_player_name()] = nil
210         player_waypoints[player:get_player_name()] = nil
211 end)