summaryrefslogtreecommitdiff
path: root/lib/widgets/editor/transform/controller.dart
blob: 57328ca0ec6f3d278baf32b156c198dafab897fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import 'dart:async';
import 'dart:ui';

import 'package:aves/widgets/editor/transform/crop_region.dart';
import 'package:aves/widgets/editor/transform/transformation.dart';
import 'package:aves_model/aves_model.dart';
import 'package:flutter/foundation.dart';
import 'package:leak_tracker/leak_tracker.dart';

class TransformController {
  ValueNotifier<CropAspectRatio> aspectRatioNotifier = ValueNotifier(CropAspectRatio.free);

  TransformActivity _activity = TransformActivity.none;

  TransformActivity get activity => _activity;

  Transformation _transformation = Transformation.zero;

  Transformation get transformation => _transformation;

  bool get modified => _transformation != Transformation.zero;

  final StreamController<Transformation> _transformationStreamController = StreamController.broadcast();

  Stream<Transformation> get transformationStream => _transformationStreamController.stream;

  final StreamController<TransformActivity> _activityStreamController = StreamController.broadcast();

  Stream<TransformActivity> get activityStream => _activityStreamController.stream;

  static const double straightenDegreesMin = -45;
  static const double straightenDegreesMax = 45;

  final Size displaySize;

  TransformController(this.displaySize) {
    if (kFlutterMemoryAllocationsEnabled) {
      LeakTracking.dispatchObjectCreated(
        library: 'aves',
        className: '$TransformController',
        object: this,
      );
    }
    reset();
    aspectRatioNotifier.addListener(_onAspectRatioChanged);
  }

  void dispose() {
    if (kFlutterMemoryAllocationsEnabled) {
      LeakTracking.dispatchObjectDisposed(object: this);
    }
    aspectRatioNotifier.dispose();
  }

  void reset() {
    _transformation = Transformation.zero.copyWith(
      region: CropRegion.fromRect(Offset.zero & displaySize),
    );
    _transformationStreamController.add(_transformation);
    setAspectRatio(CropAspectRatio.free);
  }

  void flipHorizontally() {
    _transformation = _transformation.copyWith(
      orientation: _transformation.orientation.flipHorizontally(),
      straightenDegrees: -transformation.straightenDegrees,
    );
    _transformationStreamController.add(_transformation);
  }

  void rotateClockwise() {
    _transformation = _transformation.copyWith(
      orientation: _transformation.orientation.rotateClockwise(),
    );
    _transformationStreamController.add(_transformation);
  }

  set straightenDegrees(double straightenDegrees) {
    _transformation = _transformation.copyWith(
      straightenDegrees: straightenDegrees.clamp(straightenDegreesMin, straightenDegreesMax),
    );
    _transformationStreamController.add(_transformation);
  }

  set cropRegion(CropRegion region) {
    _transformation = _transformation.copyWith(
      region: region,
    );
    _transformationStreamController.add(_transformation);
  }

  set activity(TransformActivity activity) {
    _activity = activity;
    _activityStreamController.add(_activity);
  }

  void setAspectRatio(CropAspectRatio ratio) => aspectRatioNotifier.value = ratio;

  void _onAspectRatioChanged() {
    // TODO TLAD [crop] apply
  }
}