chore(scene): cleanup main menu
This commit is contained in:
@ -61,12 +61,11 @@ public class Intro extends Scene {
|
|||||||
currentOpacity = 1 - ((float) (System.currentTimeMillis() - endTime) / 1000);
|
currentOpacity = 1 - ((float) (System.currentTimeMillis() - endTime) / 1000);
|
||||||
}
|
}
|
||||||
// Multiply with any externally applied alpha (e.g., Transition)
|
// Multiply with any externally applied alpha (e.g., Transition)
|
||||||
float externalAlpha = batch.getColor().a; // alpha set by Transition before calling render
|
float externalAlpha = batch.getColor().a;
|
||||||
float finalAlpha = currentOpacity * externalAlpha;
|
float finalAlpha = currentOpacity * externalAlpha;
|
||||||
batch.setColor(1, 1, 1, finalAlpha);
|
batch.setColor(1, 1, 1, finalAlpha);
|
||||||
batch.draw(logo, WINDOW_WIDTH / 2 - logo.getWidth() / 4, WINDOW_HEIGHT / 2 - logo.getHeight() / 4,
|
batch.draw(logo, WINDOW_WIDTH / 2 - logo.getWidth() / 4, WINDOW_HEIGHT / 2 - logo.getHeight() / 4,
|
||||||
logo.getWidth() / 2, logo.getHeight() / 2);
|
logo.getWidth() / 2, logo.getHeight() / 2);
|
||||||
// Reset color for safety (Transition will set again anyway)
|
batch.setColor(1, 1, 1, externalAlpha);
|
||||||
batch.setColor(1,1,1,externalAlpha);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,17 +2,17 @@ package org.vibecoders.moongazer.scenes;
|
|||||||
|
|
||||||
import static org.vibecoders.moongazer.Constants.*;
|
import static org.vibecoders.moongazer.Constants.*;
|
||||||
|
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
import org.vibecoders.moongazer.Game;
|
import org.vibecoders.moongazer.Game;
|
||||||
import org.vibecoders.moongazer.State;
|
import org.vibecoders.moongazer.State;
|
||||||
import org.vibecoders.moongazer.managers.Assets;
|
import org.vibecoders.moongazer.managers.Assets;
|
||||||
import org.vibecoders.moongazer.ui.UIImageButton;
|
|
||||||
import org.vibecoders.moongazer.ui.UITextButton;
|
import org.vibecoders.moongazer.ui.UITextButton;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.files.FileHandle;
|
import com.badlogic.gdx.files.FileHandle;
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||||
import com.badlogic.gdx.utils.TimeUtils;
|
|
||||||
import com.badlogic.gdx.video.VideoPlayer;
|
import com.badlogic.gdx.video.VideoPlayer;
|
||||||
import com.badlogic.gdx.video.VideoPlayerCreator;
|
import com.badlogic.gdx.video.VideoPlayerCreator;
|
||||||
|
|
||||||
@ -23,11 +23,6 @@ public class MainMenu extends Scene {
|
|||||||
private Texture titleTexture;
|
private Texture titleTexture;
|
||||||
private float titleY, titleX, titleWidth, titleHeight;
|
private float titleY, titleX, titleWidth, titleHeight;
|
||||||
private boolean videoPrepared = false;
|
private boolean videoPrepared = false;
|
||||||
private boolean firstFrameLogged = false;
|
|
||||||
private long videoStartTime;
|
|
||||||
|
|
||||||
private static final String WEBM_PATH = "videos/main_menu_background.webm";
|
|
||||||
private static final String OGV_PATH = "videos/main_menu_background.ogv";
|
|
||||||
|
|
||||||
public MainMenu(Game game) {
|
public MainMenu(Game game) {
|
||||||
super(game);
|
super(game);
|
||||||
@ -37,25 +32,17 @@ public class MainMenu extends Scene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initVideo() {
|
private void initVideo() {
|
||||||
try {
|
|
||||||
videoPlayer = VideoPlayerCreator.createVideoPlayer();
|
videoPlayer = VideoPlayerCreator.createVideoPlayer();
|
||||||
FileHandle webm = Gdx.files.internal(WEBM_PATH);
|
videoFileHandle = Assets.getAsset("videos/main_menu_background.webm", FileHandle.class);
|
||||||
FileHandle ogv = Gdx.files.internal(OGV_PATH);
|
try {
|
||||||
if (webm.exists()) {
|
videoPlayer.load(videoFileHandle);
|
||||||
videoFileHandle = webm;
|
} catch (FileNotFoundException e) {
|
||||||
log.info("Using menu background (WebM): {}", webm.path());
|
log.error("Failed to load video", e);
|
||||||
} else if (ogv.exists()) {
|
|
||||||
videoFileHandle = ogv;
|
|
||||||
log.info("Using menu background (OGV): {}", ogv.path());
|
|
||||||
} else {
|
|
||||||
log.warn("No background video found (expected {} or {}).", WEBM_PATH, OGV_PATH);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Cannot create VideoPlayer", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initUI() {
|
private void initUI() {
|
||||||
|
// Title
|
||||||
titleTexture = Assets.getAsset("textures/main_menu/title.png", Texture.class);
|
titleTexture = Assets.getAsset("textures/main_menu/title.png", Texture.class);
|
||||||
float targetTitleWidth = 500f;
|
float targetTitleWidth = 500f;
|
||||||
float scale = targetTitleWidth / titleTexture.getWidth();
|
float scale = targetTitleWidth / titleTexture.getWidth();
|
||||||
@ -64,6 +51,7 @@ public class MainMenu extends Scene {
|
|||||||
titleX = (WINDOW_WIDTH - titleWidth) / 2f;
|
titleX = (WINDOW_WIDTH - titleWidth) / 2f;
|
||||||
titleY = WINDOW_HEIGHT / 2f - titleHeight / 8f;
|
titleY = WINDOW_HEIGHT / 2f - titleHeight / 8f;
|
||||||
|
|
||||||
|
// Buttons
|
||||||
var font = Assets.getFont("ui", 24);
|
var font = Assets.getFont("ui", 24);
|
||||||
UITextButton playButton = new UITextButton("Play", font);
|
UITextButton playButton = new UITextButton("Play", font);
|
||||||
UITextButton loadButton = new UITextButton("Load", font);
|
UITextButton loadButton = new UITextButton("Load", font);
|
||||||
@ -82,10 +70,14 @@ public class MainMenu extends Scene {
|
|||||||
int startY = WINDOW_HEIGHT / 2 - buttonHeight / 2;
|
int startY = WINDOW_HEIGHT / 2 - buttonHeight / 2;
|
||||||
int spacing = 65;
|
int spacing = 65;
|
||||||
|
|
||||||
playButton.setSize(buttonWidth, buttonHeight); playButton.setPosition(centerX, startY);
|
playButton.setSize(buttonWidth, buttonHeight);
|
||||||
loadButton.setSize(buttonWidth, buttonHeight); loadButton.setPosition(centerX, startY - spacing);
|
playButton.setPosition(centerX, startY);
|
||||||
settingsButton.setSize(buttonWidth, buttonHeight); settingsButton.setPosition(centerX, startY - spacing * 2);
|
loadButton.setSize(buttonWidth, buttonHeight);
|
||||||
exitButton.setSize(buttonWidth, buttonHeight); exitButton.setPosition(centerX, startY - spacing * 3);
|
loadButton.setPosition(centerX, startY - spacing);
|
||||||
|
settingsButton.setSize(buttonWidth, buttonHeight);
|
||||||
|
settingsButton.setPosition(centerX, startY - spacing * 2);
|
||||||
|
exitButton.setSize(buttonWidth, buttonHeight);
|
||||||
|
exitButton.setPosition(centerX, startY - spacing * 3);
|
||||||
|
|
||||||
playButton.onClick(() -> log.debug("Play clicked"));
|
playButton.onClick(() -> log.debug("Play clicked"));
|
||||||
loadButton.onClick(() -> log.debug("Load clicked"));
|
loadButton.onClick(() -> log.debug("Load clicked"));
|
||||||
@ -108,56 +100,30 @@ public class MainMenu extends Scene {
|
|||||||
game.stage.addActor(root);
|
game.stage.addActor(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureVideoStarted() {
|
private void startVideoOnce() {
|
||||||
if (videoPlayer == null || videoPrepared) return;
|
if (videoPlayer == null || videoPrepared)
|
||||||
if (videoFileHandle == null || !videoFileHandle.exists()) return;
|
return;
|
||||||
if (game.transition == null && game.state != State.MAIN_MENU) return;
|
if (videoFileHandle == null || !videoFileHandle.exists())
|
||||||
startVideo();
|
return;
|
||||||
}
|
if (game.transition == null && game.state != State.MAIN_MENU)
|
||||||
|
return;
|
||||||
private void startVideo() {
|
|
||||||
try {
|
|
||||||
videoPlayer.setLooping(true);
|
videoPlayer.setLooping(true);
|
||||||
videoPlayer.play(videoFileHandle);
|
videoPlayer.play();
|
||||||
videoPrepared = true;
|
videoPrepared = true;
|
||||||
videoStartTime = TimeUtils.millis();
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Failed to play video: {}", videoFileHandle.path(), e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void forceStartVideo() { // used by Transition for early warm-up
|
|
||||||
if (!videoPrepared && videoFileHandle != null && videoFileHandle.exists()) startVideo();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(SpriteBatch batch) {
|
public void render(SpriteBatch batch) {
|
||||||
ensureVideoStarted();
|
startVideoOnce();
|
||||||
if (videoPlayer != null && videoPrepared) {
|
|
||||||
videoPlayer.update();
|
videoPlayer.update();
|
||||||
Texture videoTexture = videoPlayer.getTexture();
|
Texture videoTexture = videoPlayer.getTexture();
|
||||||
if (videoTexture != null) {
|
|
||||||
if (!firstFrameLogged) {
|
|
||||||
firstFrameLogged = true;
|
|
||||||
log.info("Menu video first frame in {} ms", TimeUtils.timeSinceMillis(videoStartTime));
|
|
||||||
}
|
|
||||||
batch.draw(videoTexture, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
batch.draw(videoTexture, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||||
} else {
|
|
||||||
batch.draw(Assets.getBlackTexture(), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
batch.draw(Assets.getBlackTexture(), 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
|
||||||
}
|
|
||||||
batch.draw(titleTexture, titleX, titleY, titleWidth, titleHeight);
|
batch.draw(titleTexture, titleX, titleY, titleWidth, titleHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateVideo() {
|
|
||||||
if (videoPlayer != null && videoPrepared) videoPlayer.update();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
super.dispose();
|
super.dispose();
|
||||||
if (videoPlayer != null) videoPlayer.dispose();
|
videoPlayer.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user