-
Notifications
You must be signed in to change notification settings - Fork 7
/
AmazingCurveEditor.cs
306 lines (266 loc) · 9.63 KB
/
AmazingCurveEditor.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace AmazingCurveEditor
{
public class FloatString4 : IComparable<FloatString4>
{
public Vector4 floats;
public string[] strings;
public int CompareTo(FloatString4 other)
{
if (other == null)
{
return 1;
}
return floats.x.CompareTo(other.floats.x);
}
public FloatString4()
{
floats = new Vector4();
strings = new string[] {"0", "0", "0", "0"};
}
public FloatString4(float x, float y, float z, float w)
{
floats = new Vector4(x, y, z, w);
UpdateStrings();
}
public void UpdateFloats()
{
float x, y, z, w;
float.TryParse(strings[0], out x);
float.TryParse(strings[1], out y);
float.TryParse(strings[2], out z);
float.TryParse(strings[3], out w);
floats = new Vector4(x, y, z, w);
}
public void UpdateStrings()
{
strings = new string[] {floats.x.ToString(), floats.y.ToString(), floats.z.ToString(), floats.w.ToString()};
}
}
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class AmazingCurveEditor : MonoBehaviour
{
private int texWidth = 512;
private int texHeight = 128;
const int GraphLabels = 4;
const float labelSpace = 20f * (GraphLabels + 1) / GraphLabels;
private List<FloatString4> points = new List<FloatString4>();
private FloatCurve curve = new FloatCurve();
private bool curveNeedsUpdate = false;
private Rect winPos = new Rect();
private Texture2D graph;
private Vector2 scrollPos = new Vector2();
private string textVersion;
private bool showUI;
private string keyName = "key";
private float minY;
private float maxY;
public void Start()
{
winPos = new Rect(Screen.width / 2, Screen.height / 2, 512, 400);
graph = new Texture2D(texWidth, texHeight, TextureFormat.RGB24, false, true);
points.Add(new FloatString4(0, 0, 0, 0.02f));
points.Add(new FloatString4(100, 1, 0.02f, 0));
UpdateCurve();
}
private void OnGUI()
{
if (graph != null && showUI)
{
GUI.skin = AssetBase.GetGUISkin("KSP window 2");
winPos = GUILayout.Window(9384, winPos, WindowGUI, "Amazing Curve Editor");
}
}
private void OnDestroy()
{
if (graph != null)
Destroy(graph);
}
private void WindowGUI(int windowID)
{
GUILayout.BeginHorizontal(GUILayout.Height(texHeight));
Vector2 sizeMax = GUI.skin.label.CalcSize(new GUIContent(maxY.ToString("F3")));
Vector2 sizeMin = GUI.skin.label.CalcSize(new GUIContent(minY.ToString("F3")));
GUILayout.BeginVertical(GUILayout.MinWidth( Mathf.Max(sizeMin.x, sizeMax.x)));
for (int i = 0; i <= GraphLabels; i++)
{
GUILayout.Label((maxY - (maxY - minY) * i / GraphLabels).ToString("F3"), new GUIStyle(GUI.skin.label) { wordWrap = false });
if (i != GraphLabels) //only do it if it's not the last one
GUILayout.Space(texHeight / GraphLabels - labelSpace);
}
GUILayout.EndVertical();
GUILayout.Box(graph);
GUILayout.EndHorizontal();
FloatString4 excludePoint = null;
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.ExpandWidth(true), GUILayout.Height(200));
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
GUILayout.BeginVertical();
GUILayout.Label("X");
foreach (FloatString4 p in points)
{
string ns = GUILayout.TextField(p.strings[0]);
if (ns != p.strings[0])
{
p.strings[0] = ns;
p.UpdateFloats();
curveNeedsUpdate = true;
}
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
GUILayout.Label("Y");
foreach (FloatString4 p in points)
{
string ns = GUILayout.TextField(p.strings[1]);
if (ns != p.strings[1])
{
p.strings[1] = ns;
p.UpdateFloats();
curveNeedsUpdate = true;
}
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
GUILayout.Label("In Tangent");
foreach (FloatString4 p in points)
{
string ns = GUILayout.TextField(p.strings[2]);
if (ns != p.strings[2])
{
p.strings[2] = ns;
p.UpdateFloats();
curveNeedsUpdate = true;
}
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
GUILayout.Label("Out Tangent");
foreach (FloatString4 p in points)
{
string ns = GUILayout.TextField(p.strings[3]);
if (ns != p.strings[3])
{
p.strings[3] = ns;
p.UpdateFloats();
curveNeedsUpdate = true;
}
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
GUILayout.Label("Remove");
foreach (FloatString4 p in points)
{
if (GUILayout.Button("X"))
{
excludePoint = p;
}
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
GUILayout.EndScrollView();
if (excludePoint != null)
{
points.Remove(excludePoint);
curveNeedsUpdate = true;
}
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
if (GUILayout.Button("Add Node"))
{
if (points.Count > 0)
{
points.Add(new FloatString4(points.Last().floats.x + 1, points.Last().floats.y, points.Last().floats.z,points.Last().floats.w));
}
else
{
points.Add(new FloatString4(0, 0, 0, 0));
}
curveNeedsUpdate = true;
}
keyName = GUILayout.TextField(keyName, GUILayout.Width(80));
GUILayout.EndHorizontal();
string newT = GUILayout.TextArea(textVersion, GUILayout.ExpandWidth(true), GUILayout.Height(100));
if (newT != textVersion)
{
StringToCurve(newT);
}
GUI.DragWindow();
}
private void UpdateCurve()
{
points.Sort();
curve = new FloatCurve();
minY = float.MaxValue;
maxY = float.MinValue;
foreach (FloatString4 v in points)
{
curve.Add(v.floats.x, v.floats.y, v.floats.z, v.floats.w);
}
textVersion = CurveToString();
for (int x = 0; x < texWidth; x++)
{
for (int y = 0; y < texHeight; y++)
{
graph.SetPixel(x, y, Color.black);
}
float fY = curve.Evaluate(curve.minTime + curve.maxTime * x / (texWidth - 1));
minY = Mathf.Min(minY, fY);
maxY = Mathf.Max(maxY, fY);
}
for (int x = 0; x < texWidth; x++)
{
float step = texHeight / (float)GraphLabels;
for (int y = 0; y < GraphLabels; y++)
{
graph.SetPixel(x, Mathf.RoundToInt(y * step), Color.gray);
}
}
for (int x = 0; x < texWidth; x++)
{
float fY = curve.Evaluate(curve.minTime + curve.maxTime * x / (texWidth - 1));
graph.SetPixel(x, Mathf.RoundToInt((fY - minY) / (maxY - minY) * (texHeight - 1)), Color.green);
}
graph.Apply();
curveNeedsUpdate = false;
}
private void Update()
{
if (GameSettings.MODIFIER_KEY.GetKey() && Input.GetKeyDown(KeyCode.P))
{
showUI = !showUI;
}
if (curveNeedsUpdate)
{
UpdateCurve();
}
}
private string CurveToString()
{
string buff = "";
foreach (FloatString4 p in points)
{
buff += keyName + " = " + p.floats.x + " " + p.floats.y + " " + p.floats.z + " " + p.floats.w + "\n";
}
return buff;
}
private void StringToCurve(string data)
{
points = new List<FloatString4>();
string[] lines = data.Split('\n');
foreach (string line in lines)
{
string[] pcs = line.Split(new char[] {'=', ' '}, StringSplitOptions.RemoveEmptyEntries);
if ((pcs.Length >= 5) && (pcs[0] == "key"))
{
FloatString4 nv = new FloatString4();
nv.strings = new string[] {pcs[1], pcs[2], pcs[3], pcs[4]};
nv.UpdateFloats();
points.Add(nv);
}
}
curveNeedsUpdate = true;
}
}
}