-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmouseStateMachine.js
154 lines (120 loc) · 3.31 KB
/
mouseStateMachine.js
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
//positions are viewport centralized
function MouseStateMachine(w,h, fovy){
var self = {};
self.width = w;
self.height = h;
self.mousePos = undefined;
self.prevMousePos = undefined;
self.currentState = BaseMouseState(self);
self.updateClosestDraggablePointToMouseRay = function(){
draggablePoints.updateClosestPointToRay(self.getRayFromMousePos(), self.getNDCMousePos());
}
self.getMouseDelta = function(){
return subtract(self.mousePos, self.prevMousePos);
}
self.getRayFromMousePos = function(){
return camera.getRayFromNDCPos(viewPortToNDC(self.mousePos, self.width, self.height));
}
self.getNDCFromViewPortPos = function(vpPos){
return viewPortToNDC(vpPos, self.width, self.height);
}
self.getNDCMousePos = function(){
return viewPortToNDC(self.mousePos, self.width, self.height);
}
self.mousemove = function(pos){
self.mousePos = pos;
self.currentState.mousemove();
self.prevMousePos = pos;
}
self.mousedown = function(pos){
self.mousePos = self.prevMousePos = pos;
self.currentState.mousedown();
}
self.mouseup= function(pos){
self.currentState.mouseup();
}
self.mousewheel = function(delta){
self.currentState.mousewheel(delta);
}
return self;
}
function BaseMouseState(msm){
return {
mousemove: function(){
msm.updateClosestDraggablePointToMouseRay();
},
mouseup: function(){
msm.currentState = BaseMouseState(msm);
},
mousedown: function(){
if(draggablePoints.isDragAxisSelected())
msm.currentState = PointDragMS(msm);
else
msm.currentState = PinchRotateMS(msm);
},
mousewheel: function(delta){
camera.zoom(delta);
msm.updateClosestDraggablePointToMouseRay();
}
};
}
function PinchRotateMS(msm){
var self = BaseMouseState(msm);
draggablePoints.deactivate();
self.mousemove = function(){
var deltaDrag = msm.getMouseDelta();
if(length(deltaDrag)>0.1){
var start = getSphereIntercept(msm.prevMousePos, msm.width, msm.height);
var end = getSphereIntercept(msm.mousePos, msm.width, msm.height);
var angle = Math.acos(dot(normalize(start), normalize(end)));
var axis = cross(start, end);
camera.rotateAroundWSOrigin(angle, axis);
}
}
return self;
}
function PointDragMS(msm){
var self = BaseMouseState(msm);
self.mousemove = function(){
var e = msm.getNDCFromViewPortPos(msm.mousePos);
var s = msm.getNDCFromViewPortPos(msm.prevMousePos);
draggablePoints.drag(s,e);
}
self.mousewheel = function(){};
return self;
}
//position relative to canvas
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return vec2(evt.clientX - rect.left, evt.clientY - rect.top);
}
function viewPortToNDC(pos, w, h){
var x = pos[0]/(w/2);
var y = pos[1]/(h/2);
return vec2(x,y);
}
function rayVectorFromNDC(pos, fovy, aspect){
var z = -1.0 / Math.tan( radians(fovy) / 2 );
var x = pos[0]*aspect;
var y = pos[1];
return vec3(x,y,z);
}
function getCentralizedCoords(v, w, h){
return vec2(v[0] - w/2, -(v[1] - h/2));
}
function getSphereIntercept(v, w, h){
var r = h;
var l = length(v);
//when outside of sphere map vector to circle
//(sphere with plane z=0 intercept) x^2 + y^2 = r^2
if(r<l){
var inter = scale(r, getNormalized(v));
return vec3(inter[0],inter[1],0);
}
else{
var x = v[0];
var y = v[1];
var z = -Math.sqrt(r*r - x*x - y*y);
return vec3(x,y,z);
}
}