private static readonly HashSet<BasePlayer> playersWithPopup = new();
#region Initialization
private void Init()
{
Ui.DestroyAll();
}
#endregion
#region Commands
[ChatCommand("ui")]
private void CmdOpenUi(BasePlayer player)
{
Ui.Destroy(player);
Ui.Create(player, ImageLibrary);
}
[ConsoleCommand("quarrycompui.close")]
private void CmdClose(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player != null)
{
Ui.Destroy(player);
PrintToChat(player, "Close button clicked.");
}
}
[ConsoleCommand("quarrycompui.remove")]
private void CmdRemoveQuarry(ConsoleSystem.Arg arg)
{
var player = arg.Player();
TogglePopup(player, "Remove the Quarry?", "quarrycompui.confirmremove", "quarrycompui.cancelremove");
}
[ConsoleCommand("quarrycompui.confirmremove")]
private void CmdConfirmRemove(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player != null)
{
Ui.ClosePopup(player);
PrintToChat(player, "Quarry removed.");
}
}
[ConsoleCommand("quarrycompui.cancelremove")]
private void CmdCancelRemove(ConsoleSystem.Arg arg)
{
ClosePopupAndNotify(arg.Player(), "Removal cancelled.");
}
[ConsoleCommand("quarrycompui.upgrade")]
private void CmdUpgradeQuarry(ConsoleSystem.Arg arg)
{
var player = arg.Player();
TogglePopup(player, "Upgrade the Quarry?", "quarrycompui.confirmupgrade", "quarrycompui.cancelupgrade");
}
[ConsoleCommand("quarrycompui.confirmupgrade")]
private void CmdConfirmUpgrade(ConsoleSystem.Arg arg)
{
var player = arg.Player();
if (player != null)
{
Ui.ClosePopup(player);
PrintToChat(player, "Quarry upgraded.");
}
}
[ConsoleCommand("quarrycompui.cancelupgrade")]
private void CmdCancelUpgrade(ConsoleSystem.Arg arg)
{
ClosePopupAndNotify(arg.Player(), "Upgrade cancelled.");
}
#endregion
private void TogglePopup(BasePlayer player, string message, string confirmCmd, string cancelCmd)
{
if (player != null && !playersWithPopup.Contains(player))
{
playersWithPopup.Add(player);
Ui.ShowPopup(player, message, confirmCmd, cancelCmd);
}
}
private void ClosePopupAndNotify(BasePlayer player, string message)
{
if (player != null)
{
Ui.ClosePopup(player);
PrintToChat(player, message);
}
}
#region UI Configuration
public static class Ui
{
public static class Cfg
{
public static readonly Dictionary<string, string> Comps =
new()
{
{ "Bg", "QuarryCompUi_Background" },
{ "MainPanel", "QuarryCompUi_MainPanel" },
{ "OvrlBlk", "Overlay_Block" },
{ "PopShd", "Popup_Shadow" },
{ "PopBdr", "Popup_Border" },
{ "PopBg", "Popup_Background" },
{ "ClsBtn", "QuarryCompUi_Close" },
{ "QryPanel", "QuarryCompUi_Full_Left" },
{ "UpgPanel", "QuarryCompUi_Full_Right" },
};
public static readonly Dictionary<string, string> Colors =
new()
{
{ "Gray", "0.5 0.5 0.5 0.9" },
{ "LightGray", "0.60 0.60 0.60 0.95" },
{ "DarkGray", "0.25 0.25 0.25 0.95" },
{ "Red", "1 0 0 1" },
{ "DarkRed", "0.55 0 0 1" },
{ "Black90", "0 0 0 0.9" },
{ "Black85", "0 0 0 0.85" },
{ "Black97", "0 0 0 0.97" },
{ "Blue", "0 0 1 1" },
{ "White", "1 1 1 1" },
{ "White80", "0.8 0.8 0.8 1" },
{ "White50", "1 1 1 0.5" },
{ "Green", "0 1 0 1" },
{ "Crimson", "0.86 0.08 0.24 1" },
};
}
public static void Create(BasePlayer player, Plugin imageLibrary)
{
var container = new CuiElementContainer();
AddPanel(container, "0 0", "1 1", Cfg.Comps["Bg"], color: Cfg.Colors["Black90"], isCursorEnabled: true);
AddMain(container);
AddCloseBtn(container, Cfg.Comps["MainPanel"]);
AddImg(container, Cfg.Comps["Bg"], "crude.oil", "0.1 0.1", "0.2 0.2", "ResImg", imageLibrary);
CuiHelper.AddUi(player, container);
}
public static void Destroy(BasePlayer player)
{
if (player == null)
return;
foreach (var elem in Cfg.Comps)
CuiHelper.DestroyUi(player, elem.Value);
playersWithPopup.Remove(player);
}
public static void DestroyAll()
{
foreach (var player in BasePlayer.activePlayerList)
Destroy(player);
}
public static void ShowPopup(BasePlayer player, string message, string confirmCmd, string cancelCmd)
{
var container = new CuiElementContainer();
AddPanel(container, "0 0", "1 1", Cfg.Comps["OvrlBlk"], "Overlay", Cfg.Colors["Black90"], isCursorEnabled: true);
AddPanel(container, "0.4 0.4", "0.6 0.6", Cfg.Comps["PopShd"], Cfg.Comps["OvrlBlk"], Cfg.Colors["Black85"]);
AddPanel(container, "0.0 0.09", "0.95 1.0", Cfg.Comps["PopBdr"], Cfg.Comps["PopShd"], Cfg.Colors["White"]);
AddPanel(container, "0.01 0.01", "0.991 0.985", Cfg.Comps["PopBg"], Cfg.Comps["PopBdr"], Cfg.Colors["Black97"]);
AddTxt(container, Cfg.Comps["PopBg"], "0.1 0.6", "0.9 0.9", message, 16, TextAnchor.MiddleCenter);
AddBtn(container, Cfg.Comps["PopBg"], "Yes", confirmCmd, "0.25", "0.05", "0.45", "0.15", Cfg.Colors["LightGray"], Cfg.Colors["Green"]);
AddBtn(container, Cfg.Comps["PopBg"], "No", cancelCmd, "0.55", "0.05", "0.75", "0.15", Cfg.Colors["LightGray"], Cfg.Colors["Red"]);
CuiHelper.AddUi(player, container);
}
public static void ClosePopup(BasePlayer player)
{
foreach (var elem in Cfg.Comps)
if (elem.Key.StartsWith("Popup") || elem.Value == Cfg.Comps["OvrlBlk"])
CuiHelper.DestroyUi(player, elem.Value);
playersWithPopup.Remove(player);
}
private static void AddMain(CuiElementContainer container)
{
AddPanel(container, "0.235 0.26", "0.765 0.724", Cfg.Comps["MainPanel"], Cfg.Comps["Bg"], Cfg.Colors["LightGray"], true);
AddQuarryPanel(container);
AddUpgradePanel(container);
}
private static void AddQuarryPanel(CuiElementContainer container)
{
AddPanel(container, "0.0 0", "0.5 1", Cfg.Comps["QryPanel"], Cfg.Comps["MainPanel"], Cfg.Colors["Black97"]);
AddInnerSquare(container, Cfg.Comps["QryPanel"], "0.10 0.05", "0.95 0.90", Cfg.Colors["Black97"]);
AddInnerSquare(container, Cfg.Comps["QryPanel"], "0.054 0.1033", "0.895 0.945", Cfg.Colors["White80"]);
AddInnerSquare(container, Cfg.Comps["QryPanel"], "0.06 0.108", "0.89 0.94", Cfg.Colors["DarkRed"]);
AddInnerSquare(container, Cfg.Comps["QryPanel"], "0.06 0.865", "0.89 0.870", Cfg.Colors["White80"]);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.84", "0.9 0.98", "Quarry Level", 15, TextAnchor.MiddleCenter);
AddSep(container, Cfg.Comps["QryPanel"], "0.06 0.82", "0.89 0.825");
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.78", "0.9 0.82", "Extraction Settings:", 12, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.74", "0.9 0.78", "Extraction Interval: 1 sec", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.7", "0.9 0.74", "Fuel/extraction: 1 LG Fuel", 10, TextAnchor.MiddleLeft);
AddSep(container, Cfg.Comps["QryPanel"], "0.06 0.65", "0.89 0.655");
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.6", "0.9 0.65", "Storage:", 12, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.56", "0.9 0.6", "Fuel Storage: 3 slots", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.52", "0.9 0.56", "Resource Storage: 6 slots", 10, TextAnchor.MiddleLeft);
AddSep(container, Cfg.Comps["QryPanel"], "0.06 0.47", "0.89 0.475");
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.42", "0.9 0.47", "Resources/Extraction:", 12, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.38", "0.9 0.42", "Crude Oil: 10", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.34", "0.9 0.38", "HQM Ore: 60", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.3", "0.9 0.34", "Metal Ore: 100", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.26", "0.9 0.3", "Stones: 250", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["QryPanel"], "0.1 0.22", "0.9 0.26", "Sulfur Ore: 160", 10, TextAnchor.MiddleLeft);
AddBtn(container, Cfg.Comps["QryPanel"], "Remove Quarry", "quarrycompui.remove", "0.35", "0.11", "0.65", "0.16", Cfg.Colors["LightGray"], Cfg.Colors["Red"]);
}
private static void AddUpgradePanel(CuiElementContainer container)
{
AddPanel(container, "0.5 0", "1 1", Cfg.Comps["UpgPanel"], Cfg.Comps["MainPanel"], Cfg.Colors["Black97"]);
AddInnerSquare(container, Cfg.Comps["UpgPanel"], "0.10 0.05", "0.95 0.90", Cfg.Colors["Black97"]);
AddInnerSquare(container, Cfg.Comps["UpgPanel"], "0.054 0.1033", "0.895 0.945", Cfg.Colors["White80"]);
AddInnerSquare(container, Cfg.Comps["UpgPanel"], "0.06 0.108", "0.89 0.94", Cfg.Colors["Blue"]);
AddInnerSquare(container, Cfg.Comps["UpgPanel"], "0.06 0.865", "0.89 0.870", Cfg.Colors["White80"]);
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.84", "0.9 0.98", "Upgrade to Next Level", 15, TextAnchor.MiddleCenter);
AddSep(container, Cfg.Comps["UpgPanel"], "0.06 0.82", "0.89 0.825");
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.78", "0.9 0.82", "Requirements:", 12, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.74", "0.9 0.78", "Scrap: 5000", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.7", "0.9 0.74", "Bone Fragments: 10000", 10, TextAnchor.MiddleLeft);
AddSep(container, Cfg.Comps["UpgPanel"], "0.06 0.65", "0.89 0.655");
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.6", "0.9 0.65", "Upgrade Effects:", 12, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.56", "0.9 0.6", "Fuel Storage: 6 slots", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.52", "0.9 0.56", "Resource Storage: 12 slots", 10, TextAnchor.MiddleLeft);
AddSep(container, Cfg.Comps["UpgPanel"], "0.06 0.47", "0.89 0.475");
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.42", "0.9 0.47", "Crude Oil: 20", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.38", "0.9 0.42", "HQM Ore: 120", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.34", "0.9 0.38", "Metal Ore: 150", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.3", "0.9 0.34", "Stones: 350", 10, TextAnchor.MiddleLeft);
AddTxt(container, Cfg.Comps["UpgPanel"], "0.1 0.26", "0.9 0.3", "Sulfur Ore: 200", 10, TextAnchor.MiddleLeft);
AddBtn(container, Cfg.Comps["UpgPanel"], "Upgrade", "quarrycompui.upgrade", "0.35", "0.11", "0.65", "0.16", Cfg.Colors["LightGray"], Cfg.Colors["Green"]);
}
private static void AddSep(CuiElementContainer container, string parent, string anchorMin, string anchorMax)
{
AddInnerSquare(container, parent, anchorMin, anchorMax, Cfg.Colors["White80"]);
}
private static void AddPanel(
CuiElementContainer container,
string anchorMin,
string anchorMax,
string panelName,
string parent = "Overlay",
string color = "0 0 0 1",
bool isCursorEnabled = false,
bool isBackground = false
)
{
var panel = new CuiPanel
{
Image = { Color = color },
RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax },
CursorEnabled = isCursorEnabled,
};
if (isBackground)
{
panel.Image.Material = "assets/content/ui/uibackgroundblur.mat";
panel.Image.Sprite = "assets/content/ui/ui.background.transparent.radial.psd";
}
container.Add(panel, parent, panelName);
}
private static void AddInnerSquare(CuiElementContainer container, string parent, string anchorMin, string anchorMax, string color)
{
container.Add(new CuiPanel { Image = { Color = color }, RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax } }, parent);
}
private static void AddTxt(CuiElementContainer container, string parent, string anchorMin, string anchorMax, string text, int fontSize, TextAnchor alignment)
{
container.Add(
new CuiLabel
{
Text =
{
Text = text,
FontSize = fontSize,
Font = "DroidSansMono.ttf",
Align = alignment,
Color = Cfg.Colors["White"],
},
RectTransform = { AnchorMin = anchorMin, AnchorMax = anchorMax },
},
parent
);
}
private static void AddBtn(
CuiElementContainer container,
string parent,
string text,
string command,
string anchorMinX,
string anchorMinY,
string anchorMaxX,
string anchorMaxY,
string backgroundColor,
string textColor
)
{
container.Add(
new CuiButton
{
Button =
{
Color = backgroundColor,
Command = command,
Close = "",
},
RectTransform = { AnchorMin = $"{anchorMinX} {anchorMinY}", AnchorMax = $"{anchorMaxX} {anchorMaxY}" },
Text =
{
Text = text,
FontSize = 14,
Align = TextAnchor.MiddleCenter,
Color = textColor,
},
},
parent
);
}
private static void AddImg(CuiElementContainer container, string parent, string resourceName, string anchorMin, string anchorMax, string imageName, Plugin imageLibrary)
{
string imageUrl = imageLibrary?.Call("GetImage", resourceName) as string;
if (imageUrl == null)
{
Debug.LogError($"Failed to get image for item: {resourceName}");
return;
}
container.Add(
new CuiElement
{
Name = imageName,
Parent = parent,
Components =
{
new CuiRawImageComponent { Png = imageUrl },
new CuiRectTransformComponent { AnchorMin = anchorMin, AnchorMax = anchorMax },
},
}
);
}
private static void AddCloseBtn(CuiElementContainer container, string parent)
{
container.Add(
new CuiButton
{
Button =
{
Color = Cfg.Colors["Red"],
Command = "quarrycompui.close",
Close = Cfg.Comps["Bg"],
},
RectTransform = { AnchorMin = "0.95 0.95", AnchorMax = "1 1" },
Text =
{
Text = "✖",
FontSize = 13,
Align = TextAnchor.MiddleCenter,
Color = Cfg.Colors["White50"],
},
},
parent,
Cfg.Comps["ClsBtn"]
);
}
}
#endregion
}