signola2020

Untitled

Jun 27th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.27 KB | None | 0 0
  1. using UnityEngine;
  2. using AC;
  3.  
  4. public class SwitchInputs : MonoBehaviour
  5. {
  6. [SerializeField] private InputMethod defaultInputMethod = InputMethod.MouseAndKeyboard;
  7. [SerializeField] private float inputSwitchDelay = 0.15f;
  8. [SerializeField] private float mouseMovementThreshold = 3f;
  9. [SerializeField] private float mouseCursorIconSize = 0.05f; // Default cursor icon size for mouse mode
  10.  
  11. private bool isUsingMouseInput = true;
  12. private float lastSwitchTime;
  13. private Vector3 lastMousePosition;
  14. private bool isInitialized = false;
  15.  
  16. // Store original cursor icon sizes
  17. private float originalMouseOverIconSize = 0.05f;
  18. private float originalCursorID0Size = 0.05f;
  19. private float originalWaitIconSize = 0.05f;
  20. private bool hasStoredOriginalSizes = false;
  21.  
  22. private void Start()
  23. {
  24. // Store original cursor icon sizes
  25. if (KickStarter.cursorManager != null && !hasStoredOriginalSizes)
  26. {
  27. originalMouseOverIconSize = KickStarter.cursorManager.mouseOverIcon.size;
  28. originalWaitIconSize = KickStarter.cursorManager.waitIcon.size;
  29.  
  30. // Store size of cursor ID 0 (main cursor)
  31. if (KickStarter.cursorManager.GetCursorIconFromID(0) != null)
  32. {
  33. originalCursorID0Size = KickStarter.cursorManager.GetCursorIconFromID(0).size;
  34. }
  35.  
  36. hasStoredOriginalSizes = true;
  37. Debug.Log($"Stored original cursor sizes - MouseOver: {originalMouseOverIconSize}, Wait: {originalWaitIconSize}, ID0: {originalCursorID0Size}");
  38. }
  39.  
  40. // Initialize immediately with default input
  41. isUsingMouseInput = (defaultInputMethod == InputMethod.MouseAndKeyboard);
  42. lastMousePosition = Input.mousePosition;
  43.  
  44. // Apply settings immediately
  45. ApplyInputMethod(defaultInputMethod);
  46. isInitialized = true;
  47.  
  48. Debug.Log($"SwitchInputs initialized with: {defaultInputMethod}");
  49. }
  50.  
  51. private void Update()
  52. {
  53. if (!isInitialized || Time.time - lastSwitchTime < inputSwitchDelay) return;
  54.  
  55. CheckForInputSwitch();
  56. }
  57.  
  58. private void CheckForInputSwitch()
  59. {
  60. bool shouldUseMouseInput = isUsingMouseInput;
  61.  
  62. // Check for mouse input first (highest priority)
  63. if (HasMouseInput())
  64. {
  65. shouldUseMouseInput = true;
  66. }
  67. // Then check for keyboard/controller input
  68. else if (HasKeyboardOrControllerInput())
  69. {
  70. shouldUseMouseInput = false;
  71. }
  72.  
  73. // Switch input method if changed
  74. if (shouldUseMouseInput != isUsingMouseInput)
  75. {
  76. isUsingMouseInput = shouldUseMouseInput;
  77. lastSwitchTime = Time.time;
  78.  
  79. InputMethod newMethod = shouldUseMouseInput ?
  80. InputMethod.MouseAndKeyboard :
  81. InputMethod.KeyboardOrController;
  82.  
  83. ApplyInputMethod(newMethod);
  84. }
  85. }
  86.  
  87. private bool HasMouseInput()
  88. {
  89. // Check for mouse button clicks
  90. if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
  91. {
  92. return true;
  93. }
  94.  
  95. // Check for significant mouse movement
  96. Vector3 currentMousePos = Input.mousePosition;
  97. float mouseDelta = Vector3.Distance(currentMousePos, lastMousePosition);
  98.  
  99. if (mouseDelta > mouseMovementThreshold)
  100. {
  101. lastMousePosition = currentMousePos;
  102. return true;
  103. }
  104.  
  105. return false;
  106. }
  107.  
  108. private bool HasKeyboardOrControllerInput()
  109. {
  110. // Check for keyboard input (excluding mouse buttons)
  111. if (Input.anyKeyDown)
  112. {
  113. // Make sure it's not a mouse button
  114. if (!Input.GetMouseButtonDown(0) && !Input.GetMouseButtonDown(1) && !Input.GetMouseButtonDown(2))
  115. {
  116. return true;
  117. }
  118. }
  119.  
  120. // Check for controller analog movement
  121. if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.2f || Mathf.Abs(Input.GetAxis("Vertical")) > 0.2f)
  122. {
  123. return true;
  124. }
  125.  
  126. return false;
  127. }
  128.  
  129. private void ApplyInputMethod(InputMethod inputMethod)
  130. {
  131. if (KickStarter.settingsManager == null) return;
  132.  
  133. // Set the input method
  134. KickStarter.settingsManager.inputMethod = inputMethod;
  135.  
  136. if (inputMethod == InputMethod.MouseAndKeyboard)
  137. {
  138. ConfigureMouseMode();
  139. }
  140. else
  141. {
  142. ConfigureKeyboardMode();
  143. }
  144.  
  145. Debug.Log($"Switched to: {inputMethod}");
  146. }
  147.  
  148. private void ConfigureMouseMode()
  149. {
  150. // Movement and detection settings
  151. KickStarter.settingsManager.movementMethod = MovementMethod.PointAndClick;
  152. KickStarter.settingsManager.hotspotDetection = HotspotDetection.MouseOver;
  153.  
  154. // Menu settings
  155. if (KickStarter.menuManager != null)
  156. {
  157. KickStarter.menuManager.keyboardControlWhenCutscene = false;
  158. KickStarter.menuManager.keyboardControlWhenPaused = false;
  159. KickStarter.menuManager.keyboardControlWhenDialogOptions = false;
  160. }
  161.  
  162. // Show cursor and restore cursor icon sizes
  163. ShowCursor();
  164. SetAllCursorIconSizes(mouseCursorIconSize);
  165. }
  166.  
  167. private void ConfigureKeyboardMode()
  168. {
  169. // Movement and detection settings
  170. KickStarter.settingsManager.movementMethod = MovementMethod.Direct;
  171. KickStarter.settingsManager.hotspotDetection = HotspotDetection.PlayerVicinity;
  172.  
  173. // Menu settings
  174. if (KickStarter.menuManager != null)
  175. {
  176. KickStarter.menuManager.keyboardControlWhenCutscene = true;
  177. KickStarter.menuManager.keyboardControlWhenPaused = true;
  178. KickStarter.menuManager.keyboardControlWhenDialogOptions = true;
  179.  
  180. // Set first selected element for keyboard navigation
  181. KickStarter.playerMenus.FindFirstSelectedElement();
  182. }
  183.  
  184. // Hide cursor and set all cursor icon sizes to 0
  185. HideCursor();
  186. SetAllCursorIconSizes(0f);
  187. }
  188.  
  189. private void ShowCursor()
  190. {
  191. // Unity cursor
  192. Cursor.visible = true;
  193. Cursor.lockState = CursorLockMode.None;
  194.  
  195. // Adventure Creator cursor
  196. if (KickStarter.cursorManager != null)
  197. {
  198. KickStarter.cursorManager.cursorDisplay = CursorDisplay.Always;
  199. }
  200. }
  201.  
  202. private void HideCursor()
  203. {
  204. // Unity cursor
  205. Cursor.visible = false;
  206. Cursor.lockState = CursorLockMode.None;
  207.  
  208. // Adventure Creator cursor
  209. if (KickStarter.cursorManager != null)
  210. {
  211. KickStarter.cursorManager.cursorDisplay = CursorDisplay.Never;
  212. }
  213. }
  214.  
  215. private void SetAllCursorIconSizes(float size)
  216. {
  217. if (KickStarter.cursorManager != null)
  218. {
  219. // Set mouseOverIcon size
  220. KickStarter.cursorManager.mouseOverIcon.size = size;
  221.  
  222. // Set waitIcon size
  223. KickStarter.cursorManager.waitIcon.size = size;
  224.  
  225. // Set cursor ID 0 size (main cursor)
  226. if (KickStarter.cursorManager.GetCursorIconFromID(0) != null)
  227. {
  228. KickStarter.cursorManager.GetCursorIconFromID(0).size = size;
  229. }
  230.  
  231. Debug.Log($"Set all cursor icon sizes to: {size}");
  232. }
  233. }
  234.  
  235. // Public methods for external control
  236. public void ForceInputMethod(InputMethod inputMethod)
  237. {
  238. isUsingMouseInput = (inputMethod == InputMethod.MouseAndKeyboard);
  239. ApplyInputMethod(inputMethod);
  240. }
  241.  
  242. public bool IsUsingMouseInput()
  243. {
  244. return isUsingMouseInput;
  245. }
  246.  
  247. public InputMethod GetCurrentInputMethod()
  248. {
  249. return isUsingMouseInput ? InputMethod.MouseAndKeyboard : InputMethod.KeyboardOrController;
  250. }
  251.  
  252. public void SetMouseCursorIconSize(float size)
  253. {
  254. mouseCursorIconSize = size;
  255. if (isUsingMouseInput)
  256. {
  257. SetAllCursorIconSizes(size);
  258. }
  259. }
  260.  
  261. public void RestoreOriginalCursorSizes()
  262. {
  263. if (KickStarter.cursorManager != null && hasStoredOriginalSizes)
  264. {
  265. KickStarter.cursorManager.mouseOverIcon.size = originalMouseOverIconSize;
  266. KickStarter.cursorManager.waitIcon.size = originalWaitIconSize;
  267.  
  268. if (KickStarter.cursorManager.GetCursorIconFromID(0) != null)
  269. {
  270. KickStarter.cursorManager.GetCursorIconFromID(0).size = originalCursorID0Size;
  271. }
  272.  
  273. Debug.Log($"Restored original cursor sizes - MouseOver: {originalMouseOverIconSize}, Wait: {originalWaitIconSize}, ID0: {originalCursorID0Size}");
  274. }
  275. }
  276.  
  277. private void OnEnable()
  278. {
  279. // Reapply settings when enabled
  280. if (isInitialized)
  281. {
  282. InputMethod currentMethod = isUsingMouseInput ?
  283. InputMethod.MouseAndKeyboard :
  284. InputMethod.KeyboardOrController;
  285. ApplyInputMethod(currentMethod);
  286. }
  287. }
  288.  
  289. private void OnDestroy()
  290. {
  291. // Restore original cursor icon sizes when destroyed
  292. RestoreOriginalCursorSizes();
  293. }
  294. }
  295.  
Advertisement
Add Comment
Please, Sign In to add comment