Skip to content
Snippets Groups Projects
Commit d8683bb6 authored by Sander Østrem Fagernes's avatar Sander Østrem Fagernes
Browse files

refactor: Moved drawing logic from MenuButton to Screen

Issue #14
parent ff8c4ab9
No related branches found
No related tags found
1 merge request!1314 Main menu screen setup
Pipeline #204311 passed
package com.game.tankwars.controller;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.game.tankwars.TankWarsGame;
import com.game.tankwars.model.MenuButton;
import com.game.tankwars.view.GameScreen;
......@@ -13,22 +10,16 @@ import com.game.tankwars.view.GameScreen;
public class MainMenuController {
private final TankWarsGame tankWarsGame;
private final SpriteBatch batch;
private final BitmapFont font;
private final Viewport viewport;
private final Array<MenuButton> menuButtons;
public MainMenuController(final TankWarsGame tankWarsGame, final SpriteBatch batch, final BitmapFont font, final Viewport viewport) {
public MainMenuController(final TankWarsGame tankWarsGame, final BitmapFont font) {
this.tankWarsGame = tankWarsGame;
this.batch = batch;
this.font = font;
this.viewport = viewport;
this.menuButtons = new Array<>();
this.menuButtons.add(new MenuButton(batch, font, "Find Game"));
this.menuButtons.add(new MenuButton(batch, font, "Leaderboard"));
this.menuButtons.add(new MenuButton(batch, font, "Settings"));
this.menuButtons.add(new MenuButton(batch, font, "Log out"));
this.menuButtons.add(new MenuButton(font, "Find Game"));
this.menuButtons.add(new MenuButton(font, "Leaderboard"));
this.menuButtons.add(new MenuButton(font, "Settings"));
this.menuButtons.add(new MenuButton(font, "Log out"));
}
......
......@@ -12,36 +12,21 @@ public class MenuButton {
created to avoid reloading already loaded textures and still provide flexibility */
private static final Texture buttonTexture = new Texture("menu-button.png");
private final SpriteBatch batch;
private final BitmapFont font;
private final String content;
private float x, y;
private float x, y, contentX, contentY;
private final float width, height;
private String content;
private final GlyphLayout contentLayout;
public MenuButton(SpriteBatch batch, BitmapFont font, String content) {
this.batch = batch;
public MenuButton(BitmapFont font, String content) {
this.font = font;
this.content = content;
this.width = buttonTexture.getWidth();
this.height = buttonTexture.getHeight();
this.contentLayout = new GlyphLayout();
}
public void draw() {
if (batch.isDrawing()) {
font.getData().setScale(0.6f);
contentLayout.setText(font, content);
batch.draw(buttonTexture, x, y);
font.draw(batch, content, x + width / 2 - contentLayout.width / 2, y + height / 2 + contentLayout.height / 4);
}
}
public static void dispose() {
buttonTexture.dispose();
this.contentLayout = new GlyphLayout(font, content);
}
/**
......@@ -53,9 +38,7 @@ public class MenuButton {
return buttonTexture;
}
public float getX() {
return x;
}
public float getX() { return x; }
public float getY() {
return y;
......@@ -69,8 +52,53 @@ public class MenuButton {
return height;
}
public float getContentX() {
return contentX;
}
public float getContentY() {
return contentY;
}
public float getContentWidth() {
return contentLayout.width;
}
public float getContentHeight() {
return contentLayout.height;
}
public String getContent() {
return content;
}
/**
* Sets x- and y-coordinates of the button and its content relative to
* the button's centre. Content is also centred.
*
* @param x x-coordinate for the centre of the button
* @param y y-coordinate for the centre of the button
*/
public void setPosition(float x, float y) {
this.x = x - width / 2;
this.y = y + height / 2;
this.y = y - height / 2;
this.contentX = x - getContentWidth() / 2;
this.contentY = y + getContentHeight() / 4;
}
/**
* Sets the textual content and the font.
* Must also be called whenever the font size changes.
*
* @param font The font to use for the content
* @param content The button's textual content
*/
public void setContent(BitmapFont font, String content) {
this.content = content;
contentLayout.setText(font, content);
}
public static void dispose() {
buttonTexture.dispose();
}
}
......@@ -11,7 +11,9 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FillViewport;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.game.tankwars.TankWarsGame;
import com.game.tankwars.controller.MainMenuController;
......@@ -37,18 +39,19 @@ public class MainMenuScreen extends InputAdapter implements Screen {
this.tankWarsGame = tankWarsGame;
this.batch = batch;
this.font = font;
this.font.getData().setScale(0.6f);
this.viewport = new FitViewport(224, 500, camera);
this.controller = new MainMenuController(tankWarsGame, batch, font, viewport);
this.viewport = new FitViewport(224f,
224f * Gdx.graphics.getHeight() / Gdx.graphics.getWidth(), camera);
this.controller = new MainMenuController(tankWarsGame, font);
this.logo = new Texture("tankwars-logo.png");
this.welcomeBox = new Texture("main-menu-welcome-box.png");
this.background = new Texture("menu-background.png");
// Get buttons from the controller and set their positions
this.menuButtons = controller.getMenuButtons();
for (int i = 0; i < menuButtons.size; i++) {
menuButtons.get(i).setPosition(0, -(i - 1) * viewport.getWorldHeight() / 7 - 90);
menuButtons.get(i).setPosition(0, -(i - 1) * viewport.getWorldHeight() / 7 - 40);
}
this.touchPos = new Vector3();
......@@ -61,11 +64,12 @@ public class MainMenuScreen extends InputAdapter implements Screen {
@Override
public void render(float delta) {
ScreenUtils.clear(0, 0, 0, 1f);
ScreenUtils.clear(0, 0, 0, 1);
batch.setProjectionMatrix(viewport.getCamera().combined);
batch.begin();
batch.draw(background, -viewport.getWorldWidth() / 2, -viewport.getWorldHeight() / 2);
batch.draw(background, -viewport.getWorldWidth() / 2, -viewport.getWorldHeight() / 2,
viewport.getWorldWidth(), viewport.getWorldHeight());
// TODO: Find a better way to scale the logo. Perhaps using a Pixmap
float logoScaling = 1.3f;
......@@ -81,9 +85,9 @@ public class MainMenuScreen extends InputAdapter implements Screen {
font.draw(batch, welcomeMessage, -welcomeLayout.width / 2, viewport.getWorldHeight() / 6 + welcomeBox.getHeight() * 8f/10f);
font.draw(batch, username, -usernameLayout.width / 2, viewport.getWorldHeight() / 6 + welcomeBox.getHeight() * 8f/10f - 2 * welcomeLayout.height);
for (MenuButton menuButton : menuButtons) {
menuButton.draw();
batch.draw(MenuButton.getTexture(), menuButton.getX(), menuButton.getY());
font.draw(batch, menuButton.getContent(), menuButton.getContentX(), menuButton.getContentY());
}
batch.end();
}
......@@ -119,6 +123,9 @@ public class MainMenuScreen extends InputAdapter implements Screen {
@Override
public void dispose() {
logo.dispose();
welcomeBox.dispose();
background.dispose();
MenuButton.dispose();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment