clean: housekeeping
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package eu.midnightdust.yaytris;
|
||||
|
||||
import eu.midnightdust.yaytris.util.NightJson;
|
||||
import eu.midnightdust.yaytris.util.json.NightJson;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -2,24 +2,26 @@ package eu.midnightdust.yaytris;
|
||||
|
||||
import eu.midnightdust.yaytris.util.Difficulty;
|
||||
import eu.midnightdust.yaytris.util.Language;
|
||||
import eu.midnightdust.yaytris.util.NightJson;
|
||||
import eu.midnightdust.yaytris.util.json.Comment;
|
||||
import eu.midnightdust.yaytris.util.json.NightJson;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@SuppressWarnings("unused") // Comments are unused in code, but are added to the JSON file for readability
|
||||
public class Settings {
|
||||
private static final NightJson json = new NightJson(Settings.class, "tetris_settings.json5");
|
||||
|
||||
public static NightJson.Comment c1 = new NightJson.Comment("Volume of theme music (0-100)");
|
||||
public static Comment c1 = new Comment("Volume of theme music (0-100)");
|
||||
public static int musicVolume = 100;
|
||||
public static NightJson.Comment c2 = new NightJson.Comment("Volume of sound effects (0-100)");
|
||||
public static Comment c2 = new Comment("Volume of sound effects (0-100)");
|
||||
public static int soundVolume = 100;
|
||||
public static NightJson.Comment c3 = new NightJson.Comment("Amount the user interface should be scaled");
|
||||
public static Comment c3 = new Comment("Amount the user interface should be scaled");
|
||||
public static float guiScale = 3.f;
|
||||
public static NightJson.Comment c4 = new NightJson.Comment("Whether speed should scale with level (true/false)");
|
||||
public static Comment c4 = new Comment("Whether speed should scale with level (true/false)");
|
||||
public static boolean shouldScaleSpeed = true;
|
||||
public static NightJson.Comment c5 = new NightJson.Comment("One of %s", Arrays.toString(Difficulty.values()));
|
||||
public static Comment c5 = new Comment("One of %s", Arrays.toString(Difficulty.values()));
|
||||
public static Difficulty difficulty = Difficulty.NORMAL;
|
||||
public static NightJson.Comment c6 = new NightJson.Comment("One of %s", Arrays.toString(Language.values()));
|
||||
public static Comment c6 = new Comment("One of %s", Arrays.toString(Language.values()));
|
||||
public static Language language = Language.ENGLISH;
|
||||
|
||||
public static void load() {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package eu.midnightdust.yaytris;
|
||||
|
||||
import eu.midnightdust.yaytris.util.NightJson;
|
||||
import eu.midnightdust.yaytris.util.json.NightJson;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
@@ -33,7 +33,7 @@ public class TetrisUI extends JFrame implements KeyListener {
|
||||
this.setResizable(false);
|
||||
this.getContentPane().setBackground(CatppuccinColor.MANTLE.getColor());
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
setWindowPosition(this, 0);
|
||||
this.setLocationRelativeTo(null);
|
||||
|
||||
titleLabel = new JLabel("Tetris");
|
||||
titleLabel.setForeground(Color.WHITE);
|
||||
@@ -66,6 +66,7 @@ public class TetrisUI extends JFrame implements KeyListener {
|
||||
titleLabel.setBounds(scale(225), scale(7), scale(110), scale(30));
|
||||
titleLabel.setIcon(new ImageIcon(new ImageIcon(titleImage).getImage().getScaledInstance(scale(110), scale(30), Image.SCALE_DEFAULT)));
|
||||
gamePanel.setBounds(scale(10), scale(10), scale(150), scale(282));
|
||||
this.setLocationRelativeTo(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,7 +103,7 @@ public class TetrisUI extends JFrame implements KeyListener {
|
||||
*
|
||||
* @param actionEvent unnecessary, but allows for more elegant lambda statements :)
|
||||
*/
|
||||
public void openMainMenu(ActionEvent actionEvent) {
|
||||
public void openMainMenu(@SuppressWarnings("unused") ActionEvent actionEvent) {
|
||||
if (this.menuPanel != null) this.remove(menuPanel);
|
||||
Tetris.resetSpace();
|
||||
rescale();
|
||||
@@ -120,7 +121,7 @@ public class TetrisUI extends JFrame implements KeyListener {
|
||||
*
|
||||
* @param actionEvent unnecessary, but allows for more elegant lambda statements :)
|
||||
*/
|
||||
public void openSettings(ActionEvent actionEvent) {
|
||||
public void openSettings(@SuppressWarnings("unused") ActionEvent actionEvent) {
|
||||
if (this.menuPanel != null) this.remove(menuPanel);
|
||||
menuPanel = new SettingsMenu(scale(170), scale(40), scale(220), scale(252), this);
|
||||
menuPanel.setBackground(CatppuccinColor.BASE.getColor());
|
||||
@@ -134,7 +135,7 @@ public class TetrisUI extends JFrame implements KeyListener {
|
||||
*
|
||||
* @param actionEvent unnecessary, but allows for more elegant lambda statements :)
|
||||
*/
|
||||
public void openScoreMenu(ActionEvent actionEvent) {
|
||||
public void openScoreMenu(@SuppressWarnings("unused") ActionEvent actionEvent) {
|
||||
if (this.menuPanel != null) this.remove(menuPanel);
|
||||
menuPanel = new ScoreMenu(scale(170), scale(40), scale(220), scale(252), this);
|
||||
menuPanel.setBackground(CatppuccinColor.BASE.getColor());
|
||||
@@ -148,7 +149,7 @@ public class TetrisUI extends JFrame implements KeyListener {
|
||||
*
|
||||
* @param actionEvent unnecessary, but allows for more elegant lambda statements :)
|
||||
*/
|
||||
public void openHighscores(ActionEvent actionEvent) {
|
||||
public void openHighscores(@SuppressWarnings("unused") ActionEvent actionEvent) {
|
||||
if (this.menuPanel != null) this.remove(menuPanel);
|
||||
menuPanel = new HighScoreMenu(scale(170), scale(40), scale(220), scale(252), this);
|
||||
menuPanel.setBackground(CatppuccinColor.BASE.getColor());
|
||||
@@ -167,40 +168,6 @@ public class TetrisUI extends JFrame implements KeyListener {
|
||||
HighScores.addScore(playerName, score);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Centers the game window on the given screen.
|
||||
* Source: <a href="https://stackoverflow.com/a/19746437">Miss Chanandler Bong & Peter Szabo on StackOverflow</a>
|
||||
*
|
||||
* @param window the window to center
|
||||
* @param screen the screen to center it on
|
||||
*/
|
||||
private void setWindowPosition(JFrame window, int screen) {
|
||||
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||
GraphicsDevice[] allDevices = env.getScreenDevices();
|
||||
int topLeftX, topLeftY, screenX, screenY, windowPosX, windowPosY;
|
||||
|
||||
if (screen < allDevices.length && screen > -1) {
|
||||
topLeftX = allDevices[screen].getDefaultConfiguration().getBounds().x;
|
||||
topLeftY = allDevices[screen].getDefaultConfiguration().getBounds().y;
|
||||
|
||||
screenX = allDevices[screen].getDefaultConfiguration().getBounds().width;
|
||||
screenY = allDevices[screen].getDefaultConfiguration().getBounds().height;
|
||||
}
|
||||
else {
|
||||
topLeftX = allDevices[0].getDefaultConfiguration().getBounds().x;
|
||||
topLeftY = allDevices[0].getDefaultConfiguration().getBounds().y;
|
||||
|
||||
screenX = allDevices[0].getDefaultConfiguration().getBounds().width;
|
||||
screenY = allDevices[0].getDefaultConfiguration().getBounds().height;
|
||||
}
|
||||
|
||||
windowPosX = ((screenX - window.getWidth()) / 2) + topLeftX;
|
||||
windowPosY = ((screenY - window.getHeight()) / 2) + topLeftY;
|
||||
|
||||
window.setLocation(windowPosX, windowPosY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Capture keyboard inputs during a game session.
|
||||
*
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package eu.midnightdust.yaytris.util;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.sound.sampled.AudioInputStream;
|
||||
import javax.sound.sampled.AudioSystem;
|
||||
import javax.sound.sampled.UnsupportedAudioFileException;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -17,21 +14,4 @@ public class FileUtil {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static AudioInputStream loadAudio(String location) {
|
||||
try (InputStream fileStream = FileUtil.class.getResourceAsStream(location)) {
|
||||
assert fileStream != null;
|
||||
return AudioSystem.getAudioInputStream(fileStream);
|
||||
} catch (IOException | NullPointerException | UnsupportedAudioFileException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static InputStream getFileStream(String location) {
|
||||
try (InputStream fileStream = FileUtil.class.getResourceAsStream(location)) {
|
||||
return fileStream;
|
||||
} catch (IOException | NullPointerException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
19
src/main/java/eu/midnightdust/yaytris/util/json/Comment.java
Normal file
19
src/main/java/eu/midnightdust/yaytris/util/json/Comment.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package eu.midnightdust.yaytris.util.json;
|
||||
|
||||
/**
|
||||
* Add comments to your json files.
|
||||
* If you decide to use this, it's best to save with the .json5 extension, as regular json does not officially support comments.
|
||||
*/
|
||||
public class Comment {
|
||||
final String commentString;
|
||||
|
||||
/**
|
||||
* Add a comment to spice-up the json file :)
|
||||
*
|
||||
* @param commentString the string you want to write as a comment
|
||||
* @param args optional formatting arguments, calls {@link String#format(String, Object...)}
|
||||
*/
|
||||
public Comment(String commentString, Object... args) {
|
||||
this.commentString = String.format(commentString, args);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package eu.midnightdust.yaytris.util;
|
||||
package eu.midnightdust.yaytris.util.json;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
@@ -12,8 +12,8 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* NightJson v0.2 by Martin Prokoph
|
||||
* Extremely lightweight (and incomplete) JSON library
|
||||
* NightJson v0.3 by Martin Prokoph
|
||||
* Extremely lightweight JSON library
|
||||
* Concept inspired by GSON
|
||||
*/
|
||||
public class NightJson {
|
||||
@@ -39,28 +39,11 @@ public class NightJson {
|
||||
/**
|
||||
* Convert the current state of the java json class to actual json and save it to disk.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void writeJson() {
|
||||
if (fileLocation == null) return;
|
||||
try {
|
||||
FileWriter jsonFile = new FileWriter(fileLocation);
|
||||
|
||||
jsonFile.write("{\n");
|
||||
Iterator<Field> it = Arrays.stream(jsonClass.getFields()).iterator();
|
||||
while (it.hasNext()) {
|
||||
Field field = it.next();
|
||||
if (field != jsonMap) writeElement(jsonFile, field.get(null), field.getType(), field.getName(), it.hasNext());
|
||||
}
|
||||
if (jsonMap != null) {
|
||||
Iterator<String> mapIt = ((Map<String,?>)jsonMap.get(null)).keySet().iterator();
|
||||
while (mapIt.hasNext()) {
|
||||
String key = mapIt.next();
|
||||
Object value = jsonMap.get(key);
|
||||
writeElement(jsonFile, value, value.getClass(), key, mapIt.hasNext());
|
||||
}
|
||||
}
|
||||
jsonFile.write("}");
|
||||
jsonFile.close();
|
||||
writeJsonToFile(jsonFile);
|
||||
} catch (IOException | IllegalAccessException e) {
|
||||
System.out.println("Oh no! An Error occurred whilst writing the JSON file :(");
|
||||
//noinspection CallToPrintStackTrace
|
||||
@@ -68,10 +51,30 @@ public class NightJson {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void writeJsonToFile(FileWriter jsonFile) throws IOException, IllegalAccessException {
|
||||
jsonFile.write("{\n");
|
||||
Iterator<Field> it = Arrays.stream(jsonClass.getFields()).iterator();
|
||||
while (it.hasNext()) {
|
||||
Field field = it.next();
|
||||
if (field != jsonMap) writeElement(jsonFile, field.get(null), field.getType(), field.getName(), it.hasNext());
|
||||
}
|
||||
if (jsonMap != null) {
|
||||
Iterator<String> mapIt = ((Map<String,?>)jsonMap.get(null)).keySet().iterator();
|
||||
while (mapIt.hasNext()) {
|
||||
String key = mapIt.next();
|
||||
Object value = jsonMap.get(key);
|
||||
writeElement(jsonFile, value, value.getClass(), key, mapIt.hasNext());
|
||||
}
|
||||
}
|
||||
jsonFile.write("}");
|
||||
jsonFile.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the desired element into the file.
|
||||
*/
|
||||
private void writeElement(FileWriter jsonFile, Object value, Class<?> type, String name, boolean hasNext) throws IOException, IllegalAccessException {
|
||||
private void writeElement(FileWriter jsonFile, Object value, Class<?> type, String name, boolean hasNext) throws IOException {
|
||||
jsonFile.write("\t");
|
||||
if (type == Comment.class) {
|
||||
jsonFile.write(String.format("// %s\n", ((Comment) value).commentString));
|
||||
@@ -120,23 +123,9 @@ public class NightJson {
|
||||
/**
|
||||
* Read the json file from disk and overwrite the json class's field values.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void readJsonFromString(String jsonString) {
|
||||
try {
|
||||
Map<String, Object> asMap = jsonToMap(
|
||||
jsonString.replaceAll("(//)+.*\n", ""), // Replace comment lines (Json5)
|
||||
(key) -> getField(key).isPresent() ? getField(key).get().getType() : String.class); // Determine data type
|
||||
|
||||
for (String key : asMap.keySet()) {
|
||||
Object value = asMap.get(key);
|
||||
Optional<Field> field = getField(key);
|
||||
if (field.isPresent()) {
|
||||
field.get().set(null, value);
|
||||
}
|
||||
else if (jsonMap != null) {
|
||||
((Map<String, Object>)jsonMap.get(null)).put(key, value);
|
||||
}
|
||||
}
|
||||
readJsonString(jsonString);
|
||||
} catch (IllegalAccessException | NoSuchElementException | ClassCastException e) {
|
||||
System.out.println("Oh no! An Error occurred whilst reading the JSON file :(");
|
||||
//noinspection CallToPrintStackTrace
|
||||
@@ -144,6 +133,24 @@ public class NightJson {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void readJsonString(String jsonString) throws IllegalAccessException {
|
||||
Map<String, Object> asMap = jsonToMap(
|
||||
jsonString.replaceAll("(//)+.*\n", ""), // Replace comment lines (Json5)
|
||||
(key) -> getField(key).isPresent() ? getField(key).get().getType() : String.class); // Determine data type
|
||||
|
||||
for (String key : asMap.keySet()) {
|
||||
Object value = asMap.get(key);
|
||||
Optional<Field> field = getField(key);
|
||||
if (field.isPresent()) {
|
||||
field.get().set(null, value);
|
||||
}
|
||||
else if (jsonMap != null) {
|
||||
((Map<String, Object>)jsonMap.get(null)).put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the json file as key-value pairs and save it as a map.
|
||||
*/
|
||||
@@ -249,20 +256,4 @@ public class NightJson {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add comments to your json files.
|
||||
* If you decide to use this, it's best to save with the .json5 extension, as regular json does not officially support comments.
|
||||
*/
|
||||
public static class Comment {
|
||||
final String commentString;
|
||||
/**
|
||||
* Add a comment to spice-up the json file :)
|
||||
* @param commentString the string you want to write as a comment
|
||||
* @param args optional formatting arguments, calls {@link String#format(String, Object...)}
|
||||
*/
|
||||
public Comment(String commentString, Object... args) {
|
||||
this.commentString = String.format(commentString, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user