-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathModuleResourceLevelIndicator.cs
More file actions
135 lines (120 loc) · 4.2 KB
/
Copy pathModuleResourceLevelIndicator.cs
File metadata and controls
135 lines (120 loc) · 4.2 KB
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
using UnityEngine;
namespace IndicatorLights
{
/// <summary>
/// A controller that sets the display based on the empty/full percentage of a
/// particular resource.
/// </summary>
class ModuleResourceLevelIndicator : ModuleResourceIndicator, IToggle, IScalar
{
private IColorSource highSource = null;
private IColorSource mediumSource = null;
private IColorSource lowSource = null;
private IColorSource criticalSource = null;
private IColorSource emptySource = null;
/// <summary>
/// The color to display when the resource is full or nearly full.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string highColor = Colors.ToString(DefaultColor.HighResource);
/// <summary>
/// The color to display when the resource is partially full. (Or, depending on your
/// outlook on life, partially empty.)
/// </summary>
[KSPField]
[ColorSourceIDField]
public string mediumColor = Colors.ToString(DefaultColor.MediumResource);
/// <summary>
/// The color to display when the resource is mostly empty.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string lowColor = Colors.ToString(DefaultColor.LowResource);
/// <summary>
/// The color to display when the resource is almost completely empty.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string criticalColor = ColorSources.Pulsate(ColorSources.Constant(DefaultColor.LowResource), 1200, 1f, 0.6f).ColorSourceID;
/// <summary>
/// The color to display when the resource is completely empty.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string emptyColor = Colors.ToString(DefaultColor.Off);
/// <summary>
/// If resource content is above this fraction, display using the "high" color.
/// </summary>
[KSPField]
[StaticField]
public double highThreshold = 0.7;
/// <summary>
/// If resource content is below this fraction, display using the "low" color.
/// </summary>
[KSPField]
[StaticField]
public double lowThreshold = 0.3;
/// <summary>
/// If resource content is below this faction, use a pulsating animation for
/// the light's brightness.
/// </summary>
[KSPField]
[StaticField]
public double criticalThreshold = 0.03;
public override void ParseIDs()
{
base.ParseIDs();
highSource = FindColorSource(highColor);
mediumSource = FindColorSource(mediumColor);
lowSource = FindColorSource(lowColor);
criticalSource = FindColorSource(criticalColor);
emptySource = FindColorSource(emptyColor);
}
public override bool HasColor
{
get
{
return base.HasColor && CurrentSource.HasColor;
}
}
public override Color OutputColor
{
get { return CurrentSource.OutputColor; }
}
private IColorSource CurrentSource
{
get
{
if (!ToggleStatus) return emptySource;
double fraction = ScalarValue;
if (fraction > highThreshold) return highSource;
if (fraction < criticalThreshold) return criticalSource;
if (fraction < lowThreshold) return lowSource;
return mediumSource;
}
}
/// <summary>
/// IToggle implementation.
/// </summary>
public bool ToggleStatus
{
get
{
PartResource resource = Resource;
return (resource != null) && (resource.amount > 0.0);
}
}
/// <summary>
/// IScalar implementation.
/// </summary>
public double ScalarValue
{
get
{
PartResource resource = Resource;
return (resource == null) ? 0.0 : resource.amount / resource.maxAmount;
}
}
}
}