From 57a40c531f1eedd2292d396d5fd2b95a56e9dc84 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Mon, 5 Sep 2022 23:08:54 -0600 Subject: [PATCH] Be explicit about when new keys are added to the map --- examples/joystick/Joystick.cpp | 53 ++++++++++++---------------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/examples/joystick/Joystick.cpp b/examples/joystick/Joystick.cpp index 479e41f5..976017d6 100644 --- a/examples/joystick/Joystick.cpp +++ b/examples/joystick/Joystick.cpp @@ -24,16 +24,16 @@ Texts texts; std::ostringstream sstr; float threshold = 0.1f; -// Axes labels in as C strings -constexpr std::array axislabels = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"}; +// Axes labels in as strings +const std::array axislabels = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"}; // Helper to set text entries to a specified value template -void set(const char* label, const T& value) +void set(const std::string& label, const T& value) { sstr.str(""); sstr << value; - texts[label].value.setString(sstr.str()); + texts.at(label).value.setString(sstr.str()); } // Update joystick identification @@ -41,8 +41,8 @@ void updateIdentification(unsigned int index) { sstr.str(""); sstr << "Joystick " << index << ":"; - texts["ID"].label.setString(sstr.str()); - texts["ID"].value.setString(sf::Joystick::getIdentification(index).name); + texts.at("ID").label.setString(sstr.str()); + texts.at("ID").value.setString(sf::Joystick::getIdentification(index).name); } // Update joystick axes @@ -63,7 +63,7 @@ void updateButtons(unsigned int index) sstr.str(""); sstr << "Button " << j; - set(sstr.str().c_str(), sf::Joystick::isButtonPressed(index, j)); + set(sstr.str(), sf::Joystick::isButtonPressed(index, j)); } } @@ -103,58 +103,43 @@ int main() sstr.setf(std::ios::fixed | std::ios::boolalpha); // Set up our joystick identification sf::Text objects - texts["ID"].label.setPosition({5.f, 5.f}); - texts["ID"].value.setPosition({80.f, 5.f}); - - texts["ID"].label.setString(""); - texts["ID"].value.setString(""); + texts.emplace("ID", JoystickObject{{"", font}, {"", font}}); + texts.at("ID").label.setPosition({5.f, 5.f}); + texts.at("ID").value.setPosition({80.f, 5.f}); // Set up our threshold sf::Text objects sstr.str(""); sstr << threshold << " (Change with up/down arrow keys)"; - texts["Threshold"].label.setPosition({5.f, 5.f + 2 * font.getLineSpacing(14)}); - texts["Threshold"].value.setPosition({80.f, 5.f + 2 * font.getLineSpacing(14)}); - - texts["Threshold"].label.setString("Threshold:"); - texts["Threshold"].value.setString(sstr.str()); + texts.emplace("Threshold", JoystickObject{{"Threshold:", font}, {sstr.str(), font}}); + texts.at("Threshold").label.setPosition({5.f, 5.f + 2 * font.getLineSpacing(14)}); + texts.at("Threshold").value.setPosition({80.f, 5.f + 2 * font.getLineSpacing(14)}); // Set up our label-value sf::Text objects for (unsigned int i = 0; i < sf::Joystick::AxisCount; ++i) { - JoystickObject& object = texts[axislabels[i]]; + auto& object = texts.insert({axislabels[i], {{axislabels[i] + ":", font}, {"N/A", font}}}).first->second; object.label.setPosition({5.f, 5.f + (static_cast(i + 4) * font.getLineSpacing(14))}); - object.label.setString(std::string(axislabels[i]) + ":"); - object.value.setPosition({80.f, 5.f + (static_cast(i + 4) * font.getLineSpacing(14))}); - object.value.setString("N/A"); } for (unsigned int i = 0; i < sf::Joystick::ButtonCount; ++i) { sstr.str(""); sstr << "Button " << i; - JoystickObject& object = texts[sstr.str()]; + auto& object = texts.insert({sstr.str(), {{sstr.str() + ":", font}, {"N/A", font}}}).first->second; object.label.setPosition( {5.f, 5.f + (static_cast(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))}); - object.label.setString(sstr.str() + ":"); - object.value.setPosition( {80.f, 5.f + (static_cast(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))}); - object.value.setString("N/A"); } for (auto& [label, joystickObject] : texts) { - joystickObject.label.setFont(font); joystickObject.label.setCharacterSize(14); - joystickObject.label.setFillColor(sf::Color::White); - - joystickObject.value.setFont(font); joystickObject.value.setCharacterSize(14); - joystickObject.value.setFillColor(sf::Color::White); } // Update initially displayed joystick values if a joystick is already connected on startup @@ -191,13 +176,13 @@ int main() for (auto& [label, joystickObject] : texts) joystickObject.value.setString("N/A"); - texts["ID"].label.setString(""); - texts["ID"].value.setString(""); + texts.at("ID").label.setString(""); + texts.at("ID").value.setString(""); sstr.str(""); sstr << threshold << " (Change with up/down arrow keys)"; - texts["Threshold"].value.setString(sstr.str()); + texts.at("Threshold").value.setString(sstr.str()); } } @@ -220,7 +205,7 @@ int main() sstr.str(""); sstr << threshold << " (Change with up/down arrow keys)"; - texts["Threshold"].value.setString(sstr.str()); + texts.at("Threshold").value.setString(sstr.str()); } // Clear the window