package org.pollux3d.editor;
import com.jme3.app.SimpleApplication;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
import java.awt.Canvas;
import java.awt.Desktop;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.URI;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import org.pollux3d.editor.app.EditorApp;
public class PolluxEdit {
private static JmeCanvasContext context;
private static Canvas canvas;
private static EditorApp app;
private static JFrame frame;
private static String workspace = "";
private static AssetsDir assetsDir;
private static JMenu menuModel;
private static ModelDir activeModel;
private static void createFrame(){
frame = new JFrame("Pollux Editor");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosed(WindowEvent e) {
quit();
}
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
quit();
}
});
}
private static void loadModel(ModelDir model) {
menuModel.setEnabled(true);
activeModel = model;
app.loadModel(model.getRelPath(workspace));
}
private static void createMenu(){
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu menuFile = new JMenu("Editor");
menuBar.add(menuFile);
JMenu menuLoad = new JMenu("Open Model");
menuFile.add(menuLoad);
for (final ModelDir model: assetsDir.getModels()) {
JMenuItem modelItem = new JMenuItem(model.getName());
menuLoad.add(modelItem);
modelItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
loadModel(model);
}
});
}
menuModel = new JMenu("Model");
menuBar.add(menuModel);
menuModel.setEnabled(false);
JMenuItem modelInfoItem = new JMenuItem("Model Info");
modelInfoItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.ALT_MASK));
menuModel.add(modelInfoItem);
modelInfoItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
showInfo();
}
});
menuFile.addSeparator();
JMenuItem itemExit = new JMenuItem("Exit");
menuFile.add(itemExit);
itemExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
quit();
}
});
JMenu menuEngine = new JMenu("Engine");
menuBar.add(menuEngine);
JMenu menuPostProcessors = new JMenu("PostProcessors");
menuEngine.add(menuPostProcessors);
JMenuItem itemBloomFilter= new JCheckBoxMenuItem("BloomFilter Glow");
itemBloomFilter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, ActionEvent.ALT_MASK));
menuPostProcessors.add(itemBloomFilter);
itemBloomFilter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JCheckBoxMenuItem item = ((JCheckBoxMenuItem)ae.getSource());
if (item.getState()) {
app.addBloomFilter();
} else {
app.removeBloomFilter();
}
}
});
JMenu menuLight = new JMenu("Lights");
menuEngine.add(menuLight);
JMenuItem itemDirectionalLight= new JCheckBoxMenuItem("Directional Light");
itemDirectionalLight.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK));
menuLight.add(itemDirectionalLight);
itemDirectionalLight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JCheckBoxMenuItem item = ((JCheckBoxMenuItem)ae.getSource());
app.setDirectionalLight(item.getState());
}
});
//itemDirectionalLight.setSelected(true);
JMenuItem itemAmbientLight= new JCheckBoxMenuItem("Ambient Light");
itemAmbientLight.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.ALT_MASK));
menuLight.add(itemAmbientLight);
itemAmbientLight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JCheckBoxMenuItem item = ((JCheckBoxMenuItem)ae.getSource());
app.setAmbientLight(item.getState());
}
});
JMenuItem itemPointLight= new JCheckBoxMenuItem("Point Light (6x)");
itemPointLight.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.ALT_MASK));
menuLight.add(itemPointLight);
itemPointLight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JCheckBoxMenuItem item = ((JCheckBoxMenuItem)ae.getSource());
app.setPointLight(item.getState());
}
});
itemPointLight.setSelected(true);
JMenu menuAbout = new JMenu("About");
menuBar.add(menuAbout);
JMenuItem itemLink = new JMenuItem("Website");
itemLink.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
Desktop desktop = Desktop.getDesktop();
desktop.browse(new URI("http://www.pollux3d.org"));
} catch (Exception e) {}
}
});
menuAbout.add(itemLink);
}
public static void quit() {
frame.dispose();
app.stop();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.exit(0);
}
public static void showInfo() {
try {
new ModelInfo(activeModel, app.getAssetLogger().getKeys(activeModel.getRelPath(workspace)));
} catch (Exception e) {}
}
public static void createCanvas(){
AppSettings settings = new AppSettings(true);
settings.setWidth( Math.max(640, frame.getContentPane().getWidth()) );
settings.setHeight( Math.max(480, frame.getContentPane().getHeight()) );
app = new EditorApp();
app.setPauseOnLostFocus(false);
app.setSettings(settings);
app.createCanvas();
context = (JmeCanvasContext) app.getContext();
canvas = context.getCanvas();
canvas.setSize(settings.getWidth(), settings.getHeight());
}
public static void startApp(){
app.startCanvas();
app.enqueue(new Callable<Void>(){
public Void call(){
if (app instanceof SimpleApplication){
SimpleApplication simpleApp = (SimpleApplication) app;
simpleApp.getFlyByCamera().setDragToRotate(true);
String assdir = new File(workspace).getAbsolutePath();
app.setAssetsPath(assdir);
}
return null;
}
});
}
public static boolean workspacePopup() {
if (System.getProperty("os.name").startsWith("Mac")) {
String prop = "apple.awt.fileDialogForDirectories";
System.setProperty(prop, "true");
FileDialog fd = new FileDialog(frame, "Select assetes directory");
fd.setVisible(true);
workspace = fd.getDirectory()+fd.getFile();
System.setProperty(prop, "false");
} else {
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Select assetes directory");
JList list = new JList();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(list)) {
File file = fc.getSelectedFile();
workspace = file.getAbsolutePath();
}
}
return new File(workspace).isDirectory();
}
private static void createModel() {
try {
assetsDir = new AssetsDir(workspace);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
Logger.getLogger("").setLevel(Level.SEVERE);
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
createFrame();
if(!workspacePopup()) System.exit(1);
createModel();
createMenu();
createCanvas();
frame.getContentPane().add(canvas);
frame.pack();
startApp();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}