Use switch statement instead of if-else-chain

Related to #1739
This commit is contained in:
Camilla Löwy 2021-07-19 21:02:30 +02:00
parent 309d79376f
commit f4d0365a5a
2 changed files with 110 additions and 92 deletions

View File

@ -522,90 +522,102 @@ GLFWAPI void glfwSetInputMode(GLFWwindow* handle, int mode, int value)
_GLFW_REQUIRE_INIT(); _GLFW_REQUIRE_INIT();
if (mode == GLFW_CURSOR) switch (mode)
{ {
if (value != GLFW_CURSOR_NORMAL && case GLFW_CURSOR:
value != GLFW_CURSOR_HIDDEN &&
value != GLFW_CURSOR_DISABLED)
{ {
_glfwInputError(GLFW_INVALID_ENUM, if (value != GLFW_CURSOR_NORMAL &&
"Invalid cursor mode 0x%08X", value != GLFW_CURSOR_HIDDEN &&
value); value != GLFW_CURSOR_DISABLED)
return;
}
if (window->cursorMode == value)
return;
window->cursorMode = value;
_glfw.platform.getCursorPos(window,
&window->virtualCursorPosX,
&window->virtualCursorPosY);
_glfw.platform.setCursorMode(window, value);
}
else if (mode == GLFW_STICKY_KEYS)
{
value = value ? GLFW_TRUE : GLFW_FALSE;
if (window->stickyKeys == value)
return;
if (!value)
{
int i;
// Release all sticky keys
for (i = 0; i <= GLFW_KEY_LAST; i++)
{ {
if (window->keys[i] == _GLFW_STICK) _glfwInputError(GLFW_INVALID_ENUM,
window->keys[i] = GLFW_RELEASE; "Invalid cursor mode 0x%08X",
value);
return;
} }
if (window->cursorMode == value)
return;
window->cursorMode = value;
_glfw.platform.getCursorPos(window,
&window->virtualCursorPosX,
&window->virtualCursorPosY);
_glfw.platform.setCursorMode(window, value);
return;
} }
window->stickyKeys = value; case GLFW_STICKY_KEYS:
}
else if (mode == GLFW_STICKY_MOUSE_BUTTONS)
{
value = value ? GLFW_TRUE : GLFW_FALSE;
if (window->stickyMouseButtons == value)
return;
if (!value)
{ {
int i; value = value ? GLFW_TRUE : GLFW_FALSE;
if (window->stickyKeys == value)
return;
// Release all sticky mouse buttons if (!value)
for (i = 0; i <= GLFW_MOUSE_BUTTON_LAST; i++)
{ {
if (window->mouseButtons[i] == _GLFW_STICK) int i;
window->mouseButtons[i] = GLFW_RELEASE;
// Release all sticky keys
for (i = 0; i <= GLFW_KEY_LAST; i++)
{
if (window->keys[i] == _GLFW_STICK)
window->keys[i] = GLFW_RELEASE;
}
} }
window->stickyKeys = value;
return;
} }
window->stickyMouseButtons = value; case GLFW_STICKY_MOUSE_BUTTONS:
}
else if (mode == GLFW_LOCK_KEY_MODS)
{
window->lockKeyMods = value ? GLFW_TRUE : GLFW_FALSE;
}
else if (mode == GLFW_RAW_MOUSE_MOTION)
{
if (!_glfw.platform.rawMouseMotionSupported())
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, value = value ? GLFW_TRUE : GLFW_FALSE;
"Raw mouse motion is not supported on this system"); if (window->stickyMouseButtons == value)
return;
if (!value)
{
int i;
// Release all sticky mouse buttons
for (i = 0; i <= GLFW_MOUSE_BUTTON_LAST; i++)
{
if (window->mouseButtons[i] == _GLFW_STICK)
window->mouseButtons[i] = GLFW_RELEASE;
}
}
window->stickyMouseButtons = value;
return; return;
} }
value = value ? GLFW_TRUE : GLFW_FALSE; case GLFW_LOCK_KEY_MODS:
if (window->rawMouseMotion == value) {
window->lockKeyMods = value ? GLFW_TRUE : GLFW_FALSE;
return; return;
}
window->rawMouseMotion = value; case GLFW_RAW_MOUSE_MOTION:
_glfw.platform.setRawMouseMotion(window, value); {
if (!_glfw.platform.rawMouseMotionSupported())
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Raw mouse motion is not supported on this system");
return;
}
value = value ? GLFW_TRUE : GLFW_FALSE;
if (window->rawMouseMotion == value)
return;
window->rawMouseMotion = value;
_glfw.platform.setRawMouseMotion(window, value);
return;
}
} }
else
_glfwInputError(GLFW_INVALID_ENUM, "Invalid input mode 0x%08X", mode); _glfwInputError(GLFW_INVALID_ENUM, "Invalid input mode 0x%08X", mode);
} }
GLFWAPI int glfwRawMouseMotionSupported(void) GLFWAPI int glfwRawMouseMotionSupported(void)

View File

@ -882,35 +882,41 @@ GLFWAPI void glfwSetWindowAttrib(GLFWwindow* handle, int attrib, int value)
value = value ? GLFW_TRUE : GLFW_FALSE; value = value ? GLFW_TRUE : GLFW_FALSE;
if (attrib == GLFW_AUTO_ICONIFY) switch (attrib)
window->autoIconify = value;
else if (attrib == GLFW_RESIZABLE)
{ {
window->resizable = value; case GLFW_AUTO_ICONIFY:
if (!window->monitor) window->autoIconify = value;
_glfw.platform.setWindowResizable(window, value); return;
case GLFW_RESIZABLE:
window->resizable = value;
if (!window->monitor)
_glfw.platform.setWindowResizable(window, value);
return;
case GLFW_DECORATED:
window->decorated = value;
if (!window->monitor)
_glfw.platform.setWindowDecorated(window, value);
return;
case GLFW_FLOATING:
window->floating = value;
if (!window->monitor)
_glfw.platform.setWindowFloating(window, value);
return;
case GLFW_FOCUS_ON_SHOW:
window->focusOnShow = value;
return;
case GLFW_MOUSE_PASSTHROUGH:
window->mousePassthrough = value;
_glfw.platform.setWindowMousePassthrough(window, value);
return;
} }
else if (attrib == GLFW_DECORATED)
{ _glfwInputError(GLFW_INVALID_ENUM, "Invalid window attribute 0x%08X", attrib);
window->decorated = value;
if (!window->monitor)
_glfw.platform.setWindowDecorated(window, value);
}
else if (attrib == GLFW_FLOATING)
{
window->floating = value;
if (!window->monitor)
_glfw.platform.setWindowFloating(window, value);
}
else if (attrib == GLFW_FOCUS_ON_SHOW)
window->focusOnShow = value;
else if (attrib == GLFW_MOUSE_PASSTHROUGH)
{
window->mousePassthrough = value;
_glfw.platform.setWindowMousePassthrough(window, value);
}
else
_glfwInputError(GLFW_INVALID_ENUM, "Invalid window attribute 0x%08X", attrib);
} }
GLFWAPI GLFWmonitor* glfwGetWindowMonitor(GLFWwindow* handle) GLFWAPI GLFWmonitor* glfwGetWindowMonitor(GLFWwindow* handle)