Introduce separate HitTestResults for Box and Sliver (#31894)

This commit is contained in:
Michael Goderbauer
2019-05-09 10:00:01 +02:00
committed by GitHub
parent ff1dbcdeb6
commit 1621baafc7
39 changed files with 1135 additions and 175 deletions

View File

@@ -6,6 +6,7 @@ import 'dart:math' as math;
import 'package:flutter/rendering.dart';
import 'package:flutter/gestures.dart';
import 'package:meta/meta.dart';
const double kTwoPi = 2 * math.pi;
@@ -76,6 +77,18 @@ class SectorParentData extends ParentData {
double theta = 0.0;
}
/// Base class for [RenderObject]s that live in a polar coordinate space.
///
/// In a polar coordinate system each point on a plane is determined by a
/// distance from a reference point ("radius") and an angle from a reference
/// direction ("theta").
///
/// See also:
///
/// * <https://en.wikipedia.org/wiki/Polar_coordinate_system>, which defines
/// the polar coordinate space.
/// * [RenderBox], which is the base class for [RenderObject]s that live in a
/// cartesian coordinate space.
abstract class RenderSector extends RenderObject {
@override
@@ -130,15 +143,15 @@ abstract class RenderSector extends RenderObject {
@override
Rect get semanticBounds => Rect.fromLTWH(-deltaRadius, -deltaRadius, 2.0 * deltaRadius, 2.0 * deltaRadius);
bool hitTest(HitTestResult result, { double radius, double theta }) {
bool hitTest(SectorHitTestResult result, { double radius, double theta }) {
if (radius < parentData.radius || radius >= parentData.radius + deltaRadius ||
theta < parentData.theta || theta >= parentData.theta + deltaTheta)
return false;
hitTestChildren(result, radius: radius, theta: theta);
result.add(HitTestEntry(this));
result.add(SectorHitTestEntry(this, radius: radius, theta: theta));
return true;
}
void hitTestChildren(HitTestResult result, { double radius, double theta }) { }
void hitTestChildren(SectorHitTestResult result, { double radius, double theta }) { }
double deltaRadius;
double deltaTheta;
@@ -190,7 +203,7 @@ class RenderSectorWithChildren extends RenderDecoratedSector with ContainerRende
RenderSectorWithChildren(BoxDecoration decoration) : super(decoration);
@override
void hitTestChildren(HitTestResult result, { double radius, double theta }) {
void hitTestChildren(SectorHitTestResult result, { double radius, double theta }) {
RenderSector child = lastChild;
while (child != null) {
if (child.hitTest(result, radius: radius, theta: theta))
@@ -530,7 +543,7 @@ class RenderBoxToRenderSectorAdapter extends RenderBox with RenderObjectWithChil
}
@override
bool hitTest(HitTestResult result, { Offset position }) {
bool hitTest(BoxHitTestResult result, { Offset position }) {
if (child == null)
return false;
double x = position.dx;
@@ -547,7 +560,7 @@ class RenderBoxToRenderSectorAdapter extends RenderBox with RenderObjectWithChil
return false;
if (theta > child.deltaTheta)
return false;
child.hitTest(result, radius: radius, theta: theta);
child.hitTest(SectorHitTestResult.wrap(result), radius: radius, theta: theta);
result.add(BoxHitTestEntry(this, position));
return true;
}
@@ -585,3 +598,52 @@ class RenderSolidColor extends RenderDecoratedSector {
}
}
}
/// The result of performing a hit test on [RenderSector]s.
class SectorHitTestResult extends HitTestResult {
/// Creates an empty hit test result for hit testing on [RenderSector].
SectorHitTestResult() : super();
/// Wraps `result` to create a [HitTestResult] that implements the
/// [SectorHitTestResult] protocol for hit testing on [RenderSector]s.
///
/// This method is used by [RenderObject]s that adapt between the
/// [RenderSector]-world and the non-[RenderSector]-world to convert a (subtype of)
/// [HitTestResult] to a [SectorHitTestResult] for hit testing on [RenderSector]s.
///
/// The [HitTestEntry]s added to the returned [SectorHitTestResult] are also
/// added to the wrapped `result` (both share the same underlying data
/// structure to store [HitTestEntry]s).
///
/// See also:
///
/// * [HitTestResult.wrap], which turns a [SectorHitTestResult] back into a
/// generic [HitTestResult].
SectorHitTestResult.wrap(HitTestResult result) : super.wrap(result);
// TODO(goderbauer): Add convenience methods to transform hit test positions
// once we have RenderSector implementations that move the origin of their
// children (e.g. RenderSectorTransform analogues to RenderTransform).
}
/// A hit test entry used by [RenderSector].
class SectorHitTestEntry extends HitTestEntry {
/// Creates a box hit test entry.
///
/// The [radius] and [theta] argument must not be null.
SectorHitTestEntry(RenderSector target, { @required this.radius, @required this.theta })
: assert(radius != null),
assert(theta != null),
super(target);
@override
RenderSector get target => super.target;
/// The radius component of the hit test position in the local coordinates of
/// [target].
final double radius;
/// The theta component of the hit test position in the local coordinates of
/// [target].
final double theta;
}