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

7
import 'package:flutter/foundation.dart';
8
import 'package:flutter/gestures.dart';
9
import 'package:flutter/painting.dart';
10 11 12 13 14
import 'package:vector_math/vector_math_64.dart';

import 'box.dart';
import 'object.dart';

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

/// 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> {
23 24 25
  /// Creates a rotated render box.
  ///
  /// The [quarterTurns] argument must not be null.
26
  RenderRotatedBox({
27
    @required int quarterTurns,
28
    RenderBox child,
29 30
  }) : assert(quarterTurns != null),
       _quarterTurns = quarterTurns {
31 32 33 34 35 36
    this.child = child;
  }

  /// The number of clockwise quarter turns the child should be rotated.
  int get quarterTurns => _quarterTurns;
  int _quarterTurns;
37
  set quarterTurns(int value) {
38 39 40 41 42 43 44 45 46
    assert(value != null);
    if (_quarterTurns == value)
      return;
    _quarterTurns = value;
    markNeedsLayout();
  }

  bool get _isVertical => quarterTurns % 2 == 1;

47
  @override
48
  double computeMinIntrinsicWidth(double height) {
49 50 51
    if (child == null)
      return 0.0;
    return _isVertical ? child.getMinIntrinsicHeight(height) : child.getMinIntrinsicWidth(height);
52 53
  }

54
  @override
55
  double computeMaxIntrinsicWidth(double height) {
56 57 58
    if (child == null)
      return 0.0;
    return _isVertical ? child.getMaxIntrinsicHeight(height) : child.getMaxIntrinsicWidth(height);
59 60
  }

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

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

  Matrix4 _paintTransform;

77
  @override
78 79 80 81
  void performLayout() {
    _paintTransform = null;
    if (child != null) {
      child.layout(_isVertical ? constraints.flipped : constraints, parentUsesSize: true);
82 83
      size = _isVertical ? Size(child.size.height, child.size.width) : child.size;
      _paintTransform = Matrix4.identity()
84 85 86 87 88 89 90 91
        ..translate(size.width / 2.0, size.height / 2.0)
        ..rotateZ(_kQuarterTurnsInRadians * (quarterTurns % 4))
        ..translate(-child.size.width / 2.0, -child.size.height / 2.0);
    } else {
      performResize();
    }
  }

92
  @override
93
  bool hitTestChildren(BoxHitTestResult result, { Offset position }) {
94
    assert(_paintTransform != null || debugNeedsLayout || child == null);
95 96
    if (child == null || _paintTransform == null)
      return false;
97 98 99 100 101 102 103
    return result.addWithPaintTransform(
      transform: _paintTransform,
      position: position,
      hitTest: (BoxHitTestResult result, Offset position) {
        return child.hitTest(result, position: position);
      },
    );
104 105 106 107 108 109
  }

  void _paintChild(PaintingContext context, Offset offset) {
    context.paintChild(child, offset);
  }

110
  @override
111 112 113 114 115
  void paint(PaintingContext context, Offset offset) {
    if (child != null)
      context.pushTransform(needsCompositing, offset, _paintTransform, _paintChild);
  }

116
  @override
117 118 119 120 121 122
  void applyPaintTransform(RenderBox child, Matrix4 transform) {
    if (_paintTransform != null)
      transform.multiply(_paintTransform);
    super.applyPaintTransform(child, transform);
  }
}