Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
Added custom mainmenu button for content packs (campaigns)
Browse files Browse the repository at this point in the history
  • Loading branch information
REHERC committed Jun 22, 2021
1 parent 5a846a6 commit 10e5e5d
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Distance.AdventureMaker/Distance.AdventureMaker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApplicationArguments.cs" />
<Compile Include="Harmony\Assembly-CSharp\MainMenuLogic\Start.cs" />
<Compile Include="Harmony\Assembly-CSharp\SteamworksUGC\OnEnable.cs" />
<Compile Include="Loader\CampaignLoaderLogic.cs" />
<Compile Include="Entry.cs" />
Expand All @@ -104,6 +105,8 @@
<Compile Include="Loader\Steps\CampaignListing.cs" />
<Compile Include="Loader\Steps\CampaignWorkspaceSetup.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Scripts\MainMenu\MainMenuButton.cs" />
<Compile Include="Scripts\MainMenu\CampaignsMenuButton.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Distance.AdventureMaker.Scripts.MainMenu;
using HarmonyLib;

namespace Distance.AdventureMaker.Harmony
{
[HarmonyPatch(typeof(MainMenuLogic), "Start")]
internal static class MainMenuLogic__Start
{
[HarmonyPrefix]
internal static void Prefix(MainMenuLogic __instance)
{
__instance.gameObject.AddComponent<CampaignsMenuButton>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal static class SteamworksUGC__OnEnable
[HarmonyPrefix]
internal static void Prefix(SteamworksUGC __instance)
{
Mod.Instance.Logger.Error("THIS IS A TEST");
//Mod.Instance.Logger.Error("THIS IS A TEST");
}
}
}
}
2 changes: 2 additions & 0 deletions Distance.AdventureMaker/Loader/CampaignLoaderLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public void Start()

public void Run()
{
// TODO: REMOVE
return;
foreach (LoaderTask task in loader)
{
Task.Run(task);
Expand Down
19 changes: 19 additions & 0 deletions Distance.AdventureMaker/Scripts/MainMenu/CampaignsMenuButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Centrifuge.Distance.Game;

namespace Distance.AdventureMaker.Scripts.MainMenu
{
public class CampaignsMenuButton : MainMenuButton
{
protected override string ButtonName => "Community Content Packs";

protected override string ButtonText => "CONTENT PACKS";

protected override int ItemPosition => 5;

protected override void OnButtonClick()
{
MessageBox.Create("This feature isn't currently implemented.", ButtonText).Show();
//SetVisible(false);
}
}
}
97 changes: 97 additions & 0 deletions Distance.AdventureMaker/Scripts/MainMenu/MainMenuButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using UnityEngine;

namespace Distance.AdventureMaker.Scripts.MainMenu
{
public abstract class MainMenuButton : MonoBehaviour
{
protected virtual string ButtonName => "MENU BUTTON";

protected virtual string ButtonText => "Text not set.";

protected virtual int ItemPosition => 0;

private MainMenuLogic menuLogic_;

protected MainMenuLogic MenuLogic => menuLogic_;

protected GameObject Button { get; private set; }

protected UITable Table { get; private set; }

public void Awake()
{
MainMenuLogic menuLogic = gameObject.GetComponent<MainMenuLogic>();
if (menuLogic)
{
Setup(menuLogic);
}
}

public void Setup(MainMenuLogic menuLogic)
{
// Do not create the button if it has already been created
if (menuLogic_) return;

menuLogic_ = menuLogic;

Table = FindTable();
Button = CreateButtonItem();

// Rename/reposition the button
Button.name = ButtonName;
Button.transform.SetSiblingIndex(ItemPosition);

// Set the button's label (text)
Button.GetComponentInChildren<UILabel>().text = ButtonText;


UnclickedMenuStatusIconLogic unclickedIcon = Button.GetComponentInChildren<UnclickedMenuStatusIconLogic>();
if (unclickedIcon)
{
unclickedIcon.isDisabled_ = true;
unclickedIcon.image_.enabled = true;
}

// There may be multiple UIExButton scripts, we only wanna set the
// event handler on one of them. Otherwise the click event will
// trigger as many times as there are UIExButton/UIButton scripts
// present on the object

bool handlerSet = false;
foreach (UIExButton button in Button.GetComponentsInChildren<UIExButton>())
{
button.onClick.Clear();
if (!handlerSet)
{
button.onClick.Add(new EventDelegate(OnButtonClick));
handlerSet = true;
}
}
}

private UITable FindTable()
{
return MenuLogic.mainButtons_.GetComponentInChildren<UITable>();
}

private GameObject CreateButtonItem()
{
// Get the table object (responsible for positioning the buttons vertically)
Transform buttonTable = Table.transform;

// Find one of the already existing buttons
Transform buttonTemplate = buttonTable.Find("Arcade");

// Return a copy of the previously found button
return Instantiate(buttonTemplate.gameObject, buttonTable);
}

public void SetVisible(bool value)
{
Button.SetActive(value);
Table.Reposition();
}

protected abstract void OnButtonClick();
}
}

0 comments on commit 10e5e5d

Please sign in to comment.