Skip to content

Commit

Permalink
Add support for saving the on screen position of the program
Browse files Browse the repository at this point in the history
fix issue #37
  • Loading branch information
RoanH committed Apr 16, 2017
1 parent 70934da commit d195242
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
38 changes: 37 additions & 1 deletion KeysPerSecond/src/me/roan/kps/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.roan.kps;

import java.awt.Color;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -357,6 +358,9 @@ private final boolean loadNewFormat(File saveloc){
modified = true;
}
break;
case "position":
Main.frame.setLocation(parsePosition(args[1]));
break;
}
}
in.close();
Expand Down Expand Up @@ -425,6 +429,29 @@ private final Color parseColor(String arg){
}
return new Color(r, g, b);
}

/**
* Parses the text representation of the position
* to it's actual data
* @param arg The text data
* @return The position data
*/
private final Point parsePosition(String data){
data = data.replace(" ", "").substring(1, data.length() - 1);
String[] args = data.split(",");
Point loc = new Point();
try{
for(String arg : args){
if(arg.startsWith("x=")){
loc.x = Integer.parseInt(arg.replace("x=", ""));
}else if(arg.startsWith("y=")){
loc.y = Integer.parseInt(arg.replace("y=", ""));
}
}
}catch(Exception e){
}
return loc;
}

/**
* Loads a legacy configuration file
Expand Down Expand Up @@ -487,8 +514,12 @@ private final boolean loadLegacyFormat(File saveloc){

/**
* Saves this configuration file
* @param pos Whether or not the ask
* to save the on screen position
* of the program
*/
protected final void saveConfig(){
protected final void saveConfig(boolean pos){
boolean savepos = (!pos) ? false : (JOptionPane.showConfirmDialog(null, "Do you want to save the onscreen position of the program?", "Keys per Second", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("Keys per second configuration file", "kpsconf", "kpsconf2"));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
Expand Down Expand Up @@ -524,6 +555,11 @@ protected final void saveConfig(){
out.println("foregroundOpacity: " + opacityfg);
out.println("backgroundOpacity: " + opacitybg);
out.println();
if(savepos){
out.println("# Position");
out.println("position: [x=" + Main.frame.getLocationOnScreen().x + ",y=" + Main.frame.getLocationOnScreen().y + "]");
out.println();
}
out.println("# Keys");
out.println("keys: ");
for(KeyInformation i : keyinfo){
Expand Down
10 changes: 5 additions & 5 deletions KeysPerSecond/src/me/roan/kps/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ private static final void configure(){
save.setEnabled(true);
});
save.addActionListener((e)->{
config.saveConfig();
config.saveConfig(false);
});
load.addActionListener((e)->{
if(!Configuration.loadConfiguration()){
Expand Down Expand Up @@ -847,13 +847,13 @@ public void setValueAt(Object value, int row, int col){
keyform.add(pane, BorderLayout.CENTER);
JButton newkey = new JButton("Add Key");
newkey.addActionListener((evt)->{
if(JOptionPane.showOptionDialog(null, "Press a key and press 'OK' to add it.", "Keys per second", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"OK", "Cancel"}, 0) == 0){
if(JOptionPane.showOptionDialog(frame.isVisible() ? frame : null, "Press a key and press 'OK' to add it.", "Keys per second", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"OK", "Cancel"}, 0) == 0){
if(lastevent == null){
JOptionPane.showMessageDialog(null, "No key pressed!", "Keys per second", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(frame.isVisible() ? frame : null, "No key pressed!", "Keys per second", JOptionPane.ERROR_MESSAGE);
return;
}
KeyInformation info = new KeyInformation(NativeKeyEvent.getKeyText(lastevent.getKeyCode()), lastevent.getKeyCode());
if(JOptionPane.showConfirmDialog(null, "Add the " + info.name + " key?", "Keys per second", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
if(JOptionPane.showConfirmDialog(frame.isVisible() ? frame : null, "Add the " + info.name + " key?", "Keys per second", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
config.keyinfo.add(info);
}
model.fireTableDataChanged();
Expand Down Expand Up @@ -899,7 +899,7 @@ public void setValueAt(Object value, int row, int col){

addform.add(buttons, BorderLayout.CENTER);

if(JOptionPane.showOptionDialog(null, addform, "Keys per second", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"OK", "Cancel"}, 0) == 0){
if(JOptionPane.showOptionDialog(frame.isVisible() ? frame : null, addform, "Keys per second", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[]{"OK", "Cancel"}, 0) == 0){
if(cm1.isSelected()){
config.keyinfo.add(new KeyInformation("M1", -NativeMouseEvent.BUTTON1));
}
Expand Down
2 changes: 1 addition & 1 deletion KeysPerSecond/src/me/roan/kps/Menu.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ protected static final void createMenu(){
e.setOpaque(true);
}
save.addActionListener((e)->{
Main.config.saveConfig();
Main.config.saveConfig(true);
});
load.addActionListener((e)->{
double oldScale = Main.config.size;
Expand Down

0 comments on commit d195242

Please sign in to comment.