CrabUI
Loading...
Searching...
No Matches
CUIRect.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6using Barotrauma;
7using Microsoft.Xna.Framework;
8using Microsoft.Xna.Framework.Input;
9using Microsoft.Xna.Framework.Graphics;
10
11namespace CrabUI
12{
13 /// <summary>
14 /// Rectangle with float
15 /// </summary>
16 public struct CUIRect
17 {
18 /// <summary>
19 /// IDK where to put it
20 /// </summary>
21 public static Rectangle CreateRect(float x, float y, float w, float h)
22 {
23 return new Rectangle((int)Math.Round(x), (int)Math.Round(y), (int)Math.Round(w), (int)Math.Round(h));
24 }
25
26
27 public float Left;
28 public float Top;
29 public float Width;
30 public float Height;
31
32 public float Right => Left + Width;
33 public float Bottom => Top + Height;
34
35 public Vector2 Size => new Vector2(Width, Height);
36 public Vector2 Position => new Vector2(Left, Top);
37 public Vector2 Center => new Vector2(Left + Width / 2, Top + Height / 2);
38 public Rectangle Box => new Rectangle((int)Left, (int)Top, (int)Width, (int)Height);
39
40 public Vector2 LeftTop => new Vector2(Left, Top);
41 public Vector2 LeftCenter => new Vector2(Left, Top + Height / 2);
42 public Vector2 LeftBottom => new Vector2(Left, Top + Height);
43 public Vector2 CenterTop => new Vector2(Left + Width / 2, Top);
44 public Vector2 CenterCenter => new Vector2(Left + Width / 2, Top + Height / 2);
45 public Vector2 CenterBottom => new Vector2(Left + Width / 2, Top + Height);
46 public Vector2 RightTop => new Vector2(Left + Width, Top);
47 public Vector2 RightCenter => new Vector2(Left + Width, Top + Height / 2);
48 public Vector2 RightBottom => new Vector2(Left + Width, Top + Height);
49
50
51
52 public CUIRect Shift(Vector2 shift)
53 {
54 return new CUIRect(Left + shift.X, Top + shift.Y, Width, Height);
55 }
56
57 public bool Contains(float x, float y)
58 {
59 return Left < x && x < Right && Top < y && y < Bottom;
60 }
61 public bool Contains(Vector2 pos)
62 {
63 return Left < pos.X && pos.X < Right && Top < pos.Y && pos.Y < Bottom;
64 }
65 public bool Intersect(CUIRect r)
66 {
67 return r.Right >= Left && r.Left <= Right && r.Bottom >= Top && r.Top <= Bottom;
68 }
69
70 public Rectangle Zoom(float Z)
71 {
72 Vector2 ScreenCenter = CUI.GameScreenSize / 2.0f;
73 Vector2 PosDif = Position - ScreenCenter;
74 Vector2 newPos = PosDif * Z + ScreenCenter;
75
76 return new Rectangle(
77 (int)newPos.X, (int)newPos.Y,
78 (int)(Width / Z), (int)(Height / Z)
79 );
80 }
81
82
83
84
85 public CUIRect(Vector2 size) : this(0, 0, size.X, size.Y) { }
86 public CUIRect(Vector2 position, Vector2 size) : this(position.X, position.Y, size.X, size.Y) { }
87 public CUIRect(float x, float y, float w, float h)
88 {
89 Left = x;
90 Top = y;
91 Width = Math.Max(0f, w);
92 Height = Math.Max(0f, h);
93 }
94
95
96
97 public override string ToString() => $"[{Left},{Top},{Width},{Height}]";
98 }
99
100 /// <summary>
101 /// Rectangle with float?
102 /// </summary>
103 public struct CUINullRect
104 {
105 // Guh...
106 public static CUINullRect Parse(string s)
107 {
108 string content = s.Substring(
109 s.IndexOf('[') + 1,
110 s.IndexOf(']') - s.IndexOf('[') - 1
111 );
112
113 var components = content.Split(',').Select(a => a.Trim());
114
115 string sx = components.ElementAtOrDefault(0);
116 string sy = components.ElementAtOrDefault(1);
117 string sw = components.ElementAtOrDefault(2);
118 string sh = components.ElementAtOrDefault(3);
119
120 float? x = null;
121 float? y = null;
122 float? w = null;
123 float? h = null;
124
125 if (sx == null || sx == "") x = null;
126 else x = float.Parse(sx);
127
128 if (sy == null || sy == "") y = null;
129 else y = float.Parse(sy);
130
131 if (sw == null || sw == "") w = null;
132 else w = float.Parse(sw);
133
134 if (sh == null || sh == "") h = null;
135 else h = float.Parse(sh);
136
137 return new CUINullRect(x, y, w, h);
138 }
139
140 public float? Left;
141 public float? Top;
142 public float? Width;
143 public float? Height;
144
145 public Vector2 Size
146 {
147 get => new Vector2(Width ?? 0, Height ?? 0);
148 set { Width = value.X; Height = value.Y; }
149 }
150 public Vector2 Position
151 {
152 get => new Vector2(Left ?? 0, Top ?? 0);
153 set { Left = value.X; Top = value.Y; }
154 }
155
156 public Vector2 Center => new Vector2(
157 (Left ?? 0) + (Width ?? 0) / 2,
158 (Top ?? 0) + (Height ?? 0) / 2
159 );
160
161 public CUINullRect(Vector2 position, Vector2 size) : this(position.X, position.Y, size.X, size.Y) { }
162
163 public CUINullRect(float? x = null, float? y = null, float? w = null, float? h = null)
164 {
165 Left = x;
166 Top = y;
167 Width = w;
168 Height = h;
169 }
170
171 public override string ToString() => $"[{Left},{Top},{Width},{Height}]";
172
173 }
174}
Rectangle with float?
Definition CUIRect.cs:104
Rectangle with float.
Definition CUIRect.cs:17
static Rectangle CreateRect(float x, float y, float w, float h)
IDK where to put it.
Definition CUIRect.cs:21