chore(button): refactor some

This commit is contained in:
2025-09-30 20:10:25 +07:00
parent 66ebb021e0
commit b2a455f3c2
9 changed files with 70 additions and 78 deletions

View File

@ -6,7 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import org.vibecoders.moongazer.managers.Assets;
import org.vibecoders.moongazer.ui.MenuButton;
import org.vibecoders.moongazer.ui.UITextButton;
import org.vibecoders.moongazer.Game;
import com.badlogic.gdx.graphics.Texture;
@ -22,10 +22,10 @@ public class MainMenu extends Scene {
titleTexture = Assets.getAsset("textures/main_menu/title.png", Texture.class);
var font = Assets.getFont("ui", 24);
MenuButton playButton = new MenuButton("Play", font);
MenuButton loadButton = new MenuButton("Load", font);
MenuButton settingsButton = new MenuButton("Settings", font);
MenuButton exitButton = new MenuButton("Exit", font);
UITextButton playButton = new UITextButton("Play", font);
UITextButton loadButton = new UITextButton("Load", font);
UITextButton settingsButton = new UITextButton("Settings", font);
UITextButton exitButton = new UITextButton("Exit", font);
int buttonWidth = 220;
int buttonHeight = 65;
@ -43,33 +43,10 @@ public class MainMenu extends Scene {
settingsButton.setPosition(centerX, startY - buttonSpacing * 2);
exitButton.setPosition(centerX, startY - buttonSpacing * 3);
playButton.addEventListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
log.debug("Play clicked");
}
});
loadButton.addEventListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
log.debug("Load clicked");
}
});
settingsButton.addEventListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
log.debug("Settings clicked");
}
});
exitButton.addEventListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
log.debug("Exit clicked");
}
});
playButton.onClick(() -> log.debug("Play clicked"));
loadButton.onClick(() -> log.debug("Load clicked"));
settingsButton.onClick(() -> log.debug("Settings clicked"));
exitButton.onClick(() -> log.debug("Exit clicked"));
root.addActor(playButton.getActor());
root.addActor(loadButton.getActor());

View File

@ -1,11 +0,0 @@
package org.vibecoders.moongazer.ui;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
public class IngameButton extends BaseButton {
private ImageButton imageButton;
public IngameButton() {
}
}

View File

@ -1,34 +0,0 @@
package org.vibecoders.moongazer.ui;
import org.vibecoders.moongazer.managers.Assets;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
public class MenuButton extends BaseButton {
private TextButton textButton;
private Texture buttonTexture;
public MenuButton(String text, BitmapFont font) {
buttonTexture = Assets.getAsset("textures/ui/menu_button.png", Texture.class);
TextureRegionDrawable buttonDrawable = new TextureRegionDrawable(new TextureRegion(buttonTexture));
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
style.font = font;
style.fontColor = Color.BLACK;
style.overFontColor = Color.BLUE;
style.downFontColor = Color.BLUE;
style.up = buttonDrawable;
style.down = buttonDrawable;
style.over = buttonDrawable;
textButton = new TextButton(text, style);
this.actor = textButton;
}
}

View File

@ -2,9 +2,11 @@ package org.vibecoders.moongazer.ui;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.EventListener;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
public abstract class BaseButton {
public abstract class UIButton {
protected Actor actor;
protected Button button;
public Actor getActor() {
return actor;
@ -21,4 +23,13 @@ public abstract class BaseButton {
public void addEventListener(EventListener eventListener) {
actor.addListener(eventListener);
}
public void onClick(Runnable action) {
button.addListener(new com.badlogic.gdx.scenes.scene2d.utils.ClickListener() {
@Override
public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) {
action.run();
}
});
}
}

View File

@ -0,0 +1,10 @@
package org.vibecoders.moongazer.ui;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
public class UIImageButton extends UIButton {
public UIImageButton() {
this.button = new ImageButton(new ImageButton.ImageButtonStyle());
this.actor = button;
}
}

View File

@ -0,0 +1,39 @@
package org.vibecoders.moongazer.ui;
import org.vibecoders.moongazer.managers.Assets;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
public class UITextButton extends UIButton {
private Texture buttonTexture;
private Texture buttonDownTexture;
private Texture buttonOverTexture;
public UITextButton(String text, BitmapFont font) {
buttonTexture = Assets.getAsset("textures/ui/menu_button.png", Texture.class);
buttonDownTexture = Assets.getAsset("textures/ui/menu_button_down.png", Texture.class);
buttonOverTexture = Assets.getAsset("textures/ui/menu_button_over.png", Texture.class);
TextureRegionDrawable buttonDrawable = new TextureRegionDrawable(new TextureRegion(buttonTexture));
TextureRegionDrawable buttonDownDrawable = new TextureRegionDrawable(new TextureRegion(buttonDownTexture));
TextureRegionDrawable buttonOverDrawable = new TextureRegionDrawable(new TextureRegion(buttonOverTexture));
TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
// Text style
style.font = font;
style.fontColor = Color.BLACK;
style.overFontColor = Color.BLACK;
style.downFontColor = Color.BLACK;
// Button style
style.up = buttonDrawable;
style.down = buttonDownDrawable;
style.over = buttonOverDrawable;
this.button = new TextButton(text, style);
this.actor = button;
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB