rotated_box.dart 4.25 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9
// 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 'package:vector_math/vector_math_64.dart';

import 'box.dart';
10
import 'layer.dart';
11 12
import 'object.dart';

13
const double _kQuarterTurnsInRadians = math.pi / 2.0;
14 15 16 17 18 19 20

/// Rotates its child by a integral number of quarter turns.
///
/// Unlike [RenderTransform], which applies a transform just prior to painting,
/// this object applies its rotation prior to layout, which means the entire
/// rotated box consumes only as much space as required by the rotated child.
class RenderRotatedBox extends RenderBox with RenderObjectWithChildMixin<RenderBox> {
21
  /// Creates a rotated render box.
22
  RenderRotatedBox({
23 24
    required int quarterTurns,
    RenderBox? child,
25
  }) : _quarterTurns = quarterTurns {
26 27 28 29 30 31
    this.child = child;
  }

  /// The number of clockwise quarter turns the child should be rotated.
  int get quarterTurns => _quarterTurns;
  int _quarterTurns;
32
  set quarterTurns(int value) {
33
    if (_quarterTurns == value) {
34
      return;
35
    }
36 37 38 39
    _quarterTurns = value;
    markNeedsLayout();
  }

40
  bool get _isVertical => quarterTurns.isOdd;
41

42
  @override
43
  double computeMinIntrinsicWidth(double height) {
44
    if (child == null) {
45
      return 0.0;
46
    }
47
    return _isVertical ? child!.getMinIntrinsicHeight(height) : child!.getMinIntrinsicWidth(height);
48 49
  }

50
  @override
51
  double computeMaxIntrinsicWidth(double height) {
52
    if (child == null) {
53
      return 0.0;
54
    }
55
    return _isVertical ? child!.getMaxIntrinsicHeight(height) : child!.getMaxIntrinsicWidth(height);
56 57
  }

58
  @override
59
  double computeMinIntrinsicHeight(double width) {
60
    if (child == null) {
61
      return 0.0;
62
    }
63
    return _isVertical ? child!.getMinIntrinsicWidth(width) : child!.getMinIntrinsicHeight(width);
64 65
  }

66
  @override
67
  double computeMaxIntrinsicHeight(double width) {
68
    if (child == null) {
69
      return 0.0;
70
    }
71
    return _isVertical ? child!.getMaxIntrinsicWidth(width) : child!.getMaxIntrinsicHeight(width);
72 73
  }

74
  Matrix4? _paintTransform;
75

76 77 78 79 80 81 82 83 84
  @override
  Size computeDryLayout(BoxConstraints constraints) {
    if (child == null) {
      return constraints.smallest;
    }
    final Size childSize = child!.getDryLayout(_isVertical ? constraints.flipped : constraints);
    return _isVertical ? Size(childSize.height, childSize.width) : childSize;
  }

85
  @override
86 87 88
  void performLayout() {
    _paintTransform = null;
    if (child != null) {
89 90
      child!.layout(_isVertical ? constraints.flipped : constraints, parentUsesSize: true);
      size = _isVertical ? Size(child!.size.height, child!.size.width) : child!.size;
91
      _paintTransform = Matrix4.identity()
92 93
        ..translate(size.width / 2.0, size.height / 2.0)
        ..rotateZ(_kQuarterTurnsInRadians * (quarterTurns % 4))
94
        ..translate(-child!.size.width / 2.0, -child!.size.height / 2.0);
95
    } else {
96
      size = constraints.smallest;
97 98 99
    }
  }

100
  @override
101
  bool hitTestChildren(BoxHitTestResult result, { required Offset position }) {
102
    assert(_paintTransform != null || debugNeedsLayout || child == null);
103
    if (child == null || _paintTransform == null) {
104
      return false;
105
    }
106 107 108
    return result.addWithPaintTransform(
      transform: _paintTransform,
      position: position,
109 110
      hitTest: (BoxHitTestResult result, Offset position) {
        return child!.hitTest(result, position: position);
111 112
      },
    );
113 114 115
  }

  void _paintChild(PaintingContext context, Offset offset) {
116
    context.paintChild(child!, offset);
117 118
  }

119
  @override
120
  void paint(PaintingContext context, Offset offset) {
121
    if (child != null) {
122
      _transformLayer.layer = context.pushTransform(
123 124 125 126
        needsCompositing,
        offset,
        _paintTransform!,
        _paintChild,
127
        oldLayer: _transformLayer.layer,
128
      );
129
    } else {
130
      _transformLayer.layer = null;
131
    }
132 133
  }

134 135 136 137 138 139 140
  final LayerHandle<TransformLayer> _transformLayer = LayerHandle<TransformLayer>();

  @override
  void dispose() {
    _transformLayer.layer = null;
    super.dispose();
  }
141

142
  @override
143
  void applyPaintTransform(RenderBox child, Matrix4 transform) {
144
    if (_paintTransform != null) {
145
      transform.multiply(_paintTransform!);
146
    }
147 148 149
    super.applyPaintTransform(child, transform);
  }
}