summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-12-02 23:49:00 +0100
committerNao Pross <naopross@thearcway.org>2018-12-02 23:49:00 +0100
commitd16019cd7bf604524d56d154559842c6353ea998 (patch)
tree009f583e17fd8133bbd169192f5134260f9a7618
parentUpdate makefile to make javac errors more verbose (diff)
downloadSubconscious-java-d16019cd7bf604524d56d154559842c6353ea998.tar.gz
Subconscious-java-d16019cd7bf604524d56d154559842c6353ea998.zip
Connect ActorInfo to mouse events to show info of actor under cursor
-rw-r--r--src/subconscious/Actor.java4
-rw-r--r--src/subconscious/graphics/MapScene.java40
-rw-r--r--src/subconscious/graphics/Scene.java1
-rw-r--r--src/subconscious/graphics/WorldScene.java36
-rw-r--r--src/subconscious/graphics/widget/ActorInfo.java9
5 files changed, 78 insertions, 12 deletions
diff --git a/src/subconscious/Actor.java b/src/subconscious/Actor.java
index 38e83ff..1c1f3ad 100644
--- a/src/subconscious/Actor.java
+++ b/src/subconscious/Actor.java
@@ -1,5 +1,8 @@
package subconscious;
+import java.awt.Point;
+
+
public class Actor {
private final String name;
@@ -39,6 +42,7 @@ public class Actor {
public String getName() { return this.name; }
+ public Point getPoint() { return new Point(this.x, this.y); }
public int getX() { return this.x; }
public int getY() { return this.y; }
diff --git a/src/subconscious/graphics/MapScene.java b/src/subconscious/graphics/MapScene.java
index 0fd2701..a198542 100644
--- a/src/subconscious/graphics/MapScene.java
+++ b/src/subconscious/graphics/MapScene.java
@@ -7,6 +7,7 @@ import subconscious.Actor;
import java.awt.Canvas;
import java.awt.Point;
+import java.awt.Rectangle;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.Dimension;
@@ -48,9 +49,11 @@ public abstract class MapScene extends Scene {
protected Point mouse = new Point(0, 0);
// used for zooming
+ protected double zoom = 1;
+
+ // flags to indicate if zoom or pan tx need to be updated
protected boolean zooming = false;
protected boolean panning = false;
- protected double zoom = 1;
public MapScene(Game g, String uniqueName) {
@@ -64,6 +67,28 @@ public abstract class MapScene extends Scene {
this.updateCanvasSize(this.canvasSize);
}
+
+ /* get the coordinates of the tile on the screen at position screenPos
+ * if there is not tile under screenPos the return value is null
+ */
+ public Point tileAtCoordinates(Point screenPos) {
+ Rectangle mapRect = new Rectangle(0, 0, this.map.getSize(), this.map.getSize());
+
+ // undo zoom
+ undoZoomTx.transform(screenPos, screenPos);
+
+ // undo translate
+ undoPanTx.transform(screenPos, screenPos);
+
+ screenPos.x /= this.tileSize;
+ screenPos.y /= this.tileSize;
+
+ if (!mapRect.contains(screenPos)) {
+ return null;
+ }
+
+ return screenPos;
+ }
protected void renderTiles(Graphics2D g) {
for (Tile tile : this.game.getMap().getGrid()) {
@@ -202,9 +227,11 @@ public abstract class MapScene extends Scene {
// update pan transform
if (this.panning) {
- this.applyPanTx.translate(this.pan.x/this.zoom, this.pan.y/this.zoom);
+ this.applyPanTx.translate(this.pan.x, this.pan.y);
this.panning = false;
this.pan.move(0, 0);
+
+ this.panning = false;
}
// update inverese transform
@@ -214,6 +241,7 @@ public abstract class MapScene extends Scene {
} catch (NoninvertibleTransformException ex) {
ex.printStackTrace();
}
+
// draw
g.transform(this.applyZoomTx);
@@ -278,12 +306,15 @@ public abstract class MapScene extends Scene {
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
+ // move the pan point by dx, dy (scaled with zoom)
this.pan.translate(
- e.getX() - this.mouse.x,
- e.getY() - this.mouse.y
+ (int) ((e.getX() - this.mouse.x)/this.zoom),
+ (int) ((e.getY() - this.mouse.y)/this.zoom)
);
+ // update mouse if zoom happens while panning
this.mouse.move(e.getX(), e.getY());
+
this.panning = true;
}
@@ -296,6 +327,7 @@ public abstract class MapScene extends Scene {
if (this.zoom > 0.3 || e.getWheelRotation() < 0) {
this.zoom -= ((double) e.getWheelRotation())/5;
}
+
this.zooming = true;
}
}
diff --git a/src/subconscious/graphics/Scene.java b/src/subconscious/graphics/Scene.java
index 1956c27..132704e 100644
--- a/src/subconscious/graphics/Scene.java
+++ b/src/subconscious/graphics/Scene.java
@@ -318,7 +318,6 @@ public abstract class Scene extends Panel
/* mouse listener */
@Override
public void mouseClicked(MouseEvent e) {
- // could be cached
for (Widget w : this.clickableWidgetsCache) {
((Clickable) w).mouseClick(e.getPoint());
}
diff --git a/src/subconscious/graphics/WorldScene.java b/src/subconscious/graphics/WorldScene.java
index 4a375fa..d2ea1db 100644
--- a/src/subconscious/graphics/WorldScene.java
+++ b/src/subconscious/graphics/WorldScene.java
@@ -1,26 +1,29 @@
package subconscious.graphics;
import subconscious.Game;
+import subconscious.Actor;
import subconscious.graphics.widget.ActorInfo;
import java.awt.Graphics2D;
+import java.awt.Point;
+import java.awt.event.MouseEvent;
@SuppressWarnings("serial")
public class WorldScene extends MapScene {
+ protected ActorInfo actorInfoWidget;
+
+
public WorldScene(Game game, String label) {
super(game, label);
}
@Override
public void build() {
- this.addWidget(
- new ActorInfo(
- "actorinfo", 0, 0
- )
- );
+ this.actorInfoWidget = new ActorInfo("actorinfo", 0, 0);
+ this.addWidget(this.actorInfoWidget);
}
@Override
@@ -32,4 +35,27 @@ public class WorldScene extends MapScene {
public void update(long deltaNanoTime) {
}
+
+ @Override
+ public void mouseMoved(MouseEvent e) {
+ super.mouseMoved(e);
+
+ Point tileUnderCursor = this.tileAtCoordinates(e.getPoint());
+ if (tileUnderCursor == null) {
+ return;
+ }
+
+ // System.out.println(this.map.getTile(tileUnderCursor.x, tileUnderCursor.y).type);
+
+ // update ActorInfo widget
+ this.actorInfoWidget.unsetActor();
+ for (Actor actor : this.map.getActors()) {
+ if (actor.getPoint().equals(tileUnderCursor)) {
+ this.actorInfoWidget.setActor(actor);
+ break;
+ }
+ }
+
+ // update tileType widget
+ }
} \ No newline at end of file
diff --git a/src/subconscious/graphics/widget/ActorInfo.java b/src/subconscious/graphics/widget/ActorInfo.java
index a43984b..a2b25ad 100644
--- a/src/subconscious/graphics/widget/ActorInfo.java
+++ b/src/subconscious/graphics/widget/ActorInfo.java
@@ -14,7 +14,7 @@ public class ActorInfo extends Widget {
public static final int BORDER = 10;
public static final int TEXT_LINES_SPACING = 5;
public static final int WIDTH = 250;
- public static final int HEIGHT = 300;
+ public static final int HEIGHT = 100;
protected Actor observingActor = null;
@@ -48,7 +48,8 @@ public class ActorInfo extends Widget {
}
// draw HP
- drawLineString(g, "HP", BORDER, postTitleY, 0);
+ drawLineString(g, "Name : " + observingActor.getName(), BORDER, postTitleY, 0);
+ drawLineString(g, "HP : " + Integer.toString(observingActor.getHP()), BORDER, postTitleY, 1);
// TODO: show actor properties
}
@@ -57,6 +58,10 @@ public class ActorInfo extends Widget {
this.observingActor = actor;
}
+ public void unsetActor() {
+ this.observingActor = null;
+ }
+
private void drawLineString(Graphics2D g, String text, int x, int y, int line) {
g.drawString(text, x,