|
|
@@ -1,9 +1,9 @@
|
|
|
-#include "TouchWindowBase.h"
|
|
|
+#include "TouchWindowBase.h"
|
|
|
#include <vector>
|
|
|
#include <cmath>
|
|
|
|
|
|
#ifndef TOUCH_COORD_TO_PIXEL
|
|
|
-// TOUCH 输入单位是 1/100 英寸,通常使用这个宏转换为像素
|
|
|
+// TOUCH 输入单位是 1/100 英寸,通常使用这个宏转换为像素
|
|
|
#define TOUCH_COORD_TO_PIXEL(l) ((l) / 100)
|
|
|
#endif
|
|
|
|
|
|
@@ -27,7 +27,7 @@ void TouchWindowBase::AddTouchSample(LONG y)
|
|
|
std::int64_t t = NowMs();
|
|
|
m_samples.push_back({ t, y });
|
|
|
|
|
|
- // 保留时间窗口或最大条目数
|
|
|
+ // 淇濈暀鏃堕棿绐楀彛鎴栨渶澶ф潯鐩�暟
|
|
|
while (!m_samples.empty() && (t - m_samples.front().tms) > SAMPLE_WINDOW_MS)
|
|
|
m_samples.pop_front();
|
|
|
|
|
|
@@ -43,7 +43,7 @@ void TouchWindowBase::StartInertia(float velocityPxPerMs)
|
|
|
m_inertiaVelocityPxPerMs = velocityPxPerMs;
|
|
|
if (std::abs(m_inertiaVelocityPxPerMs) < MIN_INERTIA_VELOCITY)
|
|
|
{
|
|
|
- // 速度太小,不启动惯性
|
|
|
+ // 速度太小,不启动惯性
|
|
|
m_inertiaActive = false;
|
|
|
return;
|
|
|
}
|
|
|
@@ -69,7 +69,7 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
switch (uMsg)
|
|
|
{
|
|
|
case WM_CREATE:
|
|
|
- // 注册接收触摸(移除:由外部窗口统一注册,避免重复注册)
|
|
|
+ // 娉ㄥ唽鎺ユ敹瑙︽懜锛堢Щ闄わ細鐢卞�閮ㄧ獥鍙g粺涓€娉ㄥ唽锛岄伩鍏嶉噸澶嶆敞鍐岋級
|
|
|
// ::RegisterTouchWindow(m_hWnd, 0);
|
|
|
break;
|
|
|
|
|
|
@@ -86,14 +86,14 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
pt.x = TOUCH_COORD_TO_PIXEL(ti.x);
|
|
|
pt.y = TOUCH_COORD_TO_PIXEL(ti.y);
|
|
|
|
|
|
- // 把屏幕坐标转换为目标 paint 窗口客户区坐标
|
|
|
+ // 把屏幕坐标转换为目标 paint 窗口客户区坐标
|
|
|
HWND target = m_hWndPaint ? m_hWndPaint : m_hWnd;
|
|
|
ScreenToClient(target, &pt);
|
|
|
|
|
|
- // 触摸按下
|
|
|
+ // 瑙︽懜鎸変笅
|
|
|
if (ti.dwFlags & TOUCHEVENTF_DOWN)
|
|
|
{
|
|
|
- // 停止可能正在进行的惯性
|
|
|
+ // 停止可能正在进行的惯性
|
|
|
StopInertia();
|
|
|
|
|
|
m_touchDown = true;
|
|
|
@@ -102,18 +102,18 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
m_samples.clear();
|
|
|
AddTouchSample(pt.y);
|
|
|
|
|
|
- // 保存最后触摸点(用于惯性期间发送位置)
|
|
|
+ // 淇濆瓨鏈€鍚庤Е鎽哥偣锛堢敤浜庢儻鎬ф湡闂村彂閫佷綅缃�級
|
|
|
m_lastTouchPt = pt;
|
|
|
|
|
|
::PostMessage(target, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(pt.x, pt.y));
|
|
|
::SetCapture(target);
|
|
|
}
|
|
|
- // 触摸移动
|
|
|
+ // 瑙︽懜绉诲姩
|
|
|
else if (ti.dwFlags & TOUCHEVENTF_MOVE)
|
|
|
{
|
|
|
if (m_touchDown)
|
|
|
{
|
|
|
- LONG dy = m_lastY - pt.y; // 向上移动产生正的滚轮增量
|
|
|
+ LONG dy = pt.y - m_lastY; // 按手机习惯:手指上滑(坐标减小) -> 负增量
|
|
|
m_lastY = pt.y;
|
|
|
|
|
|
AddTouchSample(pt.y);
|
|
|
@@ -121,10 +121,10 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
int wheelUnits = static_cast<int>(dy * PIXEL_TO_WHEEL);
|
|
|
m_accumWheel += wheelUnits;
|
|
|
|
|
|
- // 更新最后触摸点(用于惯性期间发送位置)
|
|
|
+ // 鏇存柊鏈€鍚庤Е鎽哥偣锛堢敤浜庢儻鎬ф湡闂村彂閫佷綅缃�級
|
|
|
m_lastTouchPt = pt;
|
|
|
|
|
|
- // 每当累积达到 WHEEL_DELTA 就发送一次 WM_MOUSEWHEEL
|
|
|
+ // 每当累积达到 WHEEL_DELTA 就发送一次 WM_MOUSEWHEEL
|
|
|
while (abs(m_accumWheel) >= WHEEL_DELTA)
|
|
|
{
|
|
|
int sendDelta = (m_accumWheel > 0) ? WHEEL_DELTA : -WHEEL_DELTA;
|
|
|
@@ -135,16 +135,16 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
::PostMessage(target, WM_MOUSEWHEEL, wParamWheel, lParamPos);
|
|
|
}
|
|
|
|
|
|
- // 仍然发送鼠标移动,兼容需要拖拽的控件
|
|
|
+ // 浠嶇劧鍙戦€侀紶鏍囩Щ鍔�紝鍏煎�闇€瑕佹嫋鎷界殑鎺т欢
|
|
|
::PostMessage(target, WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(pt.x, pt.y));
|
|
|
}
|
|
|
}
|
|
|
- // 触摸抬起
|
|
|
+ // 瑙︽懜鎶�捣
|
|
|
else if (ti.dwFlags & TOUCHEVENTF_UP)
|
|
|
{
|
|
|
if (m_touchDown)
|
|
|
{
|
|
|
- // 计算速度(px/ms)
|
|
|
+ // 计算速度(px/ms)
|
|
|
float velocityPxPerMs = 0.0f;
|
|
|
if (m_samples.size() >= 2)
|
|
|
{
|
|
|
@@ -153,13 +153,13 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
std::int64_t dt = last.tms - first.tms;
|
|
|
if (dt > 0)
|
|
|
{
|
|
|
- // 使用 earliest - latest,保持上方移动为正值
|
|
|
- velocityPxPerMs = static_cast<float>(first.y - last.y) / static_cast<float>(dt);
|
|
|
+ // 与 MOVE 中 dy 方向保持一致:last - first
|
|
|
+ velocityPxPerMs = static_cast<float>(last.y - first.y) / static_cast<float>(dt);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
m_touchDown = false;
|
|
|
- // 保存最后触摸点(确保惯性期间有位置可用)
|
|
|
+ // 保存最后触摸点(确保惯性期间有位置可用)
|
|
|
m_lastTouchPt = pt;
|
|
|
m_lastY = 0;
|
|
|
m_accumWheel = 0;
|
|
|
@@ -168,7 +168,7 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
::PostMessage(target, WM_LBUTTONUP, 0, MAKELPARAM(pt.x, pt.y));
|
|
|
::ReleaseCapture();
|
|
|
|
|
|
- // 启动惯性滚动(如果速度足够大)
|
|
|
+ // 鍚�姩鎯�€ф粴鍔�紙濡傛灉閫熷害瓒冲�澶э級
|
|
|
StartInertia(velocityPxPerMs);
|
|
|
}
|
|
|
}
|
|
|
@@ -177,9 +177,9 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- // 可添加日志:GetTouchInputInfo 失败
|
|
|
+ // 鍙�坊鍔犳棩蹇楋細GetTouchInputInfo 澶辫触
|
|
|
}
|
|
|
- return 0; // 已处理
|
|
|
+ return 0; // 已处理
|
|
|
}
|
|
|
|
|
|
case WM_TIMER:
|
|
|
@@ -192,14 +192,14 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
if (dt <= 0) dt = INERTIA_TIMER_INTERVAL_MS;
|
|
|
m_lastInertiaTms = now;
|
|
|
|
|
|
- // 计算本帧移动的像素
|
|
|
+ // 计算本帧移动的像素
|
|
|
float pixels = m_inertiaVelocityPxPerMs * static_cast<float>(dt);
|
|
|
|
|
|
- // 将像素转换为 wheel units 累积并发送
|
|
|
+ // 将像素转换为 wheel units 累积并发送
|
|
|
int wheelUnits = static_cast<int>(pixels * PIXEL_TO_WHEEL);
|
|
|
m_accumWheel += wheelUnits;
|
|
|
|
|
|
- // 使用保存的最后触摸点(target 的客户区坐标)
|
|
|
+ // 使用保存的最后触摸点(target 的客户区坐标)
|
|
|
POINT pt = m_lastTouchPt;
|
|
|
LPARAM lParamPos = MAKELPARAM(pt.x, pt.y);
|
|
|
|
|
|
@@ -211,11 +211,11 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
::PostMessage(target, WM_MOUSEWHEEL, wParamWheel, lParamPos);
|
|
|
}
|
|
|
|
|
|
- // 衰减速度(指数衰减)
|
|
|
+ // 琛板噺閫熷害锛堟寚鏁拌“鍑忥級
|
|
|
// velocity *= pow(INERTIA_DECAY_PER_MS, dt)
|
|
|
m_inertiaVelocityPxPerMs *= std::pow(INERTIA_DECAY_PER_MS, static_cast<float>(dt));
|
|
|
|
|
|
- // 停止条件
|
|
|
+ // 鍋滄�鏉′欢
|
|
|
if (std::abs(m_inertiaVelocityPxPerMs) < MIN_INERTIA_VELOCITY)
|
|
|
{
|
|
|
StopInertia();
|
|
|
@@ -231,6 +231,6 @@ LRESULT TouchWindowBase::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
- // 若未处理,交给基类
|
|
|
+ // 若未处理,交给基类
|
|
|
return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
|
|
|
}
|