mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 14:21:04 +08:00
Be explicit about when new keys are added to the map
This commit is contained in:
parent
f3aac01744
commit
57a40c531f
@ -24,16 +24,16 @@ Texts texts;
|
|||||||
std::ostringstream sstr;
|
std::ostringstream sstr;
|
||||||
float threshold = 0.1f;
|
float threshold = 0.1f;
|
||||||
|
|
||||||
// Axes labels in as C strings
|
// Axes labels in as strings
|
||||||
constexpr std::array axislabels = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"};
|
const std::array<std::string, 8> axislabels = {"X", "Y", "Z", "R", "U", "V", "PovX", "PovY"};
|
||||||
|
|
||||||
// Helper to set text entries to a specified value
|
// Helper to set text entries to a specified value
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void set(const char* label, const T& value)
|
void set(const std::string& label, const T& value)
|
||||||
{
|
{
|
||||||
sstr.str("");
|
sstr.str("");
|
||||||
sstr << value;
|
sstr << value;
|
||||||
texts[label].value.setString(sstr.str());
|
texts.at(label).value.setString(sstr.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update joystick identification
|
// Update joystick identification
|
||||||
@ -41,8 +41,8 @@ void updateIdentification(unsigned int index)
|
|||||||
{
|
{
|
||||||
sstr.str("");
|
sstr.str("");
|
||||||
sstr << "Joystick " << index << ":";
|
sstr << "Joystick " << index << ":";
|
||||||
texts["ID"].label.setString(sstr.str());
|
texts.at("ID").label.setString(sstr.str());
|
||||||
texts["ID"].value.setString(sf::Joystick::getIdentification(index).name);
|
texts.at("ID").value.setString(sf::Joystick::getIdentification(index).name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update joystick axes
|
// Update joystick axes
|
||||||
@ -63,7 +63,7 @@ void updateButtons(unsigned int index)
|
|||||||
sstr.str("");
|
sstr.str("");
|
||||||
sstr << "Button " << j;
|
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);
|
sstr.setf(std::ios::fixed | std::ios::boolalpha);
|
||||||
|
|
||||||
// Set up our joystick identification sf::Text objects
|
// Set up our joystick identification sf::Text objects
|
||||||
texts["ID"].label.setPosition({5.f, 5.f});
|
texts.emplace("ID", JoystickObject{{"<Not Connected>", font}, {"", font}});
|
||||||
texts["ID"].value.setPosition({80.f, 5.f});
|
texts.at("ID").label.setPosition({5.f, 5.f});
|
||||||
|
texts.at("ID").value.setPosition({80.f, 5.f});
|
||||||
texts["ID"].label.setString("<Not Connected>");
|
|
||||||
texts["ID"].value.setString("");
|
|
||||||
|
|
||||||
// Set up our threshold sf::Text objects
|
// Set up our threshold sf::Text objects
|
||||||
sstr.str("");
|
sstr.str("");
|
||||||
sstr << threshold << " (Change with up/down arrow keys)";
|
sstr << threshold << " (Change with up/down arrow keys)";
|
||||||
|
|
||||||
texts["Threshold"].label.setPosition({5.f, 5.f + 2 * font.getLineSpacing(14)});
|
texts.emplace("Threshold", JoystickObject{{"Threshold:", font}, {sstr.str(), font}});
|
||||||
texts["Threshold"].value.setPosition({80.f, 5.f + 2 * font.getLineSpacing(14)});
|
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)});
|
||||||
texts["Threshold"].label.setString("Threshold:");
|
|
||||||
texts["Threshold"].value.setString(sstr.str());
|
|
||||||
|
|
||||||
// Set up our label-value sf::Text objects
|
// Set up our label-value sf::Text objects
|
||||||
for (unsigned int i = 0; i < sf::Joystick::AxisCount; ++i)
|
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.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.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)
|
for (unsigned int i = 0; i < sf::Joystick::ButtonCount; ++i)
|
||||||
{
|
{
|
||||||
sstr.str("");
|
sstr.str("");
|
||||||
sstr << "Button " << i;
|
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(
|
object.label.setPosition(
|
||||||
{5.f, 5.f + (static_cast<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))});
|
{5.f, 5.f + (static_cast<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))});
|
||||||
object.label.setString(sstr.str() + ":");
|
|
||||||
|
|
||||||
object.value.setPosition(
|
object.value.setPosition(
|
||||||
{80.f, 5.f + (static_cast<float>(sf::Joystick::AxisCount + i + 4) * font.getLineSpacing(14))});
|
{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)
|
for (auto& [label, joystickObject] : texts)
|
||||||
{
|
{
|
||||||
joystickObject.label.setFont(font);
|
|
||||||
joystickObject.label.setCharacterSize(14);
|
joystickObject.label.setCharacterSize(14);
|
||||||
joystickObject.label.setFillColor(sf::Color::White);
|
|
||||||
|
|
||||||
joystickObject.value.setFont(font);
|
|
||||||
joystickObject.value.setCharacterSize(14);
|
joystickObject.value.setCharacterSize(14);
|
||||||
joystickObject.value.setFillColor(sf::Color::White);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update initially displayed joystick values if a joystick is already connected on startup
|
// Update initially displayed joystick values if a joystick is already connected on startup
|
||||||
@ -191,13 +176,13 @@ int main()
|
|||||||
for (auto& [label, joystickObject] : texts)
|
for (auto& [label, joystickObject] : texts)
|
||||||
joystickObject.value.setString("N/A");
|
joystickObject.value.setString("N/A");
|
||||||
|
|
||||||
texts["ID"].label.setString("<Not Connected>");
|
texts.at("ID").label.setString("<Not Connected>");
|
||||||
texts["ID"].value.setString("");
|
texts.at("ID").value.setString("");
|
||||||
|
|
||||||
sstr.str("");
|
sstr.str("");
|
||||||
sstr << threshold << " (Change with up/down arrow keys)";
|
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.str("");
|
||||||
sstr << threshold << " (Change with up/down arrow keys)";
|
sstr << threshold << " (Change with up/down arrow keys)";
|
||||||
|
|
||||||
texts["Threshold"].value.setString(sstr.str());
|
texts.at("Threshold").value.setString(sstr.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the window
|
// Clear the window
|
||||||
|
Loading…
Reference in New Issue
Block a user