📄 mapeditor.java
字号:
/* * Tiled Map Editor, (c) 2004-2006 * * 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. * * Adam Turk <aturk@biggeruniverse.com> * Bjorn Lindeijer <b.lindeijer@xs4all.nl> */package tiled.mapeditor;import java.awt.*;import java.awt.event.*;import java.awt.geom.Area;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import java.util.Iterator;import java.util.Stack;import java.util.Vector;import java.util.prefs.Preferences;import java.util.prefs.PreferenceChangeEvent;import java.util.prefs.PreferenceChangeListener;import javax.imageio.ImageIO;import javax.swing.*;import javax.swing.event.*;import javax.swing.undo.UndoableEditSupport;import tiled.core.*;import tiled.io.MapHelper;import tiled.io.MapReader;import tiled.mapeditor.actions.*;import tiled.mapeditor.brush.*;import tiled.mapeditor.dialogs.*;import tiled.mapeditor.plugin.PluginClassLoader;import tiled.mapeditor.selection.SelectionLayer;import tiled.mapeditor.undo.*;import tiled.mapeditor.util.*;import tiled.mapeditor.widget.*;import tiled.util.TiledConfiguration;import tiled.view.MapView;/** * The main class for the Tiled Map Editor. * * @version $Id: MapEditor.java 689 2006-07-15 17:44:21Z bjorn $ */public class MapEditor implements ActionListener, MouseListener, MouseMotionListener, MapChangeListener, ListSelectionListener, ChangeListener, ComponentListener{ // Constants and the like private static final int PS_POINT = 0; private static final int PS_PAINT = 1; private static final int PS_ERASE = 2; private static final int PS_POUR = 3; private static final int PS_EYED = 4; private static final int PS_MARQUEE = 5; private static final int PS_MOVE = 6; private static final int PS_MOVEOBJ = 7; private static final int APP_WIDTH = 600; private static final int APP_HEIGHT = 400; private Cursor curDefault; private Cursor curPaint; private Cursor curErase; private Cursor curPour; private Cursor curEyed; private Cursor curMarquee; /** Current release version. */ public static final String version = "0.6.1"; private Map currentMap; private MapView mapView; private final UndoHandler undoHandler; private final UndoableEditSupport undoSupport; private final MapEventAdapter mapEventAdapter; private final PluginClassLoader pluginLoader; private final Preferences prefs = TiledConfiguration.root(); private int currentPointerState; private Tile currentTile; private int currentLayer = -1; private boolean bMouseIsDown, bMouseIsDragging; private SelectionLayer cursorHighlight; private Point mousePressLocation, mouseInitialPressLocation; private Point moveDist; private int mouseButton; private AbstractBrush currentBrush; private SelectionLayer marqueeSelection; private MapLayer clipboardLayer; private float relativeMidX, relativeMidY; // GUI components private JPanel mainPanel; private JPanel dataPanel; private JPanel statusBar; private JMenuBar menuBar; private JCheckBoxMenuItem gridMenuItem, boundaryMenuItem, cursorMenuItem; private JCheckBoxMenuItem coordinatesMenuItem; private JMenu recentMenu; private JScrollPane mapScrollPane; private JTable layerTable; private JList editHistoryList; private MiniMapViewer miniMap; private TileButton tilePaletteButton; private JFrame appFrame; private JSlider opacitySlider; private JLabel zoomLabel, tileCoordsLabel; private AbstractButton paintButton, eraseButton, pourButton; private AbstractButton eyedButton, marqueeButton, moveButton; private AbstractButton objectMoveButton, objectAddButton; private TilePaletteDialog tilePaletteDialog; private AboutDialog aboutDialog; private MapLayerEdit paintEdit; /** Available brushes */ private Vector brushes = new Vector(); private Brush eraserBrush; // Actions private final SaveAction saveAction; private final SaveAsAction saveAsAction; private final Action exitAction; private final Action zoomInAction, zoomOutAction, zoomNormalAction; private final Action rot90Action, rot180Action, rot270Action; private final Action flipHorAction, flipVerAction; private final Action selectAllAction, inverseAction, cancelSelectionAction; private final Action addLayerAction, cloneLayerAction, deleteLayerAction; private final Action moveLayerDownAction, moveLayerUpAction; private final Action mergeLayerDownAction, mergeAllLayersAction; private static final String TOOL_PAINT = Resources.getString("tool.paint.name"); private static final String TOOL_ERASE = Resources.getString("tool.erase.name"); private static final String TOOL_FILL = Resources.getString("tool.fill.name"); private static final String TOOL_EYE_DROPPER = Resources.getString("tool.eyedropper.name"); private static final String TOOL_SELECT = Resources.getString("tool.select.name"); private static final String TOOL_MOVE_LAYER = Resources.getString("tool.movelayer.name"); private static final String TOOL_MOVE_OBJECT = Resources.getString("tool.moveobject.name"); public MapEditor() { /* eraserBrush = new Eraser(); brushes.add(eraserBrush()); setBrush(eraserBrush); */ /* try { Image imgPaintCursor = Resources.getImage("cursor-pencil.png"); curPaint = Toolkit.getDefaultToolkit().createCustomCursor( imgPaintCursor, new Point(0,0), "paint"); } catch (Exception e) { System.out.println("Error while loading custom cursors!"); e.printStackTrace(); } */ curEyed = new Cursor(Cursor.CROSSHAIR_CURSOR); curDefault = new Cursor(Cursor.DEFAULT_CURSOR); undoHandler = new UndoHandler(this); undoSupport = new UndoableEditSupport(); undoSupport.addUndoableEditListener(undoHandler); cursorHighlight = new SelectionLayer(1, 1); cursorHighlight.select(0, 0); cursorHighlight.setVisible(prefs.getBoolean("cursorhighlight", true)); mapEventAdapter = new MapEventAdapter(); // Create the actions saveAction = new SaveAction(this); saveAsAction = new SaveAsAction(this); exitAction = new ExitAction(this, saveAction); zoomInAction = new ZoomInAction(); zoomOutAction = new ZoomOutAction(); zoomNormalAction = new ZoomNormalAction(); rot90Action = new LayerTransformAction(MapLayer.ROTATE_90); rot180Action = new LayerTransformAction(MapLayer.ROTATE_180); rot270Action = new LayerTransformAction(MapLayer.ROTATE_270); flipHorAction = new LayerTransformAction(MapLayer.MIRROR_HORIZONTAL); flipVerAction = new LayerTransformAction(MapLayer.MIRROR_VERTICAL); selectAllAction = new SelectAllAction(); cancelSelectionAction = new CancelSelectionAction(); inverseAction = new InverseSelectionAction(); addLayerAction = new AddLayerAction(this); cloneLayerAction = new CloneLayerAction(this); deleteLayerAction = new DeleteLayerAction(this); moveLayerUpAction = new MoveLayerUpAction(this); moveLayerDownAction = new MoveLayerDownAction(this); mergeLayerDownAction = new MergeLayerDownAction(this); mergeAllLayersAction = new MergeAllLayersAction(this); // Create our frame appFrame = new JFrame(Resources.getString("dialog.main.title")); appFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); appFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { exitAction.actionPerformed(null); } }); appFrame.setContentPane(createContentPane()); createMenuBar(); appFrame.setJMenuBar(menuBar); appFrame.setSize(APP_WIDTH, APP_HEIGHT); tilePaletteDialog = new TilePaletteDialog(this, currentMap); setCurrentMap(null); updateRecent(null); appFrame.setVisible(true); // Restore the state of the main frame. This needs to happen after // making the frame visible, otherwise it has no effect (in Linux). Preferences mainDialogPrefs = prefs.node("dialog/main"); int state = mainDialogPrefs.getInt("state", Frame.NORMAL); if (state != Frame.ICONIFIED) { appFrame.setExtendedState(state); } // Load plugins pluginLoader = PluginClassLoader.getInstance(); try { pluginLoader.readPlugins(null, appFrame); } catch (Exception e) { e.printStackTrace(); JOptionPane.showMessageDialog(appFrame, e.toString(), "Plugin loader", JOptionPane.WARNING_MESSAGE); } MapHelper.init(pluginLoader); // Make sure the map view is redrawn when grid preferences change. // todo: move this functionality out of here somehow, but not back into MapView final Preferences display = prefs.node("display"); display.addPreferenceChangeListener(new PreferenceChangeListener() { public void preferenceChange(PreferenceChangeEvent event) { if (mapView == null) return; String key = event.getKey(); if ("gridOpacity".equals(key)) { mapView.setGridOpacity(display.getInt("gridOpacity", 255)); } else if ("gridAntialias".equals(key)) { mapView.setAntialiasGrid(display.getBoolean("gridAntialias", true)); } else if ("gridColor".equals(key)) { mapView.setGridColor(new Color(display.getInt("gridColor", MapView.DEFAULT_GRID_COLOR.getRGB()))); } else if ("showGrid".equals(key)) { mapView.setShowGrid(display.getBoolean("showGrid", false)); } } }); } private JPanel createContentPane() { mapScrollPane = new JScrollPane( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); mapScrollPane.setBorder(null); createData(); createStatusBar(); JSplitPane mainSplit = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, false, mapScrollPane, dataPanel); mainSplit.setResizeWeight(1.0); mainPanel = new JPanel(new BorderLayout()); mainPanel.add(createToolBar(), BorderLayout.WEST); mainPanel.add(mainSplit); mainPanel.add(statusBar, BorderLayout.SOUTH); return mainPanel; } /** * Creates all the menus and submenus of the top menu bar. Handles * assigning listeners and tooltips as well. */ private void createMenuBar() { JMenuItem save = new TMenuItem(saveAction); JMenuItem saveAs = new TMenuItem(saveAsAction); JMenuItem saveAsImage = createMenuItem(Resources.getString("menu.file.image"), null, Resources.getString("menu.file.image.tooltip"), "control shift I"); JMenuItem close = new TMenuItem(new CloseMapAction(this, saveAction)); recentMenu = new JMenu(Resources.getString("menu.file.recent")); mapEventAdapter.addListener(save); mapEventAdapter.addListener(saveAs); mapEventAdapter.addListener(saveAsImage); mapEventAdapter.addListener(close); JMenu fileMenu = new JMenu(Resources.getString("menu.file")); fileMenu.add(new TMenuItem(new NewMapAction(this, saveAction))); fileMenu.add(new TMenuItem(new OpenMapAction(this, saveAction))); fileMenu.add(recentMenu); fileMenu.add(save); fileMenu.add(saveAs); fileMenu.add(saveAsImage); fileMenu.addSeparator(); fileMenu.add(close); fileMenu.add(new TMenuItem(exitAction)); JMenuItem copyMenuItem = new TMenuItem(new CopyAction()); JMenuItem cutMenuItem = new TMenuItem(new CutAction()); JMenuItem pasteMenuItem = new TMenuItem(new PasteAction()); copyMenuItem.setEnabled(false); cutMenuItem.setEnabled(false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -