Fix error with invalid float in the gauge component. (#356)

This commit is contained in:
Arthur Sonzogni 2022-03-13 22:30:05 +01:00 committed by GitHub
parent 4da63b9260
commit c76612a3c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 4 deletions

View File

@ -44,6 +44,63 @@ int GeneratorInt(const char* data, size_t size) {
return out; return out;
} }
Color GeneratorColor(const char* data, size_t size) {
return Color::RGB(GeneratorInt(data, size), GeneratorInt(data, size),
GeneratorInt(data, size));
}
AnimatedColorOption GeneratorAnimatedColorOption(const char* data,
size_t size) {
AnimatedColorOption option;
option.enabled = GeneratorBool(data, size);
option.inactive = GeneratorColor(data, size);
option.active = GeneratorColor(data, size);
option.duration = std::chrono::milliseconds(GeneratorInt(data, size));
return option;
}
AnimatedColorsOption GeneratorAnimatedColorsOptions(const char* data,
size_t size) {
AnimatedColorsOption option;
option.background = GeneratorAnimatedColorOption(data, size);
option.foreground = GeneratorAnimatedColorOption(data, size);
return option;
}
ButtonOption GeneratorButtonOption(const char* data, size_t size) {
ButtonOption option;
option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
return option;
}
UnderlineOption GeneratorUnderlineOption(const char* data, size_t size) {
UnderlineOption option;
option.enabled = GeneratorBool(data, size);
option.color_active = GeneratorColor(data, size);
option.color_inactive = GeneratorColor(data, size);
option.leader_duration = std::chrono::milliseconds(GeneratorInt(data, size));
option.follower_duration =
std::chrono::milliseconds(GeneratorInt(data, size));
option.leader_delay = std::chrono::milliseconds(GeneratorInt(data, size));
option.follower_delay = std::chrono::milliseconds(GeneratorInt(data, size));
return option;
}
MenuEntryOption GeneratorMenuEntryOption(const char* data, size_t size) {
MenuEntryOption option;
option.animated_colors = GeneratorAnimatedColorsOptions(data, size);
return option;
}
MenuOption GeneratorMenuOption(const char* data, size_t size) {
MenuOption option;
option.underline = GeneratorUnderlineOption(data, size);
option.entries = GeneratorMenuEntryOption(data, size);
option.direction =
static_cast<MenuOption::Direction>(GeneratorInt(data, size) % 4);
return option;
}
bool g_bool; bool g_bool;
int g_int; int g_int;
std::vector<std::string> g_list; std::vector<std::string> g_list;
@ -60,13 +117,15 @@ Component GeneratorComponent(const char*& data, size_t& size, int depth) {
value = (value % value_max + value_max) % value_max; value = (value % value_max + value_max) % value_max;
switch (value) { switch (value) {
case 0: case 0:
return Button(GeneratorString(data, size), [] {}); return Button(
GeneratorString(data, size), [] {},
GeneratorButtonOption(data, size));
case 1: case 1:
return Checkbox(GeneratorString(data, size), &g_bool); return Checkbox(GeneratorString(data, size), &g_bool);
case 2: case 2:
return Input(GeneratorString(data, size), GeneratorString(data, size)); return Input(GeneratorString(data, size), GeneratorString(data, size));
case 3: case 3:
return Menu(&g_list, &g_int); return Menu(&g_list, &g_int, GeneratorMenuOption(data, size));
case 4: case 4:
return Radiobox(&g_list, &g_int); return Radiobox(&g_list, &g_int);
case 5: case 5:

View File

@ -1,3 +1,4 @@
#include <iostream>
#include <algorithm> // for max, min #include <algorithm> // for max, min
#include <memory> // for allocator, make_shared #include <memory> // for allocator, make_shared
#include <string> // for string #include <string> // for string
@ -40,8 +41,13 @@ static std::string charset_vertical[10] = {
class Gauge : public Node { class Gauge : public Node {
public: public:
Gauge(float progress, GaugeDirection direction) Gauge(float progress, GaugeDirection direction)
: progress_(std::min(std::max(progress, 0.f), 1.f)), : progress_(progress), direction_(direction) {
direction_(direction) {} // This handle NAN correctly:
if (!(progress_ > 0.f))
progress_ = 0.f;
if (!(progress_ < 1.f))
progress_ = 1.f;
}
void ComputeRequirement() override { void ComputeRequirement() override {
switch (direction_) { switch (direction_) {