Home | History | Annotate | Download | only in flame
      1 package com.badlogic.gdx.tools.flame;
      2 
      3 import java.awt.GridBagConstraints;
      4 import java.awt.Insets;
      5 
      6 import com.badlogic.gdx.graphics.g3d.particles.ParticleController;
      7 import com.badlogic.gdx.graphics.g3d.particles.ParticleEffect;
      8 import com.badlogic.gdx.graphics.g3d.particles.influencers.ParticleControllerInfluencer;
      9 import com.badlogic.gdx.utils.Array;
     10 
     11 /** @author Inferno */
     12 public class ParticleControllerInfluencerPanel extends InfluencerPanel<ParticleControllerInfluencer> implements TemplatePickerPanel.Listener<ParticleController>,
     13 																																									LoaderButton.Listener<ParticleEffect>,
     14 																																									EventManager.Listener{
     15 	TemplatePickerPanel<ParticleController> controllerPicker;
     16 
     17 	public ParticleControllerInfluencerPanel (FlameMain editor, ParticleControllerInfluencer influencer, boolean single, String name, String desc) {
     18 		super(editor, influencer, name, desc, true, false);
     19 		controllerPicker.setMultipleSelectionAllowed(!single);
     20 		EventManager.get().attach(FlameMain.EVT_ASSET_RELOADED, this);
     21 	}
     22 
     23 	@Override
     24 	public void setValue (ParticleControllerInfluencer value) {
     25 		super.setValue(value);
     26 		if(value == null) return;
     27 		controllerPicker.setValue(value.templates);
     28 	}
     29 
     30 	protected void initializeComponents () {
     31 		super.initializeComponents();
     32 		controllerPicker = new TemplatePickerPanel<ParticleController>(editor, null, this, ParticleController.class){
     33 			@Override
     34 			protected String getTemplateName (ParticleController template, int index) {
     35 				return template.name;
     36 			}
     37 		};
     38 		reloadControllers ();
     39 		controllerPicker.setIsAlwayShown(true);
     40 
     41 
     42 		contentPanel.add(new LoaderButton.ParticleEffectLoaderButton(editor, this), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,
     43 			0, 0, 6), 0, 0));
     44 		contentPanel.add(controllerPicker, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,
     45 			0, 0, 6), 0, 0));
     46 	}
     47 
     48 	@Override
     49 	public void onTemplateChecked (ParticleController model, boolean isChecked) {
     50 		editor.restart();
     51 	}
     52 
     53 	@Override
     54 	public void onResourceLoaded (ParticleEffect resource) {
     55 		reloadControllers();
     56 	}
     57 
     58 	private void reloadControllers () {
     59 		Array<ParticleEffect> effects = new Array<ParticleEffect>();
     60 		Array<ParticleController> controllers = new Array<ParticleController>();
     61 		editor.assetManager.getAll(ParticleEffect.class, effects);
     62 		for(ParticleEffect effect : effects){
     63 			controllers.addAll(effect.getControllers());
     64 		}
     65 		controllerPicker.setLoadedTemplates(controllers);
     66 	}
     67 
     68 	@Override
     69 	public void handle (int aEventType, Object aEventData) {
     70 		if(aEventType == FlameMain.EVT_ASSET_RELOADED){
     71 			Object[] data = (Object[])aEventData;
     72 			if(data[0] instanceof ParticleEffect){
     73 				ParticleEffect oldEffect = (ParticleEffect) data[0];
     74 				int currentCount = value.templates.size;
     75 				value.templates.removeAll(oldEffect.getControllers(), true);
     76 				if(value.templates.size != currentCount){
     77 					int diff = currentCount - value.templates.size;
     78 					if(diff > 0){
     79 						ParticleEffect newEffect = (ParticleEffect) data[1];
     80 						Array<ParticleController> newControllers = newEffect.getControllers();
     81 						if(newControllers.size > 0){
     82 							for(int i=0, c=Math.min(diff, newControllers.size); i<c; ++i)
     83 								value.templates.add(newControllers.get(i));
     84 						}
     85 					}
     86 					else {
     87 						value.templates.addAll( ((ParticleEffect)editor.assetManager.get(FlameMain.DEFAULT_BILLBOARD_PARTICLE)).getControllers());
     88 					}
     89 
     90 					controllerPicker.reloadTemplates();
     91 					controllerPicker.setValue(value.templates);
     92 					editor.restart();
     93 				}
     94 			}
     95 		}
     96 	}
     97 
     98 
     99 }
    100