MCPcopy Create free account
hub / github.com/SFML/SFML / processJoystickEvents

Method processJoystickEvents

src/SFML/Window/WindowImpl.cpp:287–348  ·  view source on GitHub ↗

///////////////////////////////////////////////////////

Source from the content-addressed store, hash-verified

285
286////////////////////////////////////////////////////////////
287void WindowImpl::processJoystickEvents()
288{
289 // First update the global joystick states
290 JoystickManager::getInstance().update();
291
292 for (unsigned int i = 0; i < Joystick::Count; ++i)
293 {
294 // Copy the previous state of the joystick and get the new one
295 const JoystickState previousState = m_joystickStatesImpl->states[i];
296 m_joystickStatesImpl->states[i] = JoystickManager::getInstance().getState(i);
297
298 // Connection state
299 const bool connected = m_joystickStatesImpl->states[i].connected;
300 if (previousState.connected != connected)
301 {
302 if (connected)
303 pushEvent(Event::JoystickConnected{i});
304 else
305 pushEvent(Event::JoystickDisconnected{i});
306
307 // Clear previous axes positions
308 if (connected)
309 m_previousAxes[i].fill(0.f);
310 }
311
312 if (connected)
313 {
314 const JoystickCaps caps = JoystickManager::getInstance().getCapabilities(i);
315
316 // Axes
317 for (unsigned int j = 0; j < Joystick::AxisCount; ++j)
318 {
319 const auto axis = static_cast<Joystick::Axis>(j);
320 if (caps.axes[axis])
321 {
322 const float prevPos = m_previousAxes[i][axis];
323 const float currPos = m_joystickStatesImpl->states[i].axes[axis];
324 if (std::abs(currPos - prevPos) >= m_joystickThreshold)
325 {
326 pushEvent(Event::JoystickMoved{i, axis, currPos});
327 m_previousAxes[i][axis] = currPos;
328 }
329 }
330 }
331
332 // Buttons
333 for (unsigned int j = 0; j < caps.buttonCount; ++j)
334 {
335 const bool prevPressed = previousState.buttons[j];
336 const bool currPressed = m_joystickStatesImpl->states[i].buttons[j];
337
338 if (prevPressed != currPressed)
339 {
340 if (currPressed)
341 pushEvent(Event::JoystickButtonPressed{i, j});
342 else
343 pushEvent(Event::JoystickButtonReleased{i, j});
344 }

Callers

nothing calls this directly

Calls 3

pushEventFunction · 0.85
updateMethod · 0.45
getCapabilitiesMethod · 0.45

Tested by

no test coverage detected