Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.
Merged
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
5 changes: 4 additions & 1 deletion APITest/APITest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="System.Drawing" />
<Reference Include="System.Drawing" />
<Reference Include="Unity.TextMeshPro">
<HintPath>..\Lib\Unity.TextMeshPro.dll</HintPath>
</Reference>
Expand All @@ -108,7 +108,10 @@
<Compile Include="APITestMain.cs" />
<Compile Include="Commands\AddUI.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UI\Elements\ProfilePictureElement.cs" />
<Compile Include="UI\Elements\HelloWorldElement.cs" />
<Compile Include="UI\PersonalDisplayBuilder.cs" />
<Compile Include="UI\PersonalElementDisplay.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NotAnAPI\NotAnAPI.csproj">
Expand Down
20 changes: 16 additions & 4 deletions APITest/Commands/AddUI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using APITest.UI;
using APITest.UI.Elements;
using CommandSystem;
using Exiled.API.Features;
using MEC;
Expand Down Expand Up @@ -26,14 +27,25 @@ public class AddUI : NotCommands

public override string[] GetPerms() => null;

public override bool Function(string[] args, ICommandSender sender, out string result)
public override bool GetRequirePlayer() => true;

public override bool PlayerBasedFunction(Player player, string[] args, out string result)
{
Player player = Player.Get(sender);
if (!TryGetArgument(args, 1, out string arg1))
{
PersonalDisplayBuilder display1 = new PersonalDisplayBuilder(StringBuilderPool.Shared.Rent());

player.GameObject.AddComponent<UIManager>()._mainDisplay = display1;

PersonalDisplayBuilder display = new PersonalDisplayBuilder(StringBuilderPool.Shared.Rent());
result = "PersonalDisplay enabled!";
return true;
}

PersonalElementDisplay display = new PersonalElementDisplay(StringBuilderPool.Shared.Rent());

player.GameObject.AddComponent<UIManager>()._mainDisplay = display;
result = $"Working.";

result = $"PersonalElement Enabled";
return false;
}

Expand Down
33 changes: 33 additions & 0 deletions APITest/UI/Elements/HelloWorldElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using NotAnAPI.Features.UI.API.Elements;
using NotAnAPI.Features.UI.API.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace APITest.UI.Elements
{
public class HelloWorldElement : Element
{
public override string Name { get; set; } = "Hello World";
public override string Text { get; set; } = "Hello World";
public override Vector2 Position { get; set; } = new Vector2(-500, 300);

public override TextSettings Settings { get; set; } = new()
{
Size = 60,
};

public override UIScreenZone Zone { get; set; } = UIScreenZone.Center;
public override UIType UI { get; set; } = UIType.Alive;

public override string OnRender()
{
Position = new Vector2(UnityEngine.Random.Range(0, 301), UnityEngine.Random.Range(0, 301));

return base.OnRender();
}
}
}
33 changes: 33 additions & 0 deletions APITest/UI/Elements/ProfilePictureElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using NotAnAPI.Features.UI.API.Elements;
using NotAnAPI.Features.UI.API.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace APITest.UI.Elements
{
public class ProfilePictureElement : Element
{
public override string Name { get; set; } = "Profile Picture";
public override string Text { get; set; } = "PFP";
public override Vector2 Position { get; set; } = new Vector2(-900, 300);

public override TextSettings Settings { get; set; } = new()
{
Size = 1.5f,
};

public override UIScreenZone Zone { get; set; } = UIScreenZone.Center;
public override UIType UI { get; set; } = UIType.Both;

public override string OnRender()
{
Position = new Vector2(UnityEngine.Random.Range(0, 501), UnityEngine.Random.Range(0, 501));

return base.OnRender();
}
}
}
33 changes: 33 additions & 0 deletions APITest/UI/PersonalElementDisplay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using APITest.UI.Elements;
using NorthwoodLib.Pools;
using NotAnAPI.Features.UI.API.Abstract;
using NotAnAPI.Features.UI.API.Elements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace APITest.UI
{
public class PersonalElementDisplay : GameElementDisplay
{
private readonly StringBuilder _builder;

public PersonalElementDisplay(StringBuilder builder) : base(builder)
{
_builder = builder;
}

~PersonalElementDisplay() => StringBuilderPool.Shared.Return(_builder);

public override List<Element> Elements { get; set; } = new()
{
//Alive Elements
new HelloWorldElement(),

//Both
new ProfilePictureElement(),
};
}
}
2 changes: 1 addition & 1 deletion NotAnAPI/Features/UI/API/Abstract/GameDisplayBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void WithContent(UIScreenZone zone, string content)
public void WithNotifications(List<string> notifications) => _notifications = notifications;
public void WithColor(string color) => _color = color;
public void WithPlayer(Player player) => _player = player;
public void WithPFP(string pfp) => PFP = pfp;
public void WithPFP(string pfp) => PFP = "<indent=-25%>" + pfp + "</indent>";

public abstract void CustomUIAlive(StringBuilder builder, UIScreenZone zone);
public abstract void CustomUISpectator(StringBuilder builder, UIScreenZone zone);
Expand Down
52 changes: 52 additions & 0 deletions NotAnAPI/Features/UI/API/Abstract/GameElementDisplay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using Exiled.API.Features;
using NorthwoodLib.Pools;
using NotAnAPI.Features.UI.API.Elements;
using NotAnAPI.Features.UI.API.Enums;
using NotAnAPI.Features.UI.API.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace NotAnAPI.Features.UI.API.Abstract
{
/// <summary>
/// The core and power of the UI
/// </summary>
public abstract class GameElementDisplay : GameDisplayBuilder, IElements
{
private readonly StringBuilder _builder;
public abstract List<Element> Elements { get; set; }

public GameElementDisplay(StringBuilder builder) : base(builder)
{
_builder = builder;
}

~GameElementDisplay() => StringBuilderPool.Shared.Return(_builder);

public void Renderer(StringBuilder builder, UIScreenZone zone, UIType type = UIType.Alive)
{
foreach (Element elements in Elements)
{
if (elements.UI != type && elements.UI != UIType.Both) continue;
if (elements.Zone != zone) continue;

builder.Append(elements.OnRender().Replace("PFP", PFP));
}

}

public override void CustomUIAlive(StringBuilder builder, UIScreenZone zone)
{
Renderer(builder, zone);
}

public override void CustomUISpectator(StringBuilder builder, UIScreenZone zone)
{
Renderer(builder, zone, UIType.Spectator);
}
}
}
103 changes: 103 additions & 0 deletions NotAnAPI/Features/UI/API/Elements/Element.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using Exiled.API.Features;
using NotAnAPI.API.Extensions;
using NotAnAPI.Features.UI.API.Enums;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using static System.Net.Mime.MediaTypeNames;

namespace NotAnAPI.Features.UI.API.Elements
{
public abstract class Element
{

public abstract string Name { get; set; }

public abstract string Text { get; set; }

public abstract Vector2 Position { get; set; }

public abstract TextSettings Settings { get; set; }

public abstract UIScreenZone Zone { get; set; }

public abstract UIType UI { get; set; }

public virtual string OnRender(Player player) => OnRender();
public virtual string OnRender()
{
StringBuilder sb = new StringBuilder();
var lineList = Text.Split('\n');
float yOffset = 0;

foreach (var line in lineList)
{
//Basic
float xCoordinate = Position.x;
float yCoordinate = GetVOffset(Zone) - yOffset;

if (xCoordinate != 0) sb.AddHorizontalPos(xCoordinate);
if (Settings.LineHeight >= 0) sb.SetLineHeight(Settings.LineHeight);
if (yCoordinate != 0) sb.AddVOffset(yCoordinate);
if (Settings.Size > 0) sb.SetSize(Settings.Size);
//Other options

sb.Append(line);

//Close Basic Settings
if (Settings.Size > 0) sb.CloseSize();
if (yCoordinate != 0) sb.CloseVOffset();

sb.Append("\n");
yOffset += Settings.Size;
}

return sb.ToString();
}

//Thanks to HintServiceMeow for the screen calculation
//Credits to them
public float GetVOffset(UIScreenZone align)
{
float sizeOffset;

switch (align)
{
case UIScreenZone.Top:
sizeOffset = -GetTextHeight();
break;
case UIScreenZone.CenterTop:
sizeOffset = -GetTextHeight();
break;
case UIScreenZone.Center:
sizeOffset = -GetTextHeight() / 2;
break;
case UIScreenZone.CenterBottom:
sizeOffset = -GetTextHeight();
break;
case UIScreenZone.Bottom:
sizeOffset = 0;
break;
default:
sizeOffset = 0;
break;
}

return 700 - Position.y + sizeOffset;
}

public float GetTextHeight()
{
if (string.IsNullOrEmpty(Text))
return 0;

var height = Text.Split('\n').Length;

return height;
}
}
}
19 changes: 19 additions & 0 deletions NotAnAPI/Features/UI/API/Elements/TextSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NotAnAPI.Features.UI.API.Enums;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NotAnAPI.Features.UI.API.Elements
{
public class TextSettings
{

public float Size { get; set; } = 0;

public float LineHeight { get; set; } = 0;

}
}
17 changes: 17 additions & 0 deletions NotAnAPI/Features/UI/API/Enums/UIType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NotAnAPI.Features.UI.API.Enums
{
public enum UIType
{

Spectator,
Alive,
Both

}
}
16 changes: 16 additions & 0 deletions NotAnAPI/Features/UI/API/Interfaces/IElements.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using NotAnAPI.Features.UI.API.Elements;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NotAnAPI.Features.UI.API.Interfaces
{
/// <summary>
/// Marks all the classes that uses Elements
/// </summary>
public interface IElements
{
}
}
5 changes: 5 additions & 0 deletions NotAnAPI/NotAnAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,16 @@
<Compile Include="API\Translator\Languages.cs" />
<Compile Include="API\Translator\TranslationResponse.cs" />
<Compile Include="Features\Base\APIFeaturesManager.cs" />
<Compile Include="Features\UI\API\Abstract\GameElementDisplay.cs" />
<Compile Include="Features\UI\API\Abstract\GameDisplayBuilder.cs" />
<Compile Include="Features\UI\API\Elements\Element.cs" />
<Compile Include="Features\UI\API\Elements\TextSettings.cs" />
<Compile Include="Features\UI\API\Enums\AlignStyle.cs" />
<Compile Include="Features\UI\API\Enums\CaseStyle.cs" />
<Compile Include="Features\UI\API\Enums\MeasurementUnit.cs" />
<Compile Include="Features\UI\API\Enums\UIScreenZone.cs" />
<Compile Include="Features\UI\API\Enums\UIType.cs" />
<Compile Include="Features\UI\API\Interfaces\IElements.cs" />
<Compile Include="Features\UI\API\Notification\UINotification.cs" />
<Compile Include="Features\UI\Loader\SteamLoader.cs" />
<Compile Include="Features\UI\Patch\UIRedirect.cs" />
Expand Down