@@ -180,19 +180,14 @@ void RenderDrawLists(ImDrawData* draw_data); // rendering callback function prot
180
180
// Default mapping is XInput gamepad mapping
181
181
void initDefaultJoystickMapping ();
182
182
183
- // data
184
- constexpr unsigned int NULL_JOYSTICK_ID = sf::Joystick::Count;
185
-
186
183
// Returns first id of connected joystick
187
- [[nodiscard]] unsigned int getConnectedJoystickId ()
184
+ [[nodiscard]] std::optional< unsigned int > getConnectedJoystickId ()
188
185
{
189
186
for (unsigned int i = 0 ; i < sf::Joystick::Count; ++i)
190
- {
191
187
if (sf::Joystick::isConnected (i))
192
188
return i;
193
- }
194
189
195
- return NULL_JOYSTICK_ID ;
190
+ return std::nullopt ;
196
191
}
197
192
198
193
void updateJoystickButtonState (ImGuiIO& io);
@@ -254,13 +249,13 @@ struct WindowContext
254
249
bool touchDown[3 ] = {false };
255
250
sf::Vector2i touchPos;
256
251
257
- unsigned int joystickId{getConnectedJoystickId ()};
258
- ImGuiKey joystickMapping[sf::Joystick::ButtonCount] = {ImGuiKey_None};
259
- StickInfo dPadInfo;
260
- StickInfo lStickInfo;
261
- StickInfo rStickInfo;
262
- TriggerInfo lTriggerInfo;
263
- TriggerInfo rTriggerInfo;
252
+ std::optional< unsigned int > joystickId{getConnectedJoystickId ()};
253
+ ImGuiKey joystickMapping[sf::Joystick::ButtonCount] = {ImGuiKey_None};
254
+ StickInfo dPadInfo;
255
+ StickInfo lStickInfo;
256
+ StickInfo rStickInfo;
257
+ TriggerInfo lTriggerInfo;
258
+ TriggerInfo rTriggerInfo;
264
259
265
260
std::optional<sf::Cursor> mouseCursors[ImGuiMouseCursor_COUNT];
266
261
@@ -453,7 +448,7 @@ void ProcessEvent(const sf::Window& window, const sf::Event& event)
453
448
}
454
449
else if (const auto * joystickConnected = event.getIf <sf::Event::JoystickConnected>())
455
450
{
456
- if (s_currWindowCtx->joystickId == NULL_JOYSTICK_ID )
451
+ if (! s_currWindowCtx->joystickId . has_value () )
457
452
s_currWindowCtx->joystickId = joystickConnected->joystickId ;
458
453
}
459
454
else if (const auto * joystickDisconnected = event.getIf <sf::Event::JoystickDisconnected>())
@@ -554,7 +549,7 @@ void Update(const sf::Vector2i& mousePos, const sf::Vector2f& displaySize, sf::T
554
549
// atlas (see createFontTexture)
555
550
556
551
// gamepad navigation
557
- if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) && s_currWindowCtx->joystickId != NULL_JOYSTICK_ID )
552
+ if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) && s_currWindowCtx->joystickId . has_value () )
558
553
{
559
554
updateJoystickButtonState (io);
560
555
updateJoystickDPadState (io);
@@ -1117,7 +1112,8 @@ void updateJoystickButtonState(ImGuiIO& io)
1117
1112
const ImGuiKey key = s_currWindowCtx->joystickMapping [i];
1118
1113
if (key != ImGuiKey_None)
1119
1114
{
1120
- const bool isPressed = sf::Joystick::isButtonPressed (s_currWindowCtx->joystickId , static_cast <unsigned >(i));
1115
+ const bool isPressed = sf::Joystick::isButtonPressed (s_currWindowCtx->joystickId .value (),
1116
+ static_cast <unsigned >(i));
1121
1117
if (s_currWindowCtx->windowHasFocus || !isPressed)
1122
1118
{
1123
1119
io.AddKeyEvent (key, isPressed);
@@ -1128,7 +1124,7 @@ void updateJoystickButtonState(ImGuiIO& io)
1128
1124
1129
1125
void updateJoystickAxis (ImGuiIO& io, ImGuiKey key, sf::Joystick::Axis axis, float threshold, float maxThreshold, bool inverted)
1130
1126
{
1131
- float pos = sf::Joystick::getAxisPosition (s_currWindowCtx->joystickId , axis);
1127
+ float pos = sf::Joystick::getAxisPosition (s_currWindowCtx->joystickId . value () , axis);
1132
1128
if (inverted)
1133
1129
{
1134
1130
pos = -pos;
0 commit comments