CrabUI
Loading...
Searching...
No Matches
CUIComponentSelector.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6using Barotrauma;
7using Microsoft.Xna.Framework;
8using Microsoft.Xna.Framework.Input;
9using Microsoft.Xna.Framework.Graphics;
10using System.Diagnostics;
11
12namespace CrabUI
13{
14 public class CUIComponentSelector : CUIVerticalList, IRefreshable
15 {
16 public class Button : CUIToggleButton
17 {
18 public CUIComponent Target;
19 public Button(CUIComponent target) : base()
20 {
21 Target = target;
22 Text = target.ToString();
23 TextAlign = CUIAnchor.CenterLeft;
24 ToggleOnClick = false;
25 IgnoreDebug = true;
26 OnMouseDown += (e) => DispatchUp(new CUICommand("target selected", Target));
27 OnMouseEnter += (e) => { if (Target != null) Target.DebugHighlight = true; };
28 OnMouseLeave += (e) => { if (Target != null) Target.DebugHighlight = false; };
29 }
30 }
31
32 private CUIComponent selected;
33 public CUIComponent Selected
34 {
35 get => selected;
36 set
37 {
38 selected = value;
39 OnSelect?.Invoke(value);
40 }
41 }
42 public event Action<CUIComponent> OnSelect;
43
44 public void Refresh()
45 {
46 //Stopwatch sw = Stopwatch.StartNew();
47 RemoveAllChildren();
48
49 List<CUIComponent> all = CUIComponent.AllComponents.Where(c => !c.IgnoreDebug).ToList();
50 foreach (CUIComponent c in all)
51 {
52 this.Append(new Button(c)
53 {
54 State = c == Selected,
55 Palette = this.Palette, //GUH
56 });
57 }
58
59 //CUI.Log($"CUIComponentSelector Refresh took {sw.ElapsedMilliseconds} ms");
60 }
61
62 public CUIComponentSelector() : base()
63 {
64 Scrollable = true;
65 IgnoreDebug = true;
66
67 AddCommand("target selected", o =>
68 {
69 if (o is CUIComponent c)
70 {
71 Selected = c;
72 Refresh();
73 }
74 });
75
76 OnMouseDown += (e) => Refresh();
77
78 // I think this should be called in the component it embeded in
79 //Refresh();
80 }
81 }
82}
PaletteOrder Palette
This palette will be used to resolve palette styles Primary, Secondary, Tertiary,...
virtual CUIComponent Append(CUIComponent child, string name=null, [CallerMemberName] string memberName="")
Adds children to the end of the list.
void AddCommand(string name, Action< object > action)
Manually adds command.
void DispatchUp(CUICommand command)
Dispathes command up the component tree until someone consumes it.
Vector2 TextAlign
A Vector2 ([0..1],[0..1])
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...