Coverage Summary for Class: BuildingMenu (io.github.unisim.ui)
| Class |
Method, %
|
Branch, %
|
Line, %
|
| BuildingMenu |
0%
(0/4)
|
0%
(0/14)
|
0%
(0/83)
|
| BuildingMenu$1 |
0%
(0/2)
|
0%
(0/8)
|
0%
(0/15)
|
| BuildingMenu$2 |
0%
(0/3)
|
0%
(0/4)
|
0%
(0/11)
|
| Total |
0%
(0/9)
|
0%
(0/26)
|
0%
(0/109)
|
package io.github.unisim.ui;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Cell;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
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.utils.ClickListener;
import com.badlogic.gdx.utils.Align;
import io.github.unisim.GameState;
import io.github.unisim.Point;
import io.github.unisim.building.Building;
import io.github.unisim.building.BuildingType;
import io.github.unisim.world.World;
import java.util.ArrayList;
/**
* Menu used to place buildings in the world by clicking and dragging them
* from the list onto the map.
*/
@SuppressWarnings({ "MemberName", "AbbreviationAsWordInName" })
public class BuildingMenu {
private World world;
private ShapeActor bar = new ShapeActor(GameState.UISecondaryColour);
private Table table;
private ArrayList<Building> buildings;
private ArrayList<Image> buildingImages = new ArrayList<>();
private Label buildingInfoLabel = new Label(
"", new Skin(Gdx.files.internal("ui/uiskin.json")));
private Label infoLabel = new Label(
"", new Skin(Gdx.files.internal("ui/uiskin.json")));
private Table buildingInfoTable = new Table();
private Table infoTable = new Table();
private String outputInfo = "";
private Boolean hover = false;
/**
* Create a Building Menu and attach its actors and components to the provided
* stage.
* Also handles drawing buildings and their flipped variants
*
* @param stage - The stage on which to draw the menu.
* @param world - The world to get current difficulty
*/
public BuildingMenu(Stage stage, World world) {
this.world = world;
world.setDifficulty(GameState.settings.getDifficulty());
buildings = new ArrayList<>();
// Set building images and sizes
buildings.add(new Building(
new Texture(Gdx.files.internal("buildings/restaurant.png")),
0.01f,
new Vector2(0.35f, -0.9f),
new Point(),
new Point(3, 3),
false,
BuildingType.EATING,
"Canteen",
(int) (1000 * (2 - world.getMultiplier())), 5));
buildings.add(new Building(
new Texture(Gdx.files.internal("buildings/fastfood.png")),
0.03f,
new Vector2(-1.25f, 0.8f),
new Point(),
new Point(20, 20),
false,
BuildingType.EATING,
"Fast food",
(int) (1250 * (2 - world.getMultiplier())), 10));
buildings.add(new Building(
new Texture(Gdx.files.internal("buildings/library.png")),
0.0075f,
new Vector2(1.8f, -4.6f),
new Point(),
new Point(20, 12),
false,
BuildingType.LEARNING,
"Library",
(int) (1500 * (2 - world.getMultiplier())), 10));
buildings.add(new Building(
new Texture(Gdx.files.internal("buildings/tenniscourt.png")),
0.0250f,
new Vector2(0f, 1f),
new Point(),
new Point(8, 11),
false,
BuildingType.RECREATION,
"Tennis court",
(int) (1750 * (2 - world.getMultiplier())), 15));
buildings.add(new Building(
new Texture(Gdx.files.internal("buildings/basketballCourt.png")),
0.0025f,
new Vector2(1f, -2.4f),
new Point(),
new Point(6, 9),
false,
BuildingType.RECREATION,
"Basketball Court",
(int) (2500 * (2 - world.getMultiplier())), 15));
buildings.add(new Building(
new Texture(Gdx.files.internal("buildings/computersciencebuilding.png")),
0.0350f,
new Vector2(-0.9f, -6.6f),
new Point(),
new Point(7, 5),
false,
BuildingType.LEARNING,
"Computer Science",
(int) (2750 * (2 - world.getMultiplier())), 20));
buildings.add(new Building(
new Texture(Gdx.files.internal("buildings/studentHousing.png")),
0.108f,
new Vector2(1.4f, -2.8f),
new Point(),
new Point(11, 11),
false,
BuildingType.SLEEPING,
"Student Accomodation",
(int) (3500 * (2 - world.getMultiplier())), 20));
buildings.add(new Building(
new Texture(Gdx.files.internal("buildings/eventhall.png")),
0.0250f,
new Vector2(-1.9f, -1.6f),
new Point(),
new Point(9, 9),
false,
BuildingType.SLEEPING,
"Premium Accomodation",
(int) (5000 * (2 - world.getMultiplier())), 35));
table = new Table();
// Add buldings to the table
for (int i = 0; i < buildings.size(); i++) {
buildingImages.add(new Image(buildings.get(i).texture));
final int buildingIndex = i;
Building currBuilding = buildings.get(buildingIndex);
buildingImages.get(i).addListener(new ClickListener() {
@Override
public void clicked(InputEvent e, float x, float y) {
if (GameState.paused) {
world.setPopup("paused");
}
if (world.selectedBuilding == buildings.get(buildingIndex)) {
world.selectedBuilding = null;
} else {
if (world.getPlaced() == false) {
world.selectedBuilding = buildings.get(buildingIndex);
outputInfo = ("Building Info:\n" + currBuilding.name + "\n" + currBuilding.type + "\nCost:"
+ currBuilding.cost + "\nReputation:" + currBuilding.rep);
infoLabel.setText(outputInfo);
if (world.selectedBuilding.flipped) {
world.selectedBuilding.flipped = false;
int temp = world.selectedBuilding.size.x;
world.selectedBuilding.size.x = world.selectedBuilding.size.y;
world.selectedBuilding.size.y = temp;
world.selectedBuildingUpdated = true;
}
}
}
}
});
table.add(buildingImages.get(i));
// Hover Listener
buildingImages.get(i).addListener(new InputListener() {
@Override
public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
hover = true;
if (world.selectedBuilding == null) {
outputInfo = ("Building Info:\n" + currBuilding.name + "\n" + currBuilding.type + "\nCost:"
+ currBuilding.cost + "\nReputation:" + currBuilding.rep);
infoLabel.setText(outputInfo);
} else {
String tempInfo = ("Building Info:\n" + currBuilding.name + "\n" +
currBuilding.type + "\nCost:" + currBuilding.cost + "\nReputation:" + currBuilding.rep);
infoLabel.setText(tempInfo);
}
}
@Override
public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
if (world.selectedBuilding == null) {
infoLabel.setText("");
} else {
infoLabel.setText(outputInfo);
}
hover = false;
}
});
}
buildingInfoTable.add(buildingInfoLabel).expandX().align(Align.center);
infoTable.add(infoLabel).expandX().align(Align.left);
stage.addActor(bar);
stage.addActor(table);
stage.addActor(buildingInfoTable);
stage.addActor(infoTable);
}
/**
* Called when the window is resized, scales the building menu images with the
* window size.
*
* @param width - The new width of the window in pixels
* @param height - The new height of the window in pixels
*/
@SuppressWarnings("unchecked")
public void resize(int width, int height) {
table.setBounds(0, 0, width, height * 0.1f);
bar.setBounds(0, 0, width, height * 0.1f);
buildingInfoTable.setBounds(0, height * 0.1f, width, height * 0.025f);
infoTable.setBounds(10, height * 0.78f, width, height * 0.025f);
// we must perform an unchecked type conversion here
// this is acceptable as we know our table only contains instances of Actors
for (Cell<Actor> cell : table.getCells()) {
Image buildingImage = (Image) (cell.getActor());
Vector2 textureSize = new Vector2(buildingImage.getWidth(), buildingImage.getHeight());
cell.width(
height * 0.1f * (textureSize.x < textureSize.y ? textureSize.x / textureSize.y : 1)).height(
height * 0.1f * (textureSize.y < textureSize.x ? textureSize.y / textureSize.x : 1));
}
buildingInfoLabel.setFontScale(height * 0.0015f);
infoLabel.setFontScale(height * 0.003f);
}
/**
* Called when the building menu needs to be redrawn with new values in the
* labels.
*/
public void update() {
if (world.selectedBuilding == null) {
buildingInfoLabel.setText("");
if (hover == false) {
infoLabel.setText("");
}
} else {
Building building = world.selectedBuilding;
String tempInfo;
// Set UI text whenever a building is clicked or hovered over
// Makes sure that the currently held building info is displayed
buildingInfoLabel.setText(building.name + " - Press 'R' to rotate | Press 'RMB' to cancel/remove");
if (world.getPlaced() == true) {
tempInfo = ("Building Info:\n" + building.name + "\n" +
building.type + "\nCost to move:" + (int) (building.cost / 2) + "\nReputation:0");
} else {
tempInfo = ("Building Info:\n" + building.name + "\n" +
building.type + "\nCost:" + building.cost + "\nReputation:" + building.rep);
}
infoLabel.setText(tempInfo);
}
}
public void reset() {
buildingInfoLabel.setText("");
}
}