package tk.amberide.ide.gui.editor.map.res; import tk.amberide.Amber; import tk.amberide.ide.data.res.Resource; import tk.amberide.ide.data.res.Tileset; import tk.amberide.ide.data.res.Tileset.TileSprite; import tk.amberide.ide.gui.editor.map.MapContext; import javax.swing.*; import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.HashMap; import java.util.Map; /** * @author Tudor */ public class TileSelector extends javax.swing.JPanel { Map<String, TileSheetRenderer> renderPanels = new HashMap<String, TileSheetRenderer>(); protected Tileset.TileSprite[][] selection; protected MapContext context; /** * Creates new form TileSelector */ public TileSelector(MapContext context) { initComponents(); this.context = context; tilesheetsComboBox.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { setSelected((String) e.getItem()); } }); tileScroller.getHorizontalScrollBar().setUnitIncrement(16); tileScroller.getVerticalScrollBar().setUnitIncrement(16); } /** * Synchronizes this TileSelector with the imported TileSheets */ public void synchronize() { ((DefaultComboBoxModel) tilesheetsComboBox.getModel()).removeAllElements(); for (Resource<Tileset> set : Amber.getResourceManager().getTilesets()) { addTileset(set.getName(), set.get()); } } /** * Loads the selector for the given TileSheet identifier * * @param name the identifier for the sheet to be loaded */ public void setSelected(String name) { tileScroller.getViewport().removeAll(); TileSheetRenderer renderer = renderPanels.get(name); if (renderer != null) { System.out.println("Adding renderer"); JPanel content = new JPanel(new BorderLayout()); content.add(renderer); tileScroller.getViewport().add(content); // Wrap in JPanel so we can use our own sizes } else { System.out.println("Renderer is null!"); } } /** * Imports a TileSheet under a given identifier * * @param name the identifier * @param tiles the sheet to add */ public void addTileset(String name, Tileset tiles) { tilesheetsComboBox.addItem(name); renderPanels.put(name, new TileSheetRenderer(tiles, this)); if (renderPanels.size() == 1) { setSelected(name); } } /** * Removes a TileSheet by its identifier * * @param name the identifier */ public void removeTileset(String name) { tilesheetsComboBox.removeItem(name); renderPanels.remove(name); } /** * Returns a Tile[][] representation of the current tile selection * * @return the selection */ public TileSprite[][] getSelection() { return selection; } /** * Sets the current selection * * @param selection the selection to set */ public void setSelection(TileSprite[][] selection) { context.tileSelection = this.selection = selection; } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; tilesheetsComboBox = new javax.swing.JComboBox(); tileScroller = new javax.swing.JScrollPane(); addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { formFocusGained(evt); } }); setLayout(new java.awt.GridBagLayout()); tilesheetsComboBox.setModel(new DefaultComboBoxModel()); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.ipadx = 307; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; add(tilesheetsComboBox, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.ipadx = 312; gridBagConstraints.ipady = 251; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.weightx = 1.0; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0); add(tileScroller, gridBagConstraints); }// </editor-fold>//GEN-END:initComponents private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained if (Amber.getResourceManager().getTilesets().size() != tilesheetsComboBox.getModel().getSize()) { synchronize(); } }//GEN-LAST:event_formFocusGained // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JScrollPane tileScroller; javax.swing.JComboBox tilesheetsComboBox; // End of variables declaration//GEN-END:variables }