1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
4 using System.Collections.Generic;
6 using System.Threading.Tasks;
13 private static readonly List<string> activeTextureLoads =
new List<string>();
15 private static bool loadingTextures;
21 return loadingTextures;
33 private readonly
bool scaleToFit;
35 private bool lazyLoaded, loading;
47 public void SetCrop(
bool state,
bool center =
true)
50 if (crop && sprite !=
null)
57 sourceRect.X = (sprite.
SourceRect.Width - sourceRect.Width) / 2;
58 sourceRect.Y = (sprite.
SourceRect.Height - sourceRect.Height) / 2;
61 origin = sourceRect.Size.ToVector2() / 2;
65 origin = sprite ==
null ? Vector2.Zero : sprite.
size / 2;
69 private Vector2 origin;
79 get {
return sourceRect; }
80 set { sourceRect = value; }
85 get {
return sprite; }
88 if (sprite == value) {
return; }
90 sourceRect = value ==
null ?
Rectangle.Empty : value.SourceRect;
91 origin = value ==
null ? Vector2.Zero : value.size / 2;
92 if (scaleToFit) { RecalculateScale(); }
101 : this(rectT, null, null, scaleToFit, style)
106 : this(rectT, sprite, sourceRect, scaleToFit, null)
112 this.scaleToFit = scaleToFit;
114 if (sourceRect.HasValue)
116 this.sourceRect = sourceRect.Value;
120 this.sourceRect = sprite ==
null ? Rectangle.Empty : sprite.
SourceRect;
140 protected override void Draw(SpriteBatch spriteBatch)
142 if (!
Visible || loading) {
return; }
151 loadingTextures =
true;
153 TaskPool.Add(
"LoadTextureAsync",
154 LoadTextureAsync(), task =>
156 if (task.Exception !=
null)
158 var innerMost = task.Exception.GetInnermost();
159 DebugConsole.ThrowError($
"Failed to load \"{Sprite.FilePath}\"", innerMost);
190 if (Math.Abs(
Rotation) >
float.Epsilon)
202 else if (sprite?.Texture is { IsDisposed:
false })
204 spriteBatch.Draw(sprite.
Texture,
new Vector2(
Rect.X +
Rect.Width / 2.0f,
Rect.Y +
Rect.Height / 2.0f), sourceRect, currentColor * (currentColor.A / 255.0f),
Rotation, origin,
215 private void RecalculateScale()
217 if (sourceRect ==
Rectangle.Empty && sprite !=
null)
227 private async Task<bool> LoadTextureAsync()
236 lock (activeTextureLoads)
244 lock (activeTextureLoads)
252 DateTime timeOut = DateTime.Now +
new TimeSpan(0, 0, 10);
257 lock (activeTextureLoads)
260 loadingTextures = activeTextureLoads.Count > 0;
virtual ComponentState State
virtual Color GetColor(ComponentState state)
RectTransform RectTransform
SpriteEffects SpriteEffects
void SetCrop(bool state, bool center=true)
ComponentState? OverrideState
GUIImage(RectTransform rectT, string style, bool scaleToFit=false)
override void Draw(SpriteBatch spriteBatch)
static bool LoadingTextures
GUIImage(RectTransform rectT, Sprite sprite, Rectangle? sourceRect=null, bool scaleToFit=false)
static RasterizerState ScissorTestEnable
async Task LazyLoadAsync()
void EnsureLazyLoaded(bool isAsync=false)
void Draw(SpriteBatch spriteBatch, RectangleF rect, Color color, SpriteEffects spriteEffects=SpriteEffects.None, Vector2? uvOffset=null)