So here is what i came up with.
Edit improved tested no garbage.
Pattern changes here as follows…
Each thing registers once.
Everything registers to the single method.
Each thing can turn its notifications on or off thru the disable enable calls.
However 2 problems 1) as stated the delegates have to be wrapped so i have to make one of these classes for each delegate type (that sucks but fine) unless i wanted to try to make the whole thing generic which just nooo. It could be optimized better as well don’t care for the moment.
Edit 3
Changed this to make improvements.
// ....
// ....
//
//TestClass t;
//public Game1() : base()
//{
// //,,,
// t = new TestClass();
//}
//protected override void Initialize()
//{
// ActionTextInputPoolingList.Initialize(Window);
// base.Initialize();
//}
//protected override void Update(GameTime gameTime)
//{
// if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))Exit();
//
// t.Update(gameTime);
// base.Update(gameTime);
//}
//protected override void Draw(GameTime gameTime)
//{
// GraphicsDevice.Clear(Color.CornflowerBlue);
// spriteBatch.Begin();
// spriteBatch.DrawString(spriteFont, test.sbMsg, new Vector2(100, 100), Color.Wheat);
// test.Draw(spriteBatch, spriteFont, test.sbMsg, new Vector2(200, 100), Color.Wheat, gameTime);
// spriteBatch.End();
// base.Draw(gameTime);
//}
public class TestClass
{
public StringBuilder sbMsg = new StringBuilder(64);
public TestClass()
{
ActionTextInputPoolingList.InitialRegistration(CallBackForTest, this);
var dummyTest2 = new TestClassB();
}
public void CallBackForTest(char s, Keys k)
{
sbMsg.Clear();
sbMsg.Append(" CallBackForTest ");
sbMsg.Append(s);
}
public void Update(GameTime gameTime)
{
// over and over.
if (Keyboard.GetState().IsKeyDown(Keys.T))
{
ActionTextInputPoolingList.Disable(this);
ActionTextInputPoolingList.Enable(this);
}
if (Keyboard.GetState().IsKeyDown(Keys.D1))
ActionTextInputPoolingList.Enable(this);
if (Keyboard.GetState().IsKeyDown(Keys.D2))
ActionTextInputPoolingList.Disable(this);
if (Keyboard.GetState().IsKeyDown(Keys.R))
ActionTextInputPoolingList.InitialRegistration(CallBackForTest, this);
if (Keyboard.GetState().IsKeyDown(Keys.U))
ActionTextInputPoolingList.UnRegister(CallBackForTest, this);
}
public void Draw(SpriteBatch spriteBatch, SpriteFont spriteFont, Vector2 pos, Color color, GameTime gameTime)
{
spriteBatch.DrawString(spriteFont, sbMsg, pos, color);
}
}
// disconnected class test.
public class TestClassB
{
public StringBuilder sbMsg = new StringBuilder(64);
public TestClassB()
{
ActionTextInputPoolingList.InitialRegistration(CallBackForTestB, this);
}
public void CallBackForTestB(char s, Keys k)
{
sbMsg.Clear();
sbMsg.Append(" CallBackForTestB ");
sbMsg.Append(s);
}
public void Draw(SpriteBatch spriteBatch, SpriteFont spriteFont, Vector2 pos, Color color, GameTime gameTime)
{
spriteBatch.DrawString(spriteFont, sbMsg, pos, color);
}
}
public static class ActionTextInputPoolingList
{
static readonly object accessLock = new object();
static List<Action<char, Keys>> actionList = new List<Action<char, Keys>>();
static List<ActionListTrackingAndMapping> actionListMap = new List<ActionListTrackingAndMapping>();
public static void Initialize(GameWindow window)
{
window.TextInput += TextInputCallBack;
}
private static void TextInputCallBack(object sender, TextInputEventArgs args)
{
for (int forindex = 0; forindex < actionListMap.Count; forindex++)
{
if (actionListMap[forindex].actionIsEnabled)
actionList[forindex]?.Invoke(args.Character, args.Key);
}
}
public static void InitialRegistration<T>(Action<char, Keys> methodToCall, T inst)
{
try
{
Monitor.Enter(accessLock);
InitialRegistration(methodToCall, inst.GetHashCode());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void UnRegister<T>(Action<char, Keys> methodToCall, T inst)
{
try
{
Monitor.Enter(accessLock);
UnRegister(methodToCall, inst.GetHashCode());
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void Enable<T>(T inst)
{
EnableCallback(inst.GetHashCode());
}
public static void Disable<T>(T inst)
{
DisableCallback(inst.GetHashCode());
}
private static void InitialRegistration(Action<char, Keys> methodToCall, int id)
{
bool match = false;
int index = actionListMap.Count;
int theid = id;
bool isEnabled = false;
for (int forindex = 0; forindex < actionListMap.Count; forindex++)
{
var m = actionListMap[forindex];
if (m.actionID == id)
{
match = true;
index = forindex;
isEnabled = m.actionIsEnabled;
forindex = actionListMap.Count; // break
}
}
if (match == false)
{
actionList.Add(methodToCall);
actionListMap.Add(new ActionListTrackingAndMapping(id, index, true));
}
}
private static void UnRegister(Action<char, Keys> methodToCall, int id)
{
bool match = false;
int index = actionListMap.Count;
int theid = id;
bool isEnabled = false;
for (int forindex = 0; forindex < actionListMap.Count; forindex++)
{
var m = actionListMap[forindex];
if (m.actionID == id)
{
match = true;
index = forindex;
isEnabled = m.actionIsEnabled;
forindex = actionListMap.Count; // break
}
}
if (match == true) // its not in the list already user error throw msg.
{
actionList.Remove(methodToCall);
actionListMap.Remove(new ActionListTrackingAndMapping(id, index, true));
}
}
private static void EnableCallback(int id)
{
for (int forindex = 0; forindex < actionListMap.Count; forindex++)
{
if (actionListMap[forindex].actionID == id)
{
actionListMap[forindex] = new ActionListTrackingAndMapping(id, forindex, true);
forindex = actionListMap.Count; // break
}
}
}
private static void DisableCallback(int id)
{
for (int forindex = 0; forindex < actionListMap.Count; forindex++)
{
if (actionListMap[forindex].actionID == id)
{
actionListMap[forindex] = new ActionListTrackingAndMapping(id, forindex, false);
forindex = actionListMap.Count; // break
}
}
}
public struct ActionListTrackingAndMapping
{
public int actionID { get; set; }
public int actionIndexToListForId;
public bool actionIsEnabled;
public ActionListTrackingAndMapping(int id, int index, bool alive)
{
actionID = id;
actionIndexToListForId = index;
actionIsEnabled = alive;
}
}
}
}
ok there we go lot of work to make that work.