CrabUI
Loading...
Searching...
No Matches
CrabUI.CUISprite Class Reference

Wrapper Containing link to texture and drawing settings,
like SourceRedt, DrawMode, Effects, Rotation...
Multiple sprites can use the same texture. More...

Public Member Functions

override bool Equals (object obj)
 
 CUISprite ()
 Default 1x1 white sprite.
 
 CUISprite (string path, Rectangle? sourceRect=null, string baseFolder=null)
 
 CUISprite (Texture2D texture, Rectangle? sourceRect=null)
 
object Clone ()
 
override string ToString ()
 

Static Public Member Functions

static CUISprite FromVanilla (Sprite sprite)
 Creates a CUISprite from vanilla Sprite.
 
static CUISprite FromName (string name)
 Uses vanilla GUI sprite from GUIStyle.ComponentStyles with this name.
 
static CUISprite FromId (Identifier id)
 
static CUISprite FromComponentStyle (GUIComponentStyle style, GUIComponent.ComponentState state=GUIComponent.ComponentState.None)
 
static CUISprite Parse (string raw)
 
static CUISprite ParseWithContext (string raw, string baseFolder=null)
 

Public Attributes

string Path = ""
 Set when you load it from some path.
 
SpriteEffects Effects
 None, FlipHorizontally, FlipVertically.
 
CUISpriteDrawMode DrawMode
 Resize - will resize the sprite to component
Wrap - will loop the texture
Static - sprite ignores component position.
 
Rectangle SourceRect
 Part of the texture that is drawn
Won't work in Wrap mode becase it will loop the whole texture.
 
float Rotation
 In radians.
 
Vector2 Origin
 Origin of rotation in pixels.
 

Properties

static Texture2D BackupTexture [get]
 1x1 white texture
 
static CUISprite Default [get]
 new Sprite that uses 1x1 default texture
 
Texture2D Texture [get, set]
 The link to the texture
Multiple sprites can use the same texture.
 
float RotationAngle [get, set]
 In degree.
 
Vector2 RelativeOrigin [set]
 Origin of rotation in [0..1] of texture size.
 
Vector2 Offset [get, set]
 Draw offset from CUIComponent Position
For your convenience also sets origin.
 
static string BaseFolder [get, set]
 When you load sprite from file, relative paths are considered relative to barotrauma folder
if BaseFolder != null sprite will check files in BaseFolder first Don't forget to set it back to null.
 

Detailed Description

Wrapper Containing link to texture and drawing settings,
like SourceRedt, DrawMode, Effects, Rotation...
Multiple sprites can use the same texture.

Definition at line 26 of file CUISprite.cs.

Constructor & Destructor Documentation

◆ CUISprite() [1/3]

CrabUI.CUISprite.CUISprite ( )

Default 1x1 white sprite.

Definition at line 166 of file CUISprite.cs.

167 {
169 SourceRect = new Rectangle(0, 0, Texture.Width, Texture.Height);
170 }
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
Texture2D Texture
The link to the texture Multiple sprites can use the same texture.
Definition CUISprite.cs:62
static Texture2D BackupTexture
1x1 white texture
Definition CUISprite.cs:31

◆ CUISprite() [2/3]

CrabUI.CUISprite.CUISprite ( string path,
Rectangle? sourceRect = null,
string baseFolder = null )

Definition at line 171 of file CUISprite.cs.

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 }
static string BaseFolder
When you load sprite from file, relative paths are considered relative to barotrauma folder if Base...
Definition CUISprite.cs:161
string Path
Set when you load it from some path.
Definition CUISprite.cs:40

◆ CUISprite() [3/3]

CrabUI.CUISprite.CUISprite ( Texture2D texture,
Rectangle? sourceRect = null )

Definition at line 186 of file CUISprite.cs.

187 {
188 Texture = texture ?? BackupTexture;
189 if (sourceRect.HasValue) SourceRect = sourceRect.Value;
190 }

Member Function Documentation

◆ Clone()

object CrabUI.CUISprite.Clone ( )

Definition at line 192 of file CUISprite.cs.

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 }
Vector2 Offset
Draw offset from CUIComponent Position For your convenience also sets origin.
Definition CUISprite.cs:103
float Rotation
In radians.
Definition CUISprite.cs:73
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
Vector2 Origin
Origin of rotation in pixels.
Definition CUISprite.cs:85
SpriteEffects Effects
None, FlipHorizontally, FlipVertically.
Definition CUISprite.cs:44

◆ Equals()

override bool CrabUI.CUISprite.Equals ( object obj)

Definition at line 112 of file CUISprite.cs.

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 }

◆ FromComponentStyle()

static CUISprite CrabUI.CUISprite.FromComponentStyle ( GUIComponentStyle style,
GUIComponent.ComponentState state = GUIComponent::ComponentState::None )
static

Definition at line 150 of file CUISprite.cs.

151 {
152 return FromVanilla(style.Sprites[state].FirstOrDefault()?.Sprite);
153 }
static CUISprite FromVanilla(Sprite sprite)
Creates a CUISprite from vanilla Sprite.
Definition CUISprite.cs:128

◆ FromId()

static CUISprite CrabUI.CUISprite.FromId ( Identifier id)
static

Definition at line 142 of file CUISprite.cs.

143 {
144 GUIComponentStyle? style = GUIStyle.ComponentStyles[id];
145 if (style == null) return Default;
146
147 return FromComponentStyle(style);
148 }
static CUISprite Default
new Sprite that uses 1x1 default texture
Definition CUISprite.cs:35

◆ FromVanilla()

static CUISprite CrabUI.CUISprite.FromVanilla ( Sprite sprite)
static

Creates a CUISprite from vanilla Sprite.

Definition at line 128 of file CUISprite.cs.

129 {
130 if (sprite == null) return Default;
131
132 return new CUISprite(sprite.Texture, sprite.SourceRect)
133 {
134 Path = sprite.FullPath,
135 };
136 }

◆ Parse()

static CUISprite CrabUI.CUISprite.Parse ( string raw)
static

Definition at line 221 of file CUISprite.cs.

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 }

◆ ParseWithContext()

static CUISprite CrabUI.CUISprite.ParseWithContext ( string raw,
string baseFolder = null )
static

Definition at line 266 of file CUISprite.cs.

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 }

◆ ToString()

override string CrabUI.CUISprite.ToString ( )

Definition at line 207 of file CUISprite.cs.

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 }

Member Data Documentation

◆ DrawMode

CUISpriteDrawMode CrabUI.CUISprite.DrawMode

Resize - will resize the sprite to component
Wrap - will loop the texture
Static - sprite ignores component position.

Definition at line 50 of file CUISprite.cs.

◆ Effects

SpriteEffects CrabUI.CUISprite.Effects

None, FlipHorizontally, FlipVertically.

Definition at line 44 of file CUISprite.cs.

◆ Origin

Vector2 CrabUI.CUISprite.Origin

Origin of rotation in pixels.

Definition at line 85 of file CUISprite.cs.

◆ Path

string CrabUI.CUISprite.Path = ""

Set when you load it from some path.

Definition at line 40 of file CUISprite.cs.

◆ Rotation

float CrabUI.CUISprite.Rotation

In radians.

Definition at line 73 of file CUISprite.cs.

◆ SourceRect

Rectangle CrabUI.CUISprite.SourceRect

Part of the texture that is drawn
Won't work in Wrap mode becase it will loop the whole texture.

Definition at line 55 of file CUISprite.cs.

Property Documentation

◆ BackupTexture

Texture2D CrabUI.CUISprite.BackupTexture
staticget

1x1 white texture

Definition at line 31 of file CUISprite.cs.

◆ BaseFolder

string CrabUI.CUISprite.BaseFolder
staticgetset

When you load sprite from file, relative paths are considered relative to barotrauma folder
if BaseFolder != null sprite will check files in BaseFolder first Don't forget to set it back to null.

Definition at line 161 of file CUISprite.cs.

161{ get; set; }

◆ Default

CUISprite CrabUI.CUISprite.Default
staticget

new Sprite that uses 1x1 default texture

Definition at line 35 of file CUISprite.cs.

◆ Offset

Vector2 CrabUI.CUISprite.Offset
getset

Draw offset from CUIComponent Position
For your convenience also sets origin.

Definition at line 102 of file CUISprite.cs.

103 {
104 get => offset;
105 set
106 {
107 offset = value;
108 RelativeOrigin = value;
109 }
110 }
Vector2 RelativeOrigin
Origin of rotation in [0..1] of texture size.
Definition CUISprite.cs:90

◆ RelativeOrigin

Vector2 CrabUI.CUISprite.RelativeOrigin
set

Origin of rotation in [0..1] of texture size.

Definition at line 89 of file CUISprite.cs.

90 {
91 set
92 {
93 if (Texture == null) return;
94 Origin = new Vector2(value.X * Texture.Width, value.Y * Texture.Height);
95 }
96 }

◆ RotationAngle

float CrabUI.CUISprite.RotationAngle
getset

In degree.

Definition at line 77 of file CUISprite.cs.

78 {
79 get => (float)(Rotation * 180 / Math.PI);
80 set => Rotation = (float)(value * Math.PI / 180);
81 }

◆ Texture

Texture2D CrabUI.CUISprite.Texture
getset

The link to the texture
Multiple sprites can use the same texture.

Definition at line 61 of file CUISprite.cs.

62 {
63 get => texture;
64 set
65 {
66 texture = value;
67 SourceRect = new Rectangle(0, 0, texture.Width, texture.Height);
68 }
69 }

The documentation for this class was generated from the following file: