add tap event on iOS

This commit is contained in:
2019-11-26 21:44:01 +01:00
parent 9eafcecde9
commit 64f6b90911
5 changed files with 80 additions and 3 deletions

View File

@@ -344,13 +344,19 @@ std::recursive_mutex lock_mutex;
float get_force(UITouch* t)
{
//glm::pow(t.force / t.maximumPossibleForce, 0.5);
return glm::clamp(t.force / 2.0, 0.0, 1.0);
return glm::clamp(t.force / 1.0, 0.0, 1.0);
}
std::set<UITouch*> ignored_touch;
std::map<UITouch*, glm::vec2> touch_start;
int max_touch_count = 0;
bool is_tap = true;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSUInteger n = [[event allTouches] count];
UITouch *touch = [[event allTouches] anyObject];
max_touch_count = n;
CGPoint touchLocation = [touch locationInView:self.view];
float scale = self.view.contentScaleFactor;
@@ -365,7 +371,15 @@ std::set<UITouch*> ignored_touch;
// apple pencil
if (touch.type == UITouchType::UITouchTypeStylus)
{
pen_down = true;
is_tap = false;
}
else
{
is_tap = true;
touch_start.clear();
}
App::I->ui_task_async([touchLocation, scale, f=force, source] {
App::I->mouse_down(0, touchLocation.x * scale, touchLocation.y * scale, f, source, 0);
@@ -375,9 +389,11 @@ std::set<UITouch*> ignored_touch;
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
is_tap = false;
NSUInteger n = [[event allTouches] count];
float scale = self.view.contentScaleFactor;
max_touch_count = std::max<int>(max_touch_count, n);
std::vector<UITouch*> valid_touches;
for (int i = 0; i < n; i++)
{
@@ -386,6 +402,11 @@ std::set<UITouch*> ignored_touch;
if (it == ignored_touch.end())
{
valid_touches.push_back(t);
if (touch_start.find(t) == touch_start.end())
{
auto loc = [t locationInView:self.view];
touch_start[t] = { loc.x, loc.y };
}
}
else
{
@@ -458,10 +479,35 @@ std::set<UITouch*> ignored_touch;
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
static NSTimeInterval last_time = 0;
static int tap_count = 1;
int n = (int)[[event allTouches] count];
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
float scale = self.view.contentScaleFactor;
if (touch.timestamp - last_time < 0.2)
tap_count++;
else
tap_count = 1;
last_time = touch.timestamp;
glm::vec2 center(0);
float max_dist = 0;
for (UITouch* t : [event allTouches])
{
auto loc = [t locationInView:self.view];
center += glm::vec2(loc.x, loc.y);
if (touch_start.find(t) != touch_start.end())
{
max_dist = std::max<float>(max_dist, glm::distance(touch_start[t], { loc.x, loc.y }));
}
}
center /= (float)n;
//LOG("max touches (%d-%d) (max %d) dist %f",
// n, (int)touch_start.size(), max_touch_count, (int)max_dist);
if (ignored_touch.count(touch))
{
ignored_touch.erase(touch);
@@ -471,11 +517,13 @@ std::set<UITouch*> ignored_touch;
kEventSource source = touch.type == UITouchType::UITouchTypeStylus ? kEventSource::Stylus : kEventSource::Touch;
pen_down = false;
App::I->ui_task_async([tc=t_count, touchLocation, scale, source] {
App::I->ui_task_async([tc=t_count, touchLocation, scale, source, n, center, max_dist] {
if (tc == 2)
App::I->gesture_end();
else
App::I->mouse_up(0, touchLocation.x * scale, touchLocation.y * scale, source, 0);
if (is_tap || max_dist < 10)
App::I->touch_tap(center, n == 1 ? max_touch_count : n, tap_count);
});
t_count = 0;