1 package com.badlogic.gdx.tools.flame; 2 3 import java.awt.Color; 4 import java.awt.Graphics; 5 import java.awt.Rectangle; 6 import java.awt.event.MouseAdapter; 7 import java.awt.event.MouseEvent; 8 9 import com.badlogic.gdx.graphics.Texture; 10 import com.badlogic.gdx.graphics.g2d.TextureRegion; 11 import com.badlogic.gdx.graphics.glutils.FileTextureData; 12 import com.badlogic.gdx.utils.Array; 13 14 /** @author Inferno */ 15 public class TexturePanel extends ImagePanel { 16 private Color selectedColor = Color.GREEN, 17 unselectedColor = Color.BLUE, 18 indexBackgroundColor = Color.BLACK, 19 indexColor = Color.WHITE; 20 Array<TextureRegion> selectedRegions; 21 Array<TextureRegion> unselectedRegions; 22 Texture texture; 23 24 public TexturePanel(){ 25 selectedRegions = new Array<TextureRegion>(); 26 unselectedRegions = new Array<TextureRegion>(); 27 28 addMouseListener(new MouseAdapter() { 29 public void mouseClicked (MouseEvent event) { 30 float x = event.getX(), y = event.getY(); 31 for(TextureRegion region : unselectedRegions){ 32 if(isInsideRegion(region, x, y)){ 33 select(region); 34 return; 35 } 36 } 37 38 for(TextureRegion region : selectedRegions){ 39 if(isInsideRegion(region, x, y)){ 40 unselect(region); 41 return; 42 } 43 } 44 } 45 }); 46 } 47 48 protected boolean isInsideRegion (TextureRegion region, float x, float y) { 49 float rx = region.getRegionX(), ry = region.getRegionY(); 50 return rx <= x && x <= rx +region.getRegionWidth() && 51 ry <= y && y <= ry +region.getRegionHeight(); 52 } 53 54 public TexturePanel(Texture texture, Array<TextureRegion> regions){ 55 this(); 56 setTexture(texture); 57 setRegions(regions); 58 } 59 60 public void setTexture(Texture texture){ 61 if(this.texture == texture) return; 62 this.texture = texture; 63 FileTextureData data = (FileTextureData)texture.getTextureData(); 64 setImage(data.getFileHandle().file().getAbsolutePath()); 65 } 66 67 public Texture getTexture(){ 68 return texture; 69 } 70 71 public void clear(){ 72 selectedRegions.clear(); 73 unselectedRegions.clear(); 74 } 75 76 public void clearSelection(){ 77 unselectedRegions.addAll(selectedRegions); 78 selectedRegions.clear(); 79 repaint(); 80 } 81 82 public void setRegions(Array<TextureRegion> regions){ 83 unselectedRegions.clear(); 84 selectedRegions.clear(); 85 unselectedRegions.addAll(regions); 86 } 87 88 private void swap(TextureRegion region, Array<TextureRegion> src, Array<TextureRegion> dst) 89 { 90 int index = src.indexOf(region, true); 91 if(index > -1){ 92 src.removeIndex(index); 93 dst.add(region); 94 repaint(); 95 } 96 } 97 98 public void select(TextureRegion region){ 99 swap(region, unselectedRegions, selectedRegions); 100 } 101 102 public void unselect(TextureRegion region){ 103 swap(region, selectedRegions, unselectedRegions); 104 } 105 106 public void selectAll () { 107 selectedRegions.addAll(unselectedRegions); 108 unselectedRegions.clear(); 109 repaint(); 110 } 111 112 113 @Override 114 protected void paintComponent (Graphics g) { 115 super.paintComponent(g); 116 draw(g, unselectedRegions, unselectedColor, false); 117 draw(g, selectedRegions, selectedColor, true); 118 } 119 120 private void draw (Graphics g, Array<TextureRegion> regions, Color color, boolean drawIndex) { 121 int i=0; 122 for(TextureRegion region : regions){ 123 int x = region.getRegionX(), y = region.getRegionY(), 124 h = region.getRegionHeight(); 125 if(drawIndex){ 126 String indexString = ""+i; 127 Rectangle bounds = g.getFontMetrics().getStringBounds(indexString, g).getBounds(); 128 g.setColor(indexBackgroundColor); 129 g.fillRect(x, y+h-bounds.height, bounds.width, bounds.height); 130 g.setColor(indexColor); 131 g.drawString(indexString, x, y+h); 132 ++i; 133 } 134 g.setColor(color); 135 g.drawRect(x, y, region.getRegionWidth(), h); 136 } 137 } 138 } 139