diff --git a/download/mapping-1.1.jar b/download/mapping-1.1.1.jar similarity index 67% rename from download/mapping-1.1.jar rename to download/mapping-1.1.1.jar index efca413..4349c05 100644 Binary files a/download/mapping-1.1.jar and b/download/mapping-1.1.1.jar differ diff --git a/pom.xml b/pom.xml index c85ca64..681e503 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ fr.klemek mapping - 1.1 + 1.1.1 diff --git a/src/main/java/fr/klemek/mapping/MainPanel.java b/src/main/java/fr/klemek/mapping/MainPanel.java index 5d18200..b5fcce5 100644 --- a/src/main/java/fr/klemek/mapping/MainPanel.java +++ b/src/main/java/fr/klemek/mapping/MainPanel.java @@ -1,8 +1,6 @@ package fr.klemek.mapping; import java.awt.*; -import java.awt.event.MouseEvent; -import java.awt.event.MouseMotionListener; import javax.swing.*; @@ -13,10 +11,10 @@ class MainPanel extends JPanel { private float ratio = DEFAULT_RATIO; private transient Map map; - private int cx; - private int cy; - private float mw; - private float mh; + private int x0; + private int y0; + private float xstep; + private float ystep; private int sx = -1; private int sy = -1; @@ -29,45 +27,7 @@ class MainPanel extends JPanel { MainPanel(Map map) { this.map = map; - - this.addMouseMotionListener(new MouseMotionListener() { - @Override - public void mouseDragged(MouseEvent e) { - //ignored - } - - @Override - public void mouseMoved(MouseEvent e) { - sx = -1; - sy = -1; - if (MainPanel.this.xs != null) { - for (int x = 0; x < MainPanel.this.map.size(); x++) - for (int y = 0; y < MainPanel.this.map.size(); y++) - if (e.getPoint().distance(MainPanel.this.xs[x][y], MainPanel.this.ys[x][y]) < 6) { - sx = x; - sy = y; - return; - } - } - } - }); - this.addMouseWheelListener(e -> { - if (sx >= 0 && sy >= 0) { - if (e.isShiftDown()) { - for (int dx = -1; dx < 2; dx++) - for (int dy = -1; dy < 2; dy++) - if (sx + dx >= 0 && sx + dx < MainPanel.this.map.size() - && sy + dy >= 0 && sy + dy < MainPanel.this.map.size()) - MainPanel.this.map.set(sx + dx, sy + dy, MainPanel.this.map.get(sx + dx, sy + dy) - + (e.getPreciseWheelRotation() < 0 ? 1f : -1f) - * (e.isControlDown() ? 1f : .1f)); - } else { - MainPanel.this.map.set(sx, sy, MainPanel.this.map.get(sx, sy) - + (e.getPreciseWheelRotation() < 0 ? 1f : -1f) - * (e.isControlDown() ? 1f : .1f)); - } - } - }); + new MouseListener(this); } @Override @@ -77,56 +37,65 @@ class MainPanel extends JPanel { RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)); - int w = this.getWidth(); - int h = this.getHeight(); - this.cx = Math.round(w / 2f); - this.cy = Math.round(h / 2f); - if (w > h) { - this.mh = h * SIZE * ratio; - this.mw = mh / ratio; - } else { - this.mw = w * SIZE; - this.mh = mw * ratio; - } - - g2.clearRect(0, 0, w, h); - drawMap(g2); + g2.clearRect(0, 0, this.getWidth(), this.getHeight()); + this.computeNodes(); + if (this.showGrid) + this.drawGrid(g2); + this.drawMap(g2); } - private void drawMap(Graphics2D g2) { - g2.setStroke(new BasicStroke(1f)); + private void computeNodes() { + int w = this.getWidth(); + int h = this.getHeight(); - int x0 = Math.round(this.cx - this.mw / 2f); + float mw; + float mh; + if (w > h) { + mh = h * SIZE * ratio; + mw = mh / ratio; + } else { + mw = w * SIZE; + mh = mw * ratio; + } + + this.x0 = Math.round(w / 2f - mw / 2f); + this.y0 = Math.round(h / 2f); this.xs = new int[this.map.size()][this.map.size()]; this.ys = new int[this.map.size()][this.map.size()]; - float xstep = this.mw / (2 * (this.map.size() - 1)); - float ystep = this.mh / (2 * (this.map.size() - 1)); - - g2.setColor(Color.GRAY); + this.xstep = mw / (2 * (this.map.size() - 1)); + this.ystep = mh / (2 * (this.map.size() - 1)); for (int x = 0; x < this.map.size(); x++) { for (int y = 0; y < this.map.size(); y++) { - Point p = this.getPoint(x0, xstep, ystep, x, y, this.map.get(x, y)); - + Point p = this.getPoint(x, y, this.map.get(x, y)); this.xs[x][y] = p.x; this.ys[x][y] = p.y; - - if (this.showGrid) { - Point p0 = this.getPoint(x0, xstep, ystep, x, y, 0); - if (x > 0) { - Point p1 = this.getPoint(x0, xstep, ystep, x - 1, y, 0); - g2.drawLine(p0.x, p0.y, p1.x, p1.y); - } - if (y > 0) { - Point p1 = this.getPoint(x0, xstep, ystep, x, y - 1, 0); - g2.drawLine(p0.x, p0.y, p1.x, p1.y); - } - g2.drawLine(p0.x, p0.y, p.x, p.y); - } } } + } + + private void drawGrid(Graphics2D g2) { + g2.setColor(Color.GRAY); + for (int x = 0; x < this.map.size(); x++) { + for (int y = 0; y < this.map.size(); y++) { + Point p0 = this.getPoint(x, y, 0); + if (x > 0) { + Point p1 = this.getPoint(x - 1, y, 0); + g2.drawLine(p0.x, p0.y, p1.x, p1.y); + } + if (y > 0) { + Point p1 = this.getPoint(x, y - 1, 0); + g2.drawLine(p0.x, p0.y, p1.x, p1.y); + } + g2.drawLine(p0.x, p0.y, this.xs[x][y], this.ys[x][y]); + } + } + } + + private void drawMap(Graphics2D g2) { + for (int x = 0; x < this.map.size(); x++) { for (int y = 0; y < this.map.size(); y++) { @@ -136,15 +105,15 @@ class MainPanel extends JPanel { } } - private Point getPoint(int x0, float xstep, float ystep, int x, int y, float value) { + private Point getPoint(int x, int y, float value) { float center = this.map.size() / 2f; double r = Math.sqrt(Math.pow(x - center, 2) + Math.pow(y - center, 2)); double a = Math.atan2(y - center, x - center); double x1 = center + r * Math.cos(a + this.angle); double y1 = center + r * Math.sin(a + this.angle); return new Point( - Math.round(x0 + xstep * (float) x1 + xstep * (float) y1), - Math.round(this.cy - ystep * (float) x1 + ystep * (float) y1 - ystep * value) + Math.round(this.x0 + this.xstep * (float) x1 + this.xstep * (float) y1), + Math.round(this.y0 - this.ystep * (float) x1 + this.ystep * (float) y1 - this.ystep * value) ); } @@ -162,6 +131,39 @@ class MainPanel extends JPanel { } } + void computeMouseMoved(Point position) { + sx = -1; + sy = -1; + if (MainPanel.this.xs != null) { + for (int x = 0; x < MainPanel.this.map.size(); x++) + for (int y = 0; y < MainPanel.this.map.size(); y++) + if (position.distance(MainPanel.this.xs[x][y], MainPanel.this.ys[x][y]) < 6) { + sx = x; + sy = y; + return; + } + } + } + + void computeMouseWheel(int amount, boolean controlDown, boolean shiftDown) { + if (sx >= 0 && sy >= 0) { + if (shiftDown) { + for (int dx = -1; dx < 2; dx++) + for (int dy = -1; dy < 2; dy++) + changeNode(amount, controlDown, sx + dx, sy + dy); + } else + changeNode(amount, controlDown, sx, sy); + } + } + + private void changeNode(int amount, boolean controlDown, int x, int y) { + if (x >= 0 && x < this.map.size() + && y >= 0 && y < this.map.size()) + this.map.set(x, y, MainPanel.this.map.get(x, y) + + amount * (controlDown ? -1f : -.1f)); + } + + float getRatio() { return ratio; } diff --git a/src/main/java/fr/klemek/mapping/MouseListener.java b/src/main/java/fr/klemek/mapping/MouseListener.java new file mode 100644 index 0000000..16a455b --- /dev/null +++ b/src/main/java/fr/klemek/mapping/MouseListener.java @@ -0,0 +1,32 @@ +package fr.klemek.mapping; + +import java.awt.event.MouseEvent; +import java.awt.event.MouseMotionListener; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; + +public class MouseListener implements MouseMotionListener, MouseWheelListener { + + private MainPanel mp; + + MouseListener(MainPanel mp) { + this.mp = mp; + this.mp.addMouseWheelListener(this); + this.mp.addMouseMotionListener(this); + } + + @Override + public void mouseDragged(MouseEvent e) { + //ignored + } + + @Override + public void mouseMoved(MouseEvent e) { + this.mp.computeMouseMoved(e.getPoint()); + } + + @Override + public void mouseWheelMoved(MouseWheelEvent e) { + this.mp.computeMouseWheel(e.getWheelRotation(), e.isControlDown(), e.isShiftDown()); + } +}