|
|
@@ -0,0 +1,117 @@
|
|
|
+package me.lethunderhawk.screen;
|
|
|
+
|
|
|
+import com.badlogic.gdx.Gdx;
|
|
|
+import com.badlogic.gdx.Screen;
|
|
|
+import com.badlogic.gdx.assets.AssetManager;
|
|
|
+import com.badlogic.gdx.graphics.GL20;
|
|
|
+import com.badlogic.gdx.scenes.scene2d.Stage;
|
|
|
+import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
|
|
+import com.badlogic.gdx.scenes.scene2d.ui.Skin;
|
|
|
+import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
|
|
+import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
|
|
+import com.badlogic.gdx.scenes.scene2d.ui.TextField;
|
|
|
+import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
|
|
|
+import com.badlogic.gdx.utils.viewport.ScreenViewport;
|
|
|
+import com.badlogic.gdx.InputMultiplexer;
|
|
|
+import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
|
|
+import me.lethunderhawk.Main;
|
|
|
+
|
|
|
+public class MenuScreen implements Screen {
|
|
|
+
|
|
|
+ private final Main game;
|
|
|
+ private Stage stage;
|
|
|
+ private Skin skin;
|
|
|
+ private TextField ipField;
|
|
|
+ private final AssetManager assets;
|
|
|
+
|
|
|
+ public MenuScreen(Main game) {
|
|
|
+ this.game = game;
|
|
|
+ this.assets = new AssetManager();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void show() {
|
|
|
+ // Setup stage and viewport
|
|
|
+ stage = new Stage(new ScreenViewport());
|
|
|
+
|
|
|
+ // Load skin via AssetManager (skin file path relative to assets/)
|
|
|
+ // Ensure ui/uiskin.json references the atlas/image with correct relative paths.
|
|
|
+ try {
|
|
|
+ assets.load("ui/uiskin.json", Skin.class);
|
|
|
+ assets.finishLoading(); // blocking load; change if async desired
|
|
|
+ skin = assets.get("ui/uiskin.json", Skin.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Gdx.app.error("MenuScreen", "Failed to load skin: ui/uiskin.json", e);
|
|
|
+ // Fallback: create an empty skin so UI code doesn't NPE (will look ugly)
|
|
|
+ skin = new Skin();
|
|
|
+ }
|
|
|
+
|
|
|
+ // Build UI table
|
|
|
+ Table table = new Table();
|
|
|
+ table.setFillParent(true);
|
|
|
+ table.center();
|
|
|
+ stage.addActor(table);
|
|
|
+
|
|
|
+ // Label
|
|
|
+ Label label = new Label("Enter Server IP:", skin);
|
|
|
+
|
|
|
+ // IP Input Field (use style from skin)
|
|
|
+ ipField = new TextField("localhost", skin);
|
|
|
+
|
|
|
+ // Start Button
|
|
|
+ TextButton startButton = new TextButton("Start Game", skin);
|
|
|
+ startButton.addListener(new ClickListener() {
|
|
|
+ @Override
|
|
|
+ public void clicked(InputEvent event, float x, float y) {
|
|
|
+ String ip = ipField.getText();
|
|
|
+ if (ip == null || ip.trim().isEmpty()) {
|
|
|
+ ip = "localhost";
|
|
|
+ }
|
|
|
+ game.setScreen(new GameScreen(game, ip));
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // Layout components
|
|
|
+ table.add(label).padBottom(10).row();
|
|
|
+ table.add(ipField).width(300).padBottom(20).row();
|
|
|
+ table.add(startButton).width(200);
|
|
|
+
|
|
|
+ // Set input processor
|
|
|
+ Gdx.input.setInputProcessor(stage);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void render(float delta) {
|
|
|
+ Gdx.gl.glClearColor(0.12f, 0.12f, 0.12f, 1f); // dark background
|
|
|
+ Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
|
|
+
|
|
|
+ stage.act(Math.min(delta, 1/30f));
|
|
|
+ stage.draw();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void resize(int width, int height) {
|
|
|
+ if (stage != null) {
|
|
|
+ stage.getViewport().update(width, height, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void pause() {}
|
|
|
+ @Override
|
|
|
+ public void resume() {}
|
|
|
+ @Override
|
|
|
+ public void hide() {
|
|
|
+ // clear input when hidden
|
|
|
+ if (Gdx.input.getInputProcessor() == stage) {
|
|
|
+ Gdx.input.setInputProcessor(null);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void dispose() {
|
|
|
+ if (stage != null) stage.dispose();
|
|
|
+ if (skin != null) skin.dispose();
|
|
|
+ if (assets != null) assets.dispose();
|
|
|
+ }
|
|
|
+}
|