2using System.Collections.Generic;
4using System.Reflection;
5using System.Diagnostics;
6using System.Runtime.CompilerServices;
8using System.Collections.ObjectModel;
9using System.Collections.Specialized;
12using Microsoft.Xna.Framework;
13using Microsoft.Xna.Framework.Input;
14using Microsoft.Xna.Framework.Graphics;
22 public class CommandAttribute : System.Attribute { }
29 public record
CUICommand(
string Name,
object Data =
null);
35 public record
CUIData(
string Name,
object Data =
null);
38 private void SetupCommands()
42 OnTreeChanged += UpdateDataTargets;
49 [CUISerializable]
public string Command {
get;
set; }
65 [CUISerializable]
public string Consumes {
get;
set; }
67 private bool reflectCommands;
69 public bool ReflectCommands
71 get => reflectCommands;
74 reflectCommands = value;
85 private bool retranslateCommands;
87 public bool RetranslateCommands
89 get => retranslateCommands;
92 retranslateCommands = value;
106 public ObservableCollection<string>
Emits
112 emits.CollectionChanged += (o, e) => UpdateDataTargets();
116 private ObservableCollection<string> emits =
new();
118 private void UpdateDataTargets()
126 if (
Emits.Contains(c.Consumes))
138 public Dictionary<string, List<CUIComponent>>
DataTargets =
new();
144 public Dictionary<string, Action<object>>
Commands {
get;
set; } =
new();
152 public void RemoveCommand(
string name) =>
Commands.Remove(name);
158 private void AddCommands()
160 foreach (MethodInfo mi
in this.GetType().GetMethods())
162 if (Attribute.IsDefined(mi, typeof(CommandAttribute)))
166 string name = mi.Name;
167 if (name !=
"Command" && name.EndsWith(
"Command"))
169 name = name.Substring(0, name.Length -
"Command".Length);
171 AddCommand(name, mi.CreateDelegate<Action<object>>(
this));
175 Info($
"{e.Message}\nMethod: {this.GetType()}.{mi.Name}");
197 if (
Emits.Contains(data.Name))
224 Commands.GetValueOrDefault(command.Name)?.Invoke(command.Data);
Base class for all components.
Dictionary< string, Action< object > > Commands
All commands.
void DispatchDown(CUIData data)
Dispathes command down the component tree until someone consumes it.
string Consumes
Will consume data with this name.
Action< CUICommand > OnAnyCommand
this will be executed on any command
Dictionary< string, List< CUIComponent > > DataTargets
Consumers of emmited data, updates on tree change.
string Command
This command will be dispatched up when some component specific event happens.
void AddCommand(string name, Action< object > action)
Manually adds command.
void Execute(CUICommand command)
Will execute action corresponding to this command.
ObservableCollection< string > Emits
Optimization to data flow If not empty component will search for consumers of the data and pass it ...
void Info(object msg, [CallerFilePath] string source="", [CallerLineNumber] int lineNumber=0)
Prints component and then message.
Action< Object > OnConsume
Happens when appropriate data is received.
void DispatchUp(CUICommand command)
Dispathes command up the component tree until someone consumes it.
static void RunRecursiveOn(CUIComponent component, Action< CUIComponent > action)
designed to be versatile, in fact never used
Action< CUIData > OnAnyData
Will be executed when receiving any data.
record CUIData(string Name, object Data=null)
Can be dispatched down the component tree to pass some data to the children without creating a hard l...
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...