Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
5e9b2fba
Commit
5e9b2fba
authored
Nov 02, 2015
by
Kris Giesing
Committed by
kgiesing
Nov 03, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Style pass on velocity tracker and event recorder
parent
24ea0f0d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
137 additions
and
122 deletions
+137
-122
lsq_solver.dart
packages/flutter/lib/src/gestures/lsq_solver.dart
+58
-50
velocity_tracker.dart
packages/flutter/lib/src/gestures/velocity_tracker.dart
+69
-65
event_recorder.dart
packages/flutter/lib/src/widgets/event_recorder.dart
+10
-7
No files found.
packages/flutter/lib/src/gestures/lsq_solver.dart
View file @
5e9b2fba
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
"dart:math"
as
math
;
import
"dart:math"
as
math
;
import
"dart:typed_data"
;
import
"dart:typed_data"
;
class
Vector
{
class
_
Vector
{
Vector
(
int
size
)
_
Vector
(
int
size
)
:
_offset
=
0
,
_length
=
size
,
_elem
=
new
Float64List
(
size
);
:
_offset
=
0
,
_length
=
size
,
_elem
ents
=
new
Float64List
(
size
);
Vector
.
fromValues
(
List
<
double
>
values
)
_
Vector
.
fromValues
(
List
<
double
>
values
)
:
_offset
=
0
,
_length
=
values
.
length
,
_elem
=
values
;
:
_offset
=
0
,
_length
=
values
.
length
,
_elem
ents
=
values
;
Vector
.
fromVOL
(
List
<
double
>
values
,
int
offset
,
int
length
)
_
Vector
.
fromVOL
(
List
<
double
>
values
,
int
offset
,
int
length
)
:
_offset
=
offset
,
_length
=
length
,
_elem
=
values
;
:
_offset
=
offset
,
_length
=
length
,
_elem
ents
=
values
;
int
get
length
=>
_length
;
int
get
length
=>
_length
;
operator
[](
int
i
)
=>
_elem
[
i
+
_offset
];
operator
[](
int
i
)
=>
_elem
ents
[
i
+
_offset
];
operator
[]=(
int
i
,
double
value
)
=>
_elem
[
i
+
_offset
]
=
value
;
operator
[]=(
int
i
,
double
value
)
=>
_elem
ents
[
i
+
_offset
]
=
value
;
operator
*(
Vector
a
)
{
operator
*(
_
Vector
a
)
{
double
result
=
0.0
;
double
result
=
0.0
;
for
(
int
i
=
0
;
i
<
_length
;
i
++)
{
for
(
int
i
=
0
;
i
<
_length
;
i
++)
{
result
+=
this
[
i
]
*
a
[
i
];
result
+=
this
[
i
]
*
a
[
i
];
...
@@ -38,28 +42,32 @@ class Vector {
...
@@ -38,28 +42,32 @@ class Vector {
final
int
_offset
;
final
int
_offset
;
final
int
_length
;
final
int
_length
;
final
List
<
double
>
_elem
;
final
List
<
double
>
_elem
ents
;
}
}
class
Matrix
{
class
_
Matrix
{
Matrix
(
int
rows
,
int
cols
)
_
Matrix
(
int
rows
,
int
cols
)
:
_rows
=
rows
,
:
_rows
=
rows
,
_cols
=
cols
,
_col
umn
s
=
cols
,
_elem
=
new
Float64List
(
rows
*
cols
);
_elem
ents
=
new
Float64List
(
rows
*
cols
);
double
get
(
int
row
,
int
col
)
=>
_elem
[
row
*
_col
s
+
col
];
double
get
(
int
row
,
int
col
)
=>
_elem
ents
[
row
*
_column
s
+
col
];
void
set
(
int
row
,
int
col
,
double
value
)
{
void
set
(
int
row
,
int
col
,
double
value
)
{
_elem
[
row
*
_col
s
+
col
]
=
value
;
_elem
ents
[
row
*
_column
s
+
col
]
=
value
;
}
}
Vector
getRow
(
int
row
)
=>
new
Vector
.
fromVOL
(
_elem
,
row
*
_cols
,
_cols
);
_Vector
getRow
(
int
row
)
=>
new
_Vector
.
fromVOL
(
_elements
,
row
*
_columns
,
_columns
);
String
toString
()
{
String
toString
()
{
String
result
=
""
;
String
result
=
""
;
for
(
int
i
=
0
;
i
<
_rows
;
i
++)
{
for
(
int
i
=
0
;
i
<
_rows
;
i
++)
{
if
(
i
>
0
)
if
(
i
>
0
)
result
+=
"; "
;
result
+=
"; "
;
for
(
int
j
=
0
;
j
<
_cols
;
j
++)
{
for
(
int
j
=
0
;
j
<
_col
umn
s
;
j
++)
{
if
(
j
>
0
)
if
(
j
>
0
)
result
+=
", "
;
result
+=
", "
;
result
+=
get
(
i
,
j
).
toString
();
result
+=
get
(
i
,
j
).
toString
();
...
@@ -69,8 +77,8 @@ class Matrix {
...
@@ -69,8 +77,8 @@ class Matrix {
}
}
final
int
_rows
;
final
int
_rows
;
final
int
_cols
;
final
int
_col
umn
s
;
final
List
<
double
>
_elem
;
final
List
<
double
>
_elem
ents
;
}
}
class
PolynomialFit
{
class
PolynomialFit
{
...
@@ -99,10 +107,9 @@ class LeastSquaresSolver {
...
@@ -99,10 +107,9 @@ class LeastSquaresSolver {
// Shorthands for the purpose of notation equivalence to original C++ code
// Shorthands for the purpose of notation equivalence to original C++ code
final
int
m
=
x
.
length
;
final
int
m
=
x
.
length
;
final
int
n
=
degree
+
1
;
final
int
n
=
degree
+
1
;
final
List
<
double
>
out_b
=
result
.
coefficients
;
// Expand the X vector to a matrix A, pre-multiplied by the weights.
// Expand the X vector to a matrix A, pre-multiplied by the weights.
Matrix
a
=
new
Matrix
(
n
,
m
);
_Matrix
a
=
new
_
Matrix
(
n
,
m
);
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
a
.
set
(
0
,
h
,
w
[
h
]);
a
.
set
(
0
,
h
,
w
[
h
]);
for
(
int
i
=
1
;
i
<
n
;
i
++)
{
for
(
int
i
=
1
;
i
<
n
;
i
++)
{
...
@@ -113,15 +120,15 @@ class LeastSquaresSolver {
...
@@ -113,15 +120,15 @@ class LeastSquaresSolver {
// Apply the Gram-Schmidt process to A to obtain its QR decomposition.
// Apply the Gram-Schmidt process to A to obtain its QR decomposition.
// Orthonormal basis, column-major ordVectorer.
// Orthonormal basis, column-major ordVectorer.
Matrix
q
=
new
Matrix
(
n
,
m
);
_Matrix
q
=
new
_
Matrix
(
n
,
m
);
// Upper triangular matrix, row-major order.
// Upper triangular matrix, row-major order.
Matrix
r
=
new
Matrix
(
n
,
n
);
_Matrix
r
=
new
_
Matrix
(
n
,
n
);
for
(
int
j
=
0
;
j
<
n
;
j
++)
{
for
(
int
j
=
0
;
j
<
n
;
j
++)
{
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
q
.
set
(
j
,
h
,
a
.
get
(
j
,
h
));
q
.
set
(
j
,
h
,
a
.
get
(
j
,
h
));
}
}
for
(
int
i
=
0
;
i
<
j
;
i
++)
{
for
(
int
i
=
0
;
i
<
j
;
i
++)
{
double
dot
=
q
.
getRow
(
j
)
*
q
.
getRow
(
i
);
double
dot
=
q
.
getRow
(
j
)
*
q
.
getRow
(
i
);
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
q
.
set
(
j
,
h
,
q
.
get
(
j
,
h
)
-
dot
*
q
.
get
(
i
,
h
));
q
.
set
(
j
,
h
,
q
.
get
(
j
,
h
)
-
dot
*
q
.
get
(
i
,
h
));
}
}
...
@@ -133,56 +140,57 @@ class LeastSquaresSolver {
...
@@ -133,56 +140,57 @@ class LeastSquaresSolver {
return
null
;
return
null
;
}
}
double
invNorm
=
1.0
/
norm
;
double
inv
erse
Norm
=
1.0
/
norm
;
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
q
.
set
(
j
,
h
,
q
.
get
(
j
,
h
)
*
invNorm
);
q
.
set
(
j
,
h
,
q
.
get
(
j
,
h
)
*
inv
erse
Norm
);
}
}
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
r
.
set
(
j
,
i
,
i
<
j
?
0.0
:
q
.
getRow
(
j
)
*
a
.
getRow
(
i
));
r
.
set
(
j
,
i
,
i
<
j
?
0.0
:
q
.
getRow
(
j
)
*
a
.
getRow
(
i
));
}
}
}
}
// Solve R B = Qt W Y to find B. This is easy because R is upper triangular.
// Solve R B = Qt W Y to find B. This is easy because R is upper triangular.
// We just work from bottom-right to top-left calculating B's coefficients.
// We just work from bottom-right to top-left calculating B's coefficients.
Vector
wy
=
new
Vector
(
m
);
_Vector
wy
=
new
_
Vector
(
m
);
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
wy
[
h
]
=
y
[
h
]
*
w
[
h
];
wy
[
h
]
=
y
[
h
]
*
w
[
h
];
}
}
for
(
int
i
=
n
;
i
--
!=
0
;)
{
for
(
int
i
=
n
;
i
--
!=
0
;)
{
out_b
[
i
]
=
q
.
getRow
(
i
)
*
wy
;
result
.
coefficients
[
i
]
=
q
.
getRow
(
i
)
*
wy
;
for
(
int
j
=
n
-
1
;
j
>
i
;
j
--)
{
for
(
int
j
=
n
-
1
;
j
>
i
;
j
--)
{
out_b
[
i
]
-=
r
.
get
(
i
,
j
)
*
out_b
[
j
];
result
.
coefficients
[
i
]
-=
r
.
get
(
i
,
j
)
*
result
.
coefficients
[
j
];
}
}
out_b
[
i
]
/=
r
.
get
(
i
,
i
);
result
.
coefficients
[
i
]
/=
r
.
get
(
i
,
i
);
}
}
// Calculate the coefficient of determination as 1 - (SSerr / SStot) where
// Calculate the coefficient of determination (confidence) as:
// SSerr is the residual sum of squares (variance of the error),
// 1 - (sumSquaredError / sumSquaredTotal)
// and SStot is the total sum of squares (variance of the data) where each
// where sumSquaredError is the residual sum of squares (variance of the
// has been weighted.
// error), and sumSquaredTotal is the total sum of squares (variance of the
double
ymean
=
0.0
;
// data) where each has been weighted.
double
yMean
=
0.0
;
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
y
m
ean
+=
y
[
h
];
y
M
ean
+=
y
[
h
];
}
}
y
m
ean
/=
m
;
y
M
ean
/=
m
;
double
s
ser
r
=
0.0
;
double
s
umSquaredErro
r
=
0.0
;
double
s
stot
=
0.0
;
double
s
umSquaredTotal
=
0.0
;
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
for
(
int
h
=
0
;
h
<
m
;
h
++)
{
double
err
=
y
[
h
]
-
out_b
[
0
];
double
err
=
y
[
h
]
-
result
.
coefficients
[
0
];
double
term
=
1.0
;
double
term
=
1.0
;
for
(
int
i
=
1
;
i
<
n
;
i
++)
{
for
(
int
i
=
1
;
i
<
n
;
i
++)
{
term
*=
x
[
h
];
term
*=
x
[
h
];
err
-=
term
*
out_b
[
i
];
err
-=
term
*
result
.
coefficients
[
i
];
}
}
s
ser
r
+=
w
[
h
]
*
w
[
h
]
*
err
*
err
;
s
umSquaredErro
r
+=
w
[
h
]
*
w
[
h
]
*
err
*
err
;
double
v
=
y
[
h
]
-
y
m
ean
;
double
v
=
y
[
h
]
-
y
M
ean
;
s
stot
+=
w
[
h
]
*
w
[
h
]
*
v
*
v
;
s
umSquaredTotal
+=
w
[
h
]
*
w
[
h
]
*
v
*
v
;
}
}
double
det
=
sstot
>
0.000001
?
1.0
-
(
sserr
/
sstot
)
:
1.0
;
result
.
confidence
=
sumSquaredTotal
>
0.000001
?
1.0
-
(
sumSquaredError
/
sumSquaredTotal
)
:
result
.
confidence
=
det
;
1.0
;
return
result
;
return
result
;
}
}
...
...
packages/flutter/lib/src/gestures/velocity_tracker.dart
View file @
5e9b2fba
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import
'dart:ui'
as
ui
;
import
'dart:ui'
as
ui
;
import
'lsq_solver.dart'
;
import
'lsq_solver.dart'
;
...
@@ -10,62 +14,62 @@ class GestureVelocity {
...
@@ -10,62 +14,62 @@ class GestureVelocity {
final
double
y
;
final
double
y
;
}
}
class
Estimator
{
class
_
Estimator
{
int
degree
;
int
degree
;
double
time
;
double
time
;
List
<
double
>
x
coeff
;
List
<
double
>
x
Coefficients
;
List
<
double
>
y
coeff
;
List
<
double
>
y
Coefficients
;
double
confidence
;
double
confidence
;
String
toString
()
{
String
toString
()
{
String
result
=
"Estimator(degree: "
+
degree
.
toString
();
String
result
=
"Estimator(degree: "
+
degree
.
toString
();
result
+=
", time: "
+
time
.
toString
();
result
+=
", time: "
+
time
.
toString
();
result
+=
", confidence: "
+
confidence
.
toString
();
result
+=
", confidence: "
+
confidence
.
toString
();
result
+=
", x
coeff: "
+
(
new
Vector
.
fromValues
(
xcoeff
))
.
toString
();
result
+=
", x
Coefficients: "
+
xCoefficients
.
toString
();
result
+=
", y
coeff: "
+
(
new
Vector
.
fromValues
(
ycoeff
))
.
toString
();
result
+=
", y
Coefficients: "
+
yCoefficients
.
toString
();
return
result
;
return
result
;
}
}
}
}
abstract
class
VelocityTrackerStrategy
{
abstract
class
_
VelocityTrackerStrategy
{
void
addMovement
(
double
timeStamp
,
double
x
,
double
y
);
void
addMovement
(
double
timeStamp
,
double
x
,
double
y
);
bool
getEstimator
(
Estimator
estimator
);
bool
getEstimator
(
_
Estimator
estimator
);
void
clear
();
void
clear
();
}
}
enum
Weighting
{
enum
_
Weighting
{
WEIGHTING_NONE
,
weightingNone
,
WEIGHTING_DELTA
,
weightingDelta
,
WEIGHTING_CENTRAL
,
weightingCentral
,
WEIGHTING_RECENT
weightingRecent
}
}
class
Movement
{
class
_
Movement
{
double
event
_t
ime
=
0.0
;
double
event
T
ime
=
0.0
;
ui
.
Point
position
=
ui
.
Point
.
origin
;
ui
.
Point
position
=
ui
.
Point
.
origin
;
}
}
class
LeastSquaresVelocityTrackerStrategy
extends
VelocityTrackerStrategy
{
class
_LeastSquaresVelocityTrackerStrategy
extends
_
VelocityTrackerStrategy
{
static
const
int
kHistorySize
=
20
;
static
const
int
kHistorySize
=
20
;
static
const
int
kHorizonM
S
=
100
;
static
const
int
kHorizonM
illiseconds
=
100
;
LeastSquaresVelocityTrackerStrategy
(
this
.
degree
,
this
.
weighting
)
_
LeastSquaresVelocityTrackerStrategy
(
this
.
degree
,
this
.
weighting
)
:
_index
=
0
,
_movements
=
new
List
<
Movement
>(
kHistorySize
);
:
_index
=
0
,
_movements
=
new
List
<
_
Movement
>(
kHistorySize
);
final
int
degree
;
final
int
degree
;
final
Weighting
weighting
;
final
_
Weighting
weighting
;
final
List
<
Movement
>
_movements
;
final
List
<
_
Movement
>
_movements
;
int
_index
;
int
_index
;
void
addMovement
(
double
timeStamp
,
double
x
,
double
y
)
{
void
addMovement
(
double
timeStamp
,
double
x
,
double
y
)
{
if
(++
_index
==
kHistorySize
)
if
(++
_index
==
kHistorySize
)
_index
=
0
;
_index
=
0
;
Movement
movement
=
_getMovement
(
_index
);
_
Movement
movement
=
_getMovement
(
_index
);
movement
.
event
_t
ime
=
timeStamp
;
movement
.
event
T
ime
=
timeStamp
;
movement
.
position
=
new
ui
.
Point
(
x
,
y
);
movement
.
position
=
new
ui
.
Point
(
x
,
y
);
}
}
bool
getEstimator
(
Estimator
estimator
)
{
bool
getEstimator
(
_
Estimator
estimator
)
{
// Iterate over movement samples in reverse time order and collect samples.
// Iterate over movement samples in reverse time order and collect samples.
List
<
double
>
x
=
new
List
<
double
>();
List
<
double
>
x
=
new
List
<
double
>();
List
<
double
>
y
=
new
List
<
double
>();
List
<
double
>
y
=
new
List
<
double
>();
...
@@ -73,12 +77,12 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
...
@@ -73,12 +77,12 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
List
<
double
>
time
=
new
List
<
double
>();
List
<
double
>
time
=
new
List
<
double
>();
int
m
=
0
;
int
m
=
0
;
int
index
=
_index
;
int
index
=
_index
;
Movement
newest_m
ovement
=
_getMovement
(
index
);
_Movement
newestM
ovement
=
_getMovement
(
index
);
do
{
do
{
Movement
movement
=
_getMovement
(
index
);
_
Movement
movement
=
_getMovement
(
index
);
double
age
=
newest
_movement
.
event_time
-
movement
.
event_t
ime
;
double
age
=
newest
Movement
.
eventTime
-
movement
.
eventT
ime
;
if
(
age
>
kHorizonM
S
)
if
(
age
>
kHorizonM
illiseconds
)
break
;
break
;
ui
.
Point
position
=
movement
.
position
;
ui
.
Point
position
=
movement
.
position
;
...
@@ -104,9 +108,9 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
...
@@ -104,9 +108,9 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
LeastSquaresSolver
ySolver
=
new
LeastSquaresSolver
(
time
,
y
,
w
);
LeastSquaresSolver
ySolver
=
new
LeastSquaresSolver
(
time
,
y
,
w
);
PolynomialFit
yFit
=
ySolver
.
solve
(
n
);
PolynomialFit
yFit
=
ySolver
.
solve
(
n
);
if
(
yFit
!=
null
)
{
if
(
yFit
!=
null
)
{
estimator
.
x
coeff
=
xFit
.
coefficients
;
estimator
.
x
Coefficients
=
xFit
.
coefficients
;
estimator
.
y
coeff
=
yFit
.
coefficients
;
estimator
.
y
Coefficients
=
yFit
.
coefficients
;
estimator
.
time
=
newest
_movement
.
event_t
ime
;
estimator
.
time
=
newest
Movement
.
eventT
ime
;
estimator
.
degree
=
n
;
estimator
.
degree
=
n
;
estimator
.
confidence
=
xFit
.
confidence
*
yFit
.
confidence
;
estimator
.
confidence
=
xFit
.
confidence
*
yFit
.
confidence
;
return
true
;
return
true
;
...
@@ -116,9 +120,9 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
...
@@ -116,9 +120,9 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
// No velocity data available for this pointer, but we do have its current
// No velocity data available for this pointer, but we do have its current
// position.
// position.
estimator
.
x
coeff
=
[
x
[
0
]
];
estimator
.
x
Coefficients
=
[
x
[
0
]
];
estimator
.
y
coeff
=
[
y
[
0
]
];
estimator
.
y
Coefficients
=
[
y
[
0
]
];
estimator
.
time
=
newest
_movement
.
event_t
ime
;
estimator
.
time
=
newest
Movement
.
eventT
ime
;
estimator
.
degree
=
0
;
estimator
.
degree
=
0
;
estimator
.
confidence
=
1.0
;
estimator
.
confidence
=
1.0
;
return
true
;
return
true
;
...
@@ -130,7 +134,7 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
...
@@ -130,7 +134,7 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
double
_chooseWeight
(
int
index
)
{
double
_chooseWeight
(
int
index
)
{
switch
(
weighting
)
{
switch
(
weighting
)
{
case
Weighting
.
WEIGHTING_DELTA
:
case
_Weighting
.
weightingDelta
:
// Weight points based on how much time elapsed between them and the next
// Weight points based on how much time elapsed between them and the next
// point so that points that "cover" a shorter time span are weighed less.
// point so that points that "cover" a shorter time span are weighed less.
// delta 0ms: 0.5
// delta 0ms: 0.5
...
@@ -138,61 +142,61 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
...
@@ -138,61 +142,61 @@ class LeastSquaresVelocityTrackerStrategy extends VelocityTrackerStrategy {
if
(
index
==
_index
)
{
if
(
index
==
_index
)
{
return
1.0
;
return
1.0
;
}
}
int
next
_i
ndex
=
(
index
+
1
)
%
kHistorySize
;
int
next
I
ndex
=
(
index
+
1
)
%
kHistorySize
;
double
delta
_millis
=
_movements
[
next_index
].
event_t
ime
-
double
delta
Milliseconds
=
_movements
[
nextIndex
].
eventT
ime
-
_movements
[
index
].
event
_t
ime
;
_movements
[
index
].
event
T
ime
;
if
(
delta
_milli
s
<
0
)
if
(
delta
Millisecond
s
<
0
)
return
0.5
;
return
0.5
;
if
(
delta
_milli
s
<
10
)
if
(
delta
Millisecond
s
<
10
)
return
0.5
+
delta
_milli
s
*
0.05
;
return
0.5
+
delta
Millisecond
s
*
0.05
;
return
1.0
;
return
1.0
;
case
Weighting
.
WEIGHTING_CENTRAL
:
case
_Weighting
.
weightingCentral
:
// Weight points based on their age, weighing very recent and very old
// Weight points based on their age, weighing very recent and very old
// points less.
// points less.
// age 0ms: 0.5
// age 0ms: 0.5
// age 10ms: 1.0
// age 10ms: 1.0
// age 50ms: 1.0
// age 50ms: 1.0
// age 60ms: 0.5
// age 60ms: 0.5
double
age
_millis
=
_movements
[
_index
].
event_t
ime
-
double
age
Milliseconds
=
_movements
[
_index
].
eventT
ime
-
_movements
[
index
].
event
_t
ime
;
_movements
[
index
].
event
T
ime
;
if
(
age
_milli
s
<
0
)
if
(
age
Millisecond
s
<
0
)
return
0.5
;
return
0.5
;
if
(
age
_milli
s
<
10
)
if
(
age
Millisecond
s
<
10
)
return
0.5
+
age
_milli
s
*
0.05
;
return
0.5
+
age
Millisecond
s
*
0.05
;
if
(
age
_milli
s
<
50
)
if
(
age
Millisecond
s
<
50
)
return
1.0
;
return
1.0
;
if
(
age
_milli
s
<
60
)
if
(
age
Millisecond
s
<
60
)
return
0.5
+
(
60
-
age
_milli
s
)
*
0.05
;
return
0.5
+
(
60
-
age
Millisecond
s
)
*
0.05
;
return
0.5
;
return
0.5
;
case
Weighting
.
WEIGHTING_RECENT
:
case
_Weighting
.
weightingRecent
:
// Weight points based on their age, weighing older points less.
// Weight points based on their age, weighing older points less.
// age 0ms: 1.0
// age 0ms: 1.0
// age 50ms: 1.0
// age 50ms: 1.0
// age 100ms: 0.5
// age 100ms: 0.5
double
age
_millis
=
_movements
[
_index
].
event_t
ime
-
double
age
Milliseconds
=
_movements
[
_index
].
eventT
ime
-
_movements
[
index
].
event
_t
ime
;
_movements
[
index
].
event
T
ime
;
if
(
age
_milli
s
<
50
)
{
if
(
age
Millisecond
s
<
50
)
{
return
1.0
;
return
1.0
;
}
}
if
(
age
_milli
s
<
100
)
{
if
(
age
Millisecond
s
<
100
)
{
return
0.5
+
(
100
-
age
_milli
s
)
*
0.01
;
return
0.5
+
(
100
-
age
Millisecond
s
)
*
0.01
;
}
}
return
0.5
;
return
0.5
;
case
Weighting
.
WEIGHTING_NONE
:
case
_Weighting
.
weightingNone
:
default
:
default
:
return
1.0
;
return
1.0
;
}
}
}
}
Movement
_getMovement
(
int
i
)
{
_
Movement
_getMovement
(
int
i
)
{
Movement
result
=
_movements
[
i
];
_
Movement
result
=
_movements
[
i
];
if
(
result
==
null
)
{
if
(
result
==
null
)
{
result
=
new
Movement
();
result
=
new
_
Movement
();
_movements
[
i
]
=
result
;
_movements
[
i
]
=
result
;
}
}
return
result
;
return
result
;
...
@@ -206,7 +210,7 @@ class VelocityTracker {
...
@@ -206,7 +210,7 @@ class VelocityTracker {
VelocityTracker
()
:
_lastTimeStamp
=
0.0
,
_strategy
=
_createStrategy
();
VelocityTracker
()
:
_lastTimeStamp
=
0.0
,
_strategy
=
_createStrategy
();
double
_lastTimeStamp
;
double
_lastTimeStamp
;
VelocityTrackerStrategy
_strategy
;
_
VelocityTrackerStrategy
_strategy
;
void
addPosition
(
double
timeStamp
,
double
x
,
double
y
)
{
void
addPosition
(
double
timeStamp
,
double
x
,
double
y
)
{
if
((
timeStamp
-
_lastTimeStamp
)
>=
kAssumePointerMoveStoppedTimeMs
)
if
((
timeStamp
-
_lastTimeStamp
)
>=
kAssumePointerMoveStoppedTimeMs
)
...
@@ -216,19 +220,19 @@ class VelocityTracker {
...
@@ -216,19 +220,19 @@ class VelocityTracker {
}
}
GestureVelocity
getVelocity
()
{
GestureVelocity
getVelocity
()
{
Estimator
estimator
=
new
Estimator
();
_Estimator
estimator
=
new
_
Estimator
();
if
(
_strategy
.
getEstimator
(
estimator
)
&&
estimator
.
degree
>=
1
)
{
if
(
_strategy
.
getEstimator
(
estimator
)
&&
estimator
.
degree
>=
1
)
{
// convert from pixels/ms to pixels/s
// convert from pixels/ms to pixels/s
return
new
GestureVelocity
(
return
new
GestureVelocity
(
isValid:
true
,
isValid:
true
,
x:
estimator
.
x
coeff
[
1
]*
1000
,
x:
estimator
.
x
Coefficients
[
1
]
*
1000
,
y:
estimator
.
y
coeff
[
1
]*
1000
y:
estimator
.
y
Coefficients
[
1
]
*
1000
);
);
}
}
return
new
GestureVelocity
(
isValid:
false
,
x:
0.0
,
y:
0.0
);
return
new
GestureVelocity
(
isValid:
false
,
x:
0.0
,
y:
0.0
);
}
}
static
VelocityTrackerStrategy
_createStrategy
()
{
static
_
VelocityTrackerStrategy
_createStrategy
()
{
return
new
LeastSquaresVelocityTrackerStrategy
(
2
,
Weighting
.
WEIGHTING_NONE
);
return
new
_LeastSquaresVelocityTrackerStrategy
(
2
,
_Weighting
.
weightingNone
);
}
}
}
}
packages/flutter/lib/src/widgets/event_recorder.dart
View file @
5e9b2fba
...
@@ -13,8 +13,14 @@ enum EventRecorderMode {
...
@@ -13,8 +13,14 @@ enum EventRecorderMode {
record
record
}
}
typedef
void
EventsReady
(
List
<
PointerInputEvent
>
events
);
typedef
void
EventsReady
Callback
(
List
<
PointerInputEvent
>
events
);
/// EventRecorder is a utility widget that allows input events occurring
/// on the child to be recorded. The widget is initially in the "stop" state
/// by default. When in the "record" state, all pointer input events
/// occurring on the child are recorded into a buffer. When the "stop" state
/// is entered again, the onEventsReady callback is invoked with a list of
/// the recorded events.
class
EventRecorder
extends
StatefulComponent
{
class
EventRecorder
extends
StatefulComponent
{
EventRecorder
({
EventRecorder
({
Key
key
,
Key
key
,
...
@@ -25,32 +31,29 @@ class EventRecorder extends StatefulComponent {
...
@@ -25,32 +31,29 @@ class EventRecorder extends StatefulComponent {
final
Widget
child
;
final
Widget
child
;
final
EventRecorderMode
mode
;
final
EventRecorderMode
mode
;
final
EventsReady
onEventsReady
;
final
EventsReady
Callback
onEventsReady
;
_EventRecorderState
createState
()
=>
new
_EventRecorderState
();
_EventRecorderState
createState
()
=>
new
_EventRecorderState
();
}
}
class
_EventRecorderState
extends
State
<
EventRecorder
>
{
class
_EventRecorderState
extends
State
<
EventRecorder
>
{
EventRecorderMode
_mode
;
List
<
PointerInputEvent
>
_events
=
new
List
<
PointerInputEvent
>();
List
<
PointerInputEvent
>
_events
=
new
List
<
PointerInputEvent
>();
void
initState
()
{
void
initState
()
{
super
.
initState
();
super
.
initState
();
_mode
=
config
.
mode
;
}
}
void
didUpdateConfig
(
EventRecorder
oldConfig
)
{
void
didUpdateConfig
(
EventRecorder
oldConfig
)
{
if
(
_
mode
==
EventRecorderMode
.
record
&&
if
(
oldConfig
.
mode
==
EventRecorderMode
.
record
&&
config
.
mode
==
EventRecorderMode
.
stop
)
{
config
.
mode
==
EventRecorderMode
.
stop
)
{
config
.
onEventsReady
(
_events
);
config
.
onEventsReady
(
_events
);
_events
.
clear
();
_events
.
clear
();
}
}
_mode
=
config
.
mode
;
}
}
void
_recordEvent
(
PointerInputEvent
event
)
{
void
_recordEvent
(
PointerInputEvent
event
)
{
if
(
_
mode
==
EventRecorderMode
.
record
)
{
if
(
config
.
mode
==
EventRecorderMode
.
record
)
{
_events
.
add
(
event
);
_events
.
add
(
event
);
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment