2using System.Collections.Generic;
6using Microsoft.Xna.Framework;
7using Microsoft.Xna.Framework.Input;
8using Microsoft.Xna.Framework.Graphics;
20 public CUIMenuText(
string text) :
this() => Text = text;
26 TextColor = Color.Black;
36 public GUISoundType ClickSound {
get;
set; } = GUISoundType.Select;
40 [CUISerializable]
public string Value {
get;
set; }
48 get => (Color)Animations[
"hover"].StartValue;
51 Animations[
"hover"].StartValue = value;
52 Animations[
"hover"].ApplyValue();
62 get => (Color)Animations[
"hover"].EndValue;
63 set => Animations[
"hover"].EndValue = value;
73 Command =
"CUIMenuOption select";
76 SoundPlayer.PlayUISound(ClickSound);
80 Animations[
"hover"] =
new CUIAnimation()
82 StartValue =
new Color(255, 255, 255, 255),
83 EndValue =
new Color(255, 255, 255, 255),
85 ReverseDuration = 0.3,
86 Property =
"BackgroundColor",
89 OnMouseEnter += (e) => Animations[
"hover"].Forward();
90 OnMouseLeave += (e) => Animations[
"hover"].Back();
94 public class CUIMenu : CUIComponent, IKeyboardSubscriber
99 public void ReceiveSpecialInput(Keys key) {
if (key == Keys.Escape) Close(); }
100 public void ReceiveTextInput(
char inputChar) => ReceiveTextInput(inputChar.ToString());
101 public void ReceiveTextInput(
string text) { }
102 public void ReceiveCommandInput(
char command) { }
103 public void ReceiveEditingInput(
string text,
int start,
int length) { }
104 public bool Selected {
get;
set; }
107 public static void InitStatic() => CUI.OnDispose += () => Menus.Clear();
111 public static Dictionary<string, CUIMenu> Menus =
new();
118 public double FadeInDuration
120 get => Animations[
"fade"].Duration;
121 set => Animations[
"fade"].Duration = value;
127 [CUISerializable]
public string Name {
get;
set; }
131 [CUISerializable]
public string Author {
get;
set; }
136 [CUISerializable]
public bool BlockInput {
get;
set; }
140 public event Action<string> OnSelect;
144 public void Open(CUIComponent host =
null)
146 if (Parent !=
null)
return;
150 if (BlockInput) CUI.FocusedComponent =
this;
152 Animations[
"fade"].SetToStart();
153 Animations[
"fade"].Forward();
156 public void Close() => RemoveSelf();
158 public void Toggle(CUIComponent host =
null)
160 if (Parent !=
null) Close();
167 public static CUIMenu Load(
string path)
169 CUIMenu menu = CUIComponent.LoadFromFile<CUIMenu>(path);
170 if (menu ==
null) CUI.Warning($
"Couldn't load CUIMenu from {path}");
171 if (menu?.Name !=
null) Menus[menu.Name] = menu;
175 public CUIMenu() : base()
178 Anchor = CUIAnchor.Center;
183 if (o is
string s) OnSelect?.Invoke(s);
187 Animations[
"fade"] =
new CUIAnimation()
192 Property =
"Transparency",
195 if (CUI.Main !=
null)
197 CUI.Main.Global.OnKeyDown += (e) =>
199 if (e.PressedKeys.Contains(Keys.Escape)) Close();
202 CUI.Main.OnMouseDown += (e) => Close();
CUIAnchor is just a Vector2 This is a static class containing some helper methods.
Base class for all components.
CUINullRect Relative
Relative to parent size and position, [0..1].
string Command
This command will be dispatched up when some component specific event happens.
void AddCommand(string name, Action< object > action)
Manually adds command.
Color BackgroundColor
Color of BackgroundSprite, default is black If you're using custom sprite and don't see it make sur...
Vector2 Anchor
this point of this component
bool IgnoreTransparent
If true, mouse events on transparent pixels will be ignored Note: this will buffer texture data and...
void DispatchUp(CUICommand command)
Dispathes command up the component tree until someone consumes it.
int? ZIndex
Components are drawn in order of their ZIndex Normally it's derived from component position in the ...
record CUICommand(string Name, object Data=null)
Can be dispatched up the component tree to notify parent about something add pass some event data wit...