Skip to content

Commit f12b790

Browse files
author
Raymond Hulha
committed
Added a global variable to allow skipping the default mouse move listener: skipDefaultMouseMoveListener.
Changed the Vector.cross function to be like the rest, added a cross2 for the old usage. Made most Vector functions return _this_ to allow chaining.
1 parent 0e0a92b commit f12b790

4 files changed

Lines changed: 65 additions & 40 deletions

File tree

lib/src/input.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ int clientX=0;
77
int clientY=0;
88
int mouseX=0;
99
int mouseY=0;
10+
bool skipDefaultMouseMoveListener = false;
1011

1112

1213
void setUpCapture()
1314
{
1415
HTML.document.onKeyDown.listen( (HTML.KeyboardEvent e) { currentlyPressedKeys[e.keyCode] = true; });
1516
HTML.document.onKeyUp.listen( (HTML.KeyboardEvent e) { currentlyPressedKeys[e.keyCode] = null; });
1617

17-
HTML.document.onMouseMove.listen( (HTML.MouseEvent e) {
18-
e.preventDefault();
19-
20-
clientX = e.client.x;
21-
clientY = HTML.window.innerHeight - e.client.y;
22-
mouseX = e.client.x-(HTML.window.innerWidth~/2);
23-
mouseY = -(e.client.y-(HTML.window.innerHeight~/2));
24-
25-
});
18+
if( ! skipDefaultMouseMoveListener )
19+
{
20+
HTML.document.onMouseMove.listen( (HTML.MouseEvent e) {
21+
e.preventDefault();
22+
23+
clientX = e.client.x;
24+
clientY = HTML.window.innerHeight - e.client.y;
25+
mouseX = e.client.x-(HTML.window.innerWidth~/2);
26+
mouseY = -(e.client.y-(HTML.window.innerHeight~/2));
27+
28+
});
29+
}
2630

2731
HTML.document.onMouseDown.listen( (HTML.MouseEvent e) {
2832
e.preventDefault();

lib/src/matrix4.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class Matrix4 {
289289
}
290290

291291
// dest = Vector4
292-
void multiplyVec4( List<num> vec4) {
292+
void multiplyVec4( List<double> vec4) {
293293
var x = vec4[0], y = vec4[1], z = vec4[2], w = vec4[3];
294294

295295
vec4[0] = array[0] * x + array[4] * y + array[8] * z + array[12] * w;
@@ -347,9 +347,9 @@ class Matrix4 {
347347
// vec3.direction goes from argument2 to argument1 and normalizes
348348
// as we want a back vector, we want from target to this spatial position
349349
newBack.direction( eye, target );
350-
newRight.cross( up, newBack);
350+
newRight.cross2( up, newBack);
351351
newRight.normalize();
352-
newUp.cross(newBack, newRight);
352+
newUp.cross2(newBack, newRight);
353353

354354
array[ 0] = newRight[0];
355355
array[ 1] = newUp[0];

lib/src/shapes/torusknot.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ MeshData createTorusKnotInternal( {double radius:20.0, double tube:4.0, int seg
2929
n.y = p2.y + p1.y;
3030
n.z = p2.z + p1.z;
3131

32-
bitan.cross( tang, n).normalize();
33-
n.cross( bitan, tang ).normalize();
32+
bitan.cross2( tang, n).normalize();
33+
n.cross2( bitan, tang ).normalize();
3434

3535
for ( int j = 0; j < segmentsT; ++ j ) {
3636
double v = j / segmentsT * 2 * Math.PI;

lib/src/vector.dart

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Vector {
1515

1616
Vector.fromList( this.array);
1717

18-
Vector( [num x=0.0, num y=0.0, num z=0.0])
18+
Vector( [double x=0.0, double y=0.0, double z=0.0])
1919
{
2020
array[0] = x;
2121
array[1] = y;
@@ -31,37 +31,42 @@ class Vector {
3131
array[index] = value;
3232
}
3333

34-
void set( Vector v) {
34+
Vector set( Vector v) {
3535
array[0] = v[0];
3636
array[1] = v[1];
3737
array[2] = v[2];
38+
return this;
3839
}
3940

40-
void add( Vector v) {
41+
Vector add( Vector v) {
4142
array[0] += v[0];
4243
array[1] += v[1];
4344
array[2] += v[2];
45+
return this;
4446
}
4547

46-
void subtract( Vector v) {
48+
Vector subtract( Vector v) {
4749
array[0] -= v[0];
4850
array[1] -= v[1];
4951
array[2] -= v[2];
52+
return this;
5053
}
5154

52-
void scale( num val) {
55+
Vector scale( num val) {
5356
array[0] *= val;
5457
array[1] *= val;
5558
array[2] *= val;
59+
return this;
5660
}
5761

58-
void negate() {
62+
Vector negate() {
5963
array[0] = -array[0];
6064
array[1] = -array[1];
6165
array[2] = -array[2];
66+
return this;
6267
}
6368

64-
void normalize() {
69+
Vector normalize() {
6570
double x = array[0], y = array[1], z = array[2];
6671
double len = Math.sqrt(x * x + y * y + z * z);
6772

@@ -79,12 +84,13 @@ class Vector {
7984
array[1] = y * len;
8085
array[2] = z * len;
8186
}
87+
return this;
8288
}
8389

8490
// I think the from and to is swapped, but it's the same in glmatrix...
85-
void direction( Vector from, Vector to)
91+
Vector direction( Vector from, Vector to)
8692
{
87-
num x = from[0] - to[0],
93+
double x = from[0] - to[0],
8894
y = from[1] - to[1],
8995
z = from[2] - to[2],
9096
len = Math.sqrt(x * x + y * y + z * z);
@@ -99,11 +105,15 @@ class Vector {
99105
array[1] = y * len;
100106
array[2] = z * len;
101107
}
102-
108+
return this;
103109
}
104110

105-
Vector cross(Vector vec1, Vector vec2) {
106-
num x = vec1[0], y = vec1[1], z = vec1[2],
111+
Vector cross( Vector vec2) {
112+
return cross2( this, vec2);
113+
}
114+
115+
Vector cross2( Vector vec1, Vector vec2) {
116+
double x = vec1[0], y = vec1[1], z = vec1[2],
107117
x2 = vec2[0], y2 = vec2[1], z2 = vec2[2];
108118

109119
array[0] = y * z2 - z * y2;
@@ -116,10 +126,11 @@ class Vector {
116126
return array[0] * vec2[0] + array[1] * vec2[1] + array[2] * vec2[2];
117127
}
118128

119-
void lerp( Vector v, double lerp) {
129+
Vector lerp( Vector v, double lerp) {
120130
array[0] += lerp * (v[0] - array[0]);
121131
array[1] += lerp * (v[1] - array[1]);
122132
array[2] += lerp * (v[2] - array[2]);
133+
return this;
123134
}
124135

125136
double dist( Vector to) {
@@ -130,35 +141,45 @@ class Vector {
130141
return Math.sqrt(x*x + y*y + z*z);
131142
}
132143

144+
double lengthSquared() {
145+
double x = array[0],
146+
y = array[1],
147+
z = array[2];
148+
return (x*x + y*y + z*z);
149+
}
150+
151+
double length() {
152+
return Math.sqrt(lengthSquared());
153+
}
154+
133155
// if we initialize the matrix here we get an endless loop
134156
// because Matrix also creates a new Vector...
135157
Matrix4 m = null;
136-
List<num> v = new List<num>(4);
158+
List<double> _v = new List<double>(4);
137159

138-
bool unproject( Matrix4 view, Matrix4 proj, List<num> viewport) {
160+
bool unproject( Matrix4 view, Matrix4 proj, List<double> viewport) {
139161

140162
if( m == null)
141163
m = new Matrix4();
142164

143-
v[0] = (array[0] - viewport[0]) * 2.0 / viewport[2] - 1.0;
144-
v[1] = (array[1] - viewport[1]) * 2.0 / viewport[3] - 1.0;
145-
v[2] = 2.0 * array[2] - 1.0;
146-
v[3] = 1.0;
165+
_v[0] = (array[0] - viewport[0]) * 2.0 / viewport[2] - 1.0;
166+
_v[1] = (array[1] - viewport[1]) * 2.0 / viewport[3] - 1.0;
167+
_v[2] = 2.0 * array[2] - 1.0;
168+
_v[3] = 1.0;
147169

148170
m.setElements(proj);
149171
m.multiplyWith( view );
150172
if(!m.invert()) { return false; }
151173

152-
m.multiplyVec4( v);
153-
if(v[3] == 0.0) { return false; }
174+
m.multiplyVec4( _v);
175+
if(_v[3] == 0.0) { return false; }
154176

155-
array[0] = v[0] / v[3];
156-
array[1] = v[1] / v[3];
157-
array[2] = v[2] / v[3];
177+
array[0] = _v[0] / _v[3];
178+
array[1] = _v[1] / _v[3];
179+
array[2] = _v[2] / _v[3];
158180
}
159181

160182
String toString() {
161183
return array.toString();
162184
}
163-
164-
}
185+
}

0 commit comments

Comments
 (0)