11#include < algorithm>
22#include < cmath>
3- #include < cctype>
4- #include < cstdlib>
5- #include < limits>
63#include < optional>
74#include < string>
8- #include < string_view>
95
106#include < glm/gtx/intersect.hpp>
117#include < imgui_internal.h>
@@ -202,171 +198,12 @@ void OverlayComponent::on_config_save(utility::Config& cfg) {
202198 for (IModValue& option : m_options) {
203199 option.config_save (cfg);
204200 }
205-
206- if (m_should_reserialize_ui_invert_alpha) {
207- const auto clamped_value = std::clamp (m_ui_invert_alpha->value (), 0 .01f , 0 .99f );
208- cfg.set <float >(m_ui_invert_alpha->get_config_name (), clamped_value);
209- m_should_reserialize_ui_invert_alpha = false ;
210- }
211201}
212202
213203void OverlayComponent::on_config_load (const utility::Config& cfg, bool set_defaults) {
214204 for (IModValue& option : m_options) {
215- if (&option == m_ui_invert_alpha.get ()) {
216- continue ;
217- }
218-
219205 option.config_load (cfg, set_defaults);
220206 }
221-
222- if (set_defaults) {
223- const float default_value = m_ui_invert_alpha->default_value ();
224- m_ui_invert_alpha->value () = std::clamp (default_value, 0 .01f , 0 .99f );
225- m_should_reserialize_ui_invert_alpha = false ;
226- return ;
227- }
228-
229- m_should_reserialize_ui_invert_alpha = false ;
230-
231- const auto config_name = m_ui_invert_alpha->get_config_name ();
232-
233- constexpr auto slider_min = 0 .01f ;
234- constexpr auto slider_max = 0 .99f ;
235-
236- auto apply_invert_alpha = [&](float new_value, bool mark_for_reserialize) {
237- const auto clamped_value = std::clamp (new_value, slider_min, slider_max);
238- if (std::abs (clamped_value - new_value) > std::numeric_limits<float >::epsilon ()) {
239- mark_for_reserialize = true ;
240- }
241-
242- m_ui_invert_alpha->value () = clamped_value;
243- m_should_reserialize_ui_invert_alpha |= mark_for_reserialize;
244- };
245-
246- auto apply_legacy_toggle = [&](bool enabled) {
247- apply_invert_alpha (enabled ? slider_max : slider_min, true );
248- };
249-
250- auto trim = [](std::string_view value) {
251- while (!value.empty () && std::isspace (static_cast <unsigned char >(value.front ()))) {
252- value.remove_prefix (1 );
253- }
254-
255- while (!value.empty () && std::isspace (static_cast <unsigned char >(value.back ()))) {
256- value.remove_suffix (1 );
257- }
258-
259- return value;
260- };
261-
262- auto parse_float = [&](std::string_view value) -> std::optional<float > {
263- const auto trimmed = trim (value);
264-
265- if (trimmed.empty ()) {
266- return std::nullopt ;
267- }
268-
269- std::string buffer{trimmed};
270- char * end_ptr{};
271- const auto parsed_value = std::strtof (buffer.c_str (), &end_ptr);
272-
273- if (end_ptr == buffer.c_str ()) {
274- return std::nullopt ;
275- }
276-
277- while (*end_ptr != ' \0 ' ) {
278- if (!std::isspace (static_cast <unsigned char >(*end_ptr))) {
279- return std::nullopt ;
280- }
281-
282- ++end_ptr;
283- }
284-
285- return parsed_value;
286- };
287-
288- auto try_apply_from_float = [&](std::optional<float > candidate) {
289- if (!candidate) {
290- return false ;
291- }
292-
293- const auto value = *candidate;
294-
295- if (value <= 0 .0f || value >= 1 .0f ) {
296- apply_legacy_toggle (value >= 1 .0f );
297- return true ;
298- }
299-
300- apply_invert_alpha (value, value < slider_min || value > slider_max);
301- return true ;
302- };
303-
304- auto safe_get_float = [&]() -> std::optional<float > {
305- try {
306- if (auto value = cfg.get <float >(config_name)) {
307- return *value;
308- }
309- } catch (...) {
310- }
311-
312- return std::nullopt ;
313- };
314-
315- if (try_apply_from_float (safe_get_float ())) {
316- return ;
317- }
318-
319- auto safe_get_string = [&]() -> std::optional<std::string> {
320- try {
321- return cfg.get (config_name);
322- } catch (...) {
323- return std::nullopt ;
324- }
325- };
326-
327- if (auto raw_value = safe_get_string ()) {
328- const auto trimmed = trim (*raw_value);
329-
330- if (!trimmed.empty ()) {
331- std::string lowered{trimmed};
332- std::transform (lowered.begin (), lowered.end (), lowered.begin (), [](unsigned char c) {
333- return static_cast <char >(std::tolower (c));
334- });
335-
336- if (lowered == " true" || lowered == " false" ) {
337- apply_legacy_toggle (lowered == " true" );
338- return ;
339- }
340-
341- if (lowered == " 1" || lowered == " 0" ) {
342- apply_legacy_toggle (lowered == " 1" );
343- return ;
344- }
345-
346- if (auto parsed = parse_float (trimmed)) {
347- apply_invert_alpha (*parsed, true );
348- return ;
349- }
350- }
351- }
352-
353- auto safe_get_bool = [&]() -> std::optional<bool > {
354- try {
355- if (auto value = cfg.get <bool >(config_name)) {
356- return *value;
357- }
358- } catch (...) {
359- }
360-
361- return std::nullopt ;
362- };
363-
364- if (auto legacy_toggle = safe_get_bool ()) {
365- apply_legacy_toggle (*legacy_toggle);
366- return ;
367- }
368-
369- m_ui_invert_alpha->value () = std::clamp (m_ui_invert_alpha->value (), slider_min, slider_max);
370207}
371208
372209void OverlayComponent::on_draw_ui () {
@@ -1297,4 +1134,4 @@ std::optional<std::reference_wrapper<XrCompositionLayerQuad>> OverlayComponent::
12971134
12981135 return layer;
12991136}
1300- }
1137+ }
0 commit comments