From ee6818234a28a63dca52f615d9fa57c7925e6149 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Tue, 20 Nov 2018 21:01:01 +0100 Subject: Refractor MapScene.render() AffineTransforms --- src/subconscious/graphics/BattleScene.java | 3 ++ src/subconscious/graphics/MapEditorScene.java | 3 ++ src/subconscious/graphics/MapScene.java | 45 ++++++++++++++++----------- 3 files changed, 32 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/subconscious/graphics/BattleScene.java b/src/subconscious/graphics/BattleScene.java index b1def18..0252dec 100644 --- a/src/subconscious/graphics/BattleScene.java +++ b/src/subconscious/graphics/BattleScene.java @@ -23,6 +23,7 @@ import java.awt.event.ActionListener; import java.awt.image.BufferStrategy; import java.awt.geom.Point2D; +import java.awt.geom.AffineTransform; import java.awt.geom.NoninvertibleTransformException; import javax.swing.JFrame; @@ -38,6 +39,8 @@ public class BattleScene extends MapScene implements ActionListener { NONE, ATTACK, MOVE }; + private AffineTransform tx = new AffineTransform(); + // TODO: refractor, make Points() private int previousX = -1; private int previousY = -1; diff --git a/src/subconscious/graphics/MapEditorScene.java b/src/subconscious/graphics/MapEditorScene.java index 17e3c0c..edf9323 100644 --- a/src/subconscious/graphics/MapEditorScene.java +++ b/src/subconscious/graphics/MapEditorScene.java @@ -20,6 +20,7 @@ import java.awt.event.ActionListener; import java.awt.image.BufferStrategy; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; +import java.awt.geom.AffineTransform; import java.awt.geom.NoninvertibleTransformException; import javax.swing.JFrame; @@ -34,6 +35,7 @@ import javax.swing.JCheckBox; import java.lang.Integer; + @SuppressWarnings("serial") public class MapEditorScene extends MapScene implements ActionListener { private Tile.Type placingTile = Tile.Type.CLEAR; @@ -41,6 +43,7 @@ public class MapEditorScene extends MapScene implements ActionListener { private ArrayList actorFields = new ArrayList<>(); private JFrame actorFrame; private boolean nukeActor = false; + private AffineTransform tx = new AffineTransform(); public MapEditorScene() { super(); diff --git a/src/subconscious/graphics/MapScene.java b/src/subconscious/graphics/MapScene.java index db12f63..a16fcf3 100644 --- a/src/subconscious/graphics/MapScene.java +++ b/src/subconscious/graphics/MapScene.java @@ -36,7 +36,11 @@ public abstract class MapScene extends Scene { // used for rendering protected int tileSize; protected int shorterCanvasLenght; - protected AffineTransform tx = new AffineTransform(); + + protected AffineTransform applyZoomTx = new AffineTransform(); + protected AffineTransform applyPanTx = new AffineTransform(); + protected AffineTransform undoZoomTx; + protected AffineTransform undoPanTx; // used for panning protected Point pan = new Point(0, 0); @@ -183,44 +187,47 @@ public abstract class MapScene extends Scene { g.setColor(Palette.BLACK); g.fillRect(0, 0, this.canvasSize.width, this.canvasSize.height); - // zoom + // update zoom transform if (this.zooming) { - Point2D p1 = new Point2D.Double(this.mouse.x, this.mouse.y); - Point2D p2 = null; + Point2D center = new Point2D.Double(this.mouse.x, this.mouse.y); + Point2D restore = null; try { - p2 = tx.inverseTransform(p1, null); + restore = this.applyZoomTx.inverseTransform(center, null); } catch (NoninvertibleTransformException ex) { ex.printStackTrace(); } - this.tx.setToIdentity(); - this.tx.translate(p1.getX(), p1.getY()); - this.tx.scale(this.zoom, this.zoom); - this.tx.translate(-p2.getX(), -p2.getY()); + this.applyZoomTx.setToIdentity(); + this.applyZoomTx.translate(center.getX(), center.getY()); + this.applyZoomTx.scale(this.zoom, this.zoom); + this.applyZoomTx.translate(-restore.getX(), -restore.getY()); this.zooming = false; } - // pan + // update pan transform if (this.panning) { - this.tx.translate(this.pan.x/this.zoom, this.pan.y/this.zoom); + this.applyPanTx.translate(this.pan.x/this.zoom, this.pan.y/this.zoom); this.panning = false; this.pan.move(0, 0); } - - g.transform(this.tx); - - this.renderTiles(g); - this.renderActors(g); - // Restore absolute coordinates + // update inverese transform try { - g.transform(this.tx.createInverse()); + this.undoPanTx = this.applyPanTx.createInverse(); + this.undoZoomTx = this.applyZoomTx.createInverse(); } catch (NoninvertibleTransformException ex) { ex.printStackTrace(); } - + + // draw + g.transform(this.applyZoomTx); + g.transform(this.applyPanTx); + this.renderTiles(g); + this.renderActors(g); + g.transform(this.undoPanTx); + g.transform(this.undoZoomTx); } @Override -- cgit v1.2.1