2018-02-03 20:08:44 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Graphics.hpp>
|
2022-07-05 00:20:58 +08:00
|
|
|
|
2018-02-03 20:08:44 +08:00
|
|
|
#include <algorithm>
|
2022-06-13 00:41:53 +08:00
|
|
|
#include <array>
|
2018-02-03 20:08:44 +08:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2021-12-06 19:36:16 +08:00
|
|
|
#include <unordered_map>
|
2018-02-03 20:08:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2022-07-05 00:20:58 +08:00
|
|
|
struct JoystickObject
|
|
|
|
{
|
|
|
|
sf::Text label;
|
|
|
|
sf::Text value;
|
|
|
|
};
|
2018-02-03 20:08:44 +08:00
|
|
|
|
2022-07-05 00:20:58 +08:00
|
|
|
using Texts = std::unordered_map<std::string, JoystickObject>;
|
|
|
|
Texts texts;
|
|
|
|
std::ostringstream sstr;
|
|
|
|
float threshold = 0.1f;
|
2018-02-03 20:08:44 +08:00
|
|
|
|
2022-09-06 13:08:54 +08:00
|
|
|
// Axes labels in as strings
|
|
|
|
const std::array<std::string, 8> axislabels = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"};
|
2018-02-03 20:08:44 +08:00
|
|
|
|
2022-07-05 00:20:58 +08:00
|
|
|
// Helper to set text entries to a specified value
|
|
|
|
template <typename T>
|
2022-09-06 13:08:54 +08:00
|
|
|
void set(const std::string& label, const T& value)
|
2022-07-05 00:20:58 +08:00
|
|
|
{
|
|
|
|
sstr.str("");
|
|
|
|
sstr << value;
|
2022-09-06 13:08:54 +08:00
|
|
|
texts.at(label).value.setString(sstr.str());
|
2022-07-05 00:20:58 +08:00
|
|
|
}
|
2018-02-03 20:08:44 +08:00
|
|
|
|
2022-07-05 00:20:58 +08:00
|
|
|
// Update joystick identification
|
|
|
|
void updateIdentification(unsigned int index)
|
|
|
|
{
|
|
|
|
sstr.str("");
|
|
|
|
sstr << "Joystick " << index << ":";
|
2022-09-06 13:18:29 +08:00
|
|
|
auto& [label, value] = texts.at("ID");
|
|
|
|
label.setString(sstr.str());
|
|
|
|
value.setString(sf::Joystick::getIdentification(index).name);
|
2022-07-05 00:20:58 +08:00
|
|
|
}
|
2018-02-03 20:08:44 +08:00
|
|
|
|
2022-07-05 00:20:58 +08:00
|
|
|
// Update joystick axes
|
|
|
|
void updateAxes(unsigned int index)
|
|
|
|
{
|
|
|
|
for (unsigned int j = 0; j < sf::Joystick::AxisCount; ++j)
|
2018-02-03 20:08:44 +08:00
|
|
|
{
|
2022-07-05 00:20:58 +08:00
|
|
|
if (sf::Joystick::hasAxis(index, static_cast<sf::Joystick::Axis>(j)))
|
|
|
|
set(axislabels[j], sf::Joystick::getAxisPosition(index, static_cast<sf::Joystick::Axis>(j)));
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
2022-07-05 00:20:58 +08:00
|
|
|
}
|
2018-02-03 20:08:44 +08:00
|
|
|
|
2022-07-05 00:20:58 +08:00
|
|
|
// Update joystick buttons
|
|
|
|
void updateButtons(unsigned int index)
|
|
|
|
{
|
|
|
|
for (unsigned int j = 0; j < sf::Joystick::getButtonCount(index); ++j)
|
2018-02-03 20:08:44 +08:00
|
|
|
{
|
2022-07-05 00:20:58 +08:00
|
|
|
sstr.str("");
|
|
|
|
sstr << "Button " << j;
|
2018-02-03 20:08:44 +08:00
|
|
|
|
2022-09-06 13:08:54 +08:00
|
|
|
set(sstr.str(), sf::Joystick::isButtonPressed(index, j));
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
2022-07-05 00:20:58 +08:00
|
|
|
}
|
2018-02-03 20:08:44 +08:00
|
|
|
|
2022-07-05 00:20:58 +08:00
|
|
|
// Helper to update displayed joystick values
|
|
|
|
void updateValues(unsigned int index)
|
|
|
|
{
|
|
|
|
if (sf::Joystick::isConnected(index))
|
2018-02-03 20:08:44 +08:00
|
|
|
{
|
2022-07-05 00:20:58 +08:00
|
|
|
// Update the label-value sf::Text objects based on the current joystick state
|
|
|
|
updateIdentification(index);
|
|
|
|
updateAxes(index);
|
|
|
|
updateButtons(index);
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
|
|
|
}
|
2022-07-05 00:20:58 +08:00
|
|
|
} // namespace
|
2018-02-03 20:08:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Create the window of the application
|
2022-03-30 08:53:17 +08:00
|
|
|
sf::RenderWindow window(sf::VideoMode({400, 775}), "Joystick", sf::Style::Close);
|
2018-02-03 20:08:44 +08:00
|
|
|
window.setVerticalSyncEnabled(true);
|
|
|
|
|
2024-06-25 04:08:51 +08:00
|
|
|
// Open the text font
|
2024-07-06 09:38:32 +08:00
|
|
|
const auto font = sf::Font::createFromFile("resources/tuffy.ttf").value();
|
2018-02-03 20:08:44 +08:00
|
|
|
|
|
|
|
// Set up our string conversion parameters
|
|
|
|
sstr.precision(2);
|
|
|
|
sstr.setf(std::ios::fixed | std::ios::boolalpha);
|
|
|
|
|
|
|
|
// Set up our joystick identification sf::Text objects
|
2022-09-06 13:18:29 +08:00
|
|
|
{
|
|
|
|
auto [it, success] = texts.emplace("ID", JoystickObject{{font, "<Not Connected>"}, {font}});
|
|
|
|
auto& [label, value] = it->second;
|
|
|
|
label.setPosition({5.f, 5.f});
|
|
|
|
value.setPosition({80.f, 5.f});
|
|
|
|
}
|
2018-02-03 20:08:44 +08:00
|
|
|
|
|
|
|
// Set up our threshold sf::Text objects
|
|
|
|
sstr.str("");
|
|
|
|
sstr << threshold << " (Change with up/down arrow keys)";
|
2022-09-06 13:18:29 +08:00
|
|
|
{
|
|
|
|
auto [it, success] = texts.emplace("Threshold", JoystickObject{{font, "Threshold:"}, {font, sstr.str()}});
|
|
|
|
auto& [label, value] = it->second;
|
|
|
|
label.setPosition({5.f, 5.f + 2 * font.getLineSpacing(14)});
|
|
|
|
value.setPosition({80.f, 5.f + 2 * font.getLineSpacing(14)});
|
|
|
|
}
|
2018-02-03 20:08:44 +08:00
|
|
|
|
|
|
|
// Set up our label-value sf::Text objects
|
|
|
|
for (unsigned int i = 0; i < sf::Joystick::AxisCount; ++i)
|
|
|
|
{
|
2022-09-06 13:18:29 +08:00
|
|
|
auto [it, success] = texts.emplace(axislabels[i], JoystickObject{{font, axislabels[i] + ":"}, {font, "N/A"}});
|
|
|
|
auto& [label, value] = it->second;
|
|
|
|
label.setPosition({5.f, 5.f + (static_cast<float>(i + 4) * font.getLineSpacing(14))});
|
|
|
|
value.setPosition({80.f, 5.f + (static_cast<float>(i + 4) * font.getLineSpacing(14))});
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < sf::Joystick::ButtonCount; ++i)
|
|
|
|
{
|
|
|
|
sstr.str("");
|
|
|
|
sstr << "Button " << i;
|
2022-09-06 13:18:29 +08:00
|
|
|
auto [it, success] = texts.emplace(sstr.str(), JoystickObject{{font, sstr.str() + ":"}, {font, "N/A"}});
|
|
|
|
auto& [label, value] = it->second;
|
|
|
|
label.setPosition({5.f, 5.f + (static_cast<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))});
|
|
|
|
value.setPosition({80.f, 5.f + (static_cast<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))});
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
|
|
|
|
2021-12-08 09:08:11 +08:00
|
|
|
for (auto& [label, joystickObject] : texts)
|
2018-02-03 20:08:44 +08:00
|
|
|
{
|
2021-12-08 09:08:11 +08:00
|
|
|
joystickObject.label.setCharacterSize(14);
|
|
|
|
joystickObject.value.setCharacterSize(14);
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update initially displayed joystick values if a joystick is already connected on startup
|
|
|
|
for (unsigned int i = 0; i < sf::Joystick::Count; ++i)
|
|
|
|
{
|
|
|
|
if (sf::Joystick::isConnected(i))
|
|
|
|
{
|
|
|
|
updateValues(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (window.isOpen())
|
|
|
|
{
|
|
|
|
// Handle events
|
2024-06-23 18:30:02 +08:00
|
|
|
while (const std::optional event = window.pollEvent())
|
2018-02-03 20:08:44 +08:00
|
|
|
{
|
|
|
|
// Window closed or escape key pressed: exit
|
2024-06-23 18:30:02 +08:00
|
|
|
if (event->is<sf::Event::Closed>() ||
|
|
|
|
(event->is<sf::Event::KeyPressed>() &&
|
|
|
|
event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape))
|
2018-02-03 20:08:44 +08:00
|
|
|
{
|
|
|
|
window.close();
|
|
|
|
break;
|
|
|
|
}
|
2024-06-23 18:30:02 +08:00
|
|
|
|
|
|
|
if (const auto* joystickButtonPressed = event->getIf<sf::Event::JoystickButtonPressed>())
|
2018-02-03 20:08:44 +08:00
|
|
|
{
|
|
|
|
// Update displayed joystick values
|
2023-11-04 11:47:14 +08:00
|
|
|
updateValues(joystickButtonPressed->joystickId);
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
2024-06-23 18:30:02 +08:00
|
|
|
else if (const auto* joystickButtonReleased = event->getIf<sf::Event::JoystickButtonReleased>())
|
2023-11-04 11:47:14 +08:00
|
|
|
{
|
|
|
|
// Update displayed joystick values
|
|
|
|
updateValues(joystickButtonReleased->joystickId);
|
|
|
|
}
|
2024-06-23 18:30:02 +08:00
|
|
|
else if (const auto* joystickMoved = event->getIf<sf::Event::JoystickMoved>())
|
2023-11-04 11:47:14 +08:00
|
|
|
{
|
|
|
|
// Update displayed joystick values
|
|
|
|
updateValues(joystickMoved->joystickId);
|
|
|
|
}
|
2024-06-23 18:30:02 +08:00
|
|
|
else if (const auto* joystickConnected = event->getIf<sf::Event::JoystickConnected>())
|
2023-11-04 11:47:14 +08:00
|
|
|
{
|
|
|
|
// Update displayed joystick values
|
|
|
|
updateValues(joystickConnected->joystickId);
|
|
|
|
}
|
2024-06-23 18:30:02 +08:00
|
|
|
else if (event->is<sf::Event::JoystickDisconnected>())
|
2018-02-03 20:08:44 +08:00
|
|
|
{
|
|
|
|
// Reset displayed joystick values to empty
|
2021-12-08 09:08:11 +08:00
|
|
|
for (auto& [label, joystickObject] : texts)
|
|
|
|
joystickObject.value.setString("N/A");
|
2018-02-03 20:08:44 +08:00
|
|
|
|
2022-09-06 13:18:29 +08:00
|
|
|
auto& [label, value] = texts.at("ID");
|
|
|
|
label.setString("<Not Connected>");
|
|
|
|
value.setString("");
|
2018-02-03 20:08:44 +08:00
|
|
|
|
|
|
|
sstr.str("");
|
|
|
|
sstr << threshold << " (Change with up/down arrow keys)";
|
|
|
|
|
2022-09-06 13:08:54 +08:00
|
|
|
texts.at("Threshold").value.setString(sstr.str());
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update threshold if the user wants to change it
|
|
|
|
float newThreshold = threshold;
|
|
|
|
|
2023-12-20 06:45:33 +08:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up))
|
2018-02-03 20:08:44 +08:00
|
|
|
newThreshold += 0.1f;
|
|
|
|
|
2023-12-20 06:45:33 +08:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down))
|
2018-02-03 20:08:44 +08:00
|
|
|
newThreshold -= 0.1f;
|
|
|
|
|
2022-12-28 07:58:49 +08:00
|
|
|
newThreshold = std::clamp(newThreshold, 0.1f, 100.0f);
|
2018-02-03 20:08:44 +08:00
|
|
|
|
|
|
|
if (newThreshold != threshold)
|
|
|
|
{
|
|
|
|
threshold = newThreshold;
|
|
|
|
window.setJoystickThreshold(threshold);
|
|
|
|
|
|
|
|
sstr.str("");
|
|
|
|
sstr << threshold << " (Change with up/down arrow keys)";
|
|
|
|
|
2022-09-06 13:08:54 +08:00
|
|
|
texts.at("Threshold").value.setString(sstr.str());
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the window
|
|
|
|
window.clear();
|
|
|
|
|
|
|
|
// Draw the label-value sf::Text objects
|
2021-12-08 09:08:11 +08:00
|
|
|
for (const auto& [label, joystickObject] : texts)
|
2018-02-03 20:08:44 +08:00
|
|
|
{
|
2021-12-08 09:08:11 +08:00
|
|
|
window.draw(joystickObject.label);
|
|
|
|
window.draw(joystickObject.value);
|
2018-02-03 20:08:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display things on screen
|
|
|
|
window.display();
|
|
|
|
}
|
|
|
|
}
|