Split out string/number components

This commit is contained in:
Luke Street
2026-04-29 20:19:27 -06:00
parent ecd74a4cbd
commit 1657fe8083
9 changed files with 296 additions and 246 deletions
+44
View File
@@ -0,0 +1,44 @@
#include "number_button.hpp"
#include <charconv>
#include <fmt/format.h>
namespace dusk::ui {
NumberButton::NumberButton(Rml::Element* parent, Props props)
: BaseStringButton(parent, {.key = std::move(props.key), .type = "number"}),
mGetValue(std::move(props.getValue)), mSetValue(std::move(props.setValue)), mMin(props.min),
mMax(props.max), mStep(props.step) {}
Rml::String NumberButton::format_value() {
return fmt::to_string(mGetValue());
}
void NumberButton::set_value(Rml::String value) {
if (!mSetValue) {
return;
}
int parsedValue = 0;
const char* begin = value.data();
const char* end = begin + value.size();
const auto result = std::from_chars(begin, end, parsedValue);
if (result.ec != std::errc() || result.ptr != end) {
return;
}
mSetValue(std::clamp(parsedValue, mMin, mMax));
}
bool NumberButton::handle_nav_command(NavCommand cmd) {
if (cmd == NavCommand::Left) {
mSetValue(std::clamp(mGetValue() - mStep, mMin, mMax));
return true;
} else if (cmd == NavCommand::Right) {
mSetValue(std::clamp(mGetValue() + mStep, mMin, mMax));
return true;
}
return BaseStringButton::handle_nav_command(cmd);
}
} // namespace dusk::ui