Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ModSourcesMenu.cs #279

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions BloonsTD6 Mod Helper/Api/Components/ModHelperText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ public void SetText(string text)
}
}

/// <summary>
/// Enables auto sizing on the text component
/// </summary>
public void EnableAutoSizing()
{
Text.enableAutoSizing = true;
}

/// <summary>
/// Enables auto sizing on the text component.
/// Changes the maximum font size.
/// </summary>
/// <param name="fontSizeMax">New max font size</param>
public void EnableAutoSizing(float fontSizeMax)
{
EnableAutoSizing();
Text.fontSizeMax = fontSizeMax;
}

/// <summary>
/// Enables auto sizing on the text component.
/// Changes the maximum and minimum font size.
/// </summary>
/// <param name="fontSizeMax">New max font size</param>
/// <param name="fontSizeMin">New min font size</param>
public void EnableAutoSizing(float fontSizeMax, float fontSizeMin)
{
EnableAutoSizing(fontSizeMax);
Text.fontSizeMin = fontSizeMin;
}

/// <summary>
/// Creates a new ModHelperText
/// </summary>
Expand Down
Binary file added BloonsTD6 Mod Helper/Resources/OpenFolderIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
171 changes: 171 additions & 0 deletions BloonsTD6 Mod Helper/UI/Menus/ModSourcesMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using BTD_Mod_Helper.Api;
using BTD_Mod_Helper.Api.Components;
using BTD_Mod_Helper.Api.Enums;
using BTD_Mod_Helper.Api.Helpers;
using Il2CppAssets.Scripts.Unity.UI_New.Popups;
using Il2CppAssets.Scripts.Unity.UI_New.Settings;
using Il2CppNinjaKiwi.Common;
using System;
using System.Collections;
using System.IO;
using System.Linq;
using Il2CppAssets.Scripts.Unity.Menu;
using UnityEngine;

namespace BTD_Mod_Helper.UI.Menus
{
internal class ModSourcesMenu : ModGameMenu<SettingsScreen>
{
internal static bool ModSourcesPresent => Directory.GetFiles(MelonMain.ModSourcesFolder).Length > 0;

private static ModHelperScrollPanel sourcesScroll;

private static readonly string FoundModSources = ModHelper.Localize(nameof(FoundModSources), "Found Mod Sources");

private static bool currentlyLoadingSources = false;

bool IsModSource(string path)
{
foreach (var file in Directory.GetFiles(path))
{
if (Path.GetExtension(file) == ".sln")
{
return true;
}
}

return false;
}

public override bool OnMenuOpened(Il2CppSystem.Object data)
{
GameMenu.gameObject.DestroyAllChildren();
CommonForegroundHeader.SetText("Mod Sources");

var panel = GameMenu.gameObject.AddModHelperPanel(new("Panel", 0, -150, 3300, 1900), VanillaSprites.MainBGPanelBlue);

panel.AddText(new("TitleText", 0, 850, 2000, 125), FoundModSources).EnableAutoSizing(150);

sourcesScroll = panel.AddScrollPanel(new("ScrollPanel", 0, -150, 3000, 1500), RectTransform.Axis.Vertical, VanillaSprites.BlueInsertPanelRound, 100, 100);

MelonCoroutines.Start(GenerateScrollContent());

CreateExtraElements(panel);

return true;
}

public IEnumerator GenerateScrollContent(string search = "")
{
if(currentlyLoadingSources)
{
PopupScreen.instance.SafelyQueue(screen => screen.ShowOkPopup("Wait until the mod sources have finished loading!"));

yield break;
}

currentlyLoadingSources = true;

sourcesScroll.ScrollContent.RectTransform.DestroyAllChildren();

int i = 0;

Check warning on line 71 in BloonsTD6 Mod Helper/UI/Menus/ModSourcesMenu.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'i' is assigned but its value is never used

Check warning on line 71 in BloonsTD6 Mod Helper/UI/Menus/ModSourcesMenu.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'i' is assigned but its value is never used

foreach (var folder in Directory.GetDirectories(MelonMain.ModSourcesFolder).OrderBy(path => path))
{
Sprite icon = null;
string name = "";

foreach(var file in Directory.GetFiles(folder))
{
if(Path.GetFileName(file) == "Icon.png")
{
Texture2D iconTexture = new(2, 2);
iconTexture.LoadFromFile(file);

icon = Sprite.Create(new Rect(0, 0, iconTexture.width, iconTexture.height), new Vector2(), 10, iconTexture);
}

if(Path.GetExtension(file) == ".sln" && name == "")
{
name = Path.GetFileNameWithoutExtension(file);

}

if (Closing) { currentlyLoadingSources = false; yield break; }
}

if (Closing) { currentlyLoadingSources = false; yield break; }

if (name != "" && name.ToLower().Contains(search)) sourcesScroll.AddScrollContent(CreateSourcePanel(name, folder, icon));

yield return null;
}

currentlyLoadingSources = false;
}

internal void CreateExtraElements(ModHelperPanel panel)
{
string searchText = "";

var searchBtn = panel.AddButton(new("SearchBtn", -1500, 700, 175), VanillaSprites.BlueBtn, new Action(() =>
{
if (!string.IsNullOrEmpty(searchText) && !string.IsNullOrWhiteSpace(searchText))
{
MelonCoroutines.Start(GenerateScrollContent(searchText));
}

MenuManager.instance.buttonClickSound.Play("ClickSounds");
}));
searchBtn.AddImage(new("Image", 125), VanillaSprites.SearchIcon);

var searchBar = panel.AddInputField(new("SearchBar", -650, 700, 1500, 125), searchText, VanillaSprites.BlueInsertPanel, new Action<string>(input =>
{
if(input != null)
{
searchText = input.ToLower();
}
}), 75, Il2CppTMPro.TMP_InputField.CharacterValidation.Alphanumeric, Il2CppTMPro.TextAlignmentOptions.Left, "", 20);

var refreshButton = panel.AddButton(new("RefreshBtn", 1400, 750, 250), VanillaSprites.RestartBtn, new Action(() =>
{
MelonCoroutines.Start(GenerateScrollContent());
MenuManager.instance.buttonClickSound.Play("ClickSounds");
}));
}

private ModHelperPanel CreateSourcePanel(string name, string path, Sprite iconSprite)
{
var panel = ModHelperPanel.Create(new("SourcePanel_" + name, 2500, 600), VanillaSprites.MainBGPanelBlue);

var nameText = panel.AddText(new("Name", 100, 100, 1500, 200), name.Localize());
nameText.EnableAutoSizing(200);

var pathText = panel.AddText(new("Path", 100, -100, 1500, 150), path);
pathText.EnableAutoSizing(150);
var openFolder = panel.AddButton(new("OpenFolderButton", 1050, 150, 250), VanillaSprites.BlueBtn, new Action(() =>
{
ProcessHelper.OpenFolder(path);
MenuManager.instance.buttonClick2Sound.Play("ClickSounds");

}));
openFolder.AddImage(new("Icon", 150), GetSprite("OpenFolderIcon"));

var openProject = panel.AddButton(new("OpenProjectBtn", 1050, -150, 250), VanillaSprites.EditBtn, new Action(
() =>
{
ProcessHelper.OpenFile(Path.Combine(path, name + ".sln"));
MenuManager.instance.buttonClick2Sound.Play("ClickSounds");
}));


// -- DOESN'T WORK-- (idk why)
/*if (iconSprite != null)
{
panel.AddImage(new("Icon", -1900, 0, 500), iconSprite);
}*/

return panel;
}
}
}
14 changes: 14 additions & 0 deletions BloonsTD6 Mod Helper/UI/Menus/ModsMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ Would you like to export this mod's current localization?
ModHelper.Localize(nameof(LocalizationGeneratedBody), "Would you like to view the generated file?");
private static readonly string LocalizationFailed = ModHelper.Localize(nameof(LocalizationFailed),
"Localization failed to generate, see console for details.");
private static readonly string ModSources = ModHelper.Localize(nameof(ModSources), "Mod Sources");

/// <inheritdoc />
public override bool OnMenuOpened(Object data)
Expand Down Expand Up @@ -228,6 +229,19 @@ private static void CreateExtraButtons(ExtraSettingsScreen gameMenu)
modBrowserButton.AddText(new Info("Text", 0, -200, 500, 150), BrowseMods, 60f);
modBrowserButton.SetActive(InGame.instance == null);

var modSourcesButton = bottomButtonGroup.AddButton(new("ModSourcesButton", 225, Padding * 2 + 400, 400)
{
Anchor = Vector2.zero,
Pivot = new Vector2(0.5f, 0)
}, VanillaSprites.WoodenRoundButton, new Action(() =>
{
Open<ModSourcesMenu>();
}));

modSourcesButton.AddImage(new("ChallengesIcon", InfoPreset.FillParent), VanillaSprites.ChallengesIcon);

modSourcesButton.AddText(new("Text", 0, -200, 500, 100), ModSources, 60f);

var createModButton = bottomButtonGroup.AddButton(new Info("CreateModButton", 225, Padding, 400)
{
Anchor = Vector2.zero,
Expand Down
Loading