-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathModuleScalarIndicator.cs
More file actions
143 lines (126 loc) · 4.47 KB
/
Copy pathModuleScalarIndicator.cs
File metadata and controls
143 lines (126 loc) · 4.47 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
136
137
138
139
140
141
142
143
using System;
using UnityEngine;
namespace IndicatorLights
{
/// <summary>
/// A scalar controller whose output is controlled by other scalars.
/// </summary>
class ModuleScalarIndicator : ModuleEmissiveController, IScalar
{
/// <summary>
/// Required. Specifies the input scalar that controls this module. Could be a simple
/// identifier (such as a module or controller name), or it could be parseable scalar
/// syntax such as "min(scalar1, scalar2)".
/// </summary>
[KSPField]
[ScalarIDField]
public string input = string.Empty;
/// <summary>
/// The color to display when the input scalar is high.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string highColor = Colors.ToString(DefaultColor.HighResource);
/// <summary>
/// The color to display when the input scalar is medium.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string mediumColor = Colors.ToString(DefaultColor.MediumResource);
/// <summary>
/// The color to display when the input scalar is low.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string lowColor = Colors.ToString(DefaultColor.LowResource);
/// <summary>
/// The color to display when the input scalar is very close to zero.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string criticalColor = "lowColor";
/// <summary>
/// The color to display when the input scalar is zero.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string absentColor = Colors.ToString(DefaultColor.Off);
/// <summary>
/// If input scalar is above this value, display using the "high" color.
/// </summary>
[KSPField]
[StaticField]
public double highThreshold = 0.7;
/// <summary>
/// If input scalar is below this value, display using the "low" color.
/// </summary>
[KSPField]
[StaticField]
public double lowThreshold = 0.3;
/// <summary>
/// If input scalar is below this value, display using the "critical" color.
/// </summary>
[KSPField]
[StaticField]
public double criticalThreshold = 0.03;
/// <summary>
/// Defines "effective zero". Values at or below this value will display using the "absent" color.
/// </summary>
[KSPField]
[StaticField]
public double epsilon = 0.0;
private bool isValid = false;
private IScalar inputScalar = null;
private IColorSource highSource = null;
private IColorSource mediumSource = null;
private IColorSource lowSource = null;
private IColorSource criticalSource = null;
private IColorSource absentSource = null;
public override void ParseIDs()
{
base.ParseIDs();
try
{
inputScalar = RequireScalar(input);
isValid = true;
}
catch (ArgumentException e)
{
Logging.Warn("Invalid input for " + Identifier + " on " + part.GetTitle() + ": " + e.Message);
}
highSource = FindColorSource(highColor);
mediumSource = FindColorSource(mediumColor);
lowSource = FindColorSource(lowColor);
criticalSource = FindColorSource(criticalColor);
absentSource = FindColorSource(absentColor);
}
public override bool HasColor
{
get
{
return base.HasColor && CurrentSource.HasColor;
}
}
public override Color OutputColor
{
get { return CurrentSource.OutputColor; }
}
public double ScalarValue
{
get { return isValid ? inputScalar.ScalarValue : 0.0; }
}
private IColorSource CurrentSource
{
get
{
if (!isValid) return ColorSources.ERROR;
double value = ScalarValue;
if (value <= epsilon) return absentSource;
if (value <= criticalThreshold) return criticalSource;
if (value <= lowThreshold) return lowSource;
if (value >= highThreshold) return highSource;
return mediumSource;
}
}
}
}