Skip to content

Commit ba40ebf

Browse files
committed
BoxFloatOption
1 parent 73f1b57 commit ba40ebf

File tree

5 files changed

+549
-0
lines changed

5 files changed

+549
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/* Box Float Option
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include "Common/Cpp/Containers/Pimpl.tpp"
8+
#include "Common/Cpp/Concurrency/SpinLock.h"
9+
#include "Common/Cpp/Json/JsonObject.h"
10+
#include "BoxFloatOption.h"
11+
12+
namespace PokemonAutomation{
13+
14+
15+
16+
struct BoxFloatOption::Data{
17+
std::string m_label;
18+
double m_default_x;
19+
double m_default_y;
20+
double m_default_width;
21+
double m_default_height;
22+
23+
double m_x;
24+
double m_y;
25+
double m_width;
26+
double m_height;
27+
28+
mutable SpinLock m_lock;
29+
};
30+
31+
32+
33+
BoxFloatOption::~BoxFloatOption() = default;
34+
BoxFloatOption::BoxFloatOption(
35+
std::string label,
36+
LockMode lock_while_running,
37+
double default_x,
38+
double default_y,
39+
double default_width,
40+
double default_height
41+
)
42+
: ConfigOption(lock_while_running)
43+
, m_data(CONSTRUCT_TOKEN)
44+
{
45+
Data& self = *m_data;
46+
47+
self.m_label = std::move(label);
48+
49+
set_all(default_x, default_y, default_width, default_height);
50+
51+
self.m_default_x = self.m_x;
52+
self.m_default_y = self.m_y;
53+
self.m_default_width = self.m_width;
54+
self.m_default_height = self.m_height;
55+
}
56+
const std::string& BoxFloatOption::label() const{
57+
return m_data->m_label;
58+
}
59+
double BoxFloatOption::x() const{
60+
return m_data->m_x;
61+
}
62+
double BoxFloatOption::y() const{
63+
return m_data->m_y;
64+
}
65+
double BoxFloatOption::width() const{
66+
return m_data->m_width;
67+
}
68+
double BoxFloatOption::height() const{
69+
return m_data->m_height;
70+
}
71+
72+
73+
bool BoxFloatOption::sanitize(double& x, double& y, double& width, double& height) const{
74+
bool changed = false;
75+
76+
if (x < 0.0){
77+
x = 0.0;
78+
changed = true;
79+
}
80+
if (x > 1.0){
81+
x = 1.0;
82+
changed = true;
83+
}
84+
85+
if (y < 0.0){
86+
y = 0.0;
87+
changed = true;
88+
}
89+
if (y > 1.0){
90+
y = 1.0;
91+
changed = true;
92+
}
93+
94+
if (width < 0.0){
95+
width = 0.0;
96+
changed = true;
97+
}
98+
if (width > 1.0){
99+
width = 1.0;
100+
changed = true;
101+
}
102+
103+
if (height < 0.0){
104+
height = 0.0;
105+
changed = true;
106+
}
107+
if (height > 1.0){
108+
height = 1.0;
109+
changed = true;
110+
}
111+
112+
if (x + width > 1.00000000001){
113+
width = 1.0 - x;
114+
changed = true;
115+
}
116+
if (y + height > 1.00000000001){
117+
height = 1.0 - y;
118+
changed = true;
119+
}
120+
121+
return changed;
122+
}
123+
void BoxFloatOption::get_all(double& x, double& y, double& width, double& height) const{
124+
const Data& self = *m_data;
125+
126+
ReadSpinLock lg(self.m_lock);
127+
x = self.m_x;
128+
y = self.m_y;
129+
width = self.m_width;
130+
height = self.m_height;
131+
}
132+
void BoxFloatOption::set_all(double x, double y, double width, double height){
133+
sanitize(x, y, width, height);
134+
135+
Data& self = *m_data;
136+
bool changed = false;
137+
138+
{
139+
WriteSpinLock lg(self.m_lock);
140+
141+
changed |= self.m_x != x;
142+
changed |= self.m_y != y;
143+
changed |= self.m_width != width;
144+
changed |= self.m_height != height;
145+
146+
self.m_x = x;
147+
self.m_y = y;
148+
self.m_width = width;
149+
self.m_height = height;
150+
}
151+
152+
if (changed){
153+
report_value_changed(this);
154+
}
155+
}
156+
157+
158+
void BoxFloatOption::load_json(const JsonValue& json){
159+
const JsonObject* obj = json.to_object();
160+
if (obj == nullptr){
161+
return;
162+
}
163+
164+
double x, y, width, height;
165+
get_all(x, y, width, height);
166+
167+
obj->read_float(x, "x");
168+
obj->read_float(y, "y");
169+
obj->read_float(width, "width");
170+
obj->read_float(height, "height");
171+
172+
set_all(x, y, width, height);
173+
}
174+
JsonValue BoxFloatOption::to_json() const{
175+
double x, y, width, height;
176+
get_all(x, y, width, height);
177+
178+
JsonObject ret;
179+
ret["x"] = std::to_string(x);
180+
ret["y"] = std::to_string(y);
181+
ret["width"] = std::to_string(width);
182+
ret["height"] = std::to_string(height);
183+
return ret;
184+
}
185+
186+
std::string BoxFloatOption::check_validity() const{
187+
double x, y, width, height;
188+
get_all(x, y, width, height);
189+
190+
if (x < 0 || x > 1.0){
191+
return "x must be between 0.0 and 1.0.";
192+
}
193+
if (y < 0 || y > 1.0){
194+
return "y must be between 0.0 and 1.0.";
195+
}
196+
if (width < 0 || width > 1.0){
197+
return "width must be between 0.0 and 1.0.";
198+
}
199+
if (height < 0 || height > 1.0){
200+
return "height must be between 0.0 and 1.0.";
201+
}
202+
if (x + width > 1.00000000001){
203+
return "x + width must be <= 1.0.";
204+
}
205+
if (y + width > 00000000001){
206+
return "y + width must be <= 1.0.";
207+
}
208+
209+
return std::string();
210+
}
211+
void BoxFloatOption::restore_defaults(){
212+
Data& self = *m_data;
213+
set_all(
214+
self.m_default_x,
215+
self.m_default_y,
216+
self.m_default_width,
217+
self.m_default_height
218+
);
219+
}
220+
221+
222+
223+
224+
}
225+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Box Float Option
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_Options_BoxFloatOption_H
8+
#define PokemonAutomation_Options_BoxFloatOption_H
9+
10+
#include "Common/Cpp/Containers/Pimpl.h"
11+
#include "Common/Cpp/Options/ConfigOption.h"
12+
13+
namespace PokemonAutomation{
14+
15+
16+
class BoxFloatOption : public ConfigOption{
17+
public:
18+
~BoxFloatOption();
19+
BoxFloatOption(
20+
std::string label,
21+
LockMode lock_while_running,
22+
double default_x,
23+
double default_y,
24+
double default_width,
25+
double default_height
26+
);
27+
28+
const std::string& label() const;
29+
double x() const;
30+
double y() const;
31+
double width() const;
32+
double height() const;
33+
34+
// Returns true if a value was changed.
35+
bool sanitize(double& x, double& y, double& width, double& height) const;
36+
37+
void get_all(double& x, double& y, double& width, double& height) const;
38+
void set_all(double x, double y, double width, double height);
39+
40+
virtual void load_json(const JsonValue& json) override;
41+
virtual JsonValue to_json() const override;
42+
43+
virtual std::string check_validity() const override;
44+
virtual void restore_defaults() override;
45+
46+
virtual ConfigWidget* make_QtWidget(QWidget& parent) override;
47+
48+
49+
private:
50+
struct Data;
51+
Pimpl<Data> m_data;
52+
};
53+
54+
55+
56+
}
57+
#endif

0 commit comments

Comments
 (0)