Skip to content

Commit f462fd4

Browse files
committed
Episode 12, custom game menu
1 parent c7fa483 commit f462fd4

5 files changed

Lines changed: 154 additions & 1 deletion

File tree

ExampleMod.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using ExampleMod.UI.Custom;
99
using Il2CppAssets.Scripts.Unity;
1010
using BTD_Mod_Helper.Api.Components;
11+
using HarmonyLib;
12+
using Il2CppAssets.Scripts.Unity.UI_New.Main;
13+
using Il2CppAssets.Scripts.Models;
1114

1215
[assembly: MelonInfo(typeof(ExampleMod.ExampleMod), ModHelperData.Name, ModHelperData.Version, ModHelperData.RepoOwner)]
1316
[assembly: MelonGame("Ninja Kiwi", "BloonsTD6")]
@@ -21,6 +24,42 @@ public override void OnMatchStart()
2124
ModdedMonkeys.CreateMenu(Game.instance.model.towers.ToList());
2225
}
2326

27+
public override void OnGameModelLoaded(GameModel model)
28+
{
29+
foreach (var bloon in model.bloons)
30+
{
31+
if (bloon.baseId == bloon.name)
32+
{
33+
ExampleGameMenu.baseModels.Add(bloon);
34+
}
35+
else
36+
{
37+
if (!ExampleGameMenu.bloonVarients.TryAdd(bloon.baseId, [bloon]))
38+
{
39+
ExampleGameMenu.bloonVarients[bloon.baseId].Add(bloon);
40+
}
41+
}
42+
}
43+
}
44+
45+
[HarmonyPatch(typeof(MainMenu), nameof(MainMenu.Open))]
46+
static class MainMenu_Open
47+
{
48+
public static void Postfix(MainMenu __instance)
49+
{
50+
ExampleGameMenuButton.CreateButton(__instance);
51+
}
52+
}
53+
54+
[HarmonyPatch(typeof(MainMenu), nameof(MainMenu.ReOpen))]
55+
static class MainMenu_ReOpen
56+
{
57+
public static void Postfix(MainMenu __instance)
58+
{
59+
ExampleGameMenuButton.CreateButton(__instance);
60+
}
61+
}
62+
2463
public override void OnMatchEnd()
2564
{
2665
ModdedMonkeys.instance.Close();

UI/Custom/BloonMenuBtn.png

154 KB
Loading

UI/Custom/ExampleGameMenu.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using BTD_Mod_Helper.Api;
2+
using BTD_Mod_Helper.Api.Components;
3+
using BTD_Mod_Helper.Api.Enums;
4+
using BTD_Mod_Helper.Extensions;
5+
using Il2CppAssets.Scripts.Models.Bloons;
6+
using Il2CppAssets.Scripts.Unity;
7+
using Il2CppAssets.Scripts.Unity.UI_New.Settings;
8+
using Il2CppNinjaKiwi.Common;
9+
using Il2CppSystem.Runtime.InteropServices;
10+
using MelonLoader;
11+
using System;
12+
using System.Collections;
13+
using System.Collections.Generic;
14+
using System.Linq;
15+
using System.Text;
16+
using System.Threading.Tasks;
17+
using UnityEngine;
18+
using UnityEngine.UI;
19+
20+
namespace ExampleMod.UI.Custom
21+
{
22+
public class ExampleGameMenu : ModGameMenu<SettingsScreen>
23+
{
24+
ModHelperPanel mainPanel;
25+
26+
RectTransform rect;
27+
28+
internal static Dictionary<string, List<BloonModel>> bloonVarients = [];
29+
internal static List<BloonModel> baseModels = [];
30+
31+
public override bool OnMenuOpened(Il2CppSystem.Object data)
32+
{
33+
CommonForegroundHeader.SetText("Ye Olde Bloon Menu");
34+
35+
GameMenu.transform.DestroyAllChildren();
36+
37+
rect= GameMenu.transform.Cast<RectTransform>();
38+
39+
mainPanel = rect.gameObject.AddModHelperPanel(new("BloonMenu", 3200, 1800), VanillaSprites.MainBGPanelBlue);
40+
41+
MelonCoroutines.Start(CreateMenu());
42+
43+
return true;
44+
}
45+
46+
public IEnumerator CreateMenu()
47+
{
48+
var scrollPanel = mainPanel.AddScrollPanel(new("Content", 0, -50, 3000, 1700), RectTransform.Axis.Vertical, VanillaSprites.BlueInsertPanel, 50, 50);
49+
scrollPanel.ScrollContent.RemoveComponent<VerticalLayoutGroup>();
50+
yield return null;
51+
var layoutGroup = scrollPanel.ScrollContent.AddComponent<GridLayoutGroup>();
52+
layoutGroup.cellSize = new(200, 300);
53+
54+
foreach(var bloon in Game.instance.model.bloons.OrderBy(bl => bl.danger))
55+
{
56+
scrollPanel.AddScrollContent(BloonProfile(bloon));
57+
yield return null;
58+
}
59+
60+
yield return null;
61+
}
62+
63+
public ModHelperPanel BloonProfile(BloonModel bloon)
64+
{
65+
ModHelperPanel panel = ModHelperPanel.Create(new(bloon.name), VanillaSprites.MainBGPanelBlue);
66+
67+
var icon = panel.AddImage(new("Icon", 200), bloon.icon.GetGUID());
68+
69+
var name = panel.AddText(new("Name", 0, 100, 200, 100), bloon.name.GetBtd6Localization());
70+
name.EnableAutoSizing();
71+
72+
var health = panel.AddText(new("Health", 0, -100, 200, 100), bloon.maxHealth.ToString());
73+
74+
return panel;
75+
}
76+
77+
//public void CreateBloonProfile();
78+
}
79+
}

UI/Custom/ExampleGameMenuButton.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using BTD_Mod_Helper;
2+
using BTD_Mod_Helper.Api;
3+
using BTD_Mod_Helper.Api.Components;
4+
using BTD_Mod_Helper.Extensions;
5+
using Il2CppAssets.Scripts.Unity.UI_New.Main;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using UnityEngine.UI;
12+
13+
namespace ExampleMod.UI.Custom
14+
{
15+
public static class ExampleGameMenuButton
16+
{
17+
public static void CreateButton(MainMenu menu)
18+
{
19+
var buttonToCopy = menu.transform.FindChild("TrophyStore");
20+
21+
var newBtn = buttonToCopy.gameObject.Duplicate();
22+
newBtn.transform.parent = buttonToCopy.parent;
23+
newBtn.name = "ExampleButton";
24+
var matchLocalPosition = newBtn.AddComponent<MatchLocalPosition>();
25+
matchLocalPosition.transformToCopy = buttonToCopy;
26+
matchLocalPosition.offset = new(0, -340, 0);
27+
28+
newBtn.GetComponentInChildren<Image>().SetSprite(ModContent.GetSpriteReference<ExampleMod>("BloonMenuBtn"));
29+
newBtn.GetComponentInChildren<Button>().SetOnClick(new(() => ModGameMenu.Open<ExampleGameMenu>()));
30+
}
31+
}
32+
}

UI/Custom/ModdedMonkeys.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
using Il2CppAssets.Scripts.Models.Towers;
88
using Il2CppAssets.Scripts.Models.TowerSets;
99
using Il2CppAssets.Scripts.Unity.UI_New.InGame;
10+
using Il2CppInterop.Runtime.Attributes;
1011
using MelonLoader;
1112
using System;
1213
using System.Collections.Generic;
1314
using UnityEngine;
1415

1516
namespace ExampleMod.UI.Custom
1617
{
17-
[RegisterTypeInIl2Cpp]
18+
[RegisterTypeInIl2Cpp(false)]
1819
internal class ModdedMonkeys : MonoBehaviour
1920
{
2021
public static ModdedMonkeys instance;
@@ -43,6 +44,7 @@ public static void CreateMenu(List<TowerModel> towers)
4344
}));
4445
}
4546

47+
[HideFromIl2Cpp]
4648
public void OpenMenu(List<TowerModel> moddedTowers)
4749
{
4850
var towersPanel = mapRect.gameObject.AddModHelperPanel(new("ModdedMonkeysMenu", mapRect.rect.center.x, mapRect.rect.center.y, 780, 1560), VanillaSprites.MainBGPanelBlue);
@@ -61,6 +63,7 @@ public void OpenMenu(List<TowerModel> moddedTowers)
6163
}
6264
}
6365

66+
[HideFromIl2Cpp]
6467
string[] backgroundGuid(TowerSet set)
6568
{
6669
switch (set)

0 commit comments

Comments
 (0)