diff --git a/src/lsystem/Main.java b/src/lsystem/Main.java index 76dab7a..688ffb1 100644 --- a/src/lsystem/Main.java +++ b/src/lsystem/Main.java @@ -5,9 +5,15 @@ import lsystem.screen.main.MainFrame; public class Main { + public static MainFrame mainFrame; + public static GLCanvas joglFrame; + public static void main(String[] args) { - MainFrame frame = new MainFrame(); - frame.setVisible(true); + new Thread(() -> { + mainFrame = new MainFrame(); + mainFrame.setVisible(true); + }).start(); + new Thread(() -> joglFrame = new GLCanvas()).start(); } } diff --git a/src/lsystem/Main2D.java b/src/lsystem/Main2D.java index 9044f09..c5ffde3 100644 --- a/src/lsystem/Main2D.java +++ b/src/lsystem/Main2D.java @@ -9,7 +9,7 @@ public class Main2D { MainFrame frame = new MainFrame(); frame.setVisible(true); - SwingGLCanvas2D canvas = new SwingGLCanvas2D(null); + SwingGLCanvas2D canvas = new SwingGLCanvas2D(); canvas.setVisible(true); } diff --git a/src/lsystem/screen/AbstractCanvas.java b/src/lsystem/screen/AbstractCanvas.java index 0540ef9..6cdf589 100644 --- a/src/lsystem/screen/AbstractCanvas.java +++ b/src/lsystem/screen/AbstractCanvas.java @@ -7,15 +7,20 @@ import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.FPSAnimator; import com.jogamp.opengl.util.gl2.GLUT; import lsystem.engine.Element; +import lsystem.engine.Parser; +import lsystem.engine.Rewrite; +import lsystem.utils.Pair; import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.util.List; public abstract class AbstractCanvas { - private final Element lsystem; + private Element lsystem; + public State parsedState = State.FINISH_OR_NULL; public JFrame frame; protected FPSAnimator animator; public final GLCanvas glCanvas; @@ -25,8 +30,7 @@ public abstract class AbstractCanvas { 0f, 0f, 0f}; // camera rotation yaw(x-axis), pitch(y-axis), roll(z-axis) - protected AbstractCanvas(Element parsed) { - this.lsystem = parsed; + protected AbstractCanvas() { GLProfile glProfile = GLProfile.getDefault(); GLCapabilities glCapabilities = new GLCapabilities(glProfile); this.glCanvas = new GLCanvas(glCapabilities); @@ -37,6 +41,9 @@ public abstract class AbstractCanvas { public void windowClosing(WindowEvent e) { frame.dispose(); setVisible(false); + lsystem = null; + parsedState = State.FINISH_OR_NULL; + System.gc(); } }); frame.getContentPane().add(glCanvas, BorderLayout.CENTER); @@ -53,4 +60,15 @@ public abstract class AbstractCanvas { animator.stop(); frame.setVisible(bl); } + + public void setLsystem(String axiom, List> rules, int iterations) { + parsedState = State.LOAD; + this.lsystem = Parser.parse(Rewrite.rewrite(axiom, rules, iterations)); + parsedState = State.FINISH_OR_NULL; + } + + public enum State { + LOAD, + FINISH_OR_NULL; + } } diff --git a/src/lsystem/screen/gl2d/SwingGLCanvas2D.java b/src/lsystem/screen/gl2d/SwingGLCanvas2D.java index 931be1b..f6396ef 100644 --- a/src/lsystem/screen/gl2d/SwingGLCanvas2D.java +++ b/src/lsystem/screen/gl2d/SwingGLCanvas2D.java @@ -1,15 +1,10 @@ package lsystem.screen.gl2d; -import lsystem.engine.Element; import lsystem.screen.AbstractCanvas; public class SwingGLCanvas2D extends AbstractCanvas { - public SwingGLCanvas2D(Element parsed) { - super(parsed); - } - @Override protected void addEventsListeners() { glCanvas.addGLEventListener(new JoglEventListener2D(this)); diff --git a/src/lsystem/screen/gl3d/GLCanvas.java b/src/lsystem/screen/gl3d/GLCanvas.java index d1a2c0f..cec118f 100644 --- a/src/lsystem/screen/gl3d/GLCanvas.java +++ b/src/lsystem/screen/gl3d/GLCanvas.java @@ -1,15 +1,10 @@ package lsystem.screen.gl3d; -import lsystem.engine.Element; import lsystem.screen.AbstractCanvas; public class GLCanvas extends AbstractCanvas { - public GLCanvas(Element parsed) { - super(parsed); - } - @Override protected void addEventsListeners() { glCanvas.addGLEventListener(new GLEventListener(this)); diff --git a/src/lsystem/screen/main/Listener.java b/src/lsystem/screen/main/Listener.java index 7fe7948..f2edcda 100644 --- a/src/lsystem/screen/main/Listener.java +++ b/src/lsystem/screen/main/Listener.java @@ -1,8 +1,8 @@ package lsystem.screen.main; +import lsystem.Main; import lsystem.engine.Parser; -import lsystem.engine.Rewrite; -import lsystem.screen.gl3d.GLCanvas; +import lsystem.screen.AbstractCanvas; import javax.swing.*; import java.awt.event.ActionEvent; @@ -48,24 +48,29 @@ public class Listener implements ActionListener, KeyListener { String axiom = tab.getAxiom(); List rules = tab.getRules(); Parser parser = new Parser(axiom, rules, tab.getNbIterations()); - if (!parser.isCorrect()) { - JOptionPane.showMessageDialog(null, "Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); - new Listener(null, index, "Clear",tab); + if(Main.joglFrame.frame.isVisible()) { + openDialog("Veuillez fermer la fenêtre 3D avant de lancer une nouvelle génération"); + } else if(Main.joglFrame.parsedState == AbstractCanvas.State.LOAD) { + openDialog("Une génération est actuellement en cours, impossible d'en relancer un autre"); + } else if (!parser.isCorrect()) { + openDialog("Vos règles ou votre axiome ne sont pas correctement écrites, veuillez recommencer"); } else { - Thread thread = new Thread(() -> { - GLCanvas canvas = new GLCanvas( - Parser.parse(Rewrite.rewrite(axiom, parser.parseRules(), tab.getNbIterations())) - ); - canvas.setVisible(true); - }); - thread.setDaemon(true); - thread.start(); + new Thread(() -> { + Main.joglFrame.setLsystem(axiom, parser.parseRules(), tab.getNbIterations()); + Main.joglFrame.setVisible(true); + }).start(); } break; } } + + private void openDialog(String message) { + JOptionPane.showMessageDialog(null, message); + new Listener(null, index, "Clear", tab); + } + @Override public void keyTyped(KeyEvent ke) { diff --git a/src/lsystem/screen/main/MainFrame.java b/src/lsystem/screen/main/MainFrame.java index fe4a071..0c706ef 100644 --- a/src/lsystem/screen/main/MainFrame.java +++ b/src/lsystem/screen/main/MainFrame.java @@ -2,9 +2,11 @@ package lsystem.screen.main; import lsystem.screen.Constants; -import java.awt.event.*; + import javax.swing.*; import java.awt.*; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; public class MainFrame extends JFrame { @@ -49,7 +51,7 @@ public class MainFrame extends JFrame { } public void newHelp() { - if(helpWindow != false){ + if(helpWindow){ JOptionPane.showMessageDialog(null, "Une fenêtre d'aide est déjà ouverte."); } else {