Treat left/right mouse as keys too, so hotkeys can be bound to them

This commit is contained in:
UnknownShadow200 2019-08-29 19:31:13 +10:00
parent d82b40bb0b
commit 9505a8c373
9 changed files with 71 additions and 96 deletions

View File

@ -559,9 +559,9 @@ static void Game_Render3D(double delta, float t) {
Selections_Render(delta);
Entities_RenderHoveredNames(delta);
left = InputHandler_IsMousePressed(MOUSE_LEFT);
left = KeyBind_IsPressed(KEYBIND_DELETE_BLOCK);
middle = KeyBind_IsPressed(KEYBIND_PICK_BLOCK);
right = InputHandler_IsMousePressed(MOUSE_RIGHT);
right = KeyBind_IsPressed(KEYBIND_PLACE_BLOCK);
InputHandler_PickBlocks(true, left, middle, right);
if (!Game_HideGui) HeldBlockRenderer_Render(delta);

View File

@ -42,7 +42,7 @@ const char* const Key_Names[KEY_COUNT] = {
"KeypadAdd", "KeypadDecimal", "KeypadEnter",
"Tilde", "Minus", "Plus", "BracketLeft", "BracketRight", "Slash",
"Semicolon", "Quote", "Comma", "Period", "BackSlash",
"XButton1", "XButton2", "MouseMid"
"XButton1", "XButton2", "LeftMouse", "RightMouse", "MiddleMouse"
};
/* TODO: Should this only be shown in GUI? not saved to disc? */
@ -64,7 +64,7 @@ const char* const Key_Names[KEY_COUNT] = {
"5", "6", "7", "8", "9",
"GRAVE", "MINUS", "PLUS", "LBRACKET", "RBRACKET",
"SEMICOLON", "APOSTROPHE", "COMMA", "PERIOD", "SLASH", "BACKSLASH",
"XBUTTON1", "XBUTTON2", "MOUSEMID"
"XBUTTON1", "XBUTTON2", "MMOUSE"
};*/
void Key_SetPressed(Key key, bool pressed) {
@ -76,6 +76,10 @@ void Key_SetPressed(Key key, bool pressed) {
} else if (wasPressed) {
Event_RaiseInt(&KeyEvents.Up, key);
}
/* don't allow multiple left mouse down events */
if (key != KEY_LMOUSE || pressed == wasPressed) return;
Mouse_SetPressed(pressed);
}
void Key_Clear(void) {
@ -93,14 +97,11 @@ float Mouse_Wheel;
int Mouse_X, Mouse_Y;
bool Mouse_Pressed[MOUSE_COUNT];
void Mouse_SetPressed(MouseButton btn, bool pressed) {
if (Mouse_Pressed[btn] == pressed) return;
Mouse_Pressed[btn] = pressed;
void Mouse_SetPressed(bool pressed) {
if (pressed) {
Event_RaiseInt(&MouseEvents.Down, btn);
Event_RaiseInt(&MouseEvents.Down, MOUSE_LEFT);
} else {
Event_RaiseInt(&MouseEvents.Up, btn);
Event_RaiseInt(&MouseEvents.Up, MOUSE_LEFT);
}
}
@ -128,7 +129,7 @@ const cc_uint8 KeyBind_Defaults[KEYBIND_COUNT] = {
KEY_LSHIFT, 'X', 'Z', 'Q', 'E',
KEY_LALT, KEY_F3, KEY_F12, KEY_F11,
KEY_F5, KEY_F1, KEY_F7, 'C',
KEY_LCTRL, 0, KEY_MOUSEMID, 0,
KEY_LCTRL, KEY_LMOUSE, KEY_MMOUSE, KEY_RMOUSE,
KEY_F6, KEY_LALT, KEY_F8,
'G', KEY_F10, 0
};
@ -139,7 +140,7 @@ static const char* const keybindNames[KEYBIND_COUNT] = {
"Speed", "NoClip", "Fly", "FlyUp", "FlyDown",
"ExtInput", "HideFPS", "Screenshot", "Fullscreen",
"ThirdPerson", "HideGUI", "AxisLines", "ZoomScrolling",
"HalfSpeed", "MouseLeft", "PickBlock", "MouseRight",
"HalfSpeed", "DeleteBlock", "PickBlock", "PlaceBlock",
"AutoRotate", "HotbarSwitching", "SmoothCamera",
"DropBlock", "IDOverlay", "BreakableLiquids"
};

View File

@ -39,7 +39,8 @@ enum Key_ {
KEY_TILDE, KEY_MINUS, KEY_EQUALS, KEY_LBRACKET, KEY_RBRACKET, KEY_SLASH,
KEY_SEMICOLON, KEY_QUOTE, KEY_COMMA, KEY_PERIOD, KEY_BACKSLASH,
KEY_XBUTTON1, KEY_XBUTTON2, KEY_MOUSEMID, /* so these can be used for hotkeys */
/* NOTE: RMOUSE must be before MMOUSE for PlayerClick compatibility */
KEY_XBUTTON1, KEY_XBUTTON2, KEY_LMOUSE, KEY_RMOUSE, KEY_MMOUSE,
KEY_COUNT
};
typedef int Key;
@ -81,11 +82,8 @@ extern float Mouse_Wheel;
/* X and Y coordinates of the mouse. Use Mouse_SetPosition to change. */
extern int Mouse_X, Mouse_Y;
/* Pressed state of each mouse button. Use Mouse_SetPressed to change. */
extern bool Mouse_Pressed[MOUSE_COUNT];
/* Sets the pressed state of a mouse button. */
/* Raises MouseEvents.Up or MouseEvents.Down if state differs. */
void Mouse_SetPressed(MouseButton btn, bool pressed);
/* Raises MouseEvents.Up or MouseEvents.Down. */
void Mouse_SetPressed(bool pressed);
/* Sets wheel position of the mouse, always raising MouseEvents.Wheel. */
void Mouse_SetWheel(float wheel);
/* Sets X and Y position of the mouse, always raising MouseEvents.Moved. */
@ -100,7 +98,7 @@ enum KeyBind_ {
KEYBIND_SPEED, KEYBIND_NOCLIP, KEYBIND_FLY, KEYBIND_FLY_UP, KEYBIND_FLY_DOWN,
KEYBIND_EXT_INPUT, KEYBIND_HIDE_FPS, KEYBIND_SCREENSHOT, KEYBIND_FULLSCREEN,
KEYBIND_THIRD_PERSON, KEYBIND_HIDE_GUI, KEYBIND_AXIS_LINES, KEYBIND_ZOOM_SCROLL,
KEYBIND_HALF_SPEED, KEYBIND_MOUSE_LEFT, KEYBIND_PICK_BLOCK, KEYBIND_MOUSE_RIGHT,
KEYBIND_HALF_SPEED, KEYBIND_DELETE_BLOCK, KEYBIND_PICK_BLOCK, KEYBIND_PLACE_BLOCK,
KEYBIND_AUTOROTATE, KEYBIND_HOTBAR_SWITCH, KEYBIND_SMOOTH_CAMERA,
KEYBIND_DROP_BLOCK, KEYBIND_IDOVERLAY, KEYBIND_BREAK_LIQUIDS,
KEYBIND_COUNT

View File

@ -30,15 +30,6 @@ static float input_fovIndex = -1.0f;
static bool suppressEscape;
#endif
bool InputHandler_IsMousePressed(MouseButton button) {
if (Mouse_Pressed[button]) return true;
/* Key --> mouse mappings */
if (button == MOUSE_LEFT && KeyBind_IsPressed(KEYBIND_MOUSE_LEFT)) return true;
if (button == MOUSE_RIGHT && KeyBind_IsPressed(KEYBIND_MOUSE_RIGHT)) return true;
return false;
}
static void InputHandler_ButtonStateUpdate(MouseButton button, bool pressed) {
struct Entity* p;
/* defer getting the targeted entity, as it's a costly operation */
@ -200,6 +191,10 @@ static bool InputHandler_HandleCoreKey(Key key) {
} else {
InputHandler_CycleDistanceForwards(viewDists, count);
}
} else if (key == KeyBinds[KEYBIND_DELETE_BLOCK]) {
InputHandler_PickBlocks(false, true, false, false);
} else if (key == KeyBinds[KEYBIND_PLACE_BLOCK]) {
InputHandler_PickBlocks(false, false, false, true);
} else if (key == KeyBinds[KEYBIND_PICK_BLOCK]) {
InputHandler_PickBlocks(false, false, true, false);
} else if (key == KEY_F5 && Game_ClassicMode) {
@ -332,7 +327,7 @@ void InputHandler_PickBlocks(bool cooldown, bool left, bool middle, bool right)
}
if (left) {
/* always play delete animations, even if we aren't picking a block */
/* always play delete animations, even if we aren't deleting a block */
HeldBlockRenderer_ClickAnim(true);
pos = Game_SelectedPos.BlockPos;
@ -408,8 +403,6 @@ static void InputHandler_MouseDown(void* obj, int btn) {
input_lastClick = DateTime_CurrentUTC_MS(); return;
}
}
InputHandler_PickBlocks(false, btn == MOUSE_LEFT, false, btn == MOUSE_RIGHT);
}
static void InputHandler_MouseUp(void* obj, int btn) {
@ -420,23 +413,6 @@ static void InputHandler_MouseUp(void* obj, int btn) {
s = Gui_Screens[i];
if (s->VTABLE->HandlesMouseUp(s, Mouse_X, Mouse_Y, btn)) return;
}
if (Server.SupportsPlayerClick && btn <= MOUSE_MIDDLE) {
input_pickingId = -1;
InputHandler_ButtonStateChanged(btn, false);
}
}
static bool InputHandler_SimulateMouse(Key key, bool pressed) {
Key left = KeyBinds[KEYBIND_MOUSE_LEFT];
Key right = KeyBinds[KEYBIND_MOUSE_RIGHT];
MouseButton btn;
if (!(key == left || key == right)) return false;
btn = key == left ? MOUSE_LEFT : MOUSE_RIGHT;
if (pressed) { InputHandler_MouseDown(NULL, btn); }
else { InputHandler_MouseUp(NULL, btn); }
return true;
}
static void InputHandler_KeyDown(void* obj, int key, bool was) {
@ -445,7 +421,6 @@ static void InputHandler_KeyDown(void* obj, int key, bool was) {
struct HotkeyData* hkey;
String text;
if (!was && InputHandler_SimulateMouse(key, true)) return;
#ifndef CC_BUILD_WEB
if (key == KEY_ESCAPE && (s = Gui_GetClosable())) {
/* Don't want holding down escape to go in and out of pause menu */
@ -478,6 +453,11 @@ static void InputHandler_KeyDown(void* obj, int key, bool was) {
PauseScreen_Show(); return;
}
if (Server.SupportsPlayerClick && key >= KEY_LMOUSE && key <= KEY_MMOUSE) {
input_pickingId = -1;
InputHandler_ButtonStateChanged(key - KEY_LMOUSE, true);
}
/* These should not be triggered multiple times when holding down */
if (was) return;
if (InputHandler_HandleCoreKey(key)) {
@ -501,9 +481,7 @@ static void InputHandler_KeyUp(void* obj, int key) {
struct Screen* s;
int i;
if (InputHandler_SimulateMouse(key, false)) return;
if (key == KeyBinds[KEYBIND_ZOOM_SCROLL]) Game_SetFov(Game_DefaultFov);
#ifdef CC_BUILD_WEB
/* When closing menus (which reacquires mouse focus) in key down, */
/* this still leaves the cursor visible. But if this is instead */
@ -518,6 +496,11 @@ static void InputHandler_KeyUp(void* obj, int key) {
s = Gui_Screens[i];
if (s->VTABLE->HandlesKeyUp(s, key)) return;
}
if (Server.SupportsPlayerClick && key >= KEY_LMOUSE && key <= KEY_MMOUSE) {
input_pickingId = -1;
InputHandler_ButtonStateChanged(key - KEY_LMOUSE, false);
}
}
static void InputHandler_KeyPress(void* obj, int keyChar) {

View File

@ -6,7 +6,6 @@
*/
struct Screen;
bool InputHandler_IsMousePressed(MouseButton button);
bool InputHandler_SetFOV(int fov);
void InputHandler_PickBlocks(bool cooldown, bool left, bool middle, bool right);
void InputHandler_Init(void);

View File

@ -122,7 +122,7 @@ static int Menu_DoMouseDown(void* screen, int x, int y, MouseButton btn) {
if (!w || !Widget_Contains(w, x, y)) continue;
if (w->disabled) return i;
if (w->MenuClick && btn == MOUSE_LEFT) {
if (w->MenuClick) {
w->MenuClick(s, w);
} else {
Elem_HandlesMouseDown(w, x, y, btn);
@ -1753,13 +1753,13 @@ void OtherKeyBindingsScreen_Show(void) {
*#########################################################################################################################*/
static void MouseKeyBindingsScreen_Init(struct KeyBindingsScreen* s) {
s->leftPage = Menu_SwitchKeysOther;
s->msgText = "&eRight click to remove the key binding";
s->msgText = "&ePress escape to reset the binding";
KeyBindingsScreen_InitWidgets(s, -40, 10, -1, 260, "Mouse key bindings");
}
void MouseKeyBindingsScreen_Show(void) {
static const cc_uint8 binds[3] = { KEYBIND_MOUSE_LEFT, KEYBIND_PICK_BLOCK, KEYBIND_MOUSE_RIGHT };
static const char* descs[3] = { "Left", "Pick block", "Right" };
static const cc_uint8 binds[3] = { KEYBIND_DELETE_BLOCK, KEYBIND_PICK_BLOCK, KEYBIND_PLACE_BLOCK };
static const char* descs[3] = { "Delete block", "Pick block", "Place block" };
KeyBindingsScreen_Show(Array_Elems(binds), binds, descs, MouseKeyBindingsScreen_Init);
}

View File

@ -185,7 +185,7 @@ static bool InventoryScreen_MouseDown(void* screen, int x, int y, MouseButton bt
if (table->scroll.draggingMouse || Elem_HandlesMouseDown(&hud->hotbar, x, y, btn)) return true;
handled = Elem_HandlesMouseDown(table, x, y, btn);
if ((!handled || table->pendingClose) && btn == MOUSE_LEFT) {
if (!handled || table->pendingClose) {
hotbar = Key_IsControlPressed() || Key_IsShiftPressed();
if (!hotbar) Gui_Remove(screen);
}
@ -1091,7 +1091,7 @@ static bool HUDScreen_MouseDown(void* screen, int x, int y, MouseButton btn) {
struct HUDScreen* s = (struct HUDScreen*)screen;
int height, chatY;
if (btn != MOUSE_LEFT || !s->grabsInput) return false;
if (!s->grabsInput) return false;
/* player clicks on name in tab list */
/* TODO: Move to PlayerListWidget */
@ -1354,8 +1354,6 @@ static bool DisconnectScreen_MouseDown(void* screen, int x, int y, MouseButton b
struct DisconnectScreen* s = (struct DisconnectScreen*)screen;
struct ButtonWidget* w = &s->reconnect;
if (btn != MOUSE_LEFT) return true;
if (!w->disabled && Widget_Contains(w, x, y)) {
Gui_Remove((struct Screen*)s);
Gui_ShowDefault();

View File

@ -244,7 +244,6 @@ static bool ScrollbarWidget_MouseDown(void* widget, int x, int y, MouseButton bt
int posY, height;
if (w->draggingMouse) return true;
if (btn != MOUSE_LEFT) return false;
if (x < w->x || x >= w->x + w->width) return false;
y -= w->y;
@ -441,7 +440,7 @@ static bool HotbarWidget_MouseDown(void* widget, int x, int y, MouseButton btn)
int width, height;
int i, cellX, cellY;
if (btn != MOUSE_LEFT || !Widget_Contains(w, x, y)) return false;
if (!Widget_Contains(w, x, y)) return false;
width = (int)(w->elemSize + w->borderSize);
height = Math_Ceil(w->barHeight);
@ -704,7 +703,6 @@ static void TableWidget_ScrollRelative(struct TableWidget* w, int delta) {
static bool TableWidget_MouseDown(void* widget, int x, int y, MouseButton btn) {
struct TableWidget* w = (struct TableWidget*)widget;
w->pendingClose = false;
if (btn != MOUSE_LEFT) return false;
if (Elem_HandlesMouseDown(&w->scroll, x, y, btn)) {
return true;
@ -1191,9 +1189,7 @@ static bool InputWidget_MouseDown(void* widget, int x, int y, MouseButton button
int cx, cy, offset = 0;
int charX, charWidth, charHeight;
if (button != MOUSE_LEFT) return true;
x -= w->inputTex.X; y -= w->inputTex.Y;
DrawTextArgs_MakeEmpty(&args, w->font, true);
charHeight = w->lineHeight;
String_InitArray(line, lineBuffer);

View File

@ -105,7 +105,7 @@ static void Window_AddTouch(long id, int x, int y) {
touchesCount++;
Mouse_SetPosition(x, y);
Mouse_SetPressed(MOUSE_LEFT, true);
Key_SetPressed(KEY_LMOUSE, true);
}
static void Window_UpdateTouch(long id, int x, int y) {
@ -136,7 +136,7 @@ static void Window_RemoveTouch(long id, int x, int y) {
touchesCount--;
Mouse_SetPosition(x, y);
Mouse_SetPressed(MOUSE_LEFT, false);
Key_SetPressed(KEY_LMOUSE, false);
return;
}
}
@ -266,9 +266,9 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara
case WM_MOUSEMOVE:
/* Set before position change, in case mouse buttons changed when outside window */
Mouse_SetPressed(MOUSE_LEFT, (wParam & 0x01) != 0);
Mouse_SetPressed(MOUSE_RIGHT, (wParam & 0x02) != 0);
Key_SetPressed(KEY_MOUSEMID, (wParam & 0x10) != 0);
Key_SetPressed(KEY_LMOUSE, (wParam & 0x01) != 0);
Key_SetPressed(KEY_RMOUSE, (wParam & 0x02) != 0);
Key_SetPressed(KEY_MMOUSE, (wParam & 0x10) != 0);
/* TODO: do we need to set XBUTTON1/XBUTTON2 here */
Mouse_SetPosition(LOWORD(lParam), HIWORD(lParam));
break;
@ -279,21 +279,21 @@ static LRESULT CALLBACK Window_Procedure(HWND handle, UINT message, WPARAM wPara
return 0;
case WM_LBUTTONDOWN:
Mouse_SetPressed(MOUSE_LEFT, true); break;
Key_SetPressed(KEY_LMOUSE, true); break;
case WM_MBUTTONDOWN:
Key_SetPressed(KEY_MOUSEMID, true); break;
Key_SetPressed(KEY_MMOUSE, true); break;
case WM_RBUTTONDOWN:
Mouse_SetPressed(MOUSE_RIGHT, true); break;
Key_SetPressed(KEY_RMOUSE, true); break;
case WM_XBUTTONDOWN:
Key_SetPressed(HIWORD(wParam) == 1 ? KEY_XBUTTON1 : KEY_XBUTTON2, true);
break;
case WM_LBUTTONUP:
Mouse_SetPressed(MOUSE_LEFT, false); break;
Key_SetPressed(KEY_LMOUSE, false); break;
case WM_MBUTTONUP:
Key_SetPressed(KEY_MOUSEMID, false); break;
Key_SetPressed(KEY_MMOUSE, false); break;
case WM_RBUTTONUP:
Mouse_SetPressed(MOUSE_RIGHT, false); break;
Key_SetPressed(KEY_RMOUSE, false); break;
case WM_XBUTTONUP:
Key_SetPressed(HIWORD(wParam) == 1 ? KEY_XBUTTON1 : KEY_XBUTTON2, false);
break;
@ -1096,9 +1096,9 @@ void Window_ProcessEvents(void) {
break;
case ButtonPress:
if (e.xbutton.button == 1) Mouse_SetPressed(MOUSE_LEFT, true);
else if (e.xbutton.button == 2) Key_SetPressed(KEY_MOUSEMID, true);
else if (e.xbutton.button == 3) Mouse_SetPressed(MOUSE_RIGHT, true);
if (e.xbutton.button == 1) Key_SetPressed(KEY_LMOUSE, true);
else if (e.xbutton.button == 2) Key_SetPressed(KEY_MMOUSE, true);
else if (e.xbutton.button == 3) Key_SetPressed(KEY_RMOUSE, true);
else if (e.xbutton.button == 4) Mouse_SetWheel(Mouse_Wheel + 1);
else if (e.xbutton.button == 5) Mouse_SetWheel(Mouse_Wheel - 1);
else if (e.xbutton.button == 6) Key_SetPressed(KEY_XBUTTON1, true);
@ -1106,9 +1106,9 @@ void Window_ProcessEvents(void) {
break;
case ButtonRelease:
if (e.xbutton.button == 1) Mouse_SetPressed(MOUSE_LEFT, false);
else if (e.xbutton.button == 2) Key_SetPressed(KEY_MOUSEMID, false);
else if (e.xbutton.button == 3) Mouse_SetPressed(MOUSE_RIGHT, false);
if (e.xbutton.button == 1) Key_SetPressed(KEY_LMOUSE, false);
else if (e.xbutton.button == 2) Key_SetPressed(KEY_MMOUSE, false);
else if (e.xbutton.button == 3) Key_SetPressed(KEY_RMOUSE, false);
else if (e.xbutton.button == 6) Key_SetPressed(KEY_XBUTTON1, false);
else if (e.xbutton.button == 7) Key_SetPressed(KEY_XBUTTON2, false);
break;
@ -1657,11 +1657,11 @@ static OSStatus Window_ProcessMouseEvent(EventRef inEvent) {
switch (button) {
case kEventMouseButtonPrimary:
Mouse_SetPressed(MOUSE_LEFT, down); break;
Key_SetPressed(KEY_LMOUSE, down); break;
case kEventMouseButtonSecondary:
Mouse_SetPressed(MOUSE_RIGHT, down); break;
Key_SetPressed(KEY_RMOUSE, down); break;
case kEventMouseButtonTertiary:
Key_SetPressed(KEY_MOUSEMID, down); break;
Key_SetPressed(KEY_MMOUSE, down); break;
}
return eventNotHandledErr;
@ -2224,15 +2224,15 @@ static void Window_HandleMouseEvent(const SDL_Event* e) {
bool pressed = e->button.state == SDL_PRESSED;
switch (e->button.button) {
case SDL_BUTTON_LEFT:
Mouse_SetPressed(MOUSE_LEFT, pressed); break;
Key_SetPressed(KEY_LMOUSE, pressed); break;
case SDL_BUTTON_MIDDLE:
Key_SetPressed(KEY_MOUSEMID, pressed); break;
Key_SetPressed(KEY_MMOUSE, pressed); break;
case SDL_BUTTON_RIGHT:
Mouse_SetPressed(MOUSE_RIGHT, pressed); break;
Key_SetPressed(KEY_RMOUSE, pressed); break;
case SDL_BUTTON_X1:
Key_SetPressed(KEY_XBUTTON1, pressed); break;
Key_SetPressed(KEY_XBUTTON1, pressed); break;
case SDL_BUTTON_X2:
Key_SetPressed(KEY_XBUTTON2, pressed); break;
Key_SetPressed(KEY_XBUTTON2, pressed); break;
}
}
@ -2407,18 +2407,18 @@ static EM_BOOL Window_MouseButton(int type, const EmscriptenMouseEvent* ev, void
Window_CorrectFocus();
switch (ev->button) {
case 0: Mouse_SetPressed(MOUSE_LEFT, down); break;
case 1: Input_SetPressed(KEY_MOUSEMID, down); break;
case 2: Mouse_SetPressed(MOUSE_RIGHT, down); break;
case 0: Key_SetPressed(KEY_LMOUSE, down); break;
case 1: Key_SetPressed(KEY_MMOUSE, down); break;
case 2: Key_SetPressed(KEY_RMOUSE, down); break;
}
return true;
}
static EM_BOOL Window_MouseMove(int type, const EmscriptenMouseEvent* ev, void* data) {
/* Set before position change, in case mouse buttons changed when outside window */
Mouse_SetPressed(MOUSE_LEFT, (ev->buttons & 0x01) != 0);
Mouse_SetPressed(MOUSE_RIGHT, (ev->buttons & 0x02) != 0);
Input_SetPressed(KEY_MOUSEMID, (ev->buttons & 0x04) != 0);
Key_SetPressed(KEY_LMOUSE, (ev->buttons & 0x01) != 0);
Key_SetPressed(KEY_RMOUSE, (ev->buttons & 0x02) != 0);
Key_SetPressed(KEY_MMOUSE, (ev->buttons & 0x04) != 0);
Mouse_SetPosition(ev->canvasX, ev->canvasY);
if (win_rawMouse) Event_RaiseMouseMove(&MouseEvents.RawMoved, ev->movementX, ev->movementY);