using System; using UnityEngine; using UnityEngine.UI; using static GameManager; public class SettingsInput : MonoBehaviour { [SerializeField] private SettingsScreen screen; public GameManager gameManager; private bool isPaused = false; private bool pushUp = false; private bool leftHeld; private bool rightHeld; private bool downHeld; private bool turn; private bool isActivePanel = false; private bool waitForKeyRelease = false; private float unifiedTimer = 0f; private float timerThreshold = 1.25f; private float secretButtonTimer = 0f; private ButtonPinMapping secretKey = ButtonPinMapping.secret1; private ButtonPinMapping downKey = ButtonPinMapping.down1; private ButtonPinMapping upKey = ButtonPinMapping.up1; private ButtonPinMapping leftKey = ButtonPinMapping.left1; private ButtonPinMapping rightKey = ButtonPinMapping.right1; private ButtonPinMapping action1 = ButtonPinMapping.action1; private ButtonPinMapping turnLeftKey = ButtonPinMapping.turnLeft1; private ButtonPinMapping turnRightKey = ButtonPinMapping.turnRight1; private bool IsSecretHeld() => In.GetButton(secretKey) || Input.GetKey(KeyCode.F5); private bool IsSecretReleased() => In.GetButtonUp(secretKey) || Input.GetKeyUp(KeyCode.F5); private SettingsPanelBase panel = null; private string currentOverlayId = null; private void OnEnable() { CreditSystem.OnInitialize += Subscribe; } private void OnDisable() { if (CreditSystem.onCoinAdded != null) CreditSystem.onCoinAdded.RemoveListener(View); CreditSystem.OnInitialize -= Subscribe; } private void Subscribe() { CreditSystem.onCoinAdded.AddListener(View); } private void View(int __, int _) { screen.AcceptView(); } void Start() { screen.Open(); } void Update() { // --- Вход в настройки через секретную кнопку --- if (IsSecretHeld()) { secretButtonTimer += Time.deltaTime; if (secretButtonTimer <= 3f) return; if (isActivePanel) { ClosePanel(); waitForKeyRelease = true; } isPaused = !isPaused; if (isPaused) { gameManager.SetState(GameState.Settings); gameManager.SetInputMode(InputMode.Settings); } else { gameManager.ReturnToMainMenu(); if (gameManager.gameMode == GameMode.PlayerVsPlayer) gameManager.opponentManager.ReturnToMainMenu(); } secretButtonTimer = 0f; } if (IsSecretReleased()) { secretButtonTimer = 0f; return; } if (!isPaused) return; if (isActivePanel) { HandelPanelInput(); return; } HandleSettingsLogic(); } // ------------------------------------------------------- // Управление внутри подпанели (TextInput и др.) // ------------------------------------------------------- private void HandelPanelInput() { if (waitForKeyRelease) { if (!In.GetButton(downKey)) waitForKeyRelease = false; return; } // Action1 — закрыть подпанель и вернуться в настройки if (In.GetButtonDown(action1)) { ClosePanel(); return; } int dirX = 0; if (panel.UsesContinuousInput) { if (In.GetButton(upKey)) dirX++; if (In.GetButton(downKey)) dirX--; } else { if (In.GetButtonDown(upKey)) dirX++; if (In.GetButtonDown(downKey)) dirX--; } panel.SwitchVertical(dirX); bool changeAmount = In.GetButton(turnLeftKey) || In.GetButton(turnRightKey); int dirY = 0; if (In.GetButtonDown(leftKey)) dirY--; if (In.GetButtonDown(rightKey)) dirY++; panel.SwitchHorizontal(dirY, changeAmount); } // ------------------------------------------------------- // Основная логика настроек // ------------------------------------------------------- private void HandleSettingsLogic() { // --- Режим выбора опции (второй шаг) --- if (screen.IsChoosingOption) { // Left/Right — навигация между опциями (они расположены горизонтально) if (In.GetButtonDown(leftKey)) screen.Switch(-1); if (In.GetButtonDown(rightKey)) screen.Switch(+1); // Down — подтвердить и сохранить выбранную опцию if (In.GetButtonDown(downKey)) screen.Accept(); // Action1 — отмена без сохранения, вернуться к списку настроек if (In.GetButtonDown(action1)) screen.CancelOption(); return; } // --- Режим навигации по списку настроек (первый шаг) --- // Action1 = выход из меню настроек if (In.GetButtonDown(action1)) { ExitSettings(); return; } SettingType currentType = screen.GetCurrentSettingType(); // Up / Down — навигация по списку if (In.GetButtonDown(upKey)) screen.Switch(-1); if (In.GetButtonDown(downKey)) screen.Switch(+1); // Panel / Action: удержание Right открывает панель / выполняет экшн if (currentType == SettingType.Panel || currentType == SettingType.Action) { if (In.GetButton(rightKey)) { unifiedTimer += Time.deltaTime; screen.UpdateProgressBar(unifiedTimer / timerThreshold); if (unifiedTimer >= timerThreshold) { int id = screen.GetCurrentPanelId(); if (currentType == SettingType.Panel) OpenPanel(id); else ExecuteAction(id); ResetTimer(); } } else { ResetTimer(); } return; // у Panel/Action нет Accept через Right-тап } ResetTimer(); // Svitcher: Left / Right переключает мгновенно if (currentType == SettingType.Svitcher) { if (In.GetButtonDown(leftKey) || In.GetButtonDown(rightKey)) screen.ToggleSwitcher(); return; } // Normal: Right открывает панель опций (шаг 1) if (In.GetButtonDown(rightKey)) screen.Accept(); } // ------------------------------------------------------- // Открытие / закрытие подпанели // ------------------------------------------------------- private void OpenPanel(int idPanel) { if (panel != null) return; if (!PanelRouter.Instance.GetPanelInfo(idPanel, out var foundPanel, out var overlayId)) return; isActivePanel = true; waitForKeyRelease = true; panel = foundPanel; currentOverlayId = overlayId; if (!string.IsNullOrEmpty(currentOverlayId)) gameManager.uiManager.ShowOverlay(currentOverlayId, true, hideOtherOverlays: false); gameManager.SetInputMode(InputMode.Settings); panel.Initialize(idPanel); } private void ClosePanel() { isActivePanel = false; panel.Accept(); if (!string.IsNullOrEmpty(currentOverlayId)) gameManager.uiManager.ShowOverlay(currentOverlayId, false); panel = null; currentOverlayId = null; gameManager.SetState(GameState.Settings); gameManager.SetInputMode(InputMode.Settings); } // ------------------------------------------------------- // Выход из настроек // ------------------------------------------------------- private void ExitSettings() { if (isActivePanel) ClosePanel(); isPaused = false; gameManager.ReturnToMainMenu(); if (gameManager.gameMode == GameMode.PlayerVsPlayer) gameManager.opponentManager.ReturnToMainMenu(); } // ------------------------------------------------------- // Экшены // ------------------------------------------------------- private void ExecuteAction(int actionId) { switch (actionId) { case 100: Zeroize(PrizeSystem.key_startsNul); screen.AcceptView(); Debug.Log("Сброс статистики игр выполнен"); break; case 101: Zeroize(PrizeSystem.key_prizesgivenNul); screen.AcceptView(); Debug.Log("Сброс статистики призов выполнен"); break; case 102: DifficultyManager.SyncChanceToMax(); screen.AcceptView(); Debug.Log("Сброс прогресса до суперприза"); break; default: Debug.LogWarning($"Неизвестный actionId: {actionId}"); break; } } private void ResetTimer() { unifiedTimer = 0f; screen.UpdateProgressBar(0f); } private void Zeroize(string key) { PlayerPrefs.SetInt(key, 0); } }