Skip to content

Commit d9f4825

Browse files
committed
Use std::optional to represent nullable data
1 parent 5e8f8fb commit d9f4825

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

imgui-SFML.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,14 @@ void RenderDrawLists(ImDrawData* draw_data); // rendering callback function prot
180180
// Default mapping is XInput gamepad mapping
181181
void initDefaultJoystickMapping();
182182

183-
// data
184-
constexpr unsigned int NULL_JOYSTICK_ID = sf::Joystick::Count;
185-
186183
// Returns first id of connected joystick
187-
[[nodiscard]] unsigned int getConnectedJoystickId()
184+
[[nodiscard]] std::optional<unsigned int> getConnectedJoystickId()
188185
{
189186
for (unsigned int i = 0; i < sf::Joystick::Count; ++i)
190-
{
191187
if (sf::Joystick::isConnected(i))
192188
return i;
193-
}
194189

195-
return NULL_JOYSTICK_ID;
190+
return std::nullopt;
196191
}
197192

198193
void updateJoystickButtonState(ImGuiIO& io);
@@ -254,13 +249,13 @@ struct WindowContext
254249
bool touchDown[3] = {false};
255250
sf::Vector2i touchPos;
256251

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;
264259

265260
std::optional<sf::Cursor> mouseCursors[ImGuiMouseCursor_COUNT];
266261

@@ -453,7 +448,7 @@ void ProcessEvent(const sf::Window& window, const sf::Event& event)
453448
}
454449
else if (const auto* joystickConnected = event.getIf<sf::Event::JoystickConnected>())
455450
{
456-
if (s_currWindowCtx->joystickId == NULL_JOYSTICK_ID)
451+
if (!s_currWindowCtx->joystickId.has_value())
457452
s_currWindowCtx->joystickId = joystickConnected->joystickId;
458453
}
459454
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
554549
// atlas (see createFontTexture)
555550

556551
// 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())
558553
{
559554
updateJoystickButtonState(io);
560555
updateJoystickDPadState(io);
@@ -1117,7 +1112,8 @@ void updateJoystickButtonState(ImGuiIO& io)
11171112
const ImGuiKey key = s_currWindowCtx->joystickMapping[i];
11181113
if (key != ImGuiKey_None)
11191114
{
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));
11211117
if (s_currWindowCtx->windowHasFocus || !isPressed)
11221118
{
11231119
io.AddKeyEvent(key, isPressed);
@@ -1128,7 +1124,7 @@ void updateJoystickButtonState(ImGuiIO& io)
11281124

11291125
void updateJoystickAxis(ImGuiIO& io, ImGuiKey key, sf::Joystick::Axis axis, float threshold, float maxThreshold, bool inverted)
11301126
{
1131-
float pos = sf::Joystick::getAxisPosition(s_currWindowCtx->joystickId, axis);
1127+
float pos = sf::Joystick::getAxisPosition(s_currWindowCtx->joystickId.value(), axis);
11321128
if (inverted)
11331129
{
11341130
pos = -pos;

0 commit comments

Comments
 (0)