feat: add button

This commit is contained in:
GiaQuang
2025-09-30 16:19:43 +07:00
parent 2e5e260a0a
commit f36fbb9c1e
4 changed files with 77 additions and 1 deletions

View File

@ -0,0 +1,24 @@
package org.vibecoders.moongazer.buttons;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.EventListener;
public abstract class BaseButton {
protected Actor actor;
public Actor getActor() {
return actor;
}
public void setPosition(int x, int y) {
actor.setPosition(x, y);
}
public void setSize(int width, int height) {
actor.setSize(width, height);
}
public void eventListener(EventListener eventListener) {
actor.addListener(eventListener);
}
}

View File

@ -0,0 +1,46 @@
package org.vibecoders.moongazer.buttons;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
public class MenuButton extends BaseButton {
private TextButton textButton;
public MenuButton(String text, BitmapFont font) {
this(text, font, Color.WHITE, Color.YELLOW);
}
public MenuButton(String text, BitmapFont font, Color normalColor, Color hoverColor) {
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
style.font = font;
style.fontColor = normalColor;
style.overFontColor = hoverColor;
style.downFontColor = hoverColor;
// Transparent backgrounds
style.up = null;
style.down = null;
style.over = null;
style.checked = null;
textButton = new TextButton(text, style);
// Add the text button as an actor to this button
this.actor = textButton;
}
public TextButton getTextButton() {
return textButton;
}
public void setText(String text) {
textButton.setText(text);
}
public String getText() {
return textButton.getText().toString();
}
}

View File

@ -2,6 +2,9 @@ package org.vibecoders.moongazer.scenes;
import static org.vibecoders.moongazer.Constants.*; import static org.vibecoders.moongazer.Constants.*;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import org.vibecoders.moongazer.buttons.MenuButton;
import org.vibecoders.moongazer.managers.Assets; import org.vibecoders.moongazer.managers.Assets;
import org.vibecoders.moongazer.Game; import org.vibecoders.moongazer.Game;
@ -22,6 +25,9 @@ public class MainMenu extends Scene {
textLabel = new Label("Moongazer", new LabelStyle(font, Color.BLACK)); textLabel = new Label("Moongazer", new LabelStyle(font, Color.BLACK));
textLabel.setPosition(WINDOW_WIDTH / 2f - textLabel.getWidth() / 2f, WINDOW_HEIGHT / 2f - textLabel.getHeight() / 2f); textLabel.setPosition(WINDOW_WIDTH / 2f - textLabel.getWidth() / 2f, WINDOW_HEIGHT / 2f - textLabel.getHeight() / 2f);
game.root.addActor(textLabel); game.root.addActor(textLabel);
MenuButton startButton = new MenuButton("Start", font);
startButton.getActor().setPosition(WINDOW_WIDTH / 2f, WINDOW_HEIGHT / 2f);
game.root.addActor(startButton.getActor());
} }
/** /**
* Renders the main menu scene. * Renders the main menu scene.
@ -29,7 +35,7 @@ public class MainMenu extends Scene {
*/ */
@Override @Override
public void render(SpriteBatch batch) { public void render(SpriteBatch batch) {
batch.draw(TEXTURE_WHITE, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); batch.draw(TEXTURE_BLACK, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
// Unneeded as using Scene2D Stage to render the label // Unneeded as using Scene2D Stage to render the label
// textLabel.draw(batch, 1.0f); // textLabel.draw(batch, 1.0f);
} }

0
gradlew vendored Normal file → Executable file
View File