Skip to content

Commit 6c6e5a7

Browse files
committed
Update float option UI behavior.
1 parent 345567c commit 6c6e5a7

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

Common/Cpp/Options/FloatingPointOption.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ std::string FloatingPointCell::set(double x){
8989
}
9090
return err;
9191
}
92+
void FloatingPointCell::set_and_sanitize(double x){
93+
if (std::isnan(x)){
94+
x = 0;
95+
}
96+
const Data& data = *m_data;
97+
x = std::max(x, data.m_min_value);
98+
x = std::min(x, data.m_max_value);
99+
if (x != m_data->m_current.exchange(x, std::memory_order_relaxed)){
100+
report_value_changed(this);
101+
}
102+
}
92103

93104
void FloatingPointCell::load_json(const JsonValue& json){
94105
double value;

Common/Cpp/Options/FloatingPointOption.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class FloatingPointCell : public ConfigOption{
3838

3939
operator double() const;
4040
std::string set(double x);
41+
void set_and_sanitize(double x);
4142

4243
virtual void load_json(const JsonValue& json) override;
4344
virtual JsonValue to_json() const override;

Common/Qt/Options/FloatingPointWidget.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
#include "ConfigWidget.h"
1111
#include "FloatingPointWidget.h"
1212

13+
//#include <iostream>
14+
//using std::cout;
15+
//using std::endl;
16+
1317
namespace PokemonAutomation{
1418

1519

1620

1721
ConfigWidget* FloatingPointCell::make_QtWidget(QWidget& parent){
18-
return new FloatingPointCellWidget(parent, *this);
22+
return new FloatingPointCellWidget(parent, *this, true);
1923
}
2024
ConfigWidget* FloatingPointOption::make_QtWidget(QWidget& parent){
2125
return new FloatingPointOptionWidget(parent, *this);
@@ -27,7 +31,7 @@ ConfigWidget* FloatingPointOption::make_QtWidget(QWidget& parent){
2731
FloatingPointCellWidget::~FloatingPointCellWidget(){
2832
m_value.remove_listener(*this);
2933
}
30-
FloatingPointCellWidget::FloatingPointCellWidget(QWidget& parent, FloatingPointCell& value)
34+
FloatingPointCellWidget::FloatingPointCellWidget(QWidget& parent, FloatingPointCell& value, bool sanitize)
3135
: QLineEdit(QString::number(value, 'f'), &parent)
3236
, ConfigWidget(value, *this)
3337
, m_value(value)
@@ -48,17 +52,23 @@ FloatingPointCellWidget::FloatingPointCellWidget(QWidget& parent, FloatingPointC
4852
);
4953
connect(
5054
this, &QLineEdit::editingFinished,
51-
this, [this](){
55+
this, [this, sanitize](){
5256
bool ok;
5357
double current = this->text().toDouble(&ok);
5458
QPalette palette;
55-
if (ok && m_value.check_validity(current).empty()){
56-
palette.setColor(QPalette::Text, Qt::black);
59+
palette.setColor(QPalette::Text, Qt::black);
60+
if (sanitize){
61+
m_value.set_and_sanitize(current);
5762
}else{
58-
palette.setColor(QPalette::Text, Qt::red);
63+
if (!ok || !m_value.check_validity(current).empty()){
64+
palette.setColor(QPalette::Text, Qt::red);
65+
}
66+
m_value.set(current);
5967
}
6068
this->setPalette(palette);
61-
m_value.set(current);
69+
70+
// Always update the UI.
71+
on_config_value_changed(this);
6272
}
6373
);
6474
value.add_listener(*this);
@@ -91,7 +101,7 @@ FloatingPointOptionWidget::FloatingPointOptionWidget(QWidget& parent, FloatingPo
91101
text->setTextInteractionFlags(Qt::TextBrowserInteraction);
92102
text->setOpenExternalLinks(true);
93103
layout->addWidget(text, 1);
94-
m_cell = new FloatingPointCellWidget(*this, value);
104+
m_cell = new FloatingPointCellWidget(*this, value, false);
95105
layout->addWidget(m_cell, 1);
96106
value.add_listener(*this);
97107
}

Common/Qt/Options/FloatingPointWidget.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace PokemonAutomation{
1717
class FloatingPointCellWidget : public QLineEdit, public ConfigWidget{
1818
public:
1919
~FloatingPointCellWidget();
20-
FloatingPointCellWidget(QWidget& parent, FloatingPointCell& value);
20+
FloatingPointCellWidget(QWidget& parent, FloatingPointCell& value, bool sanitize);
2121

2222
virtual void update_value() override;
2323
virtual void on_config_value_changed(void* object) override;

0 commit comments

Comments
 (0)