Be explicit about when new keys are added to the map

This commit is contained in:
Chris Thrasher 2022-09-05 23:08:54 -06:00
parent f3aac01744
commit 57a40c531f

View File

@ -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<std::string, 8> axislabels = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"};
// Helper to set text entries to a specified value
template <typename T>
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("<Not Connected>");
texts["ID"].value.setString("");
texts.emplace("ID", JoystickObject{{"<Not Connected>", 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<float>(i + 4) * font.getLineSpacing(14))});
object.label.setString(std::string(axislabels[i]) + ":");
object.value.setPosition({80.f, 5.f + (static_cast<float>(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<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))});
object.label.setString(sstr.str() + ":");
object.value.setPosition(
{80.f, 5.f + (static_cast<float>(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("<Not Connected>");
texts["ID"].value.setString("");
texts.at("ID").label.setString("<Not Connected>");
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