Last active
September 27, 2022 16:18
-
-
Save renaudfv/7032459b893e2077b2e7249c2a80a58e to your computer and use it in GitHub Desktop.
Processing config file loader and UI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import controlP5.*; | |
SettingsManager sm; | |
ControlP5 cp5; | |
boolean visible; | |
float speed; | |
void setup() { | |
size(400, 400); | |
cp5 = new ControlP5(this); | |
// In a real context this file would live in the data folder | |
sm = new SettingsManager("settings.json"); | |
visible = sm.getSettings().getBoolean("visible"); | |
speed = sm.getSettings().getFloat("speed"); | |
cp5.addToggle("visible").setValue(visible); | |
cp5.addSlider("speed") | |
.setMax(1.0) | |
.setMin(0.0) | |
.setValue(speed); | |
cp5.addButton("Save"); | |
} | |
void draw() {} | |
void visible(boolean val) { | |
sm.getSettings().setBoolean("visible", val); | |
} | |
void speed(float val) { | |
sm.getSettings().setFloat("speed", val); | |
} | |
void Save() { | |
// Saves to file | |
sm.saveSettings(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"visible": false, | |
"speed": 0.5252525210380554 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
class SettingsManager { | |
String filePath; | |
JSONObject settings; | |
SettingsManager(String path) { | |
this.setFilepath(path); | |
this.loadSettings(); | |
} | |
void setFilepath(String path) { | |
this.filePath = path; | |
} | |
void loadSettings() { | |
this.settings = loadJSONObject(this.filePath); | |
println("Settings loaded\n"); | |
Iterator iterator = this.settings.keyIterator(); | |
while (iterator.hasNext()) { | |
String key = (String)iterator.next(); | |
println("\t" + key + ": " + this.settings.get(key)); | |
} | |
} | |
JSONObject getSettings() { | |
return this.settings; | |
} | |
void saveSettings() { | |
saveJSONObject(this.settings, this.filePath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment