Skip to content

Commit

Permalink
Add PFButton helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows10CE committed Aug 2, 2021
1 parent cdeb967 commit 3f8d75b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 10 deletions.
12 changes: 11 additions & 1 deletion ExampleMod/ExampleModPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Pathfinder.Administrator;
using Pathfinder.Util;
using Pathfinder.Daemon;
using Pathfinder.GUI;
using Pathfinder.Mission;
using Pathfinder.Util.XML;

Expand All @@ -37,6 +38,13 @@ public override bool Load()
return true;
}

public override bool Unload()
{
PatchClass2.bruhButton.Dispose();

return base.Unload();
}

public static void TestCommand(OS os, string[] args)
{
os.write("pathfinder is here!");
Expand Down Expand Up @@ -204,12 +212,14 @@ public override void traceEjectionDetected(Computer c, OS os)
[HarmonyPatch]
public static class PatchClass2
{
internal static PFButton bruhButton = new PFButton(5, 5, 30, 600, "bruh", Color.BlueViolet);

[HarmonyPostfix]
[HarmonyPatch(typeof(MainMenu), nameof(MainMenu.Draw))]
public static void MainMenuTextPatch()
{
GuiData.startDraw();
Hacknet.Gui.Button.doButton(3473249, 5, 5, 30, 600, "bruh", Color.BlueViolet);
bruhButton.Do();
GuiData.endDraw();
}
}
Expand Down
67 changes: 67 additions & 0 deletions PathfinderAPI/GUI/PFButton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using Hacknet.Gui;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pathfinder.GUI
{
public class PFButton : IDisposable
{
private static int _idCounter = int.MinValue;
private static readonly List<int> returnedIds = new List<int>();
public static int GetNextID()
{
if (returnedIds.Count > 0)
{
var ret = returnedIds[0];
returnedIds.RemoveAt(0);
return ret;
}

return _idCounter++;
}
public static void ReturnID(int id)
{
returnedIds.Add(id);
}

public readonly int ID = GetNextID();

public int X;
public int Y;
public int Height;
public int Width;
public string Text;
public Color? Color;
public Texture2D Texture;

private bool invalid = false;

public PFButton(int x, int y, int width, int height, string text, Color? color = null, Texture2D texture = null)
{
X = x;
Y = y;
Width = width;
Height = height;
Text = text ?? throw new ArgumentNullException(nameof(text), "Button text cannot be null!");
Color = color;
Texture = texture;
}

public bool Do()
{
if (invalid)
throw new InvalidOperationException("This Button has been disposed, and is no longer valid");
if (Texture == null)
return Button.doButton(ID, X, Y, Width, Height, Text, Color);
return Button.doButton(ID, X, Y, Width, Height, Text, Color, Texture);
}

public void Dispose()
{
returnedIds.Add(ID);
invalid = true;
}
}
}
9 changes: 6 additions & 3 deletions PathfinderAPI/Options/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Hacknet.Localization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Pathfinder.GUI;

namespace Pathfinder.Options
{
Expand All @@ -31,7 +32,7 @@ public Option(string name, string description="")
this.Description = description;
}

public abstract void Draw(int id, int x, int y);
public abstract void Draw(int x, int y);
}

public class OptionCheckbox : Option
Expand All @@ -41,15 +42,17 @@ public class OptionCheckbox : Option
public override int SizeX => 100;
public override int SizeY => 75;

private int ButtonID = PFButton.GetNextID();

public OptionCheckbox(string name, string description="", bool defVal=false) : base(name, description)
{
this.Value = defVal;
}

public override void Draw(int id, int x, int y)
public override void Draw(int x, int y)
{
TextItem.doLabel(new Vector2(x, y), Name, null, 200);
Value = CheckBox.doCheckBox(id, x, y + 34, Value, null);
Value = CheckBox.doCheckBox(ButtonID, x, y + 34, Value, null);

TextItem.doSmallLabel(new Vector2(x+32, y+30), Description, null);
}
Expand Down
3 changes: 3 additions & 0 deletions PathfinderAPI/Options/OptionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Hacknet.Localization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Pathfinder.GUI;

namespace Pathfinder.Options
{
Expand All @@ -39,6 +40,8 @@ public class OptionsTab

public List<Option> Options = new List<Option>();

internal int ButtonID = PFButton.GetNextID();

public OptionsTab(string name) {
Name = name;
}
Expand Down
15 changes: 9 additions & 6 deletions PathfinderAPI/Options/PathfinderOptionsMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Xna.Framework.Input;
using Pathfinder.Event;
using Pathfinder.Event.Options;
using Pathfinder.GUI;

namespace Pathfinder.Options
{
Expand All @@ -24,6 +25,8 @@ internal static class PathfinderOptionsMenu
private static bool isInPathfinderMenu = false;
private static string currentTabName = null;

private static PFButton ReturnButton = new PFButton(10, 10, 220, 54, "Back to Options", Color.Yellow);

[HarmonyPrefix]
[HarmonyPatch(typeof(OptionsMenu), nameof(OptionsMenu.Draw))]
internal static bool Draw(ref OptionsMenu __instance, GameTime gameTime)
Expand All @@ -35,7 +38,7 @@ internal static bool Draw(ref OptionsMenu __instance, GameTime gameTime)
GuiData.startDraw();
PatternDrawer.draw(new Rectangle(0, 0, __instance.ScreenManager.GraphicsDevice.Viewport.Width, __instance.ScreenManager.GraphicsDevice.Viewport.Height), 0.5f, Color.Black, new Color(2, 2, 2), GuiData.spriteBatch);

if (Button.doButton(798000, 10, 10, 220, 54, "Back to Options", Color.Yellow))
if (ReturnButton.Do())
{
currentTabName = null;
isInPathfinderMenu = false;
Expand All @@ -48,7 +51,6 @@ internal static bool Draw(ref OptionsMenu __instance, GameTime gameTime)

var tabs = OptionsManager.Tabs;

int tabId = 798010;
int tabX = 10;

foreach (var tab in tabs.Values)
Expand All @@ -57,7 +59,7 @@ internal static bool Draw(ref OptionsMenu __instance, GameTime gameTime)
currentTabName = tab.Name;
var active = currentTabName == tab.Name;
// Display tab button
if (Button.doButton(tabId++, tabX, 70, 128, 20, tab.Name, active ? Color.Green : Color.Gray))
if (Button.doButton(tab.ButtonID, tabX, 70, 128, 20, tab.Name, active ? Color.Green : Color.Gray))
{
currentTabName = tab.Name;
break;
Expand All @@ -68,11 +70,10 @@ internal static bool Draw(ref OptionsMenu __instance, GameTime gameTime)
continue;

// Display options
int optId = 798100;
int optX = 80, optY = 110;
foreach (var option in tab.Options)
{
option.Draw(optId++, optX, optY);
option.Draw(optX, optY);
optY += 10 + option.SizeY;
}
}
Expand All @@ -82,6 +83,8 @@ internal static bool Draw(ref OptionsMenu __instance, GameTime gameTime)
return false;
}

private static PFButton EnterButton = new PFButton(240, 10, 220, 54, "Pathfinder Options", Color.Yellow);

[HarmonyILManipulator]
[HarmonyPatch(typeof(OptionsMenu), nameof(OptionsMenu.Draw))]
internal static void BeforeEndDrawOptions(ILContext il)
Expand All @@ -92,7 +95,7 @@ internal static void BeforeEndDrawOptions(ILContext il)

c.EmitDelegate<System.Action>(() =>
{
if (Button.doButton(798000, 240, 10, 220, 54, "Pathfinder Options", Color.Yellow))
if (EnterButton.Do())
{
isInPathfinderMenu = true;
}
Expand Down
1 change: 1 addition & 0 deletions PathfinderAPI/PathfinderAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<Compile Include="Event\SaveEvent.cs" />
<Compile Include="Executable\BaseExecutable.cs" />
<Compile Include="Executable\ExecutableManager.cs" />
<Compile Include="GUI\PFButton.cs" />
<Compile Include="Logger.cs" />
<Compile Include="MiscPatches.cs" />
<Compile Include="Mission\GoalManager.cs" />
Expand Down

0 comments on commit 3f8d75b

Please sign in to comment.