Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using AC;
- public class SwitchInputs : MonoBehaviour
- {
- [SerializeField] private InputMethod defaultInputMethod = InputMethod.MouseAndKeyboard;
- [SerializeField] private float inputSwitchDelay = 0.15f;
- [SerializeField] private float mouseMovementThreshold = 3f;
- [SerializeField] private float mouseCursorIconSize = 0.05f; // Default cursor icon size for mouse mode
- private bool isUsingMouseInput = true;
- private float lastSwitchTime;
- private Vector3 lastMousePosition;
- private bool isInitialized = false;
- // Store original cursor icon sizes
- private float originalMouseOverIconSize = 0.05f;
- private float originalCursorID0Size = 0.05f;
- private float originalWaitIconSize = 0.05f;
- private bool hasStoredOriginalSizes = false;
- private void Start()
- {
- // Store original cursor icon sizes
- if (KickStarter.cursorManager != null && !hasStoredOriginalSizes)
- {
- originalMouseOverIconSize = KickStarter.cursorManager.mouseOverIcon.size;
- originalWaitIconSize = KickStarter.cursorManager.waitIcon.size;
- // Store size of cursor ID 0 (main cursor)
- if (KickStarter.cursorManager.GetCursorIconFromID(0) != null)
- {
- originalCursorID0Size = KickStarter.cursorManager.GetCursorIconFromID(0).size;
- }
- hasStoredOriginalSizes = true;
- Debug.Log($"Stored original cursor sizes - MouseOver: {originalMouseOverIconSize}, Wait: {originalWaitIconSize}, ID0: {originalCursorID0Size}");
- }
- // Initialize immediately with default input
- isUsingMouseInput = (defaultInputMethod == InputMethod.MouseAndKeyboard);
- lastMousePosition = Input.mousePosition;
- // Apply settings immediately
- ApplyInputMethod(defaultInputMethod);
- isInitialized = true;
- Debug.Log($"SwitchInputs initialized with: {defaultInputMethod}");
- }
- private void Update()
- {
- if (!isInitialized || Time.time - lastSwitchTime < inputSwitchDelay) return;
- CheckForInputSwitch();
- }
- private void CheckForInputSwitch()
- {
- bool shouldUseMouseInput = isUsingMouseInput;
- // Check for mouse input first (highest priority)
- if (HasMouseInput())
- {
- shouldUseMouseInput = true;
- }
- // Then check for keyboard/controller input
- else if (HasKeyboardOrControllerInput())
- {
- shouldUseMouseInput = false;
- }
- // Switch input method if changed
- if (shouldUseMouseInput != isUsingMouseInput)
- {
- isUsingMouseInput = shouldUseMouseInput;
- lastSwitchTime = Time.time;
- InputMethod newMethod = shouldUseMouseInput ?
- InputMethod.MouseAndKeyboard :
- InputMethod.KeyboardOrController;
- ApplyInputMethod(newMethod);
- }
- }
- private bool HasMouseInput()
- {
- // Check for mouse button clicks
- if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
- {
- return true;
- }
- // Check for significant mouse movement
- Vector3 currentMousePos = Input.mousePosition;
- float mouseDelta = Vector3.Distance(currentMousePos, lastMousePosition);
- if (mouseDelta > mouseMovementThreshold)
- {
- lastMousePosition = currentMousePos;
- return true;
- }
- return false;
- }
- private bool HasKeyboardOrControllerInput()
- {
- // Check for keyboard input (excluding mouse buttons)
- if (Input.anyKeyDown)
- {
- // Make sure it's not a mouse button
- if (!Input.GetMouseButtonDown(0) && !Input.GetMouseButtonDown(1) && !Input.GetMouseButtonDown(2))
- {
- return true;
- }
- }
- // Check for controller analog movement
- if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.2f || Mathf.Abs(Input.GetAxis("Vertical")) > 0.2f)
- {
- return true;
- }
- return false;
- }
- private void ApplyInputMethod(InputMethod inputMethod)
- {
- if (KickStarter.settingsManager == null) return;
- // Set the input method
- KickStarter.settingsManager.inputMethod = inputMethod;
- if (inputMethod == InputMethod.MouseAndKeyboard)
- {
- ConfigureMouseMode();
- }
- else
- {
- ConfigureKeyboardMode();
- }
- Debug.Log($"Switched to: {inputMethod}");
- }
- private void ConfigureMouseMode()
- {
- // Movement and detection settings
- KickStarter.settingsManager.movementMethod = MovementMethod.PointAndClick;
- KickStarter.settingsManager.hotspotDetection = HotspotDetection.MouseOver;
- // Menu settings
- if (KickStarter.menuManager != null)
- {
- KickStarter.menuManager.keyboardControlWhenCutscene = false;
- KickStarter.menuManager.keyboardControlWhenPaused = false;
- KickStarter.menuManager.keyboardControlWhenDialogOptions = false;
- }
- // Show cursor and restore cursor icon sizes
- ShowCursor();
- SetAllCursorIconSizes(mouseCursorIconSize);
- }
- private void ConfigureKeyboardMode()
- {
- // Movement and detection settings
- KickStarter.settingsManager.movementMethod = MovementMethod.Direct;
- KickStarter.settingsManager.hotspotDetection = HotspotDetection.PlayerVicinity;
- // Menu settings
- if (KickStarter.menuManager != null)
- {
- KickStarter.menuManager.keyboardControlWhenCutscene = true;
- KickStarter.menuManager.keyboardControlWhenPaused = true;
- KickStarter.menuManager.keyboardControlWhenDialogOptions = true;
- // Set first selected element for keyboard navigation
- KickStarter.playerMenus.FindFirstSelectedElement();
- }
- // Hide cursor and set all cursor icon sizes to 0
- HideCursor();
- SetAllCursorIconSizes(0f);
- }
- private void ShowCursor()
- {
- // Unity cursor
- Cursor.visible = true;
- Cursor.lockState = CursorLockMode.None;
- // Adventure Creator cursor
- if (KickStarter.cursorManager != null)
- {
- KickStarter.cursorManager.cursorDisplay = CursorDisplay.Always;
- }
- }
- private void HideCursor()
- {
- // Unity cursor
- Cursor.visible = false;
- Cursor.lockState = CursorLockMode.None;
- // Adventure Creator cursor
- if (KickStarter.cursorManager != null)
- {
- KickStarter.cursorManager.cursorDisplay = CursorDisplay.Never;
- }
- }
- private void SetAllCursorIconSizes(float size)
- {
- if (KickStarter.cursorManager != null)
- {
- // Set mouseOverIcon size
- KickStarter.cursorManager.mouseOverIcon.size = size;
- // Set waitIcon size
- KickStarter.cursorManager.waitIcon.size = size;
- // Set cursor ID 0 size (main cursor)
- if (KickStarter.cursorManager.GetCursorIconFromID(0) != null)
- {
- KickStarter.cursorManager.GetCursorIconFromID(0).size = size;
- }
- Debug.Log($"Set all cursor icon sizes to: {size}");
- }
- }
- // Public methods for external control
- public void ForceInputMethod(InputMethod inputMethod)
- {
- isUsingMouseInput = (inputMethod == InputMethod.MouseAndKeyboard);
- ApplyInputMethod(inputMethod);
- }
- public bool IsUsingMouseInput()
- {
- return isUsingMouseInput;
- }
- public InputMethod GetCurrentInputMethod()
- {
- return isUsingMouseInput ? InputMethod.MouseAndKeyboard : InputMethod.KeyboardOrController;
- }
- public void SetMouseCursorIconSize(float size)
- {
- mouseCursorIconSize = size;
- if (isUsingMouseInput)
- {
- SetAllCursorIconSizes(size);
- }
- }
- public void RestoreOriginalCursorSizes()
- {
- if (KickStarter.cursorManager != null && hasStoredOriginalSizes)
- {
- KickStarter.cursorManager.mouseOverIcon.size = originalMouseOverIconSize;
- KickStarter.cursorManager.waitIcon.size = originalWaitIconSize;
- if (KickStarter.cursorManager.GetCursorIconFromID(0) != null)
- {
- KickStarter.cursorManager.GetCursorIconFromID(0).size = originalCursorID0Size;
- }
- Debug.Log($"Restored original cursor sizes - MouseOver: {originalMouseOverIconSize}, Wait: {originalWaitIconSize}, ID0: {originalCursorID0Size}");
- }
- }
- private void OnEnable()
- {
- // Reapply settings when enabled
- if (isInitialized)
- {
- InputMethod currentMethod = isUsingMouseInput ?
- InputMethod.MouseAndKeyboard :
- InputMethod.KeyboardOrController;
- ApplyInputMethod(currentMethod);
- }
- }
- private void OnDestroy()
- {
- // Restore original cursor icon sizes when destroyed
- RestoreOriginalCursorSizes();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment