• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.badlogic.gdx.tools.flame;
2 
3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 
9 import javax.swing.DefaultComboBoxModel;
10 import javax.swing.JButton;
11 import javax.swing.JComboBox;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.JSeparator;
15 
16 import com.badlogic.gdx.graphics.Texture;
17 import com.badlogic.gdx.graphics.g2d.TextureAtlas;
18 import com.badlogic.gdx.graphics.g2d.TextureRegion;
19 import com.badlogic.gdx.utils.Array;
20 
21 /** @author Inferno */
22 public class RegionPickerPanel extends JPanel{
23 
24 	private enum GenerationMode {
25 		ByRows("Generate By Rows"), ByColumns("Generate By Columns");
26 		String string;
GenerationMode(String string)27 		private GenerationMode(String string){
28 			this.string = string;
29 		}
30 
31 		@Override
toString()32 		public String toString () {
33 			return string;
34 		}
35 	}
36 
37 	public interface Listener{
onRegionsSelected(Array<TextureRegion> regions)38 		void onRegionsSelected(Array<TextureRegion> regions);
39 	}
40 
41 	TextureAtlasPanel atlasPanel;
42 	TexturePanel texturePanel;
43 	JButton selectButton, selectAllButton, clearButton, generateButton, reverseButton;
44 	JComboBox generateBox;
45 	Slider rowSlider, columnSlider;
46 	JPanel generationPanel, content;
47 	Listener listener;
48 
RegionPickerPanel(Listener listener)49 	public RegionPickerPanel(Listener listener){
50 		initializeComponents();
51 		this.listener = listener;
52 	}
53 
initializeComponents()54 	private void initializeComponents () {
55 		setLayout(new GridBagLayout());
56 		content = new JPanel();
57 		atlasPanel = new TextureAtlasPanel();
58 		texturePanel = new TexturePanel();
59 		CustomCardLayout cardLayout = new CustomCardLayout();
60 		content.setLayout(cardLayout);
61 		content.add(atlasPanel, "atlas");
62 		content.add(texturePanel, "texture");
63 
64 
65 		add(content, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE,
66 			new Insets(0, 0, 0, 0), 0, 0));
67 
68 		JPanel controls = new JPanel(new GridBagLayout());
69 		controls.add(selectButton = new JButton("Select"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
70 			new Insets(0, 0, 0, 0), 0, 0));
71 
72 		controls.add(new JSeparator(JSeparator.HORIZONTAL), new GridBagConstraints(0, -1, 1, 1, 0, 0,
73 			GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 0));
74 
75 		//Pick
76 		JPanel pickPanel = new JPanel(new GridBagLayout());
77 		pickPanel.add(selectAllButton = new JButton("Pick All"), new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
78 			new Insets(0, 0, 0, 0), 0, 0));
79 		pickPanel.add(clearButton = new JButton("Clear Selection"), new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
80 			new Insets(0, 0, 0, 0), 0, 0));
81 		pickPanel.add(reverseButton = new JButton("Reverse Selection"), new GridBagConstraints(0, 1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
82 			new Insets(0, 0, 0, 0), 0, 0));
83 		controls.add(pickPanel, new GridBagConstraints(0, -1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
84 			new Insets(0, 0, 0, 0), 0, 0));
85 
86 		//Generation
87 		generationPanel = new JPanel(new GridBagLayout());
88 		generationPanel.add(new JLabel("Rows"), new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
89 			new Insets(0, 0, 0, 0), 0, 0));
90 		generationPanel.add(rowSlider = new Slider(1, 1, 9999, 1), new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
91 			new Insets(0, 0, 0, 0), 0, 0));
92 		generationPanel.add(new JLabel("Columns"), new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
93 			new Insets(0, 0, 0, 0), 0, 0));
94 		generationPanel.add(columnSlider = new Slider(1, 1, 9999, 1), new GridBagConstraints(1, 1, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
95 			new Insets(0, 0, 0, 0), 0, 0));
96 		generationPanel.add(generateBox = new JComboBox(new DefaultComboBoxModel(GenerationMode.values())), new GridBagConstraints(0, 2, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
97 			new Insets(0, 0, 0, 0), 0, 0));
98 		generationPanel.add(generateButton = new JButton("Generate"), new GridBagConstraints(1, 2, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
99 			new Insets(0, 0, 0, 0), 0, 0));
100 		controls.add(new JSeparator(JSeparator.HORIZONTAL), new GridBagConstraints(0, -1, 1, 1, 0, 0,
101 			GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 6, 0), 0, 0));
102 		controls.add(generationPanel, new GridBagConstraints(0, -1, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
103 			new Insets(0, 0, 0, 0), 0, 0));
104 		add(controls, new GridBagConstraints(1, 0, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
105 			new Insets(0, 0, 0, 0), 0, 0));
106 
107 		selectButton.addActionListener(new ActionListener() {
108 			@Override
109 			public void actionPerformed (ActionEvent arg0) {
110 				JPanel panel = ((CustomCardLayout)content.getLayout()).getCurrentCard(content);
111 				TexturePanel currentTexturePanel = panel == atlasPanel ? atlasPanel.getCurrentRegionPanel() : texturePanel;
112 				listener.onRegionsSelected(currentTexturePanel.selectedRegions);
113 			}
114 		});
115 
116 		selectAllButton.addActionListener(new ActionListener() {
117 			@Override
118 			public void actionPerformed (ActionEvent arg0) {
119 				JPanel panel = ((CustomCardLayout)content.getLayout()).getCurrentCard(content);
120 				TexturePanel currentTexturePanel = panel == atlasPanel ? atlasPanel.getCurrentRegionPanel() : texturePanel;
121 				currentTexturePanel.selectAll();
122 			}
123 		});
124 
125 		reverseButton.addActionListener(new ActionListener() {
126 			@Override
127 			public void actionPerformed (ActionEvent arg0) {
128 				JPanel panel = ((CustomCardLayout)content.getLayout()).getCurrentCard(content);
129 				TexturePanel currentTexturePanel = panel == atlasPanel ? atlasPanel.getCurrentRegionPanel() : texturePanel;
130 				currentTexturePanel.selectedRegions.reverse();
131 				currentTexturePanel.revalidate();
132 				currentTexturePanel.repaint();
133 			}
134 		});
135 
136 		clearButton.addActionListener(new ActionListener() {
137 			@Override
138 			public void actionPerformed (ActionEvent arg0) {
139 				JPanel panel = ((CustomCardLayout)content.getLayout()).getCurrentCard(content);
140 				TexturePanel currentPanel = panel == atlasPanel ? atlasPanel.getCurrentRegionPanel() : texturePanel;
141 				currentPanel.clearSelection();
142 			}
143 		});
144 
145 		generateButton.addActionListener(new ActionListener() {
146 			@Override
147 			public void actionPerformed (ActionEvent arg0) {
148 				generateRegions((GenerationMode)generateBox.getSelectedItem());
149 				texturePanel.revalidate();
150 				texturePanel.repaint();
151 			}
152 		});
153 
154 	}
155 
generateRegions(GenerationMode mode)156 	void generateRegions(GenerationMode mode){
157 		//generate regions
158 		texturePanel.clear();
159 		Texture texture = texturePanel.getTexture();
160 		int 	rows = (int)rowSlider.getValue(), columns = (int)columnSlider.getValue(),
161 				yOffset = texture.getHeight()/rows, xOffset = texture.getWidth()/columns;
162 
163 		if(mode == GenerationMode.ByRows){
164 			for(int j=0; j < rows; ++j){
165 				int rowOffset = j*yOffset;
166 				for(int i=0; i < columns; ++i){
167 					texturePanel.unselectedRegions.add(new TextureRegion(texture, i*xOffset, rowOffset, xOffset, yOffset) );
168 				}
169 			}
170 		}
171 		else 	if(mode == GenerationMode.ByColumns){
172 			for(int i=0; i < columns; ++i){
173 				int columnOffset = i*xOffset;
174 				for(int j=0; j <rows; ++j){
175 					texturePanel.unselectedRegions.add(new TextureRegion(texture, columnOffset, j*yOffset, xOffset, yOffset) );
176 				}
177 			}
178 		}
179 	}
180 
setAtlas(TextureAtlas atlas)181 	public void setAtlas (TextureAtlas atlas) {
182 		atlasPanel.clearSelection();
183 		atlasPanel.setAtlas(atlas);
184 		CustomCardLayout cardLayout = (CustomCardLayout)content.getLayout();
185 		cardLayout.show(content, "atlas");
186 		showGenerationPanel(false);
187 		content.revalidate();
188 		content.repaint();
189 		revalidate();
190 		repaint();
191 	}
192 
setTexture(Texture texture)193 	public void setTexture(Texture texture){
194 		texturePanel.clearSelection();
195 		texturePanel.setTexture(texture);
196 		CustomCardLayout cardLayout = (CustomCardLayout)content.getLayout();
197 		cardLayout.show(content, "texture");
198 		showGenerationPanel(true);
199 		content.revalidate();
200 		content.repaint();
201 		revalidate();
202 		repaint();
203 	}
204 
showGenerationPanel(boolean isShown)205 	private void showGenerationPanel(boolean isShown){
206 		generationPanel.setVisible(isShown);
207 	}
208 
209 }
210