1
+ using Unity . UIWidgets . foundation ;
2
+ using Unity . UIWidgets . ui ;
3
+
4
+ namespace Unity . UIWidgets . painting {
5
+ public class FractionalOffset : Alignment {
6
+ public FractionalOffset ( float dx , float dy )
7
+ : base ( dx * 2.0f - 1.0f , dy * 2.0f - 1.0f ) {
8
+ }
9
+
10
+ public static FractionalOffset fromOffsetAndSize ( Offset offset , Size size ) {
11
+ D . assert ( size != null ) ;
12
+ D . assert ( offset != null ) ;
13
+ return new FractionalOffset (
14
+ offset . dx / size . width ,
15
+ offset . dy / size . height
16
+ ) ;
17
+ }
18
+
19
+ public static FractionalOffset fromOffsetAndRect ( Offset offset , Rect rect ) {
20
+ return fromOffsetAndSize (
21
+ offset - rect . topLeft ,
22
+ rect . size
23
+ ) ;
24
+ }
25
+
26
+ public float dx {
27
+ get { return ( this . x + 1.0f ) / 2.0f ; }
28
+ }
29
+
30
+ public float dy {
31
+ get { return ( this . y + 1.0f ) / 2.0f ; }
32
+ }
33
+
34
+ public readonly FractionalOffset topLeft = new FractionalOffset ( 0.0f , 0.0f ) ;
35
+
36
+ public readonly FractionalOffset topCenter = new FractionalOffset ( 0.5f , 0.0f ) ;
37
+
38
+ public readonly FractionalOffset topRight = new FractionalOffset ( 1.0f , 0.0f ) ;
39
+
40
+ public readonly FractionalOffset centerLeft = new FractionalOffset ( 0.0f , 0.5f ) ;
41
+
42
+ public readonly FractionalOffset center = new FractionalOffset ( 0.5f , 0.5f ) ;
43
+
44
+ public readonly FractionalOffset centerRight = new FractionalOffset ( 1.0f , 0.5f ) ;
45
+
46
+ public readonly FractionalOffset bottomLeft = new FractionalOffset ( 0.0f , 1.0f ) ;
47
+
48
+ public readonly FractionalOffset bottomCenter = new FractionalOffset ( 0.5f , 1.0f ) ;
49
+
50
+ public readonly FractionalOffset bottomRight = new FractionalOffset ( 1.0f , 1.0f ) ;
51
+
52
+ public static Alignment operator - ( FractionalOffset a , Alignment b ) {
53
+ if ( ! ( b is FractionalOffset ) ) {
54
+ return ( a as Alignment ) - b ;
55
+ }
56
+
57
+ FractionalOffset typedOther = ( FractionalOffset ) b ;
58
+ return new FractionalOffset ( a . dx - typedOther . dx , a . dy - typedOther . dy ) ;
59
+ }
60
+
61
+ public static Alignment operator + ( FractionalOffset a , Alignment b ) {
62
+ if ( ! ( b is FractionalOffset ) ) {
63
+ return ( a as Alignment ) + b ;
64
+ }
65
+
66
+ FractionalOffset typedOther = ( FractionalOffset ) b ;
67
+ return new FractionalOffset ( a . dx + typedOther . dx , a . dy + typedOther . dy ) ;
68
+ }
69
+
70
+ public static FractionalOffset operator - ( FractionalOffset a ) {
71
+ return new FractionalOffset ( - a . dx , - a . dy ) ;
72
+ }
73
+
74
+ public static FractionalOffset operator * ( FractionalOffset a , float b ) {
75
+ return new FractionalOffset ( a . dx * b , a . dy * b ) ;
76
+ }
77
+
78
+ public static FractionalOffset operator / ( FractionalOffset a , float b ) {
79
+ return new FractionalOffset ( a . dx / b , a . dy / b ) ;
80
+ }
81
+
82
+ public static FractionalOffset operator % ( FractionalOffset a , float b ) {
83
+ return new FractionalOffset ( a . dx % b , a . dy % b ) ;
84
+ }
85
+
86
+ public static FractionalOffset lerp ( FractionalOffset a , FractionalOffset b , float t ) {
87
+ if ( a == null && b == null ) {
88
+ return null ;
89
+ }
90
+
91
+ if ( a == null ) {
92
+ return new FractionalOffset ( MathUtils . lerpFloat ( 0.5f , b . dx , t ) , MathUtils . lerpFloat ( 0.5f , b . dy , t ) ) ;
93
+ }
94
+
95
+ if ( b == null ) {
96
+ return new FractionalOffset ( MathUtils . lerpFloat ( a . dx , 0.5f , t ) , MathUtils . lerpFloat ( a . dy , 0.5f , t ) ) ;
97
+ }
98
+
99
+ return new FractionalOffset ( MathUtils . lerpFloat ( a . dx , b . dx , t ) , MathUtils . lerpFloat ( a . dy , b . dy , t ) ) ;
100
+ }
101
+
102
+ public override string ToString ( ) {
103
+ return $ "FractionalOffset({ this . dx : 0.0} , " +
104
+ $ "{ this . dy : 0.0} )";
105
+ }
106
+ }
107
+ }
0 commit comments