SanAndreasUnity/Assets/Scripts/UI/AnimationsWindow.cs

228 lines
5.7 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Importing.Animation;
using SanAndreasUnity.Behaviours;
using SanAndreasUnity.Utilities;
using System.Linq;
namespace SanAndreasUnity.UI {
public class AnimationsWindow : PauseMenuWindow {
private Vector2 m_scrollViewPos = Vector2.zero;
private bool m_displayPackages = true;
private bool m_displayWalkcycleAnims = false;
private bool m_displayAnimStats = false;
private float m_minScrollViewHeight = 200;
private float m_maxScrollViewHeight = 600;
private int m_selectedPackageIndex = 0;
AnimationsWindow() {
// set default parameters
this.windowName = "Animations";
this.useScrollView = false;
}
void Start () {
this.RegisterButtonInPauseMenu ();
// adjust rect
this.windowRect = Utilities.GUIUtils.GetCenteredRect( new Vector2( 500, Screen.height * 0.85f ) );
}
protected override void OnWindowGUI ()
{
bool playerExists = Ped.Instance != null;
// float headerHeight = m_displayAnimStats ? 300 : 100;
// m_headerScrollViewPos = GUILayout.BeginScrollView (m_headerScrollViewPos, GUILayout.Height(headerHeight));
if (playerExists)
Ped.Instance.shouldPlayAnims = !GUILayout.Toggle( !Ped.Instance.shouldPlayAnims, "Override player anims" );
m_displayPackages = GUILayout.Toggle(m_displayPackages, "Display packages");
m_displayWalkcycleAnims = GUILayout.Toggle( m_displayWalkcycleAnims, "Display walkcycle anims");
m_displayAnimStats = GUILayout.Toggle( m_displayAnimStats, "Display anim stats");
// display anim stats
if (m_displayAnimStats && playerExists) {
DisplayAnimStats ();
}
// GUILayout.EndScrollView ();
GUILayout.Space (10);
if (m_displayPackages)
{
// display loaded ifp packages and their anims
this.DisplayPackages (playerExists);
}
else
{
// display anim groups and their anims
this.DisplayAnimGroups (playerExists);
}
}
private void DisplayAnimStats ()
{
GUILayout.Space (5);
var model = Ped.Instance.PlayerModel;
int numActiveClips = model.AnimComponent.OfType<AnimationState>().Where(a => a.enabled).Count();
GUILayout.Label("Currently played anims [" + numActiveClips + "] :");
// display all currently played clips
foreach (AnimationState animState in model.AnimComponent) {
if (!animState.enabled)
continue;
DisplayStatsForAnim (animState);
}
if (model.LastAnimState != null) {
GUILayout.Space (3);
GUILayout.Label ("Last played anim:");
DisplayStatsForAnim (model.LastAnimState);
}
if (model.LastSecondaryAnimState != null) {
GUILayout.Space (3);
GUILayout.Label ("Last secondary played anim:");
DisplayStatsForAnim (model.LastSecondaryAnimState);
}
GUILayout.Space (7);
GUILayout.Label ("Root frame velocity: " + model.RootFrame.LocalVelocity);
}
private void DisplayStatsForAnim (AnimationState animState)
{
// GUILayout.BeginHorizontal ();
var clip = animState.clip;
GUILayout.Label (string.Format ("name: {0}, length: {1}, frame rate: {2}, wrap mode: {3}, speed: {4}, " +
"normalized speed: {5}, time: {6}, normalized time: {7}, time perc: {8}",
clip.name, animState.length, clip.frameRate, animState.wrapMode, animState.speed, animState.normalizedSpeed,
animState.time, animState.normalizedTime, animState.GetTimePerc ()
));
// GUILayout.EndHorizontal ();
}
private void DisplayAnimGroups(bool playerExists)
{
m_scrollViewPos = GUILayout.BeginScrollView (m_scrollViewPos, GUILayout.MinHeight(m_minScrollViewHeight), GUILayout.MaxHeight(m_maxScrollViewHeight));
float elementHeight = 25;
foreach (var pair in Importing.Animation.AnimationGroup.AllLoadedGroups) {
GUILayout.Space (5);
GUILayout.Label ("Name: " + pair.Key);
foreach (var pair2 in pair.Value) {
if (!m_displayWalkcycleAnims && pair2.Key == AnimGroup.WalkCycle)
continue;
GUILayout.Label ("Type: " + pair2.Key);
var animGroup = pair2.Value;
for (int i=0; i < animGroup.Animations.Length; i++) {
string animName = animGroup.Animations[i];
if (playerExists) {
// display button which will play the anim
if (GUILayout.Button (animName, GUILayout.Height(elementHeight))) {
this.PlayAnim(new AnimId(animGroup.Type, AnimIndexUtil.Get(i)));
}
} else {
GUILayout.Label (animName, GUILayout.Height(elementHeight));
}
}
}
}
GUILayout.EndScrollView();
}
void DisplayPackages(bool playerExists)
{
var packages = Importing.Conversion.Animation.Loaded.ToArray ();
if (packages.Length < 1)
{
GUILayout.Label ("There are no loaded ifp packages");
return;
}
float animHeight = 25;
GUILayout.Label ("Ifp name:");
m_selectedPackageIndex = GUILayout.Toolbar(m_selectedPackageIndex, packages.Select(p => p.Key).ToArray());
var package = packages [m_selectedPackageIndex].Value.AnimPackage;
var clips = package.Clips;
GUILayout.Space (10);
m_scrollViewPos = GUILayout.BeginScrollView (m_scrollViewPos, GUILayout.MinHeight(m_minScrollViewHeight), GUILayout.MaxHeight(m_maxScrollViewHeight));
for (int i = 0; i < clips.Length; i++)
{
var clip = clips [i];
if (playerExists)
{
if (GUILayout.Button (clip.Name, GUILayout.Height(animHeight)))
{
// play this anim
this.PlayAnim(new AnimId (package.FileName, clip.Name));
}
}
else
{
GUILayout.Label (clip.Name, GUILayout.Height(animHeight));
}
}
GUILayout.EndScrollView ();
}
void PlayAnim(AnimId animId)
{
Ped.Instance.PlayerModel.ResetModelState ();
var state = Ped.Instance.PlayerModel.PlayAnim (animId);
state.wrapMode = WrapMode.Loop;
}
}
}