Split up Input_SetPressed into Input_Set/Input_SetPressed/Input_SetReleased

This commit is contained in:
UnknownShadow200 2020-11-23 22:40:30 +11:00
parent 9578de501a
commit 64d9a03aff
5 changed files with 85 additions and 73 deletions

View File

@ -545,7 +545,7 @@ static int MeasureBitmappedWidth(const struct DrawTextArgs* args) {
/* TODO: this should be always done? */ /* TODO: this should be always done? */
/* Remove padding at end */ /* Remove padding at end */
if (width && (args->font->flags & FONT_FLAGS_NOPADDING)) width -= xPadding; if (width) width -= xPadding;
if (args->useShadow) { width += Drawer2D_ShadowOffset(point); } if (args->useShadow) { width += Drawer2D_ShadowOffset(point); }
return width; return width;

View File

@ -217,25 +217,36 @@ const char* const Input_Names[INPUT_COUNT] = {
"XBUTTON1", "XBUTTON2, "MMOUSE" "XBUTTON1", "XBUTTON2, "MMOUSE"
};*/ };*/
void Input_SetPressed(int key, cc_bool pressed) { void Input_SetPressed(int key) {
cc_bool wasPressed = Input_Pressed[key]; cc_bool wasPressed = Input_Pressed[key];
Input_Pressed[key] = pressed; Input_Pressed[key] = true;
Event_RaiseInput(&InputEvents.Down, key, wasPressed);
if (pressed) {
Event_RaiseInput(&InputEvents.Down, key, wasPressed);
} else if (wasPressed) {
Event_RaiseInt(&InputEvents.Up, key);
}
/* don't allow multiple left mouse down events */ /* don't allow multiple left mouse down events */
if (key != KEY_LMOUSE || pressed == wasPressed) return; if (key != KEY_LMOUSE || wasPressed) return;
Pointer_SetPressed(0, pressed); Pointer_SetPressed(0, true);
}
void Input_SetReleased(int key) {
if (!Input_Pressed[key]) return;
Input_Pressed[key] = false;
Event_RaiseInt(&InputEvents.Up, key);
if (key == KEY_LMOUSE) Pointer_SetPressed(0, false);
}
void Input_Set(int key, int pressed) {
if (pressed) {
Input_SetPressed(key);
} else {
Input_SetReleased(key);
}
} }
void Input_Clear(void) { void Input_Clear(void) {
int i; int i;
for (i = 0; i < INPUT_COUNT; i++) { for (i = 0; i < INPUT_COUNT; i++) {
if (Input_Pressed[i]) Input_SetPressed(i, false); if (Input_Pressed[i]) Input_SetReleased(i);
} }
} }

View File

@ -62,14 +62,15 @@ extern const char* const Input_Names[INPUT_COUNT];
#define Key_IsActionPressed() Key_IsControlPressed() #define Key_IsActionPressed() Key_IsControlPressed()
#endif #endif
/* Pressed state of each keyboard button. Use Input_SetPressed to change. */ /* Pressed state of each keyboard button. Use Input_Set to change. */
extern cc_bool Input_Pressed[INPUT_COUNT]; extern cc_bool Input_Pressed[INPUT_COUNT];
/* Sets the pressed state of a keyboard button. */ /* Sets Input_Pressed[key] to true and raises InputEvents.Down */
/* Raises InputEvents.Up if not pressed, but was pressed before. */ void Input_SetPressed(int key);
/* Raises InputEvents.Down if pressed (repeating is whether it was pressed before) */ /* Sets Input_Pressed[key] to false and raises InputEvents.Up */
void Input_SetPressed(int key, cc_bool pressed); void Input_SetReleased(int key);
/* Resets all keyboard buttons to released state. */ /* Calls either Input_SetPressed or Input_SetReleased */
/* Raises InputEvents.Up for each previously pressed button. */ void Input_Set(int key, int pressed);
/* Resets all keyboard buttons to released state. (Input_SetReleased) */
void Input_Clear(void); void Input_Clear(void);
/* Whether raw mouse/touch input is currently being listened for. */ /* Whether raw mouse/touch input is currently being listened for. */

View File

@ -1938,7 +1938,7 @@ static void TouchScreen_OnscreenClick(void* screen, void* widget) {
struct TouchScreen* s = (struct TouchScreen*)screen; struct TouchScreen* s = (struct TouchScreen*)screen;
int i = Screen_Index(screen, widget); int i = Screen_Index(screen, widget);
int key = KeyBinds[s->onscreenDescs[i]->bind]; int key = KeyBinds[s->onscreenDescs[i]->bind];
Input_SetPressed(KeyBinds[s->onscreenDescs[i]->bind], !Input_Pressed[key]); Input_Set(KeyBinds[s->onscreenDescs[i]->bind], !Input_Pressed[key]);
} }
static void TouchScreen_ChatClick(void* s, void* w) { ChatScreen_OpenInput(&String_Empty); } static void TouchScreen_ChatClick(void* s, void* w) { ChatScreen_OpenInput(&String_Empty); }
@ -1959,7 +1959,7 @@ static void TouchScreen_TabClick(void* s, void* w) {
static void TouchScreen_BindClick(void* screen, void* widget) { static void TouchScreen_BindClick(void* screen, void* widget) {
struct TouchScreen* s = (struct TouchScreen*)screen; struct TouchScreen* s = (struct TouchScreen*)screen;
int i = Screen_Index(screen, widget) - ONSCREEN_MAX_BTNS; int i = Screen_Index(screen, widget) - ONSCREEN_MAX_BTNS;
Input_SetPressed(KeyBinds[s->descs[i].bind], true); Input_Set(KeyBinds[s->descs[i].bind], true);
} }
static const struct TouchButtonDesc onscreenDescs[8] = { static const struct TouchButtonDesc onscreenDescs[8] = {
@ -2078,7 +2078,7 @@ static int TouchScreen_PointerUp(void* screen, int id, int x, int y) {
if (!(s->btns[i].active & id)) continue; if (!(s->btns[i].active & id)) continue;
if (s->descs[i].bind < KEYBIND_COUNT) { if (s->descs[i].bind < KEYBIND_COUNT) {
Input_SetPressed(KeyBinds[s->descs[i].bind], false); Input_Set(KeyBinds[s->descs[i].bind], false);
} }
s->btns[i].active &= ~id; s->btns[i].active &= ~id;
return true; return true;

View File

@ -266,7 +266,7 @@ static int MapNativeKey(SDL_Keycode k) {
static void OnKeyEvent(const SDL_Event* e) { static void OnKeyEvent(const SDL_Event* e) {
cc_bool pressed = e->key.state == SDL_PRESSED; cc_bool pressed = e->key.state == SDL_PRESSED;
int key = MapNativeKey(e->key.keysym.sym); int key = MapNativeKey(e->key.keysym.sym);
if (key) Input_SetPressed(key, pressed); if (key) Input_Set(key, pressed);
} }
static void OnMouseEvent(const SDL_Event* e) { static void OnMouseEvent(const SDL_Event* e) {
@ -280,7 +280,7 @@ static void OnMouseEvent(const SDL_Event* e) {
case SDL_BUTTON_X2: btn = KEY_XBUTTON2; break; case SDL_BUTTON_X2: btn = KEY_XBUTTON2; break;
default: return; default: return;
} }
Input_SetPressed(btn, pressed); Input_Set(btn, pressed);
} }
static void OnTextEvent(const SDL_Event* e) { static void OnTextEvent(const SDL_Event* e) {
@ -552,9 +552,9 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
/* Set before position change, in case mouse buttons changed when outside window */ /* Set before position change, in case mouse buttons changed when outside window */
Input_SetPressed(KEY_LMOUSE, (wParam & 0x01) != 0); Input_Set(KEY_LMOUSE, wParam & 0x01);
Input_SetPressed(KEY_RMOUSE, (wParam & 0x02) != 0); Input_Set(KEY_RMOUSE, wParam & 0x02);
Input_SetPressed(KEY_MMOUSE, (wParam & 0x10) != 0); Input_Set(KEY_MMOUSE, wParam & 0x10);
/* TODO: do we need to set XBUTTON1/XBUTTON2 here */ /* TODO: do we need to set XBUTTON1/XBUTTON2 here */
Pointer_SetPosition(0, LOWORD(lParam), HIWORD(lParam)); Pointer_SetPosition(0, LOWORD(lParam), HIWORD(lParam));
break; break;
@ -565,23 +565,23 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara
return 0; return 0;
case WM_LBUTTONDOWN: case WM_LBUTTONDOWN:
Input_SetPressed(KEY_LMOUSE, true); break; Input_SetPressed(KEY_LMOUSE); break;
case WM_MBUTTONDOWN: case WM_MBUTTONDOWN:
Input_SetPressed(KEY_MMOUSE, true); break; Input_SetPressed(KEY_MMOUSE); break;
case WM_RBUTTONDOWN: case WM_RBUTTONDOWN:
Input_SetPressed(KEY_RMOUSE, true); break; Input_SetPressed(KEY_RMOUSE); break;
case WM_XBUTTONDOWN: case WM_XBUTTONDOWN:
Input_SetPressed(HIWORD(wParam) == 1 ? KEY_XBUTTON1 : KEY_XBUTTON2, true); Input_SetPressed(HIWORD(wParam) == 1 ? KEY_XBUTTON1 : KEY_XBUTTON2);
break; break;
case WM_LBUTTONUP: case WM_LBUTTONUP:
Input_SetPressed(KEY_LMOUSE, false); break; Input_SetReleased(KEY_LMOUSE); break;
case WM_MBUTTONUP: case WM_MBUTTONUP:
Input_SetPressed(KEY_MMOUSE, false); break; Input_SetReleased(KEY_MMOUSE); break;
case WM_RBUTTONUP: case WM_RBUTTONUP:
Input_SetPressed(KEY_RMOUSE, false); break; Input_SetReleased(KEY_RMOUSE); break;
case WM_XBUTTONUP: case WM_XBUTTONUP:
Input_SetPressed(HIWORD(wParam) == 1 ? KEY_XBUTTON1 : KEY_XBUTTON2, false); Input_SetReleased(HIWORD(wParam) == 1 ? KEY_XBUTTON1 : KEY_XBUTTON2);
break; break;
case WM_INPUT: case WM_INPUT:
@ -631,12 +631,12 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara
rShiftDown = ((USHORT)GetKeyState(VK_RSHIFT)) >> 15; rShiftDown = ((USHORT)GetKeyState(VK_RSHIFT)) >> 15;
if (!pressed || lShiftDown != rShiftDown) { if (!pressed || lShiftDown != rShiftDown) {
Input_SetPressed(KEY_LSHIFT, lShiftDown); Input_Set(KEY_LSHIFT, lShiftDown);
Input_SetPressed(KEY_RSHIFT, rShiftDown); Input_Set(KEY_RSHIFT, rShiftDown);
} }
} else { } else {
key = MapNativeKey(wParam, lParam); key = MapNativeKey(wParam, lParam);
if (key) Input_SetPressed(key, pressed); if (key) Input_Set(key, pressed);
else Platform_Log1("Unknown key: %x", &wParam); else Platform_Log1("Unknown key: %x", &wParam);
} }
return 0; return 0;
@ -1357,7 +1357,7 @@ static void Window_ToggleKey(XKeyEvent* ev, cc_bool pressed) {
if (!key) key = MapNativeKey(keysym2, ev->state); if (!key) key = MapNativeKey(keysym2, ev->state);
if (key) { if (key) {
Input_SetPressed(key, pressed); Input_Set(key, pressed);
} else { } else {
Platform_Log2("Unknown key: (%x, %x)", &keysym1, &keysym2); Platform_Log2("Unknown key: (%x, %x)", &keysym1, &keysym2);
} }
@ -1461,14 +1461,14 @@ void Window_ProcessEvents(void) {
case ButtonPress: case ButtonPress:
btn = MapNativeMouse(e.xbutton.button); btn = MapNativeMouse(e.xbutton.button);
if (btn) Input_SetPressed(btn, true); if (btn) Input_SetPressed(btn);
else if (e.xbutton.button == 4) Mouse_ScrollWheel(+1); else if (e.xbutton.button == 4) Mouse_ScrollWheel(+1);
else if (e.xbutton.button == 5) Mouse_ScrollWheel(-1); else if (e.xbutton.button == 5) Mouse_ScrollWheel(-1);
break; break;
case ButtonRelease: case ButtonRelease:
btn = MapNativeMouse(e.xbutton.button); btn = MapNativeMouse(e.xbutton.button);
if (btn) Input_SetPressed(btn, false); if (btn) Input_SetReleased(btn);
break; break;
case MotionNotify: case MotionNotify:
@ -2135,7 +2135,7 @@ static OSStatus Window_ProcessKeyboardEvent(EventRef inEvent) {
key = MapNativeKey(code); key = MapNativeKey(code);
if (key) { if (key) {
Input_SetPressed(key, kind != kEventRawKeyUp); Input_Set(key, kind != kEventRawKeyUp);
} else { } else {
Platform_Log1("Ignoring unknown key %i", &code); Platform_Log1("Ignoring unknown key %i", &code);
} }
@ -2146,11 +2146,11 @@ static OSStatus Window_ProcessKeyboardEvent(EventRef inEvent) {
NULL, sizeof(UInt32), NULL, &code); NULL, sizeof(UInt32), NULL, &code);
if (res) Logger_Abort2(res, "Getting key modifiers"); if (res) Logger_Abort2(res, "Getting key modifiers");
Input_SetPressed(KEY_LCTRL, (code & 0x1000) != 0); Input_Set(KEY_LCTRL, code & 0x1000);
Input_SetPressed(KEY_LALT, (code & 0x0800) != 0); Input_Set(KEY_LALT, code & 0x0800);
Input_SetPressed(KEY_LSHIFT, (code & 0x0200) != 0); Input_Set(KEY_LSHIFT, code & 0x0200);
Input_SetPressed(KEY_LWIN, (code & 0x0100) != 0); Input_Set(KEY_LWIN, code & 0x0100);
Input_SetPressed(KEY_CAPSLOCK, (code & 0x0400) != 0); Input_Set(KEY_CAPSLOCK, code & 0x0400);
return eventNotHandledErr; return eventNotHandledErr;
} }
return eventNotHandledErr; return eventNotHandledErr;
@ -2241,11 +2241,11 @@ static OSStatus Window_ProcessMouseEvent(EventRef inEvent) {
switch (button) { switch (button) {
case kEventMouseButtonPrimary: case kEventMouseButtonPrimary:
Input_SetPressed(KEY_LMOUSE, down); break; Input_Set(KEY_LMOUSE, down); break;
case kEventMouseButtonSecondary: case kEventMouseButtonSecondary:
Input_SetPressed(KEY_RMOUSE, down); break; Input_Set(KEY_RMOUSE, down); break;
case kEventMouseButtonTertiary: case kEventMouseButtonTertiary:
Input_SetPressed(KEY_MMOUSE, down); break; Input_Set(KEY_MMOUSE, down); break;
} }
return eventNotHandledErr; return eventNotHandledErr;
@ -2735,7 +2735,7 @@ static Class Window_MakeClass(void) {
/* This causes the game to get stuck with left mouse down after user finishes resizing */ /* This causes the game to get stuck with left mouse down after user finishes resizing */
/* So work arond that by always releasing left mouse when a live resize is finished */ /* So work arond that by always releasing left mouse when a live resize is finished */
static void DidEndLiveResize(id self, SEL cmd) { static void DidEndLiveResize(id self, SEL cmd) {
Input_SetPressed(KEY_LMOUSE, false); Input_SetReleased(KEY_LMOUSE);
} }
static void View_DrawRect(id self, SEL cmd, CGRect r); static void View_DrawRect(id self, SEL cmd, CGRect r);
@ -2932,40 +2932,40 @@ void Window_ProcessEvents(void) {
case 3: /* NSRightMouseDown */ case 3: /* NSRightMouseDown */
case 25: /* NSOtherMouseDown */ case 25: /* NSOtherMouseDown */
key = MapNativeMouse((int)objc_msgSend(ev, selButton)); key = MapNativeMouse((int)objc_msgSend(ev, selButton));
if (GetMouseCoords(&x, &y) && key) Input_SetPressed(key, true); if (GetMouseCoords(&x, &y) && key) Input_SetPressed(key);
break; break;
case 2: /* NSLeftMouseUp */ case 2: /* NSLeftMouseUp */
case 4: /* NSRightMouseUp */ case 4: /* NSRightMouseUp */
case 26: /* NSOtherMouseUp */ case 26: /* NSOtherMouseUp */
key = MapNativeMouse((int)objc_msgSend(ev, selButton)); key = MapNativeMouse((int)objc_msgSend(ev, selButton));
if (key) Input_SetPressed(key, false); if (key) Input_SetReleased(key);
break; break;
case 10: /* NSKeyDown */ case 10: /* NSKeyDown */
key = MapNativeKey((int)objc_msgSend(ev, selKeycode)); key = MapNativeKey((int)objc_msgSend(ev, selKeycode));
if (key) Input_SetPressed(key, true); if (key) Input_SetPressed(key);
// TODO: Test works properly with other languages // TODO: Test works properly with other languages
ProcessKeyChars(ev); ProcessKeyChars(ev);
break; break;
case 11: /* NSKeyUp */ case 11: /* NSKeyUp */
key = MapNativeKey((int)objc_msgSend(ev, selKeycode)); key = MapNativeKey((int)objc_msgSend(ev, selKeycode));
if (key) Input_SetPressed(key, false); if (key) Input_SetReleased(key);
break; break;
case 12: /* NSFlagsChanged */ case 12: /* NSFlagsChanged */
key = (int)objc_msgSend(ev, selModifiers); key = (int)objc_msgSend(ev, selModifiers);
/* TODO: Figure out how to only get modifiers that changed */ /* TODO: Figure out how to only get modifiers that changed */
Input_SetPressed(KEY_LCTRL, (key & 0x000001) != 0); Input_Set(KEY_LCTRL, key & 0x000001);
Input_SetPressed(KEY_LSHIFT, (key & 0x000002) != 0); Input_Set(KEY_LSHIFT, key & 0x000002);
Input_SetPressed(KEY_RSHIFT, (key & 0x000004) != 0); Input_Set(KEY_RSHIFT, key & 0x000004);
Input_SetPressed(KEY_LWIN, (key & 0x000008) != 0); Input_Set(KEY_LWIN, key & 0x000008);
Input_SetPressed(KEY_RWIN, (key & 0x000010) != 0); Input_Set(KEY_RWIN, key & 0x000010);
Input_SetPressed(KEY_LALT, (key & 0x000020) != 0); Input_Set(KEY_LALT, key & 0x000020);
Input_SetPressed(KEY_RALT, (key & 0x000040) != 0); Input_Set(KEY_RALT, key & 0x000040);
Input_SetPressed(KEY_RCTRL, (key & 0x002000) != 0); Input_Set(KEY_RCTRL, key & 0x002000);
Input_SetPressed(KEY_CAPSLOCK, (key & 0x010000) != 0); Input_Set(KEY_CAPSLOCK, key & 0x010000);
break; break;
case 22: /* NSScrollWheel */ case 22: /* NSScrollWheel */
@ -3124,9 +3124,9 @@ static EM_BOOL OnMouseWheel(int type, const EmscriptenWheelEvent* ev, void* data
static EM_BOOL OnMouseButton(int type, const EmscriptenMouseEvent* ev, void* data) { static EM_BOOL OnMouseButton(int type, const EmscriptenMouseEvent* ev, void* data) {
cc_bool down = type == EMSCRIPTEN_EVENT_MOUSEDOWN; cc_bool down = type == EMSCRIPTEN_EVENT_MOUSEDOWN;
switch (ev->button) { switch (ev->button) {
case 0: Input_SetPressed(KEY_LMOUSE, down); break; case 0: Input_Set(KEY_LMOUSE, down); break;
case 1: Input_SetPressed(KEY_MMOUSE, down); break; case 1: Input_Set(KEY_MMOUSE, down); break;
case 2: Input_SetPressed(KEY_RMOUSE, down); break; case 2: Input_Set(KEY_RMOUSE, down); break;
} }
DeferredEnableRawMouse(); DeferredEnableRawMouse();
@ -3151,9 +3151,9 @@ static void RescaleXY(int srcX, int srcY, int* dstX, int* dstY) {
static EM_BOOL OnMouseMove(int type, const EmscriptenMouseEvent* ev, void* data) { static EM_BOOL OnMouseMove(int type, const EmscriptenMouseEvent* ev, void* data) {
int x, y, buttons = ev->buttons; int x, y, buttons = ev->buttons;
/* Set before position change, in case mouse buttons changed when outside window */ /* Set before position change, in case mouse buttons changed when outside window */
Input_SetPressed(KEY_LMOUSE, (buttons & 0x01) != 0); Input_Set(KEY_LMOUSE, buttons & 0x01);
Input_SetPressed(KEY_RMOUSE, (buttons & 0x02) != 0); Input_Set(KEY_RMOUSE, buttons & 0x02);
Input_SetPressed(KEY_MMOUSE, (buttons & 0x04) != 0); Input_Set(KEY_MMOUSE, buttons & 0x04);
RescaleXY(ev->targetX, ev->targetY, &x, &y); RescaleXY(ev->targetX, ev->targetY, &x, &y);
Pointer_SetPosition(0, x, y); Pointer_SetPosition(0, x, y);
@ -3303,7 +3303,7 @@ static EM_BOOL OnKey(int type, const EmscriptenKeyboardEvent* ev, void* data) {
} }
} }
if (key) Input_SetPressed(key, type == EMSCRIPTEN_EVENT_KEYDOWN); if (key) Input_Set(key, type == EMSCRIPTEN_EVENT_KEYDOWN);
DeferredEnableRawMouse(); DeferredEnableRawMouse();
if (!key) return false; if (!key) return false;
@ -3765,13 +3765,13 @@ static int MapNativeKey(int code) {
static void JNICALL java_processKeyDown(JNIEnv* env, jobject o, jint code) { static void JNICALL java_processKeyDown(JNIEnv* env, jobject o, jint code) {
int key = MapNativeKey(code); int key = MapNativeKey(code);
Platform_Log2("KEY - DOWN %i,%i", &code, &key); Platform_Log2("KEY - DOWN %i,%i", &code, &key);
if (key) Input_SetPressed(key, true); if (key) Input_SetPressed(key);
} }
static void JNICALL java_processKeyUp(JNIEnv* env, jobject o, jint code) { static void JNICALL java_processKeyUp(JNIEnv* env, jobject o, jint code) {
int key = MapNativeKey(code); int key = MapNativeKey(code);
Platform_Log2("KEY - UP %i,%i", &code, &key); Platform_Log2("KEY - UP %i,%i", &code, &key);
if (key) Input_SetPressed(key, false); if (key) Input_SetReleased(key);
} }
static void JNICALL java_processKeyChar(JNIEnv* env, jobject o, jint code) { static void JNICALL java_processKeyChar(JNIEnv* env, jobject o, jint code) {