Skip to content

Commit

Permalink
Merge branch 'customkeyname-dev' into 'master'
Browse files Browse the repository at this point in the history
Custom key names

See merge request roan/KeyPerSecond!12
  • Loading branch information
RoanH committed Feb 5, 2019
2 parents 5b18eaf + 09f31ab commit 732a4cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
7 changes: 4 additions & 3 deletions KeysPerSecond/src/me/roan/kps/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ private final boolean loadNewFormat(File saveloc, boolean v2){
}
String[] args = line.split(":", 2);
if(args[0].startsWith("keys")){
while((line = in.readLine()) != null && (line = line.replace(" ", "")).startsWith("-")){
while((line = in.readLine()) != null && (line = line.trim()).startsWith("-")){
try{
keyinfo.add(parseKey(line.substring(1), defaultMode, v2));
keyinfo.add(parseKey(line.substring(1).trim(), defaultMode, v2));
}catch(Exception e){
modified = true;
}
Expand Down Expand Up @@ -917,7 +917,8 @@ private final KeyInformation parseKey(String arg, RenderingMode mode, boolean v2
boolean shift = false;
for(String str : args){
String[] comp = str.split("=", 2);
switch(comp[0]){
comp[1] = comp[1].trim();
switch(comp[0].trim()){
case "keycode":
code = Integer.parseInt(comp[1]);
break;
Expand Down
39 changes: 28 additions & 11 deletions KeysPerSecond/src/me/roan/kps/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -1472,12 +1472,14 @@ private static final void createListItem(Positionable info, JPanel fields, JPane
protected static final void configureKeys(){
List<KeyInformation> copy = new ArrayList<KeyInformation>(config.keyinfo);
boolean[] visibleState = new boolean[copy.size()];
String[] nameState = new String[copy.size()];
for(int i = 0; i < copy.size(); i++){
visibleState[i] = copy.get(i).visible;
nameState[i] = copy.get(i).name;
}

JPanel keyform = new JPanel(new BorderLayout());
keyform.add(new JLabel("Currently added keys (you can hide or remove them):"), BorderLayout.PAGE_START);
keyform.add(new JLabel("Currently added keys (you can edit these fields):"), BorderLayout.PAGE_START);
JTable keys = new JTable();
DefaultTableModel model = new DefaultTableModel(){
/**
Expand All @@ -1499,12 +1501,7 @@ public int getColumnCount(){
public Object getValueAt(int rowIndex, int columnIndex){
switch(columnIndex){
case 0:
if(CommandKeys.isMouseButton(config.keyinfo.get(rowIndex).keycode)){
return config.keyinfo.get(rowIndex).name;
}else{
int n = (CommandKeys.hasAlt(config.keyinfo.get(rowIndex).keycode) ? 1 : 0) + (CommandKeys.hasCtrl(config.keyinfo.get(rowIndex).keycode) ? 1 : 0) + (CommandKeys.hasShift(config.keyinfo.get(rowIndex).keycode) ? 1 : 0);
return config.keyinfo.get(rowIndex).getModifierString() + config.keyinfo.get(rowIndex).name.substring(n);
}
return config.keyinfo.get(rowIndex).name;
case 1:
return config.keyinfo.get(rowIndex).visible;
case 2:
Expand Down Expand Up @@ -1538,19 +1535,25 @@ public Class<?> getColumnClass(int columnIndex){

@Override
public boolean isCellEditable(int row, int col){
return col != 0;
return true;
}

@Override
public void setValueAt(Object value, int row, int col){
if(col == 1){
switch(col){
case 0:
config.keyinfo.get(row).setName((String)value);
break;
case 1:
config.keyinfo.get(row).visible = (boolean)value;
}else{
break;
case 2:
if((boolean)value == true){
Main.keys.remove(config.keyinfo.get(row).keycode);
config.keyinfo.remove(row);
keys.repaint();
}
break;
}
}
};
Expand Down Expand Up @@ -1643,6 +1646,7 @@ public void setValueAt(Object value, int row, int col){
if(!showOptionDialog(keyform, true)){
for(int i = 0; i < copy.size(); i++){
copy.get(i).visible = visibleState[i];
copy.get(i).setName(nameState[i]);
}
config.keyinfo = copy;
}
Expand Down Expand Up @@ -2024,7 +2028,7 @@ public static class Key implements Serializable{
* The key in string form<br>
* For example: X
*/
public final String name;
public String name;
/**
* The graphical display for this key
*/
Expand Down Expand Up @@ -2185,6 +2189,19 @@ protected KeyInformation(String name, int code, boolean visible){
this.keycode = code;
this.visible = visible;
}

/**
* Changes the display name of this
* key to the given string. If {@link Key}
* panels are active with the same key code
* as this {@link KeyInformation} object
* then their display name is also updated.
* @param name The new display name
*/
public void setName(String name){
this.name = name;
keys.getOrDefault(keycode, DUMMY_KEY).name = name;
}

/**
* Gets a string containing all
Expand Down

0 comments on commit 732a4cd

Please sign in to comment.