using OctoCore.Components;
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace OctoCore.UI
{
///
/// UI management module.
/// Handles global UI scaling and screen size detection.
///
[System.Serializable]
public class OctoUI : IOctoModule
{
///
/// Global scale factor for all UI elements.
///
public float GlobalScale = 1;
///
/// Current screen size in pixels.
///
[OctoReadOnly] public Vector2Int ScreenSize;
private Action OnGlobalScaleChange;
private Action OnScreenResize;
public void Init(OctoSettings settings)
{
// Callback OctoEvents
if (OctoEvents.Instance)
OctoEvents.Instance.SetUiInitialized();
}
public void Tick()
{
CheckSize();
}
public void OnSceneChanged(Scene current, Scene next)
{
}
public void OnQuit()
{
}
public void UnsubscribeOnGlobalScaleChange(Action handler)
{
SubscribeOnGlobalScaleChange(handler, false);
}
public void SubscribeOnGlobalScaleChange(Action handler, bool subscribe = true)
{
if (subscribe) OnGlobalScaleChange += handler;
else OnGlobalScaleChange -= handler;
}
public void UnsubscribeOnScreenResize(Action handler)
{
SubscribeOnScreenResize(handler, false);
}
public void SubscribeOnScreenResize(Action handler, bool subscribe = true)
{
if (subscribe) OnScreenResize += handler;
else OnScreenResize -= handler;
}
public void SetGlobalScale(float scale)
{
GlobalScale = scale;
OnGlobalScaleChange?.Invoke(scale);
}
public void CheckSize()
{
if (ScreenSize.x != Screen.width || ScreenSize.y != Screen.height)
{
ScreenSize.x = Screen.width;
ScreenSize.y = Screen.height;
OnScreenResize?.Invoke(ScreenSize);
}
}
public void SetCursor()
{
bool visible = true;
CursorLockMode lockMode = CursorLockMode.None;
if (OctoPlayer.Instance && !OctoPlayer.Instance.IsPaused)
{
visible = true;
lockMode = CursorLockMode.Locked;
}
Cursor.visible = visible;
Cursor.lockState = lockMode;
}
}
}