Update
This commit is contained in:
@@ -72,8 +72,13 @@ namespace Hallucinate.UI
|
||||
await _runner.Shutdown();
|
||||
}
|
||||
|
||||
Debug.Log("[BasicSpawner] Destroying existing runner component.");
|
||||
Destroy(_runner);
|
||||
// Check if it still exists (Unity pseudo-null check)
|
||||
if (_runner != null)
|
||||
{
|
||||
// Only log if it's actually a valid object to destroy
|
||||
// If it's already marked for destruction, Unity == null will be true soon
|
||||
Destroy(_runner);
|
||||
}
|
||||
_runner = null;
|
||||
|
||||
await Task.Yield();
|
||||
@@ -84,6 +89,8 @@ namespace Hallucinate.UI
|
||||
}
|
||||
}
|
||||
|
||||
if (this == null) return; // BasicSpawner itself might be destroyed
|
||||
|
||||
Debug.Log("[BasicSpawner] Creating new NetworkRunner component.");
|
||||
_runner = gameObject.AddComponent<NetworkRunner>();
|
||||
_runner.ProvideInput = true;
|
||||
@@ -118,7 +125,12 @@ namespace Hallucinate.UI
|
||||
|
||||
public async Task<bool> StartHost(string sessionName, string displayName, string password = null)
|
||||
{
|
||||
if (_isStarting) return false;
|
||||
// Wait for any existing startup process (like StartLobby) to finish
|
||||
while (_isStarting)
|
||||
{
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
_isStarting = true;
|
||||
|
||||
try
|
||||
|
||||
@@ -24,15 +24,22 @@ public class PlayerDataManager : NetworkBehaviour
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public override void Despawned(NetworkRunner runner, bool hasState)
|
||||
{
|
||||
if (Instance == this) Instance = null;
|
||||
}
|
||||
|
||||
[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
|
||||
public void RPC_UpdatePlayerMetaData(PlayerRef playerRef, _PlayerMetaData metaData)
|
||||
{
|
||||
if (Object == null || !Object.IsValid) return;
|
||||
Players.Set(playerRef, metaData);
|
||||
}
|
||||
|
||||
[Rpc(RpcSources.All, RpcTargets.StateAuthority)]
|
||||
public void RPC_SetReady(PlayerRef playerRef, bool ready)
|
||||
{
|
||||
if (Object == null || !Object.IsValid) return;
|
||||
if (Players.TryGet(playerRef, out var data))
|
||||
{
|
||||
data.IsReady = ready;
|
||||
@@ -48,6 +55,10 @@ public class PlayerDataManager : NetworkBehaviour
|
||||
|
||||
public bool TryGetPlayerMetaData(PlayerRef playerRef, out _PlayerMetaData metaData)
|
||||
{
|
||||
metaData = default;
|
||||
// Kiểm tra xem object đã được Spawned chưa trước khi truy cập networked property
|
||||
if (Object == null || !Object.IsValid) return false;
|
||||
|
||||
return Players.TryGet(playerRef, out metaData);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
@@ -31,6 +31,8 @@ namespace Hallucinate.UI
|
||||
private Label _guestChatMessage;
|
||||
private TextField _chatInput;
|
||||
|
||||
private bool _isBusy = false;
|
||||
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
@@ -49,6 +51,7 @@ namespace Hallucinate.UI
|
||||
_loungeRoomName = root.Q<Label>("LoungeRoomName");
|
||||
_readyBtn = root.Q<Button>("ReadyBtn");
|
||||
_startBtn = root.Q<Button>("StartBtn");
|
||||
if (_startBtn != null) _startBtn.style.display = DisplayStyle.None; // Default to hidden
|
||||
_hostNameLabel = root.Q<Label>("HostName");
|
||||
_hostStatusLabel = root.Q<Label>("HostReadyStatus");
|
||||
_hostChatBox = root.Q<VisualElement>("HostChatBox");
|
||||
@@ -59,9 +62,10 @@ namespace Hallucinate.UI
|
||||
_guestChatMessage = root.Q<Label>("GuestChatMessage");
|
||||
_chatInput = root.Q<TextField>("ChatInput");
|
||||
|
||||
|
||||
root.Q<Button>("GoToCreateBtn").clicked += ShowCreate;
|
||||
root.Q<Button>("CancelCreateBtn").clicked += ShowJoin;
|
||||
root.Q<Button>("BackToMenuBtn").clicked += async () => await uiManager.Pop();
|
||||
root.Q<Button>("BackToMenuBtn").clicked += async () => { if (_isBusy) return; await uiManager.Pop(); };
|
||||
_confirmCreateBtn = root.Q<Button>("ConfirmCreateBtn");
|
||||
if (_confirmCreateBtn != null) _confirmCreateBtn.clicked += OnCreateRoomClicked;
|
||||
root.Q<Button>("ConfirmJoinBtn").clicked += OnConfirmPasswordClicked;
|
||||
@@ -103,7 +107,8 @@ namespace Hallucinate.UI
|
||||
{
|
||||
var runner = Object.FindFirstObjectByType<NetworkRunner>();
|
||||
if (runner == null) return;
|
||||
bool isHost = sender.PlayerId == 1;
|
||||
var sortedPlayers = runner.ActivePlayers.OrderBy(p => p.PlayerId).ToList();
|
||||
bool isHost = sortedPlayers.Count > 0 && sender == sortedPlayers[0];
|
||||
if (isHost) ShowChatBubble(_hostChatBox, _hostChatMessage, message);
|
||||
else ShowChatBubble(_guestChatBox, _guestChatMessage, message);
|
||||
}
|
||||
@@ -120,7 +125,13 @@ namespace Hallucinate.UI
|
||||
private void ApplyLocalization() { if (LocalizationManager.Instance == null) return; }
|
||||
private string GetT(string key) => LocalizationManager.Instance != null ? LocalizationManager.Instance.GetLocalizedString(key) : key;
|
||||
public void SetRoomTemplate(VisualTreeAsset template) => _roomItemTemplate = template;
|
||||
public override async Task PlayTransitionIn() { await base.PlayTransitionIn(); ShowJoin(); }
|
||||
|
||||
public override async Task PlayTransitionIn()
|
||||
{
|
||||
_isBusy = false;
|
||||
await base.PlayTransitionIn();
|
||||
ShowJoin();
|
||||
}
|
||||
|
||||
public void ShowJoin()
|
||||
{
|
||||
@@ -158,18 +169,39 @@ namespace Hallucinate.UI
|
||||
|
||||
private async void OnCreateRoomClicked()
|
||||
{
|
||||
if (_isBusy) return;
|
||||
_isBusy = true;
|
||||
|
||||
if (_confirmCreateBtn != null) _confirmCreateBtn.SetEnabled(false);
|
||||
if (_createErrorLabel != null) _createErrorLabel.style.display = DisplayStyle.None;
|
||||
|
||||
var spawner = BasicSpawner.Instance;
|
||||
if (spawner == null) { ShowCreateError("System Error: Spawner missing. Please re-enter the Lobby."); return; }
|
||||
if (spawner == null)
|
||||
{
|
||||
ShowCreateError("System Error: Spawner missing. Please re-enter the Lobby.");
|
||||
_isBusy = false;
|
||||
return;
|
||||
}
|
||||
|
||||
string id = (_roomIDInput != null && !string.IsNullOrEmpty(_roomIDInput.value)) ? _roomIDInput.value.Trim() : "ROOM_" + Random.Range(1000, 9999).ToString();
|
||||
string name = (_roomNameInput != null && !string.IsNullOrEmpty(_roomNameInput.value)) ? _roomNameInput.value.Trim() : id;
|
||||
string pass = (_passToggle != null && _passToggle.value && _roomPassInput != null) ? _roomPassInput.value : null;
|
||||
try {
|
||||
|
||||
try
|
||||
{
|
||||
bool success = await spawner.StartHost(id, name, pass);
|
||||
if (success) ShowLounge(name);
|
||||
else ShowCreateError("Failed to create room. ID might be taken.");
|
||||
} catch (System.Exception ex) { ShowCreateError("Network Error: " + ex.Message); }
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
ShowCreateError("Network Error: " + ex.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_isBusy = false;
|
||||
if (_confirmCreateBtn != null) _confirmCreateBtn.SetEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void ShowCreateError(string message)
|
||||
@@ -203,6 +235,8 @@ namespace Hallucinate.UI
|
||||
|
||||
private async void OnRoomItemClicked(SessionInfo session)
|
||||
{
|
||||
if (_isBusy) return;
|
||||
|
||||
bool needsPass = session.Properties.ContainsKey("pw");
|
||||
if (needsPass)
|
||||
{
|
||||
@@ -216,16 +250,49 @@ namespace Hallucinate.UI
|
||||
|
||||
private async void OnConfirmPasswordClicked()
|
||||
{
|
||||
if (_selectedSession == null) return;
|
||||
if (_isBusy || _selectedSession == null) return;
|
||||
_isBusy = true;
|
||||
|
||||
string pass = _joinPassInput != null ? _joinPassInput.value : "";
|
||||
if (_passOverlay != null) _passOverlay.style.display = DisplayStyle.None;
|
||||
await JoinRoom(_selectedSession.Name, pass);
|
||||
_isBusy = false;
|
||||
}
|
||||
|
||||
private async Task JoinRoom(string sessionName, string password) { if (BasicSpawner.Instance != null) { if (await BasicSpawner.Instance.StartClient(sessionName, password)) ShowLounge(sessionName); } }
|
||||
private void OnReadyClicked() { var runner = Object.FindFirstObjectByType<NetworkRunner>(); if (runner != null && _playerDataManager != null && _playerDataManager.TryGetPlayerMetaData(runner.LocalPlayer, out var myData)) _playerDataManager.RPC_SetReady(runner.LocalPlayer, !myData.IsReady); }
|
||||
private void OnStartClicked() { BasicSpawner.Instance?.StartGame(); }
|
||||
private async void OnLeaveLoungeClicked() { var runner = Object.FindFirstObjectByType<NetworkRunner>(); if (runner != null) await runner.Shutdown(); if (_playerDataManager != null) _playerDataManager.OnChatMessageReceived -= OnChatMessageReceived; ShowJoin(); }
|
||||
private async Task JoinRoom(string sessionName, string password)
|
||||
{
|
||||
if (BasicSpawner.Instance != null)
|
||||
{
|
||||
if (await BasicSpawner.Instance.StartClient(sessionName, password)) ShowLounge(sessionName);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnReadyClicked()
|
||||
{
|
||||
if (_isBusy) return;
|
||||
var runner = Object.FindFirstObjectByType<NetworkRunner>();
|
||||
if (runner != null && _playerDataManager != null && _playerDataManager.TryGetPlayerMetaData(runner.LocalPlayer, out var myData))
|
||||
_playerDataManager.RPC_SetReady(runner.LocalPlayer, !myData.IsReady);
|
||||
}
|
||||
|
||||
private void OnStartClicked()
|
||||
{
|
||||
if (_isBusy) return;
|
||||
BasicSpawner.Instance?.StartGame();
|
||||
}
|
||||
|
||||
private async void OnLeaveLoungeClicked()
|
||||
{
|
||||
if (_isBusy) return;
|
||||
_isBusy = true;
|
||||
|
||||
var runner = Object.FindFirstObjectByType<NetworkRunner>();
|
||||
if (runner != null) await runner.Shutdown(false);
|
||||
if (_playerDataManager != null) _playerDataManager.OnChatMessageReceived -= OnChatMessageReceived;
|
||||
ShowJoin();
|
||||
|
||||
_isBusy = false;
|
||||
}
|
||||
|
||||
public override void Update() { if (_loungeContainer != null && _loungeContainer.style.display == DisplayStyle.Flex) UpdateLoungeUI(); }
|
||||
|
||||
@@ -234,24 +301,44 @@ namespace Hallucinate.UI
|
||||
var spawner = BasicSpawner.Instance;
|
||||
if (spawner == null) return;
|
||||
var runner = spawner.Runner;
|
||||
if (runner == null || _playerDataManager == null || _playerDataManager.Object == null || !_playerDataManager.Object.IsValid) return;
|
||||
if (runner == null) return;
|
||||
|
||||
PlayerRef hostRef = PlayerRef.None;
|
||||
PlayerRef guestRef = PlayerRef.None;
|
||||
// 1. Strict Visibility Check
|
||||
bool isHost = runner.IsServer;
|
||||
if (_startBtn != null)
|
||||
{
|
||||
_startBtn.style.display = isHost ? DisplayStyle.Flex : DisplayStyle.None;
|
||||
}
|
||||
|
||||
// 2. PlayerDataManager Sync
|
||||
if (_playerDataManager == null || _playerDataManager.Object == null || !_playerDataManager.Object.IsValid)
|
||||
{
|
||||
_playerDataManager = PlayerDataManager.Instance;
|
||||
if (_playerDataManager != null)
|
||||
{
|
||||
_playerDataManager.OnChatMessageReceived += OnChatMessageReceived;
|
||||
}
|
||||
else return; // Still waiting for synchronization
|
||||
}
|
||||
|
||||
// 3. Identify Players
|
||||
var sortedPlayers = runner.ActivePlayers.OrderBy(p => p.PlayerId).ToList();
|
||||
if (sortedPlayers.Count > 0) hostRef = sortedPlayers[0];
|
||||
if (sortedPlayers.Count > 1) guestRef = sortedPlayers[1];
|
||||
PlayerRef hostRef = sortedPlayers.Count > 0 ? sortedPlayers[0] : PlayerRef.None;
|
||||
PlayerRef guestRef = sortedPlayers.Count > 1 ? sortedPlayers[1] : PlayerRef.None;
|
||||
|
||||
if (runner.SessionInfo != null && runner.SessionInfo.Properties.TryGetValue("rn", out var rnProp)) _loungeRoomName.text = rnProp.ToString().ToUpper();
|
||||
|
||||
// Host Display
|
||||
if (hostRef != PlayerRef.None && _playerDataManager.TryGetPlayerMetaData(hostRef, out var hostData))
|
||||
{
|
||||
_hostNameLabel.text = hostData.Name.ToString().ToUpper();
|
||||
_hostStatusLabel.text = hostData.IsReady ? GetT("LOBBY_READY") : GetT("LOBBY_NOT_READY");
|
||||
string readyStatus = hostData.IsReady ? GetT("LOBBY_READY") : GetT("LOBBY_NOT_READY");
|
||||
_hostStatusLabel.text = $"{GetT("LOBBY_HOST_LABEL")} - {readyStatus}";
|
||||
_hostStatusLabel.style.color = hostData.IsReady ? Color.green : Color.red;
|
||||
}
|
||||
else if (hostRef != PlayerRef.None) { _hostNameLabel.text = GetT("LOBBY_SYNCING"); _hostStatusLabel.text = "-"; }
|
||||
else { _hostNameLabel.text = GetT("LOBBY_SYNCING"); _hostStatusLabel.text = "-"; }
|
||||
|
||||
// Guest Display
|
||||
if (guestRef != PlayerRef.None && _playerDataManager.TryGetPlayerMetaData(guestRef, out var guestData))
|
||||
{
|
||||
_guestNameLabel.text = guestData.Name.ToString().ToUpper();
|
||||
@@ -261,17 +348,55 @@ namespace Hallucinate.UI
|
||||
else if (runner.ActivePlayers.Count() >= 2) { _guestNameLabel.text = GetT("LOBBY_SYNCING"); _guestStatusLabel.text = "-"; }
|
||||
else { _guestNameLabel.text = GetT("LOBBY_WAITING_LABEL"); _guestStatusLabel.text = "-"; _guestStatusLabel.style.color = Color.gray; }
|
||||
|
||||
bool allReady = true;
|
||||
int playerCount = 0;
|
||||
foreach (var p in runner.ActivePlayers) { playerCount++; if (_playerDataManager.TryGetPlayerMetaData(p, out var data)) { if (!data.IsReady) allReady = false; } else allReady = false; }
|
||||
|
||||
bool isHost = runner.LocalPlayer == hostRef;
|
||||
if (_startBtn != null) { _startBtn.text = GetT("LOBBY_START_BTN"); _startBtn.style.display = isHost ? DisplayStyle.Flex : DisplayStyle.None; _startBtn.SetEnabled(allReady && playerCount >= 2); }
|
||||
|
||||
if (_readyBtn != null && _playerDataManager.TryGetPlayerMetaData(runner.LocalPlayer, out var myData))
|
||||
// 4. Start Button Logic (Host Only)
|
||||
if (_startBtn != null && isHost)
|
||||
{
|
||||
if (myData.IsReady) { _readyBtn.text = GetT("LOBBY_UNREADY_BTN"); _readyBtn.style.backgroundColor = new StyleColor(Color.green); _readyBtn.style.color = new StyleColor(Color.black); }
|
||||
else { _readyBtn.text = GetT("LOBBY_READY_BTN"); _readyBtn.style.backgroundColor = new StyleColor(new Color(0.2f, 0.2f, 0.2f, 0.8f)); _readyBtn.style.color = new StyleColor(Color.white); }
|
||||
bool allReady = true;
|
||||
int playerCount = 0;
|
||||
foreach (var p in runner.ActivePlayers)
|
||||
{
|
||||
playerCount++;
|
||||
if (_playerDataManager.TryGetPlayerMetaData(p, out var data))
|
||||
{
|
||||
if (!data.IsReady) allReady = false;
|
||||
}
|
||||
else allReady = false;
|
||||
}
|
||||
|
||||
bool canStart = allReady && playerCount >= 2;
|
||||
_startBtn.SetEnabled(canStart);
|
||||
_startBtn.text = GetT("LOBBY_START_BTN");
|
||||
|
||||
if (canStart)
|
||||
{
|
||||
_startBtn.style.backgroundColor = new StyleColor(Color.green);
|
||||
_startBtn.style.color = new StyleColor(Color.black);
|
||||
}
|
||||
else
|
||||
{
|
||||
_startBtn.style.backgroundColor = new StyleColor(new Color(0.2f, 0.2f, 0.2f, 0.8f));
|
||||
_startBtn.style.color = new StyleColor(new Color(1f, 1f, 1f, 0.5f));
|
||||
}
|
||||
}
|
||||
|
||||
// Ready Button Logic (Everyone)
|
||||
if (_readyBtn != null)
|
||||
{
|
||||
if (_playerDataManager.TryGetPlayerMetaData(runner.LocalPlayer, out var myData))
|
||||
{
|
||||
if (myData.IsReady)
|
||||
{
|
||||
_readyBtn.text = GetT("LOBBY_UNREADY_BTN");
|
||||
_readyBtn.style.backgroundColor = new StyleColor(Color.green);
|
||||
_readyBtn.style.color = new StyleColor(Color.black);
|
||||
}
|
||||
else
|
||||
{
|
||||
_readyBtn.text = GetT("LOBBY_READY_BTN");
|
||||
_readyBtn.style.backgroundColor = new StyleColor(new Color(0.2f, 0.2f, 0.2f, 0.8f));
|
||||
_readyBtn.style.color = new StyleColor(Color.white);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,8 @@ namespace Hallucinate.UI
|
||||
private Tween _rotationTween;
|
||||
private Texture2D _currentIcon;
|
||||
|
||||
private bool _isBusy = false;
|
||||
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
@@ -42,11 +44,11 @@ namespace Hallucinate.UI
|
||||
_logo.RegisterCallback<ClickEvent>(OnLogoClicked);
|
||||
|
||||
var settingsBtn = root.Q<Button>("SettingsBtn");
|
||||
if (settingsBtn != null) settingsBtn.clicked += () => uiManager.ToggleSettings();
|
||||
if (settingsBtn != null) settingsBtn.clicked += () => { if (_isBusy) return; uiManager.ToggleSettings(); };
|
||||
|
||||
root.Q<Button>("JoinBtn").clicked += async () => await uiManager.Push<LobbyController>();
|
||||
root.Q<Button>("CreateBtn").clicked += async () => await uiManager.Push<LobbyController>();
|
||||
root.Q<Button>("ProfileBtn").clicked += async () => await uiManager.Push<ProfileController>();
|
||||
root.Q<Button>("JoinBtn").clicked += async () => { if (_isBusy) return; _isBusy = true; await uiManager.Push<LobbyController>(); };
|
||||
root.Q<Button>("CreateBtn").clicked += async () => { if (_isBusy) return; _isBusy = true; await uiManager.Push<LobbyController>(); };
|
||||
root.Q<Button>("ProfileBtn").clicked += async () => { if (_isBusy) return; _isBusy = true; await uiManager.Push<ProfileController>(); };
|
||||
root.Q<Button>("ExitBtn").clicked += () => Application.Quit();
|
||||
|
||||
// Đăng ký Localization
|
||||
@@ -142,6 +144,7 @@ namespace Hallucinate.UI
|
||||
|
||||
public override async Task PlayTransitionIn()
|
||||
{
|
||||
_isBusy = false;
|
||||
_lastInteractionTime = Time.time;
|
||||
_currentState = MenuState.Idle;
|
||||
ResetLogoPosition();
|
||||
@@ -167,9 +170,14 @@ namespace Hallucinate.UI
|
||||
|
||||
private async void OnLogoClicked(ClickEvent evt)
|
||||
{
|
||||
if (_isBusy) return;
|
||||
_lastInteractionTime = Time.time;
|
||||
if (_currentState == MenuState.Idle) TransitionToRibbon();
|
||||
else await uiManager.Push<LobbyController>();
|
||||
else
|
||||
{
|
||||
_isBusy = true;
|
||||
await uiManager.Push<LobbyController>();
|
||||
}
|
||||
}
|
||||
|
||||
private void TransitionToRibbon()
|
||||
|
||||
76
Assets/Scripts/UI/PauseMenuController.cs
Normal file
76
Assets/Scripts/UI/PauseMenuController.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using UnityEngine.UIElements;
|
||||
using System.Threading.Tasks;
|
||||
using OnlyScove.Scripts;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Hallucinate.UI
|
||||
{
|
||||
public class PauseMenuController : BaseUIController
|
||||
{
|
||||
private Button _resumeBtn;
|
||||
private Button _quitBtn;
|
||||
|
||||
public override void Initialize(VisualElement uxmlRoot, UIManager manager)
|
||||
{
|
||||
base.Initialize(uxmlRoot, manager);
|
||||
|
||||
_resumeBtn = root.Q<Button>("ResumeBtn");
|
||||
_quitBtn = root.Q<Button>("QuitBtn");
|
||||
|
||||
if (_resumeBtn != null) _resumeBtn.clicked += OnResumeClicked;
|
||||
if (_quitBtn != null) _quitBtn.clicked += OnQuitClicked;
|
||||
|
||||
ApplyLocalization();
|
||||
if (LocalizationManager.Instance != null)
|
||||
LocalizationManager.Instance.OnLanguageChanged += ApplyLocalization;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (LocalizationManager.Instance != null)
|
||||
LocalizationManager.Instance.OnLanguageChanged -= ApplyLocalization;
|
||||
}
|
||||
|
||||
private void ApplyLocalization()
|
||||
{
|
||||
if (_resumeBtn != null) _resumeBtn.text = GetLoc("PAUSE_RESUME");
|
||||
if (_quitBtn != null) _quitBtn.text = GetLoc("PAUSE_QUIT");
|
||||
|
||||
var title = root.Q<Label>("PauseTitle");
|
||||
if (title != null) title.text = GetLoc("PAUSE_TITLE");
|
||||
}
|
||||
|
||||
private void OnResumeClicked()
|
||||
{
|
||||
uiManager.TogglePauseMenu();
|
||||
}
|
||||
|
||||
private void OnQuitClicked()
|
||||
{
|
||||
Debug.Log("[PauseMenu] Quit clicked - shutting down runner.");
|
||||
if (BasicSpawner.Instance != null && BasicSpawner.Instance.Runner != null)
|
||||
{
|
||||
BasicSpawner.Instance.Runner.Shutdown();
|
||||
}
|
||||
else
|
||||
{
|
||||
uiManager.OnBackToMenu();
|
||||
}
|
||||
}
|
||||
|
||||
public override async Task PlayTransitionIn()
|
||||
{
|
||||
Show();
|
||||
root.style.opacity = 0;
|
||||
PrimeTween.Tween.Custom(0f, 1f, duration: 0.2f, onValueChange: val => root.style.opacity = val);
|
||||
await Task.Delay(200);
|
||||
}
|
||||
|
||||
public override async Task PlayTransitionOut()
|
||||
{
|
||||
PrimeTween.Tween.Custom(1f, 0f, duration: 0.2f, onValueChange: val => root.style.opacity = val);
|
||||
await Task.Delay(200);
|
||||
Hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/UI/PauseMenuController.cs.meta
Normal file
2
Assets/Scripts/UI/PauseMenuController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a8be766526476e44ea7c5e19567f2568
|
||||
@@ -39,6 +39,9 @@ namespace Hallucinate.UI
|
||||
private Action<float> _hoveredOnChanged;
|
||||
private float _sliderMin, _sliderMax;
|
||||
|
||||
// Audio Slider Tracking for Sync
|
||||
private readonly Dictionary<string, (Slider slider, TextField input)> _audioSliders = new Dictionary<string, (Slider slider, TextField input)>();
|
||||
|
||||
// Osu-style Volume Overlay
|
||||
private VisualElement _volumeContainer;
|
||||
private VisualElement _masterRing;
|
||||
@@ -303,6 +306,7 @@ namespace Hallucinate.UI
|
||||
_sectionHeaders["SOUND"] = header;
|
||||
_content.Add(header);
|
||||
|
||||
_audioSliders.Clear();
|
||||
_content.Add(CreateSubSection("AUDIO_VOLUMES"));
|
||||
_content.Add(CreateAudioSlider(GetT("MASTER"), "MasterVolume"));
|
||||
_content.Add(CreateAudioSlider(GetT("MUSIC"), "MusicVolume"));
|
||||
@@ -456,20 +460,31 @@ namespace Hallucinate.UI
|
||||
evt.StopPropagation();
|
||||
}
|
||||
|
||||
private void UpdateMasterVolume(float delta)
|
||||
{
|
||||
_masterVol = Mathf.Clamp(_masterVol + delta, 0f, 100f);
|
||||
PlayerPrefs.SetFloat("MasterVolume", _masterVol);
|
||||
AudioManager.Instance?.SetVolume("MasterVolume", _masterVol);
|
||||
_masterVolLabel.text = $"{Mathf.RoundToInt(_masterVol)}%";
|
||||
}
|
||||
private void UpdateMasterVolume(float delta) => UpdateVolume("MasterVolume", _masterVol + delta);
|
||||
|
||||
private void UpdateSubVolume(string key, float delta)
|
||||
private void UpdateSubVolume(string key, float delta) => UpdateVolume(key, PlayerPrefs.GetFloat(key, 80f) + delta);
|
||||
|
||||
private void UpdateVolume(string key, float volume, bool updateSlider = true)
|
||||
{
|
||||
float newVal = Mathf.Clamp(PlayerPrefs.GetFloat(key, 80f) + delta, 0f, 100f);
|
||||
PlayerPrefs.SetFloat(key, newVal);
|
||||
AudioManager.Instance?.SetVolume(key, newVal);
|
||||
if (_subRings.TryGetValue(key, out var data)) data.label.text = $"{Mathf.RoundToInt(newVal)}%";
|
||||
volume = Mathf.Clamp(volume, 0f, 100f);
|
||||
PlayerPrefs.SetFloat(key, volume);
|
||||
AudioManager.Instance?.SetVolume(key, volume);
|
||||
|
||||
if (key == "MasterVolume")
|
||||
{
|
||||
_masterVol = volume;
|
||||
if (_masterVolLabel != null) _masterVolLabel.text = $"{Mathf.RoundToInt(volume)}%";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_subRings.TryGetValue(key, out var data)) data.label.text = $"{Mathf.RoundToInt(volume)}%";
|
||||
}
|
||||
|
||||
if (updateSlider && _audioSliders.TryGetValue(key, out var sliderData))
|
||||
{
|
||||
sliderData.slider.SetValueWithoutNotify(volume);
|
||||
sliderData.input.value = volume.ToString("F1");
|
||||
}
|
||||
}
|
||||
|
||||
private async void ShowVolumeOverlay()
|
||||
@@ -508,12 +523,15 @@ namespace Hallucinate.UI
|
||||
return label;
|
||||
}
|
||||
|
||||
private VisualElement CreateSliderWithInput(string labelText, float min, float max, float startVal, Action<float> OnValueChanged)
|
||||
private VisualElement CreateSliderWithInput(string labelText, float min, float max, float startVal, Action<float> OnValueChanged, string audioKey = null)
|
||||
{
|
||||
var row = new VisualElement { style = { flexDirection = FlexDirection.Row, alignItems = Align.Center, marginTop = 5, marginBottom = 5 } };
|
||||
var label = new Label(labelText) { style = { width = Length.Percent(35) } }; label.AddToClassList("text-body");
|
||||
var slider = new Slider(min, max) { value = startVal, style = { flexGrow = 1 } };
|
||||
var input = new TextField { value = startVal.ToString("F1"), style = { width = 50, marginLeft = 10 } }; input.AddToClassList("input-field");
|
||||
|
||||
if (audioKey != null) _audioSliders[audioKey] = (slider, input);
|
||||
|
||||
slider.RegisterCallback<PointerEnterEvent>(evt => { _hoveredSlider = slider; _hoveredOnChanged = OnValueChanged; _sliderMin = min; _sliderMax = max; });
|
||||
slider.RegisterCallback<PointerLeaveEvent>(evt => { if (_hoveredSlider == slider) { _hoveredSlider = null; _hoveredOnChanged = null; } });
|
||||
slider.RegisterValueChangedCallback(evt => { float val = Mathf.Round(evt.newValue * 10f) / 10f; if (input.panel?.focusController?.focusedElement != input.ElementAt(0)) input.value = val.ToString("F1"); OnValueChanged?.Invoke(val); });
|
||||
@@ -524,12 +542,10 @@ namespace Hallucinate.UI
|
||||
private VisualElement CreateAudioSlider(string label, string prefKey)
|
||||
{
|
||||
var sliderRow = CreateSliderWithInput(label, 0, 100, PlayerPrefs.GetFloat(prefKey, 80), val => {
|
||||
PlayerPrefs.SetFloat(prefKey, val); AudioManager.Instance?.SetVolume(prefKey, val);
|
||||
});
|
||||
UpdateVolume(prefKey, val, false);
|
||||
}, prefKey);
|
||||
sliderRow.RegisterCallback<WheelEvent>(evt => {
|
||||
float newVal = Mathf.Clamp(PlayerPrefs.GetFloat(prefKey, 80f) - (evt.delta.y * 2f), 0f, 100f);
|
||||
PlayerPrefs.SetFloat(prefKey, newVal); AudioManager.Instance?.SetVolume(prefKey, newVal);
|
||||
var slider = sliderRow.Q<Slider>(); if (slider != null) slider.value = newVal;
|
||||
UpdateVolume(prefKey, PlayerPrefs.GetFloat(prefKey, 80f) - (evt.delta.y * 2f));
|
||||
});
|
||||
return sliderRow;
|
||||
}
|
||||
|
||||
@@ -49,12 +49,14 @@ namespace Hallucinate.UI
|
||||
[SerializeField] private VisualTreeAsset profileTemplate;
|
||||
[SerializeField] private VisualTreeAsset settingsTemplate;
|
||||
[SerializeField] private VisualTreeAsset hudTemplate;
|
||||
[SerializeField] private VisualTreeAsset pauseMenuTemplate;
|
||||
[SerializeField] private StyleSheet globalStyleSheet;
|
||||
|
||||
private LoginController _loginController;
|
||||
private MainMenuController _mainMenuController;
|
||||
private LobbyController _lobbyController;
|
||||
private SettingsController _settingsController;
|
||||
private PauseMenuController _pauseMenuController;
|
||||
|
||||
// Osu Trail Pooling
|
||||
private const int MAX_TRAIL_PARTICLES = 60;
|
||||
@@ -65,6 +67,9 @@ namespace Hallucinate.UI
|
||||
private bool _isSettingsOpen = false;
|
||||
public bool IsSettingsOpen => _isSettingsOpen;
|
||||
|
||||
private bool _isPauseMenuOpen = false;
|
||||
public bool IsPauseMenuOpen => _isPauseMenuOpen;
|
||||
|
||||
private const string UI_SCALE_KEY = "UIScale";
|
||||
|
||||
#if UNITY_EDITOR
|
||||
@@ -242,7 +247,43 @@ namespace Hallucinate.UI
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleCancel() { if (_isSettingsOpen) ToggleSettings(); }
|
||||
private void HandleCancel()
|
||||
{
|
||||
if (_isSettingsOpen) ToggleSettings();
|
||||
else if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "Main Scene")
|
||||
{
|
||||
TogglePauseMenu();
|
||||
}
|
||||
}
|
||||
|
||||
public async void TogglePauseMenu()
|
||||
{
|
||||
if (_pauseMenuController == null) return;
|
||||
if (!_isPauseMenuOpen)
|
||||
{
|
||||
_isPauseMenuOpen = true;
|
||||
_pauseMenuController.Root.BringToFront();
|
||||
if (_cursorLayer != null) _cursorLayer.BringToFront();
|
||||
|
||||
// Unlock cursor when menu is open
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.None;
|
||||
UnityEngine.Cursor.visible = false;
|
||||
|
||||
await _pauseMenuController.PlayTransitionIn();
|
||||
}
|
||||
else
|
||||
{
|
||||
_isPauseMenuOpen = false;
|
||||
|
||||
// Re-lock cursor when menu is closed
|
||||
if (!_isSettingsOpen)
|
||||
{
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
|
||||
await _pauseMenuController.PlayTransitionOut();
|
||||
}
|
||||
}
|
||||
|
||||
public async void ToggleSettings()
|
||||
{
|
||||
@@ -354,6 +395,7 @@ namespace Hallucinate.UI
|
||||
RegisterController<ProfileController>(profileTemplate);
|
||||
_settingsController = RegisterController<SettingsController>(settingsTemplate);
|
||||
RegisterController<HUDController>(hudTemplate);
|
||||
_pauseMenuController = RegisterController<PauseMenuController>(pauseMenuTemplate);
|
||||
_loginController = RegisterController<LoginController>(loginTemplate);
|
||||
}
|
||||
catch (Exception e) { Debug.LogError($"[UIManager] Failed to initialize controllers: {e}"); }
|
||||
|
||||
Reference in New Issue
Block a user