OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 part of dart.sky; | 5 part of dart.sky; |
6 | 6 |
7 /// Holds a 2D floating-point size. | 7 /// Holds a 2D floating-point size. |
8 /// Think of this as a vector from Point(0,0) to Point(size.width, size.height) | 8 /// Think of this as a vector from Point(0,0) to Point(size.width, size.height) |
9 class Size { | 9 class Size extends OffsetBase { |
10 const Size(this.width, this.height); | 10 const Size(double width, double height) : super(width, height); |
11 Size.copy(Size source) : width = source.width, height = source.height; | 11 Size.copy(Size source) : super(source.width, source.height); |
12 const Size.fromWidth(this.width) : height = double.INFINITY; | 12 const Size.fromWidth(double width) : super(width, double.INFINITY); |
13 const Size.fromHeight(this.height) : width = double.INFINITY; | 13 const Size.fromHeight(double height) : super(double.INFINITY, height); |
14 | 14 |
15 final double width; | 15 double get width => _dx; |
16 final double height; | 16 double get height => _dy; |
17 | 17 |
18 static const Size zero = const Size(0.0, 0.0); | 18 static const Size zero = const Size(0.0, 0.0); |
19 static const Size infinite = const Size(double.INFINITY, double.INFINITY); | 19 static const Size infinite = const Size(double.INFINITY, double.INFINITY); |
20 | 20 |
21 bool operator ==(other) => other is Size && width == other.width && height ==
other.height; | 21 dynamic operator -(dynamic other) { |
22 Size operator +(Size other) => new Size(width + other.width, height + other.he
ight); | 22 if (other is Size) |
23 Size operator -(Size other) => new Size(width - other.width, height - other.he
ight); | 23 return new Offset(width - other.width, height - other.height); |
| 24 if (other is Offset) |
| 25 return new Size(width - other.dx, height - other.dy); |
| 26 throw new ArgumentError(other); |
| 27 } |
| 28 Size operator +(Offset other) => new Size(width + other.dx, height + other.dy)
; |
| 29 Size operator *(double operand) => new Size(width * operand, height * operand)
; |
| 30 Size operator /(double operand) => new Size(width / operand, height / operand)
; |
| 31 Size operator ~/(double operand) => new Size((width ~/ operand).toDouble(), (h
eight ~/ operand).toDouble()); |
| 32 Size operator %(double operand) => new Size(width % operand, height % operand)
; |
24 | 33 |
25 bool get isInfinite => width >= double.INFINITY || height >= double.INFINITY; | 34 double get shortestSide { |
| 35 double w = width.abs(); |
| 36 double h = height.abs(); |
| 37 return w < h ? w : h; |
| 38 } |
26 | 39 |
27 // does the equivalent of "return new Point(0,0) + this" | 40 // Convenience methods that do the equivalent of calling the similarly named |
28 Point toPoint() => new Point(this.width, this.height); | 41 // methods on a Rect constructed from the given origin and this size. |
| 42 Point center(Point origin) => new Point(origin.x + width / 2.0, origin.y + hei
ght / 2.0); |
| 43 Point topLeft(Point origin) => origin; |
| 44 Point topRight(Point origin) => new Point(origin.x + width, origin.y); |
| 45 Point bottomLeft(Point origin) => new Point(origin.x, origin.y + height); |
| 46 Point bottomRight(Point origin) => new Point(origin.x + width, origin.y + heig
ht); |
29 | 47 |
30 int get hashCode { | |
31 int result = 373; | |
32 result = 37 * result + width.hashCode; | |
33 result = 37 * result + height.hashCode; | |
34 return result; | |
35 } | |
36 String toString() => "Size($width, $height)"; | 48 String toString() => "Size($width, $height)"; |
37 } | 49 } |
OLD | NEW |