CrabUI
Loading...
Searching...
No Matches
CUITextureManager.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5using System.Diagnostics;
6
7using Barotrauma;
8using Microsoft.Xna.Framework;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Graphics;
11using System.IO;
12
13namespace CrabUI
14{
15 /// <summary>
16 /// Class for loading textures without duplicates
17 /// Also disposes all loaded textures automatically
18 /// </summary>
19 public class CUITextureManager : IDisposable
20 {
21 public static void CreateCheckers()
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 }
39 public static void InitStatic()
40 {
41 CreateCheckers();
42
43 CUI.OnDispose += () =>
44 {
45 Checkers.Dispose();
46 Checkers = null;
47 };
48 }
49
50 // private static Texture2D backupTexture;
51 // public static Texture2D BackupTexture
52 // {
53 // get
54 // {
55 // if (backupTexture == null)
56 // {
57 // backupTexture = new Texture2D(GameMain.Instance.GraphicsDevice, 1, 1);
58 // backupTexture.SetData(new Color[] { Color.White });
59 // }
60 // return backupTexture;
61 // }
62 // }
63
64 /// <summary>
65 /// Path to additional PNGs, it can be set in CUI
66 /// </summary>
67 ///
68 public static Texture2D Checkers;
69 public static Texture2D BackupTexture => GUI.WhiteTexture;
70 public Dictionary<string, Texture2D> LoadedTextures = new();
71 public void DisposeAllTextures()
72 {
73 foreach (Texture2D texture in LoadedTextures.Values)
74 {
75 if (texture == BackupTexture) continue;
76 texture.Dispose();
77 }
78 }
79
80 public string NormalizePath(string path)
81 {
82 return path; //TODO
83 }
84
85 public CUISprite GetSprite(string path, Rectangle? sourceRect = null, CUISpriteDrawMode? drawMode = null, SpriteEffects? effects = null)
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 }
98
99 /// <summary>
100 /// 32x32 square on (x,y) position from CUI.png
101 /// </summary>
102 public CUISprite GetCUISprite(int x, int y, CUISpriteDrawMode? drawMode = null, SpriteEffects? effects = null)
103 {
104 return GetSprite(CUI.CUITexturePath, new Rectangle(x * 32, y * 32, 32, 32), drawMode, effects);
105 }
106
107 private Texture2D TryLoad(string path)
108 {
109 Texture2D texture = null;
110 try
111 {
112 if (File.Exists(path))
113 {
114 using (FileStream fs = File.OpenRead(path))
115 {
116 texture = Texture2D.FromStream(GameMain.Instance.GraphicsDevice, fs);
117 }
118 }
119 }
120 catch { }
121 return texture;
122 }
123
124 public Texture2D GetTexture(string path)
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 }
152
153 public void Dispose()
154 {
155 DisposeAllTextures();
156
157 LoadedTextures.Clear();
158 LoadedTextures = null;
159 }
160 }
161
162}
In fact a static class managing static things.
Definition CUIErrors.cs:16
Wrapper Containing link to texture and drawing settings, like SourceRedt, DrawMode,...
Definition CUISprite.cs:27
Class for loading textures without duplicates Also disposes all loaded textures automatically.
static Texture2D Checkers
Path to additional PNGs, it can be set in CUI.
CUISprite GetCUISprite(int x, int y, CUISpriteDrawMode? drawMode=null, SpriteEffects? effects=null)
32x32 square on (x,y) position from CUI.png