Files
MxValheim/MxValheim/EventSystem/EventHud.cs
2026-02-12 23:38:28 -05:00

130 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Annotations;
using System.Windows.Controls;
using UnityEngine;
using UnityEngine.UI;
using Image = UnityEngine.UI.Image;
namespace MxValheim.EventSystem
{
internal class EventHud
{
private static GameObject _eventRoot;
private static Text _eventTitleText;
private static Text _eventProgressText;
private static Image _progressBar;
private static CanvasGroup _canvasGroup;
public static void CreateEventHud()
{
// 1. Setup Root (Stacked under your KillFeed which is at -30)
_eventRoot = new GameObject("MxEventHUD_Root");
UnityEngine.Object.DontDestroyOnLoad(_eventRoot);
UnityEngine.Canvas c = _eventRoot.AddComponent<UnityEngine.Canvas>();
c.renderMode = RenderMode.ScreenSpaceOverlay;
c.sortingOrder = 9999; // Just behind killfeed
CanvasScaler scaler = _eventRoot.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = new Vector2(1920, 1080);
_canvasGroup = _eventRoot.AddComponent<CanvasGroup>();
_canvasGroup.alpha = 0f;
// 2. Main Panel (Positioned at -80 to sit below the KillFeed)
GameObject panel = new GameObject("Background", typeof(RectTransform), typeof(Image));
panel.transform.SetParent(_eventRoot.transform, false);
Image panelImage = panel.GetComponent<Image>();
panelImage.color = new UnityEngine.Color(0, 0, 0, 0.7f);
// Find Valheim Sprite
foreach (Sprite s in Resources.FindObjectsOfTypeAll<Sprite>())
{
if (s.name == "item_background")
{
panelImage.sprite = s;
panelImage.type = Image.Type.Sliced;
break;
}
}
RectTransform pRect = panel.GetComponent<RectTransform>();
pRect.anchorMin = pRect.anchorMax = pRect.pivot = new Vector2(0.5f, 1f);
pRect.anchoredPosition = new Vector2(0, -80); // Lowered to avoid overlap
pRect.sizeDelta = new Vector2(450, 60); // Slightly taller for progress bar
// 3. Vertical Layout for Title + Progress Bar
VerticalLayoutGroup layout = panel.AddComponent<VerticalLayoutGroup>();
layout.childAlignment = UnityEngine.TextAnchor.MiddleCenter;
layout.spacing = 5f;
layout.padding = new RectOffset(10, 10, 5, 5);
// 4. Title Text
_eventTitleText = CreateText(panel.transform, "EVENT ACTIVE", 14, UnityEngine.Color.yellow);
// 5. Progress Bar Background
GameObject barBg = new GameObject("BarBG", typeof(RectTransform), typeof(Image));
barBg.transform.SetParent(panel.transform, false);
barBg.GetComponent<Image>().color = new UnityEngine.Color(0.2f, 0.2f, 0.2f, 0.8f);
barBg.GetComponent<RectTransform>().sizeDelta = new Vector2(400, 10);
// 6. Actual Progress Fill
GameObject fillObj = new GameObject("Fill", typeof(RectTransform), typeof(Image));
fillObj.transform.SetParent(barBg.transform, false);
_progressBar = fillObj.GetComponent<Image>();
_progressBar.color = UnityEngine.Color.red;
_progressBar.type = Image.Type.Filled;
_progressBar.fillMethod = Image.FillMethod.Horizontal;
RectTransform fRect = _progressBar.GetComponent<RectTransform>();
fRect.anchorMin = Vector2.zero;
fRect.anchorMax = Vector2.one;
fRect.sizeDelta = Vector2.zero;
// 7. Counter Text (e.g., 12/20)
_eventProgressText = CreateText(panel.transform, "0 / 0", 10, UnityEngine.Color.white);
_eventRoot.SetActive(false);
}
public static Text CreateText(Transform parent, string content, int size, UnityEngine.Color col)
{
GameObject txtObj = new GameObject("Text", typeof(RectTransform), typeof(Text));
txtObj.transform.SetParent(parent, false);
Text t = txtObj.GetComponent<Text>();
t.text = content;
t.fontSize = size;
t.color = col;
t.alignment = UnityEngine.TextAnchor.MiddleCenter;
t.font = GetValheimFont();
return t;
}
public static UnityEngine.Font GetValheimFont()
{
foreach (UnityEngine.Font f in Resources.FindObjectsOfTypeAll<UnityEngine.Font>())
if (f.name == "AveriaSerifLibre-Bold") return f;
return Resources.GetBuiltinResource<UnityEngine.Font>("Arial.ttf");
}
public static void UpdateDisplay(string title, int current, int total, Color barColor)
{
if (_eventRoot == null) return;
_eventRoot.SetActive(true);
_canvasGroup.alpha = 1f;
_eventTitleText.text = title.ToUpper();
_eventProgressText.text = $"{current} / {total}";
_progressBar.fillAmount = (float)current / total;
_progressBar.color = barColor;
}
public static void Hide() => _canvasGroup.alpha = 0f;
}
}