aboutsummaryrefslogtreecommitdiff
path: root/src/main/SettingsController.java
blob: 4bd2b2433c460acb49840c2eefdd4f743d926928 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main;

import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import javafx.beans.property.SimpleIntegerProperty;

public class SettingsController 
{
	public SimpleIntegerProperty effectsVol = new SimpleIntegerProperty(0);
	public SimpleIntegerProperty musicVol = new SimpleIntegerProperty(0);
	private JSONObject settings;
	
	public void read() throws ParseException
	{
		JSONParser jsonParser = new JSONParser(); //parser to read the file
		
		try(FileReader reader = new FileReader("settings.json"))
		{
			Object obj = jsonParser.parse(reader); 
			
			settings = (JSONObject)(obj); //converts read object to a JSONObject
			
			effectsVol.set((int) settings.get("effectsVol"));
			musicVol.set((int) settings.get("musicVol"));
		}
		catch (FileNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
				
	}

	public void write(int newEffVol, int newMusVol)
	{
		settings.put("musicVol", newMusVol);
		settings.put("effectsVol", newEffVol);
		try (FileWriter file = new FileWriter("settings.json")) 
		{
            //write the settings JSONObject instance to the file
            file.write(settings.toJSONString()); 
            file.flush();
 
        } 
		catch (IOException e) {
            e.printStackTrace();
        }
	}
}