using System.Collections.Generic;
using Oxide.Core;
using Oxide.Game.Rust.Cui;
using Rust;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("CustomMenu", "YourName", "1.0.0")]
[Description("Adds a custom menu with tabs and icons")]
class CustomMenu : RustPlugin
{
private const string MENU_PARENT_NAME = "CustomMenu";
private const string MENU_BUTTON_NAME = "CustomMenuButton";
private const string TAB_CONTAINER_NAME = "CustomTabContainer";
private const string TAB_BUTTON_NAME = "CustomTabButton";
private const string TAB_ICON_NAME = "CustomTabIcon";
private const string TAB_TEXT_NAME = "CustomTabText";
private readonly Dictionary<BasePlayer, CuiElementContainer> menuElements = new Dictionary<BasePlayer, CuiElementContainer>();
private void Init()
{
foreach (var player in BasePlayer.activePlayerList)
{
CreateMenuElements(player);
}
}
private void Unload()
{
foreach (var player in BasePlayer.activePlayerList)
{
DestroyMenuElements(player);
}
}
private void OnPlayerInit(BasePlayer player)
{
CreateMenuElements(player);
}
private void OnPlayerDisconnected(BasePlayer player)
{
DestroyMenuElements(player);
}
private void CreateMenuElements(BasePlayer player)
{
if (menuElements.ContainsKey(player)) return;
var menuContainer = new CuiElementContainer();
// Add the main menu button
menuContainer.Add(new CuiButton
{
Button = { Command = "custommenu.open", Color = "0.5 0.5 0.5 0.7" },
RectTransform = { AnchorMin = "0.01 0.85", AnchorMax = "0.11 0.95" },
Text = { Text = "Menu", FontSize = 18, Align = TextAnchor.MiddleCenter }
}, MENU_PARENT_NAME, MENU_BUTTON_NAME);
menuElements[player] = menuContainer;
CuiHelper.AddUi(player, menuContainer);
}
private void DestroyMenuElements(BasePlayer player)
{
if (!menuElements.TryGetValue(player, out var menuContainer)) return;
CuiHelper.DestroyUi(player, MENU_PARENT_NAME);
menuElements.Remove(player);
}
[ChatCommand("custommenu.open")]
private void OpenMenu(BasePlayer player)
{
var tabContainer = new CuiElementContainer();
for (var i = 0; i < 15; i++)
{
var yPos = 0.8f - i * 0.05f;
var tabIndex = i;
tabContainer.Add(new CuiButton
{
Button = { Command = $"custommenu.tab{tabIndex}", Color = "0.5 0.5 0.5 0.7" },
RectTransform = { AnchorMin = $"0.01 {yPos:F}", AnchorMax = $"0.11 {yPos + 0.04f:F}" },
Text = { Text = (i + 1).ToString(), FontSize = 18, Align = TextAnchor.MiddleCenter }
}, MENU_PARENT_NAME, $"{TAB_BUTTON_NAME}{tabIndex}");
tabContainer.Add(new CuiElement
{
Name = $"{TAB_ICON_NAME}{tabIndex}",
Parent = MENU_PARENT_NAME,
Components =
{
new CuiRawImageComponent
{
Png = FileStorage.server.GetServerFile($"custommenu/icon{tabIndex}.png"),
Color = "1 1 1 1",
Sprite = "assets/content/textures/generic/fulltransparent.tga"
},
new CuiRectTransformComponent
{
AnchorMin = $"0.12 {yPos:F}",
AnchorMax = $"0.22 {yPos + 0.04f:F}"
}
}
});
tabContainer.Add(new CuiLabel
{
Text = { Text = $"Tab {tabIndex + 1}", FontSize = 16, Align = TextAnchor.MiddleLeft },
RectTransform = { AnchorMin = $"0.23 {yPos:F}", AnchorMax = $"0.6 {yPos + 0.04f:F}" }
}, MENU_PARENT_NAME, $"{TAB_TEXT_NAME}{tabIndex}");
}
menuElements[player].Add(tabContainer, MENU_PARENT_NAME, TAB_CONTAINER_NAME);
CuiHelper.AddUi(player, menuElements[player]);
}
[ConsoleCommand("custommenu.tab")]
private void OpenTab(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player == null) return;
if (!int.TryParse(arg.cmd.Name.Substring(13), out var tabIndex)) return;
CuiHelper.DestroyUi(player, MENU_PARENT_NAME, TAB_CONTAINER_NAME);
CuiHelper.AddUi(player, new CuiElementContainer());
SendReply(player, $"You opened tab {tabIndex + 1}.");
}
}
}