2using System.Collections.Generic;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
10using Microsoft.Xna.Framework;
11using Microsoft.Xna.Framework.Input;
12using Microsoft.Xna.Framework.Graphics;
17using System.Threading;
24 public partial class CUIComponent : ICUIComponent, IDisposable
26 #region Static --------------------------------------------------------
27 internal static void InitStatic()
34 CUI.OnDispose += () =>
36 foreach (
int id in ComponentsById.Keys)
38 CUIComponent component =
null;
39 ComponentsById[id].TryGetTarget(out component);
43 ComponentsById.Clear();
44 ComponentsByType.Clear();
51 internal static int MaxID;
52 public static Dictionary<int, WeakReference<CUIComponent>> ComponentsById =
new();
53 public static IEnumerable<CUIComponent> AllComponents => ComponentsById.Values
56 CUIComponent component =
null;
57 wr.TryGetTarget(out component);
60 .Where(c => c !=
null);
61 public static WeakCatalog<Type, CUIComponent> ComponentsByType =
new();
67 public static GUIButton
dummyComponent =
new GUIButton(
new RectTransform(
new Point(0, 0)))
84 public static void ForEach(Action<CUIComponent> action)
86 foreach (
int id in ComponentsById.Keys)
89 ComponentsById[id].TryGetTarget(out component);
90 if (component is not
null) action(component);
94 public static IEnumerable<Type> GetClassHierarchy(Type type)
96 while (type != typeof(Object) && type !=
null)
103 public static IEnumerable<Type> GetReverseClassHierarchy(Type type)
104 => CUIComponent.GetClassHierarchy(type).Reverse<Type>();
107 #region Virtual --------------------------------------------------------
114 internal virtual CUIBoundaries ChildOffsetBounds =>
new CUIBoundaries();
119 internal virtual void UpdatePseudoChildren()
121 LeftResizeHandle.Update();
122 RightResizeHandle.Update();
130 internal virtual Vector2 AmIOkWithThisSize(Vector2 size) => size;
135 public virtual partial
void Draw(SpriteBatch spriteBatch);
140 public virtual partial
void DrawFront(SpriteBatch spriteBatch);
143 #region Draw --------------------------------------------------------
145 public virtual partial
void Draw(SpriteBatch spriteBatch)
149 CUI.DrawBorders(spriteBatch,
this);
152 if (OutlineVisible) GUI.DrawRectangle(spriteBatch, OutlineBox.Position, OutlineBox.Size,
OutlineColor, thickness: OutlineThickness);
155 public virtual partial
void DrawFront(SpriteBatch spriteBatch)
157 LeftResizeHandle.Draw(spriteBatch);
158 RightResizeHandle.Draw(spriteBatch);
168 #region Constructors --------------------------------------------------------
171 internal void Vitalize()
173 foreach (FieldInfo fi
in this.GetType().GetFields(AccessTools.all))
175 if (fi.FieldType.IsAssignableTo(typeof(ICUIVitalizable)))
177 ICUIVitalizable prop = (ICUIVitalizable)fi.GetValue(
this);
178 if (prop ==
null)
continue;
183 internal void VitalizeProps()
185 foreach (FieldInfo fi
in this.GetType().GetFields(AccessTools.all))
187 if (fi.FieldType.IsAssignableTo(typeof(ICUIProp)))
189 ICUIProp prop = (ICUIProp)fi.GetValue(
this);
190 if (prop ==
null)
continue;
192 prop.SetName(fi.Name);
196 foreach (FieldInfo fi
in typeof(CUIComponentProps).GetFields(AccessTools.all))
198 if (fi.FieldType.IsAssignableTo(typeof(ICUIProp)))
200 ICUIProp prop = (ICUIProp)fi.GetValue(
CUIProps);
201 if (prop ==
null)
continue;
203 prop.SetName(fi.Name);
208 public CUIComponent()
218 ComponentsById[
ID] =
new WeakReference<CUIComponent>(
this);
219 ComponentsByType.Add(this.GetType(),
this);
226 Layout =
new CUILayoutSimple();
232 public CUIComponent(
float? x =
null,
float? y =
null,
float? w =
null,
float? h =
null) : this()
234 Relative =
new CUINullRect(x, y, w, h);
237 public bool Disposed;
238 public void Dispose()
240 if (Disposed)
return;
244 public virtual void CleanUp() { }
246 ~CUIComponent() => Dispose();
248 public override string ToString() => $
"{this.GetType().Name}:{ID}:{AKA}";
Base class for all components.
CUINullRect Relative
Relative to parent size and position, [0..1].
virtual partial void DrawFront(SpriteBatch spriteBatch)
Method for drawing something that should always be on top, e.g. resize handles.
CUIComponentProps CUIProps
Just a wrapper for CUIProps idk how to separate them better.
bool BackgroundVisible
BackgroundColor != Color.Transparent.
Color BackgroundColor
Color of BackgroundSprite, default is black If you're using custom sprite and don't see it make sur...
CUIRect Real
Calculated prop, position on real screen in pixels Should be fully calculated after CUIMainComponent....
virtual partial void Draw(SpriteBatch spriteBatch)
Here component should be drawn.
int ID
Global ID, unique for component.
static GUIButton dummyComponent
This is used to trick vanilla GUI into believing that mouse is hovering some component and block clic...
Color OutlineColor
Outline is like a border, but on the outside of the component.
static void RunRecursiveOn(CUIComponent component, Action< CUIComponent > action)
designed to be versatile, in fact never used
CUISprite BackgroundSprite
Will be drawn in background with BackgroundColor Default is solid white 1x1 texture.
In fact a static class managing static things.
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.