Skip to content

Commit 607e336

Browse files
committed
Added a basic effects section to the main window
1 parent d3e5d44 commit 607e336

File tree

3 files changed

+293
-35
lines changed

3 files changed

+293
-35
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
}

src/io/github/rowak/nanoleafdesktop/ui/menu/EffectOptionsMenu.java

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,32 @@
1212

1313
import io.github.rowak.Aurora;
1414
import io.github.rowak.StatusCodeException;
15+
import io.github.rowak.nanoleafdesktop.tools.BasicEffects;
1516
import io.github.rowak.nanoleafdesktop.ui.dialog.OptionDialog;
1617
import io.github.rowak.nanoleafdesktop.ui.dialog.SingleEntryDialog;
1718
import io.github.rowak.nanoleafdesktop.ui.panel.EffectsPanel;
1819

1920
public class EffectOptionsMenu extends ModernPopupMenu
2021
{
22+
private boolean basicEffectsMode;
2123
private String effect;
2224
private EffectsPanel effectsPanel;
2325
private Aurora[] devices;
2426
private Component parent;
2527

26-
public EffectOptionsMenu(EffectsPanel effectsPanel,
28+
public EffectOptionsMenu(EffectsPanel effectsPanel, String label,
2729
Aurora[] devices, Component parent)
2830
{
2931
this.effectsPanel = effectsPanel;
3032
this.devices = devices;
3133
this.parent = parent;
3234
effect = getEffect();
3335
initUI();
36+
37+
if (label.equals("Basic Effects"))
38+
{
39+
basicEffectsMode = true;
40+
}
3441
}
3542

3643
private void initUI()
@@ -39,12 +46,12 @@ private void initUI()
3946
ModernMenuItem itemRename = new ModernMenuItem("Rename");
4047
itemRename.addActionListener((e) ->
4148
{
42-
renameEffect();
49+
showRenameDialog();
4350
});
4451
ModernMenuItem itemDelete = new ModernMenuItem("Delete");
4552
itemDelete.addActionListener((e) ->
4653
{
47-
deleteEffect();
54+
showDeleteDialog();
4855
});
4956

5057
add(itemRename);
@@ -86,7 +93,22 @@ private Point getEffectLocation()
8693
return listPoint;
8794
}
8895

89-
private void renameEffect()
96+
private void renameEffectOnDevice(String newName)
97+
{
98+
try
99+
{
100+
for (Aurora device : devices)
101+
{
102+
device.effects().renameEffect(effect, newName);
103+
}
104+
}
105+
catch (StatusCodeException sce)
106+
{
107+
sce.printStackTrace();
108+
}
109+
}
110+
111+
private void showRenameDialog()
90112
{
91113
SingleEntryDialog renameDialog = new SingleEntryDialog(
92114
parent, effect, "Ok",
@@ -99,16 +121,13 @@ public void actionPerformed(ActionEvent e)
99121
(SingleEntryDialog)((JButton)e.getSource())
100122
.getTopLevelAncestor();
101123
String newName = dialog.getEntryField().getText();
102-
try
124+
if (basicEffectsMode)
103125
{
104-
for (Aurora device : devices)
105-
{
106-
device.effects().renameEffect(effect, newName);
107-
}
126+
BasicEffects.renameBasicEffect(effect, newName);
108127
}
109-
catch (StatusCodeException sce)
128+
else
110129
{
111-
sce.printStackTrace();
130+
renameEffectOnDevice(newName);
112131
}
113132
int i = effectsPanel.getModel().indexOf(effect);
114133
effectsPanel.getModel().setElementAt(newName, i);
@@ -118,25 +137,42 @@ public void actionPerformed(ActionEvent e)
118137
renameDialog.setVisible(true);
119138
}
120139

121-
private void deleteEffect()
140+
private void deleteEffectFromDevice()
122141
{
142+
try
143+
{
144+
for (Aurora device : devices)
145+
{
146+
device.effects().deleteEffect(effect);
147+
}
148+
}
149+
catch (StatusCodeException sce)
150+
{
151+
sce.printStackTrace();
152+
}
153+
}
154+
155+
private void showDeleteDialog()
156+
{
157+
String target = " from your device?";
158+
if (basicEffectsMode)
159+
{
160+
target = " from this computer?";
161+
}
123162
OptionDialog deleteDialog = new OptionDialog(parent,
124-
"Are you sure you want to delete " + effect + " from your device?",
163+
"Are you sure you want to delete " + effect + target,
125164
"Yes", "No", new ActionListener()
126165
{
127166
@Override
128167
public void actionPerformed(ActionEvent e)
129168
{
130-
try
169+
if (basicEffectsMode)
131170
{
132-
for (Aurora device : devices)
133-
{
134-
device.effects().deleteEffect(effect);
135-
}
171+
BasicEffects.removeBasicEffect(effect);
136172
}
137-
catch (StatusCodeException sce)
173+
else
138174
{
139-
sce.printStackTrace();
175+
deleteEffectFromDevice();
140176
}
141177
effectsPanel.removeEffect(effect);
142178
OptionDialog dialog = (OptionDialog)((JButton)e.getSource())

0 commit comments

Comments
 (0)