|
9 | 9 | import java.io.IOException; |
10 | 10 | import java.io.PrintWriter; |
11 | 11 | import java.util.ArrayList; |
| 12 | +import java.util.HashMap; |
12 | 13 |
|
13 | 14 | public class Settings { |
14 | 15 |
|
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);; |
18 | 19 |
|
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; |
21 | 22 |
|
22 | 23 | public static void init() { |
| 24 | + ArrayList<String> lines = new ArrayList<>(); |
| 25 | + |
23 | 26 | try (BufferedReader br = new BufferedReader(new FileReader("./settings.ini"))) { |
24 | | - ArrayList<String> lines = new ArrayList<>(); |
25 | 27 |
|
26 | 28 | String line; |
27 | 29 | while ((line = br.readLine()) != null) { |
28 | | - |
29 | | - lines.add(line); |
30 | | - } |
31 | 30 |
|
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); |
83 | 32 | } |
84 | 33 |
|
85 | 34 | } catch (FileNotFoundException fnfe) { |
86 | 35 | createNewInitFile(); |
87 | 36 | } catch (IOException e) { |
88 | 37 | 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 | + } |
91 | 110 | } |
92 | 111 |
|
93 | 112 | } |
@@ -128,28 +147,28 @@ private static void createNewInitFile() { |
128 | 147 | pw.println("// Window size"); |
129 | 148 | pw.println("SIZE: 200"); |
130 | 149 |
|
131 | | - } catch (Exception e) { |
| 150 | + } catch (IOException e) { |
132 | 151 | e.printStackTrace(); |
133 | 152 | return; |
134 | 153 | } |
135 | 154 | init(); |
136 | 155 |
|
137 | 156 | } |
138 | 157 |
|
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]; |
142 | 161 |
|
143 | | - String nums = s.split(": ")[1]; |
144 | | - String[] parts = nums.split(","); |
| 162 | + try { |
145 | 163 |
|
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()); |
148 | 166 | } |
149 | 167 |
|
150 | 168 | } catch (Exception e) { |
151 | 169 | return null; |
152 | 170 | } |
| 171 | + |
153 | 172 | return res; |
154 | 173 | } |
155 | 174 | } |
0 commit comments