1 package com.badlogic.gdx.tools.flame; 2 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import java.io.File; 6 7 import javax.swing.JButton; 8 import javax.swing.JOptionPane; 9 10 import com.badlogic.gdx.assets.loaders.ModelLoader; 11 import com.badlogic.gdx.assets.loaders.resolvers.AbsoluteFileHandleResolver; 12 import com.badlogic.gdx.graphics.g3d.Model; 13 import com.badlogic.gdx.graphics.g3d.loader.G3dModelLoader; 14 import com.badlogic.gdx.graphics.g3d.loader.ObjLoader; 15 import com.badlogic.gdx.graphics.g3d.particles.ParticleEffect; 16 import com.badlogic.gdx.utils.JsonReader; 17 import com.badlogic.gdx.utils.UBJsonReader; 18 19 /** @author Inferno */ 20 public abstract class LoaderButton<T> extends JButton{ 21 22 public static class ParticleEffectLoaderButton extends LoaderButton<ParticleEffect>{ 23 public ParticleEffectLoaderButton (FlameMain editor) { 24 this(editor, null); 25 } 26 public ParticleEffectLoaderButton (FlameMain editor, Listener<ParticleEffect> listener) { 27 super(editor, "Load Controller", listener); 28 } 29 30 protected void loadResource () { 31 File file = editor.showFileLoadDialog(); 32 if(file != null){ 33 try{ 34 String resource = file.getAbsolutePath(); 35 listener.onResourceLoaded(editor.openEffect(file, false)); 36 } catch (Exception ex) { 37 System.out.println("Error loading effect: " + file.getAbsolutePath()); 38 ex.printStackTrace(); 39 JOptionPane.showMessageDialog(getParent(), "Error opening effect."); 40 return; 41 } 42 } 43 } 44 } 45 46 public static class ModelLoaderButton extends LoaderButton<Model>{ 47 public ModelLoaderButton (FlameMain editor) { 48 this(editor, null); 49 } 50 public ModelLoaderButton (FlameMain editor, Listener<Model> listener) { 51 super(editor, "Load Model", listener); 52 } 53 54 protected void loadResource () { 55 File file = editor.showFileLoadDialog(); 56 if(file != null){ 57 try{ 58 String resource = file.getAbsolutePath(); 59 ModelLoader modelLoader = null; 60 if(resource.endsWith(".obj")){ 61 modelLoader = new ObjLoader(new AbsoluteFileHandleResolver()); 62 } 63 else if(resource.endsWith(".g3dj")){ 64 modelLoader = new G3dModelLoader(new JsonReader(), new AbsoluteFileHandleResolver()); 65 } 66 else if(resource.endsWith(".g3db")){ 67 modelLoader = new G3dModelLoader(new UBJsonReader(), new AbsoluteFileHandleResolver()); 68 } 69 else throw new Exception(); 70 listener.onResourceLoaded(editor.load(resource, Model.class, modelLoader, null)); 71 72 } catch (Exception ex) { 73 System.out.println("Error loading model: " + file.getAbsolutePath()); 74 ex.printStackTrace(); 75 JOptionPane.showMessageDialog(getParent(), "Error opening effect."); 76 return; 77 } 78 } 79 } 80 } 81 82 public interface Listener<T>{ 83 void onResourceLoaded (T resource); 84 } 85 86 private String lastDir; 87 protected Listener<T> listener; 88 FlameMain editor; 89 90 public LoaderButton (FlameMain editor, String text, Listener<T> listener) { 91 super(text); 92 this.editor = editor; 93 this.listener = listener; 94 addActionListener(new ActionListener() { 95 @Override 96 public void actionPerformed (ActionEvent e) { 97 loadResource(); 98 } 99 }); 100 } 101 102 public LoaderButton (FlameMain editor, String text){ 103 this(editor, text, null); 104 } 105 106 public void setListener(Listener listener){ 107 this.listener = listener; 108 } 109 110 111 protected abstract void loadResource (); 112 113 } 114