From eb0ec0469f21f7d5baf196da227ed702008870b6 Mon Sep 17 00:00:00 2001 From: Pegacraffft Date: Thu, 15 Jul 2021 19:38:05 +0200 Subject: [PATCH] Fixed scaling bug --- .idea/misc.xml | 2 +- src/main/java/engine/Display.java | 12 +- src/main/java/engine/Game.java | 21 +-- src/main/java/engine/editor/EditScene.java | 119 ---------------- src/main/java/engine/editor/Environment.java | 131 ------------------ .../java/engine/editor/SettingsScene.java | 51 ------- src/main/java/engine/editor/Tile.java | 90 ------------ src/main/java/engine/editor/menu/Sprite.java | 86 ------------ src/main/java/engine/listeners/Mouse.java | 4 +- .../java/engine/mechanics/DropDownMenu.java | 2 +- src/main/java/engine/rendering/Graphics.java | 42 ++++-- 11 files changed, 43 insertions(+), 517 deletions(-) delete mode 100644 src/main/java/engine/editor/EditScene.java delete mode 100644 src/main/java/engine/editor/Environment.java delete mode 100644 src/main/java/engine/editor/SettingsScene.java delete mode 100644 src/main/java/engine/editor/Tile.java delete mode 100644 src/main/java/engine/editor/menu/Sprite.java diff --git a/.idea/misc.xml b/.idea/misc.xml index d75accb..61a60e7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/src/main/java/engine/Display.java b/src/main/java/engine/Display.java index db39b0e..134cf0d 100644 --- a/src/main/java/engine/Display.java +++ b/src/main/java/engine/Display.java @@ -6,7 +6,6 @@ import javax.swing.*; import java.awt.*; -import java.awt.event.FocusEvent; import java.awt.image.BufferStrategy; /** @@ -96,6 +95,9 @@ public Display setSize(int width, int height) { this.width = width; this.height = height; frame.setSize(width, height); + canvas.setSize(width, height); + Graphics.setStdWidth(width); + Graphics.setStdHeight(height); } return this; } @@ -196,17 +198,17 @@ public boolean isFullScreen() { } /** - * @return Returns the screen width. + * @return Returns the screen width. (Don't use this to render something at a relative position of the screen, use Graphics.getStdWidth()) */ public int getWidth() { - return frame.getWidth(); + return canvas.getWidth(); } /** - * @return Returns the screen height. + * @return Returns the screen height. (Don't use this to render something at a relative position of the screen, use Graphics.getStdHeight()) */ public int getHeight() { - return frame.getHeight(); + return canvas.getHeight(); } /** diff --git a/src/main/java/engine/Game.java b/src/main/java/engine/Game.java index eb932c7..50accb2 100644 --- a/src/main/java/engine/Game.java +++ b/src/main/java/engine/Game.java @@ -1,9 +1,6 @@ package engine; -import engine.editor.EditScene; -import engine.editor.SettingsScene; import engine.loops.Loop; -import engine.rendering.Graphics; import java.util.HashMap; import java.util.Map; @@ -54,12 +51,12 @@ public static void addScene(Scene scene, String alias) { } /** - * proxy to a display named "MAIN" + * proxy to a display named "main" * * @return the display named MAIN */ public static Display display() { - return display("MAIN"); + return display("main"); } /** @@ -76,19 +73,6 @@ public static Display display(String name) { return displays.get(name); } - /** - * start the environment-editor using a new display called "editor" - */ - public static void startEditor() { - Display display = Game.display("editor"); - Game.addScene(new EditScene(), "EditScene"); - Game.addScene(new SettingsScene(), "Settings"); - display.attachScene("EditScene"); - Game.start(); - Game.setFrameRate(60); - display.setTitle("Map Editor"); - } - /** * get a scene using it's alias * @@ -100,7 +84,6 @@ public static Scene getScene(String alias) { } /** - * * @return How long it took to calculate the last frame in respect to the framerate */ public static double deltaTime() { diff --git a/src/main/java/engine/editor/EditScene.java b/src/main/java/engine/editor/EditScene.java deleted file mode 100644 index 0835b23..0000000 --- a/src/main/java/engine/editor/EditScene.java +++ /dev/null @@ -1,119 +0,0 @@ -package engine.editor; - -import engine.Game; -import engine.Scene; -import engine.mechanics.Button; -import engine.editor.menu.Sprite; -import engine.listeners.MouseButtons; -import engine.mechanics.*; - -import javax.swing.*; -import javax.swing.filechooser.FileNameExtensionFilter; -import java.awt.*; -import java.awt.event.MouseEvent; -import java.io.File; - -import static engine.rendering.Graphics.g; - -public class EditScene extends Scene { - Grid grid; - private Hitbox workArea; - private TextBox scaleBox; - private JFileChooser chooser; - private Environment env; - private double scale; - private Tile selection; - - @Override - public void init() { - workArea = new Hitbox(new Point(0, 0), new Point(1100, 720)); - scaleBox = new TextBox(1190, 460, 30, 20, this); - chooser = new JFileChooser(); - grid = new Grid(0, 0, 1100, 720, 10, 10); - env = new Environment(); - scale = 1; - MethodObject backPanel = new MethodObject(this).execRenderLoop(e -> { - g.setColor(Color.white); - g.fillRect(1100, 451, 1280, 720); - }); - TickBox t = new TickBox(1190, 650, 30, 30, this); - Button b = new Button(1130, 500, 100, 40, this) - .addEvent(MouseButtons.LEFT_DOWN, this::mouseHandler) - .addEvent(MouseButtons.RIGHT_DOWN, this::mouseHandler) - .addEvent(MouseButtons.MIDDLE_DOWN, this::mouseHandler) - .setColor(Color.cyan) - .setText("[L]oad/Save[R]") - .setHoverColor(Color.cyan.darker()) - .setTextColor(Color.red) - .setFontSize(15) - .setFont("JhengHei UI"); - Button settings = new Button(1130, 600, 100, 40, this) - .addEvent(MouseButtons.LEFT_DOWN, e -> display.attachScene("Settings")) - .setColor(Color.GREEN) - .setText("Settings") - .setFont("JHengHei UI"); - grid.show(Color.DARK_GRAY); - SettingsScene scene = (SettingsScene) (Game.getScene("Settings")); - grid.setWidth(scene.gridWidth); - grid.setHeight(scene.gridHeight); - addObject(env); - addObject(grid); - for (int i = 0; i < 10; i++) - addObject(new Tile(1100 + (i / 5) * 90, (i % 5) * 90)); - addObject(env); - addObject(backPanel); - addObject(scaleBox); - addObject(b); - addObject(settings); - addObject(t); - - mouseListener.addEvent(MouseButtons.LEFT_DOWN, e -> { - Point p = grid.toGrid(mouseListener.getMousePos()); - if (workArea.isInside(mouseListener.getMousePos()) && selection != null && selection.importPath != null) - env.add(new Sprite( - p.x, p.y, - scale, selection.importPath, this)); - }, false); - } - - private void mouseHandler(MouseEvent e) { - File f = new File("src/main/resources"); - String url = f.getAbsolutePath(); - chooser.setCurrentDirectory(new File(url)); - chooser.setFileFilter(new FileNameExtensionFilter("Environments", "ini")); - int returnValue; - if (e.getButton() == MouseEvent.BUTTON1) returnValue = chooser.showOpenDialog(display.getCanvas()); - else if (e.getButton() == MouseEvent.BUTTON3) returnValue = chooser.showSaveDialog(display.getCanvas()); - else { - env.reload(); - return; - } - if (returnValue == JFileChooser.APPROVE_OPTION) { - String path = chooser.getSelectedFile().getPath(); - if (!path.matches(".*\\.ini")) path += ".ini"; - if (e.getButton() == MouseEvent.BUTTON1) env.loadEnv(path); - else env.saveEnv(path); - } - } - - @Override - public void logicLoop() { - try { - scale = Double.parseDouble(scaleBox.getText()); - } catch (NumberFormatException ignore) { // - } - } - - @Override - public void renderLoop() { - if (selection != null) - selection.drawSelectionHUD(); - g.setColor(Color.BLACK); - g.drawString("Scale:", 1150, 475); - g.draw(workArea.getShape()); - } - - public void setSelection(Tile selection) { - this.selection = selection; - } -} diff --git a/src/main/java/engine/editor/Environment.java b/src/main/java/engine/editor/Environment.java deleted file mode 100644 index cb1edcf..0000000 --- a/src/main/java/engine/editor/Environment.java +++ /dev/null @@ -1,131 +0,0 @@ -package engine.editor; - -import engine.Entity; -import engine.Game; -import engine.editor.menu.Sprite; -import engine.listeners.MouseButtons; - -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.util.Arrays; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import static java.lang.String.format; - -public class Environment extends Entity { - private final Map entityList = new ConcurrentHashMap<>(); - private String path; - - /** - * save the environment to a given path - * - * @param path the path to save to - */ - public void saveEnv(String path) { - this.path = path; - try (FileWriter fw = new FileWriter(path)) { - for (Sprite e : entityList.values()) { - fw.write(format("[%d]%n", e.hashCode())); - fw.write(format("Pos=%d,%d%n", e.getPos().x, e.getPos().y)); - fw.write(format("Image=%s%n", e.getSpritePath())); - fw.write(format("Scale=%s%n", e.getScale())); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * add a sprite to the environment, as an id, it's hashCode will be used - * - * @param sprite the sprite to be added - */ - public void add(Sprite sprite) { - entityList.put(sprite.hashCode(), sprite); - } - - /** - * load an environment.ini file using it's path - * - * @param path the path pointing to said file - */ - public void loadEnv(String path) { - this.path = path; - entityList.clear(); - String[] content = new String[0]; - try (FileReader fr = new FileReader(path)) { - int c; - StringBuilder temp = new StringBuilder(); - while ((c = fr.read()) != -1) { - temp.append((char) c); - } - content = temp.toString().replaceAll("\r", "").split("\n"); - } catch (IOException e) { - e.printStackTrace(); - } - if (content.length == 0) return; - - int id = 0; - int x = 0; - int y = 0; - double scale = 0; - String spritePath = ""; - for (String line : content) { - if (line.startsWith("[")) { - if (id != 0) - entityList.put(id, new Sprite(x, y, scale, spritePath, Game.getScene("EditScene"))); - id = Integer.parseInt(line.replaceAll("[\\[\\]]", " ").trim()); - } - - if (!line.contains("=")) continue; - String key = line.split("=")[0].trim(); - String value = line.split("=")[1].trim(); - switch (key) { - case "Pos" -> { - x = Integer.parseInt(value.split(",")[0]); - y = Integer.parseInt(value.split(",")[1]); - } - case "Image" -> spritePath = value; - case "Scale" -> scale = Double.parseDouble(value); - default -> System.out.println("invalid value in line " + (Arrays.asList(content).indexOf(line) + 1)); - } - } - } - - @Override - public void init() { - entityList.clear(); - } - - @Override - public void logicLoop() { - entityList.values().forEach(e -> { - if (e.delete) Game.getScene("EditScene") - .mouseListener.deleteEvent(MouseButtons.RIGHT_DOWN, e.onClickEvent); - }); - entityList.values().removeIf(sprite -> sprite.delete); - entityList.values().forEach(Sprite::logicLoop); - } - - @Override - public void renderLoop() { - entityList.values().forEach(Sprite::renderLoop); - } - - /** - * use this to get direct interaction to a sprite if you need to manipulate it via code - * - * @param id the id of said object (see the env.ini file) - * @return the wanted object - */ - public Sprite getSprite(int id) { - return entityList.get(id); - } - - public void reload() { - if (path != null) - loadEnv(path); - } -} diff --git a/src/main/java/engine/editor/SettingsScene.java b/src/main/java/engine/editor/SettingsScene.java deleted file mode 100644 index 17bbdce..0000000 --- a/src/main/java/engine/editor/SettingsScene.java +++ /dev/null @@ -1,51 +0,0 @@ -package engine.editor; - -import engine.Scene; -import engine.mechanics.Button; -import engine.listeners.MouseButtons; -import engine.mechanics.TextBox; - -import java.awt.*; - -import static engine.rendering.Graphics.g; - -public class SettingsScene extends Scene { - int gridWidth = 90; - int gridHeight = 90; - - @Override - public void init() { - TextBox gridWidthBox = new TextBox(90, 87, 100, 20, this) - .setMatcher("[0-9]*") - .setMaxValue(4) - .setText(String.valueOf(gridWidth)); - TextBox gridHeightBox = new TextBox(90, 127, 100, 20, this) - .setMatcher("[0-9]*") - .setMaxValue(4) - .setText(String.valueOf(gridHeight)); - Button back = new Button(20, 20, 100, 40, this) - .addEvent(MouseButtons.LEFT_DOWN, e -> { - gridWidth = Integer.parseInt(gridWidthBox.getText()); - gridHeight = Integer.parseInt(gridHeightBox.getText()); - display.attachScene("EditScene"); - }) - .setColor(Color.GRAY) - .setFont("JhengHei UI") - .setText("Back") - .setTextColor(Color.white); - addObject(back); - addObject(gridWidthBox); - addObject(gridHeightBox); - } - - @Override - public void logicLoop() { - - } - - @Override - public void renderLoop() { - g.drawString("Grid width:", 20, 100); - g.drawString("Grid height:", 20, 140); - } -} diff --git a/src/main/java/engine/editor/Tile.java b/src/main/java/engine/editor/Tile.java deleted file mode 100644 index 68dfc2c..0000000 --- a/src/main/java/engine/editor/Tile.java +++ /dev/null @@ -1,90 +0,0 @@ -package engine.editor; - -import engine.Entity; -import engine.Game; -import engine.listeners.MouseButtons; -import engine.mechanics.Hitbox; -import engine.rendering.Image; - -import javax.swing.*; -import javax.swing.filechooser.FileNameExtensionFilter; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.io.File; - -import static engine.rendering.Graphics.g; - -public class Tile extends Entity { - private static final int WIDTH = 90; - private static final int HEIGHT = 90; - private final int x; - private final int y; - private final BufferedImage select = Image.load("editor/Selected.png"); - private final Hitbox h; - private final JFileChooser chooser = new JFileChooser(); - private final EditScene scene = (EditScene) Game.getScene("EditScene"); - String importPath; - private BufferedImage preview; - - public Tile(int x, int y) { - this.x = x; - this.y = y; - h = new Hitbox(new Point(x, y), new Point(x + WIDTH, y + HEIGHT)); - chooser.setFileFilter(new FileNameExtensionFilter("images", "png", "jpeg", "jpg", "gif")); - //opens the import dialog - scene.mouseListener.addEvent(MouseButtons.RIGHT_DOWN, e -> { - if (h.isInside(scene.mouseListener.getMousePos())) { - File f = new File("src/main/resources"); - String url = f.getAbsolutePath(); - chooser.setCurrentDirectory(new File(url)); - int returnValue = chooser.showOpenDialog(scene.display.getCanvas()); - //gets file path - if (returnValue == JFileChooser.APPROVE_OPTION) { - importPath = chooser.getSelectedFile().getPath(); - //makes path relative - importPath = importPath.substring(importPath.lastIndexOf("resources\\") + 10); - - //checks if file type is compatible - if (getFileExtension(importPath).matches("png|jpe?g|gif")) - preview = Image.load(importPath); - else throw new IllegalArgumentException("this is not a valid image i believe"); - } - } - }, false); - // selects a sprite - scene.mouseListener.addEvent(MouseButtons.LEFT_DOWN, e -> { - if (h.isInside(scene.mouseListener.getMousePos())) - scene.setSelection(this); - }, false); - } - - @Override - public void init() { - // TBD - } - - @Override - public void logicLoop() { - // TBD - } - - @Override - public void renderLoop() { - g.setColor(Color.blue); - g.fillRect(x, y, WIDTH, HEIGHT); - if (preview != null) - g.drawImage(preview, x, y, WIDTH, HEIGHT, null); - g.setColor(Color.red); - g.draw(h.getShape()); - } - - public void drawSelectionHUD() { - g.drawImage(select, x, y, null); - } - - public String getFileExtension(String fullName) { - String fileName = new File(fullName).getName(); - int dotIndex = fileName.lastIndexOf('.'); - return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1); - } -} diff --git a/src/main/java/engine/editor/menu/Sprite.java b/src/main/java/engine/editor/menu/Sprite.java deleted file mode 100644 index 3963ce8..0000000 --- a/src/main/java/engine/editor/menu/Sprite.java +++ /dev/null @@ -1,86 +0,0 @@ -package engine.editor.menu; - -import engine.Entity; -import engine.Scene; -import engine.listeners.MouseButtons; -import engine.mechanics.Hitbox; -import engine.rendering.Image; - -import java.awt.*; -import java.awt.event.MouseEvent; -import java.awt.image.BufferedImage; -import java.util.function.Predicate; - -import static engine.rendering.Graphics.g; - -public class Sprite extends Entity { - public final Predicate onClickEvent; - private final int x; - private final int y; - private final java.awt.Image spriteScaled; - private final Hitbox h; - private final String spritePath; - private final double scale; - public boolean delete = false; - private final Scene scene; - - /** - * create a sprite at a given location using a position, a scale,a path to an image, and a scene to be bound to. - * - * @param x the x coordinate of the sprite - * @param y the y coordinate of the sprite - * @param scale the sprite's scaling factor - * @param spritePath a path to an image - * @param scene the scene to be bound to - */ - public Sprite(int x, int y, double scale, String spritePath, Scene scene) { - this.x = x; - this.y = y; - this.spritePath = spritePath; - this.scale = scale; - this.scene = scene; - BufferedImage sprite = Image.load(spritePath); - spriteScaled = sprite.getScaledInstance((int) (sprite.getWidth() * scale), - (int) (sprite.getHeight() * scale), 3); - - h = new Hitbox(new Point(x, y), new Point(x + (int) (sprite.getWidth() * scale), - y + (int) (sprite.getHeight() * scale))); - onClickEvent = e -> { - if (h.isInside(scene.mouseListener.getMousePos())) { - delete = true; - return true; - } - return false; - }; - scene.mouseListener.addEvent(MouseButtons.RIGHT_DOWN, onClickEvent); - } - - @Override - public void init() { - // TBD - } - - @Override - public void logicLoop() { - if (scene.mouseListener.isHeld(3)) - if (h.isInside(scene.mouseListener.getMousePos())) - delete = true; - } - - @Override - public void renderLoop() { - g.drawImage(spriteScaled, x, y, null); - } - - public Point getPos() { - return new Point(x, y); - } - - public String getSpritePath() { - return spritePath; - } - - public double getScale() { - return scale; - } -} diff --git a/src/main/java/engine/listeners/Mouse.java b/src/main/java/engine/listeners/Mouse.java index 65715f2..527121f 100644 --- a/src/main/java/engine/listeners/Mouse.java +++ b/src/main/java/engine/listeners/Mouse.java @@ -174,11 +174,11 @@ public Point getMousePosToCam() { } private double scaledX(int x) { - return (x / ((double) (display.getWidth()) / Graphics.stdWidth)); + return (x / ((double) (display.getWidth()) / Graphics.getStdWidth())); } private double scaledY(int y) { - return (y / ((double) (display.getHeight()) / Graphics.stdHeight)); + return (y / ((double) (display.getHeight()) / Graphics.getStdHeight())); } @Override diff --git a/src/main/java/engine/mechanics/DropDownMenu.java b/src/main/java/engine/mechanics/DropDownMenu.java index e06c1d9..d69c534 100644 --- a/src/main/java/engine/mechanics/DropDownMenu.java +++ b/src/main/java/engine/mechanics/DropDownMenu.java @@ -53,7 +53,7 @@ public void logicLoop() { totalSize += dropDown.getEntityList().get(i).height; } - if (totalSize < Graphics.stdHeight) { + if (totalSize < Graphics.getStdHeight()) { //dropDown Button alignment dropDown.getEntityList().get(0).x = x; dropDown.getEntityList().get(0).y = y + menuButton.height; diff --git a/src/main/java/engine/rendering/Graphics.java b/src/main/java/engine/rendering/Graphics.java index 963f336..25dd216 100644 --- a/src/main/java/engine/rendering/Graphics.java +++ b/src/main/java/engine/rendering/Graphics.java @@ -22,10 +22,8 @@ public class Graphics { public static Graphics2D g; private static int xOffset = 0; private static int yOffset = 0; - public static double stdWidth = 1280d; - public static double stdHeight = 720d; - private static int dHeight = (int) stdHeight; - private static int dWidth = (int) stdHeight; + private static double stdWidth = 1280d; + private static double stdHeight = 720d; private static double scaleWidth = 1; private static double scaleHeight = 1; @@ -39,8 +37,6 @@ private Graphics() { */ public static void graphicsLoop() { Game.getDisplays().values().forEach(e -> { - dHeight = e.getHeight(); - dWidth = e.getWidth(); BufferStrategy bs = e.bs(); try { g = (Graphics2D) bs.getDrawGraphics(); @@ -52,9 +48,9 @@ public static void graphicsLoop() { RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(e.getCanvas().getBackground()); g.fillRect(0, 0, e.getWidth(), e.getHeight()); - g.scale(e.getWidth() / stdWidth, e.getHeight() / stdHeight); scaleWidth = e.getWidth() / stdWidth; scaleHeight = e.getHeight() / stdHeight; + g.scale(scaleWidth, scaleHeight); Scene s = Game.getScene(e.getAttachedScene()); if (s != null) { List queue = new ArrayList<>(s.getObjectList()); @@ -107,7 +103,7 @@ private static void renderChildren(List entities) { * @param x The x offset * @param y The y offset */ - public static void moveCam(int x, int y) { + public static void setCamPos(int x, int y) { xOffset = x; yOffset = y; } @@ -127,11 +123,33 @@ public static Point getCamPos() { return new Point(xOffset, yOffset); } - public static double getScaleHeight() { - return scaleHeight; + /** + * @return Returns the Standard width (Original window width) + */ + public static int getStdWidth() { + return (int) stdWidth; + } + + /** + * Sets the standard width of the window (changing could break scaling) + * @param stdWidth The new standard width + */ + public static void setStdWidth(int stdWidth) { + Graphics.stdWidth = stdWidth; } - public static double getScaleWidth() { - return scaleWidth; + /** + * @return Returns the Standard height (Original window height) + */ + public static int getStdHeight() { + return (int) stdHeight; + } + + /** + * Sets the standard height of the window (changing could break scaling) + * @param stdHeight The new standard height + */ + public static void setStdHeight(int stdHeight) { + Graphics.stdHeight = stdHeight; } }