|
| 1 | +package io.github.rowak.nanoleafdesktop.tools; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.json.JSONArray; |
| 7 | +import org.json.JSONObject; |
| 8 | + |
| 9 | +import io.github.rowak.Aurora; |
| 10 | +import io.github.rowak.Color; |
| 11 | +import io.github.rowak.Effect; |
| 12 | +import io.github.rowak.Frame; |
| 13 | +import io.github.rowak.StatusCodeException; |
| 14 | +import io.github.rowak.StatusCodeException.UnauthorizedException; |
| 15 | +import io.github.rowak.effectbuilder.StaticEffectBuilder; |
| 16 | +import io.github.rowak.nanoleafdesktop.Main; |
| 17 | + |
| 18 | +public class BasicEffects |
| 19 | +{ |
| 20 | + public static final Object[][] BUILTIN_EFFECTS = |
| 21 | + { |
| 22 | + new Object[]{"Warm White", 40, 80}, |
| 23 | + new Object[]{"Reading Light", 48, 48}, |
| 24 | + new Object[]{"Daylight", 50, 26} |
| 25 | + }; |
| 26 | + |
| 27 | + public static void initializeBasicEffects() |
| 28 | + { |
| 29 | + PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH); |
| 30 | + if (manager.getProperty("basicEffects") == null) |
| 31 | + { |
| 32 | + JSONArray arr = new JSONArray(); |
| 33 | + for (int i = 0; i < BUILTIN_EFFECTS.length; i++) |
| 34 | + { |
| 35 | + JSONObject obj = new JSONObject(); |
| 36 | + obj.put("name", BUILTIN_EFFECTS[i][0]); |
| 37 | + obj.put("hue", (int)BUILTIN_EFFECTS[i][1]); |
| 38 | + obj.put("sat", BUILTIN_EFFECTS[i][2]); |
| 39 | + arr.put(obj); |
| 40 | + } |
| 41 | + manager.setProperty("basicEffects", arr.toString()); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public static void addBasicEffect(String name, int hue, int sat) |
| 46 | + { |
| 47 | + initializeBasicEffects(); |
| 48 | + |
| 49 | + PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH); |
| 50 | + String saved = manager.getProperty("basicEffects"); |
| 51 | + if (saved == null) |
| 52 | + { |
| 53 | + saved = ""; |
| 54 | + } |
| 55 | + JSONArray arr = new JSONArray(saved); |
| 56 | + |
| 57 | + JSONObject obj = new JSONObject(); |
| 58 | + obj.put("name", name); |
| 59 | + obj.put("hut", hue); |
| 60 | + obj.put("sat", sat); |
| 61 | + |
| 62 | + for (int i = 0; i < arr.length(); i++) |
| 63 | + { |
| 64 | + if (arr.getJSONObject(i).getString("name").equals(name)) |
| 65 | + { |
| 66 | + // update effect with same name |
| 67 | + arr.put(i, obj); |
| 68 | + manager.setProperty("basicEffects", saved.toString()); |
| 69 | + return; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // add new effect |
| 74 | + arr.put(obj); |
| 75 | + manager.setProperty("basicEffects", saved.toString()); |
| 76 | + } |
| 77 | + |
| 78 | + public static void removeBasicEffect(String name) |
| 79 | + { |
| 80 | + initializeBasicEffects(); |
| 81 | + |
| 82 | + PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH); |
| 83 | + String saved = manager.getProperty("basicEffects"); |
| 84 | + if (saved == null) |
| 85 | + { |
| 86 | + saved = ""; |
| 87 | + } |
| 88 | + JSONArray arr = new JSONArray(saved); |
| 89 | + |
| 90 | + for (int i = 0; i < arr.length(); i++) |
| 91 | + { |
| 92 | + if (arr.getJSONObject(i).getString("name").equals(name)) |
| 93 | + { |
| 94 | + arr.remove(i); |
| 95 | + manager.setProperty("basicEffects", arr.toString()); |
| 96 | + return; |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public static void renameBasicEffect(String name, String newName) |
| 102 | + { |
| 103 | + initializeBasicEffects(); |
| 104 | + |
| 105 | + PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH); |
| 106 | + String saved = manager.getProperty("basicEffects"); |
| 107 | + if (saved == null) |
| 108 | + { |
| 109 | + saved = ""; |
| 110 | + } |
| 111 | + JSONArray arr = new JSONArray(saved); |
| 112 | + |
| 113 | + for (int i = 0; i < arr.length(); i++) |
| 114 | + { |
| 115 | + if (arr.getJSONObject(i).getString("name").equals(name)) |
| 116 | + { |
| 117 | + arr.getJSONObject(i).put("name", newName); |
| 118 | + manager.setProperty("basicEffects", arr.toString()); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static List<List<Effect>> getBasicEffects(Aurora[] devices) |
| 124 | + throws UnauthorizedException, StatusCodeException |
| 125 | + { |
| 126 | + List<List<Effect>> basicEffects = new ArrayList<List<Effect>>(); |
| 127 | + for (int i = 0; i < devices.length; i++) |
| 128 | + { |
| 129 | + basicEffects.add(new ArrayList<Effect>()); |
| 130 | + } |
| 131 | + |
| 132 | + PropertyManager manager = new PropertyManager(Main.PROPERTIES_FILEPATH); |
| 133 | + String strEffects = manager.getProperty("basicEffects"); |
| 134 | + if (strEffects != null) |
| 135 | + { |
| 136 | + JSONArray arr = new JSONArray(strEffects); |
| 137 | + for (int i = 0; i < arr.length(); i++) |
| 138 | + { |
| 139 | + for (int d = 0; d < devices.length; d++) |
| 140 | + { |
| 141 | + JSONObject obj = arr.getJSONObject(i); |
| 142 | + String name = obj.getString("name"); |
| 143 | + int hue = obj.getInt("hue"); |
| 144 | + int sat = obj.getInt("sat"); |
| 145 | + basicEffects.get(d).add(getEffect(name, |
| 146 | + hue, sat, devices[d])); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + return basicEffects; |
| 151 | + } |
| 152 | + |
| 153 | + private static Effect getEffect(String name, int hue, int sat, Aurora device) |
| 154 | + throws UnauthorizedException, StatusCodeException |
| 155 | + { |
| 156 | + Color color = Color.fromHSB(hue, sat, |
| 157 | + device.state().getBrightness()); |
| 158 | + return new StaticEffectBuilder(device) |
| 159 | + .setAllPanels(new Frame(color, 2)) |
| 160 | + .build(name); |
| 161 | + } |
| 162 | +} |
0 commit comments