Amélioration de l'effet de focus sur une Tile
This commit is contained in:
@@ -3,6 +3,7 @@ package fr.monkhanny.dorfromantik.game;
|
|||||||
import fr.monkhanny.dorfromantik.listeners.GameResizeListener;
|
import fr.monkhanny.dorfromantik.listeners.GameResizeListener;
|
||||||
import fr.monkhanny.dorfromantik.listeners.GameZoomListener;
|
import fr.monkhanny.dorfromantik.listeners.GameZoomListener;
|
||||||
import fr.monkhanny.dorfromantik.listeners.GameArrowKeyListener;
|
import fr.monkhanny.dorfromantik.listeners.GameArrowKeyListener;
|
||||||
|
import fr.monkhanny.dorfromantik.Options;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -172,9 +173,52 @@ public class Board extends JPanel{
|
|||||||
calculateAvailablePositions(newTile); // Calculer de nouvelles positions disponibles
|
calculateAvailablePositions(newTile); // Calculer de nouvelles positions disponibles
|
||||||
repaint(); // Re-dessiner le plateau
|
repaint(); // Re-dessiner le plateau
|
||||||
}
|
}
|
||||||
|
|
||||||
|
autoReFocus(newTile); // Réinitialiser le zoom et le déplacement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void autoReFocus(Tile newlyPlacedTile) {
|
||||||
|
if (Options.AUTO_FOCUS) {
|
||||||
|
// Récupérer les coordonnées de la nouvelle tuile
|
||||||
|
int newlyPlacedTileX = newlyPlacedTile.getXCoord();
|
||||||
|
int newlyPlacedTileY = newlyPlacedTile.getYCoord();
|
||||||
|
|
||||||
|
// Calculer les décalages nécessaires pour centrer la tuile
|
||||||
|
// Nous utilisons la largeur et la hauteur du panneau de jeu (getWidth et getHeight)
|
||||||
|
// Divisé par 2 pour centrer la nouvelle tuile dans la fenêtre.
|
||||||
|
int targetOffsetX = (int) ((getWidth() - newlyPlacedTile.getRadius() * 2) / 2 - newlyPlacedTileX);
|
||||||
|
int targetOffsetY = (int) ((getHeight() - newlyPlacedTile.getRadius() * 2) / 2 - newlyPlacedTileY);
|
||||||
|
|
||||||
|
TilePanningTransition panningTransition = new TilePanningTransition(this, targetOffsetX, targetOffsetY, 20, 20);
|
||||||
|
panningTransition.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getZoomFactor() {
|
||||||
|
return zoomFactor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setZoomFactor(double zoomFactor) {
|
||||||
|
this.zoomFactor = zoomFactor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOffsetX() {
|
||||||
|
return offsetX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOffsetX(int offsetX) {
|
||||||
|
this.offsetX = offsetX;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getOffsetY() {
|
||||||
|
return offsetY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOffsetY(int offsetY) {
|
||||||
|
this.offsetY = offsetY;
|
||||||
|
}
|
||||||
|
|
||||||
public void zoomIn() {
|
public void zoomIn() {
|
||||||
zoomFactor *= 1.1; // Augmenter le facteur de zoom
|
zoomFactor *= 1.1; // Augmenter le facteur de zoom
|
||||||
repaint();
|
repaint();
|
||||||
|
@@ -0,0 +1,28 @@
|
|||||||
|
package fr.monkhanny.dorfromantik.game;
|
||||||
|
|
||||||
|
import fr.monkhanny.dorfromantik.listeners.TilePanningActionListener;
|
||||||
|
|
||||||
|
import javax.swing.Timer;
|
||||||
|
|
||||||
|
public class TilePanningTransition {
|
||||||
|
private Board board;
|
||||||
|
private int targetOffsetX, targetOffsetY;
|
||||||
|
private int speed;
|
||||||
|
private int steps;
|
||||||
|
|
||||||
|
public TilePanningTransition(Board board, int targetOffsetX, int targetOffsetY, int speed, int steps) {
|
||||||
|
this.board = board;
|
||||||
|
this.targetOffsetX = targetOffsetX;
|
||||||
|
this.targetOffsetY = targetOffsetY;
|
||||||
|
this.speed = speed;
|
||||||
|
this.steps = steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
// Créer un listener d'animation
|
||||||
|
TilePanningActionListener listener = new TilePanningActionListener(board, targetOffsetX, targetOffsetY, speed, steps);
|
||||||
|
|
||||||
|
// Démarrer l'animation si aucune n'est en cours
|
||||||
|
listener.startAnimation();
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,86 @@
|
|||||||
|
package fr.monkhanny.dorfromantik.listeners;
|
||||||
|
|
||||||
|
import fr.monkhanny.dorfromantik.game.Board;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
public class TilePanningActionListener implements ActionListener {
|
||||||
|
private Board board;
|
||||||
|
private int targetOffsetX, targetOffsetY;
|
||||||
|
private int speed; // Vitesse de la transition
|
||||||
|
private int steps; // Nombre d'étapes pour l'animation
|
||||||
|
private Timer timer;
|
||||||
|
|
||||||
|
// Indicateur pour vérifier si l'animation est en cours
|
||||||
|
private static boolean isAnimating = false;
|
||||||
|
|
||||||
|
// Variables pour suivre l'état de l'animation
|
||||||
|
private int currentStep = 0;
|
||||||
|
|
||||||
|
public TilePanningActionListener(Board board, int targetOffsetX, int targetOffsetY, int speed, int steps) {
|
||||||
|
this.board = board;
|
||||||
|
this.targetOffsetX = targetOffsetX;
|
||||||
|
this.targetOffsetY = targetOffsetY;
|
||||||
|
this.speed = speed;
|
||||||
|
this.steps = steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
int currentOffsetX = board.getOffsetX();
|
||||||
|
int currentOffsetY = board.getOffsetY();
|
||||||
|
|
||||||
|
// Si l'animation a atteint la cible (exactement)
|
||||||
|
if (currentStep >= steps) {
|
||||||
|
board.setOffsetX(targetOffsetX);
|
||||||
|
board.setOffsetY(targetOffsetY);
|
||||||
|
stopAnimation();
|
||||||
|
} else {
|
||||||
|
// Calculer le delta pour chaque étape
|
||||||
|
int deltaX = (targetOffsetX - currentOffsetX) / (steps - currentStep);
|
||||||
|
int deltaY = (targetOffsetY - currentOffsetY) / (steps - currentStep);
|
||||||
|
|
||||||
|
// Appliquer la transition progressivement
|
||||||
|
board.setOffsetX(currentOffsetX + deltaX);
|
||||||
|
board.setOffsetY(currentOffsetY + deltaY);
|
||||||
|
board.repaint(); // Re-dessiner le plateau
|
||||||
|
|
||||||
|
// Augmenter le compteur d'étapes
|
||||||
|
currentStep++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Lancer l'animation
|
||||||
|
public void startAnimation() {
|
||||||
|
if (isAnimating) {
|
||||||
|
stopCurrentAnimation();
|
||||||
|
}
|
||||||
|
|
||||||
|
isAnimating = true;
|
||||||
|
|
||||||
|
// Réinitialiser les étapes
|
||||||
|
currentStep = 0;
|
||||||
|
|
||||||
|
// Créer et démarrer le timer pour l'animation
|
||||||
|
this.timer = new Timer(1000 / 60, this); // 60 FPS
|
||||||
|
timer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Arrêter l'animation proprement
|
||||||
|
private void stopAnimation() {
|
||||||
|
if (timer != null) {
|
||||||
|
timer.stop(); // Arrêter le timer
|
||||||
|
}
|
||||||
|
isAnimating = false; // Réinitialiser l'indicateur d'animation
|
||||||
|
}
|
||||||
|
|
||||||
|
// Annuler l'animation en cours
|
||||||
|
private void stopCurrentAnimation() {
|
||||||
|
if (timer != null) {
|
||||||
|
timer.stop(); // Arrêter le timer de l'animation en cours
|
||||||
|
}
|
||||||
|
isAnimating = false; // Réinitialiser l'animation
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user