Add fire mode
[kaka/cakelight.git] / src / kaka / cakelight / Commands.java
1 package kaka.cakelight;
2
3 import kaka.cakelight.mode.*;
4
5 import java.util.function.BiFunction;
6 import java.util.stream.Stream;
7
8 class Commands {
9     private static Console.Command command(String[] names, BiFunction<Console, String[], Boolean> activate) {
10         return new Console.Command() {
11             @Override
12             public String[] getNames() {
13                 return names;
14             }
15
16             @Override
17             public Object activate(Console console, String[] args) {
18                 if (!activate.apply(console, args)) {
19                     console.out("did NOT run command");
20                 }
21                 return null;
22             }
23         };
24     }
25
26     private static Console.Command modeCommand(String[] names, BiFunction<Console, String[], Mode> activate) {
27         return new Console.Command() {
28             @Override
29             public String[] getNames() {
30                 return names;
31             }
32
33             @Override
34             public Object activate(Console console, String[] args) {
35                 Mode mode = activate.apply(console, args);
36                 if (mode == null) {
37                     console.out("did NOT run command");
38                 }
39                 return mode;
40             }
41         };
42     }
43
44     static Console.Command help() {
45         return command(new String[] {"?", "h", "help"}, (console, args) -> {
46             for (Console.Command c : console.getCommands()) {
47                 System.out.println(String.join("|", c.getNames()));
48             }
49             return true;
50         });
51     }
52
53     static Console.Command quit() {
54         return command(new String[] {"q", "quit"}, (console, args) -> {
55             console.quit();
56             console.out("terminating");
57             return true;
58         });
59     }
60
61     static Console.Command push() {
62         return command(new String[] {"push"}, (console, args) -> {
63             Object obj = console.internalHandleInput(String.join(" ", args));
64             if (obj instanceof Mode) { // obj could be anything, which should be fixed
65                 console.out("pushing mode " + obj.getClass().getSimpleName());
66                 console.getCakelight().pushMode((Mode) obj);
67             }
68             return true;
69         });
70     }
71
72     static Console.Command pop() {
73         return command(new String[] {"pop"}, (console, args) -> {
74             console.out("popping mode " + console.getCakelight().popMode().getClass().getSimpleName());
75             return true;
76         });
77     }
78
79     static Console.Command video() {
80         return modeCommand(new String[] {"v", "video"}, (console, args) -> new VideoMode());
81     }
82
83     static Console.Command color() {
84         return modeCommand(new String[] {"c", "col", "color"}, (console, args) -> {
85             Color c = null;
86             if (args.length == 1) {
87                 c = console.parseColor(args[0]);
88             } else if (args.length == 3) {
89                 c = Color.rgb(
90                         Integer.parseInt(args[0]),
91                         Integer.parseInt(args[1]),
92                         Integer.parseInt(args[2])
93                 );
94             }
95             if (c != null) {
96                 console.out("setting color to " + c);
97                 return new SingleColorMode(c);
98             }
99             return null;
100         });
101     }
102
103     static Console.Command brightness() {
104         return command(new String[] {"b", "brightness"}, (console, args) -> {
105             if (args.length == 1) {
106                 int b = Integer.parseInt(args[0].replaceAll("[+-]+", ""));
107                 if (args[0].startsWith("+")) {
108                     b = Math.min(console.getConfig().leds.brightness + b, 31);
109                 } else if (args[0].startsWith("-")) {
110                     b = Math.max(console.getConfig().leds.brightness - b, 0);
111                 }
112                 console.getConfig().leds.brightness = b;
113                 console.out("setting brightness to " + b);
114                 return true;
115             } else {
116                 return false;
117             }
118         });
119     }
120
121     static Console.Command gamma() {
122         return command(new String[] {"g", "gamma"}, (console, args) -> {
123             if (args.length == 1) {
124                 double g = Double.parseDouble(args[0]);
125                 console.getConfig().gamma = g;
126                 Color.calculateGammaCorrection(g);
127                 console.out("setting gamma to " + g);
128                 return true;
129             } else {
130                 return false;
131             }
132         });
133     }
134
135     static Console.Command saturation() {
136         return command(new String[] {"s", "saturation"}, (console, args) -> {
137             if (args.length == 1) {
138                 double s = Double.parseDouble(args[0]);
139                 console.getConfig().video.saturation = s;
140                 console.out("setting saturation to " + s);
141                 return true;
142             } else {
143                 return false;
144             }
145         });
146     }
147
148     static Console.Command ambientMode() {
149         return modeCommand(new String[] {"m", "mode"}, (console, args) -> {
150             if (args.length == 1) {
151                 console.out("setting ambient mode to " + args[0]);
152                 return new AmbientMode(new String[]{args[0]});
153             }
154             return null;
155         });
156     }
157
158     static Console.Command noiseMode() {
159         return modeCommand(new String[] {"n", "noise"}, (console, args) -> {
160             if (args.length > 1) {
161                 console.out("setting multi-color noise mode");
162                 return new NoiseMode(Stream.of(args)
163                                              .map(console::parseColor)
164                                              .toArray(Color[]::new)
165                 );
166             }
167             return null;
168         });
169     }
170
171     static Console.Command fireMode() {
172         return modeCommand(new String[] {"f", "fire"}, (console, args) -> {
173             if (args.length > 1) {
174                 console.out("setting multi-color fire mode");
175                 return new FireMode(Stream.of(args)
176                         .map(console::parseColor)
177                         .toArray(Color[]::new)
178                 );
179             }
180             return null;
181         });
182     }
183
184     static Console.Command sunriseMode() {
185         return modeCommand(new String[] {"sunrise"}, (console, args) -> {
186             if (args.length == 1) {
187                 int durationSeconds = Integer.parseInt(args[0]);
188                 console.out("setting sunrise mode with duration " + durationSeconds);
189                 return new SunriseMode(durationSeconds);
190             }
191             return null;
192         });
193     }
194 }