aboutsummaryrefslogtreecommitdiff
path: root/src/main/SettingsController.java
blob: b7b2cb94e157106e9babf197834bcb7eb2cb59aa (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
package main;

import java.io.FileWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import javafx.beans.property.SimpleDoubleProperty;

public class SettingsController 
{
	public SimpleDoubleProperty effectsVol = new SimpleDoubleProperty(1);
	public SimpleDoubleProperty musicVol = new SimpleDoubleProperty(1);
	private File file = new File("settings.json");

	public SettingsController()
	{
		read();
	}
	
	public void read()
	{
		JSONParser jsonParser = new JSONParser(); //parser to read the file
		try(FileReader reader = new FileReader(file))
		{
			Object obj = jsonParser.parse(reader); 
			JSONObject settings = new JSONObject();
			settings = (JSONObject)(obj); //converts read object to a JSONObject
			
			effectsVol.set(Double.parseDouble(settings.get("effectsVol")+""));
			musicVol.set(Double.parseDouble(settings.get("musicVol")+""));
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}		
	}

	public void write()
	{
		FileWriter fileWriter;
		try
		{
			fileWriter = new FileWriter(file);
			JSONObject obj = new JSONObject();
			obj.put("musicVol", musicVol.getValue());
			obj.put("effectsVol", effectsVol.getValue());
            obj.writeJSONString(fileWriter);
            fileWriter.flush();
        } 
		catch (IOException e) {
            e.printStackTrace();
        }
	}
}