Skip to content

Commit a0337d3

Browse files
committed
Make changes less verbose
1 parent d306c35 commit a0337d3

3 files changed

Lines changed: 18 additions & 167 deletions

File tree

src/Mod.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,17 @@ class ModSlider : public ModFloat {
251251
: ModFloat{ config_name, default_value, advanced_option },
252252
m_range{ mn, mx }
253253
{
254+
clamp_to_range();
255+
}
256+
257+
void config_load(const utility::Config& cfg, bool set_defaults) override {
258+
ModFloat::config_load(cfg, set_defaults);
259+
clamp_to_range();
260+
};
261+
262+
void set(const std::string& value) override {
263+
ModValue<float>::set(value);
264+
clamp_to_range();
254265
}
255266

256267
bool draw(std::string_view name) override {
@@ -280,6 +291,10 @@ class ModSlider : public ModFloat {
280291
}
281292

282293
protected:
294+
void clamp_to_range() {
295+
m_value = std::clamp(m_value, m_range.x, m_range.y);
296+
}
297+
283298
Vector2f m_range{ 0.0f, 1.0f };
284299
};
285300

@@ -691,4 +706,4 @@ inline IModValue* Mod::get_value(std::string_view name) const {
691706
}
692707

693708
return &it->get();
694-
}
709+
}

src/mods/vr/OverlayComponent.cpp

Lines changed: 1 addition & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
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

213203
void 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

372209
void OverlayComponent::on_draw_ui() {
@@ -1297,4 +1134,4 @@ std::optional<std::reference_wrapper<XrCompositionLayerQuad>> OverlayComponent::
12971134

12981135
return layer;
12991136
}
1300-
}
1137+
}

src/mods/vr/OverlayComponent.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ class OverlayComponent : public ModComponent {
8181
bool m_just_closed_ui{false};
8282
bool m_just_opened_ui{false};
8383
bool m_forced_aim{false};
84-
bool m_should_reserialize_ui_invert_alpha{false};
8584

8685
glm::vec2 m_last_mouse_pos{};
8786
std::chrono::steady_clock::time_point m_last_mouse_move_time{};
@@ -178,4 +177,4 @@ class OverlayComponent : public ModComponent {
178177
void update_overlay_openvr();
179178
bool update_wrist_overlay_openvr();
180179
void update_slate_openvr();
181-
};}
180+
};}

0 commit comments

Comments
 (0)