aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <naopross@thearcway.org>2018-02-10 19:40:08 +0100
committerNao Pross <naopross@thearcway.org>2018-02-10 19:40:08 +0100
commit221aec874940378f5d8b6411e8b5c141816d5a1e (patch)
tree4ca7cb54583d50666d4e17230a505739813a3ab5
parentAdd missing @Override(s) (diff)
downloadSubconscious-old-221aec874940378f5d8b6411e8b5c141816d5a1e.tar.gz
Subconscious-old-221aec874940378f5d8b6411e8b5c141816d5a1e.zip
Add simple mouseclick listener to scenes
-rw-r--r--src/main/java/Scene.java8
-rw-r--r--src/main/java/Subconscious.java23
-rw-r--r--src/main/java/Tile.java2
-rw-r--r--src/main/java/WorldScene.java39
4 files changed, 51 insertions, 21 deletions
diff --git a/src/main/java/Scene.java b/src/main/java/Scene.java
index a2949fd..0d8a908 100644
--- a/src/main/java/Scene.java
+++ b/src/main/java/Scene.java
@@ -1,5 +1,11 @@
import java.awt.Graphics;
public abstract class Scene {
- abstract void render(Graphics g);
+ protected int sceneYOffset = 0;
+ protected int sceneXOffset = 0;
+
+ abstract public void render(Graphics g);
+
+ public void mouseHover(int x, int y) {}
+ public void mouseClicked(int x, int y) {}
}
diff --git a/src/main/java/Subconscious.java b/src/main/java/Subconscious.java
index 3e66800..d462385 100644
--- a/src/main/java/Subconscious.java
+++ b/src/main/java/Subconscious.java
@@ -42,21 +42,23 @@ public class Subconscious
public Subconscious() {
createWindow();
+ // setup component
+ addMouseListener(this);
+ addKeyListener(this);
+ addComponentListener(this);
setFocusable(true);
requestFocus();
// TODO remove demo
currentScene = new WorldScene(new Dimension(200, 200), 50);
- addMouseListener(this);
- addKeyListener(this);
- addComponentListener(this);
}
private void createWindow() {
window = new JFrame(TITLE);
window.setSize(new Dimension(800, 600));
window.setMinimumSize(getSize());
- window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.add(this);
+ window.setLocationRelativeTo(null); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ window.add(this);
}
/* game logic */
@@ -99,19 +101,15 @@ public class Subconscious
while (running) {
gameLoop();
gameRender();
- repaint();
-
+ repaint();
// could be better with NanoSeconds, but this is
// probably enough for this
afterTime = System.currentTimeMillis();
sleepTime = RENDER_PERIOD_MS - (beforeTime - afterTime);
try {
- // TODO replace with correct timing
Thread.sleep(sleepTime);
- } catch (InterruptedException ex) {
- //
- }
+ } catch (InterruptedException ex) {}
beforeTime = System.currentTimeMillis();
}
@@ -127,7 +125,7 @@ public class Subconscious
g.drawImage(dbImage, 0, 0, null);
}
- Toolkit.getDefaultToolkit().sync();
+ Toolkit.getDefaultToolkit().sync(); // needed on linux
g.dispose();
}
@@ -137,8 +135,7 @@ public class Subconscious
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
@Override public void mouseClicked(MouseEvent e) {
- int x = e.getX();
- int y = e.getY();
+ currentScene.mouseClicked(e.getX(), e.getY());
}
@Override public void keyPressed(KeyEvent e) {}
diff --git a/src/main/java/Tile.java b/src/main/java/Tile.java
index 1a95820..a5e0daa 100644
--- a/src/main/java/Tile.java
+++ b/src/main/java/Tile.java
@@ -7,6 +7,8 @@ public class Tile {
public final int x, y;
public final Type type;
+ public boolean selected = false;
+
public Tile(Type type, int x, int y) {
this.type = type;
this.x = x;
diff --git a/src/main/java/WorldScene.java b/src/main/java/WorldScene.java
index 2145234..829afbb 100644
--- a/src/main/java/WorldScene.java
+++ b/src/main/java/WorldScene.java
@@ -25,6 +25,13 @@ public class WorldScene extends Scene {
this.actors.add(player);
}
+ private Composite makeAlpha(Graphics2D g2d, float alpha) {
+ Composite originalComposite = g2d.getComposite();
+ g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
+
+ return originalComposite;
+ }
+
@Override
public void render(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
@@ -46,12 +53,19 @@ public class WorldScene extends Scene {
this.tileSize, this.tileSize
);
- // draw grid
- // composite (enable alpha)
- Composite originalComposite = g2d.getComposite();
- g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));
+ Composite originalComposite = makeAlpha(g2d, .5f);
+
+ if (tile.selected) {
+ g2d.setColor(Palette.RED);
+ g2d.fillRect(
+ this.tileSize * tile.x,
+ this.tileSize * tile.y,
+ this.tileSize, this.tileSize
+ );
+ }
- g2d.setPaint(Color.BLACK);
+ // draw grid (with composite)
+ g2d.setPaint(Palette.BLACK);
g2d.drawRect(
this.tileSize * tile.x,
this.tileSize * tile.y,
@@ -71,12 +85,23 @@ public class WorldScene extends Scene {
g2d.setColor(Palette.RED);
break;
}
- int gap = this.tileSize/10;
+ int gap = this.tileSize / 10;
g2d.fillRect(
(this.tileSize * actor.x) + gap,
(this.tileSize * actor.y) + gap,
- this.tileSize - gap*2, this.tileSize - gap*2
+ this.tileSize - gap * 2, this.tileSize - gap * 2
);
}
}
+
+ @Override
+ public void mouseClicked(int x, int y) {
+ Tile tile = map.getTile(
+ (int) (x/(double)this.tileSize) + sceneXOffset,
+ (int) (y/(double)this.tileSize) + sceneYOffset
+ );
+
+ // TODO find clicked actor
+ tile.selected = !tile.selected;
+ }
}