A game about forced loneliness, made by TACStudios
at master 517 lines 21 kB view raw
1#if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE 2#define USE_INPUT_SYSTEM 3using UnityEngine.InputSystem; 4using UnityEngine.InputSystem.EnhancedTouch; 5#endif 6 7using System; 8using System.Collections.Generic; 9 10namespace UnityEngine.Rendering 11{ 12 internal enum DebugAction 13 { 14 EnableDebugMenu, 15 PreviousDebugPanel, 16 NextDebugPanel, 17 Action, 18 MakePersistent, 19 MoveVertical, 20 MoveHorizontal, 21 Multiplier, 22 ResetAll, 23 DebugActionCount 24 } 25 26 enum DebugActionRepeatMode 27 { 28 Never, 29 Delay 30 } 31 32 public sealed partial class DebugManager 33 { 34 const string kEnableDebugBtn1 = "Enable Debug Button 1"; 35 const string kEnableDebugBtn2 = "Enable Debug Button 2"; 36 const string kDebugPreviousBtn = "Debug Previous"; 37 const string kDebugNextBtn = "Debug Next"; 38 const string kValidateBtn = "Debug Validate"; 39 const string kPersistentBtn = "Debug Persistent"; 40 const string kDPadVertical = "Debug Vertical"; 41 const string kDPadHorizontal = "Debug Horizontal"; 42 const string kMultiplierBtn = "Debug Multiplier"; 43 const string kResetBtn = "Debug Reset"; 44 const string kEnableDebug = "Enable Debug"; 45 46 DebugActionDesc[] m_DebugActions; 47 DebugActionState[] m_DebugActionStates; 48 49#if USE_INPUT_SYSTEM 50 InputActionMap debugActionMap = new InputActionMap("Debug Menu"); 51#endif 52 53 void RegisterActions() 54 { 55 m_DebugActions = new DebugActionDesc[(int)DebugAction.DebugActionCount]; 56 m_DebugActionStates = new DebugActionState[(int)DebugAction.DebugActionCount]; 57 58 var enableDebugMenu = new DebugActionDesc(); 59#if USE_INPUT_SYSTEM 60 enableDebugMenu.buttonAction = debugActionMap.FindAction(kEnableDebug); 61#else 62 enableDebugMenu.buttonTriggerList.Add(new[] { kEnableDebugBtn1, kEnableDebugBtn2 }); 63 enableDebugMenu.keyTriggerList.Add(new[] { KeyCode.LeftControl, KeyCode.Backspace }); 64#endif 65 enableDebugMenu.repeatMode = DebugActionRepeatMode.Never; 66 AddAction(DebugAction.EnableDebugMenu, enableDebugMenu); 67 68 var resetDebugMenu = new DebugActionDesc(); 69#if USE_INPUT_SYSTEM 70 resetDebugMenu.buttonAction = debugActionMap.FindAction(kResetBtn); 71#else 72 resetDebugMenu.keyTriggerList.Add(new[] { KeyCode.LeftAlt, KeyCode.Backspace }); 73 resetDebugMenu.buttonTriggerList.Add(new[] { kResetBtn, kEnableDebugBtn2 }); 74#endif 75 resetDebugMenu.repeatMode = DebugActionRepeatMode.Never; 76 AddAction(DebugAction.ResetAll, resetDebugMenu); 77 78 var nextDebugPanel = new DebugActionDesc(); 79#if USE_INPUT_SYSTEM 80 nextDebugPanel.buttonAction = debugActionMap.FindAction(kDebugNextBtn); 81#else 82 nextDebugPanel.buttonTriggerList.Add(new[] { kDebugNextBtn }); 83#endif 84 nextDebugPanel.repeatMode = DebugActionRepeatMode.Never; 85 AddAction(DebugAction.NextDebugPanel, nextDebugPanel); 86 87 var previousDebugPanel = new DebugActionDesc(); 88#if USE_INPUT_SYSTEM 89 previousDebugPanel.buttonAction = debugActionMap.FindAction(kDebugPreviousBtn); 90#else 91 previousDebugPanel.buttonTriggerList.Add(new[] { kDebugPreviousBtn }); 92#endif 93 previousDebugPanel.repeatMode = DebugActionRepeatMode.Never; 94 AddAction(DebugAction.PreviousDebugPanel, previousDebugPanel); 95 96 var validate = new DebugActionDesc(); 97#if USE_INPUT_SYSTEM 98 validate.buttonAction = debugActionMap.FindAction(kValidateBtn); 99#else 100 validate.buttonTriggerList.Add(new[] { kValidateBtn }); 101#endif 102 validate.repeatMode = DebugActionRepeatMode.Never; 103 AddAction(DebugAction.Action, validate); 104 105 var persistent = new DebugActionDesc(); 106#if USE_INPUT_SYSTEM 107 persistent.buttonAction = debugActionMap.FindAction(kPersistentBtn); 108#else 109 persistent.buttonTriggerList.Add(new[] { kPersistentBtn }); 110#endif 111 persistent.repeatMode = DebugActionRepeatMode.Never; 112 AddAction(DebugAction.MakePersistent, persistent); 113 114 var multiplier = new DebugActionDesc(); 115#if USE_INPUT_SYSTEM 116 multiplier.buttonAction = debugActionMap.FindAction(kMultiplierBtn); 117#else 118 multiplier.buttonTriggerList.Add(new[] { kMultiplierBtn }); 119#endif 120 multiplier.repeatMode = DebugActionRepeatMode.Delay; 121 validate.repeatDelay = 0f; 122 123 AddAction(DebugAction.Multiplier, multiplier); 124 125 var moveVertical = new DebugActionDesc(); 126#if USE_INPUT_SYSTEM 127 moveVertical.buttonAction = debugActionMap.FindAction(kDPadVertical); 128#else 129 moveVertical.axisTrigger = kDPadVertical; 130#endif 131 moveVertical.repeatMode = DebugActionRepeatMode.Delay; 132 moveVertical.repeatDelay = 0.16f; 133 AddAction(DebugAction.MoveVertical, moveVertical); 134 135 var moveHorizontal = new DebugActionDesc(); 136#if USE_INPUT_SYSTEM 137 moveHorizontal.buttonAction = debugActionMap.FindAction(kDPadHorizontal); 138#else 139 moveHorizontal.axisTrigger = kDPadHorizontal; 140#endif 141 moveHorizontal.repeatMode = DebugActionRepeatMode.Delay; 142 moveHorizontal.repeatDelay = 0.16f; 143 AddAction(DebugAction.MoveHorizontal, moveHorizontal); 144 } 145 146 internal void EnableInputActions() 147 { 148#if USE_INPUT_SYSTEM 149 foreach (var action in debugActionMap) 150 action.Enable(); 151#endif 152 } 153 154 void AddAction(DebugAction action, DebugActionDesc desc) 155 { 156 int index = (int)action; 157 m_DebugActions[index] = desc; 158 m_DebugActionStates[index] = new DebugActionState(); 159 } 160 161 void SampleAction(int actionIndex) 162 { 163 var desc = m_DebugActions[actionIndex]; 164 var state = m_DebugActionStates[actionIndex]; 165 166 // Disable all input events if we're using the new input system 167#if USE_INPUT_SYSTEM 168 if (state.runningAction == false) 169 { 170 if (desc.buttonAction != null) 171 { 172 var value = desc.buttonAction.ReadValue<float>(); 173 if (!Mathf.Approximately(value, 0)) 174 state.TriggerWithButton(desc.buttonAction, value); 175 } 176 } 177#elif ENABLE_LEGACY_INPUT_MANAGER 178 //bool canSampleAction = (state.actionTriggered == false) || (desc.repeatMode == DebugActionRepeatMode.Delay && state.timer > desc.repeatDelay); 179 if (state.runningAction == false) 180 { 181 // Check button triggers 182 for (int buttonListIndex = 0; buttonListIndex < desc.buttonTriggerList.Count; ++buttonListIndex) 183 { 184 var buttons = desc.buttonTriggerList[buttonListIndex]; 185 bool allButtonPressed = true; 186 187 try 188 { 189 foreach (var button in buttons) 190 { 191 allButtonPressed = Input.GetButton(button); 192 if (!allButtonPressed) 193 break; 194 } 195 } 196 catch (ArgumentException) 197 { 198 // Exception thrown if the input mapping gets removed while in play mode (UUM-37148) 199 allButtonPressed = false; 200 } 201 202 if (allButtonPressed) 203 { 204 state.TriggerWithButton(buttons, 1f); 205 break; 206 } 207 } 208 209 // Check axis triggers 210 if (desc.axisTrigger != "") 211 { 212 try 213 { 214 float axisValue = Input.GetAxis(desc.axisTrigger); 215 216 if (axisValue != 0f) 217 state.TriggerWithAxis(desc.axisTrigger, axisValue); 218 } 219 catch (ArgumentException) 220 { 221 // Exception thrown if the input mapping gets removed while in play mode (UUM-37148) 222 } 223 } 224 225 // Check key triggers 226 for (int keyListIndex = 0; keyListIndex < desc.keyTriggerList.Count; ++keyListIndex) 227 { 228 bool allKeyPressed = true; 229 230 var keys = desc.keyTriggerList[keyListIndex]; 231 232 try 233 { 234 foreach (var key in keys) 235 { 236 allKeyPressed = Input.GetKey(key); 237 if (!allKeyPressed) 238 break; 239 } 240 } 241 catch (ArgumentException) 242 { 243 // Exception thrown if the input mapping gets removed while in play mode (UUM-37148) 244 allKeyPressed = false; 245 } 246 247 if (allKeyPressed) 248 { 249 state.TriggerWithKey(keys, 1f); 250 break; 251 } 252 } 253 } 254 255#endif 256 } 257 258 void UpdateAction(int actionIndex) 259 { 260 var desc = m_DebugActions[actionIndex]; 261 var state = m_DebugActionStates[actionIndex]; 262 263 if (state.runningAction) 264 state.Update(desc); 265 } 266 267 internal void UpdateActions() 268 { 269 for (int actionIndex = 0; actionIndex < m_DebugActions.Length; ++actionIndex) 270 { 271 UpdateAction(actionIndex); 272 SampleAction(actionIndex); 273 } 274 } 275 276 internal float GetAction(DebugAction action) 277 { 278 return m_DebugActionStates[(int)action].actionState; 279 } 280 281 internal bool GetActionToggleDebugMenuWithTouch() 282 { 283#if USE_INPUT_SYSTEM 284 if (!EnhancedTouchSupport.enabled) 285 return false; 286 287 var touches = InputSystem.EnhancedTouch.Touch.activeTouches; 288 var touchCount = touches.Count; 289 InputSystem.TouchPhase? expectedTouchPhase = null; 290#else 291 var touchCount = Input.touchCount; 292 TouchPhase? expectedTouchPhase = TouchPhase.Began; 293#endif 294 if (touchCount == 3) 295 { 296#if !USE_INPUT_SYSTEM 297 var touches = Input.touches; // Causes an allocation, which is why this is inside the condition 298#endif 299 foreach (var touch in touches) 300 { 301 // Gesture: 3-finger double-tap 302 if ((!expectedTouchPhase.HasValue || touch.phase == expectedTouchPhase.Value) && touch.tapCount == 2) 303 return true; 304 } 305 } 306 307 return false; 308 } 309 310 internal bool GetActionReleaseScrollTarget() 311 { 312#if USE_INPUT_SYSTEM 313 bool mouseWheelActive = Mouse.current != null && Mouse.current.scroll.ReadValue() != Vector2.zero; 314 bool touchSupported = Touchscreen.current != null; 315#else 316 bool mouseWheelActive = Input.mouseScrollDelta != Vector2.zero; 317 bool touchSupported = Input.touchSupported; 318#endif 319 return mouseWheelActive || touchSupported; // Touchscreens have general problems with scrolling, so it's disabled. 320 } 321 322 void RegisterInputs() 323 { 324#if UNITY_EDITOR && !USE_INPUT_SYSTEM 325 var inputEntries = new List<InputManagerEntry> 326 { 327 new InputManagerEntry { name = kEnableDebugBtn1, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "left ctrl", altBtnPositive = "joystick button 8" }, 328 new InputManagerEntry { name = kEnableDebugBtn2, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "backspace", altBtnPositive = "joystick button 9" }, 329 new InputManagerEntry { name = kResetBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "left alt", altBtnPositive = "joystick button 1" }, 330 new InputManagerEntry { name = kDebugNextBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page down", altBtnPositive = "joystick button 5" }, 331 new InputManagerEntry { name = kDebugPreviousBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page up", altBtnPositive = "joystick button 4" }, 332 new InputManagerEntry { name = kValidateBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "return", altBtnPositive = "joystick button 0" }, 333 new InputManagerEntry { name = kPersistentBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "right shift", altBtnPositive = "joystick button 2" }, 334 new InputManagerEntry { name = kMultiplierBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "left shift", altBtnPositive = "joystick button 3" }, 335 new InputManagerEntry { name = kDPadHorizontal, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "right", btnNegative = "left", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f }, 336 new InputManagerEntry { name = kDPadVertical, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "up", btnNegative = "down", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f }, 337 new InputManagerEntry { name = kDPadVertical, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Seventh, btnPositive = "up", btnNegative = "down", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f }, 338 new InputManagerEntry { name = kDPadHorizontal, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Sixth, btnPositive = "right", btnNegative = "left", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f }, 339 }; 340 341 InputRegistering.RegisterInputs(inputEntries); 342#endif 343 344#if USE_INPUT_SYSTEM 345 // Register input system actions 346 var enableAction = debugActionMap.AddAction(kEnableDebug, type: InputActionType.Button); 347 enableAction.AddCompositeBinding("ButtonWithOneModifier") 348 .With("Modifier", "<Gamepad>/rightStickPress") 349 .With("Button", "<Gamepad>/leftStickPress") 350 .With("Modifier", "<Keyboard>/leftCtrl") 351 .With("Button", "<Keyboard>/backspace"); 352 353 var resetAction = debugActionMap.AddAction(kResetBtn, type: InputActionType.Button); 354 resetAction.AddCompositeBinding("ButtonWithOneModifier") 355 .With("Modifier", "<Gamepad>/rightStickPress") 356 .With("Button", "<Gamepad>/b") 357 .With("Modifier", "<Keyboard>/leftAlt") 358 .With("Button", "<Keyboard>/backspace"); 359 360 var next = debugActionMap.AddAction(kDebugNextBtn, type: InputActionType.Button); 361 next.AddBinding("<Keyboard>/pageDown"); 362 next.AddBinding("<Gamepad>/rightShoulder"); 363 364 var previous = debugActionMap.AddAction(kDebugPreviousBtn, type: InputActionType.Button); 365 previous.AddBinding("<Keyboard>/pageUp"); 366 previous.AddBinding("<Gamepad>/leftShoulder"); 367 368 var validateAction = debugActionMap.AddAction(kValidateBtn, type: InputActionType.Button); 369 validateAction.AddBinding("<Keyboard>/enter"); 370 validateAction.AddBinding("<Gamepad>/a"); 371 372 var persistentAction = debugActionMap.AddAction(kPersistentBtn, type: InputActionType.Button); 373 persistentAction.AddBinding("<Keyboard>/rightShift"); 374 persistentAction.AddBinding("<Gamepad>/x"); 375 376 var multiplierAction = debugActionMap.AddAction(kMultiplierBtn, type: InputActionType.Value); 377 multiplierAction.AddBinding("<Keyboard>/leftShift"); 378 multiplierAction.AddBinding("<Gamepad>/y"); 379 380 var moveVerticalAction = debugActionMap.AddAction(kDPadVertical); 381 moveVerticalAction.AddCompositeBinding("1DAxis") 382 .With("Positive", "<Gamepad>/dpad/up") 383 .With("Negative", "<Gamepad>/dpad/down") 384 .With("Positive", "<Keyboard>/upArrow") 385 .With("Negative", "<Keyboard>/downArrow"); 386 387 var moveHorizontalAction = debugActionMap.AddAction(kDPadHorizontal); 388 moveHorizontalAction.AddCompositeBinding("1DAxis") 389 .With("Positive", "<Gamepad>/dpad/right") 390 .With("Negative", "<Gamepad>/dpad/left") 391 .With("Positive", "<Keyboard>/rightArrow") 392 .With("Negative", "<Keyboard>/leftArrow"); 393#endif 394 } 395 } 396 397 class DebugActionDesc 398 { 399#if USE_INPUT_SYSTEM 400 public InputAction buttonAction = null; 401#else 402 public string axisTrigger = ""; 403 public List<string[]> buttonTriggerList = new List<string[]>(); 404 public List<KeyCode[]> keyTriggerList = new List<KeyCode[]>(); 405#endif 406 public DebugActionRepeatMode repeatMode = DebugActionRepeatMode.Never; 407 public float repeatDelay; 408 } 409 410 class DebugActionState 411 { 412 enum DebugActionKeyType 413 { 414 Button, 415 Axis, 416 Key 417 } 418 419 DebugActionKeyType m_Type; 420#if USE_INPUT_SYSTEM 421 InputAction inputAction; 422#else 423 string[] m_PressedButtons; 424 string m_PressedAxis = ""; 425 KeyCode[] m_PressedKeys; 426#endif 427 bool[] m_TriggerPressedUp; 428 float m_Timer; 429 430 internal bool runningAction { get; private set; } 431 internal float actionState { get; private set; } 432 433 void Trigger(int triggerCount, float state) 434 { 435 actionState = state; 436 runningAction = true; 437 m_Timer = 0f; 438 439 m_TriggerPressedUp = new bool[triggerCount]; 440 for (int i = 0; i < m_TriggerPressedUp.Length; ++i) 441 m_TriggerPressedUp[i] = false; 442 } 443 444#if USE_INPUT_SYSTEM 445 public void TriggerWithButton(InputAction action, float state) 446 { 447 inputAction = action; 448 Trigger(action.bindings.Count, state); 449 } 450 451#else 452 public void TriggerWithButton(string[] buttons, float state) 453 { 454 m_Type = DebugActionKeyType.Button; 455 m_PressedButtons = buttons; 456 m_PressedAxis = ""; 457 Trigger(buttons.Length, state); 458 } 459 460 public void TriggerWithAxis(string axis, float state) 461 { 462 m_Type = DebugActionKeyType.Axis; 463 m_PressedAxis = axis; 464 Trigger(1, state); 465 } 466 467 public void TriggerWithKey(KeyCode[] keys, float state) 468 { 469 m_Type = DebugActionKeyType.Key; 470 m_PressedKeys = keys; 471 m_PressedAxis = ""; 472 Trigger(keys.Length, state); 473 } 474 475#endif 476 477 void Reset() 478 { 479 runningAction = false; 480 m_Timer = 0f; 481 m_TriggerPressedUp = null; 482 } 483 484 public void Update(DebugActionDesc desc) 485 { 486 // Always reset this so that the action can only be caught once until repeat/reset 487 actionState = 0f; 488 489 if (m_TriggerPressedUp != null) 490 { 491 m_Timer += Time.deltaTime; 492 493 for (int i = 0; i < m_TriggerPressedUp.Length; ++i) 494 { 495#if USE_INPUT_SYSTEM 496 if (inputAction != null) 497 m_TriggerPressedUp[i] |= Mathf.Approximately(inputAction.ReadValue<float>(), 0f); 498#else 499 if (m_Type == DebugActionKeyType.Button) 500 m_TriggerPressedUp[i] |= Input.GetButtonUp(m_PressedButtons[i]); 501 else if (m_Type == DebugActionKeyType.Axis) 502 m_TriggerPressedUp[i] |= Mathf.Approximately(Input.GetAxis(m_PressedAxis), 0f); 503 else 504 m_TriggerPressedUp[i] |= Input.GetKeyUp(m_PressedKeys[i]); 505#endif 506 } 507 508 bool allTriggerUp = true; 509 foreach (bool value in m_TriggerPressedUp) 510 allTriggerUp &= value; 511 512 if (allTriggerUp || (m_Timer > desc.repeatDelay && desc.repeatMode == DebugActionRepeatMode.Delay)) 513 Reset(); 514 } 515 } 516 } 517}