📄 tilesetmanager.java
字号:
/* * MegaMek - Copyright (C) 2002,2003,2004 Ben Mazur (bmazur@sev.org) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. *//* * TilesetManager.java * * Created on April 15, 2002, 11:41 PM */package megamek.client.ui.swing;import megamek.client.ui.swing.util.ImageFileFactory;import megamek.client.ui.swing.util.PlayerColors;import megamek.client.ui.swing.util.RotateFilter;import megamek.common.Entity;import megamek.common.IBoard;import megamek.common.IGame;import megamek.common.IHex;import megamek.common.Infantry;import megamek.common.Mech;import megamek.common.Minefield;import megamek.common.Player;import megamek.common.Protomech;import megamek.common.preference.IClientPreferences;import megamek.common.preference.IPreferenceChangeListener;import megamek.common.preference.PreferenceChangeEvent;import megamek.common.preference.PreferenceManager;import megamek.common.util.DirectoryItems;import javax.swing.ImageIcon;import javax.swing.JComponent;import javax.swing.JLabel;import java.awt.Image;import java.awt.MediaTracker;import java.awt.image.FilteredImageSource;import java.awt.image.ImageObserver;import java.awt.image.ImageProducer;import java.awt.image.MemoryImageSource;import java.awt.image.PixelGrabber;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashMap;import java.util.Iterator;import java.util.List;/** * Handles loading and manipulating images from both the mech tileset and the * terrain tileset. * * @author Ben */public class TilesetManager implements IPreferenceChangeListener { // component to load images to private JComponent comp; // keep tracking of loading images private MediaTracker tracker; private boolean started = false; private boolean loaded = false; // keep track of camo images private DirectoryItems camos; // mech images private MechTileset mechTileset = new MechTileset("data/images/units/"); //$NON-NLS-1$ private MechTileset wreckTileset = new MechTileset("data/images/units/wrecks/"); //$NON-NLS-1$ private ArrayList mechImageList = new ArrayList(); private HashMap mechImages = new HashMap(); // hex images private HexTileset hexTileset = new HexTileset(); private Image minefieldSign; private Image nightFog; private Image artilleryAutohit; private Image artilleryAdjusted; private Image artilleryIncoming; private HashMap<Integer, Image> ecmShades = new HashMap<Integer, Image>(); private static final String NIGHT_IMAGE_FILE = "data/images/hexes/transparent/night.png"; private static final String ARTILLERY_AUTOHIT_IMAGE_FILE = "data/images/hexes/artyauto.gif"; private static final String ARTILLERY_ADJUSTED_IMAGE_FILE = "data/images/hexes/artyadj.gif"; private static final String ARTILLERY_INCOMING_IMAGE_FILE = "data/images/hexes/artyinc.gif"; public static final int ARTILLERY_AUTOHIT = 0; public static final int ARTILLERY_ADJUSTED = 1; public static final int ARTILLERY_INCOMING = 2; /** * Creates new TilesetManager */ public TilesetManager(JComponent comp) throws IOException { this.comp = comp; tracker = new MediaTracker(comp); try { camos = new DirectoryItems(new File("data/images/camo"), "", //$NON-NLS-1$ //$NON-NLS-2$ ImageFileFactory.getInstance()); } catch (Exception e) { camos = null; } mechTileset.loadFromFile("mechset.txt"); //$NON-NLS-1$ wreckTileset.loadFromFile("wreckset.txt"); //$NON-NLS-1$ hexTileset.loadFromFile(PreferenceManager.getClientPreferences().getMapTileset()); PreferenceManager.getClientPreferences().addPreferenceChangeListener(this); } public void preferenceChange(PreferenceChangeEvent e) { if (e.getName().equals(IClientPreferences.MAP_TILESET)) { HexTileset hts = new HexTileset(); try { hts.loadFromFile((String) e.getNewValue()); hexTileset = hts; } catch (IOException ex) { return; } } } public Image iconFor(Entity entity) { EntityImage entityImage = (EntityImage) mechImages.get(new Integer(entity.getId())); if (entityImage == null) { // probably double_blind. Try to load on the fly System.out.println("Loading image for " + entity.getShortNameRaw() + " on the fly."); //$NON-NLS-1$ //$NON-NLS-2$ loadImage(entity); entityImage = (EntityImage) mechImages.get(new Integer(entity.getId())); if (entityImage == null) { // now it's a real problem System.out.println("Unable to load image for entity: " + entity.getShortNameRaw()); //$NON-NLS-1$ } } return entityImage.getIcon(); } public Image wreckMarkerFor(Entity entity) { EntityImage entityImage = (EntityImage) mechImages.get(new Integer(entity.getId())); if (entityImage == null) { // probably double_blind. Try to load on the fly System.out.println("Loading image for " + entity.getShortNameRaw() + " on the fly."); //$NON-NLS-1$ //$NON-NLS-2$ loadImage(entity); entityImage = (EntityImage) mechImages.get(new Integer(entity.getId())); if (entityImage == null) { // now it's a real problem System.out.println("Unable to load image for entity: " + entity.getShortNameRaw()); //$NON-NLS-1$ return null; } } return entityImage.getWreckFacing(entity.getFacing()); } /** * Return the image for the entity */ public Image imageFor(Entity entity) { // mechs look like they're facing their secondary facing if (entity instanceof Mech || entity instanceof Protomech) { return imageFor(entity, entity.getSecondaryFacing()); } return imageFor(entity, entity.getFacing()); } public Image imageFor(Entity entity, int facing) { EntityImage entityImage = (EntityImage) mechImages.get(new Integer(entity.getId())); if (entityImage == null) { // probably double_blind. Try to load on the fly System.out.println("Loading image for " + entity.getShortNameRaw() + " on the fly."); //$NON-NLS-1$ //$NON-NLS-2$ loadImage(entity); entityImage = (EntityImage) mechImages.get(new Integer(entity.getId())); if (entityImage == null) { // now it's a real problem System.out.println("Unable to load image for entity: " + entity.getShortNameRaw()); //$NON-NLS-1$ } } // get image rotated for facing return entityImage.getFacing(facing); } /** * Return the base image for the hex */ public Image baseFor(IHex hex) { return hexTileset.getBase(hex, comp); } /** * Return a list of superimposed images for the hex */ public List supersFor(IHex hex) { return hexTileset.getSupers(hex, comp); } public Image getMinefieldSign() { return minefieldSign; } public Image getNightFog() { return nightFog; } public Image getEcmShade(int tint) { Image image = ecmShades.get(new Integer(tint)); if (image == null) { Image iMech; iMech = nightFog; int[] pMech = new int[EntityImage.IMG_SIZE]; PixelGrabber pgMech = new PixelGrabber(iMech, 0, 0, EntityImage.IMG_WIDTH, EntityImage.IMG_HEIGHT, pMech, 0, EntityImage.IMG_WIDTH); try { pgMech.grabPixels(); } catch (InterruptedException e) { System.err.println("EntityImage.applyColor(): Failed to grab pixels for mech image." + e.getMessage()); //$NON-NLS-1$ return image; } if ((pgMech.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("EntityImage.applyColor(): Failed to grab pixels for mech image. ImageObserver aborted."); //$NON-NLS-1$ return image; } for (int i = 0; i < EntityImage.IMG_SIZE; i++) { int pixel = pMech[i]; int alpha = (pixel >> 24) & 0xff; if (alpha != 0) { int pixel1 = tint & 0xffffff; pMech[i] = (alpha << 24) | pixel1; } } image = comp.createImage(new MemoryImageSource(EntityImage.IMG_WIDTH, EntityImage.IMG_HEIGHT, pMech, 0, EntityImage.IMG_WIDTH)); ecmShades.put(new Integer(tint), image); } return image; } public Image getArtilleryTarget(int which) { switch (which) { case ARTILLERY_AUTOHIT: return artilleryAutohit; case ARTILLERY_ADJUSTED: return artilleryAdjusted; case ARTILLERY_INCOMING: default: return artilleryIncoming; } } /** * @return true if we're in the process of loading some images */ public boolean isStarted() { return started; } /** * @return true if we're done loading images */ public synchronized boolean isLoaded() { if (!loaded) { loaded = tracker.checkAll(true); } return started && loaded; } /** * Load all the images we'll need for the game and place them in the tracker */ public void loadNeededImages(IGame game) { loaded = false; IBoard board = game.getBoard(); // pre-match all hexes with images, load hex images for (int y = 0; y < board.getHeight(); y++) { for (int x = 0; x < board.getWidth(); x++) { IHex hex = board.getHex(x, y); loadHexImage(hex); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -