chore(ui/button): refactor a little bit
This commit is contained in:
@ -88,6 +88,7 @@ public class Assets {
|
|||||||
// Load all assets here
|
// Load all assets here
|
||||||
assetManager.load("textures/main_menu/background.png", Texture.class);
|
assetManager.load("textures/main_menu/background.png", Texture.class);
|
||||||
assetManager.load("textures/main_menu/title.png", Texture.class);
|
assetManager.load("textures/main_menu/title.png", Texture.class);
|
||||||
|
assetManager.load("textures/ui/menu_button.png", Texture.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isLoadedAll() {
|
public static boolean isLoadedAll() {
|
||||||
|
|||||||
@ -3,34 +3,25 @@ package org.vibecoders.moongazer.scenes;
|
|||||||
import static org.vibecoders.moongazer.Constants.*;
|
import static org.vibecoders.moongazer.Constants.*;
|
||||||
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
||||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
|
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
|
||||||
import org.vibecoders.moongazer.buttons.MenuButton;
|
|
||||||
import org.vibecoders.moongazer.managers.Assets;
|
import org.vibecoders.moongazer.managers.Assets;
|
||||||
|
import org.vibecoders.moongazer.ui.MenuButton;
|
||||||
import org.vibecoders.moongazer.Game;
|
import org.vibecoders.moongazer.Game;
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Color;
|
|
||||||
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.scenes.scene2d.ui.Label;
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
|
|
||||||
|
|
||||||
public class MainMenu extends Scene {
|
public class MainMenu extends Scene {
|
||||||
private Label newGameLabel;
|
|
||||||
private Label loadGameLabel;
|
|
||||||
private Label settingsLabel;
|
|
||||||
private Label quitGameLabel;
|
|
||||||
|
|
||||||
private Texture backgroundTexture;
|
private Texture backgroundTexture;
|
||||||
private Texture titleTexture;
|
private Texture titleTexture;
|
||||||
|
|
||||||
public MainMenu(Game game) {
|
public MainMenu(Game game) {
|
||||||
super(game);
|
super(game);
|
||||||
var font = Assets.getFont("ui", 24);
|
backgroundTexture = Assets.getAsset("textures/main_menu/background.png", Texture.class);
|
||||||
textLabel = new Label("Moongazer", new LabelStyle(font, Color.BLACK));
|
titleTexture = Assets.getAsset("textures/main_menu/title.png", Texture.class);
|
||||||
textLabel.setPosition(WINDOW_WIDTH / 2f - textLabel.getWidth() / 2f, WINDOW_HEIGHT / 2f - textLabel.getHeight() / 2f);
|
|
||||||
|
|
||||||
|
var font = Assets.getFont("ui", 24);
|
||||||
MenuButton playButton = new MenuButton("Play", font);
|
MenuButton playButton = new MenuButton("Play", font);
|
||||||
MenuButton loadButton = new MenuButton("Load", font);
|
MenuButton loadButton = new MenuButton("Load", font);
|
||||||
MenuButton settingsButton = new MenuButton("Settings", font);
|
MenuButton settingsButton = new MenuButton("Settings", font);
|
||||||
@ -52,45 +43,44 @@ public class MainMenu extends Scene {
|
|||||||
settingsButton.setPosition(centerX, startY - buttonSpacing * 2);
|
settingsButton.setPosition(centerX, startY - buttonSpacing * 2);
|
||||||
exitButton.setPosition(centerX, startY - buttonSpacing * 3);
|
exitButton.setPosition(centerX, startY - buttonSpacing * 3);
|
||||||
|
|
||||||
playButton.getActor().addListener(new ClickListener() {
|
playButton.addEventListener(new ClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void clicked(InputEvent event, float x, float y) {
|
public void clicked(InputEvent event, float x, float y) {
|
||||||
System.out.println("Play clicked");
|
log.debug("Play clicked");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
loadButton.getActor().addListener(new ClickListener() {
|
loadButton.addEventListener(new ClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void clicked(InputEvent event, float x, float y) {
|
public void clicked(InputEvent event, float x, float y) {
|
||||||
System.out.println("Load clicked");
|
log.debug("Load clicked");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
settingsButton.getActor().addListener(new ClickListener() {
|
settingsButton.addEventListener(new ClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void clicked(InputEvent event, float x, float y) {
|
public void clicked(InputEvent event, float x, float y) {
|
||||||
System.out.println("Settings clicked");
|
log.debug("Settings clicked");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
exitButton.getActor().addListener(new ClickListener() {
|
exitButton.addEventListener(new ClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void clicked(InputEvent event, float x, float y) {
|
public void clicked(InputEvent event, float x, float y) {
|
||||||
System.out.println("Exit clicked");
|
log.debug("Exit clicked");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
game.root.addActor(textLabel);
|
root.addActor(playButton.getActor());
|
||||||
game.root.addActor(playButton.getActor());
|
root.addActor(loadButton.getActor());
|
||||||
game.root.addActor(loadButton.getActor());
|
root.addActor(settingsButton.getActor());
|
||||||
game.root.addActor(settingsButton.getActor());
|
root.addActor(exitButton.getActor());
|
||||||
game.root.addActor(exitButton.getActor());
|
game.stage.addActor(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render(SpriteBatch batch) {
|
public void render(SpriteBatch batch) {
|
||||||
batch.draw(backgroundTexture, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
batch.draw(backgroundTexture, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||||
|
|
||||||
float targetTitleWidth = 300f;
|
float targetTitleWidth = 300f;
|
||||||
float originalWidth = titleTexture.getWidth();
|
float originalWidth = titleTexture.getWidth();
|
||||||
float originalHeight = titleTexture.getHeight();
|
float originalHeight = titleTexture.getHeight();
|
||||||
@ -100,10 +90,5 @@ public class MainMenu extends Scene {
|
|||||||
float titleX = (WINDOW_WIDTH - titleWidth) / 2f;
|
float titleX = (WINDOW_WIDTH - titleWidth) / 2f;
|
||||||
float titleY = WINDOW_HEIGHT * 0.58f;
|
float titleY = WINDOW_HEIGHT * 0.58f;
|
||||||
batch.draw(titleTexture, titleX, titleY, titleWidth, titleHeight);
|
batch.draw(titleTexture, titleX, titleY, titleWidth, titleHeight);
|
||||||
|
|
||||||
newGameLabel.draw(batch, 1.0f);
|
|
||||||
loadGameLabel.draw(batch, 1.0f);
|
|
||||||
settingsLabel.draw(batch, 1.0f);
|
|
||||||
quitGameLabel.draw(batch, 1.0f);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package org.vibecoders.moongazer.buttons;
|
package org.vibecoders.moongazer.ui;
|
||||||
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.Actor;
|
import com.badlogic.gdx.scenes.scene2d.Actor;
|
||||||
import com.badlogic.gdx.scenes.scene2d.EventListener;
|
import com.badlogic.gdx.scenes.scene2d.EventListener;
|
||||||
@ -14,11 +14,11 @@ public abstract class BaseButton {
|
|||||||
actor.setPosition(x, y);
|
actor.setPosition(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSize(int width, int height) {
|
public void setSize(int width, int height) {
|
||||||
actor.setSize(width, height);
|
actor.setSize(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void eventListener(EventListener eventListener) {
|
public void addEventListener(EventListener eventListener) {
|
||||||
actor.addListener(eventListener);
|
actor.addListener(eventListener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package org.vibecoders.moongazer.buttons;
|
package org.vibecoders.moongazer.ui;
|
||||||
|
|
||||||
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
|
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
|
||||||
|
|
||||||
@ -1,4 +1,6 @@
|
|||||||
package org.vibecoders.moongazer.buttons;
|
package org.vibecoders.moongazer.ui;
|
||||||
|
|
||||||
|
import org.vibecoders.moongazer.managers.Assets;
|
||||||
|
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.graphics.Color;
|
import com.badlogic.gdx.graphics.Color;
|
||||||
@ -10,9 +12,10 @@ import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
|
|||||||
|
|
||||||
public class MenuButton extends BaseButton {
|
public class MenuButton extends BaseButton {
|
||||||
private TextButton textButton;
|
private TextButton textButton;
|
||||||
|
private Texture buttonTexture;
|
||||||
|
|
||||||
public MenuButton(String text, BitmapFont font) {
|
public MenuButton(String text, BitmapFont font) {
|
||||||
Texture buttonTexture = new Texture(Gdx.files.internal("icons/MenuIcon.png"));
|
buttonTexture = Assets.getAsset("textures/ui/menu_button.png", Texture.class);
|
||||||
TextureRegionDrawable buttonDrawable = new TextureRegionDrawable(new TextureRegion(buttonTexture));
|
TextureRegionDrawable buttonDrawable = new TextureRegionDrawable(new TextureRegion(buttonTexture));
|
||||||
|
|
||||||
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
|
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
|
||||||
|
Before Width: | Height: | Size: 29 KiB After Width: | Height: | Size: 29 KiB |
Reference in New Issue
Block a user