From cf5d172922018190e2bc5afd88fa23a427e9bfb6 Mon Sep 17 00:00:00 2001 From: TranquillyUnpleasant <62061444+TranquillyUnpleasant@users.noreply.github.com> Date: Sun, 5 Sep 2021 20:21:49 +0500 Subject: [PATCH] Wave graph y axis refactor (#5926) * Y axis refactor * Fix formatting inconsistencies * Make style match xml --- core/src/mindustry/editor/WaveGraph.java | 51 ++++++++++++++++++------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/core/src/mindustry/editor/WaveGraph.java b/core/src/mindustry/editor/WaveGraph.java index 13c6021d93..76bbc82d2b 100644 --- a/core/src/mindustry/editor/WaveGraph.java +++ b/core/src/mindustry/editor/WaveGraph.java @@ -38,8 +38,14 @@ public class WaveGraph extends Table{ lay.setText(font, "1"); + int maxY = switch(mode){ + case counts -> nextStep(max); + case health -> nextStep((int)maxHealth); + case totals -> nextStep(maxTotal); + }; + float fh = lay.height; - float offsetX = Scl.scl(30f), offsetY = Scl.scl(22f) + fh + Scl.scl(5f); + float offsetX = Scl.scl(lay.width * (maxY + "").length() * 2), offsetY = Scl.scl(22f) + fh + Scl.scl(5f); float graphX = x + offsetX, graphY = y + offsetY, graphW = width - offsetX, graphH = height - offsetY; float spacing = graphW / (values.length - 1); @@ -53,7 +59,7 @@ public class WaveGraph extends Table{ for(int i = 0; i < values.length; i++){ int val = values[i][type.id]; - float cx = graphX + i*spacing, cy = graphY + val * graphH / max; + float cx = graphX + i * spacing, cy = graphY + val * graphH / maxY; Lines.linePoint(cx, cy); } @@ -69,7 +75,7 @@ public class WaveGraph extends Table{ sum += values[i][type.id]; } - float cx = graphX + i*spacing, cy = graphY + sum * graphH / maxTotal; + float cx = graphX + i * spacing, cy = graphY + sum * graphH / maxY; Lines.linePoint(cx, cy); } @@ -84,7 +90,7 @@ public class WaveGraph extends Table{ sum += (type.health) * values[i][type.id]; } - float cx = graphX + i*spacing, cy = graphY + sum * graphH / maxHealth; + float cx = graphX + i * spacing, cy = graphY + sum * graphH / maxY; Lines.linePoint(cx, cy); } @@ -92,19 +98,23 @@ public class WaveGraph extends Table{ } //how many numbers can fit here - float totalMarks = (graphH - getMarginBottom() *2f) / (lay.height * 2); + float totalMarks = Mathf.clamp(maxY, 1, 10); - int markSpace = Math.max(1, Mathf.ceil(max / totalMarks)); + int markSpace = Math.max(1, Mathf.ceil(maxY / totalMarks)); Draw.color(Color.lightGray); - for(int i = 0; i < max; i += markSpace){ - float cy = graphY + i * graphH / max, cx = graphX; - //Lines.line(cx, cy, cx + len, cy); + Draw.alpha(0.1f); + + for(int i = 0; i < maxY; i += markSpace){ + float cy = graphY + i * graphH / maxY, cx = graphX; + + Lines.line(cx, cy, cx + graphW, cy); lay.setText(font, "" + i); - font.draw("" + i, cx, cy + lay.height/2f, Align.right); + font.draw("" + i, cx, cy + lay.height / 2f, Align.right); } + Draw.alpha(1f); float len = Scl.scl(4f); font.setColor(Color.lightGray); @@ -113,7 +123,7 @@ public class WaveGraph extends Table{ float cy = y + fh, cx = graphX + graphW / (values.length - 1) * i; Lines.line(cx, cy, cx, cy + len); - if(i == values.length/2){ + if(i == values.length / 2){ font.draw("" + (i + from + 1), cx, cy - Scl.scl(2f), Align.center); } } @@ -164,7 +174,7 @@ public class WaveGraph extends Table{ sum += spawned; } maxTotal = Math.max(maxTotal, sum); - maxHealth = Math.max(maxHealth,healthsum); + maxHealth = Math.max(maxHealth, healthsum); } ObjectSet usedCopy = new ObjectSet<>(used); @@ -200,6 +210,23 @@ public class WaveGraph extends Table{ return Tmp.c1.fromHsv(type.id / (float)Vars.content.units().size * 360f, 0.7f, 1f); } + int nextStep(float value){ + int order = 1; + while(order < value){ + if(order * 2 > value){ + return order * 2; + } + if(order * 5 > value){ + return order * 5; + } + if(order * 10 > value){ + return order * 10; + } + order *= 10; + } + return order; + } + enum Mode{ counts, totals, health;