CrabUI
Loading...
Searching...
No Matches
CUISprite.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;
10using System.IO;
11namespace CrabUI
12{
13 public enum CUISpriteDrawMode
14 {
15 Resize,
16 Wrap,
17 Static,
18 StaticDeep,
19 }
20
21 /// <summary>
22 /// Wrapper Containing link to texture and drawing settings,
23 /// like SourceRedt, DrawMode, Effects, Rotation...
24 /// Multiple sprites can use the same texture
25 /// </summary>
26 public class CUISprite : ICloneable
27 {
28 /// <summary>
29 /// 1x1 white texture
30 /// </summary>
31 public static Texture2D BackupTexture => GUI.WhiteTexture;
32 /// <summary>
33 /// new Sprite that uses 1x1 default texture
34 /// </summary>
35 public static CUISprite Default => new CUISprite();
36
37 /// <summary>
38 /// Set when you load it from some path
39 /// </summary>
40 public string Path = "";
41 /// <summary>
42 /// None, FlipHorizontally, FlipVertically
43 /// </summary>
44 public SpriteEffects Effects;
45 /// <summary>
46 /// Resize - will resize the sprite to component
47 /// Wrap - will loop the texture
48 /// Static - sprite ignores component position
49 /// </summary>
50 public CUISpriteDrawMode DrawMode;
51 /// <summary>
52 /// Part of the texture that is drawn
53 /// Won't work in Wrap mode becase it will loop the whole texture
54 /// </summary>
55 public Rectangle SourceRect;
56 private Texture2D texture = BackupTexture;
57 /// <summary>
58 /// The link to the texture
59 /// Multiple sprites can use the same texture
60 /// </summary>
61 public Texture2D Texture
62 {
63 get => texture;
64 set
65 {
66 texture = value;
67 SourceRect = new Rectangle(0, 0, texture.Width, texture.Height);
68 }
69 }
70 /// <summary>
71 /// In radians
72 /// </summary>
73 public float Rotation;
74 /// <summary>
75 /// In degree
76 /// </summary>
77 public float RotationAngle
78 {
79 get => (float)(Rotation * 180 / Math.PI);
80 set => Rotation = (float)(value * Math.PI / 180);
81 }
82 /// <summary>
83 /// Origin of rotation in pixels
84 /// </summary>
85 public Vector2 Origin;
86 /// <summary>
87 /// Origin of rotation in [0..1] of texture size
88 /// </summary>
89 public Vector2 RelativeOrigin
90 {
91 set
92 {
93 if (Texture == null) return;
94 Origin = new Vector2(value.X * Texture.Width, value.Y * Texture.Height);
95 }
96 }
97 private Vector2 offset;
98 /// <summary>
99 /// Draw offset from CUIComponent Position
100 /// For your convenience also sets origin
101 /// </summary>
102 public Vector2 Offset
103 {
104 get => offset;
105 set
106 {
107 offset = value;
108 RelativeOrigin = value;
109 }
110 }
111
112 public override bool Equals(object obj)
113 {
114 if (obj is not CUISprite b) return false;
115
116 return Texture == b.Texture &&
117 SourceRect == b.SourceRect &&
118 DrawMode == b.DrawMode &&
119 Effects == b.Effects &&
120 Rotation == b.Rotation &&
121 Origin == b.Origin &&
122 Offset == b.Offset;
123 }
124
125 /// <summary>
126 /// Creates a CUISprite from vanilla Sprite
127 /// </summary>
128 public static CUISprite FromVanilla(Sprite sprite)
129 {
130 if (sprite == null) return Default;
131
132 return new CUISprite(sprite.Texture, sprite.SourceRect)
133 {
134 Path = sprite.FullPath,
135 };
136 }
137
138 /// <summary>
139 /// Uses vanilla GUI sprite from GUIStyle.ComponentStyles with this name
140 /// </summary>
141 public static CUISprite FromName(string name) => FromId(new Identifier(name));
142 public static CUISprite FromId(Identifier id)
143 {
144 GUIComponentStyle? style = GUIStyle.ComponentStyles[id];
145 if (style == null) return Default;
146
147 return FromComponentStyle(style);
148 }
149
150 public static CUISprite FromComponentStyle(GUIComponentStyle style, GUIComponent.ComponentState state = GUIComponent.ComponentState.None)
151 {
152 return FromVanilla(style.Sprites[state].FirstOrDefault()?.Sprite);
153 }
154
155 //TODO add using construction
156 /// <summary>
157 /// When you load sprite from file, relative paths are considered relative to barotrauma folder
158 /// if BaseFolder != null sprite will check files in BaseFolder first
159 /// Don't forget to set it back to null
160 /// </summary>
161 public static string BaseFolder { get; set; }
162
163 /// <summary>
164 /// Default 1x1 white sprite
165 /// </summary>
166 public CUISprite()
167 {
169 SourceRect = new Rectangle(0, 0, Texture.Width, Texture.Height);
170 }
171 public CUISprite(string path, Rectangle? sourceRect = null, string baseFolder = null)
172 {
173 baseFolder ??= BaseFolder;
174 string realpath = path;
175
176 if (!System.IO.Path.IsPathRooted(path) && baseFolder != null)
177 {
178 string localPath = System.IO.Path.Combine(baseFolder, path);
179 if (File.Exists(localPath)) realpath = localPath;
180 }
181
182 Path = path;
183 Texture = CUI.TextureManager.GetTexture(realpath);
184 if (sourceRect.HasValue) SourceRect = sourceRect.Value;
185 }
186 public CUISprite(Texture2D texture, Rectangle? sourceRect = null)
187 {
188 Texture = texture ?? BackupTexture;
189 if (sourceRect.HasValue) SourceRect = sourceRect.Value;
190 }
191
192 public object Clone()
193 {
195 {
196 Path = this.Path,
197 Rotation = this.Rotation,
198 Offset = this.Offset,
199 Origin = this.Origin,
200 Effects = this.Effects,
201 DrawMode = this.DrawMode,
202 };
203
204 return sprite;
205 }
206
207 public override string ToString()
208 {
209 string mode = DrawMode != CUISpriteDrawMode.Resize ? $", Mode: {DrawMode}" : "";
210 string rect = SourceRect != Texture.Bounds ? $", SourceRect: {CUIExtensions.RectangleToString(SourceRect)}" : "";
211 string effect = Effects != SpriteEffects.None ? $", Effects: {CUIExtensions.SpriteEffectsToString(Effects)}" : "";
212
213 string rotation = Rotation != 0.0f ? $", Rotation: {Rotation}" : "";
214 string offset = Offset != Vector2.Zero ? $", Offset: {CUIExtensions.Vector2ToString(Offset)}" : "";
215 string origin = Origin != Vector2.Zero ? $", Origin: {CUIExtensions.Vector2ToString(Origin)}" : "";
216
217 return $"{{ Path: {Path}{mode}{rect}{effect}{rotation}{offset}{origin} }}";
218 }
219
220 //BUG it can't use absolute paths because of the c://
221 public static CUISprite Parse(string raw)
222 {
223 Dictionary<string, string> props = CUIExtensions.ParseKVPairs(raw);
224
225 if (!props.ContainsKey("path")) return new CUISprite();
226
227 CUISprite sprite = CUI.TextureManager.GetSprite(props["path"]);
228 if (props.ContainsKey("mode"))
229 {
230 sprite.DrawMode = Enum.Parse<CUISpriteDrawMode>(props["mode"]);
231 }
232 if (props.ContainsKey("sourcerect"))
233 {
234 sprite.SourceRect = CUIExtensions.ParseRectangle(props["sourcerect"]);
235 }
236 else
237 {
238 sprite.SourceRect = new Rectangle(0, 0, sprite.Texture.Width, sprite.Texture.Height);
239 }
240 if (props.ContainsKey("effects"))
241 {
242 sprite.Effects = CUIExtensions.ParseSpriteEffects(props["effects"]);
243 }
244
245 if (props.ContainsKey("rotation"))
246 {
247 float r;
248 float.TryParse(props["rotation"], out r);
249 sprite.Rotation = r;
250 }
251
252 if (props.ContainsKey("offset"))
253 {
254 sprite.Offset = CUIExtensions.ParseVector2(props["offset"]);
255 }
256
257 if (props.ContainsKey("origin"))
258 {
259 sprite.Origin = CUIExtensions.ParseVector2(props["origin"]);
260 }
261
262 return sprite;
263 }
264
265 //TODO find less hacky solution
266 public static CUISprite ParseWithContext(string raw, string baseFolder = null)
267 {
268 Dictionary<string, string> props = CUIExtensions.ParseKVPairs(raw);
269
270 if (!props.ContainsKey("path")) return new CUISprite();
271
272 if (!System.IO.Path.IsPathRooted(props["path"]) && baseFolder != null)
273 {
274 string localPath = System.IO.Path.Combine(baseFolder, props["path"]);
275
276 if (File.Exists(localPath)) props["path"] = localPath;
277 }
278
279 CUISprite sprite = CUI.TextureManager.GetSprite(props["path"]);
280 if (props.ContainsKey("mode"))
281 {
282 sprite.DrawMode = Enum.Parse<CUISpriteDrawMode>(props["mode"]);
283 }
284 if (props.ContainsKey("sourcerect"))
285 {
286 sprite.SourceRect = CUIExtensions.ParseRectangle(props["sourcerect"]);
287 }
288 else
289 {
290 sprite.SourceRect = new Rectangle(0, 0, sprite.Texture.Width, sprite.Texture.Height);
291 }
292 if (props.ContainsKey("effects"))
293 {
294 sprite.Effects = CUIExtensions.ParseSpriteEffects(props["effects"]);
295 }
296
297 if (props.ContainsKey("rotation"))
298 {
299 float r;
300 float.TryParse(props["rotation"], out r);
301 sprite.Rotation = r;
302 }
303
304 if (props.ContainsKey("offset"))
305 {
306 sprite.Offset = CUIExtensions.ParseVector2(props["offset"]);
307 }
308
309 if (props.ContainsKey("origin"))
310 {
311 sprite.Origin = CUIExtensions.ParseVector2(props["origin"]);
312 }
313
314 return sprite;
315 }
316 }
317}
Wrapper Containing link to texture and drawing settings, like SourceRedt, DrawMode,...
Definition CUISprite.cs:27
float RotationAngle
In degree.
Definition CUISprite.cs:78
Vector2 Offset
Draw offset from CUIComponent Position For your convenience also sets origin.
Definition CUISprite.cs:103
static string BaseFolder
When you load sprite from file, relative paths are considered relative to barotrauma folder if Base...
Definition CUISprite.cs:161
float Rotation
In radians.
Definition CUISprite.cs:73
Rectangle SourceRect
Part of the texture that is drawn Won't work in Wrap mode becase it will loop the whole texture.
Definition CUISprite.cs:55
CUISprite()
Default 1x1 white sprite.
Definition CUISprite.cs:166
CUISpriteDrawMode DrawMode
Resize - will resize the sprite to component Wrap - will loop the texture Static - sprite ignores...
Definition CUISprite.cs:50
static CUISprite FromName(string name)
Uses vanilla GUI sprite from GUIStyle.ComponentStyles with this name.
Vector2 Origin
Origin of rotation in pixels.
Definition CUISprite.cs:85
string Path
Set when you load it from some path.
Definition CUISprite.cs:40
Texture2D Texture
The link to the texture Multiple sprites can use the same texture.
Definition CUISprite.cs:62
static CUISprite Default
new Sprite that uses 1x1 default texture
Definition CUISprite.cs:35
static CUISprite FromVanilla(Sprite sprite)
Creates a CUISprite from vanilla Sprite.
Definition CUISprite.cs:128
SpriteEffects Effects
None, FlipHorizontally, FlipVertically.
Definition CUISprite.cs:44
static Texture2D BackupTexture
1x1 white texture
Definition CUISprite.cs:31
Vector2 RelativeOrigin
Origin of rotation in [0..1] of texture size.
Definition CUISprite.cs:90