feat: input handling!

This commit is contained in:
Martin Prokoph
2025-06-27 23:22:34 +02:00
parent 755599b4f3
commit b1be4e0731
6 changed files with 147 additions and 29 deletions

View File

@@ -1,5 +1,6 @@
package eu.midnightdust.yaytris.ui;
import eu.midnightdust.yaytris.Tetris;
import eu.midnightdust.yaytris.game.Space;
import javax.imageio.ImageIO;
@@ -7,11 +8,13 @@ import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import static eu.midnightdust.yaytris.Settings.guiScale;
public class TetrisUI extends JFrame {
public class TetrisUI extends JFrame implements KeyListener {
JLabel titleLabel;
GameCanvas gamePanel;
JPanel menuPanel;
@@ -44,6 +47,7 @@ public class TetrisUI extends JFrame {
this.add(gamePanel);
rescale();
this.addKeyListener(this);
openMainMenu(null);
this.setVisible(true);
@@ -65,6 +69,13 @@ public class TetrisUI extends JFrame {
//if (label.getFont() != null) label.setFont(label.getFont().deriveFont((float) label.getFont().getSize() * guiScale));
}
public void startGame(ActionEvent actionEvent) {
//this.remove(menuPanel);
//menuPanel = null;
this.requestFocus();
this.repaint();
}
public void openMainMenu(ActionEvent actionEvent) {
if (this.menuPanel != null) this.remove(menuPanel);
rescale();
@@ -85,22 +96,19 @@ public class TetrisUI extends JFrame {
}
// Source: https://stackoverflow.com/a/19746437
private void setWindowPosition(JFrame window, int screen)
{
private void setWindowPosition(JFrame window, int screen) {
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] allDevices = env.getScreenDevices();
int topLeftX, topLeftY, screenX, screenY, windowPosX, windowPosY;
if (screen < allDevices.length && screen > -1)
{
if (screen < allDevices.length && screen > -1) {
topLeftX = allDevices[screen].getDefaultConfiguration().getBounds().x;
topLeftY = allDevices[screen].getDefaultConfiguration().getBounds().y;
screenX = allDevices[screen].getDefaultConfiguration().getBounds().width;
screenY = allDevices[screen].getDefaultConfiguration().getBounds().height;
}
else
{
else {
topLeftX = allDevices[0].getDefaultConfiguration().getBounds().x;
topLeftY = allDevices[0].getDefaultConfiguration().getBounds().y;
@@ -113,4 +121,41 @@ public class TetrisUI extends JFrame {
window.setLocation(windowPosX, windowPosY);
}
@Override
public void keyTyped(KeyEvent e) {
//System.out.println("Typed");
}
@Override
public void keyPressed(KeyEvent e) {
//System.out.println("Pressed");
if (e.getKeyCode() == KeyEvent.VK_W) {
Tetris.space.spawnTetromino();
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
else if (e.getKeyCode() == KeyEvent.VK_S) {
Tetris.space.getCurrentTetromino().rotate();
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
else if (e.getKeyCode() == KeyEvent.VK_D) {
Tetris.space.getCurrentTetromino().move(1);
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
else if (e.getKeyCode() == KeyEvent.VK_A) {
Tetris.space.getCurrentTetromino().move(-1);
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
Tetris.space.getCurrentTetromino().fall(1);
//Tetris.space.onLinesChanged(Tetris.space.getCurrentTetromino(), 0, 1, 2, 3);
}
gamePanel.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
//System.out.println("Released");
}
}