fix bitwise switch when not all checkboxes are on screen

This commit is contained in:
stirkac 2019-03-05 14:57:19 +01:00 committed by Dale Schultz
parent fcbfc85327
commit 2b0ea46757
2 changed files with 34 additions and 10 deletions

View File

@ -33,6 +33,8 @@ import javax.swing.JPanel;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import com.romraider.Settings; import com.romraider.Settings;
import com.romraider.util.ByteUtil;
import static com.romraider.util.ByteUtil.isBitSet; import static com.romraider.util.ByteUtil.isBitSet;
public class TableBitwiseSwitch extends Table { public class TableBitwiseSwitch extends Table {
@ -89,23 +91,30 @@ public class TableBitwiseSwitch extends Table {
@Override @Override
public byte[] saveFile(byte[] input) { public byte[] saveFile(byte[] input) {
// get original values
boolean[] bools = new boolean[8]; boolean[] bools = ByteUtil.byteToBoolArr(input[storageAddress]);
// invert original values because input checboxes are in reverse order
for(int i=0; i<bools.length/2; i++){
boolean temp = bools[i];
bools[i] = bools[bools.length -i -1];
bools[bools.length -i -1] = temp;
}
// set to new state from checkboxes, in reverse order
for (Entry<String, Integer> entry: controlBits.entrySet()) { for (Entry<String, Integer> entry: controlBits.entrySet()) {
JCheckBox cb = getButtonByText(entry.getKey()); JCheckBox cb = getButtonByText(entry.getKey());
bools[entry.getValue()] = cb.isSelected(); bools[entry.getValue()] = cb.isSelected();
} }
// reverse back the array so it is correct representation of byte
for(int i=0; i<bools.length/2; i++){
boolean temp = bools[i];
bools[i] = bools[bools.length -i -1];
bools[bools.length -i -1] = temp;
}
int bits = 0; input[storageAddress] = ByteUtil.booleanArrayToBit(bools);
for (int i = 0; i < bools.length; i++) {
if (bools[i])
bits |= 1 << i;
}
input[storageAddress] = (byte) bits;
return input; return input;
} }
public void setValues(String name, String input) { public void setValues(String name, String input) {
controlBits.put(name, Integer.parseInt(input)); controlBits.put(name, Integer.parseInt(input));
} }

View File

@ -104,6 +104,21 @@ public final class ByteUtil {
return (b & 1 << position) != 0; return (b & 1 << position) != 0;
} }
public static boolean[] byteToBoolArr(byte b) {
boolean boolArr[] = new boolean[8];
for(int i=0;i<8;i++) boolArr[i] = (b & (byte)(128 / Math.pow(2, i))) != 0;
return boolArr;
}
public static byte booleanArrayToBit(boolean[] arr){
byte val = 0;
for (boolean b: arr) {
val <<= 1;
if (b) val |= 1;
}
return val;
}
private static int[] computeFailure(byte[] pattern) { private static int[] computeFailure(byte[] pattern) {
int[] failure = new int[pattern.length]; int[] failure = new int[pattern.length];
int j = 0; int j = 0;