From 4c62a00bc6c02b0fbc677ea672ebb787bf8f3420 Mon Sep 17 00:00:00 2001 From: Roan Hofland Date: Tue, 16 Jan 2024 19:04:25 +0100 Subject: [PATCH 1/7] Implement a maximum for graphs --- KeysPerSecond/src/dev/roanh/kps/Main.java | 6 +++++- KeysPerSecond/src/dev/roanh/kps/Menu.java | 2 +- .../src/dev/roanh/kps/config/group/GraphSettings.java | 9 +++++++++ KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java | 4 +++- .../src/dev/roanh/kps/ui/editor/GraphEditor.java | 8 ++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/KeysPerSecond/src/dev/roanh/kps/Main.java b/KeysPerSecond/src/dev/roanh/kps/Main.java index 461fd5a..840e737 100644 --- a/KeysPerSecond/src/dev/roanh/kps/Main.java +++ b/KeysPerSecond/src/dev/roanh/kps/Main.java @@ -140,7 +140,7 @@ public class Main{ /** * Graph panel. */ - protected static final List graphs = new ArrayList(); + private static final List graphs = new ArrayList(); /** * Linked list containing all the past key counts per time frame */ @@ -488,6 +488,10 @@ public static final void resetPanels(){ } } } + + public static final void resetGraphs(){ + Main.graphs.forEach(GraphPanel::reset); + } /** * Reconfigures the layout of the program diff --git a/KeysPerSecond/src/dev/roanh/kps/Menu.java b/KeysPerSecond/src/dev/roanh/kps/Menu.java index fe1b8af..c07bf2d 100644 --- a/KeysPerSecond/src/dev/roanh/kps/Menu.java +++ b/KeysPerSecond/src/dev/roanh/kps/Menu.java @@ -214,7 +214,7 @@ protected static final void createMenu(){ Main.hits = 0; }); sresetgraph.addActionListener((e)->{ - Main.graphs.forEach(GraphPanel::reset); + Main.resetGraphs(); }); commandkeys.addActionListener((e)->{ CommandKeysDialog.configureCommandKeys(Main.config.getCommands()); diff --git a/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java b/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java index 3b985c2..0e58dea 100644 --- a/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java +++ b/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java @@ -46,6 +46,7 @@ public class GraphSettings extends PanelSettings implements LegacyProxyStore{ * Number of points the graph consists of. */ private final IntSetting backlog = new IntSetting("backlog", 2, Short.MAX_VALUE, 30); + private final IntSetting max = new IntSetting("max", 1, Integer.MAX_VALUE, Integer.MAX_VALUE); /** * Creates new graph settings. @@ -54,6 +55,14 @@ public GraphSettings(){ super("graphs", 0, -1, -1, 3, "KPS"); } + public void setMaxValue(int max){ + this.max.update(max); + } + + public int getMaxValue(){ + return max.getValue(); + } + /** * Check if the average should be drawn in the graph. * @return True if the average KPS should be drawn in the graph. diff --git a/KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java b/KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java index a87fe18..862a303 100644 --- a/KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java +++ b/KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java @@ -124,7 +124,7 @@ public void paintComponent(Graphics g1){ //average line ThemeColor foreground = Main.config.getTheme().getForeground(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, foreground.getAlpha())); - if(config.isAverageVisible()){ + if(config.isAverageVisible() && Main.avg <= config.getMaxValue()){ int y = (int)(oy + 1 - ((insideHeight * Main.avg) / maxval)); g.setColor(foreground.getColor().darker()); g.setStroke(avgstroke); @@ -154,6 +154,8 @@ public void paintComponent(Graphics g1){ * @param value The new point to add. */ public void addPoint(int value){ + value = Math.min(value, config.getMaxValue()); + if(value > maxval){ maxval = value; } diff --git a/KeysPerSecond/src/dev/roanh/kps/ui/editor/GraphEditor.java b/KeysPerSecond/src/dev/roanh/kps/ui/editor/GraphEditor.java index 634af7c..cdac485 100644 --- a/KeysPerSecond/src/dev/roanh/kps/ui/editor/GraphEditor.java +++ b/KeysPerSecond/src/dev/roanh/kps/ui/editor/GraphEditor.java @@ -57,5 +57,13 @@ public GraphEditor(GraphSettings config, boolean live){ config.setAverageVisible(avg.isSelected()); Main.frame.repaint(); }); + + labels.add(new JLabel("Maximum (Y cap): ")); + JSpinner max = new JSpinner(new SpinnerNumberModel(config.getMaxValue(), 1, Integer.MAX_VALUE, 1)); + fields.add(max); + max.addChangeListener(e->{ + config.setMaxValue((int)max.getValue()); + Main.resetGraphs(); + }); } } From 21963ccea175e07204c19360a7c06d428f8fa8fd Mon Sep 17 00:00:00 2001 From: Roan Hofland Date: Tue, 16 Jan 2024 19:06:22 +0100 Subject: [PATCH 2/7] Extend tests --- KeysPerSecond/test/config88.kps | 1 + KeysPerSecond/test/config88nodefault.kps | 1 + KeysPerSecond/test/dev/roanh/kps/config/ConfigParserTest.java | 3 +++ 3 files changed, 5 insertions(+) diff --git a/KeysPerSecond/test/config88.kps b/KeysPerSecond/test/config88.kps index 5649d73..71d420b 100644 --- a/KeysPerSecond/test/config88.kps +++ b/KeysPerSecond/test/config88.kps @@ -46,6 +46,7 @@ graphs: height: 8 showAvg: false backlog: 45 + max: 2147483647 panels: - type: max diff --git a/KeysPerSecond/test/config88nodefault.kps b/KeysPerSecond/test/config88nodefault.kps index e0166e1..3a2bb14 100644 --- a/KeysPerSecond/test/config88nodefault.kps +++ b/KeysPerSecond/test/config88nodefault.kps @@ -42,6 +42,7 @@ graphs: height: 8 showAvg: false backlog: 45 + max: 20 panels: - type: max diff --git a/KeysPerSecond/test/dev/roanh/kps/config/ConfigParserTest.java b/KeysPerSecond/test/dev/roanh/kps/config/ConfigParserTest.java index 18d16dc..29869bc 100644 --- a/KeysPerSecond/test/dev/roanh/kps/config/ConfigParserTest.java +++ b/KeysPerSecond/test/dev/roanh/kps/config/ConfigParserTest.java @@ -140,6 +140,7 @@ public void fullTest1() throws IOException{ assertEquals(7, graph.getLayoutHeight()); assertTrue(graph.isAverageVisible()); assertEquals(1800, graph.getBacklog()); + assertEquals(Integer.MAX_VALUE, graph.getMaxValue()); //special panels Iterator panels = config.getPanels().iterator(); @@ -330,6 +331,7 @@ public void fullTest2() throws IOException{ assertEquals(8, graph.getLayoutHeight()); assertFalse(graph.isAverageVisible()); assertEquals(45, graph.getBacklog()); + assertEquals(Integer.MAX_VALUE, graph.getMaxValue()); //special panels Iterator panels = config.getPanels().iterator(); @@ -582,6 +584,7 @@ public void fullTest4() throws IOException{ assertEquals(8, graph.getLayoutHeight()); assertFalse(graph.isAverageVisible()); assertEquals(45, graph.getBacklog()); + assertEquals(20, graph.getMaxValue()); //special panels Iterator panels = config.getPanels().iterator(); From 8ed5341b47ec71779c4c0a30831d353fafff1c34 Mon Sep 17 00:00:00 2001 From: Roan Hofland Date: Tue, 16 Jan 2024 19:06:47 +0100 Subject: [PATCH 3/7] Actually read and write the new setting --- .../src/dev/roanh/kps/config/group/GraphSettings.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java b/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java index 0e58dea..48bf5fe 100644 --- a/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java +++ b/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java @@ -113,7 +113,7 @@ public void showEditor(boolean live){ @Override public boolean parse(Map data){ - return super.parse(data) | findAndParse(data, showAvg, backlog); + return super.parse(data) | findAndParse(data, showAvg, backlog, max); } @Override @@ -121,6 +121,7 @@ public void writeItems(IndentWriter out){ super.writeItems(out); showAvg.write(out); backlog.write(out); + max.write(out); } @Override From c0ca991e09873d4008a091ed162796df36cf1a73 Mon Sep 17 00:00:00 2001 From: Roan Hofland Date: Tue, 16 Jan 2024 19:08:58 +0100 Subject: [PATCH 4/7] Complete the javadoc --- KeysPerSecond/src/dev/roanh/kps/Main.java | 3 +++ KeysPerSecond/src/dev/roanh/kps/Menu.java | 1 - .../src/dev/roanh/kps/config/group/GraphSettings.java | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/KeysPerSecond/src/dev/roanh/kps/Main.java b/KeysPerSecond/src/dev/roanh/kps/Main.java index 840e737..cd3fd1f 100644 --- a/KeysPerSecond/src/dev/roanh/kps/Main.java +++ b/KeysPerSecond/src/dev/roanh/kps/Main.java @@ -489,6 +489,9 @@ public static final void resetPanels(){ } } + /** + * Clears the data for all active graphs. + */ public static final void resetGraphs(){ Main.graphs.forEach(GraphPanel::reset); } diff --git a/KeysPerSecond/src/dev/roanh/kps/Menu.java b/KeysPerSecond/src/dev/roanh/kps/Menu.java index c07bf2d..5d310c3 100644 --- a/KeysPerSecond/src/dev/roanh/kps/Menu.java +++ b/KeysPerSecond/src/dev/roanh/kps/Menu.java @@ -55,7 +55,6 @@ import dev.roanh.kps.config.ConfigLoader; import dev.roanh.kps.config.UpdateRate; import dev.roanh.kps.config.group.KeyPanelSettings; -import dev.roanh.kps.panels.GraphPanel; import dev.roanh.kps.ui.dialog.AboutDialog; import dev.roanh.kps.ui.dialog.ColorDialog; import dev.roanh.kps.ui.dialog.CommandKeysDialog; diff --git a/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java b/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java index 48bf5fe..baef27a 100644 --- a/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java +++ b/KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java @@ -46,6 +46,9 @@ public class GraphSettings extends PanelSettings implements LegacyProxyStore{ * Number of points the graph consists of. */ private final IntSetting backlog = new IntSetting("backlog", 2, Short.MAX_VALUE, 30); + /** + * Maximum graph y value that will be displayed. Any higher values are capped at this value. + */ private final IntSetting max = new IntSetting("max", 1, Integer.MAX_VALUE, Integer.MAX_VALUE); /** @@ -55,10 +58,18 @@ public GraphSettings(){ super("graphs", 0, -1, -1, 3, "KPS"); } + /** + * Sets the new maximum value to be displayed by this graph. + * @param max The new graph maximum. + */ public void setMaxValue(int max){ this.max.update(max); } + /** + * Gets the maximum y value for this graph. + * @return The maximum y value. + */ public int getMaxValue(){ return max.getValue(); } From 3671f54e9538d56a8174acec361b08ddc87c50ac Mon Sep 17 00:00:00 2001 From: Roan Hofland Date: Tue, 16 Jan 2024 19:10:53 +0100 Subject: [PATCH 5/7] Reorder editor fields --- .../src/dev/roanh/kps/ui/editor/GraphEditor.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/KeysPerSecond/src/dev/roanh/kps/ui/editor/GraphEditor.java b/KeysPerSecond/src/dev/roanh/kps/ui/editor/GraphEditor.java index cdac485..bd13d31 100644 --- a/KeysPerSecond/src/dev/roanh/kps/ui/editor/GraphEditor.java +++ b/KeysPerSecond/src/dev/roanh/kps/ui/editor/GraphEditor.java @@ -50,14 +50,6 @@ public GraphEditor(GraphSettings config, boolean live){ backlog.addChangeListener(e->config.setBacklog((int)backlog.getValue())); fields.add(backlog); - labels.add(new JLabel("Show average: ")); - JCheckBox avg = new JCheckBox("", config.isAverageVisible()); - fields.add(avg); - avg.addActionListener(e->{ - config.setAverageVisible(avg.isSelected()); - Main.frame.repaint(); - }); - labels.add(new JLabel("Maximum (Y cap): ")); JSpinner max = new JSpinner(new SpinnerNumberModel(config.getMaxValue(), 1, Integer.MAX_VALUE, 1)); fields.add(max); @@ -65,5 +57,13 @@ public GraphEditor(GraphSettings config, boolean live){ config.setMaxValue((int)max.getValue()); Main.resetGraphs(); }); + + labels.add(new JLabel("Show average: ")); + JCheckBox avg = new JCheckBox("", config.isAverageVisible()); + fields.add(avg); + avg.addActionListener(e->{ + config.setAverageVisible(avg.isSelected()); + Main.frame.repaint(); + }); } } From df622539400788960b03b59e055b401854756bfe Mon Sep 17 00:00:00 2001 From: Roan Hofland Date: Tue, 16 Jan 2024 19:11:10 +0100 Subject: [PATCH 6/7] Remove prefix --- KeysPerSecond/src/dev/roanh/kps/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KeysPerSecond/src/dev/roanh/kps/Main.java b/KeysPerSecond/src/dev/roanh/kps/Main.java index cd3fd1f..eb9fc39 100644 --- a/KeysPerSecond/src/dev/roanh/kps/Main.java +++ b/KeysPerSecond/src/dev/roanh/kps/Main.java @@ -493,7 +493,7 @@ public static final void resetPanels(){ * Clears the data for all active graphs. */ public static final void resetGraphs(){ - Main.graphs.forEach(GraphPanel::reset); + graphs.forEach(GraphPanel::reset); } /** From 7cfd1dfa623be3526dc26358f542ff60e03245d3 Mon Sep 17 00:00:00 2001 From: Roan Hofland Date: Tue, 16 Jan 2024 19:12:43 +0100 Subject: [PATCH 7/7] Repaint on reset --- KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java | 1 + 1 file changed, 1 insertion(+) diff --git a/KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java b/KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java index 862a303..6780a5d 100644 --- a/KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java +++ b/KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java @@ -81,6 +81,7 @@ public GraphPanel(GraphSettings config){ public final void reset(){ values.clear(); maxval = 1; + repaint(); } @Override