-
Notifications
You must be signed in to change notification settings - Fork 1
/
ofkMultiTouchEvent.cpp
executable file
·160 lines (128 loc) · 4.34 KB
/
ofkMultiTouchEvent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// MultiTouchEvent.cpp
// advancedEventsExample
//
// Created by Shunichi Kasahara on 12/03/22.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#include "ofkMultiTouchEvent.h"
ofkMultiTouchEvent::ofkMultiTouchEvent():mCurrentTouchPointNum(0)
{
}
void ofkMultiTouchEvent::setTouchPos(int index, float x, float y)
{
//CHECK VAILED TOUCH NUM
if(index< TOUCHNUMMAX)
{
mTouchPos[index].x = x;
mTouchPos[index].y = y;
}
}
void ofkMultiTouchEvent::touchDownMulti(ofTouchEventArgs &touch)
{
mCurrentTouchPointNum = touch.numTouches;
/*
string mes = "ofTouchEventArgs event: id" + ofToString(touch.id) + "in " + ofToString(mCurrentTouchPointNum) + " pos" + ofToString(touch.x);
cout << mes << "\n";
*/
//CHECK VAILED TOUCH NUM
if(touch.id < TOUCHNUMMAX)
{
setTouchPos(touch.id,touch.x, touch.y);
//mTouchPos[touch.id].x = touch.x;
//mTouchPos[touch.id].y = touch.y;
}
//2 FINEGR
if(1 == touch.id)
{
//when 2 finger interaction start
//initialize two finger diference vector
mVecBetweenTwoFingerStart = mVecBetweenTwoFinger = mTouchPos[1] - mTouchPos[0];
//init average vector
mAvarageVector = ( mTouchPos[1] + mTouchPos[0] ) / 2.0f;
ofVec3f Diffvec = ( mTouchPos[1] - mTouchPos[0] );
mDegTwoFinger = -180.0f * ((float)atan(Diffvec.y / Diffvec.x)) / PI;
}
}
void ofkMultiTouchEvent::touchMoveMulti(ofTouchEventArgs &touch)
{
mCurrentTouchPointNum = touch.numTouches;
//CHECK VAILED TOUCH NUM
//THIS WILL BECAME BUG
if(touch.id < TOUCHNUMMAX)
{
setTouchPos(touch.id,touch.x, touch.y);
}
if(2 == touch.numTouches)
{
calcAndNotifyTouchEvent();
}
}
void ofkMultiTouchEvent::touchUpMulti(ofTouchEventArgs &touch)
{
mCurrentTouchPointNum = touch.numTouches;
}
void ofkMultiTouchEvent::calcAndNotifyTouchEvent()
{
ofVec2f updateBetweenVector;
updateBetweenVector = mTouchPos[1] - mTouchPos[0];
ofVec3f Diffvec = ( mTouchPos[1] - mTouchPos[0] );
float updateAngle = -180.0f * ((float)atan(Diffvec.y / Diffvec.x)) / PI;
//this is new update two finger ave point;
ofVec2f updateTwoFingerAve = ( mTouchPos[1] + mTouchPos[0] ) / 2.0f;
ofkMultiTouchEventArgs arg;
arg.time = ofGetSystemTimeMicros();
if(updateAngle - mDegTwoFinger > 150)
{
arg.angleDif = (updateAngle -180 - mDegTwoFinger) ;
}
else if(updateAngle - mDegTwoFinger < -150)
{
arg.angleDif = updateAngle + 180 - mDegTwoFinger;
}
else
{
arg.angleDif = updateAngle - mDegTwoFinger;
}
/*
string mes = "ANGEL DIF" + ofToString(arg.angleDif);
cout << mes << "\n";
*/
arg.angle = updateAngle;
arg.pinchLength = (updateBetweenVector).length();
arg.pinchLengthDif = updateBetweenVector.length() - mVecBetweenTwoFinger.length();
arg.pinchLengthFromStart = updateBetweenVector.length() - mVecBetweenTwoFingerStart.length();
arg.centroidPosDif = (updateTwoFingerAve - mAvarageVector);
arg.centroidPos = mAvarageVector = updateTwoFingerAve;
ofNotifyEvent(touchTwoFingerEvent , arg , this);
mVecBetweenTwoFinger = updateBetweenVector;
mDegTwoFinger = updateAngle;
}
void ofkMultiTouchEvent::debugDraw()
{
ofPushMatrix();
ofPushStyle();
ofSetupScreen();
ofEnableBlendMode(OF_BLENDMODE_ALPHA);
ofEnableSmoothing();
if(2 == mCurrentTouchPointNum)
{
ofSetColor(255,25,100,20);
//REDNER PINCH CIRCLE
ofVec3f Centvec = ( mTouchPos[1] + mTouchPos[0] ) / 2.0;
ofCircle(Centvec.x, Centvec.y, ( mTouchPos[1] - mTouchPos[0] ).length() / 2);
ofSetColor(255,25,100,50);
ofVec3f Diffvec = ( mTouchPos[1] - mTouchPos[0] );
ofLine(mTouchPos[1].x, mTouchPos[1].y, mTouchPos[0].x, mTouchPos[0].y);
ofSetColor(255,100,100,50);
string str;
str = "angle " + ofToString(mDegTwoFinger);
ofDrawBitmapString(str, Centvec);
//ave finger pos rendering
ofSetColor(25,100, 255, 20);
ofCircle(mAvarageVector.x, mAvarageVector.y, 30);
}
//RENDER TWO FINGER ANGLE
ofPopStyle();
ofPopMatrix();
}