scale.dart 3.57 KB
Newer Older
1 2 3 4
// 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.

5 6 7
import 'arena.dart';
import 'recognizer.dart';
import 'constants.dart';
8
import 'events.dart';
9

10
enum ScaleState {
11 12
  ready,
  possible,
13 14
  accepted,
  started
15 16
}

Ian Hickson's avatar
Ian Hickson committed
17 18
typedef void GestureScaleStartCallback(Point focalPoint);
typedef void GestureScaleUpdateCallback(double scale, Point focalPoint);
19
typedef void GestureScaleEndCallback();
20

21
class ScaleGestureRecognizer extends OneSequenceGestureRecognizer {
22 23 24
  GestureScaleStartCallback onStart;
  GestureScaleUpdateCallback onUpdate;
  GestureScaleEndCallback onEnd;
25

26
  ScaleState _state = ScaleState.ready;
27 28 29

  double _initialSpan;
  double _currentSpan;
Ian Hickson's avatar
Ian Hickson committed
30
  Map<int, Point> _pointerLocations;
31

32
  double get _scaleFactor => _initialSpan > 0.0 ? _currentSpan / _initialSpan : 1.0;
33

Ian Hickson's avatar
Ian Hickson committed
34
  void addPointer(PointerEvent event) {
35
    startTrackingPointer(event.pointer);
36 37
    if (_state == ScaleState.ready) {
      _state = ScaleState.possible;
38 39
      _initialSpan = 0.0;
      _currentSpan = 0.0;
Ian Hickson's avatar
Ian Hickson committed
40
      _pointerLocations = new Map<int, Point>();
41 42 43
    }
  }

Ian Hickson's avatar
Ian Hickson committed
44
  void handleEvent(PointerEvent event) {
45
    assert(_state != ScaleState.ready);
46
    bool configChanged = false;
Ian Hickson's avatar
Ian Hickson committed
47 48 49 50 51 52 53 54
    if (event is PointerMoveEvent) {
      _pointerLocations[event.pointer] = event.position;
    } else if (event is PointerDownEvent) {
      configChanged = true;
      _pointerLocations[event.pointer] = event.position;
    } else if (event is PointerUpEvent) {
      configChanged = true;
      _pointerLocations.remove(event.pointer);
55 56
    }

57 58 59 60 61 62
    _update(configChanged);

    stopTrackingIfPointerNoLongerDown(event);
  }

  void _update(bool configChanged) {
63 64 65
    int count = _pointerLocations.keys.length;

    // Compute the focal point
Ian Hickson's avatar
Ian Hickson committed
66
    Point focalPoint = Point.origin;
67 68
    for (int pointer in _pointerLocations.keys)
      focalPoint += _pointerLocations[pointer].toOffset();
Ian Hickson's avatar
Ian Hickson committed
69
    focalPoint = new Point(focalPoint.x / count, focalPoint.y / count);
70 71 72 73 74 75 76 77 78

    // Span is the average deviation from focal point
    double totalDeviation = 0.0;
    for (int pointer in _pointerLocations.keys)
      totalDeviation += (focalPoint - _pointerLocations[pointer]).distance;
    _currentSpan = count > 0 ? totalDeviation / count : 0.0;

    if (configChanged) {
      _initialSpan = _currentSpan;
79
      if (_state == ScaleState.started) {
80 81
        if (onEnd != null)
          onEnd();
82
        _state = ScaleState.accepted;
83 84 85
      }
    }

86 87
    if (_state == ScaleState.ready)
      _state = ScaleState.possible;
88

89 90
    if (_state == ScaleState.possible &&
        (_currentSpan - _initialSpan).abs() > kScaleSlop) {
91 92 93
      resolve(GestureDisposition.accepted);
    }

94 95
    if (_state == ScaleState.accepted && !configChanged) {
      _state = ScaleState.started;
96
      if (onStart != null)
97
        onStart(focalPoint);
98 99
    }

100 101
    if (_state == ScaleState.started && onUpdate != null)
      onUpdate(_scaleFactor, focalPoint);
102 103 104
  }

  void acceptGesture(int pointer) {
105 106 107
    if (_state != ScaleState.accepted) {
      _state = ScaleState.accepted;
      _update(false);
108 109 110 111 112
    }
  }

  void didStopTrackingLastPointer(int pointer) {
    switch(_state) {
113
      case ScaleState.possible:
114 115
        resolve(GestureDisposition.rejected);
        break;
116 117
      case ScaleState.ready:
        assert(false);  // We should have not seen a pointer yet
118
        break;
119
      case ScaleState.accepted:
120
        break;
121 122
      case ScaleState.started:
        assert(false);  // We should be in the accepted state when user is done
123 124
        break;
    }
125
    _state = ScaleState.ready;
126
  }
127 128

  String toStringShort() => 'scale';
129
}