chore(button): refactor some
This commit is contained in:
@ -6,7 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
|||||||
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
|
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
|
||||||
|
|
||||||
import org.vibecoders.moongazer.managers.Assets;
|
import org.vibecoders.moongazer.managers.Assets;
|
||||||
import org.vibecoders.moongazer.ui.MenuButton;
|
import org.vibecoders.moongazer.ui.UITextButton;
|
||||||
import org.vibecoders.moongazer.Game;
|
import org.vibecoders.moongazer.Game;
|
||||||
|
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
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);
|
titleTexture = Assets.getAsset("textures/main_menu/title.png", Texture.class);
|
||||||
|
|
||||||
var font = Assets.getFont("ui", 24);
|
var font = Assets.getFont("ui", 24);
|
||||||
MenuButton playButton = new MenuButton("Play", font);
|
UITextButton playButton = new UITextButton("Play", font);
|
||||||
MenuButton loadButton = new MenuButton("Load", font);
|
UITextButton loadButton = new UITextButton("Load", font);
|
||||||
MenuButton settingsButton = new MenuButton("Settings", font);
|
UITextButton settingsButton = new UITextButton("Settings", font);
|
||||||
MenuButton exitButton = new MenuButton("Exit", font);
|
UITextButton exitButton = new UITextButton("Exit", font);
|
||||||
|
|
||||||
int buttonWidth = 220;
|
int buttonWidth = 220;
|
||||||
int buttonHeight = 65;
|
int buttonHeight = 65;
|
||||||
@ -43,33 +43,10 @@ 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.addEventListener(new ClickListener() {
|
playButton.onClick(() -> log.debug("Play clicked"));
|
||||||
@Override
|
loadButton.onClick(() -> log.debug("Load clicked"));
|
||||||
public void clicked(InputEvent event, float x, float y) {
|
settingsButton.onClick(() -> log.debug("Settings clicked"));
|
||||||
log.debug("Play clicked");
|
exitButton.onClick(() -> log.debug("Exit 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");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
root.addActor(playButton.getActor());
|
root.addActor(playButton.getActor());
|
||||||
root.addActor(loadButton.getActor());
|
root.addActor(loadButton.getActor());
|
||||||
|
|||||||
@ -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() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -2,9 +2,11 @@ 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;
|
||||||
|
import com.badlogic.gdx.scenes.scene2d.ui.Button;
|
||||||
|
|
||||||
public abstract class BaseButton {
|
public abstract class UIButton {
|
||||||
protected Actor actor;
|
protected Actor actor;
|
||||||
|
protected Button button;
|
||||||
|
|
||||||
public Actor getActor() {
|
public Actor getActor() {
|
||||||
return actor;
|
return actor;
|
||||||
@ -21,4 +23,13 @@ public abstract class BaseButton {
|
|||||||
public void addEventListener(EventListener eventListener) {
|
public void addEventListener(EventListener eventListener) {
|
||||||
actor.addListener(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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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 |
BIN
app/src/main/resources/textures/ui/menu_button_down.png
Normal file
BIN
app/src/main/resources/textures/ui/menu_button_down.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
BIN
app/src/main/resources/textures/ui/menu_button_over.png
Normal file
BIN
app/src/main/resources/textures/ui/menu_button_over.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
Reference in New Issue
Block a user