-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlowAnalyticsPanel.java
More file actions
402 lines (363 loc) · 15.6 KB
/
FlowAnalyticsPanel.java
File metadata and controls
402 lines (363 loc) · 15.6 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
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
package moddy.resflow.ui;
import init.resources.RESOURCE;
import init.sprite.UI.UI;
import moddy.resflow.analysis.ResourceFlowAnalyzer;
import moddy.resflow.analysis.ResourceFlowData;
import snake2d.SPRITE_RENDERER;
import snake2d.util.color.ColorImp;
import snake2d.util.gui.GuiSection;
import util.colors.GCOLOR;
import util.data.GETTER;
import util.gui.misc.GBox;
import util.gui.misc.GStat;
import util.gui.misc.GText;
import util.gui.table.GStaples;
import util.gui.table.GTableBuilder;
import util.info.GFORMAT;
/**
* Analytics panel showing detailed flow statistics for selected/hovered resources.
* Displays:
* - Production/consumption rates and deltas
* - Net flow and trend indicators
* - Storage status and projections
* - Haul statistics
* - Flow path breakdowns
*/
public class FlowAnalyticsPanel extends GuiSection {
private static final ColorImp COLOR_POSITIVE = new ColorImp(100, 255, 100); // Green
private static final ColorImp COLOR_NEGATIVE = new ColorImp(255, 100, 100); // Red
private static final ColorImp COLOR_MAINT = new ColorImp(255, 210, 80); // Amber
private final ResourceFlowAnalyzer analyzer;
private final GuiSection tableSection;
private RESOURCE currentResource = null;
public FlowAnalyticsPanel(ResourceFlowAnalyzer analyzer, int width) {
this.analyzer = analyzer;
body().setWidth(width);
body().setHeight(500); // Increased for graphs
tableSection = new GuiSection();
tableSection.body().setDim(width - 24, 300);
rebuildTable();
add(tableSection, 12, 40);
// Add historical graphs section
GuiSection graphs = new GuiSection();
graphs.body().setDim(width - 24, 100);
// Bar count must match available width; GStaples will early-out if bar width < 3.
// Also, hover math divides by (body.width/amount) so amount must never be 0.
final int graphW = Math.max(40, (width - 40) / 2);
final int graphBarsMax = Math.max(10, ResourceFlowData.ResourceFlowStats.DEFAULT_GRAPH_SAMPLES);
final int graphBars = Math.max(10, Math.min(graphBarsMax, graphW / 3));
// Production history graph
GStaples prodGraph = new GStaples(graphBars) {
@Override
public double getValue(int i) {
if (currentResource == null) return 0;
ResourceFlowData.ResourceFlowStats stats = analyzer.getData().getStats(currentResource);
int hs = stats.getHistorySize();
if (hs <= 0) return 0;
int samples = Math.min(hs, graphBars);
int offset = graphBars - samples;
if (i < offset) return 0;
int fromOldest = i - offset;
return stats.productionAtOldestIndex(fromOldest);
}
@Override
public void setColor(ColorImp c, int i, double v) {
c.set(COLOR_POSITIVE);
}
@Override
public void hover(GBox box, int i) {
if (currentResource == null) return;
box.text().add("Produced: ");
GFORMAT.i(box.text(), (int) getValue(i));
}
};
prodGraph.body().setDim(graphW, 80);
graphs.add(prodGraph);
// Consumption history graph
GStaples consGraph = new GStaples(graphBars) {
@Override
public double getValue(int i) {
if (currentResource == null) return 0;
ResourceFlowData.ResourceFlowStats stats = analyzer.getData().getStats(currentResource);
int hs = stats.getHistorySize();
if (hs <= 0) return 0;
int samples = Math.min(hs, graphBars);
int offset = graphBars - samples;
if (i < offset) return 0;
int fromOldest = i - offset;
return stats.consumptionAtOldestIndex(fromOldest);
}
@Override
public void setColor(ColorImp c, int i, double v) {
c.set(COLOR_NEGATIVE);
}
@Override
public void hover(GBox box, int i) {
if (currentResource == null) return;
box.text().add("Consumed: ");
GFORMAT.i(box.text(), (int) getValue(i));
}
};
consGraph.body().setDim(graphW, 80);
graphs.addRightC(8, consGraph);
add(graphs, 12, 360);
// Labels for graphs
GStat graphLabels = new GStat() {
@Override
public void update(GText text) {
text.clear();
if (currentResource != null) {
ResourceFlowData.ResourceFlowStats stats = analyzer.getData().getStats(currentResource);
ResourceFlowData.HistoryWindow window = ResourceFlowData.computeHistoryWindow(stats);
text.add("History: ");
text.add(window.format());
}
}
};
add(graphLabels.r(), 12, 345);
}
private void rebuildTable() {
tableSection.clear();
GTableBuilder builder = new GTableBuilder() {
@Override
public int nrOFEntries() {
return 10; // Rows: Prod, Cons, Maint, Net, Storage, Projection, Haulers, Trips, Distance, Session
}
};
// Column 1: Label
builder.column("Metric", 110, new GTableBuilder.GRowBuilder() {
@Override
public snake2d.util.gui.renderable.RENDEROBJ build(final GETTER<Integer> ier) {
GStat s = new GStat() {
@Override
public void update(GText text) {
text.clear();
switch (ier.get()) {
case 0:
text.add("Production");
break;
case 1:
text.add("Consumption");
break;
case 2:
text.add("Maintenance");
break;
case 3:
text.add("Net Flow");
break;
case 4:
text.add("Storage");
break;
case 5:
text.add("Status");
break;
case 6:
text.add("Haulers");
break;
case 7:
text.add("Trips");
break;
case 8:
text.add("Distance");
break;
case 9:
text.add("Session");
break;
}
}
};
return s.r();
}
});
// Column 2: Primary Value
builder.column("Value", 120, new GTableBuilder.GRowBuilder() {
@Override
public snake2d.util.gui.renderable.RENDEROBJ build(final GETTER<Integer> ier) {
GStat s = new GStat() {
@Override
public void update(GText text) {
text.clear();
if (currentResource == null) return;
ResourceFlowData.ResourceFlowStats stats = analyzer.getData().getStats(currentResource);
switch (ier.get()) {
case 0:
GFORMAT.i(text, stats.totalProduced);
break;
case 1:
GFORMAT.i(text, stats.totalConsumed);
break;
case 2:
// Maintenance is an estimate, not a lifetime counter.
text.add("-");
break;
case 3:
int net = stats.netFlowPerDay;
if (net > 0) text.add("+");
GFORMAT.i(text, net);
break;
case 4:
GFORMAT.i(text, stats.currentStored);
text.add("/");
GFORMAT.i(text, stats.storageCapacity);
break;
case 5:
// Only show projections if they're meaningful:
// 1. Net flow must be significant (> 20/day) to avoid noise
// 2. Projection must be reasonable (< 30 days) to be useful
break;
case 6:
GFORMAT.i(text, stats.activeHaulers);
break;
case 7:
GFORMAT.i(text, stats.totalHaulTrips);
break;
case 8:
GFORMAT.f1(text, stats.avgHaulDistance);
break;
case 9:
GFORMAT.i(text, stats.producedThisSession);
break;
}
}
};
return s.r();
}
});
// Column 3: Rate / Additional Info
builder.column("Rate/Detail", 110, new GTableBuilder.GRowBuilder() {
@Override
public snake2d.util.gui.renderable.RENDEROBJ build(final GETTER<Integer> ier) {
GStat s = new GStat() {
@Override
public void update(GText text) {
text.clear();
if (currentResource == null) return;
ResourceFlowData.ResourceFlowStats stats = analyzer.getData().getStats(currentResource);
// Default text color
text.color(GCOLOR.T().NORMAL);
switch (ier.get()) {
case 0:
text.color(COLOR_POSITIVE);
text.add("+");
GFORMAT.i(text, stats.productionRatePerDay);
text.add("/d");
break;
case 1:
text.color(COLOR_NEGATIVE);
text.add("-");
GFORMAT.i(text, stats.consumptionRatePerDay);
text.add("/d");
break;
case 2:
// Maintenance is always a drain, but not necessarily "bad".
text.color(COLOR_MAINT);
text.add("-");
GFORMAT.i(text, stats.maintenanceConsumptionPerDay);
text.add("/d");
break;
case 3:
if (Math.abs(stats.velocityTrend) > 1) {
text.add(stats.velocityTrend > 0 ? "▲ accel" : "▼ decel");
}
break;
case 4:
if (stats.storageCapacity > 0) {
GFORMAT.i(text, (int) ((stats.currentStored * 100.0) / stats.storageCapacity));
text.add("%");
}
break;
case 5:
// Status row: color should reflect the message.
double daysUntilChange;
if (stats.netFlowPerDay < -20) {
text.color(COLOR_NEGATIVE);
daysUntilChange = stats.getDaysUntilEmpty();
if (daysUntilChange < 30) {
text.add("Empty ");
GFORMAT.f1(text, daysUntilChange);
text.add("d");
} else {
text.add("Decreasing");
}
} else if (stats.netFlowPerDay > 20 && stats.storageCapacity > 0) {
text.color(COLOR_POSITIVE);
daysUntilChange = stats.getDaysUntilFull();
if (daysUntilChange < 30) {
text.add("Full ");
GFORMAT.f1(text, daysUntilChange);
text.add("d");
} else {
text.add("Increasing");
}
} else {
text.add("Stable");
}
break;
case 6:
text.add("active");
break;
case 7:
text.add("total");
break;
case 8:
text.add("avg");
break;
case 9:
text.add("-");
GFORMAT.i(text, stats.consumedThisSession);
break;
}
}
};
return s.r();
}
});
tableSection.add(builder.create(10, true));
}
/**
* Update displayed data for a resource
*/
public void updateResource(RESOURCE resource) {
this.currentResource = resource;
}
/**
* Clear all displayed data
*/
private void clearDisplay() {
this.currentResource = null;
}
/**
* Refresh the display (e.g., on each frame)
*/
public void refresh() {
// Nothing to do here, GStat handles updates
}
@Override
public void render(SPRITE_RENDERER r, float ds) {
if (!visableIs()) {
return;
}
// Render panel background
GCOLOR.UI().panBG.render(r, body());
GCOLOR.UI().borderH(r, body(), 0);
// Render Title
if (currentResource != null) {
UI.FONT().M.render(r, currentResource.name, body().x1() + 12, body().y1() + 10);
} else {
UI.FONT().M.render(r, "No Resource Selected", body().x1() + 12, body().y1() + 10);
}
super.render(r, ds);
}
/**
* Set which resource to display (called externally)
*/
public void setResource(RESOURCE resource) {
updateResource(resource);
}
/**
* Get currently displayed resource
*/
public RESOURCE getCurrentResource() {
return currentResource;
}
}