1- local lighting_sections = {
2- {n = " shadows" , d = " Shadows" ,
3- entries = {
4- { n = " intensity" , d = " Shadow Intensity" , min = 0 , max = 1 }
5- }
6- },
7- {
8- n = " exposure" , d = " Exposure" ,
9- entries = {
10- {n = " luminance_min" , d = " Minimum Luminance" , min = - 10 , max = 10 },
11- {n = " luminance_max" , d = " Maximum Luminance" , min = - 10 , max = 10 },
12- {n = " exposure_correction" , d = " Exposure Correction" , min = - 10 , max = 10 },
13- {n = " speed_dark_bright" , d = " Bright light adaptation speed" , min = - 10 , max = 10 , type = " log2" },
14- {n = " speed_bright_dark" , d = " Dark scene adaptation speed" , min = - 10 , max = 10 , type = " log2" },
15- {n = " center_weight_power" , d = " Power factor for center-weighting" , min = 0.1 , max = 10 },
16- }
17- }
18- }
191
20- local function dump_lighting (lighting )
2+ local modpath = minetest .get_modpath (minetest .get_current_modname ())
3+
4+
5+ local function dumpByRecipe (data , recipe )
216 local result = " {\n "
227 local section_count = 0
23- for _ ,section in ipairs (lighting_sections ) do
8+ for _ ,section in ipairs (recipe ) do
249 section_count = section_count + 1
2510
2611 local parameters = section .entries or {}
27- local state = lighting [section .n ] or {}
12+ local state = data [section .n ] or {}
2813
2914 result = result .. " " .. section .n .. " = {\n "
3015
@@ -40,7 +25,7 @@ local function dump_lighting(lighting)
4025
4126 result = result .. " }"
4227
43- if section_count < # lighting_sections then
28+ if section_count < # recipe then
4429 result = result .. " ,"
4530 end
4631 result = result .. " \n "
@@ -49,74 +34,59 @@ local function dump_lighting(lighting)
4934 return result
5035end
5136
52- minetest .register_chatcommand (" set_lighting" , {
53- params = " " ,
54- description = " Tune lighting parameters" ,
55- func = function (player_name , param )
56- local player = minetest .get_player_by_name (player_name );
57- if not player then return end
37+ local function buildGUI (player , data , recipe , gui_name )
38+ local form = {
39+ " formspec_version[2]" ,
40+ " size[15,30]" ,
41+ " position[0.99,0.15]" ,
42+ " anchor[1,0]" ,
43+ " padding[0.05,0.1]" ,
44+ " no_prepend[]"
45+ };
46+
47+ local line = 1
48+ for _ ,section in ipairs (recipe ) do
49+ local parameters = section .entries or {}
50+ local state = data [section .n ] or {}
5851
59- local lighting = player :get_lighting ()
60- local exposure = lighting .exposure or {}
61-
62- local form = {
63- " formspec_version[2]" ,
64- " size[15,30]" ,
65- " position[0.99,0.15]" ,
66- " anchor[1,0]" ,
67- " padding[0.05,0.1]" ,
68- " no_prepend[]"
69- };
70-
71- local line = 1
72- for _ ,section in ipairs (lighting_sections ) do
73- local parameters = section .entries or {}
74- local state = lighting [section .n ] or {}
75-
76- table.insert (form , " label[1," .. line .. " ;" .. section .d .. " ]" )
77- line = line + 1
78-
79- for _ ,v in ipairs (parameters ) do
80- table.insert (form , " label[2," .. line .. " ;" .. v .d .. " ]" )
81- table.insert (form , " scrollbaroptions[min=0;max=1000;smallstep=10;largestep=100;thumbsize=10]" )
82- local value = state [v .n ]
83- if v .type == " log2" then
84- value = math.log (value or 1 ) / math.log (2 )
85- end
86- local sb_scale = math.floor (1000 * (math.max (v .min , value or 0 ) - v .min ) / (v .max - v .min ))
87- table.insert (form , " scrollbar[2," .. (line + 0.7 ).. " ;12,1;horizontal;" .. section .n .. " ." .. v .n .. " ;" .. sb_scale .. " ]" )
88- line = line + 2.7
89- end
52+ table.insert (form , " label[1," .. line .. " ;" .. section .d .. " ]" )
53+ line = line + 1
9054
91- line = line + 1
55+ for _ ,v in ipairs (parameters ) do
56+ table.insert (form , " label[2," .. line .. " ;" .. v .d .. " ]" )
57+ table.insert (form , " scrollbaroptions[min=0;max=1000;smallstep=10;largestep=100;thumbsize=10]" )
58+ local value = state [v .n ]
59+ if v .type == " log2" then
60+ value = math.log (value or 1 ) / math.log (2 )
61+ end
62+ local sb_scale = math.floor (1000 * (math.max (v .min , value or 0 ) - v .min ) / (v .max - v .min ))
63+ table.insert (form , " scrollbar[2," .. (line + 0.7 ).. " ;12,1;horizontal;" .. section .n .. " ." .. v .n .. " ;" .. sb_scale .. " ]" )
64+ line = line + 2.7
9265 end
9366
94- minetest .show_formspec (player_name , " lighting" , table.concat (form ))
95- local debug_value = dump_lighting (lighting )
96- local debug_ui = player :hud_add ({type = " text" , position = {x = 0.1 , y = 0.3 }, scale = {x = 1 ,y = 1 }, alignment = {x = 1 , y = 1 }, text = debug_value , number = 0xFFFFFF })
97- player :get_meta ():set_int (" lighting_hud" , debug_ui )
67+ line = line + 1
9868 end
99- })
10069
101- minetest .register_on_player_receive_fields (function (player , formname , fields )
102- if formname ~= " lighting" then return end
103-
104- if not player then return end
70+ minetest .show_formspec (player :get_player_name (), gui_name , table.concat (form ))
71+ local debug_value = dumpByRecipe (data , recipe )
72+ local debug_ui = player :hud_add ({type = " text" , position = {x = 0.1 , y = 0.3 }, scale = {x = 1 ,y = 1 }, alignment = {x = 1 , y = 1 }, text = debug_value , number = 0xFFFFFF })
73+ player :get_meta ():set_int (gui_name .. " _hud" , debug_ui )
74+ end
10575
106- local hud_id = player :get_meta ():get_int (" lighting_hud" )
76+ local function receiveFields (player , fields , data , recipe , gui_name )
77+ local hud_id = player :get_meta ():get_int (gui_name .. " _hud" )
10778
10879 if fields .quit then
10980 player :hud_remove (hud_id )
110- player :get_meta ():set_int (" lighting_hud " , - 1 )
81+ player :get_meta ():set_int (gui_name .. " _hud " , - 1 )
11182 return
11283 end
11384
114- local lighting = player :get_lighting ()
115- for _ ,section in ipairs (lighting_sections ) do
85+ for _ ,section in ipairs (recipe ) do
11686 local parameters = section .entries or {}
11787
118- local state = (lighting [section .n ] or {})
119- lighting [section .n ] = state
88+ local state = (data [section .n ] or {})
89+ data [section .n ] = state
12090
12191 for _ ,v in ipairs (parameters ) do
12292
@@ -133,8 +103,96 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
133103 end
134104 end
135105
136- local debug_value = dump_lighting ( lighting )
106+ local debug_value = dumpByRecipe ( data , recipe )
137107 player :hud_change (hud_id , " text" , debug_value )
108+ end
109+
110+ local lighting_recipe = {
111+ {n = " shadows" , d = " Shadows" ,
112+ entries = {
113+ { n = " intensity" , d = " Shadow Intensity" , min = 0 , max = 1 }
114+ }
115+ },
116+ {
117+ n = " exposure" , d = " Exposure" ,
118+ entries = {
119+ {n = " luminance_min" , d = " Minimum Luminance" , min = - 10 , max = 10 },
120+ {n = " luminance_max" , d = " Maximum Luminance" , min = - 10 , max = 10 },
121+ {n = " exposure_correction" , d = " Exposure Correction" , min = - 10 , max = 10 },
122+ {n = " speed_dark_bright" , d = " Bright light adaptation speed" , min = - 10 , max = 10 , type = " log2" },
123+ {n = " speed_bright_dark" , d = " Dark scene adaptation speed" , min = - 10 , max = 10 , type = " log2" },
124+ {n = " center_weight_power" , d = " Power factor for center-weighting" , min = 0.1 , max = 10 },
125+ }
126+ },
127+ }
128+
129+ minetest .register_chatcommand (" set_lighting" , {
130+ params = " " ,
131+ description = " Tune lighting parameters" ,
132+ func = function (player_name , param )
133+ local player = minetest .get_player_by_name (player_name )
134+ if not player then return end
135+
136+ local lighting = player :get_lighting ()
137+
138+ buildGUI (player , lighting , lighting_recipe , " lighting" )
139+ end
140+ })
141+
142+ minetest .register_on_player_receive_fields (function (player , formname , fields )
143+ if formname ~= " lighting" then return end
144+
145+ if not player then return end
146+
147+ local lighting = player :get_lighting ()
148+
149+ receiveFields (player , fields , lighting , lighting_recipe , " lighting" )
138150
139151 player :set_lighting (lighting )
140- end )
152+ end )
153+
154+ local sky_light_recipe = {
155+ {n = " color_offset" , d = " Color offset" ,
156+ entries = {
157+ {n = " r" , d = " Red color offset" , min = - 1 , max = 2 },
158+ {n = " g" , d = " Green color offset" , min = - 1 , max = 2 },
159+ {n = " b" , d = " Blue color offset" , min = - 1 , max = 2 },
160+ }
161+ },
162+ {n = " color_ratio_coef" , d = " Color day-night ratio coefficient" ,
163+ entries = {
164+ {n = " r" , d = " Red color day-night ratio coefficient" , min = - 1e-3 , max = 2e-3 },
165+ {n = " g" , d = " Green color day-night ratio coefficient" , min = - 1e-3 , max = 2e-3 },
166+ {n = " b" , d = " Blue color day-night ratio coefficient" , min = - 1e-3 , max = 2e-3 },
167+ }
168+ }
169+ }
170+
171+
172+ minetest .register_chatcommand (" set_sky_light" , {
173+ params = " " ,
174+ description = " Tune lighting sky_light parameters" ,
175+ func = function (player_name , param )
176+ local player = minetest .get_player_by_name (player_name )
177+ if not player then return end
178+
179+ local lighting = player :get_lighting ()
180+ local sky_light = lighting .sky_light
181+
182+ buildGUI (player , sky_light , sky_light_recipe , " sky_light" )
183+ end
184+ })
185+
186+ minetest .register_on_player_receive_fields (function (player , formname , fields )
187+ if formname ~= " sky_light" then return end
188+
189+ if not player then return end
190+
191+ local lighting = player :get_lighting ()
192+ local sky_light = lighting .sky_light
193+
194+ receiveFields (player , fields , sky_light , sky_light_recipe , " sky_light" )
195+
196+ player :set_lighting (lighting )
197+ end )
198+
0 commit comments