Fixed formatting, more IDEA removal.

This commit is contained in:
Aaron Helton
2017-08-03 23:56:18 -04:00
parent 160dd8ec01
commit b4bfc9680d
15 changed files with 385 additions and 572 deletions

1
.gitignore vendored
View File

@@ -7,6 +7,7 @@ target/
bin/
.idea/
*.iml
# User-specific stuff:
.idea/**/workspace.xml

112
pom.xml
View File

@@ -1,61 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
<artifactId>tile-editor</artifactId>
<packaging>jar</packaging>
<version>0.1.0-SNAPSHOT</version>
<name>TileGameEditor</name>
<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>
<artifactId>tile-editor</artifactId>
<packaging>jar</packaging>
<version>0.1.0-SNAPSHOT</version>
<name>TileGameEditor</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.aargonian.editor.GameFrame</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.aargonian.editor.GameFrame</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.pcollections</groupId>
<artifactId>pcollections</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.pcollections</groupId>
<artifactId>pcollections</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
</project>

View File

@@ -14,25 +14,22 @@ 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
* pattern for the sake of a cleaner MVC implementation.
*/
private final List<Image> tileImages;
/**
* 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;
}
}

View File

@@ -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) {
}
}

View File

@@ -11,56 +11,47 @@ 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
}
return frame;
}
/**
* Creates the pane that holds the different tile implementations the user may choose to put into the editor.
*
* @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);
//Create TileMap Display
TileMapDisplay.OptionsBuilder displayOptions = new TileMapDisplay.OptionsBuilder();
displayOptions = displayOptions.tileSize(32, 32).displaySize(640, 480).borderColor(Color.black);
@@ -68,24 +59,24 @@ public class GameFrame
currentTileMapDisplay.setCurrentTileMap(map);
currentTileMapDisplay.setCurrentActiveTool(new TileSelectTool(currentTileMapDisplay));
frame.add(BorderLayout.CENTER, currentTileMapDisplay);
//Create TilesetDisplay
ArrayList<ImageResource> images = new ArrayList<>();
images.add(ResourceLoader.loadImage("res/Water.png"));
images.add(ResourceLoader.loadImage("res/Grass.png"));
TilesetDisplay tileDisplay = new TilesetDisplay(32, images);
frame.add(BorderLayout.WEST, tileDisplay);
//CREATE ALL TOOLS with their necessary connections
createTools(currentTileMapDisplay, tileDisplay);
//Create TileInfoDisplay
//Set the Tile Pencil Tool as the Current Active Tool
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

View File

@@ -8,31 +8,26 @@ 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()
.setTileResourceAt(TileImpl.PROPERTY_IMG, this.currentImage, tileX, tileY);
.setTileResourceAt(TileImpl.PROPERTY_IMG, this.currentImage, tileX, tileY);
this.getCurrentDisplay().repaint();
}
}

View File

@@ -12,205 +12,170 @@ 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;
private TileMap tileMap;
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);
}
this.currentActiveTool = tool;
this.addMouseListener(tool);
this.addMouseMotionListener(tool);
this.addKeyListener(tool);
}
@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;
g2.setColor(Color.lightGray);
g2.fillRect(tileXLoc, tileYLoc, HALF_WIDTH, HALF_HEIGHT);
g2.fillRect(tileXLoc + HALF_WIDTH, tileYLoc + HALF_HEIGHT, HALF_WIDTH, HALF_HEIGHT);
g2.setColor(Color.gray);
g2.fillRect(tileXLoc, tileYLoc + HALF_HEIGHT, HALF_WIDTH, HALF_HEIGHT);
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());
g2.fillRect(tileXLoc + 2, tileYLoc + 2, this.options.tileWidth() - 2,
this.options.tileHeight() - 2);
this.options.tileHeight() - 2);
}
}
}
}
}
}
public static final class OptionsBuilder
{
public static final class OptionsBuilder {
private int tileWidth = 32;
private int tileHeight = 32;
private int displayWidth = 640;
private int displayHeight = 480;
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;
}
}

View File

@@ -8,35 +8,30 @@ 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)));
this.getCurrentDisplay().getCurrentTileMap()
.setTileResourceAt(TileImpl.PROPERTY_IMG, new ImageResource("res/Water.png"), tileX, tileY);
.setTileResourceAt(TileImpl.PROPERTY_IMG, new ImageResource("res/Water.png"), tileX, tileY);
this.getCurrentDisplay().repaint();
}
@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;
}

View File

@@ -13,131 +13,109 @@ 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;
this.tileSize = tileSize;
this.listeners = new ArrayList<>();
this.addMouseListener(this);
//TODO: Remove this later?
int squareSize = ((int) (Math.sqrt(tileImages.size()))) * tileSize;
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.
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);
this.tileSize, this.tileSize, null);
}
}
}
@Override
public void mouseClicked(MouseEvent e)
{
if(this.images.size() < 1)
{
public void mouseClicked(MouseEvent e) {
if (this.images.size() < 1) {
return;
}
int columns = this.getWidth() / this.tileSize;
int tileX = e.getX() / this.tileSize;
int tileY = e.getY() / this.tileSize;
this.setCurrentlySelectedImage(tileY * columns + tileX);
}
@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);
}
}

View File

@@ -12,87 +12,66 @@ 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);
}
return this.hash;
}
@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;
}
}

View File

@@ -7,36 +7,31 @@ 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;
}
@Override
abstract public boolean equals(Object o);
@Override
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
}
}

View File

@@ -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;

View File

@@ -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
*/
@@ -18,132 +17,105 @@ public class TileImpl
public static final String PROPERTY_IMG = "ImageRef";
public static final String VALUE_TRUE = "True";
public static final String VALUE_FALSE = "False";
private final HashPMap<String, String> properties;
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;
}
TileImpl tile = (TileImpl) o;
return (this.properties != null ? this.properties.equals(tile.properties) : tile.properties == null) &&
(this.resources != null ? this.resources.equals(tile.resources) : tile.resources == null);
(this.resources != null ? this.resources.equals(tile.resources) : tile.resources == null);
}
@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;
}
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;
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;
this.resources.get(resourceKey) != null;
}
}

View File

@@ -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;
}
@@ -65,91 +54,74 @@ public class TileMap
this.tilesImplementations.add(newImpl);
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];
TileImpl newImpl = tile.getTileImplementation().setProperty(property, value);
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];
TileImpl newImpl = tile.getTileImplementation().setResource(resourceKey, resource);
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;
}
Tile tile = (Tile) o;
return this.implementation.equals(tile.implementation);
}
@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;
}
}

View File

@@ -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;
}
}