Fixed formatting, more IDEA removal.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -7,6 +7,7 @@ target/
|
||||
|
||||
bin/
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
# User-specific stuff:
|
||||
.idea/**/workspace.xml
|
||||
|
||||
2
pom.xml
2
pom.xml
@@ -1,4 +1,4 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.aargonian.editor</groupId>
|
||||
|
||||
@@ -14,8 +14,7 @@ import java.util.List;
|
||||
* keep each tile instance as a subimage within a larger sheet, unless written out by a TileSheetWriter. Instead,
|
||||
* individual tile images can be added or removed from the set at will.
|
||||
*/
|
||||
public class EditableTileSheet
|
||||
{
|
||||
public class EditableTileSheet {
|
||||
/*
|
||||
* An EditableTileSheet is the backing model for the TileSheets dock of the editor, and as such implements the
|
||||
* Observer
|
||||
@@ -26,13 +25,11 @@ public class EditableTileSheet
|
||||
/**
|
||||
* Creates an empty EditableTileSheet.
|
||||
*/
|
||||
public EditableTileSheet()
|
||||
{
|
||||
public EditableTileSheet() {
|
||||
this.tileImages = new ArrayList<>();
|
||||
}
|
||||
|
||||
public EditableTileSheet(List<Image> tileImages)
|
||||
{
|
||||
public EditableTileSheet(List<Image> tileImages) {
|
||||
this.tileImages = tileImages;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,71 +10,57 @@ import java.awt.event.*;
|
||||
* model, and receives input events from the TileMapDisplay. It is the job of the EditorTool to translate these input
|
||||
* events into corresponding actions on the underlying TileMap or in the TileMapDisplay.
|
||||
*/
|
||||
public class EditorTool implements MouseListener, MouseMotionListener, KeyListener
|
||||
{
|
||||
public class EditorTool implements MouseListener, MouseMotionListener, KeyListener {
|
||||
private final TileMapDisplay display;
|
||||
|
||||
protected EditorTool(TileMapDisplay display)
|
||||
{
|
||||
if(display == null)
|
||||
{
|
||||
protected EditorTool(TileMapDisplay display) {
|
||||
if (display == null) {
|
||||
throw new NullPointerException("Given TileMapDisplay was Null!");
|
||||
}
|
||||
this.display = display;
|
||||
}
|
||||
|
||||
protected TileMapDisplay getCurrentDisplay()
|
||||
{
|
||||
protected TileMapDisplay getCurrentDisplay() {
|
||||
return this.display;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e)
|
||||
{
|
||||
public void keyTyped(KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e)
|
||||
{
|
||||
public void keyPressed(KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e)
|
||||
{
|
||||
public void keyReleased(KeyEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e)
|
||||
{
|
||||
public void mousePressed(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e)
|
||||
{
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e)
|
||||
{
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e)
|
||||
{
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e)
|
||||
{
|
||||
public void mouseExited(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e)
|
||||
{
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e)
|
||||
{
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,28 +11,22 @@ import java.util.ArrayList;
|
||||
/**
|
||||
* Created by aargonian on 7/8/17.
|
||||
*/
|
||||
public class GameFrame
|
||||
{
|
||||
public class GameFrame {
|
||||
private static final String PROG_TITLE = "TileEditor 0.1.0";
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
public static void main(String[] args) {
|
||||
EventQueue.invokeLater(GameFrame::setupUI);
|
||||
}
|
||||
|
||||
//TODO: Add an Icon Image
|
||||
private static JFrame setupFrame()
|
||||
{
|
||||
private static JFrame setupFrame() {
|
||||
JFrame frame = new JFrame(PROG_TITLE);
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
if(screenSize.width <= 1024 || screenSize.height <= 800)
|
||||
{
|
||||
if (screenSize.width <= 1024 || screenSize.height <= 800) {
|
||||
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //maximize by default if the screen is really small
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
frame.setSize((int) Math.round(screenSize.width * 0.8), (int) Math.round(screenSize.height * 0.8));
|
||||
frame.setLocationRelativeTo(null); //Centers the frame
|
||||
}
|
||||
@@ -44,19 +38,16 @@ public class GameFrame
|
||||
*
|
||||
* @return The Tile Pane Component, Fully Constructed
|
||||
*/
|
||||
private static JComponent setupTilesPanel()
|
||||
{
|
||||
private static JComponent setupTilesPanel() {
|
||||
throw new UnsupportedOperationException("Not Supported Yet.");
|
||||
}
|
||||
|
||||
private static void createTools(TileMapDisplay tileMapDisplay, TilesetDisplay tilesetDisplay)
|
||||
{
|
||||
private static void createTools(TileMapDisplay tileMapDisplay, TilesetDisplay tilesetDisplay) {
|
||||
PencilTool pencil = new PencilTool(tileMapDisplay, tilesetDisplay);
|
||||
tileMapDisplay.setCurrentActiveTool(pencil);
|
||||
}
|
||||
|
||||
private static void setupUI()
|
||||
{
|
||||
private static void setupUI() {
|
||||
JFrame frame = setupFrame();
|
||||
|
||||
TileMap map = new TileMap(10, 10);
|
||||
|
||||
@@ -8,27 +8,22 @@ import java.awt.event.MouseEvent;
|
||||
/**
|
||||
* Created by aargonian on 7/23/17.
|
||||
*/
|
||||
public class PencilTool extends EditorTool implements TilesetDisplay.TilesetListener
|
||||
{
|
||||
public class PencilTool extends EditorTool implements TilesetDisplay.TilesetListener {
|
||||
private ImageResource currentImage;
|
||||
|
||||
public PencilTool(TileMapDisplay display, TilesetDisplay tilesetDisplay)
|
||||
{
|
||||
public PencilTool(TileMapDisplay display, TilesetDisplay tilesetDisplay) {
|
||||
super(display);
|
||||
tilesetDisplay.addSetListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void imageSelected(ImageResource resource)
|
||||
{
|
||||
public void imageSelected(ImageResource resource) {
|
||||
this.currentImage = resource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e)
|
||||
{
|
||||
if(this.currentImage != null)
|
||||
{
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if (this.currentImage != null) {
|
||||
int tileX = e.getX() / super.getCurrentDisplay().getOptions().tileWidth();
|
||||
int tileY = e.getY() / super.getCurrentDisplay().getOptions().tileHeight();
|
||||
this.getCurrentDisplay().getCurrentTileMap()
|
||||
|
||||
@@ -12,8 +12,7 @@ import java.util.List;
|
||||
/**
|
||||
* Created by aargonian on 7/8/17.
|
||||
*/
|
||||
public class TileMapDisplay extends JComponent
|
||||
{
|
||||
public class TileMapDisplay extends JComponent {
|
||||
private static final OptionsBuilder DEFAULT_OPTIONS =
|
||||
new OptionsBuilder().borderColor(Color.black).displaySize(640, 480).tileSize(32, 32);
|
||||
private final OptionsBuilder options;
|
||||
@@ -21,52 +20,42 @@ public class TileMapDisplay extends JComponent
|
||||
private EditorTool currentActiveTool;
|
||||
private List<Pair<Integer, Integer>> selectedTiles;
|
||||
|
||||
public TileMapDisplay()
|
||||
{
|
||||
public TileMapDisplay() {
|
||||
this.options = DEFAULT_OPTIONS;
|
||||
}
|
||||
|
||||
public TileMapDisplay(OptionsBuilder options)
|
||||
{
|
||||
public TileMapDisplay(OptionsBuilder options) {
|
||||
this.options = options;
|
||||
this.setPreferredSize(new Dimension(options.displayWidth(), options.displayHeight()));
|
||||
}
|
||||
|
||||
public OptionsBuilder getOptions()
|
||||
{
|
||||
public OptionsBuilder getOptions() {
|
||||
return this.options;
|
||||
}
|
||||
|
||||
public TileMap getCurrentTileMap()
|
||||
{
|
||||
public TileMap getCurrentTileMap() {
|
||||
return this.tileMap;
|
||||
}
|
||||
|
||||
public void setCurrentTileMap(TileMap map)
|
||||
{
|
||||
public void setCurrentTileMap(TileMap map) {
|
||||
this.tileMap = map;
|
||||
}
|
||||
|
||||
public List<Pair<Integer, Integer>> getSelectedTiles()
|
||||
{
|
||||
public List<Pair<Integer, Integer>> getSelectedTiles() {
|
||||
return this.selectedTiles;
|
||||
}
|
||||
|
||||
public void setSelectedTiles(List<Pair<Integer, Integer>> tiles)
|
||||
{
|
||||
public void setSelectedTiles(List<Pair<Integer, Integer>> tiles) {
|
||||
this.selectedTiles = tiles;
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public EditorTool getCurrentActiveTool()
|
||||
{
|
||||
public EditorTool getCurrentActiveTool() {
|
||||
return this.currentActiveTool;
|
||||
}
|
||||
|
||||
public void setCurrentActiveTool(EditorTool tool)
|
||||
{
|
||||
if(this.currentActiveTool != null)
|
||||
{
|
||||
public void setCurrentActiveTool(EditorTool tool) {
|
||||
if (this.currentActiveTool != null) {
|
||||
this.removeMouseListener(this.currentActiveTool);
|
||||
this.removeMouseMotionListener(this.currentActiveTool);
|
||||
this.removeKeyListener(this.currentActiveTool);
|
||||
@@ -79,26 +68,20 @@ public class TileMapDisplay extends JComponent
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
public void paintComponent(Graphics g) {
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
g2.setColor(this.options.borderColor());
|
||||
g2.fillRect(0, 0, this.getWidth(), this.getHeight());
|
||||
for(int x = 0; x < this.tileMap.getColumns(); x++)
|
||||
{
|
||||
for(int y = 0; y < this.tileMap.getRows(); y++)
|
||||
{
|
||||
for (int x = 0; x < this.tileMap.getColumns(); x++) {
|
||||
for (int y = 0; y < this.tileMap.getRows(); y++) {
|
||||
final int tileXLoc = x * this.options.tileWidth();
|
||||
final int tileYLoc = y * this.options.tileHeight();
|
||||
|
||||
ImageResource imgRes = (ImageResource) this.tileMap.getTileResourceAt(TileImpl.PROPERTY_IMG, x, y);
|
||||
Image img = imgRes == null ? null : imgRes.getImage();
|
||||
if(img != null)
|
||||
{
|
||||
if (img != null) {
|
||||
g2.drawImage(img, tileXLoc, tileYLoc, this.options.tileWidth(), this.options.tileHeight(), null);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
final int HALF_WIDTH = this.options.tileWidth() / 2;
|
||||
final int HALF_HEIGHT = this.options.tileHeight() / 2;
|
||||
|
||||
@@ -111,12 +94,9 @@ public class TileMapDisplay extends JComponent
|
||||
g2.fillRect(tileXLoc + HALF_WIDTH, tileYLoc, HALF_WIDTH, HALF_HEIGHT);
|
||||
}
|
||||
|
||||
if(this.selectedTiles != null)
|
||||
{
|
||||
for(Pair<Integer, Integer> selectedTile : this.selectedTiles)
|
||||
{
|
||||
if(selectedTile.getFirst() == x && selectedTile.getSecond() == y)
|
||||
{
|
||||
if (this.selectedTiles != null) {
|
||||
for (Pair<Integer, Integer> selectedTile : this.selectedTiles) {
|
||||
if (selectedTile.getFirst() == x && selectedTile.getSecond() == y) {
|
||||
g2.setColor(Color.YELLOW);
|
||||
g2.fillRect(tileXLoc, tileYLoc, this.options.tileWidth(), this.options.tileHeight());
|
||||
g2.setColor(Color.YELLOW.brighter());
|
||||
@@ -129,8 +109,7 @@ public class TileMapDisplay extends JComponent
|
||||
}
|
||||
}
|
||||
|
||||
public static final class OptionsBuilder
|
||||
{
|
||||
public static final class OptionsBuilder {
|
||||
private int tileWidth = 32;
|
||||
private int tileHeight = 32;
|
||||
private int displayWidth = 640;
|
||||
@@ -138,79 +117,65 @@ public class TileMapDisplay extends JComponent
|
||||
private Color borderColor = Color.black;
|
||||
private boolean fullscreen = false;
|
||||
|
||||
public OptionsBuilder tileWidth(int width)
|
||||
{
|
||||
public OptionsBuilder tileWidth(int width) {
|
||||
this.tileWidth = width <= 0 ? 0 : width;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OptionsBuilder tileHeight(int height)
|
||||
{
|
||||
public OptionsBuilder tileHeight(int height) {
|
||||
this.tileHeight = height <= 0 ? 0 : height;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OptionsBuilder tileSize(int width, int height)
|
||||
{
|
||||
public OptionsBuilder tileSize(int width, int height) {
|
||||
return this.tileWidth(width).tileHeight(height);
|
||||
}
|
||||
|
||||
public OptionsBuilder borderColor(Color c)
|
||||
{
|
||||
public OptionsBuilder borderColor(Color c) {
|
||||
this.borderColor = c;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OptionsBuilder displayWidth(int width)
|
||||
{
|
||||
public OptionsBuilder displayWidth(int width) {
|
||||
this.displayWidth = width <= 0 ? 0 : width;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OptionsBuilder displayHeight(int height)
|
||||
{
|
||||
public OptionsBuilder displayHeight(int height) {
|
||||
this.displayHeight = height <= 0 ? 0 : height;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OptionsBuilder displaySize(int width, int height)
|
||||
{
|
||||
public OptionsBuilder displaySize(int width, int height) {
|
||||
return this.displayWidth(width).displayHeight(height);
|
||||
}
|
||||
|
||||
public OptionsBuilder fullscreen(boolean value)
|
||||
{
|
||||
public OptionsBuilder fullscreen(boolean value) {
|
||||
this.fullscreen = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int tileWidth()
|
||||
{
|
||||
public int tileWidth() {
|
||||
return this.tileWidth;
|
||||
}
|
||||
|
||||
public int tileHeight()
|
||||
{
|
||||
public int tileHeight() {
|
||||
return this.tileHeight;
|
||||
}
|
||||
|
||||
public int displayWidth()
|
||||
{
|
||||
public int displayWidth() {
|
||||
return this.displayWidth;
|
||||
}
|
||||
|
||||
public int displayHeight()
|
||||
{
|
||||
public int displayHeight() {
|
||||
return this.displayHeight;
|
||||
}
|
||||
|
||||
public Color borderColor()
|
||||
{
|
||||
public Color borderColor() {
|
||||
return this.borderColor;
|
||||
}
|
||||
|
||||
public boolean fulllscreen()
|
||||
{
|
||||
public boolean fulllscreen() {
|
||||
return this.fullscreen;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,19 +8,16 @@ import java.awt.event.MouseEvent;
|
||||
/**
|
||||
* Created by aargonian on 7/16/17.
|
||||
*/
|
||||
public class TileSelectTool extends EditorTool
|
||||
{
|
||||
public class TileSelectTool extends EditorTool {
|
||||
private int currentTileX = 0;
|
||||
private int currentTileY = 0;
|
||||
|
||||
public TileSelectTool(TileMapDisplay display)
|
||||
{
|
||||
public TileSelectTool(TileMapDisplay display) {
|
||||
super(display);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e)
|
||||
{
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
int tileX = e.getX() / super.getCurrentDisplay().getOptions().tileWidth();
|
||||
int tileY = e.getY() / super.getCurrentDisplay().getOptions().tileHeight();
|
||||
//getCurrentDisplay().setSelectedTiles(Arrays.asList(new Pair<Integer, Integer>(tileX, tileY)));
|
||||
@@ -30,13 +27,11 @@ public class TileSelectTool extends EditorTool
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e)
|
||||
{
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
int tileX = e.getX() / super.getCurrentDisplay().getOptions().tileWidth();
|
||||
int tileY = e.getY() / super.getCurrentDisplay().getOptions().tileHeight();
|
||||
|
||||
if(tileX != this.currentTileX || tileY != this.currentTileY)
|
||||
{
|
||||
if (tileX != this.currentTileX || tileY != this.currentTileY) {
|
||||
this.currentTileX = tileX;
|
||||
this.currentTileY = tileY;
|
||||
}
|
||||
|
||||
@@ -13,21 +13,17 @@ import java.util.ArrayList;
|
||||
* <p>
|
||||
* This class implements a display for a set of tiles.
|
||||
*/
|
||||
public final class TilesetDisplay extends JComponent implements MouseListener
|
||||
{
|
||||
public final class TilesetDisplay extends JComponent implements MouseListener {
|
||||
private final ArrayList<ImageResource> images;
|
||||
private final ArrayList<TilesetListener> listeners;
|
||||
private int currentlySelectedImage = 0;
|
||||
private int tileSize = 0;
|
||||
|
||||
public TilesetDisplay(int tileSize, ArrayList<ImageResource> tileImages)
|
||||
{
|
||||
if(tileImages == null)
|
||||
{
|
||||
public TilesetDisplay(int tileSize, ArrayList<ImageResource> tileImages) {
|
||||
if (tileImages == null) {
|
||||
throw new NullPointerException("Passed Image Set is Null.");
|
||||
}
|
||||
if(tileSize < 0)
|
||||
{
|
||||
if (tileSize < 0) {
|
||||
throw new IllegalArgumentException("Tilesize Cannot Be Negative!");
|
||||
}
|
||||
this.images = tileImages;
|
||||
@@ -40,58 +36,47 @@ public final class TilesetDisplay extends JComponent implements MouseListener
|
||||
this.setPreferredSize(new Dimension(squareSize, squareSize));
|
||||
}
|
||||
|
||||
public void addSetListener(TilesetListener listener)
|
||||
{
|
||||
public void addSetListener(TilesetListener listener) {
|
||||
this.listeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeSetListener(TilesetListener listener)
|
||||
{
|
||||
public void removeSetListener(TilesetListener listener) {
|
||||
this.listeners.remove(listener);
|
||||
}
|
||||
|
||||
private void setCurrentlySelectedImage(int index)
|
||||
{
|
||||
private void setCurrentlySelectedImage(int index) {
|
||||
this.currentlySelectedImage = index;
|
||||
if(index < this.images.size() && index >= 0)
|
||||
{
|
||||
for(TilesetListener listener : this.listeners)
|
||||
{
|
||||
if (index < this.images.size() && index >= 0) {
|
||||
for (TilesetListener listener : this.listeners) {
|
||||
listener.imageSelected(this.images.get(index));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ImageResource getCurrentlySelectedImage(ImageResource res)
|
||||
{
|
||||
if(this.currentlySelectedImage >= 0 && this.currentlySelectedImage < this.images.size())
|
||||
{
|
||||
public ImageResource getCurrentlySelectedImage(ImageResource res) {
|
||||
if (this.currentlySelectedImage >= 0 && this.currentlySelectedImage < this.images.size()) {
|
||||
return this.images.get(this.currentlySelectedImage);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addImageToDisplay(ImageResource img)
|
||||
{
|
||||
public void addImageToDisplay(ImageResource img) {
|
||||
this.images.add(img);
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public void removeImageFromDisplay(ImageResource img)
|
||||
{
|
||||
public void removeImageFromDisplay(ImageResource img) {
|
||||
this.images.remove(img);
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
//Todo: Update this to use a scrollpane and avoid divide by zero.
|
||||
@Override
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
public void paintComponent(Graphics g) {
|
||||
int columns = this.getWidth() / this.tileSize;
|
||||
if (columns != 0) // Avoid divide by zero and simply display nothing.
|
||||
{
|
||||
for(int i = 0; i < this.images.size(); i++)
|
||||
{
|
||||
for (int i = 0; i < this.images.size(); i++) {
|
||||
g.drawImage(this.images.get(i).getImage(), (i % columns) * this.tileSize, i / columns * this.tileSize,
|
||||
this.tileSize, this.tileSize, null);
|
||||
}
|
||||
@@ -99,10 +84,8 @@ public final class TilesetDisplay extends JComponent implements MouseListener
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e)
|
||||
{
|
||||
if(this.images.size() < 1)
|
||||
{
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
if (this.images.size() < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -113,31 +96,26 @@ public final class TilesetDisplay extends JComponent implements MouseListener
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e)
|
||||
{
|
||||
public void mousePressed(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e)
|
||||
{
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e)
|
||||
{
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e)
|
||||
{
|
||||
public void mouseExited(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
public interface TilesetListener
|
||||
{
|
||||
public interface TilesetListener {
|
||||
void imageSelected(ImageResource image);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,48 +12,36 @@ import java.util.Arrays;
|
||||
* This is a simple extension to the existing BufferedImage class that provides an implementation of equals() and
|
||||
* hashCode().
|
||||
*/
|
||||
public class ImageResource extends Resource
|
||||
{
|
||||
public class ImageResource extends Resource {
|
||||
private final BufferedImage res;
|
||||
private int hash = 0;
|
||||
|
||||
public ImageResource(String imagePath)
|
||||
{
|
||||
public ImageResource(String imagePath) {
|
||||
super(imagePath, ResourceType.IMAGE);
|
||||
|
||||
BufferedImage temp = null;
|
||||
if(imagePath != null && !imagePath.isEmpty())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (imagePath != null && !imagePath.isEmpty()) {
|
||||
try {
|
||||
File imageFile = new File(imagePath);
|
||||
if(imageFile.exists())
|
||||
{
|
||||
if (imageFile.exists()) {
|
||||
temp = ImageIO.read(imageFile);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
throw new IOException("Image File Does Not Exist: " + imagePath);
|
||||
}
|
||||
}
|
||||
catch(IOException ex)
|
||||
{
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
this.res = temp;
|
||||
}
|
||||
|
||||
public BufferedImage getImage()
|
||||
{
|
||||
public BufferedImage getImage() {
|
||||
return this.res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
if(this.hash == 0 && this.res != null)
|
||||
{
|
||||
public int hashCode() {
|
||||
if (this.hash == 0 && this.res != null) {
|
||||
int[] data = new int[this.res.getWidth() * this.res.getHeight()];
|
||||
this.res.getRGB(0, 0, this.res.getWidth(), this.res.getHeight(), data, 0, 0);
|
||||
this.hash = Arrays.hashCode(data);
|
||||
@@ -62,37 +50,28 @@ public class ImageResource extends Resource
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if(this == o)
|
||||
{
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if(o == null)
|
||||
{
|
||||
if (o == null) {
|
||||
return false;
|
||||
}
|
||||
if(o.getClass() != this.getClass())
|
||||
{
|
||||
if (o.getClass() != this.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ImageResource other = (ImageResource) o;
|
||||
if(this.res.getWidth() != other.res.getWidth())
|
||||
{
|
||||
if (this.res.getWidth() != other.res.getWidth()) {
|
||||
return false;
|
||||
}
|
||||
if(this.res.getHeight() != other.res.getHeight())
|
||||
{
|
||||
if (this.res.getHeight() != other.res.getHeight()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for(int x = 0; x < this.res.getHeight(); x++)
|
||||
{
|
||||
for(int y = 0; y < this.res.getWidth(); y++)
|
||||
{
|
||||
if(this.res.getRGB(x, y) != other.res.getRGB(x, y))
|
||||
{
|
||||
for (int x = 0; x < this.res.getHeight(); x++) {
|
||||
for (int y = 0; y < this.res.getWidth(); y++) {
|
||||
if (this.res.getRGB(x, y) != other.res.getRGB(x, y)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,24 +7,20 @@ package com.aargonian.resource;
|
||||
* or anything else that may be limited or is unique in some fashion. Resources are named entities, and are cacheable
|
||||
* and ownable. Resources are required to override the equals() and hashCode() methods, for convenience.
|
||||
*/
|
||||
abstract public class Resource
|
||||
{
|
||||
abstract public class Resource {
|
||||
private final String name;
|
||||
private final ResourceType type;
|
||||
|
||||
protected Resource(String name, ResourceType type)
|
||||
{
|
||||
protected Resource(String name, ResourceType type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public ResourceType getType()
|
||||
{
|
||||
public ResourceType getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
@@ -35,8 +31,7 @@ abstract public class Resource
|
||||
abstract public int hashCode();
|
||||
|
||||
//We know at compile-time what sort of resources exist, so they are enumerated here for checking
|
||||
public enum ResourceType
|
||||
{
|
||||
public enum ResourceType {
|
||||
FILE, IMAGE, SOUND, SCRIPT, SOCKET
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,22 +9,15 @@ import java.util.Map;
|
||||
* A utility class for loading and caching common resources used by the editor. Primarily used to load images and sound
|
||||
* files.
|
||||
*/
|
||||
public final class ResourceLoader
|
||||
{
|
||||
public final class ResourceLoader {
|
||||
private static final Map<String, Resource> resourceCache = new HashMap<>(100);
|
||||
|
||||
public static ImageResource loadImage(String imagePath)
|
||||
{
|
||||
if(imagePath == null || imagePath.isEmpty())
|
||||
{
|
||||
public static ImageResource loadImage(String imagePath) {
|
||||
if (imagePath == null || imagePath.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
else if(resourceCache.containsKey(imagePath) && resourceCache.get(imagePath) != null)
|
||||
{
|
||||
} else if (resourceCache.containsKey(imagePath) && resourceCache.get(imagePath) != null) {
|
||||
return (ImageResource) resourceCache.get(imagePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
ImageResource resource = new ImageResource(imagePath);
|
||||
resourceCache.put(imagePath, resource);
|
||||
return resource;
|
||||
|
||||
@@ -9,8 +9,7 @@ import java.util.Map;
|
||||
/**
|
||||
* Created by aargonian on 7/4/17.
|
||||
*/
|
||||
public class TileImpl
|
||||
{
|
||||
public class TileImpl {
|
||||
/**
|
||||
* Very important magic string values used by the tile class for common properties/values
|
||||
*/
|
||||
@@ -23,42 +22,31 @@ public class TileImpl
|
||||
private final HashPMap<String, Resource> resources;
|
||||
private int hash = 0;
|
||||
|
||||
public TileImpl(Map<String, String> startingProperties, Map<String, Resource> startingResources)
|
||||
{
|
||||
if(startingProperties != null && startingProperties.size() > 0)
|
||||
{
|
||||
public TileImpl(Map<String, String> startingProperties, Map<String, Resource> startingResources) {
|
||||
if (startingProperties != null && startingProperties.size() > 0) {
|
||||
this.properties = HashTreePMap.from(startingProperties);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
this.properties = null;
|
||||
}
|
||||
|
||||
if(startingResources != null && startingResources.size() > 0)
|
||||
{
|
||||
if (startingResources != null && startingResources.size() > 0) {
|
||||
this.resources = HashTreePMap.from(startingResources);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
this.resources = null;
|
||||
}
|
||||
}
|
||||
|
||||
private TileImpl(HashPMap<String, String> implProperties, HashPMap<String, Resource> implResources)
|
||||
{
|
||||
private TileImpl(HashPMap<String, String> implProperties, HashPMap<String, Resource> implResources) {
|
||||
this.properties = implProperties;
|
||||
this.resources = implResources;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if(this == o)
|
||||
{
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if(o == null || this.getClass() != o.getClass())
|
||||
{
|
||||
if (o == null || this.getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -69,10 +57,8 @@ public class TileImpl
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
if(this.hash == 0)
|
||||
{
|
||||
public int hashCode() {
|
||||
if (this.hash == 0) {
|
||||
int result = this.properties != null ? this.properties.hashCode() : 0;
|
||||
result = 31 * result + (this.resources != null ? this.resources.hashCode() : 0);
|
||||
this.hash = result;
|
||||
@@ -80,68 +66,54 @@ public class TileImpl
|
||||
return this.hash;
|
||||
}
|
||||
|
||||
public TileImpl removeProperty(String property)
|
||||
{
|
||||
if(this.properties == null)
|
||||
{
|
||||
public TileImpl removeProperty(String property) {
|
||||
if (this.properties == null) {
|
||||
return this;
|
||||
}
|
||||
return new TileImpl(this.properties.minus(property), this.resources);
|
||||
}
|
||||
|
||||
public String getProperty(String property)
|
||||
{
|
||||
if(this.properties == null)
|
||||
{
|
||||
public String getProperty(String property) {
|
||||
if (this.properties == null) {
|
||||
return null;
|
||||
}
|
||||
return this.properties.get(property);
|
||||
}
|
||||
|
||||
public TileImpl setProperty(String property, String value)
|
||||
{
|
||||
if(this.properties == null)
|
||||
{
|
||||
public TileImpl setProperty(String property, String value) {
|
||||
if (this.properties == null) {
|
||||
return new TileImpl(HashTreePMap.singleton(property, value), this.resources);
|
||||
}
|
||||
return new TileImpl(this.properties.plus(property, value), this.resources);
|
||||
}
|
||||
|
||||
public boolean hasProperty(String property)
|
||||
{
|
||||
public boolean hasProperty(String property) {
|
||||
return this.properties != null && this.properties.containsKey(property) &&
|
||||
this.properties.get(property) != null;
|
||||
}
|
||||
|
||||
public TileImpl removeResource(String resource)
|
||||
{
|
||||
if(this.resources == null)
|
||||
{
|
||||
public TileImpl removeResource(String resource) {
|
||||
if (this.resources == null) {
|
||||
return this;
|
||||
}
|
||||
return new TileImpl(this.properties, this.resources.minus(resource));
|
||||
}
|
||||
|
||||
public Resource getResource(String resource)
|
||||
{
|
||||
if(this.resources == null)
|
||||
{
|
||||
public Resource getResource(String resource) {
|
||||
if (this.resources == null) {
|
||||
return null;
|
||||
}
|
||||
return this.resources.get(resource);
|
||||
}
|
||||
|
||||
public TileImpl setResource(String resourceKey, Resource resource)
|
||||
{
|
||||
if(this.resources == null)
|
||||
{
|
||||
public TileImpl setResource(String resourceKey, Resource resource) {
|
||||
if (this.resources == null) {
|
||||
return new TileImpl(this.properties, HashTreePMap.singleton(resourceKey, resource));
|
||||
}
|
||||
return new TileImpl(this.properties, this.resources.plus(resourceKey, resource));
|
||||
}
|
||||
|
||||
public boolean hasResource(String resourceKey)
|
||||
{
|
||||
public boolean hasResource(String resourceKey) {
|
||||
return this.resources != null && this.resources.containsKey(resourceKey) &&
|
||||
this.resources.get(resourceKey) != null;
|
||||
}
|
||||
|
||||
@@ -7,55 +7,44 @@ import java.util.ArrayList;
|
||||
/**
|
||||
* Created by aargonian on 7/4/17.
|
||||
*/
|
||||
public class TileMap
|
||||
{
|
||||
public class TileMap {
|
||||
private final ArrayList<TileImpl> tilesImplementations;
|
||||
private final Tile[] tiles;
|
||||
private final int rows;
|
||||
private final int columns;
|
||||
|
||||
public TileMap(int columns, int rows)
|
||||
{
|
||||
public TileMap(int columns, int rows) {
|
||||
this.columns = columns;
|
||||
this.rows = rows;
|
||||
this.tilesImplementations = new ArrayList<>();
|
||||
this.tilesImplementations.add(new TileImpl(null, null));
|
||||
this.tilesImplementations.get(0).setProperty(TileImpl.PROPERTY_TILETYPE, "EmptyTile");
|
||||
this.tiles = new Tile[columns * rows];
|
||||
for(int i = 0; i < this.tiles.length; i++)
|
||||
{
|
||||
for (int i = 0; i < this.tiles.length; i++) {
|
||||
this.tiles[i] = new Tile(this.tilesImplementations.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
public void addTileImplementation(TileImpl impl)
|
||||
{
|
||||
public void addTileImplementation(TileImpl impl) {
|
||||
this.tilesImplementations.add(impl);
|
||||
}
|
||||
|
||||
public ArrayList<TileImpl> getTileImplementations()
|
||||
{
|
||||
public ArrayList<TileImpl> getTileImplementations() {
|
||||
return this.tilesImplementations;
|
||||
}
|
||||
|
||||
public int getRows()
|
||||
{
|
||||
public int getRows() {
|
||||
return this.rows;
|
||||
}
|
||||
|
||||
public int getColumns()
|
||||
{
|
||||
public int getColumns() {
|
||||
return this.columns;
|
||||
}
|
||||
|
||||
private void setTileImplementation(Tile tile, TileImpl newImpl)
|
||||
{
|
||||
for(TileImpl implementation : this.tilesImplementations)
|
||||
{
|
||||
if(implementation.hashCode() == newImpl.hashCode())
|
||||
{
|
||||
if(implementation.equals(newImpl))
|
||||
{
|
||||
private void setTileImplementation(Tile tile, TileImpl newImpl) {
|
||||
for (TileImpl implementation : this.tilesImplementations) {
|
||||
if (implementation.hashCode() == newImpl.hashCode()) {
|
||||
if (implementation.equals(newImpl)) {
|
||||
tile.setTileImplementation(implementation);
|
||||
return;
|
||||
}
|
||||
@@ -66,24 +55,19 @@ public class TileMap
|
||||
tile.setTileImplementation(newImpl);
|
||||
}
|
||||
|
||||
private boolean inBounds(int x, int y)
|
||||
{
|
||||
private boolean inBounds(int x, int y) {
|
||||
return !(x < 0 || x >= this.columns || y < 0 || y >= this.rows);
|
||||
}
|
||||
|
||||
public String getTilePropertyAt(String property, int x, int y)
|
||||
{
|
||||
if(this.inBounds(x, y))
|
||||
{
|
||||
public String getTilePropertyAt(String property, int x, int y) {
|
||||
if (this.inBounds(x, y)) {
|
||||
return this.tiles[(y * this.columns) + x].getTileImplementation().getProperty(property);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setTilePropertyAt(String property, String value, int x, int y)
|
||||
{
|
||||
if(!this.inBounds(x, y))
|
||||
{
|
||||
public void setTilePropertyAt(String property, String value, int x, int y) {
|
||||
if (!this.inBounds(x, y)) {
|
||||
return;
|
||||
}
|
||||
Tile tile = this.tiles[(y * this.columns) + x];
|
||||
@@ -91,19 +75,15 @@ public class TileMap
|
||||
this.setTileImplementation(tile, newImpl);
|
||||
}
|
||||
|
||||
public Resource getTileResourceAt(String resource, int x, int y)
|
||||
{
|
||||
if(!this.inBounds(x, y))
|
||||
{
|
||||
public Resource getTileResourceAt(String resource, int x, int y) {
|
||||
if (!this.inBounds(x, y)) {
|
||||
return null;
|
||||
}
|
||||
return this.tiles[(y * this.columns) + x].getTileImplementation().getResource(resource);
|
||||
}
|
||||
|
||||
public void setTileResourceAt(String resourceKey, Resource resource, int x, int y)
|
||||
{
|
||||
if(!this.inBounds(x, y))
|
||||
{
|
||||
public void setTileResourceAt(String resourceKey, Resource resource, int x, int y) {
|
||||
if (!this.inBounds(x, y)) {
|
||||
return;
|
||||
}
|
||||
Tile tile = this.tiles[(y * this.columns) + x];
|
||||
@@ -111,24 +91,19 @@ public class TileMap
|
||||
this.setTileImplementation(tile, newImpl);
|
||||
}
|
||||
|
||||
private final class Tile
|
||||
{
|
||||
private final class Tile {
|
||||
private TileImpl implementation;
|
||||
|
||||
private Tile(TileImpl impl)
|
||||
{
|
||||
private Tile(TileImpl impl) {
|
||||
this.implementation = impl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o)
|
||||
{
|
||||
if(this == o)
|
||||
{
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if(o == null || this.getClass() != o.getClass())
|
||||
{
|
||||
if (o == null || this.getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -138,18 +113,15 @@ public class TileMap
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
public int hashCode() {
|
||||
return this.implementation.hashCode();
|
||||
}
|
||||
|
||||
private TileImpl getTileImplementation()
|
||||
{
|
||||
private TileImpl getTileImplementation() {
|
||||
return this.implementation;
|
||||
}
|
||||
|
||||
private void setTileImplementation(TileImpl impl)
|
||||
{
|
||||
private void setTileImplementation(TileImpl impl) {
|
||||
this.implementation = impl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,34 +3,28 @@ package com.aargonian.util;
|
||||
/**
|
||||
* Created by aargonian on 7/17/17.
|
||||
*/
|
||||
public class Pair<K, T>
|
||||
{
|
||||
public class Pair<K, T> {
|
||||
private K first;
|
||||
private T second;
|
||||
|
||||
public Pair(K first, T second)
|
||||
{
|
||||
public Pair(K first, T second) {
|
||||
this.first = first;
|
||||
this.second = second;
|
||||
}
|
||||
|
||||
public K getFirst()
|
||||
{
|
||||
public K getFirst() {
|
||||
return this.first;
|
||||
}
|
||||
|
||||
public void setFirst(K first)
|
||||
{
|
||||
public void setFirst(K first) {
|
||||
this.first = first;
|
||||
}
|
||||
|
||||
public T getSecond()
|
||||
{
|
||||
public T getSecond() {
|
||||
return this.second;
|
||||
}
|
||||
|
||||
public void setSecond(T second)
|
||||
{
|
||||
public void setSecond(T second) {
|
||||
this.second = second;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user