1 package com.badlogic.gdx.tools.flame; 2 3 import java.awt.Component; 4 import java.awt.GridBagConstraints; 5 import java.awt.GridBagLayout; 6 import java.awt.Insets; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 10 import javax.swing.JButton; 11 import javax.swing.JPanel; 12 13 import com.badlogic.gdx.graphics.Texture; 14 import com.badlogic.gdx.graphics.g2d.TextureAtlas; 15 import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion; 16 import com.badlogic.gdx.graphics.g2d.TextureRegion; 17 import com.badlogic.gdx.graphics.glutils.FileTextureData; 18 import com.badlogic.gdx.utils.Array; 19 20 /** @author Inferno */ 21 public class TextureAtlasPanel extends JPanel { 22 JPanel regionsPanel; 23 TextureAtlas atlas; 24 25 public TextureAtlasPanel(){ 26 initializeComponents(); 27 } 28 29 private void initializeComponents () { 30 setLayout(new GridBagLayout()); 31 JButton backwardButton, forwardButton; 32 33 add(backwardButton = new JButton("<"), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 34 new Insets(0, 0, 0, 0), 0, 0)); 35 add(regionsPanel = new JPanel(), new GridBagConstraints(1, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 36 new Insets(0, 0, 0, 0), 0, 0)); 37 add(forwardButton = new JButton(">"), new GridBagConstraints(2, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, 38 new Insets(0, 0, 0, 0), 0, 0)); 39 40 regionsPanel.setLayout(new CustomCardLayout()); 41 42 backwardButton.addActionListener(new ActionListener() { 43 @Override 44 public void actionPerformed (ActionEvent arg0) { 45 if(atlas == null) return; 46 CustomCardLayout layout = (CustomCardLayout)regionsPanel.getLayout(); 47 layout.previous(regionsPanel); 48 } 49 }); 50 51 forwardButton.addActionListener(new ActionListener() { 52 @Override 53 public void actionPerformed (ActionEvent arg0) { 54 if(atlas == null) return; 55 CustomCardLayout layout = (CustomCardLayout)regionsPanel.getLayout(); 56 layout.next(regionsPanel); 57 } 58 }); 59 } 60 61 public void setAtlas(TextureAtlas atlas){ 62 if(atlas == this.atlas) return; 63 regionsPanel.removeAll(); 64 Array<AtlasRegion> atlasRegions = atlas.getRegions(); 65 CustomCardLayout layout = (CustomCardLayout)regionsPanel.getLayout(); 66 Array<TextureRegion> regions = new Array<TextureRegion>(); 67 for(Texture texture : atlas.getTextures()){ 68 FileTextureData file = (FileTextureData)texture.getTextureData(); 69 regionsPanel.add(new TexturePanel( texture, getRegions(texture, atlasRegions, regions))); 70 } 71 layout.first(regionsPanel); 72 this.atlas = atlas; 73 } 74 75 protected Array<TextureRegion> getRegions (Texture texture, Array<AtlasRegion> atlasRegions, Array<TextureRegion> out) { 76 out.clear(); 77 for(TextureRegion region : atlasRegions){ 78 if(region.getTexture() == texture) 79 out.add(region); 80 } 81 return out; 82 } 83 84 public Array<TextureRegion> getSelectedRegions () { 85 CustomCardLayout layout = (CustomCardLayout)regionsPanel.getLayout(); 86 TexturePanel panel = getCurrentRegionPanel(); 87 return panel.selectedRegions; 88 } 89 90 public TexturePanel getCurrentRegionPanel(){ 91 CustomCardLayout layout = (CustomCardLayout)regionsPanel.getLayout(); 92 return layout.getCurrentCard(regionsPanel); 93 } 94 95 public void clearSelection () { 96 for(Component regionPanel : regionsPanel.getComponents()) 97 ((TexturePanel)regionPanel).clearSelection(); 98 } 99 100 } 101