Skip to content

Commit dedfc4f

Browse files
committed
Added validation, corrected time comparison
- bumped version to 1.2.2 - added validation for existence of previous touch data when comparing contact points - touch activity delays now compare against the time stored in TouchActivityEventArgs as intended - removed unused variables from previous movement calculation method
1 parent 7c743eb commit dedfc4f

File tree

5 files changed

+36
-39
lines changed

5 files changed

+36
-39
lines changed

ThreeFingerDrag/application.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Application {
1212

1313
constexpr int VERSION_MAJOR = 1;
1414
constexpr int VERSION_MINOR = 2;
15-
constexpr int VERSION_PATCH = 1;
15+
constexpr int VERSION_PATCH = 2;
1616

1717
constexpr char VERSION_FILE_NAME[] = "version.txt";
1818

ThreeFingerDrag/config/globalconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
constexpr auto DEFAULT_ACCELERATION_FACTOR = 75.0;
88
constexpr auto DEFAULT_PRECISION_CURSOR_SPEED = 0.5;
99
constexpr auto DEFAULT_MOUSE_CURSOR_SPEED = 0.5;
10-
constexpr auto DEFAULT_CANCELLATION_DELAY_MS = 650;
10+
constexpr auto DEFAULT_CANCELLATION_DELAY_MS = 500;
1111

1212
class GlobalConfig {
1313
private:

ThreeFingerDrag/gesture/touch_gestures.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Gestures
1111

1212
void GestureProcessor::ParseRawTouchData(const LPARAM lParam)
1313
{
14-
std::async(std::launch::async, [&]{ InterpretRawInput((HRAWINPUT)lParam); });
14+
auto a = std::async(std::launch::async, [&]{ InterpretRawInput((HRAWINPUT)lParam); });
1515
}
1616

1717
/**
@@ -187,7 +187,7 @@ namespace Gestures
187187
const auto time = std::chrono::high_resolution_clock::now();
188188

189189
// Interpret the touch movement into events
190-
if (!hasContact) {
190+
if (!hasContact && previousHasContact) {
191191
touchUpEvent.RaiseEvent(TouchUpEventArgs(time, &data, previous_data_));
192192
return;
193193
}

ThreeFingerDrag/gesture/touch_listeners.h

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ namespace GestureListeners {
2525

2626
// Check if it's the initial gesture
2727
const bool is_initial_gesture = !config->IsDragging() && args.data->can_perform_gesture;
28-
const auto now = std::chrono::high_resolution_clock::now();
28+
const auto time = args.time;
2929

3030
// If it's the initial gesture, set the gesture start time
3131
if (is_initial_gesture && !config->IsGestureStarted()) {
32-
gesture_start_ = now;
3332
config->SetGestureStarted(true);
33+
gesture_start_ = time;
3434
}
3535

3636
// If there's no previous data, return
3737
if (args.previous_data.contacts.empty())
3838
return;
3939

4040
// Calculate the time elapsed since the initial touchpad contact
41-
std::chrono::duration<float> duration = now - gesture_start_;
41+
std::chrono::duration<float> duration = time - gesture_start_;
4242
const float ms_since_start = duration.count() * 1000.0f;
4343

4444
// Calculate the time elapsed since previous touchpad data was received
45-
duration = now - config->GetLastGesture();
45+
duration = time - config->GetLastGesture();
4646
const float ms_since_last_gesture = duration.count() * 1000.0f;
4747

4848
// Prevents initial jumpy movement due to old data comparison
@@ -54,6 +54,9 @@ namespace GestureListeners {
5454
int valid_touches = 0;
5555
for (int i = 0; i < NUM_TOUCH_CONTACTS_REQUIRED; i++)
5656
{
57+
if (i > args.previous_data.contacts.size() - 1)
58+
continue;
59+
5760
const auto& contact = args.data->contacts[i];
5861
const auto& previous_contact = args.previous_data.contacts[i];
5962

@@ -86,29 +89,20 @@ namespace GestureListeners {
8689
return;
8790

8891
// Centroid calculation
89-
const double divisor = static_cast<double>(NUM_TOUCH_CONTACTS_REQUIRED);
92+
const long double divisor = static_cast<long double>(NUM_TOUCH_CONTACTS_REQUIRED);
9093

9194
accumulated_delta_x_ /= divisor;
9295
accumulated_delta_y_ /= divisor;
9396

94-
// Apply movement acceleration using a logarithmic function
95-
const double gesture_speed = config->GetGestureSpeed();
96-
const double movement_mag = std::sqrt(accumulated_delta_x_ * accumulated_delta_x_ + accumulated_delta_y_ * accumulated_delta_y_);
97-
const double factor = (std::log2(movement_mag + 1)) * (1 + config->GetPrecisionTouchCursorSpeed());
98-
99-
double total_delta_x = accumulated_delta_x_;
100-
double total_delta_y = accumulated_delta_y_;
101-
102-
// Apply the gesture_speed at the end of the calculation
103-
total_delta_x *= (gesture_speed / 100);
104-
total_delta_y *= (gesture_speed / 100);
105-
97+
// Apply movement acceleration
98+
const double gesture_speed = config->GetGestureSpeed() / 100.0;
99+
const double total_delta_x = accumulated_delta_x_ * gesture_speed;
100+
const double total_delta_y = accumulated_delta_y_ * gesture_speed;
106101

107102
// Delay initial dragging gesture movement, and ignore invalid movement actions
108-
if (ms_since_start <= GESTURE_START_THRESHOLD_MS || !(args.data->can_perform_gesture || config->IsDragging())) {
103+
if (ms_since_start <= GESTURE_START_THRESHOLD_MS || !(args.data->can_perform_gesture || config->IsDragging()))
109104
return;
110-
}
111-
105+
112106
config->SetCancellationStarted(false);
113107

114108
// Move the mouse pointer based on the calculated vector
@@ -118,7 +112,7 @@ namespace GestureListeners {
118112

119113
// Set timestamp for when any cursor movement occurred
120114
if (change > 0)
121-
config->SetLastValidMovement(now);
115+
config->SetLastValidMovement(time);
122116

123117
// Start dragging if left mouse is not already down.
124118
if (!config->IsDragging()) {
@@ -140,20 +134,23 @@ namespace GestureListeners {
140134
void OnTouchUp(TouchUpEventArgs args) {
141135
GlobalConfig* config = GlobalConfig::GetInstance();
142136
config->SetGestureStarted(false);
143-
if (config->IsDragging() && !config->IsCancellationStarted()) {
144-
// Calculate the time elapsed since the last valid gesture movement
145-
const auto now = std::chrono::high_resolution_clock::now();
146-
const std::chrono::duration<float> duration = now - config->GetLastValidMovement();
147-
const float ms_since_last_movement = duration.count() * 1000.0f;
148-
149-
// If there hasn't been any movement for same amount of time we will delay, then cancel immediately
150-
if (ms_since_last_movement >= CANCELLATION_TIME_MS) {
151-
CancelGesture();
152-
return;
153-
}
154-
config->SetCancellationStarted(true);
155-
config->SetCancellationTime(now);
137+
138+
if (!config->IsDragging() || config->IsCancellationStarted())
139+
return;
140+
141+
// Calculate the time elapsed since the last valid gesture movement
142+
const auto now = std::chrono::high_resolution_clock::now();
143+
const std::chrono::duration<float> duration = now - config->GetLastValidMovement();
144+
const float ms_since_last_movement = duration.count() * 1000.0f;
145+
146+
// If there hasn't been any movement for same amount of time we will delay, then cancel immediately
147+
if (ms_since_last_movement >= CANCELLATION_TIME_MS) {
148+
CancelGesture();
149+
return;
156150
}
151+
152+
config->SetCancellationStarted(true);
153+
config->SetCancellationTime(now);
157154
}
158155
};
159156
}

inno_script.iss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#define MyAppName "ThreeFingerDrag"
2-
#define MyAppVersion "1.2.1"
2+
#define MyAppVersion "1.2.2"
33
#define MyAppPublisher "Austin Nixholm"
44
#define MyAppURL "https://github.com/austinnixholm/ThreeFingerDrag"
55
#define MyAppExeName "ThreeFingerDrag.exe"

0 commit comments

Comments
 (0)