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

Class for loading textures without duplicates
Also disposes all loaded textures automatically. More...

Public Member Functions

void DisposeAllTextures ()
 
string NormalizePath (string path)
 
CUISprite GetSprite (string path, Rectangle? sourceRect=null, CUISpriteDrawMode? drawMode=null, SpriteEffects? effects=null)
 
CUISprite GetCUISprite (int x, int y, CUISpriteDrawMode? drawMode=null, SpriteEffects? effects=null)
 32x32 square on (x,y) position from CUI.png
 
Texture2D GetTexture (string path)
 
void Dispose ()
 

Static Public Member Functions

static void CreateCheckers ()
 
static void InitStatic ()
 

Public Attributes

Dictionary< string, Texture2D > LoadedTextures = new()
 

Static Public Attributes

static Texture2D Checkers
 Path to additional PNGs, it can be set in CUI.
 

Properties

static Texture2D BackupTexture [get]
 

Detailed Description

Class for loading textures without duplicates
Also disposes all loaded textures automatically.

Definition at line 19 of file CUITextureManager.cs.

Member Function Documentation

◆ CreateCheckers()

static void CrabUI.CUITextureManager.CreateCheckers ( )
static

Definition at line 21 of file CUITextureManager.cs.

22 {
23 int w = 8;
24 int h = 8;
25 Checkers = new Texture2D(GameMain.Instance.GraphicsDevice, w, h);
26
27 Color[] data = new Color[w * h];
28
29 for (int y = 0; y < h; y++)
30 {
31 for (int x = 0; x < w; x++)
32 {
33 data[y * w + x] = (x < w / 2) ^ (y < h / 2) ? Color.White : Color.Black;
34 }
35 }
36
37 Checkers.SetData(0, new Rectangle(0, 0, w, h), data, 0, w * h);
38 }
static Texture2D Checkers
Path to additional PNGs, it can be set in CUI.

◆ Dispose()

void CrabUI.CUITextureManager.Dispose ( )

Definition at line 153 of file CUITextureManager.cs.

154 {
155 DisposeAllTextures();
156
157 LoadedTextures.Clear();
158 LoadedTextures = null;
159 }

◆ DisposeAllTextures()

void CrabUI.CUITextureManager.DisposeAllTextures ( )

Definition at line 71 of file CUITextureManager.cs.

72 {
73 foreach (Texture2D texture in LoadedTextures.Values)
74 {
75 if (texture == BackupTexture) continue;
76 texture.Dispose();
77 }
78 }

◆ GetCUISprite()

CUISprite CrabUI.CUITextureManager.GetCUISprite ( int x,
int y,
CUISpriteDrawMode? drawMode = null,
SpriteEffects? effects = null )

32x32 square on (x,y) position from CUI.png

Definition at line 102 of file CUITextureManager.cs.

103 {
104 return GetSprite(CUI.CUITexturePath, new Rectangle(x * 32, y * 32, 32, 32), drawMode, effects);
105 }

◆ GetSprite()

CUISprite CrabUI.CUITextureManager.GetSprite ( string path,
Rectangle? sourceRect = null,
CUISpriteDrawMode? drawMode = null,
SpriteEffects? effects = null )

Definition at line 85 of file CUITextureManager.cs.

86 {
87 CUISprite sprite = new CUISprite(GetTexture(path))
88 {
89 Path = path,
90 };
91
92 if (sourceRect.HasValue) sprite.SourceRect = sourceRect.Value;
93 if (drawMode.HasValue) sprite.DrawMode = drawMode.Value;
94 if (effects.HasValue) sprite.Effects = effects.Value;
95
96 return sprite;
97 }

◆ GetTexture()

Texture2D CrabUI.CUITextureManager.GetTexture ( string path)

Definition at line 124 of file CUITextureManager.cs.

125 {
126 if (LoadedTextures == null)
127 {
128 CUI.Error($"LoadedTextures was null");
129 return BackupTexture;
130 }
131
132 if (LoadedTextures.ContainsKey(path)) return LoadedTextures[path];
133
134 Texture2D loaded = null;
135
136 if (CUI.AssetsPath != null) loaded ??= TryLoad(Path.Combine(CUI.AssetsPath, path));
137 if (CUI.PGNAssets != null) loaded ??= TryLoad(Path.Combine(CUI.PGNAssets, path));
138 loaded ??= TryLoad(path);
139 if (loaded == null)
140 {
141 CUI.Warning($"Coudn't find {path} texture, setting it to backup texture");
142 loaded ??= BackupTexture;
143 if (CUI.PGNAssets == null)
144 {
145 CUI.Warning($"Note: It was loaded before CUI.PGNAssets was set");
146 }
147 }
148
149 LoadedTextures[path] = loaded;
150 return loaded;
151 }

◆ InitStatic()

static void CrabUI.CUITextureManager.InitStatic ( )
static

Definition at line 39 of file CUITextureManager.cs.

40 {
41 CreateCheckers();
42
43 CUI.OnDispose += () =>
44 {
45 Checkers.Dispose();
46 Checkers = null;
47 };
48 }

◆ NormalizePath()

string CrabUI.CUITextureManager.NormalizePath ( string path)

Definition at line 80 of file CUITextureManager.cs.

81 {
82 return path; //TODO
83 }

Member Data Documentation

◆ Checkers

Texture2D CrabUI.CUITextureManager.Checkers
static

Path to additional PNGs, it can be set in CUI.

Definition at line 68 of file CUITextureManager.cs.

◆ LoadedTextures

Dictionary<string, Texture2D> CrabUI.CUITextureManager.LoadedTextures = new()

Definition at line 70 of file CUITextureManager.cs.

Property Documentation

◆ BackupTexture

Texture2D CrabUI.CUITextureManager.BackupTexture
staticget

Definition at line 69 of file CUITextureManager.cs.


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