Skip to content
This repository was archived by the owner on Jul 27, 2022. It is now read-only.

Commit 399e3bd

Browse files
committed
use more generic system with events and adapters
1 parent c4f05f4 commit 399e3bd

13 files changed

+139
-100
lines changed

AbstractUI.cs

-18
This file was deleted.

Elements/BaseElement.cs

-7
This file was deleted.

Elements/BoundElement.cs

-23
This file was deleted.

Elements/InvokeElement.cs

-13
This file was deleted.

SideUI/Adapter.cs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace KiraiMod.Core.UI.SideUI
2+
{
3+
public static class Adapter
4+
{
5+
private static bool hasInitialized;
6+
7+
public static void Initialize()
8+
{
9+
if (hasInitialized) return;
10+
hasInitialized = true;
11+
12+
UIManager.HighGroupAdded += HandleGroup;
13+
UIManager.HighGroups.ForEach(HandleGroup);
14+
}
15+
16+
private static void HandleGroup(UIGroup group)
17+
{
18+
SideUI ui = new(group.name);
19+
SideUI.GlobalContext.AddElement(new UIElement<SideUI>(group.name, ui));
20+
group.ElementAdded += element => ui.AddElement(element);
21+
ui.elements.ForEach(element => ui.AddElement(element));
22+
}
23+
}
24+
}

SideUI/BaseWrapper.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using KiraiMod.Core.UI.Elements;
2-
using UnityEngine.UI;
1+
using UnityEngine.UI;
32

43
namespace KiraiMod.Core.UI.SideUI
54
{
65
public class BaseWrapper
76
{
8-
public BaseWrapper(Image Background, BaseElement Element, Text Text)
7+
public BaseWrapper(Image Background, UIElement Element, Text Text)
98
{
109
this.Background = Background;
1110
this.Element = Element;
@@ -14,7 +13,7 @@ public BaseWrapper(Image Background, BaseElement Element, Text Text)
1413

1514
public Image Background;
1615
public Text Text;
17-
public BaseElement Element;
16+
public UIElement Element;
1817

1918
public virtual void OnLeft() => Controller.OnBack();
2019

SideUI/Controller.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using KiraiMod.Core.UI.Elements;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using UnityEngine;
43
using UnityEngine.InputSystem;
54

SideUI/SideUI.cs

+14-23
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using KiraiMod.Core.UI.Elements;
2-
using KiraiMod.Core.UI.SideUI.Wrappers;
1+
using KiraiMod.Core.UI.SideUI.Wrappers;
32
using System;
43
using System.Collections.Generic;
54
using UnityEngine;
65
using UnityEngine.UI;
76

87
namespace KiraiMod.Core.UI.SideUI
98
{
10-
public class SideUI : AbstractUI
9+
public class SideUI : UIGroup
1110
{
1211
public static Lazy<SideUI> _globalContext = new(() =>
1312
{
@@ -27,10 +26,10 @@ public static SideUI GlobalContext
2726
public GameObject Sidebar;
2827
public RectTransform rect;
2928

30-
public SideUI(string ID) : base(ID, null) { }
31-
private SideUI() : base("global.SideUI", "SideUI")
29+
public SideUI(string name) : base(name) { }
30+
private SideUI() : base("SideUI")
3231
{
33-
Sidebar = ElementFactory.CreateSide(Name, UIManager.ScreenUI.transform.parent);
32+
Sidebar = ElementFactory.CreateSide(name, UIManager.ScreenUI.transform.parent);
3433

3534
rect = Sidebar.transform.Cast<RectTransform>();
3635
rect.sizeDelta = new(200, 0);
@@ -45,30 +44,30 @@ public void IncreaseSize()
4544
rect.position = rect.position.AddY(-12.5f);
4645
}
4746

48-
protected override BaseElement AddElement(BaseElement element)
47+
public override UIElement AddElement(UIElement element)
4948
{
5049
Image image;
5150
Text text;
5251
BaseWrapper container;
5352

5453
switch (element)
5554
{
56-
case BoundElement<bool> elemBool:
57-
(image, text) = ElementFactory.CreateElement(Sidebar, elemBool.Name);
55+
case UIElement<bool> elemBool:
56+
(image, text) = ElementFactory.CreateElement(Sidebar, elemBool.name);
5857
container = new ToggleWrapper(image, elemBool, text);
5958
break;
6059

61-
case BoundElement<SideUI> elemMenu:
60+
case UIElement<SideUI> elemMenu:
6261
if (elemMenu.Bound._value is null) return null;
6362

64-
(image, text) = ElementFactory.CreateElement(Sidebar, $"< {elemMenu.Name} >");
63+
(image, text) = ElementFactory.CreateElement(Sidebar, $"< {elemMenu.name} >");
6564

6665
SideUI ui = elemMenu.Bound._value;
67-
ui.Sidebar = ElementFactory.CreateSide(elemMenu.Name, Sidebar.transform);
66+
ui.Sidebar = ElementFactory.CreateSide(elemMenu.name, Sidebar.transform);
6867
ui.Parent = this;
6968
ui.rect = ui.Sidebar.transform.Cast<RectTransform>();
7069
ui.rect.sizeDelta = new(200, 0);
71-
ui.rect.localPosition = new(201, 87.5f);
70+
ui.rect.localPosition = new(201, 12.5f);
7271
ui.Sidebar.active = false;
7372

7473
container = new SideUIWrapper(image, elemMenu, text);
@@ -80,21 +79,13 @@ protected override BaseElement AddElement(BaseElement element)
8079

8180
IncreaseSize();
8281

82+
base.AddElement(element);
83+
8384
Wrappers.Add(container);
8485
if (Wrappers.Count == 1)
8586
Controller.Redraw();
8687

8788
return element;
8889
}
89-
90-
public override bool RemoveElement(BaseElement element)
91-
{
92-
throw new NotImplementedException();
93-
}
94-
95-
public override bool RemoveElement(string element)
96-
{
97-
throw new NotImplementedException();
98-
}
9990
}
10091
}

SideUI/Wrappers/SideUIWrapper.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
using KiraiMod.Core.UI.Elements;
2-
using UnityEngine.UI;
1+
using UnityEngine.UI;
32

43
namespace KiraiMod.Core.UI.SideUI.Wrappers
54
{
65
public class SideUIWrapper : BaseWrapper
76
{
8-
private static BoundElement<SideUI> _element;
7+
private static UIElement<SideUI> _element;
98

10-
public SideUIWrapper(Image Background, BoundElement<SideUI> Element, Text Text) : base(Background, Element, Text) => _element = Element;
9+
public SideUIWrapper(Image Background, UIElement<SideUI> Element, Text Text) : base(Background, Element, Text) => _element = Element;
1110

1211
public override void OnRight()
1312
{

SideUI/Wrappers/ToggleWrapper.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using KiraiMod.Core.UI.Elements;
2-
using UnityEngine;
1+
using UnityEngine;
32
using UnityEngine.UI;
43

54
namespace KiraiMod.Core.UI.SideUI.Wrappers
@@ -9,9 +8,9 @@ public class ToggleWrapper : BaseWrapper
98
public static Color on = new Color32(0xCC, 0xCC, 0xFF, 0xFF);
109
public static Color off = new Color32(0x56, 0x00, 0xA5, 0xFF);
1110

12-
private readonly BoundElement<bool> boolElem;
11+
private readonly UIElement<bool> boolElem;
1312

14-
public ToggleWrapper(Image Background, BoundElement<bool> Element, Text text) : base(Background, Element, text)
13+
public ToggleWrapper(Image Background, UIElement<bool> Element, Text text) : base(Background, Element, text)
1514
{
1615
boolElem = Element;
1716
boolElem.Bound.ValueChanged += value => text.color = value ? on : off;

UIElement.cs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using KiraiMod.Core.Utils;
2+
using System;
3+
4+
namespace KiraiMod.Core.UI
5+
{
6+
public class UIElement
7+
{
8+
public string name;
9+
10+
public UIElement(string name) => this.name = name;
11+
12+
public event Action Changed;
13+
14+
public void Invoke() => Changed?.Invoke();
15+
}
16+
17+
public class UIElement<T> : UIElement
18+
{
19+
public readonly Bound<T> Bound;
20+
21+
public UIElement(string name, T value) : this(name) => Bound._value = value;
22+
public UIElement(string name) : base(name)
23+
{
24+
Bound = new();
25+
Bound.ValueChanged += val => Changed?.Invoke(val);
26+
}
27+
28+
public UIElement(string name, Bound<T> bound) : base(name)
29+
{
30+
this.name = name;
31+
Bound = bound;
32+
Bound.ValueChanged += val => Changed?.Invoke(val);
33+
}
34+
35+
public new event Action<T> Changed;
36+
37+
[Obsolete("Change the inner Value of Bound")]
38+
public void Invoke(T val) => Changed?.Invoke(val);
39+
}
40+
}

UIGroup.cs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using KiraiMod.Core.Utils;
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace KiraiMod.Core.UI
6+
{
7+
public class UIGroup
8+
{
9+
public readonly List<UIElement> elements = new();
10+
public event Action<UIElement> ElementAdded;
11+
public event Action<UIElement> ElementRemoved;
12+
13+
public readonly string name;
14+
15+
public UIGroup(string name) => this.name = name;
16+
17+
/// <summary> This will register your group as a top level group </summary>
18+
public virtual void RegisterAsHighest() => UIManager.RegisterHighGroup(this);
19+
20+
// these overloads are for convenience and are not intended to be overloaded
21+
public UIElement<T> AddElement<T>(string name, T value) => (UIElement<T>)AddElement(new UIElement<T>(name, value));
22+
public UIElement<T> AddElement<T>(string name) => (UIElement<T>)AddElement(new UIElement<T>(name));
23+
public UIElement<T> AddElement<T>(string name, Bound<T> bound) => (UIElement<T>)AddElement(new UIElement<T>(name, bound));
24+
25+
/// <remarks> Some implementations may require that the current group is already registered </remarks>
26+
public virtual UIElement AddElement(UIElement element)
27+
{
28+
elements.Add(element);
29+
ElementAdded?.Invoke(element);
30+
return element;
31+
}
32+
33+
/// <returns> If the element was successfully removed </returns>
34+
public virtual bool RemoveElement(UIElement element)
35+
{
36+
elements.Remove(element);
37+
ElementRemoved?.Invoke(element);
38+
return true;
39+
}
40+
}
41+
}

UIManager.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace KiraiMod.Core.UI
45
{
56
public static class UIManager
67
{
7-
public static Dictionary<string, AbstractUI> UIs = new();
8+
public static readonly List<UIGroup> HighGroups = new();
9+
public static event Action<UIGroup> HighGroupAdded;
10+
11+
public static void RegisterHighGroup(UIGroup group)
12+
{
13+
HighGroups.Add(group);
14+
HighGroupAdded?.Invoke(group);
15+
}
816

917
public static UnityEngine.GameObject ScreenUI
1018
{

0 commit comments

Comments
 (0)