Skip to content

Commit

Permalink
settings persist with Preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
i-make-robots committed Jan 13, 2024
1 parent 2421b42 commit 458404a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.awt.event.*;
import java.awt.*;
import java.util.prefs.Preferences;

/**
* {@link OpenGLPanel} manages a {@link GLJPanel} and an {@link FPSAnimator}.
Expand All @@ -22,12 +23,14 @@ public class OpenGLPanel extends App implements GLEventListener, MouseListener,
private boolean doubleBuffered = true;
private int fsaaSamples = 2;
private boolean verticalSync = true;
private final int fps = 30;
private int fps = 30;
private final FPSAnimator animator = new FPSAnimator(fps);

public OpenGLPanel() {
super(new BorderLayout());

loadPrefs();

try {
logger.info("availability="+ GLProfile.glAvailabilityToString());
GLCapabilities capabilities = getCapabilities();
Expand All @@ -41,6 +44,24 @@ public OpenGLPanel() {
animator.start();
}

private void loadPrefs() {
Preferences pref = Preferences.userNodeForPackage(this.getClass());
hardwareAccelerated = pref.getBoolean("hardwareAccelerated",true);
doubleBuffered = pref.getBoolean("doubleBuffered",true);
fsaaSamples = pref.getInt("fsaaSamples",2);
verticalSync = pref.getBoolean("verticalSync",true);
fps = pref.getInt("fps",30);
}

public void savePrefs() {
Preferences pref = Preferences.userNodeForPackage(this.getClass());
pref.putBoolean("hardwareAccelerated",hardwareAccelerated);
pref.putBoolean("doubleBuffered",doubleBuffered);
pref.putInt("fsaaSamples",fsaaSamples);
pref.putBoolean("verticalSync",verticalSync);
pref.putInt("fps",fps);
}

@Override
public void addNotify() {
super.addNotify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ public ViewportSettingsPanel() {
inclination.setPreferredSize(new Dimension(100,100));
}

@Override
public void removeNotify() {
super.removeNotify();

if(subject!=null) subject.savePrefs();

var dm = getDrawMeshes();
if(dm!=null) dm.savePrefs();
}

private void setSunColor(Color color) {
var dm = getDrawMeshes();
if(dm==null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;

/**
* Draw each {@link MeshInstance} as a {@link Mesh}. If the {@link MeshInstance} has a sibling {@link Material} with
Expand All @@ -45,6 +46,8 @@ public class DrawMeshes extends AbstractRenderPass {
public DrawMeshes() {
super("Meshes");

loadPrefs();

shadowQuad.setRenderStyle(GL3.GL_QUADS);
float v = 100;
shadowQuad.addVertex(-v,-v,0); shadowQuad.addTexCoord(0,0);
Expand All @@ -53,6 +56,22 @@ public DrawMeshes() {
shadowQuad.addVertex(-v, v,0); shadowQuad.addTexCoord(0,1);
}

private void loadPrefs() {
Preferences pref = Preferences.userNodeForPackage(this.getClass());
sunlightSource.x = pref.getDouble("sunlightSource.x",sunlightSource.x);
sunlightSource.y = pref.getDouble("sunlightSource.y",sunlightSource.y);
sunlightSource.z = pref.getDouble("sunlightSource.z",sunlightSource.z);
sunlightColor = new Color(pref.getInt("sunlightColor",sunlightColor.getRGB()));
}

public void savePrefs() {
Preferences pref = Preferences.userNodeForPackage(this.getClass());
pref.putDouble("sunlightSource.x",sunlightSource.x);
pref.putDouble("sunlightSource.y",sunlightSource.y);
pref.putDouble("sunlightSource.z",sunlightSource.z);
pref.putInt("sunlightColor",sunlightColor.getRGB());
}

@Override
public void init(GLAutoDrawable glAutoDrawable) {
GL3 gl3 = glAutoDrawable.getGL().getGL3();
Expand Down

0 comments on commit 458404a

Please sign in to comment.