From cd1c53efa1889795780058c3b158000a2e0f8dd0 Mon Sep 17 00:00:00 2001 From: Souryo Date: Wed, 14 Dec 2016 18:26:52 -0500 Subject: [PATCH] Linux: Implemented mouse clicks for zapper/etc. --- GUI.NET/Controls/ctrlRenderer.cs | 25 +++++++++++++++++++++++++ Linux/LinuxKeyManager.cpp | 21 +++++++++++++++------ Linux/LinuxKeyManager.h | 3 ++- Windows/WindowsKeyManager.cpp | 20 ++++++++++++-------- Windows/WindowsKeyManager.h | 3 ++- 5 files changed, 56 insertions(+), 16 deletions(-) diff --git a/GUI.NET/Controls/ctrlRenderer.cs b/GUI.NET/Controls/ctrlRenderer.cs index 250fffee..5cde6b9a 100644 --- a/GUI.NET/Controls/ctrlRenderer.cs +++ b/GUI.NET/Controls/ctrlRenderer.cs @@ -13,6 +13,10 @@ namespace Mesen.GUI.Controls { public partial class ctrlRenderer : BaseControl { + private const int LeftMouseButtonKeyCode = 0x200; + private const int RightMouseButtonKeyCode = 0x201; + private const int MiddleMouseButtonKeyCode = 0x202; + private bool _cursorHidden = false; public ctrlRenderer() @@ -41,6 +45,27 @@ namespace Mesen.GUI.Controls } } + protected override void OnMouseDown(MouseEventArgs e) + { + base.OnMouseDown(e); + SetMouseButtonState(e.Button, true); + } + + protected override void OnMouseUp(MouseEventArgs e) + { + base.OnMouseUp(e); + SetMouseButtonState(e.Button, false); + } + + private void SetMouseButtonState(MouseButtons button, bool down) + { + switch(button) { + case MouseButtons.Left: InteropEmu.SetKeyState(LeftMouseButtonKeyCode, down); break; + case MouseButtons.Right: InteropEmu.SetKeyState(RightMouseButtonKeyCode, down); break; + case MouseButtons.Middle: InteropEmu.SetKeyState(MiddleMouseButtonKeyCode, down); break; + } + } + private void ctrlRenderer_MouseMove(object sender, MouseEventArgs e) { if(!InteropEmu.IsRunning() || InteropEmu.IsPaused() || !InteropEmu.HasArkanoidPaddle()) { diff --git a/Linux/LinuxKeyManager.cpp b/Linux/LinuxKeyManager.cpp index d77d5823..63f01f88 100755 --- a/Linux/LinuxKeyManager.cpp +++ b/Linux/LinuxKeyManager.cpp @@ -226,7 +226,7 @@ static vector _keyDefinitions = { LinuxKeyManager::LinuxKeyManager() { - _keyState.insert(_keyState.end(), 0x200, 0); + ResetKeyState(); for(KeyDefinition &keyDef : _keyDefinitions) { _keyNames[keyDef.keyCode] = keyDef.description; @@ -254,8 +254,12 @@ bool LinuxKeyManager::IsKeyPressed(uint32_t key) bool LinuxKeyManager::IsMouseButtonPressed(MouseButton button) { - //TODO: NOT IMPLEMENTED YET - //Only needed for zapper/etc + switch(button) { + case MouseButton::LeftButton: return _mouseState[0]; + case MouseButton::RightButton: return _mouseState[1]; + case MouseButton::MiddleButton: return _mouseState[2]; + } + return false; } @@ -295,10 +299,15 @@ void LinuxKeyManager::UpdateDevices() void LinuxKeyManager::SetKeyState(uint16_t scanCode, bool state) { - _keyState[scanCode & 0xFF] = state ? 1 : 0; + if(scanCode > 0x1FF) { + _mouseState[scanCode & 0x03] = state; + } else { + _keyState[scanCode & 0xFF] = state; + } } void LinuxKeyManager::ResetKeyState() { - memset(_keyState.data(), 0, 0x200 * sizeof(uint32_t)); -} + memset(_mouseState, 0, sizeof(_mouseState)); + memset(_keyState, 0, sizeof(_keyState)); +} diff --git a/Linux/LinuxKeyManager.h b/Linux/LinuxKeyManager.h index d8bce367..602f50b7 100755 --- a/Linux/LinuxKeyManager.h +++ b/Linux/LinuxKeyManager.h @@ -12,7 +12,8 @@ struct KeyDefinition { class LinuxKeyManager : public IKeyManager { private: - vector _keyState; + bool _keyState[0x200]; + bool _mouseState[0x03]; std::unordered_map _keyNames; std::unordered_map _keyCodes; diff --git a/Windows/WindowsKeyManager.cpp b/Windows/WindowsKeyManager.cpp index d36dc018..b36e81e8 100644 --- a/Windows/WindowsKeyManager.cpp +++ b/Windows/WindowsKeyManager.cpp @@ -205,7 +205,7 @@ WindowsKeyManager::WindowsKeyManager(HWND hWnd) { _hWnd = hWnd; - _keyState.insert(_keyState.end(), 0x200, 0); + ResetKeyState(); //Init XInput buttons vector buttonNames = { "Up", "Down", "Left", "Right", "Start", "Back", "L3", "R3", "L1", "R1", "?", "?", "A", "B", "X", "Y", "L2", "R2", "RT Up", "RT Down", "RT Up", "RT Left", "RT Right" }; @@ -298,14 +298,13 @@ bool WindowsKeyManager::IsKeyPressed(uint32_t key) bool WindowsKeyManager::IsMouseButtonPressed(MouseButton button) { - uint32_t key = 0; switch(button) { - case MouseButton::LeftButton: key = VK_LBUTTON; break; - case MouseButton::RightButton: key = VK_RBUTTON; break; - case MouseButton::MiddleButton: key = VK_MBUTTON; break; + case MouseButton::LeftButton: return _mouseState[0]; + case MouseButton::RightButton: return _mouseState[1]; + case MouseButton::MiddleButton: return _mouseState[2]; } - return (GetAsyncKeyState(key) & 0x8000) == 0x8000; + return false; } uint32_t WindowsKeyManager::GetPressedKey() @@ -366,10 +365,15 @@ void WindowsKeyManager::UpdateDevices() void WindowsKeyManager::SetKeyState(uint16_t scanCode, bool state) { - _keyState[scanCode & 0x1FF] = state ? 1 : 0; + if(scanCode > 0x1FF) { + _mouseState[scanCode & 0x03] = state; + } else { + _keyState[scanCode & 0x1FF] = state; + } } void WindowsKeyManager::ResetKeyState() { - memset(_keyState.data(), 0, 0x200); + memset(_mouseState, 0, sizeof(_mouseState)); + memset(_keyState, 0, sizeof(_keyState)); } \ No newline at end of file diff --git a/Windows/WindowsKeyManager.h b/Windows/WindowsKeyManager.h index a19fffc8..5ec57f75 100644 --- a/Windows/WindowsKeyManager.h +++ b/Windows/WindowsKeyManager.h @@ -18,7 +18,8 @@ class WindowsKeyManager : public IKeyManager { private: HWND _hWnd; - vector _keyState; + bool _keyState[0x200]; + bool _mouseState[0x03]; unique_ptr _directInput; unique_ptr _xInput; std::unordered_map _keyNames;