mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-19 02:23:01 +08:00
feat: use cubic interpolation in minitouch swipe
This commit is contained in:
@@ -1016,7 +1016,8 @@ bool asst::Controller::click_without_scale(const Rect& rect)
|
||||
return click_without_scale(rand_point_in_rect(rect));
|
||||
}
|
||||
|
||||
bool asst::Controller::swipe(const Point& p1, const Point& p2, int duration, bool extra_swipe, double acceleration_coef)
|
||||
bool asst::Controller::swipe(const Point& p1, const Point& p2, int duration, bool extra_swipe, double slope_in,
|
||||
double slope_out)
|
||||
{
|
||||
int x1 = static_cast<int>(p1.x * m_control_scale);
|
||||
int y1 = static_cast<int>(p1.y * m_control_scale);
|
||||
@@ -1024,16 +1025,17 @@ bool asst::Controller::swipe(const Point& p1, const Point& p2, int duration, boo
|
||||
int y2 = static_cast<int>(p2.y * m_control_scale);
|
||||
// log.trace("Swipe, raw:", p1.x, p1.y, p2.x, p2.y, "corr:", x1, y1, x2, y2);
|
||||
|
||||
return swipe_without_scale(Point(x1, y1), Point(x2, y2), duration, extra_swipe, acceleration_coef);
|
||||
return swipe_without_scale(Point(x1, y1), Point(x2, y2), duration, extra_swipe, slope_in, slope_out);
|
||||
}
|
||||
|
||||
bool asst::Controller::swipe(const Rect& r1, const Rect& r2, int duration, bool extra_swipe, double acceleration_coef)
|
||||
bool asst::Controller::swipe(const Rect& r1, const Rect& r2, int duration, bool extra_swipe, double slope_in,
|
||||
double slope_out)
|
||||
{
|
||||
return swipe(rand_point_in_rect(r1), rand_point_in_rect(r2), duration, extra_swipe, acceleration_coef);
|
||||
return swipe(rand_point_in_rect(r1), rand_point_in_rect(r2), duration, extra_swipe, slope_in, slope_out);
|
||||
}
|
||||
|
||||
bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int duration, bool extra_swipe,
|
||||
double acceleration_coef)
|
||||
double slope_in, double slope_out)
|
||||
{
|
||||
int x1 = p1.x, y1 = p1.y;
|
||||
int x2 = p2.x, y2 = p2.y;
|
||||
@@ -1052,16 +1054,21 @@ bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int
|
||||
if (duration == 0) {
|
||||
duration = 150;
|
||||
}
|
||||
auto minitouch_move = [&](int _x1, int _y1, int _x2, int _y2, int _duration) {
|
||||
double accelerationx = acceleration_coef * static_cast<double>(_x2 - _x1) / (_duration * _duration);
|
||||
double accelerationy = acceleration_coef * static_cast<double>(_y2 - _y1) / (_duration * _duration);
|
||||
double v0x = static_cast<double>(_x2 - _x1) / _duration - accelerationx * _duration;
|
||||
double v0y = static_cast<double>(_y2 - _y1) / _duration - accelerationy * _duration;
|
||||
|
||||
constexpr int TimeInterval = Minitoucher::DefaultSwipeDelay;
|
||||
constexpr int TimeInterval = Minitoucher::DefaultSwipeDelay;
|
||||
|
||||
auto cubic_spline = [](double slope_0, double slope_1, double t) {
|
||||
const double a = slope_0;
|
||||
const double b = -(2 * slope_0 + slope_1 - 3);
|
||||
const double c = -(-slope_0 - slope_1 + 2);
|
||||
return a * t + b * std::pow(t, 2) + c * std::pow(t, 3);
|
||||
}; // TODO: move this to math.hpp
|
||||
|
||||
auto minitouch_move = [&](int _x1, int _y1, int _x2, int _y2, int _duration) {
|
||||
for (int cur_time = TimeInterval; cur_time < _duration; cur_time += TimeInterval) {
|
||||
int cur_x = _x1 + static_cast<int>(v0x * cur_time + accelerationx * cur_time * cur_time);
|
||||
int cur_y = _y1 + static_cast<int>(v0y * cur_time + accelerationy * cur_time * cur_time);
|
||||
double progress = cubic_spline(slope_in, slope_out, static_cast<double>(cur_time) / duration);
|
||||
int cur_x = static_cast<int>(std::lerp(_x1, _x2, progress));
|
||||
int cur_y = static_cast<int>(std::lerp(_y1, _y2, progress));
|
||||
if (cur_x < 0 || cur_x > m_minitouch_props.max_x || cur_y < 0 || cur_y > m_minitouch_props.max_y) {
|
||||
continue;
|
||||
}
|
||||
@@ -1108,11 +1115,10 @@ bool asst::Controller::swipe_without_scale(const Point& p1, const Point& p2, int
|
||||
}
|
||||
}
|
||||
|
||||
bool asst::Controller::swipe_without_scale(const Rect& r1, const Rect& r2, int duration, bool extra_swipe,
|
||||
double acceleration_coef)
|
||||
bool asst::Controller::swipe_without_scale(const Rect& r1, const Rect& r2, int duration, bool extra_swipe, double v0,
|
||||
double v1)
|
||||
{
|
||||
return swipe_without_scale(rand_point_in_rect(r1), rand_point_in_rect(r2), duration, extra_swipe,
|
||||
acceleration_coef);
|
||||
return swipe_without_scale(rand_point_in_rect(r1), rand_point_in_rect(r2), duration, extra_swipe, v0, v1);
|
||||
}
|
||||
|
||||
bool asst::Controller::connect(const std::string& adb_path, const std::string& address, const std::string& config)
|
||||
|
||||
Reference in New Issue
Block a user