/
AnimationPlayerModule.cs
145 lines (129 loc) · 3.36 KB
/
AnimationPlayerModule.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace KerbalAnimation
{
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class EVAModuleAdder : MonoBehaviour
{
void Start()
{
KerbalEVAUtility.AddPartModule ("AnimationPlayerModule");
}
}
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class AnimationPlayerWindowHost : MonoBehaviour
{
public static AnimationPlayerWindowHost Instance
{get; private set;}
public AnimationPlayerWindow Player;
public static bool GUIOpen = false;
//events
public EventData<List<KerbalAnimationClip>> OnReloadAnimationClips = new EventData<List<KerbalAnimationClip>>("OnReloadAnimationClips");
private void Awake()
{
Instance = this;
}
private void Start()
{
Player = new AnimationPlayerWindow ();
}
private void OnGUI()
{
//return if we are not in a valid state to draw
if (!GUIHider.ShowUI)
return;
if (!HighLogic.LoadedSceneIsFlight || !FlightGlobals.ActiveVessel.isEVA)
return;
if (KerbalAnimationSuite.Instance.IsAnimating)
return;
if (GUIOpen)
{
Player.Draw ();
}
}
private void Update()
{
//return if we are not in a valid state to update
if (!HighLogic.LoadedSceneIsFlight || !FlightGlobals.ActiveVessel.isEVA)
return;
if (KerbalAnimationSuite.Instance.IsAnimating)
return;
Player.Update ();
}
}
public class AnimationPlayerModule : PartModule
{
public string OpenGUIName = "Open Animation Player";
public string CloseGUIName = "Close Animation Player";
private Animation _animation;
public Animation animation
{
get
{
if (_animation == null)
{
_animation = GetComponent<Animation> ();
}
return _animation;
}
}
//lifetime
public override void OnStart (StartState state)
{
AnimationPlayerWindowHost.Instance.OnReloadAnimationClips.Add (OnReloadAnimationClips);
}
public override void OnUpdate ()
{
Events ["ToggleGUI"].guiName = AnimationPlayerWindowHost.GUIOpen ? CloseGUIName : OpenGUIName;
for(int i = 0; i < 10; i++)
{
string buttonName = (i + 1).ToString ();
if (i >= 9)
buttonName = "0";
if (Input.GetKey (buttonName))
{
bool shift = Input.GetKey (KeyCode.LeftShift);
if(!shift && FlightGlobals.ActiveVessel != vessel)
{
continue;
}
var clip = AnimationPlayerWindowHost.Instance.Player.GetNumberKeyClip (i);
if(clip != null && !animation.IsPlaying(clip.Name))
{
if (AnimationPlayerWindow.Loop)
PlayAnimation (clip.Name, WrapMode.Loop);
else
PlayAnimation (clip.Name, WrapMode.Once);
}
}
}
}
public void PlayAnimation(string name, WrapMode wrapMode)
{
var state = animation [name];
if (state == null || animation.GetClip(name) == null)
return;
state.wrapMode = wrapMode;
animation.CrossFade (name, 0.2f * state.length, PlayMode.StopSameLayer);
}
//events
private void OnReloadAnimationClips (List<KerbalAnimationClip> clips)
{
//initialize all of the clips with this kerbal
foreach (var clip in clips)
{
clip.Initialize (animation, transform);
}
}
//KSPEvents
[KSPEvent(guiName = "Open GUI", guiActiveUnfocused = false, guiActive = true)]
public void ToggleGUI()
{
AnimationPlayerWindowHost.GUIOpen = !AnimationPlayerWindowHost.GUIOpen;
}
}
}