Skip to content

Commit 5698d78

Browse files
authored
Merge pull request #603 from jw098/boxdraw2
BoxDraw: add custom text box for ImageFloatBox coordinates.
2 parents edb3df2 + fd83448 commit 5698d78

File tree

2 files changed

+75
-6
lines changed

2 files changed

+75
-6
lines changed

SerialPrograms/Source/NintendoSwitch/DevPrograms/BoxDraw.cpp

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h"
99
#include "BoxDraw.h"
1010

11-
//#include <iostream>
12-
//using std::cout;
13-
//using std::endl;
11+
// #include <iostream>
12+
// using std::cout;
13+
// using std::endl;
1414

1515
namespace PokemonAutomation{
1616
namespace NintendoSwitch{
@@ -33,11 +33,13 @@ BoxDraw::BoxDraw()
3333
, Y("<b>Y Coordinate:</b>", LockMode::UNLOCK_WHILE_RUNNING, 0.3, 0.0, 1.0)
3434
, WIDTH("<b>Width:</b>", LockMode::UNLOCK_WHILE_RUNNING, 0.4, 0.0, 1.0)
3535
, HEIGHT("<b>Height:</b>", LockMode::UNLOCK_WHILE_RUNNING, 0.4, 0.0, 1.0)
36+
, BOX_COORDINATES(false, "ImageFloatBox coordinates", LockMode::UNLOCK_WHILE_RUNNING, "0.3, 0.3, 0.4, 0.4", "0.3, 0.3, 0.4, 0.4")
3637
{
3738
PA_ADD_OPTION(X);
3839
PA_ADD_OPTION(Y);
3940
PA_ADD_OPTION(WIDTH);
4041
PA_ADD_OPTION(HEIGHT);
42+
PA_ADD_OPTION(BOX_COORDINATES);
4143
}
4244

4345
class BoxDraw::DrawnBox : public ConfigOption::Listener, public VideoOverlay::MouseListener{
@@ -57,16 +59,28 @@ class BoxDraw::DrawnBox : public ConfigOption::Listener, public VideoOverlay::Mo
5759
m_parent.Y.add_listener(*this);
5860
m_parent.WIDTH.add_listener(*this);
5961
m_parent.HEIGHT.add_listener(*this);
62+
m_parent.BOX_COORDINATES.add_listener(*this);
6063
overlay.add_listener(*this);
6164
}catch (...){
6265
detach();
6366
throw;
6467
}
6568
}
6669
virtual void on_config_value_changed(void* object) override{
67-
std::lock_guard<std::mutex> lg(m_lock);
68-
m_overlay_set.clear();
69-
m_overlay_set.add(COLOR_RED, {m_parent.X, m_parent.Y, m_parent.WIDTH, m_parent.HEIGHT});
70+
{
71+
std::lock_guard<std::mutex> lg(m_lock);
72+
m_overlay_set.clear();
73+
m_overlay_set.add(COLOR_RED, {m_parent.X, m_parent.Y, m_parent.WIDTH, m_parent.HEIGHT});
74+
}
75+
76+
if (object == &m_parent.X || object == &m_parent.Y || object == &m_parent.WIDTH || object == &m_parent.HEIGHT){
77+
m_parent.update_box_coordinates();
78+
}
79+
else if(object == &m_parent.BOX_COORDINATES){
80+
m_parent.update_individual_coordinates();
81+
}
82+
83+
7084
}
7185
virtual void on_mouse_press(double x, double y) override{
7286
// m_parent.WIDTH.set(0);
@@ -101,6 +115,7 @@ class BoxDraw::DrawnBox : public ConfigOption::Listener, public VideoOverlay::Mo
101115
m_parent.Y.set(yl);
102116
m_parent.WIDTH.set(xh - xl);
103117
m_parent.HEIGHT.set(yh - yl);
118+
// m_parent.update_box_coordinates();
104119
}
105120

106121
private:
@@ -110,6 +125,7 @@ class BoxDraw::DrawnBox : public ConfigOption::Listener, public VideoOverlay::Mo
110125
m_parent.Y.remove_listener(*this);
111126
m_parent.WIDTH.remove_listener(*this);
112127
m_parent.HEIGHT.remove_listener(*this);
128+
m_parent.BOX_COORDINATES.remove_listener(*this);
113129
}
114130

115131
private:
@@ -121,7 +137,55 @@ class BoxDraw::DrawnBox : public ConfigOption::Listener, public VideoOverlay::Mo
121137
std::optional<std::pair<double, double>> m_mouse_start;
122138
};
123139

140+
void BoxDraw::update_box_coordinates(){
141+
std::string box_coord_string = std::to_string(X) + ", " + std::to_string(Y) + ", " + std::to_string(WIDTH) + ", " + std::to_string(HEIGHT);
142+
BOX_COORDINATES.set(box_coord_string);
143+
}
144+
145+
std::vector<std::string> split(const std::string& str, const std::string& delimiter) {
146+
std::vector<std::string> tokens;
147+
size_t start = 0;
148+
size_t end = str.find(delimiter);
149+
150+
while (end != std::string::npos) {
151+
tokens.push_back(str.substr(start, end - start));
152+
start = end + delimiter.length();
153+
end = str.find(delimiter, start);
154+
}
155+
156+
tokens.push_back(str.substr(start));
157+
return tokens;
158+
}
159+
160+
161+
void BoxDraw::update_individual_coordinates(){
162+
std::string box_coord_string = BOX_COORDINATES;
163+
std::vector<std::string> all_coords = split(box_coord_string, ", ");
164+
165+
std::string x_string = all_coords[0];
166+
std::string y_string = all_coords[1];
167+
std::string width_string = all_coords[2];
168+
std::string height_string = all_coords[3];
169+
170+
double x_coord = std::stod(x_string);
171+
double y_coord = std::stod(y_string);
172+
double width_coord = std::stod(width_string);
173+
double height_coord = std::stod(height_string);
174+
175+
// cout << box_coord_string << endl;
176+
// cout << std::to_string(x_coord) << endl;
177+
// cout << std::to_string(y_coord) << endl;
178+
// cout << std::to_string(width_coord) << endl;
179+
// cout << std::to_string(height_coord) << endl;
180+
181+
X.set(x_coord);
182+
Y.set(y_coord);
183+
WIDTH.set(width_coord);
184+
HEIGHT.set(height_coord);
185+
}
186+
124187
void BoxDraw::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){
188+
update_individual_coordinates();
125189
DrawnBox drawn_box(*this, env.console.overlay());
126190
drawn_box.on_config_value_changed(this);
127191
context.wait_until_cancel();

SerialPrograms/Source/NintendoSwitch/DevPrograms/BoxDraw.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define PokemonAutomation_NintendoSwitch_BoxDraw_H
99

1010
#include "Common/Cpp/Options/FloatingPointOption.h"
11+
#include "Common/Cpp/Options/StringOption.h"
1112
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
1213

1314
namespace PokemonAutomation{
@@ -26,6 +27,9 @@ class BoxDraw : public SingleSwitchProgramInstance{
2627
public:
2728
BoxDraw();
2829

30+
void update_box_coordinates();
31+
void update_individual_coordinates();
32+
2933
virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override;
3034

3135
private:
@@ -36,6 +40,7 @@ class BoxDraw : public SingleSwitchProgramInstance{
3640
FloatingPointOption Y;
3741
FloatingPointOption WIDTH;
3842
FloatingPointOption HEIGHT;
43+
StringOption BOX_COORDINATES;
3944
};
4045

4146

0 commit comments

Comments
 (0)