(1.5.6) Day/Time UI + New AutoDoor Logic
This commit is contained in:
96
MxValheim/Patch/HUD/Clock.cs
Normal file
96
MxValheim/Patch/HUD/Clock.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace MxValheim.Patch.HUD
|
||||
{
|
||||
internal class Clock
|
||||
{
|
||||
public static GameObject m_canvasObject;
|
||||
public static Text m_textComponent;
|
||||
|
||||
// This replaces the missing GetTimeString() method
|
||||
public string GetFormattedTime()
|
||||
{
|
||||
// Get fractional day (0.0 to 1.0)
|
||||
float fraction = EnvMan.instance.GetDayFraction();
|
||||
|
||||
// Convert to hours and minutes
|
||||
float totalHours = fraction * 24f;
|
||||
int hours = Mathf.FloorToInt(totalHours);
|
||||
int minutes = Mathf.FloorToInt((totalHours - hours) * 60f);
|
||||
|
||||
return $"{hours:00}:{minutes:00}";
|
||||
}
|
||||
|
||||
public void CreateOverlay()
|
||||
{
|
||||
// 1. Root Canvas
|
||||
m_canvasObject = new GameObject("ModdedCanvas");
|
||||
Canvas canvas = m_canvasObject.AddComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
||||
canvas.sortingOrder = 999;
|
||||
|
||||
// 2. Background Panel
|
||||
GameObject panelObj = new GameObject("TextBackground");
|
||||
panelObj.transform.SetParent(m_canvasObject.transform, false);
|
||||
|
||||
Image panelImage = panelObj.AddComponent<Image>();
|
||||
panelImage.color = new Color(0, 0, 0, 0.5f); // 50% transparent black
|
||||
|
||||
RectTransform panelRect = panelObj.GetComponent<RectTransform>();
|
||||
panelRect.anchorMin = new Vector2(0.5f, 1.0f); // Top Center
|
||||
panelRect.anchorMax = new Vector2(0.5f, 1.0f);
|
||||
panelRect.pivot = new Vector2(0.5f, 1.0f);
|
||||
panelRect.anchoredPosition = new Vector2(0, -5); // 5px gap from top
|
||||
panelRect.sizeDelta = new Vector2(180, 35); // Width and Height of the bar
|
||||
|
||||
// 3. Text (Attached to Panel)
|
||||
GameObject textObj = new GameObject("ModdedText");
|
||||
textObj.transform.SetParent(panelObj.transform, false);
|
||||
|
||||
m_textComponent = textObj.AddComponent<Text>();
|
||||
// Attempt to find Valheim's specific font, fallback to Arial if not found
|
||||
Font valheimFont = null;
|
||||
foreach (Font f in Resources.FindObjectsOfTypeAll<Font>())
|
||||
{
|
||||
if (f.name == "AveriaSerifLibre-Bold")
|
||||
{
|
||||
valheimFont = f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Corrected fallback for older Unity versions used in Valheim
|
||||
if (valheimFont == null)
|
||||
{
|
||||
valheimFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||||
}
|
||||
|
||||
m_textComponent.font = valheimFont;
|
||||
m_textComponent.fontSize = 18;
|
||||
m_textComponent.color = Color.white;
|
||||
m_textComponent.alignment = TextAnchor.MiddleCenter;
|
||||
m_textComponent.supportRichText = true;
|
||||
|
||||
// Add Shadow so it's visible in snow
|
||||
var shadow = textObj.AddComponent<Shadow>();
|
||||
shadow.effectColor = Color.black;
|
||||
shadow.effectDistance = new Vector2(2, 2);
|
||||
|
||||
// FIX: Correct way to make text fill the parent panel
|
||||
RectTransform textRect = textObj.GetComponent<RectTransform>();
|
||||
textRect.anchorMin = Vector2.zero; // (0, 0)
|
||||
textRect.anchorMax = Vector2.one; // (1, 1)
|
||||
textRect.pivot = new Vector2(0.5f, 0.5f);
|
||||
|
||||
// Resetting these to zero ensures the text box matches the panel exactly
|
||||
textRect.offsetMin = Vector2.zero;
|
||||
textRect.offsetMax = Vector2.zero;
|
||||
}
|
||||
}
|
||||
}
|
||||
75
MxValheim/Patch/HUD/Splash.cs
Normal file
75
MxValheim/Patch/HUD/Splash.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using HarmonyLib;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace MxValheim.Patch.HUD
|
||||
{
|
||||
internal class Splash
|
||||
{
|
||||
public static GameObject m_canvasObject;
|
||||
public static Text m_textComponent;
|
||||
|
||||
public void CreateOverlay()
|
||||
{
|
||||
// 1. Create a dedicated Canvas for our mod
|
||||
m_canvasObject = new GameObject("ModdedCanvas");
|
||||
Canvas canvas = m_canvasObject.AddComponent<Canvas>();
|
||||
canvas.renderMode = RenderMode.ScreenSpaceOverlay; // Always stays on top
|
||||
canvas.sortingOrder = 100; // Higher than standard HUD
|
||||
|
||||
m_canvasObject.AddComponent<CanvasScaler>();
|
||||
m_canvasObject.AddComponent<GraphicRaycaster>();
|
||||
|
||||
// 2. Create the Text Object
|
||||
GameObject textObj = new GameObject("ModdedText");
|
||||
textObj.transform.SetParent(m_canvasObject.transform, false);
|
||||
|
||||
m_textComponent = textObj.AddComponent<Text>();
|
||||
|
||||
// Attempt to find Valheim's specific font, fallback to Arial if not found
|
||||
Font valheimFont = null;
|
||||
foreach (Font f in Resources.FindObjectsOfTypeAll<Font>())
|
||||
{
|
||||
if (f.name == "AveriaSerifLibre-Bold")
|
||||
{
|
||||
valheimFont = f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Corrected fallback for older Unity versions used in Valheim
|
||||
if (valheimFont == null)
|
||||
{
|
||||
valheimFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
|
||||
}
|
||||
|
||||
m_textComponent.font = valheimFont;
|
||||
m_textComponent.fontSize = 24;
|
||||
m_textComponent.color = Color.yellow; // Bright yellow to stand out
|
||||
m_textComponent.alignment = TextAnchor.UpperRight;
|
||||
m_textComponent.supportRichText = true;
|
||||
|
||||
// This prevents the text from vanishing if the box is too small
|
||||
m_textComponent.horizontalOverflow = HorizontalWrapMode.Overflow;
|
||||
m_textComponent.verticalOverflow = VerticalWrapMode.Overflow;
|
||||
|
||||
// Add Shadow so it's visible in snow
|
||||
var shadow = textObj.AddComponent<Shadow>();
|
||||
shadow.effectColor = Color.black;
|
||||
shadow.effectDistance = new Vector2(2, 2);
|
||||
|
||||
// 3. Position it
|
||||
RectTransform rect = textObj.GetComponent<RectTransform>();
|
||||
rect.anchorMin = new Vector2(1, 1);
|
||||
rect.anchorMax = new Vector2(1, 1);
|
||||
rect.pivot = new Vector2(1, 1);
|
||||
rect.anchoredPosition = new Vector2(-10, -5); // Top Right
|
||||
rect.sizeDelta = new Vector2(300, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user