rimworld-animation-studio/Assets/Scripts/Managers/InputManager.cs

317 lines
10 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace RimWorldAnimationStudio
{
public class InputManager : Singleton<InputManager>
{
2022-10-12 05:22:29 +00:00
public Transform dialogBoxes;
2022-10-09 02:15:28 +00:00
private float lastUpdate = -1f;
private float timeBetweenUpdates = 0.15f;
private float largeStep = 0.1f;
private float smallStep = 0.03f;
2022-10-12 05:22:29 +00:00
public bool CanRepeatThisUpdate()
2022-10-09 02:15:28 +00:00
{
if (Time.unscaledTime > lastUpdate + timeBetweenUpdates)
{
lastUpdate = Time.unscaledTime;
return true;
}
return false;
}
2022-10-12 05:22:29 +00:00
public bool IsModifierKeyHeld()
{
2022-10-12 05:22:29 +00:00
if (Input.GetKey(KeyCode.LeftShift)) return true;
if (Input.GetKey(KeyCode.LeftControl)) return true;
if (Input.GetKey(KeyCode.LeftAlt)) return true;
if (Input.GetKey(KeyCode.LeftCommand)) return true;
if (Input.GetKey(KeyCode.RightShift)) return true;
if (Input.GetKey(KeyCode.RightControl)) return true;
if (Input.GetKey(KeyCode.RightAlt)) return true;
if (Input.GetKey(KeyCode.RightCommand)) return true;
return false;
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public bool IsDialogBoxIsActive()
{
foreach (Transform child in dialogBoxes)
{ if (child.gameObject.activeSelf) return true; }
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
return false;
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void Update()
{
if (IsDialogBoxIsActive()) return;
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
bool canRepeatThisUpdate = CanRepeatThisUpdate();
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
foreach (Keybind keybind in KeybindConfig.Instance.GetAllKeybinds())
2022-10-09 02:15:28 +00:00
{
2022-10-12 05:22:29 +00:00
if (IsModifierKeyHeld() && keybind.keyModifiers.NullOrEmpty()) goto nextKeybind;
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
foreach (KeyCode modKeyCode in keybind.keyModifiers)
{ if (Input.GetKey(modKeyCode) == false) goto nextKeybind; }
if (keybind.repeatable && canRepeatThisUpdate)
{ if (Input.GetKey(keybind.keyCode) == false) goto nextKeybind; }
2022-10-09 02:15:28 +00:00
else
2022-10-12 05:22:29 +00:00
{ if (Input.GetKeyDown(keybind.keyCode) == false) goto nextKeybind; }
//Debug.Log(keybind.command);
Invoke(keybind.command, 0f); return;
nextKeybind:;
2022-10-09 02:15:28 +00:00
}
2022-10-12 05:22:29 +00:00
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void QuitApplication()
{
ApplicationManager.Instance.TryToCloseApplication();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void NewAnimation()
{
ApplicationManager.Instance.TryToMakeNewAnimation();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void SaveAnimation()
{
ApplicationManager.Instance.TryToSaveAnimation();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void SaveAnimationAs()
{
ApplicationManager.Instance.TryToSaveAnimationAs();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void LoadAnimation()
{
ApplicationManager.Instance.TryToLoadAnimation();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void UndoAction()
{
if (Workspace.animationDef == null) return;
Workspace.Instance.Undo();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void RedoAction()
{
if (Workspace.animationDef == null) return;
Workspace.Instance.Redo();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void ToggleAnimationPreview()
{
if (Workspace.animationDef == null) return;
AnimationController.Instance.ToggleAnimation();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void CopyKeyframes()
{
if (Workspace.animationDef == null) return;
AnimationController.Instance.CopyPawnKeyframes();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void PasteKeyframes()
{
if (Workspace.animationDef == null) return;
AnimationController.Instance.PastePawnKeyframes();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void DeleteKeyframes()
{
if (Workspace.animationDef == null) return;
AnimationController.Instance.RemovePawnKeyframe();
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void ActorMovementMode()
{
2022-10-12 06:16:08 +00:00
//if (Workspace.animationDef == null) return;
2022-10-12 05:22:29 +00:00
AnimationController.Instance.ToggleActorManipulationMode(0);
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void ActorRotateMode()
{
2022-10-12 06:16:08 +00:00
//if (Workspace.animationDef == null) return;
2022-10-12 05:22:29 +00:00
AnimationController.Instance.ToggleActorManipulationMode(1);
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void ActorFacingMode()
{
2022-10-12 06:16:08 +00:00
//if (Workspace.animationDef == null) return;
2022-10-12 05:22:29 +00:00
AnimationController.Instance.ToggleActorManipulationMode(2);
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void AdjustActorUpward()
{
if (Workspace.animationDef == null) return;
ActorKeyframeCard.Instance.AdjustActor(Vector2.up * largeStep);
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void AdjustActorDownward()
{
if (Workspace.animationDef == null) return;
ActorKeyframeCard.Instance.AdjustActor(Vector2.down * largeStep);
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void AdjustActorLeftward()
{
if (Workspace.animationDef == null) return;
ActorKeyframeCard.Instance.AdjustActor(Vector2.left * largeStep);
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void AdjustActorRightward()
{
if (Workspace.animationDef == null) return;
ActorKeyframeCard.Instance.AdjustActor(Vector2.right * largeStep);
}
public void AdjustActorUpwardSmall()
{
if (Workspace.animationDef == null) return;
ActorKeyframeCard.Instance.AdjustActor(Vector2.up * smallStep);
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void AdjustActorDownwardSmall()
{
if (Workspace.animationDef == null) return;
ActorKeyframeCard.Instance.AdjustActor(Vector2.down * smallStep);
}
public void AdjustActorLeftwardSmall()
{
if (Workspace.animationDef == null) return;
ActorKeyframeCard.Instance.AdjustActor(Vector2.left * smallStep);
}
public void AdjustActorRightwardSmall()
{
if (Workspace.animationDef == null) return;
ActorKeyframeCard.Instance.AdjustActor(Vector2.right * smallStep);
}
public void CycleActorBodyPartSelecion()
{
if (Workspace.animationDef == null) return;
ActorBody actorBody = AnimationController.Instance.actorBodies.GetChild(Workspace.actorID).GetComponent<ActorBody>();
List<ActorBodyPart> actorBodyParts = actorBody.GetComponentsInChildren<ActorBodyPart>().Where(x => x.gameObject.activeInHierarchy)?.ToList();
if (actorBodyParts.NullOrEmpty()) return;
if (Workspace.selectedBodyPart == null)
{ actorBodyParts[0].Activate(); return; }
else
2022-10-09 02:15:28 +00:00
{
2022-10-12 05:22:29 +00:00
for (int i = 0; i < actorBodyParts.Count; i++)
2022-10-09 02:15:28 +00:00
{
2022-10-12 05:22:29 +00:00
ActorBodyPart part = actorBodyParts[i];
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
if (part == Workspace.selectedBodyPart)
{
if (i < actorBodyParts.Count - 1)
{ actorBodyParts[i + 1].Activate(); return; }
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
else
{ actorBody.Activate(); return; }
}
}
2022-10-09 02:15:28 +00:00
}
2022-10-12 05:22:29 +00:00
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void ToPreviousActor()
{
if (Workspace.animationDef == null) return;
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
Workspace.selectedBodyPart = null;
Workspace.actorID = Mathf.Clamp(Workspace.actorID - 1, 0, Workspace.animationDef.actors.Count - 1);
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void ToNextActor()
{
if (Workspace.animationDef == null) return;
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
Workspace.selectedBodyPart = null;
Workspace.actorID = Mathf.Clamp(Workspace.actorID + 1, 0, Workspace.animationDef.actors.Count - 1);
}
2022-10-09 02:15:28 +00:00
2022-10-12 05:22:29 +00:00
public void ToPreviousTick()
{
if (Workspace.animationDef == null) return;
AnimationController.Instance.stageTick = Mathf.Clamp(AnimationController.Instance.stageTick - 1, 1, Workspace.StageWindowSize);
}
2022-10-12 05:22:29 +00:00
public void ToNextTick()
{
if (Workspace.animationDef == null) return;
AnimationController.Instance.stageTick = Mathf.Clamp(AnimationController.Instance.stageTick + 1, 1, Workspace.StageWindowSize);
}
2022-10-12 05:22:29 +00:00
public void ToFirstTick()
{
if (Workspace.animationDef == null) return;
AnimationController.Instance.stageTick = 1;
}
2022-10-12 05:22:29 +00:00
public void ToLastTick()
{
if (Workspace.animationDef == null) return;
AnimationController.Instance.stageTick = Workspace.StageWindowSize;
}
2022-10-12 05:22:29 +00:00
public void ToPreviousKey()
{
if (Workspace.animationDef == null) return;
PawnKeyframe keyframe = Workspace.Instance.GetPreviousKeyframe(Workspace.actorID);
if (keyframe != null) AnimationController.Instance.stageTick = keyframe.atTick.Value;
}
2022-10-12 05:22:29 +00:00
public void ToNextKey()
{
if (Workspace.animationDef == null) return;
PawnKeyframe keyframe = Workspace.Instance.GetNextKeyframe(Workspace.actorID);
if (keyframe != null) AnimationController.Instance.stageTick = keyframe.atTick.Value;
}
2022-10-12 05:22:29 +00:00
public void ToPreviousStage()
{
if (Workspace.animationDef == null) return;
2022-10-12 05:22:29 +00:00
int prevStageID = Workspace.stageID;
Workspace.stageID = Mathf.Clamp(Workspace.stageID - 1, 0, Workspace.animationDef.animationStages.Count - 1);
2022-10-12 05:22:29 +00:00
if (Workspace.stageID != prevStageID)
{ Workspace.Instance.RecordEvent("Stage selected"); }
}
public void ToNextStage()
{
if (Workspace.animationDef == null) return;
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"); }
}
public void CenterView()
{
Camera.main.GetComponent<CameraController>().ResetCamera();
}
}
}