diff --git a/source/MaaWin32ControlUnit/Input/SendMessageInput.cpp b/source/MaaWin32ControlUnit/Input/SendMessageInput.cpp index 00ad87c8e..509cf0eaf 100644 --- a/source/MaaWin32ControlUnit/Input/SendMessageInput.cpp +++ b/source/MaaWin32ControlUnit/Input/SendMessageInput.cpp @@ -1,64 +1,133 @@ #include "SendMessageInput.h" +#include "Utils/Logger.h" +#include "Utils/SafeWindows.hpp" + MAA_CTRL_UNIT_NS_BEGIN bool SendMessageInput::click(int x, int y) { - // TODO - std::ignore = x; - std::ignore = y; + LogInfo << VAR(x) << VAR(y); + + if (!hwnd_) { + LogError << "hwnd_ is nullptr"; + return false; + } + + SendMessage(hwnd_, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y)); + SendMessage(hwnd_, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x, y)); - return false; + return true; } bool SendMessageInput::swipe(int x1, int y1, int x2, int y2, int duration) { - // TODO - std::ignore = x1; - std::ignore = y1; - std::ignore = x2; - std::ignore = y2; - std::ignore = duration; - - return false; + LogInfo << VAR(x1) << VAR(y1) << VAR(x2) << VAR(y2) << VAR(duration); + + if (!hwnd_) { + LogError << "hwnd_ is nullptr"; + return false; + } + + if (duration <= 0) { + LogWarn << "duration out of range" << VAR(duration); + duration = 500; + } + + auto start = std::chrono::steady_clock::now(); + auto now = start; + SendMessage(hwnd_, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x1, y1)); + + constexpr double kInterval = 10; // ms + const double steps = duration / kInterval; + const double x_step_len = (x2 - x1) / steps; + const double y_step_len = (y2 - y1) / steps; + const std::chrono::milliseconds delay(static_cast(kInterval)); + + for (int i = 0; i < steps; ++i) { + int tx = static_cast(x1 + i * x_step_len); + int ty = static_cast(y1 + i * y_step_len); + std::this_thread::sleep_until(now + delay); + now = std::chrono::steady_clock::now(); + + SendMessage(hwnd_, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(tx, ty)); + } + + std::this_thread::sleep_until(now + delay); + now = std::chrono::steady_clock::now(); + + SendMessage(hwnd_, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(x2, y2)); + + std::this_thread::sleep_until(now + delay); + now = std::chrono::steady_clock::now(); + SendMessage(hwnd_, WM_LBUTTONUP, MK_LBUTTON, MAKELPARAM(x2, y2)); + + return true; } bool SendMessageInput::touch_down(int contact, int x, int y, int pressure) { - // TODO + LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(pressure); + std::ignore = contact; - std::ignore = x; - std::ignore = y; std::ignore = pressure; - return false; + if (!hwnd_) { + LogError << "hwnd_ is nullptr"; + return false; + } + + SendMessage(hwnd_, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(x, y)); + + return true; } bool SendMessageInput::touch_move(int contact, int x, int y, int pressure) { - // TODO + LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(pressure); + std::ignore = contact; - std::ignore = x; - std::ignore = y; std::ignore = pressure; - return false; + if (!hwnd_) { + LogError << "hwnd_ is nullptr"; + return false; + } + + SendMessage(hwnd_, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(x, y)); + + return true; } bool SendMessageInput::touch_up(int contact) { - // TODO + LogInfo << VAR(contact); + std::ignore = contact; - return false; + if (!hwnd_) { + LogError << "hwnd_ is nullptr"; + return false; + } + + SendMessage(hwnd_, WM_LBUTTONUP, MK_LBUTTON, 0); + + return true; } bool SendMessageInput::press_key(int key) { - // TODO - std::ignore = key; + LogInfo << VAR(key); + + if (!hwnd_) { + LogError << "hwnd_ is nullptr"; + return false; + } + + SendMessage(hwnd_, WM_KEYDOWN, key, 0); + SendMessage(hwnd_, WM_KEYUP, key, 0); - return false; + return true; } MAA_CTRL_UNIT_NS_END