using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UnityEngine; namespace RimWorldAnimationStudio { public class InputManager : Singleton { private float lastUpdate = -1f; private float timeBetweenUpdates = 0.15f; private float largeStep = 0.1f; private float smallStep = 0.03f; public bool CanUpdate() { if (Time.unscaledTime > lastUpdate + timeBetweenUpdates) { lastUpdate = Time.unscaledTime; return true; } return false; } public void Update() { // Make new animation if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand)) && Input.GetKeyDown(KeyCode.N)) { ApplicationManager.Instance.TryToMakeNewAnimation(); } // Save animation else if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand)) && Input.GetKeyDown(KeyCode.S)) { ApplicationManager.Instance.TryToSaveAnimation(); } // Load animation else if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand)) && Input.GetKeyDown(KeyCode.L)) { ApplicationManager.Instance.TryToLoadAnimation(); } // Exit if animationDef has not be loaded if (Workspace.animationDef == null) return; // Play / pause if (Input.GetKeyDown(KeyCode.Space)) { AnimationController.Instance.ToggleAnimation(); } // Move / switch actors else if (Input.GetKey(KeyCode.UpArrow) && CanUpdate()) { if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand)) { Workspace.selectedBodyPart = null; Workspace.actorID = Mathf.Clamp(Workspace.actorID - 1, 0, Workspace.animationDef.actors.Count - 1); } else if (Input.GetKey(KeyCode.LeftShift)) { ActorKeyframeCard.Instance.AdjustActor(Vector2.up * smallStep); } else { ActorKeyframeCard.Instance.AdjustActor(Vector2.up * largeStep); } } else if (Input.GetKey(KeyCode.DownArrow) && CanUpdate()) { if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand)) { Workspace.selectedBodyPart = null; Workspace.actorID = Mathf.Clamp(Workspace.actorID + 1, 0, Workspace.animationDef.actors.Count - 1); } else if (Input.GetKey(KeyCode.LeftShift)) { ActorKeyframeCard.Instance.AdjustActor(Vector2.down * smallStep); } else { ActorKeyframeCard.Instance.AdjustActor(Vector2.down * largeStep); } } else if (Input.GetKey(KeyCode.LeftArrow) && CanUpdate()) { if (Input.GetKey(KeyCode.LeftShift)) { ActorKeyframeCard.Instance.AdjustActor(Vector2.left * smallStep); } else { ActorKeyframeCard.Instance.AdjustActor(Vector2.left * largeStep); } } else if (Input.GetKey(KeyCode.RightArrow) && CanUpdate()) { if (Input.GetKey(KeyCode.LeftShift)) { ActorKeyframeCard.Instance.AdjustActor(Vector2.right * smallStep); } else { ActorKeyframeCard.Instance.AdjustActor(Vector2.right * largeStep); } } // Switch to actor panning else if (Input.GetKeyDown(KeyCode.Q)) { AnimationController.Instance.ToggleActorManipulationMode(0); } // Switch to actor rotating else if (Input.GetKeyDown(KeyCode.W)) { AnimationController.Instance.ToggleActorManipulationMode(1); } // SWitch to actor facing else if (Input.GetKeyDown(KeyCode.E)) { AnimationController.Instance.ToggleActorManipulationMode(2); } // Switch between body parts else if (Input.GetKeyDown(KeyCode.Tab)) { ActorBody actorBody = AnimationController.Instance.actorBodies.GetChild(Workspace.actorID).GetComponent(); List actorBodyParts = actorBody.GetComponentsInChildren().Where(x => x.gameObject.activeInHierarchy)?.ToList(); foreach(Transform c in actorBody.transform) { Debug.Log(c.gameObject + " " + c.gameObject.activeSelf); } if (actorBodyParts.NullOrEmpty()) return; if (Workspace.selectedBodyPart == null) { actorBodyParts[0].Activate(); return; } else { for (int i = 0; i < actorBodyParts.Count; i++) { ActorBodyPart part = actorBodyParts[i]; if (part == Workspace.selectedBodyPart) { if (i < actorBodyParts.Count - 1) { actorBodyParts[i + 1].Activate(); return; } else { actorBody.Activate(); return; } } } } } // Move along time lines else if (Input.GetKey(KeyCode.PageUp) && CanUpdate()) { if (Input.GetKey(KeyCode.LeftShift)) { PawnKeyframe keyframe = Workspace.Instance.GetPreviousKeyframe(Workspace.actorID); if (keyframe != null) AnimationController.Instance.stageTick = keyframe.atTick.Value; } else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand)) { AnimationController.Instance.stageTick = 1; } else { AnimationController.Instance.stageTick = Mathf.Clamp(AnimationController.Instance.stageTick - 1, 1, Workspace.StageWindowSize); } } else if (Input.GetKey(KeyCode.PageDown) && CanUpdate()) { if (Input.GetKey(KeyCode.LeftShift)) { PawnKeyframe keyframe = Workspace.Instance.GetNextKeyframe(Workspace.actorID); if (keyframe != null) AnimationController.Instance.stageTick = keyframe.atTick.Value; } else if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftCommand)) { AnimationController.Instance.stageTick = Workspace.StageWindowSize; } else { AnimationController.Instance.stageTick = Mathf.Clamp(AnimationController.Instance.stageTick + 1, 1, Workspace.StageWindowSize); } } // Switch stages else if (Input.GetKeyDown(KeyCode.Home)) { int prevStageID = Workspace.stageID; Workspace.stageID = Mathf.Clamp(Workspace.stageID - 1, 0, Workspace.animationDef.animationStages.Count - 1); if (Workspace.stageID != prevStageID) { Workspace.Instance.RecordEvent("Stage selected"); } } else if (Input.GetKeyDown(KeyCode.End)) { int prevStageID = Workspace.stageID; Workspace.stageID = Mathf.Clamp(Workspace.stageID + 1, 0, Workspace.animationDef.animationStages.Count - 1); if (Workspace.stageID != prevStageID) { Workspace.Instance.RecordEvent("Stage selected"); } } // Undo else if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.Z)) { Workspace.Instance.Undo(); } else if (Input.GetKey(KeyCode.LeftCommand) && Input.GetKeyDown(KeyCode.Z)) { Workspace.Instance.Undo(); } // Redo else if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.Y)) { Workspace.Instance.Redo(); } else if (Input.GetKey(KeyCode.LeftCommand) && Input.GetKey(KeyCode.LeftShift) && Input.GetKeyDown(KeyCode.Z)) { Workspace.Instance.Redo(); } // Copy keyframes else if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.C)) { AnimationController.Instance.CopyPawnKeyframes(); } else if (Input.GetKey(KeyCode.LeftCommand) && Input.GetKeyDown(KeyCode.C)) { AnimationController.Instance.CopyPawnKeyframes(); } // Paste keyframes else if (Input.GetKey(KeyCode.LeftControl) && Input.GetKeyDown(KeyCode.V)) { AnimationController.Instance.PastePawnKeyframes(); } else if (Input.GetKey(KeyCode.LeftCommand) && Input.GetKeyDown(KeyCode.X)) { AnimationController.Instance.PastePawnKeyframes(); } // Delete keyframes else if (Input.GetKey(KeyCode.Backspace) || Input.GetKey(KeyCode.Delete)) { AnimationController.Instance.RemovePawnKeyframe(); } } } }