Skip to content

Commit

Permalink
Added copy to clipboard + rounding to 2 digits
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisAsanaka committed Mar 7, 2020
1 parent ba3a64f commit ad3ee7e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Chassim.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ClCompile Include="src\pointsList.cpp" />
<ClCompile Include="src\robot.cpp" />
<ClCompile Include="src\simController.cpp" />
<ClCompile Include="src\utils.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\constants.hpp" />
Expand All @@ -30,6 +31,7 @@
<ClInclude Include="include\sfLine.hpp" />
<ClInclude Include="include\structs.hpp" />
<ClInclude Include="include\simController.hpp" />
<ClInclude Include="include\utils.hpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
Expand Down
6 changes: 6 additions & 0 deletions Chassim.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<ClCompile Include="src\javaProcess.cpp">
<Filter>src\Source Files</Filter>
</ClCompile>
<ClCompile Include="src\utils.cpp">
<Filter>src\Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\constants.hpp">
Expand Down Expand Up @@ -70,5 +73,8 @@
<ClInclude Include="include\sfLine.hpp">
<Filter>include\Header Files</Filter>
</ClInclude>
<ClInclude Include="include\utils.hpp">
<Filter>include\Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions include/simController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SimController {
void unfocused();

void clearPoints();
void copyPoints();
void resetRobot();
void generateProfile();
void fillPath(const char* bytes, size_t n);
Expand Down
7 changes: 7 additions & 0 deletions include/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <string>

double ROUND(double x);

std::string ROUND2STR(double x);
17 changes: 9 additions & 8 deletions src/pointsList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "constants.hpp"
#include "structs.hpp"
#include "utils.hpp"

PointsList::PointsList() {

Expand All @@ -20,9 +21,9 @@ int PointsList::addPoint(double x, double y, double theta) {
int PointsList::addPoint(sf::String str) {
auto data = parsePointStr(str, ',');
if (data.size() == 2) {
points.emplace_back(data[0], data[1]);
points.emplace_back(ROUND(data[0]), ROUND(data[1]));
} else if (data.size() == 3) {
points.emplace_back(data[0], data[1], data[2]);
points.emplace_back(ROUND(data[0]), ROUND(data[1]), ROUND(data[2]));
} else {
return -1;
}
Expand All @@ -31,9 +32,9 @@ int PointsList::addPoint(sf::String str) {

void PointsList::setPoint(int index, double x, double y, double theta) {
Point& point = points.at(index);
point.x = x;
point.y = y;
point.theta = theta;
point.x = ROUND(x);
point.y = ROUND(y);
point.theta = ROUND(theta);
}

bool PointsList::setPoint(int index, sf::String str) {
Expand Down Expand Up @@ -87,12 +88,12 @@ std::vector<double> PointsList::parsePointStr(sf::String str, char delim) {

std::vector<sf::String> PointsList::toStrVector(const Point& point) {
std::vector<sf::String> v;
v.emplace_back(std::to_string(point.x * METERS2INCHES));
v.emplace_back(std::to_string(point.y * METERS2INCHES));
v.emplace_back(ROUND2STR(point.x * METERS2INCHES));
v.emplace_back(ROUND2STR(point.y * METERS2INCHES));
if (std::isnan(point.theta)) {
v.emplace_back();
} else {
v.emplace_back(std::to_string(point.theta));
v.emplace_back(ROUND2STR(point.theta));
}
return v;
}
48 changes: 46 additions & 2 deletions src/simController.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "simController.hpp"

#include <TGUI/TGUI.hpp>
#include <SFML/Window/Clipboard.hpp>
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <thread>
#include <json.hpp>

Expand All @@ -13,6 +15,7 @@
#include "structs.hpp"
#include "pointsList.hpp"
#include "sfLine.hpp"
#include "utils.hpp"

sf::Cursor defaultCursor;
sf::Cursor grabCursor;
Expand Down Expand Up @@ -70,6 +73,11 @@ void SimController::handleEvent(sf::Event event) {
generateProfile();
}
break;
case sf::Keyboard::C:
if (event.key.control && event.key.shift) {
copyPoints();
}
break;
}
break;
}
Expand Down Expand Up @@ -97,7 +105,7 @@ void SimController::handleEvent(sf::Event event) {
window.setMouseCursor(defaultCursor);

generateProfile();
} else {
} else if (gui.get<tgui::EditBox>("rowEditBox") == nullptr) {
int pixelX = event.mouseButton.x;
int pixelY = event.mouseButton.y;

Expand Down Expand Up @@ -236,6 +244,37 @@ void SimController::clearPoints() {
bufferedResponse = "";
}

void SimController::copyPoints() {
std::ostringstream data;
data << "{\n";

auto& points = getPoints();
int len = points.size();
int i = 0;
for (const Point& point : points) {
if (std::isnan(point.theta)) {
data << " new Point(";
data << ROUND(point.x * METERS2INCHES);
data << ", ";
data << ROUND(point.y * METERS2INCHES);
} else {
data << " new Point(";
data << ROUND(point.x * METERS2INCHES);
data << ", ";
data << ROUND(point.y * METERS2INCHES);
data << ", ";
data << ROUND(point.theta);
}
if (i == len - 1) {
data << ")\n";
} else {
data << "),\n";
}
}
data << "}";
sf::Clipboard::setString(data.str());
}

void SimController::resetRobot() {
isPathing.store(false, std::memory_order_release);

Expand Down Expand Up @@ -370,7 +409,10 @@ void SimController::addPoint(float meterX, float meterY, int pixelX, int pixelY)
// Table point
tgui::ListView::Ptr pointsList = gui.get<tgui::ListView>("pointsList");
pointsList->addItem({
std::to_string(meterX * METERS2INCHES), std::to_string(meterY * METERS2INCHES), ""});
ROUND2STR(meterX * METERS2INCHES),
ROUND2STR(meterY * METERS2INCHES),
""
});

// Sprite point
pointSprites.emplace_back();
Expand Down Expand Up @@ -459,6 +501,8 @@ void SimController::createComponents() {
menuBar->addMenu("Edit");
menuBar->addMenuItem("Clear Points");
menuBar->connectMenuItem("Edit", "Clear Points", &SimController::clearPoints, this);
menuBar->addMenuItem("Copy Points");
menuBar->connectMenuItem("Edit", "Copy Points", &SimController::copyPoints, this);
menuBar->addMenuItem("Reset Robot");
menuBar->connectMenuItem("Edit", "Reset Robot", &SimController::resetRobot, this);
menuBar->addMenu("Pathing");
Expand Down
16 changes: 16 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "utils.hpp"

#include <sstream>
#include <iomanip>
#include <cmath>

// Default precision to 2 places
double ROUND(double x) {
return (std::ceil(x * 100.0) / 100.0);
}

std::string ROUND2STR(double x) {
std::ostringstream stream;
stream << std::fixed << std::setprecision(2) << x;
return stream.str();
}

0 comments on commit ad3ee7e

Please sign in to comment.