Skip to content

Commit

Permalink
Merge branch 'graphcap' into 'master'
Browse files Browse the repository at this point in the history
Graph Cap

See merge request roan/KeysPerSecond!43
GitHub #90
  • Loading branch information
RoanH committed Jan 16, 2024
2 parents c50ea20 + 7cfd1df commit 4f6784f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 5 deletions.
9 changes: 8 additions & 1 deletion KeysPerSecond/src/dev/roanh/kps/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public class Main{
/**
* Graph panel.
*/
protected static final List<GraphPanel> graphs = new ArrayList<GraphPanel>();
private static final List<GraphPanel> graphs = new ArrayList<GraphPanel>();
/**
* Linked list containing all the past key counts per time frame
*/
Expand Down Expand Up @@ -488,6 +488,13 @@ public static final void resetPanels(){
}
}
}

/**
* Clears the data for all active graphs.
*/
public static final void resetGraphs(){
graphs.forEach(GraphPanel::reset);
}

/**
* Reconfigures the layout of the program
Expand Down
3 changes: 1 addition & 2 deletions KeysPerSecond/src/dev/roanh/kps/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -214,7 +213,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());
Expand Down
23 changes: 22 additions & 1 deletion KeysPerSecond/src/dev/roanh/kps/config/group/GraphSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ 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);

/**
* Creates new graph settings.
Expand All @@ -54,6 +58,22 @@ 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();
}

/**
* Check if the average should be drawn in the graph.
* @return True if the average KPS should be drawn in the graph.
Expand Down Expand Up @@ -104,14 +124,15 @@ public void showEditor(boolean live){

@Override
public boolean parse(Map<String, String> data){
return super.parse(data) | findAndParse(data, showAvg, backlog);
return super.parse(data) | findAndParse(data, showAvg, backlog, max);
}

@Override
public void writeItems(IndentWriter out){
super.writeItems(out);
showAvg.write(out);
backlog.write(out);
max.write(out);
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion KeysPerSecond/src/dev/roanh/kps/panels/GraphPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public GraphPanel(GraphSettings config){
public final void reset(){
values.clear();
maxval = 1;
repaint();
}

@Override
Expand Down Expand Up @@ -124,7 +125,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);
Expand Down Expand Up @@ -154,6 +155,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;
}
Expand Down
8 changes: 8 additions & 0 deletions KeysPerSecond/src/dev/roanh/kps/ui/editor/GraphEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public GraphEditor(GraphSettings config, boolean live){
backlog.addChangeListener(e->config.setBacklog((int)backlog.getValue()));
fields.add(backlog);

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();
});

labels.add(new JLabel("Show average: "));
JCheckBox avg = new JCheckBox("", config.isAverageVisible());
fields.add(avg);
Expand Down
1 change: 1 addition & 0 deletions KeysPerSecond/test/config88.kps
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ graphs:
height: 8
showAvg: false
backlog: 45
max: 2147483647

panels:
- type: max
Expand Down
1 change: 1 addition & 0 deletions KeysPerSecond/test/config88nodefault.kps
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ graphs:
height: 8
showAvg: false
backlog: 45
max: 20

panels:
- type: max
Expand Down
3 changes: 3 additions & 0 deletions KeysPerSecond/test/dev/roanh/kps/config/ConfigParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SpecialPanelSettings> panels = config.getPanels().iterator();
Expand Down Expand Up @@ -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<SpecialPanelSettings> panels = config.getPanels().iterator();
Expand Down Expand Up @@ -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<SpecialPanelSettings> panels = config.getPanels().iterator();
Expand Down

0 comments on commit 4f6784f

Please sign in to comment.