update method
Implementation
void update(double dt) {
if (_dragPosition == null ||
_rectBackgroundDirection == null ||
_rect == null) {
return;
}
if (_dragging) {
final radAngle = atan2(
_dragPosition!.dy - _rectBackgroundDirection!.center.dy,
_dragPosition!.dx - _rectBackgroundDirection!.center.dx,
);
// Distance between the center of joystick background & drag position
final centerPosition = _rectBackgroundDirection!.center.toVector2();
final dragPosition = _dragPosition!.toVector2();
var dist = centerPosition.distanceTo(dragPosition);
// The maximum distance for the knob position the edge of
// the background + half of its own size. The knob can wander in the
// background image, but not outside.
dist = min(dist, _tileSize);
// Calculation the knob position
final nextX = dist * cos(radAngle);
final nextY = dist * sin(radAngle);
final nextPoint = Offset(nextX, nextY);
final diff = Offset(
_rectBackgroundDirection!.center.dx + nextPoint.dx,
_rectBackgroundDirection!.center.dy + nextPoint.dy,
) -
_rect!.center;
_rect = _rect!.shift(diff);
final intensity = dist / _tileSize;
_controller.onJoystickAction(
JoystickActionEvent(
id: actionId,
event: ActionEvent.MOVE,
intensity: intensity,
radAngle: radAngle,
),
);
} else {
final diff = _dragPosition! - _rect!.center;
_rect = _rect!.shift(diff);
}
}