67 SourceRect =
new Rectangle(0, 0, texture.Width, texture.Height);
79 get => (float)(
Rotation * 180 / Math.PI);
80 set =>
Rotation = (float)(value * Math.PI / 180);
97 private Vector2 offset;
112 public override bool Equals(
object obj)
114 if (obj is not
CUISprite b)
return false;
130 if (sprite ==
null)
return Default;
132 return new CUISprite(sprite.Texture, sprite.SourceRect)
134 Path = sprite.FullPath,
142 public static CUISprite FromId(Identifier
id)
144 GUIComponentStyle? style = GUIStyle.ComponentStyles[id];
145 if (style ==
null)
return Default;
147 return FromComponentStyle(style);
150 public static CUISprite FromComponentStyle(GUIComponentStyle style, GUIComponent.ComponentState state = GUIComponent.ComponentState.None)
152 return FromVanilla(style.Sprites[state].FirstOrDefault()?.Sprite);
171 public CUISprite(
string path, Rectangle? sourceRect =
null,
string baseFolder =
null)
174 string realpath = path;
176 if (!System.IO.Path.IsPathRooted(path) && baseFolder !=
null)
178 string localPath = System.IO.Path.Combine(baseFolder, path);
179 if (File.Exists(localPath)) realpath = localPath;
183 Texture = CUI.TextureManager.GetTexture(realpath);
184 if (sourceRect.HasValue)
SourceRect = sourceRect.Value;
186 public CUISprite(Texture2D texture, Rectangle? sourceRect =
null)
189 if (sourceRect.HasValue)
SourceRect = sourceRect.Value;
192 public object Clone()
207 public override string ToString()
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)}" :
"";
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)}" :
"";
217 return $
"{{ Path: {Path}{mode}{rect}{effect}{rotation}{offset}{origin} }}";
221 public static CUISprite Parse(
string raw)
223 Dictionary<string, string> props = CUIExtensions.ParseKVPairs(raw);
225 if (!props.ContainsKey(
"path"))
return new CUISprite();
227 CUISprite sprite = CUI.TextureManager.GetSprite(props[
"path"]);
228 if (props.ContainsKey(
"mode"))
230 sprite.DrawMode = Enum.Parse<CUISpriteDrawMode>(props[
"mode"]);
232 if (props.ContainsKey(
"sourcerect"))
234 sprite.SourceRect = CUIExtensions.ParseRectangle(props[
"sourcerect"]);
238 sprite.SourceRect =
new Rectangle(0, 0, sprite.Texture.Width, sprite.Texture.Height);
240 if (props.ContainsKey(
"effects"))
242 sprite.Effects = CUIExtensions.ParseSpriteEffects(props[
"effects"]);
245 if (props.ContainsKey(
"rotation"))
248 float.TryParse(props[
"rotation"], out r);
252 if (props.ContainsKey(
"offset"))
254 sprite.Offset = CUIExtensions.ParseVector2(props[
"offset"]);
257 if (props.ContainsKey(
"origin"))
259 sprite.Origin = CUIExtensions.ParseVector2(props[
"origin"]);
266 public static CUISprite ParseWithContext(
string raw,
string baseFolder =
null)
268 Dictionary<string, string> props = CUIExtensions.ParseKVPairs(raw);
270 if (!props.ContainsKey(
"path"))
return new CUISprite();
272 if (!System.IO.Path.IsPathRooted(props[
"path"]) && baseFolder !=
null)
274 string localPath = System.IO.Path.Combine(baseFolder, props[
"path"]);
276 if (File.Exists(localPath)) props[
"path"] = localPath;
279 CUISprite sprite = CUI.TextureManager.GetSprite(props[
"path"]);
280 if (props.ContainsKey(
"mode"))
282 sprite.DrawMode = Enum.Parse<CUISpriteDrawMode>(props[
"mode"]);
284 if (props.ContainsKey(
"sourcerect"))
286 sprite.SourceRect = CUIExtensions.ParseRectangle(props[
"sourcerect"]);
290 sprite.SourceRect =
new Rectangle(0, 0, sprite.Texture.Width, sprite.Texture.Height);
292 if (props.ContainsKey(
"effects"))
294 sprite.Effects = CUIExtensions.ParseSpriteEffects(props[
"effects"]);
297 if (props.ContainsKey(
"rotation"))
300 float.TryParse(props[
"rotation"], out r);
304 if (props.ContainsKey(
"offset"))
306 sprite.Offset = CUIExtensions.ParseVector2(props[
"offset"]);
309 if (props.ContainsKey(
"origin"))
311 sprite.Origin = CUIExtensions.ParseVector2(props[
"origin"]);