Ajout d'effet sonnore dans le menu principal)
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package fr.monkhanny.dorfromantik.gui;
|
||||
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class ButtonHoverAnimationListener implements ActionListener {
|
||||
|
||||
private int step = 0;
|
||||
private final float scaleIncrement;
|
||||
private final boolean entering;
|
||||
private final JButton button;
|
||||
private final Color originalColor;
|
||||
private final Font originalFont;
|
||||
private float currentScale = 1.0f;
|
||||
|
||||
public ButtonHoverAnimationListener(boolean entering, JButton button, Color originalColor, Font originalFont) {
|
||||
this.entering = entering;
|
||||
this.button = button;
|
||||
this.originalColor = originalColor;
|
||||
this.originalFont = originalFont;
|
||||
this.scaleIncrement = (Options.HOVER_FONT_SCALE - 1.0f) / Options.ANIMATION_STEPS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
currentScale = entering ? (1.0f + step * scaleIncrement) : (Options.HOVER_FONT_SCALE - step * scaleIncrement);
|
||||
|
||||
button.setForeground(entering ? Options.BUTTON_HOVER_COLOR : originalColor);
|
||||
button.setFont(originalFont.deriveFont(originalFont.getSize2D() * currentScale));
|
||||
|
||||
step++;
|
||||
if (step >= Options.ANIMATION_STEPS) {
|
||||
((Timer) e.getSource()).stop();
|
||||
if (!entering) {
|
||||
button.setFont(originalFont); // Restore the original font
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
package fr.monkhanny.dorfromantik.controller;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.ButtonHoverAnimator;
|
||||
import fr.monkhanny.dorfromantik.utils.MusicPlayer;
|
||||
import fr.monkhanny.dorfromantik.enums.Sounds;
|
||||
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
public class ButtonHoverListener extends MouseAdapter {
|
||||
|
||||
private final ButtonHoverAnimator animator;
|
||||
|
||||
public ButtonHoverListener(ButtonHoverAnimator animator) {
|
||||
this.animator = animator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
animator.startAnimation(true);
|
||||
MusicPlayer.loadSound(Sounds.SOUNDS1); // Charge le son
|
||||
MusicPlayer.playSound(); // Joue le son
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
animator.startAnimation(false);
|
||||
}
|
||||
}
|
@@ -1,11 +1,9 @@
|
||||
package fr.monkhanny.dorfromantik.controller;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.ButtonPanel;
|
||||
import fr.monkhanny.dorfromantik.gui.ButtonHoverAnimator;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
|
||||
@@ -13,82 +11,21 @@ public class MainMenuMouseController {
|
||||
|
||||
private final ButtonPanel buttonPanel;
|
||||
|
||||
// Couleur et échelle au survol
|
||||
private static final Color BUTTON_HOVER_COLOR = new Color(70, 130, 180);
|
||||
private static final float HOVER_FONT_SCALE = 1.1f;
|
||||
private static final int ANIMATION_STEPS = 10;
|
||||
private static final int ANIMATION_DELAY = 15; // Délai entre chaque étape en ms
|
||||
|
||||
public MainMenuMouseController(ButtonPanel buttonPanel) {
|
||||
this.buttonPanel = buttonPanel;
|
||||
initMouseListeners();
|
||||
}
|
||||
|
||||
private void initMouseListeners() {
|
||||
buttonPanel.getNewGameButton().addMouseListener(new ButtonHoverListener(buttonPanel.getNewGameButton()));
|
||||
buttonPanel.getContinueGameButton().addMouseListener(new ButtonHoverListener(buttonPanel.getContinueGameButton()));
|
||||
buttonPanel.getHowToPlayButton().addMouseListener(new ButtonHoverListener(buttonPanel.getHowToPlayButton()));
|
||||
buttonPanel.getSettingsButton().addMouseListener(new ButtonHoverListener(buttonPanel.getSettingsButton()));
|
||||
buttonPanel.getExitButton().addMouseListener(new ButtonHoverListener(buttonPanel.getExitButton()));
|
||||
addButtonHoverListener(buttonPanel.getNewGameButton());
|
||||
addButtonHoverListener(buttonPanel.getContinueGameButton());
|
||||
addButtonHoverListener(buttonPanel.getHowToPlayButton());
|
||||
addButtonHoverListener(buttonPanel.getSettingsButton());
|
||||
addButtonHoverListener(buttonPanel.getExitButton());
|
||||
}
|
||||
|
||||
// Classe interne pour la gestion des événements de survol de la souris avec animation
|
||||
private static class ButtonHoverListener extends MouseAdapter {
|
||||
private final JButton button;
|
||||
private final Color originalColor;
|
||||
private final Font originalFont;
|
||||
private Timer animationTimer;
|
||||
private boolean isHovered = false;
|
||||
private float currentScale = 1.0f;
|
||||
|
||||
public ButtonHoverListener(JButton button) {
|
||||
this.button = button;
|
||||
this.originalColor = button.getForeground();
|
||||
this.originalFont = button.getFont();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
isHovered = true;
|
||||
startAnimation(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
isHovered = false;
|
||||
startAnimation(false);
|
||||
}
|
||||
|
||||
private void startAnimation(boolean entering) {
|
||||
if (animationTimer != null && animationTimer.isRunning()) {
|
||||
animationTimer.stop();
|
||||
}
|
||||
|
||||
animationTimer = new Timer(ANIMATION_DELAY, new ActionListener() {
|
||||
int step = 0;
|
||||
final float scaleIncrement = (HOVER_FONT_SCALE - 1.0f) / ANIMATION_STEPS;
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
// Calculer la nouvelle échelle du texte
|
||||
currentScale = entering ? (1.0f + step * scaleIncrement) : (HOVER_FONT_SCALE - step * scaleIncrement);
|
||||
|
||||
// Mettre à jour la couleur et la police
|
||||
button.setForeground(entering ? BUTTON_HOVER_COLOR : originalColor);
|
||||
button.setFont(originalFont.deriveFont(originalFont.getSize2D() * currentScale));
|
||||
|
||||
// Augmenter le compteur d'étapes
|
||||
step++;
|
||||
if (step >= ANIMATION_STEPS) {
|
||||
animationTimer.stop();
|
||||
if (!entering) {
|
||||
// Restaurer la police d'origine après l'animation de sortie
|
||||
button.setFont(originalFont);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
animationTimer.start();
|
||||
}
|
||||
private void addButtonHoverListener(JButton button) {
|
||||
ButtonHoverAnimator animator = new ButtonHoverAnimator(button);
|
||||
button.addMouseListener(new ButtonHoverListener(animator));
|
||||
}
|
||||
}
|
||||
|
@@ -1,31 +1,19 @@
|
||||
package fr.monkhanny.dorfromantik.controller;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
|
||||
public class MainMenuResizeController {
|
||||
|
||||
private MainMenu mainMenu;
|
||||
private MainMenuResizeHandler resizeHandler;
|
||||
|
||||
public MainMenuResizeController(MainMenu mainMenu) {
|
||||
this.mainMenu = mainMenu;
|
||||
this.resizeHandler = new MainMenuResizeHandler(mainMenu);
|
||||
addComponentListener();
|
||||
}
|
||||
|
||||
private void addComponentListener() {
|
||||
mainMenu.addComponentListener(new ComponentAdapter() {
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
// Ajuster la taille de la police du titre en fonction de la taille de la fenêtre
|
||||
float newFontSize = Options.BASE_TITLE_FONT_SIZE * (mainMenu.getWidth() / 900f);
|
||||
mainMenu.getTitleLabel().updateTitleFont(newFontSize);
|
||||
|
||||
// Mettre à jour les polices des boutons
|
||||
mainMenu.getButtonPanel().updateButtonFonts(mainMenu.getWidth());
|
||||
}
|
||||
});
|
||||
mainMenu.addComponentListener(resizeHandler);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,26 @@
|
||||
package fr.monkhanny.dorfromantik.controller;
|
||||
|
||||
import fr.monkhanny.dorfromantik.gui.MainMenu;
|
||||
import fr.monkhanny.dorfromantik.Options;
|
||||
|
||||
import java.awt.event.ComponentAdapter;
|
||||
import java.awt.event.ComponentEvent;
|
||||
|
||||
public class MainMenuResizeHandler extends ComponentAdapter {
|
||||
|
||||
private MainMenu mainMenu;
|
||||
|
||||
public MainMenuResizeHandler(MainMenu mainMenu) {
|
||||
this.mainMenu = mainMenu;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void componentResized(ComponentEvent e) {
|
||||
// Ajuster la taille de la police du titre en fonction de la taille de la fenêtre
|
||||
float newFontSize = Options.BASE_TITLE_FONT_SIZE * (mainMenu.getWidth() / 900f);
|
||||
mainMenu.getTitleLabel().updateTitleFont(newFontSize);
|
||||
|
||||
// Mettre à jour les polices des boutons
|
||||
mainMenu.getButtonPanel().updateButtonFonts(mainMenu.getWidth());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user