-
Notifications
You must be signed in to change notification settings - Fork 633
/
Copy pathProgram.UI.cs
469 lines (383 loc) · 16.3 KB
/
Program.UI.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
using System;
using System.Collections.Generic;
using ImGuiNET;
using Robust.Shared.Maths;
using static ImGuiNET.ImGui;
using Color = System.Drawing.Color;
using Vector2 = System.Numerics.Vector2;
using RobustVec2 = System.Numerics.Vector2;
using static Content.Server.Power.Pow3r.PowerState;
namespace Pow3r
{
internal sealed partial class Program
{
private bool _showDemo;
private Dictionary<NodeId, DisplayLoad> _displayLoads = new();
private Dictionary<NodeId, DisplayBattery> _displayBatteries = new();
private Dictionary<NodeId, DisplayNetwork> _displayNetworks = new();
private Dictionary<NodeId, DisplaySupply> _displaySupplies = new();
private void DoUI(float frameTime)
{
if (BeginMainMenuBar())
{
_showDemo ^= MenuItem("Demo");
EndMainMenuBar();
}
SetNextWindowSize(new Vector2(150, 200));
Begin("CreateButtons",
ImGuiWindowFlags.NoTitleBar |
ImGuiWindowFlags.NoCollapse |
ImGuiWindowFlags.NoResize);
if (Button("Generator"))
{
var supply = new Supply();
_state.Supplies.Allocate(out supply.Id) = supply;
_displaySupplies.Add(supply.Id, new DisplaySupply());
}
if (Button("Load"))
{
var load = new Load();
_state.Loads.Allocate(out load.Id) = load;
_displayLoads.Add(load.Id, new DisplayLoad());
}
if (Button("Network"))
{
var network = new Network();
_state.Networks.Allocate(out network.Id) = network;
_state.GroupedNets = null;
_displayNetworks.Add(network.Id, new DisplayNetwork());
}
if (Button("Battery"))
{
var battery = new Battery();
_state.Batteries.Allocate(out battery.Id) = battery;
_displayBatteries.Add(battery.Id, new DisplayBattery());
_state.GroupedNets = null;
}
Checkbox("Paused", ref _paused);
SliderInt("TPS", ref _tps, 1, 120);
SetNextItemWidth(-1);
Combo("", ref _currentSolver, _solverNames, _solverNames.Length);
if (Button("Single step"))
RunSingleStep();
End();
Begin("Simulating timing");
PlotLines("Tick time (ms)", ref _simTickTimes[0], MaxTickData, _tickDataIdx + 1,
$"{_simTickTimes[_tickDataIdx]:N2}",
0,
0.1f, new Vector2(250, 150));
End();
Begin("Frame timings");
PlotLines("Frame (ms)", ref _frameTimings[0], _frameTimings.Length, _frameTimeIdx + 1,
$"{_frameTimings[_frameTimeIdx]:N2}",
0,
33.333f, new Vector2(250, 150));
End();
{
Begin("Memory");
var heap = GC.GetTotalMemory(false);
Text($"Managed heap: {heap>>20} MiB");
End();
}
foreach (var network in _state.Networks.Values)
{
var displayNetwork = _displayNetworks[network.Id];
Begin($"Network {network.Id}##Gen{network.Id}");
Text($"Height: {network.Height}");
displayNetwork.CurrentWindowPos = CalcWindowCenter();
if (Button("Delete"))
{
_remQueue.Enqueue(network);
if (_linking == network)
{
_linking = null;
}
}
SameLine();
if (_linking != null)
{
if (_linking == network && Button("Cancel"))
{
_linking = null;
}
}
else
{
if (Button("Link..."))
{
_linking = network;
}
}
End();
}
foreach (var load in _state.Loads.Values)
{
var displayLoad = _displayLoads[load.Id];
Begin($"Load {load.Id}##Load{load.Id}");
Checkbox("Enabled", ref load.Enabled);
SliderFloat("Desired", ref load.DesiredPower, 0, 1000, "%.0f W");
displayLoad.CurrentWindowPos = CalcWindowCenter();
PlotLines("", ref displayLoad.ReceivedPowerData[0], MaxTickData, _tickDataIdx + 1,
$"Receiving: {load.ReceivingPower:N1} W",
0,
load.DesiredPower, new Vector2(250, 150));
if (Button("Delete"))
{
_remQueue.Enqueue(load);
}
SameLine();
if (_linking != null)
{
if (Button("Link"))
{
_linking.Loads.Add(load.Id);
_linking = null;
RefreshLinks();
}
}
else
{
if (load.LinkedNetwork != default && Button("Unlink"))
{
var net = _state.Networks[load.LinkedNetwork];
net.Loads.Remove(load.Id);
load.LinkedNetwork = default;
}
}
End();
}
foreach (var supply in _state.Supplies.Values)
{
var displaySupply = _displaySupplies[supply.Id];
Begin($"Generator {supply.Id}##Gen{supply.Id}");
Checkbox("Enabled", ref supply.Enabled);
SliderFloat("Available", ref supply.MaxSupply, 0, 1000, "%.0f W");
SliderFloat("Ramp", ref supply.SupplyRampRate, 0, 100, "%.0f W/s");
SliderFloat("Tolerance", ref supply.SupplyRampTolerance, 0, 100, "%.0f W");
displaySupply.CurrentWindowPos = CalcWindowCenter();
Text($"Ramp Position: {supply.SupplyRampPosition:N1}");
PlotLines("", ref displaySupply.SuppliedPowerData[0], MaxTickData, _tickDataIdx + 1,
$"Supply: {supply.CurrentSupply:N1} W",
0, supply.MaxSupply, new Vector2(250, 150));
if (Button("Delete"))
{
_remQueue.Enqueue(supply);
}
SameLine();
if (_linking != null)
{
if (Button("Link"))
{
_linking.Supplies.Add(supply.Id);
_linking = null;
RefreshLinks();
}
}
else
{
if (supply.LinkedNetwork != default && Button("Unlink"))
{
var net = _state.Networks[supply.LinkedNetwork];
net.Supplies.Remove(supply.Id);
supply.LinkedNetwork = default;
}
}
End();
}
foreach (var battery in _state.Batteries.Values)
{
var displayBattery = _displayBatteries[battery.Id];
Begin($"Battery {battery.Id}##Bat{battery.Id}");
Checkbox("Enabled", ref battery.Enabled);
Checkbox("CanDischarge", ref battery.CanDischarge);
Checkbox("CanCharge", ref battery.CanCharge);
SliderFloat("Capacity", ref battery.Capacity, 0, 100000, "%.0f J");
SliderFloat("Max charge rate", ref battery.MaxChargeRate, 0, 1000, "%.0f W");
SliderFloat("Max supply", ref battery.MaxSupply, 0, 1000, "%.0f W");
SliderFloat("Ramp", ref battery.SupplyRampRate, 0, 100, "%.0f W/s");
SliderFloat("Tolerance", ref battery.SupplyRampTolerance, 0, 100, "%.0f W");
var percent = 100 * battery.Efficiency;
SliderFloat("Efficiency", ref percent, 0, 100, "%.0f %%");
battery.Efficiency = percent / 100;
displayBattery.CurrentWindowPos = CalcWindowCenter();
SliderFloat("Ramp position", ref battery.SupplyRampPosition, 0, battery.MaxSupply, "%.0f W");
PlotLines("", ref displayBattery.SuppliedPowerData[0], MaxTickData, _tickDataIdx + 1,
$"OUT: {battery.CurrentSupply:N1} W",
0, battery.MaxSupply + 1000, new Vector2(250, 75));
PlotLines("", ref displayBattery.ReceivingPowerData[0], MaxTickData, _tickDataIdx + 1,
$"IN: {battery.CurrentReceiving:N1} W",
0, battery.MaxChargeRate + 1000, new Vector2(250, 75));
PlotLines("", ref displayBattery.StoredPowerData[0], MaxTickData, _tickDataIdx + 1,
$"Charge: {battery.CurrentStorage:N1} J",
0, battery.Capacity, new Vector2(250, 75));
if (Button("Delete"))
{
_remQueue.Enqueue(battery);
}
SameLine();
if (_linking != null)
{
if (battery.LinkedNetworkCharging == default && Button("Link as load"))
{
_linking.BatteryLoads.Add(battery.Id);
_state.GroupedNets = null;
_linking = null;
RefreshLinks();
}
else
{
SameLine();
if (battery.LinkedNetworkDischarging == default && Button("Link as supply"))
{
_linking.BatterySupplies.Add(battery.Id);
_state.GroupedNets = null;
_linking = null;
RefreshLinks();
}
}
}
else
{
if (battery.LinkedNetworkCharging != default && Button("Unlink loading"))
{
var net = _state.Networks[battery.LinkedNetworkCharging];
net.BatteryLoads.Remove(battery.Id);
_state.GroupedNets = null;
battery.LinkedNetworkCharging = default;
}
else
{
SameLine();
if (battery.LinkedNetworkDischarging != default && Button("Unlink supplying"))
{
var net = _state.Networks[battery.LinkedNetworkDischarging];
net.BatterySupplies.Remove(battery.Id);
_state.GroupedNets = null;
battery.LinkedNetworkDischarging = default;
}
}
}
if (Button("Empty"))
battery.CurrentStorage = 0;
SameLine();
if (Button("Fill"))
battery.CurrentStorage = battery.Capacity;
End();
}
var bgDrawList = GetBackgroundDrawList();
foreach (var network in _state.Networks.Values)
{
var displayNet = _displayNetworks[network.Id];
foreach (var supplyId in network.Supplies)
{
var supply = _displaySupplies[supplyId];
DrawArrowLine(bgDrawList, displayNet.CurrentWindowPos, supply.CurrentWindowPos, Color.LawnGreen);
}
foreach (var loadId in network.Loads)
{
var load = _displayLoads[loadId];
DrawArrowLine(bgDrawList, load.CurrentWindowPos, displayNet.CurrentWindowPos, Color.Red);
}
foreach (var batteryId in network.BatteryLoads)
{
var battery = _displayBatteries[batteryId];
DrawArrowLine(bgDrawList, battery.CurrentWindowPos, displayNet.CurrentWindowPos, Color.Purple);
}
foreach (var batteryId in network.BatterySupplies)
{
var battery = _displayBatteries[batteryId];
DrawArrowLine(bgDrawList, displayNet.CurrentWindowPos, battery.CurrentWindowPos, Color.Cyan);
}
}
if (_showDemo)
{
ShowDemoWindow();
}
var reLink = false;
while (_remQueue.TryDequeue(out var item))
{
switch (item)
{
case Network n:
_state.Networks.Free(n.Id);
_displayNetworks.Remove(n.Id);
_state.GroupedNets = null;
reLink = true;
break;
case Supply s:
_state.Supplies.Free(s.Id);
_state.Networks.Values.ForEach(n => n.Supplies.Remove(s.Id));
_displaySupplies.Remove(s.Id);
break;
case Load l:
_state.Loads.Free(l.Id);
_state.Networks.Values.ForEach(n => n.Loads.Remove(l.Id));
_displayLoads.Remove(l.Id);
break;
case Battery b:
_state.Batteries.Free(b.Id);
_state.Networks.Values.ForEach(n => n.BatteryLoads.Remove(b.Id));
_state.Networks.Values.ForEach(n => n.BatterySupplies.Remove(b.Id));
_displayBatteries.Remove(b.Id);
_state.GroupedNets = null;
break;
}
}
if (reLink)
RefreshLinks();
}
private void DrawArrowLine(ImDrawListPtr ptr, Vector2 a, Vector2 b, Color color)
{
// A: to
// B: from
const float wingLength = 15;
const float thickness = 3;
var cvtColor = CvtColor(color);
ptr.AddLine(a, b, cvtColor, thickness);
var angleA = Angle.FromDegrees(45);
var angleB = Angle.FromDegrees(-45);
var mid = (a + b) / 2;
var dir = -Vector2.Normalize(a - b);
var rVec = new RobustVec2(dir.X, dir.Y);
var wingADir = CvtVec(angleA.RotateVec(rVec));
var wingBDir = CvtVec(angleB.RotateVec(rVec));
var wingA = wingADir * wingLength + mid;
var wingB = wingBDir * wingLength + mid;
ptr.AddLine(mid, wingA, cvtColor, thickness);
ptr.AddLine(mid, wingB, cvtColor, thickness);
}
private static uint CvtColor(Color color)
{
return color.R | ((uint) color.G << 8) | ((uint) color.B << 16) | ((uint) color.A << 24);
}
private static Vector2 CalcWindowCenter()
{
return GetWindowPos() + GetWindowSize() / 2;
}
private static Vector2 CvtVec(RobustVec2 vec)
{
return new Vector2(vec.X, vec.Y);
}
private sealed class DisplayNetwork
{
public Vector2 CurrentWindowPos;
}
private sealed class DisplayBattery
{
public Vector2 CurrentWindowPos;
public readonly float[] ReceivingPowerData = new float[MaxTickData];
public readonly float[] SuppliedPowerData = new float[MaxTickData];
public readonly float[] StoredPowerData = new float[MaxTickData];
}
private sealed class DisplayLoad
{
public Vector2 CurrentWindowPos;
public readonly float[] ReceivedPowerData = new float[MaxTickData];
}
private sealed class DisplaySupply
{
public Vector2 CurrentWindowPos;
public readonly float[] SuppliedPowerData = new float[MaxTickData];
}
}
}