Skip to content

Commit 143b3c4

Browse files
committed
Cleanup
1 parent ac1d477 commit 143b3c4

File tree

4 files changed

+149
-119
lines changed

4 files changed

+149
-119
lines changed

src/main/Settings.java

Lines changed: 89 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,85 +9,104 @@
99
import java.io.IOException;
1010
import java.io.PrintWriter;
1111
import java.util.ArrayList;
12+
import java.util.HashMap;
1213

1314
public class Settings {
1415

15-
public static Color bg;
16-
public static Color rim;
17-
public static Color detail;
16+
public static Color bg = new Color(238,238,238);
17+
public static Color rim = new Color(0,0,0);
18+
public static Color detail = new Color(0,0,0);;
1819

19-
public static Point pos;
20-
public static int size;
20+
public static Point pos = new Point(100,100);
21+
public static int size = 200;
2122

2223
public static void init() {
24+
ArrayList<String> lines = new ArrayList<>();
25+
2326
try (BufferedReader br = new BufferedReader(new FileReader("./settings.ini"))) {
24-
ArrayList<String> lines = new ArrayList<>();
2527

2628
String line;
2729
while ((line = br.readLine()) != null) {
28-
29-
lines.add(line);
30-
}
3130

32-
lines.removeIf(s -> s.length() <= 1);
33-
lines.removeIf(s -> s.startsWith("//"));
34-
35-
for (String s : lines) {
36-
if (s.contains("POS:")) {
37-
int[] nums = parseNums(s, 2);
38-
if (nums == null)
39-
continue;
40-
pos = new Point(nums[0], nums[1]);
41-
continue;
42-
}
43-
if (s.contains("SIZE: ")) {
44-
int[] nums = parseNums(s, 1);
45-
if (nums == null)
46-
continue;
47-
size = nums[0];
48-
continue;
49-
}
50-
if (s.contains("BG:")) {
51-
int[] nums = parseNums(s, 3);
52-
if (nums == null)
53-
continue;
54-
bg = new Color(nums[0], nums[1], nums[2]);
55-
continue;
56-
}
57-
if (s.contains("RIM:")) {
58-
int[] nums = parseNums(s, 3);
59-
if (nums == null)
60-
continue;
61-
rim = new Color(nums[0], nums[1], nums[2]);
62-
continue;
63-
}
64-
if (s.contains("DETAILS:")) {
65-
int[] nums = parseNums(s, 3);
66-
if (nums == null)
67-
continue;
68-
detail = new Color(nums[0], nums[1], nums[2]);
69-
continue;
70-
}
71-
72-
if (s.contains("->")) {
73-
String[] parts = s.split(":");
74-
Shortcuts.setSC(parts[1], parts[0].split("->"));
75-
continue;
76-
}
77-
78-
if (s.contains("None")) {
79-
s+=" a "; // ugly hack
80-
}
81-
82-
Shortcuts.setTSC(s.split(": ")[0], s.split(": ")[1]);
31+
lines.add(line);
8332
}
8433

8534
} catch (FileNotFoundException fnfe) {
8635
createNewInitFile();
8736
} catch (IOException e) {
8837
e.printStackTrace();
89-
} catch (Exception e) {
90-
e.printStackTrace();
38+
}
39+
40+
lines.removeIf(s -> s.length() <= 1);
41+
lines.removeIf(s -> s.startsWith("//"));
42+
43+
HashMap<String, String> settingMap = new HashMap<>();
44+
45+
for (String s : lines) {
46+
String[] pair = s.split(":");
47+
48+
// s = keystr: value
49+
// ___ |_[0]|__|[1]|
50+
settingMap.put(pair[0].trim(), pair[1].trim());
51+
}
52+
53+
lines.clear();
54+
55+
int[] nums = null;
56+
57+
nums = parseNums(settingMap.get("POS"));
58+
if (nums != null) {
59+
pos = new Point(nums[0], nums[1]);
60+
}
61+
settingMap.remove("POS");
62+
63+
nums = parseNums(settingMap.get("SIZE"));
64+
if (nums != null) {
65+
size = nums[0];
66+
}
67+
settingMap.remove("SIZE");
68+
69+
nums = parseNums(settingMap.get("BG"));
70+
if (nums != null) {
71+
bg = new Color(nums[0], nums[1], nums[2]);
72+
}
73+
settingMap.remove("BG");
74+
75+
nums = parseNums(settingMap.get("RIM"));
76+
if (nums != null) {
77+
rim = new Color(nums[0], nums[1], nums[2]);
78+
}
79+
settingMap.remove("RIM");
80+
81+
nums = parseNums(settingMap.get("DETAILS"));
82+
if (nums != null) {
83+
detail = new Color(nums[0], nums[1], nums[2]);
84+
}
85+
settingMap.remove("DETAILS");
86+
87+
String cmd = settingMap.get("BOTLEFT");
88+
Shortcuts.setTSC(Shortcuts.BOTLEFT, cmd);
89+
settingMap.remove("BOTLEFT");
90+
91+
cmd = settingMap.get("BOTRIGHT");
92+
Shortcuts.setTSC(Shortcuts.BOTRIGHT, cmd);
93+
settingMap.remove("BOTRIGHT");
94+
95+
cmd = settingMap.get("TOPLEFT");
96+
Shortcuts.setTSC(Shortcuts.TOPLEFT, cmd);
97+
settingMap.remove("TOPLEFT");
98+
99+
cmd = settingMap.get("TOPRIGHT");
100+
Shortcuts.setTSC(Shortcuts.TOPRIGHT, cmd);
101+
settingMap.remove("TOPRIGHT");
102+
103+
for (String key : settingMap.keySet()) {
104+
if (key.contains("->")) {
105+
106+
// [settingMap] = where -> where: mode keys
107+
// _______________|_____key____|__|__val__|
108+
Shortcuts.setSC(key, settingMap.get(key));
109+
}
91110
}
92111

93112
}
@@ -128,28 +147,28 @@ private static void createNewInitFile() {
128147
pw.println("// Window size");
129148
pw.println("SIZE: 200");
130149

131-
} catch (Exception e) {
150+
} catch (IOException e) {
132151
e.printStackTrace();
133152
return;
134153
}
135154
init();
136155

137156
}
138157

139-
private static int[] parseNums(String s, int i) {
140-
int[] res = new int[i];
141-
try {
158+
private static int[] parseNums(String instr) {
159+
String[] parts = instr.split(",");
160+
int[] res = new int[parts.length];
142161

143-
String nums = s.split(": ")[1];
144-
String[] parts = nums.split(",");
162+
try {
145163

146-
for (int j = 0; j < i; j++) {
147-
res[j] = Integer.parseInt(parts[j].trim());
164+
for (int i = 0; i < res.length; i++) {
165+
res[i] = Integer.parseInt(parts[i].trim());
148166
}
149167

150168
} catch (Exception e) {
151169
return null;
152170
}
171+
153172
return res;
154173
}
155174
}

src/main/Shortcut.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11
// File is part of TouchSC (c) 2020 msg-programs, see LICENSE
22
package main;
33

4+
import java.util.ArrayList;
5+
import java.util.Arrays;
6+
import java.util.List;
7+
48
public class Shortcut {
59

6-
public String[] keys;
7-
public int mode = 0;
10+
public ArrayList<String> keys;
11+
public int mode;
812

913
public boolean isHeld = false;
1014

11-
public Shortcut(String mod, String keyRaw) {
12-
this.keys = keyRaw.split("\\+");
15+
public Shortcut(String cmd) {
16+
17+
if (cmd == null || cmd.length() == 0) {
18+
return;
19+
}
20+
21+
// [cmd] = mode keys
22+
// ________|[0|_|[1|
23+
String[] parts = cmd.split(" ");
24+
25+
if (parts.length<2) {
26+
return;
27+
}
1328

14-
switch (mod) {
29+
this.keys = new ArrayList<>(Arrays.asList(parts[1].split("\\+")));
30+
keys.removeIf(s->s==null || s.length() == 0 || s.equals(" "));
31+
32+
if (keys.size()==0) {
33+
keys = null;
34+
}
35+
36+
switch (parts[0]) {
1537
case "press":
1638
mode = Shortcuts.PRESS;
1739
break;
1840
case "toggle":
1941
mode = Shortcuts.TOGGLE;
2042
break;
21-
default:
22-
keys[0] = "None";
23-
break;
2443
}
2544
}
26-
45+
2746
public boolean isDef() {
28-
return !(keys[0].equals("None"));
47+
return keys != null;
2948
}
3049

3150
}

src/main/ShortcutMain.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class ShortcutMain {
1111
public static void main(String[] args) {
1212

1313
Settings.init();
14+
1415
Shortcuts.init();
1516

1617
JFrame frame = new JFrame();

0 commit comments

Comments
 (0)