update method

void update(
  1. double dt
)

Implementation

void update(double dt) {
  if (_dragPosition == null || _backgroundRect == null || _knobRect == null) {
    return;
  }

  if (_dragging) {
    double radAngle = atan2(
      _dragPosition!.dy - _backgroundRect!.center.dy,
      _dragPosition!.dx - _backgroundRect!.center.dx,
    );

    double degrees = radAngle * 180 / pi;

    // Distance between the center of joystick background & drag position
    Vector2 centerPosition = _backgroundRect!.center.toVector2();
    Vector2 dragPosition = _dragPosition!.toVector2();
    double 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
    double nextX = dist * cos(radAngle);
    double nextY = dist * sin(radAngle);
    Offset nextPoint = Offset(nextX, nextY);

    Offset diff = Offset(
          _backgroundRect!.center.dx + nextPoint.dx,
          _backgroundRect!.center.dy + nextPoint.dy,
        ) -
        _knobRect!.center;
    _knobRect = _knobRect!.shift(diff);

    double intensity = dist / _tileSize;

    if (intensity == 0) {
      _controller.onJoystickChangeDirectional(JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.IDLE,
        intensity: intensity,
        radAngle: radAngle,
      ));
      return;
    }

    if (degrees > -22.5 && degrees <= 22.5) {
      _controller.onJoystickChangeDirectional(JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.MOVE_RIGHT,
        intensity: intensity,
        radAngle: radAngle,
      ));
    }

    if (enableDiagonalInput && degrees > 22.5 && degrees <= 67.5) {
      _controller.onJoystickChangeDirectional(JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.MOVE_DOWN_RIGHT,
        intensity: intensity,
        radAngle: radAngle,
      ));
    }

    if (degrees > 67.5 && degrees <= 112.5) {
      _controller.onJoystickChangeDirectional(JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.MOVE_DOWN,
        intensity: intensity,
        radAngle: radAngle,
      ));
    }

    if (enableDiagonalInput && degrees > 112.5 && degrees <= 157.5) {
      _controller.onJoystickChangeDirectional(JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.MOVE_DOWN_LEFT,
        intensity: intensity,
        radAngle: radAngle,
      ));
    }

    if ((degrees > 157.5 && degrees <= 180) ||
        (degrees >= -180 && degrees <= -157.5)) {
      _controller.onJoystickChangeDirectional(JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.MOVE_LEFT,
        intensity: intensity,
        radAngle: radAngle,
      ));
    }

    if (enableDiagonalInput && degrees > -157.5 && degrees <= -112.5) {
      _controller.onJoystickChangeDirectional(JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.MOVE_UP_LEFT,
        intensity: intensity,
        radAngle: radAngle,
      ));
    }

    if (degrees > -112.5 && degrees <= -67.5) {
      _controller.onJoystickChangeDirectional(JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.MOVE_UP,
        intensity: intensity,
        radAngle: radAngle,
      ));
    }

    if (enableDiagonalInput && degrees > -67.5 && degrees <= -22.5) {
      _controller.onJoystickChangeDirectional(JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.MOVE_UP_RIGHT,
        intensity: intensity,
        radAngle: radAngle,
      ));
    }
  } else {
    if (_knobRect != null) {
      Offset diff = _dragPosition! - _knobRect!.center;
      _knobRect = _knobRect!.shift(diff);
    }
  }
}