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

Commit c4f05f4

Browse files
committed
abstract and side ui
0 parents  commit c4f05f4

13 files changed

+419
-0
lines changed

AbstractUI.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace KiraiMod.Core.UI
2+
{
3+
public abstract class AbstractUI
4+
{
5+
public AbstractUI(string ID) => UIManager.UIs[this.ID = ID] = this;
6+
public AbstractUI(string ID, string Name) : this(ID) => this.Name = Name;
7+
8+
public readonly string ID;
9+
public readonly string Name;
10+
11+
public Elements.InvokeElement AddElement(Elements.InvokeElement element) => (Elements.InvokeElement)AddElement((Elements.BaseElement)element);
12+
public Elements.BoundElement<T> AddElement<T>(Elements.BoundElement<T> element) => (Elements.BoundElement<T>)AddElement((Elements.BaseElement)element);
13+
protected abstract Elements.BaseElement AddElement(Elements.BaseElement element);
14+
15+
public abstract bool RemoveElement(Elements.BaseElement element);
16+
public abstract bool RemoveElement(string element);
17+
}
18+
}

Elements/BaseElement.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace KiraiMod.Core.UI.Elements
2+
{
3+
public class BaseElement
4+
{
5+
public string Name;
6+
}
7+
}

Elements/BoundElement.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using KiraiMod.Core.Utils;
2+
3+
namespace KiraiMod.Core.UI.Elements
4+
{
5+
public class BoundElement<T> : BaseElement
6+
{
7+
public BoundElement(string Name)
8+
{
9+
this.Name = Name;
10+
Bound = new();
11+
}
12+
13+
public BoundElement(string Name, T value) : this(Name) => Bound._value = value;
14+
15+
public BoundElement(string Name, Bound<T> bound)
16+
{
17+
this.Name = Name;
18+
Bound = bound;
19+
}
20+
21+
public Bound<T> Bound;
22+
}
23+
}

Elements/InvokeElement.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace KiraiMod.Core.UI.Elements
4+
{
5+
public class InvokeElement : BaseElement
6+
{
7+
public InvokeElement(string Name) => this.Name = Name;
8+
9+
public event Action OnClick;
10+
11+
public void Click() => OnClick();
12+
}
13+
}

KiraiMod.Core.UI.csproj

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
<LangVersion>preview</LangVersion>
6+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
7+
</PropertyGroup>
8+
9+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
10+
<NoWarn>1701;1702;1591</NoWarn>
11+
</PropertyGroup>
12+
13+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
14+
<NoWarn>1701;1702;1591</NoWarn>
15+
</PropertyGroup>
16+
17+
<ItemGroup>
18+
<PackageReference Include="BepInEx.IL2CPP" Version="6.0.0-*" />
19+
<PackageReference Include="BepInEx.IL2CPP.MSBuild" Version="*" PrivateAssets="all" />
20+
<PackageReference Include="VRC.GameLibs" Version="*-*" PrivateAssets="all" />
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\KiraiMod.Core\KiraiMod.Core.csproj" />
25+
<ProjectReference Include="..\KiraiMod\KiraiMod.csproj" />
26+
</ItemGroup>
27+
28+
</Project>

Plugin.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using BepInEx;
2+
using BepInEx.IL2CPP;
3+
4+
namespace KiraiMod.Core.UI
5+
{
6+
[BepInPlugin(GUID, "KM.Core.GUI", "0.1.0")]
7+
public class Plugin : BasePlugin
8+
{
9+
const string GUID = "me.kiraihooks.KiraiMod.Core.UI";
10+
11+
public override void Load() { }
12+
}
13+
}

SideUI/BaseWrapper.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using KiraiMod.Core.UI.Elements;
2+
using UnityEngine.UI;
3+
4+
namespace KiraiMod.Core.UI.SideUI
5+
{
6+
public class BaseWrapper
7+
{
8+
public BaseWrapper(Image Background, BaseElement Element, Text Text)
9+
{
10+
this.Background = Background;
11+
this.Element = Element;
12+
this.Text = Text;
13+
}
14+
15+
public Image Background;
16+
public Text Text;
17+
public BaseElement Element;
18+
19+
public virtual void OnLeft() => Controller.OnBack();
20+
21+
public virtual void OnRight() { }
22+
}
23+
}

SideUI/Controller.cs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using KiraiMod.Core.UI.Elements;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.InputSystem;
5+
6+
namespace KiraiMod.Core.UI.SideUI
7+
{
8+
internal static class Controller
9+
{
10+
private static readonly Color highlight = new Color32(0x56, 0x00, 0xA5, 40);
11+
12+
public static SideUI current;
13+
public static int index = 0;
14+
public static List<int> prevIndexes = new();
15+
public static bool locked = false;
16+
public static BaseWrapper previous;
17+
18+
public static void Initialize(SideUI ui)
19+
{
20+
current = ui;
21+
22+
Managers.KeybindManager.binds.Add("sideui.controller.up", new() { keys = new[] { Key.UpArrow }, OnClick = OnUp });
23+
Managers.KeybindManager.binds.Add("sideui.controller.down", new() { keys = new[] { Key.DownArrow }, OnClick = OnDown });
24+
Managers.KeybindManager.binds.Add("sideui.controller.left", new() { keys = new[] { Key.LeftArrow }, OnClick = OnLeft });
25+
Managers.KeybindManager.binds.Add("sideui.controller.right", new() { keys = new[] { Key.RightArrow }, OnClick = OnRight });
26+
Managers.KeybindManager.binds.Add("sideui.controller.back", new() { keys = new[] { Key.Backspace }, OnClick = OnBack });
27+
}
28+
29+
public static void Redraw()
30+
{
31+
if (previous != null)
32+
previous.Background.color = Color.black;
33+
(previous = current.Wrappers[index]).Background.color = highlight;
34+
}
35+
36+
private static void OnUp()
37+
{
38+
index--;
39+
if (index < 0)
40+
index = 0;
41+
Redraw();
42+
}
43+
44+
private static void OnDown()
45+
{
46+
index++;
47+
if (index >= current.Wrappers.Count)
48+
index = current.Wrappers.Count - 1;
49+
Redraw();
50+
}
51+
52+
private static void OnLeft()
53+
{
54+
current.Wrappers[index].OnLeft();
55+
}
56+
57+
private static void OnRight()
58+
{
59+
current.Wrappers[index].OnRight();
60+
}
61+
62+
public static void OnBack()
63+
{
64+
if (current.Parent != null)
65+
{
66+
current.Sidebar.active = false;
67+
current = current.Parent;
68+
index = prevIndexes.Pop();
69+
Redraw();
70+
}
71+
}
72+
}
73+
}

SideUI/ElementFactory.cs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using UnityEngine;
2+
using UnityEngine.UI;
3+
4+
namespace KiraiMod.Core.UI.SideUI
5+
{
6+
internal static class ElementFactory
7+
{
8+
public static GameObject CreateSide(string Name, Transform parent)
9+
{
10+
GameObject side = new(Name);
11+
side.transform.SetParent(parent);
12+
side.AddComponent<Image>().color = Color.black;
13+
side.AddComponent<Outline>().effectColor = new Color32(0x56, 0x00, 0xA5, 0xFF);
14+
side.AddComponent<VerticalLayoutGroup>();
15+
side.AddComponent<LayoutElement>().ignoreLayout = true;
16+
17+
return side;
18+
}
19+
20+
public static (Image, Text) CreateElement(GameObject Sidebar, string name)
21+
{
22+
GameObject parent = new();
23+
24+
Image image = parent.AddComponent<Image>();
25+
image.color = Color.black;
26+
27+
parent.transform.SetParent(Sidebar.transform);
28+
parent.transform.Cast<RectTransform>().sizeDelta = new(200, 25);
29+
30+
GameObject content = new();
31+
content.transform.SetParent(parent.transform);
32+
33+
Text text = CreateText(content);
34+
text.text = name;
35+
36+
return (image, text);
37+
}
38+
39+
public static Text CreateText(GameObject go)
40+
{
41+
Text text = go.AddComponent<Text>();
42+
text.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
43+
text.resizeTextForBestFit = true;
44+
text.alignByGeometry = true;
45+
text.alignment = TextAnchor.MiddleCenter;
46+
47+
go.transform.Cast<RectTransform>().sizeDelta = new(200, 25);
48+
49+
return text;
50+
}
51+
}
52+
}

SideUI/SideUI.cs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using KiraiMod.Core.UI.Elements;
2+
using KiraiMod.Core.UI.SideUI.Wrappers;
3+
using System;
4+
using System.Collections.Generic;
5+
using UnityEngine;
6+
using UnityEngine.UI;
7+
8+
namespace KiraiMod.Core.UI.SideUI
9+
{
10+
public class SideUI : AbstractUI
11+
{
12+
public static Lazy<SideUI> _globalContext = new(() =>
13+
{
14+
SideUI ui = new();
15+
Controller.Initialize(ui);
16+
return ui;
17+
});
18+
19+
public static SideUI GlobalContext
20+
{
21+
get => _globalContext.Value;
22+
}
23+
24+
public List<BaseWrapper> Wrappers = new();
25+
26+
public SideUI Parent;
27+
public GameObject Sidebar;
28+
public RectTransform rect;
29+
30+
public SideUI(string ID) : base(ID, null) { }
31+
private SideUI() : base("global.SideUI", "SideUI")
32+
{
33+
Sidebar = ElementFactory.CreateSide(Name, UIManager.ScreenUI.transform.parent);
34+
35+
rect = Sidebar.transform.Cast<RectTransform>();
36+
rect.sizeDelta = new(200, 0);
37+
rect.anchorMin = new(0, 1);
38+
rect.anchorMax = new(0, 1);
39+
rect.position = new(101, 1337.5f);
40+
}
41+
42+
public void IncreaseSize()
43+
{
44+
rect.sizeDelta = rect.sizeDelta.AddY(25);
45+
rect.position = rect.position.AddY(-12.5f);
46+
}
47+
48+
protected override BaseElement AddElement(BaseElement element)
49+
{
50+
Image image;
51+
Text text;
52+
BaseWrapper container;
53+
54+
switch (element)
55+
{
56+
case BoundElement<bool> elemBool:
57+
(image, text) = ElementFactory.CreateElement(Sidebar, elemBool.Name);
58+
container = new ToggleWrapper(image, elemBool, text);
59+
break;
60+
61+
case BoundElement<SideUI> elemMenu:
62+
if (elemMenu.Bound._value is null) return null;
63+
64+
(image, text) = ElementFactory.CreateElement(Sidebar, $"< {elemMenu.Name} >");
65+
66+
SideUI ui = elemMenu.Bound._value;
67+
ui.Sidebar = ElementFactory.CreateSide(elemMenu.Name, Sidebar.transform);
68+
ui.Parent = this;
69+
ui.rect = ui.Sidebar.transform.Cast<RectTransform>();
70+
ui.rect.sizeDelta = new(200, 0);
71+
ui.rect.localPosition = new(201, 87.5f);
72+
ui.Sidebar.active = false;
73+
74+
container = new SideUIWrapper(image, elemMenu, text);
75+
break;
76+
77+
default:
78+
return null;
79+
}
80+
81+
IncreaseSize();
82+
83+
Wrappers.Add(container);
84+
if (Wrappers.Count == 1)
85+
Controller.Redraw();
86+
87+
return element;
88+
}
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+
}
99+
}
100+
}

SideUI/Wrappers/SideUIWrapper.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using KiraiMod.Core.UI.Elements;
2+
using UnityEngine.UI;
3+
4+
namespace KiraiMod.Core.UI.SideUI.Wrappers
5+
{
6+
public class SideUIWrapper : BaseWrapper
7+
{
8+
private static BoundElement<SideUI> _element;
9+
10+
public SideUIWrapper(Image Background, BoundElement<SideUI> Element, Text Text) : base(Background, Element, Text) => _element = Element;
11+
12+
public override void OnRight()
13+
{
14+
Controller.current = _element.Bound._value;
15+
Controller.current.Sidebar.active = true;
16+
Controller.prevIndexes.Add(Controller.index);
17+
Controller.index = 0;
18+
Controller.Redraw();
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)