From 57fef366d1f7795213c5a3dd87864fdcc1946ddf Mon Sep 17 00:00:00 2001 From: Hixie Date: Wed, 3 Jun 2015 14:21:46 -0700 Subject: [PATCH] Provide Point+Size, Point-Point, Point.toSize(), and Rect.toPoint(). Also some minor code cleanup in affected files and nearby files. R=mpcomplete@chromium.org Review URL: https://codereview.chromium.org/1160453006 --- engine/core/painting/Color.dart | 1 + engine/core/painting/Point.dart | 11 +++++++---- engine/core/painting/Rect.dart | 14 +++++++++----- engine/core/painting/Size.dart | 10 ++++++---- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/engine/core/painting/Color.dart b/engine/core/painting/Color.dart index 5789e40e6..0ea9fa980 100644 --- a/engine/core/painting/Color.dart +++ b/engine/core/painting/Color.dart @@ -16,6 +16,7 @@ class Color { ((b & 0xff) << 0)); bool operator ==(other) => other is Color && _value == other._value; + int get hashCode => _value.hashCode; String toString() => "Color(0x${_value.toRadixString(16).padLeft(8, '0')})"; } diff --git a/engine/core/painting/Point.dart b/engine/core/painting/Point.dart index 62cd87d7b..418660537 100644 --- a/engine/core/painting/Point.dart +++ b/engine/core/painting/Point.dart @@ -11,10 +11,13 @@ class Point { const Point(this.x, this.y); - bool operator ==(other) { - if (!(other is Point)) return false; - return x == other.x && y == other.y; - } + bool operator ==(other) => other is Point && x == other.x && y == other.y; + Size operator -(Point other) => new Size(x - other.x, y - other.y); + Point operator +(Size size) => new Point(x + size.width, y + size.height); + + // does the equivalent of "return this - Point(0,0)" + Size toSize() => new Size(x, y); + int get hashCode { int result = 373; result = 37 * result + x.hashCode; diff --git a/engine/core/painting/Rect.dart b/engine/core/painting/Rect.dart index b0b7f7c40..eb82fd44d 100644 --- a/engine/core/painting/Rect.dart +++ b/engine/core/painting/Rect.dart @@ -44,18 +44,22 @@ class Rect { // Rects are inclusive of the top and left edges but exclusive of the bottom // right edges. - bool contains(Point point) => - point.x >= left && point.x < right && point.y >= top && point.y < bottom; + bool contains(Point point) { + return point.x >= left && point.x < right && point.y >= top && point.y < bottom; + } bool operator ==(other) { - if (!(other is Rect)) return false; + if (other is! Rect) + return false; for (var i = 0; i < 4; ++i) { - if (_value[i] != other._value[i]) return false; + if (_value[i] != other._value[i]) + return false; } return true; } + int get hashCode { return _value.fold(373, (value, item) => (37 * value + item.hashCode)); } - String toString() => "Rect.LTRB($left, $top, $right, $bottom)"; + String toString() => "Rect.fromLTRB($left, $top, $right, $bottom)"; } diff --git a/engine/core/painting/Size.dart b/engine/core/painting/Size.dart index 4b364bb00..0339cf20a 100644 --- a/engine/core/painting/Size.dart +++ b/engine/core/painting/Size.dart @@ -5,6 +5,7 @@ part of dart.sky; /// Holds a 2D floating-point size. +/// Think of this as a vector from Point(0,0) to Point(size.width, size.height) class Size { final double width; final double height; @@ -16,10 +17,11 @@ class Size { const Size.fromWidth(this.width) : height = double.INFINITY; const Size.fromHeight(this.height) : width = double.INFINITY; - bool operator ==(other) { - if (!(other is Size)) return false; - return width == other.width && height == other.height; - } + bool operator ==(other) => other is Size && width == other.width && height == other.height; + + // does the equivalent of "return new Point(0,0) + this" + Point toPoint() => new Point(this.width, this.height); + int get hashCode { int result = 373; result = 37 * result + width.hashCode; -- GitLab