CrabUI
Loading...
Searching...
No Matches
CUIMenu.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4
5using Barotrauma;
6using Microsoft.Xna.Framework;
7using Microsoft.Xna.Framework.Input;
8using Microsoft.Xna.Framework.Graphics;
9using EventInput;
10using System.Windows;
11
12namespace CrabUI
13{
14 //TODO move all this to defauld styles
15 /// <summary>
16 /// CUITextBlock adapted for CUIMenu
17 /// </summary>
19 {
20 public CUIMenuText(string text) : this() => Text = text;
21 public CUIMenuText() : base()
22 {
23 Anchor = CUIAnchor.Center;
24 TextScale = 1.0f;
25 ZIndex = 100;
26 TextColor = Color.Black;
27 }
28 }
29
30
31 /// <summary>
32 /// Component with a sprite that will notify parent CUIMenu when clicked
33 /// </summary>
35 {
36 public GUISoundType ClickSound { get; set; } = GUISoundType.Select;
37 /// <summary>
38 /// This is the Value that will be send to CUIMenu on click, and will be passed to OnSelect event
39 /// </summary>
40 [CUISerializable] public string Value { get; set; }
41
42 /// <summary>
43 /// Normal background color
44 /// </summary>
45 [CUISerializable]
46 public Color BaseColor
47 {
48 get => (Color)Animations["hover"].StartValue;
49 set
50 {
51 Animations["hover"].StartValue = value;
52 Animations["hover"].ApplyValue();
53 }
54 }
55
56 /// <summary>
57 /// Background color when hovered
58 /// </summary>
59 [CUISerializable]
60 public Color HoverColor
61 {
62 get => (Color)Animations["hover"].EndValue;
63 set => Animations["hover"].EndValue = value;
64 }
65
66
67 public CUIMenuOption()
68 {
69 BackgroundColor = new Color(255, 255, 255, 255);
70 Relative = new CUINullRect(0, 0, 1, 1);
71
72 IgnoreTransparent = true;
73 Command = "CUIMenuOption select";
74 OnMouseDown += (e) =>
75 {
76 SoundPlayer.PlayUISound(ClickSound);
78 };
79
80 Animations["hover"] = new CUIAnimation()
81 {
82 StartValue = new Color(255, 255, 255, 255),
83 EndValue = new Color(255, 255, 255, 255),
84 Duration = 0.1,
85 ReverseDuration = 0.3,
86 Property = "BackgroundColor",
87 };
88
89 OnMouseEnter += (e) => Animations["hover"].Forward();
90 OnMouseLeave += (e) => Animations["hover"].Back();
91 }
92 }
93
94 public class CUIMenu : CUIComponent, IKeyboardSubscriber
95 {
96 // this allows it to intercept esc key press naturally,
97 // but it also blocks normal hotkey bindings, so idk
98 // ----------------- 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; }
105 // ----------------- IKeyboardSubscriber -----------------
106
107 public static void InitStatic() => CUI.OnDispose += () => Menus.Clear();
108 /// <summary>
109 /// All loaded menus are stored here by Name
110 /// </summary>
111 public static Dictionary<string, CUIMenu> Menus = new();
112
113
114 /// <summary>
115 /// Initial fade in animation duration, set to 0 to disable
116 /// </summary>
117 [CUISerializable]
118 public double FadeInDuration
119 {
120 get => Animations["fade"].Duration;
121 set => Animations["fade"].Duration = value;
122 }
123
124 /// <summary>
125 /// Will be used as key for this menu in CUIMenu.Menus
126 /// </summary>
127 [CUISerializable] public string Name { get; set; }
128 /// <summary>
129 /// Does nothing, just a prop so you could get author programmatically
130 /// </summary>
131 [CUISerializable] public string Author { get; set; }
132
133 /// <summary>
134 /// If true will act as IKeyboardSubscriber. Don't
135 /// </summary>
136 [CUISerializable] public bool BlockInput { get; set; }
137 /// <summary>
138 /// Happens when some CUIMenuOption is clicked, the value of that option is passed to it
139 /// </summary>
140 public event Action<string> OnSelect;
141 /// <summary>
142 /// Will add this as a child to host (CUI.Main) and start fadein animation
143 /// </summary>
144 public void Open(CUIComponent host = null)
145 {
146 if (Parent != null) return;
147 host ??= CUI.Main;
148 host.Append(this);
149
150 if (BlockInput) CUI.FocusedComponent = this;
151
152 Animations["fade"].SetToStart();
153 Animations["fade"].Forward();
154 }
155
156 public void Close() => RemoveSelf();
157
158 public void Toggle(CUIComponent host = null)
159 {
160 if (Parent != null) Close();
161 else Open(host);
162 }
163
164 /// <summary>
165 /// Loads CUIMenu from a file to CUIMenu.Menus
166 /// </summary>
167 public static CUIMenu Load(string path)
168 {
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;
172 return menu;
173 }
174
175 public CUIMenu() : base()
176 {
177 BackgroundColor = new Color(255, 255, 255, 255);
178 Anchor = CUIAnchor.Center;
179 Transparency = 0.0f;
180
181 AddCommand("CUIMenuOption select", (o) =>
182 {
183 if (o is string s) OnSelect?.Invoke(s);
184 //Close();
185 });
186
187 Animations["fade"] = new CUIAnimation()
188 {
189 StartValue = 0.0f,
190 EndValue = 1.0f,
191 Duration = 0.2,
192 Property = "Transparency",
193 };
194
195 if (CUI.Main != null)
196 {
197 CUI.Main.Global.OnKeyDown += (e) =>
198 {
199 if (e.PressedKeys.Contains(Keys.Escape)) Close();
200 };
201
202 CUI.Main.OnMouseDown += (e) => Close();
203 }
204 }
205
206 }
207}
CUIAnchor is just a Vector2 This is a static class containing some helper methods.
Definition CUIAnchor.cs:18
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 ...
Component with a sprite that will notify parent CUIMenu when clicked.
Definition CUIMenu.cs:35
Color HoverColor
Background color when hovered.
Definition CUIMenu.cs:61
string Value
This is the Value that will be send to CUIMenu on click, and will be passed to OnSelect event.
Definition CUIMenu.cs:40
Color BaseColor
Normal background color.
Definition CUIMenu.cs:47
CUITextBlock adapted for CUIMenu.
Definition CUIMenu.cs:19
Passive block of text.
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...
Rectangle with float?
Definition CUIRect.cs:104