Skip to content

Commit

Permalink
refactor: 重构并修复滑动错误
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Oct 18, 2023
1 parent 9da6507 commit df9383d
Show file tree
Hide file tree
Showing 20 changed files with 333 additions and 501 deletions.
218 changes: 6 additions & 212 deletions source/MaaControlUnit/Input/MaatouchInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ bool MaatouchInput::init(int swidth, int sheight, int orientation)
return false;
}

return set_wh(swidth, sheight, orientation);
}

bool MaatouchInput::set_wh(int swidth, int sheight, int orientation)
{
auto start_time = std::chrono::steady_clock::now();
bool timeout = false;
auto check_time = [&]() {
Expand All @@ -69,159 +74,7 @@ bool MaatouchInput::init(int swidth, int sheight, int orientation)
return false;
}

std::string prev;
std::string info;
while (check_time()) {
auto str = prev + shell_handler_->read(5);
LogDebug << "output:" << str;
if (str.empty()) {
std::this_thread::sleep_for(std::chrono::seconds(2));
continue;
}
auto pos = str.find('^');
if (pos == std::string::npos) {
continue;
}
auto rpos = str.find('\n', pos);
if (rpos == std::string::npos) {
prev = str; // 也许还得再读点?
continue;
}

info = str.substr(pos + 1, rpos - pos - 1);
break;
}
if (timeout) {
LogError << "read maatouch info timeout";
return false;
}

LogInfo << "maatouch info:" << info;
string_trim_(info);

int contact = 0;
int x = 0;
int y = 0;
int pressure = 0;

std::istringstream ins(std::move(info));
if (!(ins >> contact >> x >> y >> pressure)) {
return false;
}

screen_width_ = swidth;
screen_height_ = sheight;
touch_width_ = x;
touch_height_ = y;
xscale_ = double(touch_width_) / swidth;
yscale_ = double(touch_height_) / sheight;
press_ = pressure;
orientation_ = orientation;

LogInfo << VAR(screen_width_) << VAR(screen_height_) << VAR(touch_width_) << VAR(touch_height_) << VAR(xscale_)
<< VAR(yscale_) << VAR(press_) << VAR(orientation_);

return true;
}

void MaatouchInput::set_wh(int swidth, int sheight, int orientation)
{
init(swidth, sheight, orientation);
}

bool MaatouchInput::click(int x, int y)
{
if (!shell_handler_) {
LogError << "shell handler not ready";
return false;
}

if (x < 0 || x >= screen_width_ || y < 0 || y >= screen_height_) {
LogWarn << "click point out of range" << VAR(x) << VAR(y);
x = std::clamp(x, 0, screen_width_ - 1);
y = std::clamp(y, 0, screen_height_ - 1);
}

auto [touch_x, touch_y] = screen_to_touch(x, y);

LogInfo << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x, touch_y, press_)) &&
shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));

if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

bool MaatouchInput::swipe(int x1, int y1, int x2, int y2, int duration)
{
if (!shell_handler_) {
return false;
}

if (x1 < 0 || x1 >= screen_width_ || y1 < 0 || y1 >= screen_height_ || x2 < 0 || x2 >= screen_width_ || y2 < 0 ||
y2 >= screen_height_) {
LogWarn << "swipe point out of range" << VAR(x1) << VAR(y1) << VAR(x2) << VAR(y2);
x1 = std::clamp(x1, 0, screen_width_ - 1);
y1 = std::clamp(y1, 0, screen_height_ - 1);
x2 = std::clamp(x2, 0, screen_width_ - 1);
y2 = std::clamp(y2, 0, screen_height_ - 1);
}
if (duration <= 0) {
LogWarn << "duration out of range" << VAR(duration);
duration = 500;
}

auto [touch_x1, touch_y1] = screen_to_touch(x1, y1);
auto [touch_x2, touch_y2] = screen_to_touch(x2, y2);

LogInfo << VAR(x1) << VAR(y1) << VAR(touch_x1) << VAR(touch_y1) << VAR(x2) << VAR(y2) << VAR(touch_x2)
<< VAR(touch_y2) << VAR(duration);

auto start = std::chrono::steady_clock::now();
auto now = start;
bool ret = true;
ret &= shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", 0, touch_x1, touch_y1, press_));
if (!ret) {
LogError << "write error";
return false;
}

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<int>(kInterval));

for (int i = 0; i < steps; ++i) {
auto [tx, ty] = screen_to_touch(x1 + steps * x_step_len, y1 + steps * y_step_len);
std::this_thread::sleep_until(now + delay);
now = std::chrono::steady_clock::now();

ret &= shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", 0, tx, ty, press_));
if (!ret) {
LogWarn << "write error";
}
}

std::this_thread::sleep_until(now + delay);
now = std::chrono::steady_clock::now();
ret &= shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", 0, touch_x2, touch_y2, press_));

std::this_thread::sleep_until(now + delay);
now = std::chrono::steady_clock::now();
ret &= shell_handler_->write(MAA_FMT::format("u {}\nc\n", 0));

if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
return read_info(swidth, sheight, orientation);
}

bool MaatouchInput::press_key(int key)
Expand All @@ -241,63 +94,4 @@ bool MaatouchInput::press_key(int key)
return ret;
}

bool MaatouchInput::touch_down(int contact, int x, int y, int pressure)
{
if (!shell_handler_) {
LogError << "shell handler not ready";
return false;
}

auto [touch_x, touch_y] = screen_to_touch(x, y);

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool ret = shell_handler_->write(MAA_FMT::format("d {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));

if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

bool MaatouchInput::touch_move(int contact, int x, int y, int pressure)
{
if (!shell_handler_) {
LogError << "shell handler not ready";
return false;
}

auto [touch_x, touch_y] = screen_to_touch(x, y);

LogInfo << VAR(contact) << VAR(x) << VAR(y) << VAR(touch_x) << VAR(touch_y);

bool ret = shell_handler_->write(MAA_FMT::format("m {} {} {} {}\nc\n", contact, touch_x, touch_y, pressure));
if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

bool MaatouchInput::touch_up(int contact)
{
if (!shell_handler_) {
LogError << "shell handler not ready";
return false;
}

LogInfo << VAR(contact);

bool ret = shell_handler_->write(MAA_FMT::format("u {}\nc\n", contact));
if (!ret) {
LogError << "failed to write";
return false;
}

return ret;
}

MAA_CTRL_UNIT_NS_END
36 changes: 11 additions & 25 deletions source/MaaControlUnit/Input/MaatouchInput.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "MtouchHelper.h"
#include "UnitBase.h"

#include <filesystem>
Expand All @@ -8,12 +9,11 @@

MAA_CTRL_UNIT_NS_BEGIN

class MaatouchInput : public TouchInputBase, public KeyInputBase
class MaatouchInput : public MtouchHelper, public KeyInputBase
{
public:
MaatouchInput(std::filesystem::path agent_path) : agent_path_(std::move(agent_path))
{
TouchInputBase::children_.emplace_back(invoke_app_);
KeyInputBase::children_.emplace_back(invoke_app_);
}
virtual ~MaatouchInput() override = default;
Expand All @@ -23,55 +23,41 @@ class MaatouchInput : public TouchInputBase, public KeyInputBase

virtual void set_io(std::shared_ptr<PlatformIO> io_ptr) override
{
TouchInputBase::set_io(io_ptr);
MtouchHelper::set_io(io_ptr);
KeyInputBase::set_io(io_ptr);
}
virtual void set_replacement(Argv::replacement argv_replace) override
{
TouchInputBase::set_replacement(argv_replace);
MtouchHelper::set_replacement(argv_replace);
KeyInputBase::set_replacement(argv_replace);
}
virtual void merge_replacement(Argv::replacement argv_replace, bool _override = true) override
{
TouchInputBase::merge_replacement(argv_replace, _override);
MtouchHelper::merge_replacement(argv_replace, _override);
KeyInputBase::merge_replacement(argv_replace, _override);
}

public: // from TouchInputAPI
virtual bool init(int swidth, int sheight, int orientation) override;
virtual void deinit() override {}
virtual void set_wh(int swidth, int sheight, int orientation) override;

virtual bool click(int x, int y) override;
virtual bool swipe(int x1, int y1, int x2, int y2, int duration) override;

virtual bool touch_down(int contact, int x, int y, int pressure) override;
virtual bool touch_move(int contact, int x, int y, int pressure) override;
virtual bool touch_up(int contact) override;
virtual bool set_wh(int swidth, int sheight, int orientation) override;

public: // from KeyInputAPI
virtual bool press_key(int key) override;

protected: // from MtouchHelper
virtual std::pair<int, int> screen_to_touch(int x, int y) override { return _screen_to_touch(x, y); }
virtual std::pair<int, int> screen_to_touch(double x, double y) override { return _screen_to_touch(x, y); }

private:
template <typename T1, typename T2>
inline std::pair<int, int> screen_to_touch(T1 x, T2 y)
inline std::pair<int, int> _screen_to_touch(T1 x, T2 y)
{
return std::make_pair(static_cast<int>(round(x * xscale_)), static_cast<int>(round(y * yscale_)));
}

std::shared_ptr<InvokeApp> invoke_app_ = std::make_shared<InvokeApp>();
std::shared_ptr<IOHandler> shell_handler_ = nullptr;

std::filesystem::path agent_path_;
std::string package_name_;
int screen_width_ = 0;
int screen_height_ = 0;
int touch_width_ = 0;
int touch_height_ = 0;
double xscale_ = 0;
double yscale_ = 0;
int press_ = 0;
int orientation_ = 0;
};

MAA_CTRL_UNIT_NS_END
Loading

0 comments on commit df9383d

Please sign in to comment.