Main menu
This commit is contained in:
443
UI/Scripts/MainMenuController.cs
Normal file
443
UI/Scripts/MainMenuController.cs
Normal file
@@ -0,0 +1,443 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace MegaKoop.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// Controller pro Main Menu s UI Toolkit
|
||||
/// </summary>
|
||||
public class MainMenuController : MonoBehaviour
|
||||
{
|
||||
[Header("UI Document")]
|
||||
[SerializeField] private UIDocument uiDocument;
|
||||
|
||||
[Header("Scene Names")]
|
||||
[SerializeField] private string gameSceneName = "GameScene";
|
||||
|
||||
[Header("Audio")]
|
||||
[SerializeField] private AudioSource audioSource;
|
||||
[SerializeField] private AudioClip buttonClickSound;
|
||||
[SerializeField] private AudioClip buttonHoverSound;
|
||||
|
||||
// Root element
|
||||
private VisualElement root;
|
||||
|
||||
// Main Menu Elements
|
||||
private Button newGameButton;
|
||||
private Button continueButton;
|
||||
private Button multiplayerButton;
|
||||
private Button settingsButton;
|
||||
private Button creditsButton;
|
||||
private Button quitButton;
|
||||
|
||||
// Settings Panel Elements
|
||||
private VisualElement settingsPanel;
|
||||
private DropdownField qualityDropdown;
|
||||
private Toggle fullscreenToggle;
|
||||
private DropdownField resolutionDropdown;
|
||||
private Slider masterVolumeSlider;
|
||||
private Slider musicVolumeSlider;
|
||||
private Slider sfxVolumeSlider;
|
||||
private Button applyButton;
|
||||
private Button backButton;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
// Získání UI Document
|
||||
if (uiDocument == null)
|
||||
uiDocument = GetComponent<UIDocument>();
|
||||
|
||||
root = uiDocument.rootVisualElement;
|
||||
|
||||
// Inicializace UI elementů
|
||||
InitializeMenuButtons();
|
||||
InitializeSettingsPanel();
|
||||
|
||||
// Registrace event handlerů
|
||||
RegisterButtonEvents();
|
||||
|
||||
// Načtení nastavení
|
||||
LoadSettings();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// Odregistrování event handlerů
|
||||
UnregisterButtonEvents();
|
||||
}
|
||||
|
||||
#region Initialization
|
||||
|
||||
private void InitializeMenuButtons()
|
||||
{
|
||||
newGameButton = root.Q<Button>("NewGameButton");
|
||||
continueButton = root.Q<Button>("ContinueButton");
|
||||
multiplayerButton = root.Q<Button>("MultiplayerButton");
|
||||
settingsButton = root.Q<Button>("SettingsButton");
|
||||
creditsButton = root.Q<Button>("CreditsButton");
|
||||
quitButton = root.Q<Button>("QuitButton");
|
||||
|
||||
// Kontrola, zda existuje uložená hra
|
||||
continueButton.SetEnabled(HasSaveGame());
|
||||
}
|
||||
|
||||
private void InitializeSettingsPanel()
|
||||
{
|
||||
settingsPanel = root.Q<VisualElement>("SettingsPanel");
|
||||
|
||||
// Graphics Settings
|
||||
qualityDropdown = root.Q<DropdownField>("QualityDropdown");
|
||||
fullscreenToggle = root.Q<Toggle>("FullscreenToggle");
|
||||
resolutionDropdown = root.Q<DropdownField>("ResolutionDropdown");
|
||||
|
||||
// Audio Settings
|
||||
masterVolumeSlider = root.Q<Slider>("MasterVolumeSlider");
|
||||
musicVolumeSlider = root.Q<Slider>("MusicVolumeSlider");
|
||||
sfxVolumeSlider = root.Q<Slider>("SFXVolumeSlider");
|
||||
|
||||
// Buttons
|
||||
applyButton = root.Q<Button>("ApplyButton");
|
||||
backButton = root.Q<Button>("BackButton");
|
||||
|
||||
// Setup dropdowns
|
||||
SetupQualityDropdown();
|
||||
SetupResolutionDropdown();
|
||||
}
|
||||
|
||||
private void SetupQualityDropdown()
|
||||
{
|
||||
if (qualityDropdown == null) return;
|
||||
|
||||
var qualityNames = QualitySettings.names;
|
||||
qualityDropdown.choices = new System.Collections.Generic.List<string>(qualityNames);
|
||||
qualityDropdown.value = qualityNames[QualitySettings.GetQualityLevel()];
|
||||
}
|
||||
|
||||
private void SetupResolutionDropdown()
|
||||
{
|
||||
if (resolutionDropdown == null) return;
|
||||
|
||||
var resolutions = new System.Collections.Generic.List<string>();
|
||||
foreach (var resolution in Screen.resolutions)
|
||||
{
|
||||
resolutions.Add($"{resolution.width} x {resolution.height} @ {resolution.refreshRate}Hz");
|
||||
}
|
||||
|
||||
resolutionDropdown.choices = resolutions;
|
||||
resolutionDropdown.value = $"{Screen.currentResolution.width} x {Screen.currentResolution.height} @ {Screen.currentResolution.refreshRate}Hz";
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Event Registration
|
||||
|
||||
private void RegisterButtonEvents()
|
||||
{
|
||||
// Main Menu Buttons
|
||||
if (newGameButton != null)
|
||||
{
|
||||
newGameButton.clicked += OnNewGameClicked;
|
||||
AddHoverEffects(newGameButton);
|
||||
}
|
||||
|
||||
if (continueButton != null)
|
||||
{
|
||||
continueButton.clicked += OnContinueClicked;
|
||||
AddHoverEffects(continueButton);
|
||||
}
|
||||
|
||||
if (multiplayerButton != null)
|
||||
{
|
||||
multiplayerButton.clicked += OnMultiplayerClicked;
|
||||
AddHoverEffects(multiplayerButton);
|
||||
}
|
||||
|
||||
if (settingsButton != null)
|
||||
{
|
||||
settingsButton.clicked += OnSettingsClicked;
|
||||
AddHoverEffects(settingsButton);
|
||||
}
|
||||
|
||||
if (creditsButton != null)
|
||||
{
|
||||
creditsButton.clicked += OnCreditsClicked;
|
||||
AddHoverEffects(creditsButton);
|
||||
}
|
||||
|
||||
if (quitButton != null)
|
||||
{
|
||||
quitButton.clicked += OnQuitClicked;
|
||||
AddHoverEffects(quitButton);
|
||||
}
|
||||
|
||||
// Settings Panel Buttons
|
||||
if (applyButton != null)
|
||||
{
|
||||
applyButton.clicked += OnApplySettingsClicked;
|
||||
AddHoverEffects(applyButton);
|
||||
}
|
||||
|
||||
if (backButton != null)
|
||||
{
|
||||
backButton.clicked += OnBackFromSettingsClicked;
|
||||
AddHoverEffects(backButton);
|
||||
}
|
||||
}
|
||||
|
||||
private void UnregisterButtonEvents()
|
||||
{
|
||||
if (newGameButton != null) newGameButton.clicked -= OnNewGameClicked;
|
||||
if (continueButton != null) continueButton.clicked -= OnContinueClicked;
|
||||
if (multiplayerButton != null) multiplayerButton.clicked -= OnMultiplayerClicked;
|
||||
if (settingsButton != null) settingsButton.clicked -= OnSettingsClicked;
|
||||
if (creditsButton != null) creditsButton.clicked -= OnCreditsClicked;
|
||||
if (quitButton != null) quitButton.clicked -= OnQuitClicked;
|
||||
if (applyButton != null) applyButton.clicked -= OnApplySettingsClicked;
|
||||
if (backButton != null) backButton.clicked -= OnBackFromSettingsClicked;
|
||||
}
|
||||
|
||||
private void AddHoverEffects(Button button)
|
||||
{
|
||||
button.RegisterCallback<MouseEnterEvent>(evt => OnButtonHover());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Button Callbacks
|
||||
|
||||
private void OnNewGameClicked()
|
||||
{
|
||||
PlayButtonClickSound();
|
||||
Debug.Log("Starting New Game...");
|
||||
|
||||
// Vymazání uložené hry
|
||||
PlayerPrefs.DeleteKey("SaveGame");
|
||||
|
||||
// Načtení herní scény
|
||||
LoadGameScene();
|
||||
}
|
||||
|
||||
private void OnContinueClicked()
|
||||
{
|
||||
PlayButtonClickSound();
|
||||
Debug.Log("Continuing Game...");
|
||||
|
||||
// Načtení uložené hry
|
||||
LoadSaveGame();
|
||||
|
||||
// Načtení herní scény
|
||||
LoadGameScene();
|
||||
}
|
||||
|
||||
private void OnMultiplayerClicked()
|
||||
{
|
||||
PlayButtonClickSound();
|
||||
Debug.Log("Opening Multiplayer Menu...");
|
||||
|
||||
// TODO: Implementovat multiplayer menu
|
||||
}
|
||||
|
||||
private void OnSettingsClicked()
|
||||
{
|
||||
PlayButtonClickSound();
|
||||
Debug.Log("Opening Settings...");
|
||||
ShowSettingsPanel();
|
||||
}
|
||||
|
||||
private void OnCreditsClicked()
|
||||
{
|
||||
PlayButtonClickSound();
|
||||
Debug.Log("Showing Credits...");
|
||||
|
||||
// TODO: Implementovat credits screen
|
||||
}
|
||||
|
||||
private void OnQuitClicked()
|
||||
{
|
||||
PlayButtonClickSound();
|
||||
Debug.Log("Quitting Game...");
|
||||
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorApplication.isPlaying = false;
|
||||
#else
|
||||
Application.Quit();
|
||||
#endif
|
||||
}
|
||||
|
||||
private void OnApplySettingsClicked()
|
||||
{
|
||||
PlayButtonClickSound();
|
||||
ApplySettings();
|
||||
SaveSettings();
|
||||
Debug.Log("Settings Applied!");
|
||||
}
|
||||
|
||||
private void OnBackFromSettingsClicked()
|
||||
{
|
||||
PlayButtonClickSound();
|
||||
HideSettingsPanel();
|
||||
}
|
||||
|
||||
private void OnButtonHover()
|
||||
{
|
||||
PlayButtonHoverSound();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Settings Management
|
||||
|
||||
private void ShowSettingsPanel()
|
||||
{
|
||||
if (settingsPanel != null)
|
||||
{
|
||||
settingsPanel.RemoveFromClassList("hidden");
|
||||
}
|
||||
}
|
||||
|
||||
private void HideSettingsPanel()
|
||||
{
|
||||
if (settingsPanel != null)
|
||||
{
|
||||
settingsPanel.AddToClassList("hidden");
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplySettings()
|
||||
{
|
||||
// Graphics Settings
|
||||
if (qualityDropdown != null)
|
||||
{
|
||||
int qualityIndex = qualityDropdown.index;
|
||||
QualitySettings.SetQualityLevel(qualityIndex);
|
||||
}
|
||||
|
||||
if (fullscreenToggle != null)
|
||||
{
|
||||
Screen.fullScreen = fullscreenToggle.value;
|
||||
}
|
||||
|
||||
if (resolutionDropdown != null && resolutionDropdown.index >= 0)
|
||||
{
|
||||
var resolution = Screen.resolutions[resolutionDropdown.index];
|
||||
Screen.SetResolution(resolution.width, resolution.height, Screen.fullScreen);
|
||||
}
|
||||
|
||||
// Audio Settings
|
||||
if (masterVolumeSlider != null)
|
||||
{
|
||||
AudioListener.volume = masterVolumeSlider.value / 100f;
|
||||
}
|
||||
|
||||
// TODO: Implementovat music a SFX volume přes audio mixer
|
||||
}
|
||||
|
||||
private void SaveSettings()
|
||||
{
|
||||
PlayerPrefs.SetInt("QualityLevel", QualitySettings.GetQualityLevel());
|
||||
PlayerPrefs.SetInt("Fullscreen", Screen.fullScreen ? 1 : 0);
|
||||
PlayerPrefs.SetInt("ResolutionWidth", Screen.currentResolution.width);
|
||||
PlayerPrefs.SetInt("ResolutionHeight", Screen.currentResolution.height);
|
||||
|
||||
if (masterVolumeSlider != null)
|
||||
PlayerPrefs.SetFloat("MasterVolume", masterVolumeSlider.value);
|
||||
if (musicVolumeSlider != null)
|
||||
PlayerPrefs.SetFloat("MusicVolume", musicVolumeSlider.value);
|
||||
if (sfxVolumeSlider != null)
|
||||
PlayerPrefs.SetFloat("SFXVolume", sfxVolumeSlider.value);
|
||||
|
||||
PlayerPrefs.Save();
|
||||
}
|
||||
|
||||
private void LoadSettings()
|
||||
{
|
||||
// Graphics Settings
|
||||
if (qualityDropdown != null)
|
||||
{
|
||||
int qualityLevel = PlayerPrefs.GetInt("QualityLevel", QualitySettings.GetQualityLevel());
|
||||
qualityDropdown.index = qualityLevel;
|
||||
}
|
||||
|
||||
if (fullscreenToggle != null)
|
||||
{
|
||||
fullscreenToggle.value = PlayerPrefs.GetInt("Fullscreen", 1) == 1;
|
||||
}
|
||||
|
||||
// Audio Settings
|
||||
if (masterVolumeSlider != null)
|
||||
{
|
||||
float masterVolume = PlayerPrefs.GetFloat("MasterVolume", 100f);
|
||||
masterVolumeSlider.value = masterVolume;
|
||||
AudioListener.volume = masterVolume / 100f;
|
||||
}
|
||||
|
||||
if (musicVolumeSlider != null)
|
||||
{
|
||||
musicVolumeSlider.value = PlayerPrefs.GetFloat("MusicVolume", 80f);
|
||||
}
|
||||
|
||||
if (sfxVolumeSlider != null)
|
||||
{
|
||||
sfxVolumeSlider.value = PlayerPrefs.GetFloat("SFXVolume", 100f);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Game Management
|
||||
|
||||
private bool HasSaveGame()
|
||||
{
|
||||
return PlayerPrefs.HasKey("SaveGame");
|
||||
}
|
||||
|
||||
private void LoadSaveGame()
|
||||
{
|
||||
// TODO: Implementovat načítání save game
|
||||
string saveData = PlayerPrefs.GetString("SaveGame", "");
|
||||
Debug.Log($"Loading save game: {saveData}");
|
||||
}
|
||||
|
||||
private void LoadGameScene()
|
||||
{
|
||||
// Načtení herní scény pomocí async loading
|
||||
StartCoroutine(LoadSceneAsync(gameSceneName));
|
||||
}
|
||||
|
||||
private System.Collections.IEnumerator LoadSceneAsync(string sceneName)
|
||||
{
|
||||
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);
|
||||
|
||||
while (!asyncLoad.isDone)
|
||||
{
|
||||
// Zde můžete zobrazit loading bar
|
||||
float progress = Mathf.Clamp01(asyncLoad.progress / 0.9f);
|
||||
Debug.Log($"Loading progress: {progress * 100}%");
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Audio
|
||||
|
||||
private void PlayButtonClickSound()
|
||||
{
|
||||
if (audioSource != null && buttonClickSound != null)
|
||||
{
|
||||
audioSource.PlayOneShot(buttonClickSound);
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayButtonHoverSound()
|
||||
{
|
||||
if (audioSource != null && buttonHoverSound != null)
|
||||
{
|
||||
audioSource.PlayOneShot(buttonHoverSound, 0.5f);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
2
UI/Scripts/MainMenuController.cs.meta
Normal file
2
UI/Scripts/MainMenuController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9968fc9f33c51a14292c6cac00cff4b3
|
||||
Reference in New Issue
Block a user