Java tutorial
/* * The MIT License (MIT) * Copyright (c) 2015-2016, theosirian * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.theosirian.ppioo.controllers; import com.badlogic.gdx.Input; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.ui.Label; import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.scenes.scene2d.utils.Drawable; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.Array; import com.theosirian.ppioo.Puno; import com.theosirian.ppioo.football.Fan; import com.theosirian.ppioo.football.Fan.FanComp; import com.theosirian.ppioo.football.Player; import com.theosirian.ppioo.football.Player.PlayerComp; import com.theosirian.ppioo.util.EventRunnable; import com.theosirian.ppioo.widget.*; import com.theosirian.ppioo.widget.PlayerList.ListType; import java.text.NumberFormat; import java.util.Date; /** * Created by theosirian on 07/01/2016. */ public class ShopController extends Controller { private NumberFormat percentFormatter; private Dialog noMoneyInfoDialog; private Label titleLabel; private Label moneyLabel; private TextButton backButton; private TextButton playerSortButton; private TextButton fanSortButton; private ScrollPane playersPane; private ScrollPane fansPane; private Table playersTable; private Table fansTable; private PlayerComp playerComp; private FanComp fanComp; private PlayerList playerList; private EventRunnable buyCallback = new EventRunnable() { @Override public void run() { confirmPurchase((Player) ((TextButton) event.getListenerActor()).getExtra("player")); } }; private EventRunnable infoCallback = new EventRunnable() { @Override public void run() { final TextButton target = (TextButton) event.getListenerActor(); Player p = (Player) target.getExtra("player"); Dialog dialog = new Dialog("Player File", game.skin) { @Override protected void result(Object object) { boolean result = object.equals(true); if (result) { makePurchase((Player) getExtra("player")); } else { hide(); } } }; dialog.putExtra("player", target.getExtra("player")); dialog.getContentTable().add().row(); dialog.text("Name: " + p.getFirstName() + " " + p.getLastName()); dialog.getContentTable().row(); dialog.text("Age: " + p.getAge()); dialog.getContentTable().row(); dialog.text("Country: " + p.getCountry()); dialog.getContentTable().row(); dialog.text("Quality: " + percentFormatter.format(p.getQuality())); dialog.getContentTable().row(); dialog.text("Confidence: " + percentFormatter.format(p.getConfidence())); dialog.getContentTable().row(); dialog.text("Price: $" + p.getPrice()); dialog.getContentTable().row(); dialog.button("Buy", true, game.skin.get("success.small", TextButtonStyle.class)); dialog.button("Close", false, game.skin.get("error.small", TextButtonStyle.class)); dialog.key(Keys.ENTER, true); dialog.key(Keys.ESCAPE, false); dialog.show(game.stage); } }; private EventRunnable checkboxCallback = new EventRunnable() { @Override public void run() { System.out.println("Clicked!"); CheckBox cb = (CheckBox) event.getListenerActor(); if (cb.isChecked()) { if (game.data.getTeam().size < game.data.getTeamSize()) { game.data.getTeam().add((Player) cb.getExtra("player")); } else { cb.setChecked(false); cb.invalidate(); } } else { game.data.getTeam().removeValue((Player) cb.getExtra("player"), false); } invalidate(); } }; public ShopController(Puno game) { super(game); } @Override protected void createComponents() { percentFormatter = NumberFormat.getPercentInstance(); percentFormatter.setMaximumFractionDigits(0); playerComp = new PlayerComp(Player.firstNameComparator, false); fanComp = new FanComp(Fan.nameComparator, false); rootLayout = new Table(); rootLayout.setFillParent(true); rootLayout.setBackground(game.skin.newDrawable("white", Color.DARK_GRAY)); titleLabel = new Label("Shop", game.skin, "big.white"); titleLabel.setAlignment(Align.left); moneyLabel = new Label("Money: $" + game.data.getMoney(), game.skin, "big.white"); moneyLabel.setAlignment(Align.center); backButton = new TextButton("Return", game.skin); backButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); game.getSoundController().soundPlay(1); game.changeController = MenuController.class; } }); playerSortButton = new TextButton("Sort Players", game.skin); playerSortButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); Dialog info = new PlayerSortDialog("Sort Players By:", game.skin, true) { @Override protected void result(Object object) { if (object == null) { return; } playerComp = (PlayerComp) object; ShopController.this.invalidate(); } }; info.show(game.stage); } }); fanSortButton = new TextButton("Sort Players", game.skin); fanSortButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); Dialog info = new FanSortDialog("Sort Players By:", game.skin, true) { @Override protected void result(Object object) { if (object == null) { return; } fanComp = (FanComp) object; ShopController.this.invalidate(); } }; info.show(game.stage); } }); buildPlayerList(); playersTable = new Table(); fansTable = new Table(); playersPane = new ScrollPane(playersTable, game.skin); fansPane = new ScrollPane(fansTable, game.skin); } @Override protected void buildUI() { rootLayout.clear(); buildPlayerList(); moneyLabel.setText("Money: $" + game.data.getMoney()); Table titleTable = new Table(); titleTable.add(titleLabel).expandX().left(); titleTable.add(moneyLabel).expandX().right(); rootLayout.add(titleTable).pad(16).expandX().fill().row(); Table panelsTable = new Table(); panelsTable.background(game.skin.newDrawable("white", Color.GRAY)); panelsTable.add(playerList).pad(4, 8, 8, 4).expand().fill().uniformX(); buildFanList(game.data.getNotOwnedFans()); panelsTable.add(fansPane).pad(4, 4, 8, 8).expand().fill().uniformX(); panelsTable.row(); rootLayout.add(panelsTable).expand().fill().row(); Table buttonTable = new Table(); buttonTable.add(backButton).pad(8, 8, 8, 4).expandX().fillX().uniformX(); buttonTable.add(playerSortButton).pad(8, 8, 8, 4).expandX().fillX().uniformX(); buttonTable.add(fanSortButton).pad(8, 8, 8, 4).expandX().fillX().uniformX(); rootLayout.add(buttonTable).expandX().fillX().row(); } private void confirmPurchase(Player player) { if (game.data.getMoney() >= player.getPrice()) { Dialog dialog = new Dialog("Confirm Purchase", game.skin) { public void result(Object obj) { boolean confirmation = obj.equals(true); if (confirmation) { makePurchase((Player) getExtra("player")); } else { hide(); } } }; dialog.putExtra("player", player); dialog.text("Are you sure you want to buy " + player.getFirstName() + " " + player.getLastName() + " for $" + player.getPrice() + "?"); dialog.button("Yes", true); dialog.button("No", false); dialog.key(Input.Keys.ENTER, true); dialog.key(Input.Keys.ESCAPE, false); dialog.show(game.stage); } else { noMoneyInfoDialog = new Dialog("Insufficient money!", game.skin); noMoneyInfoDialog.button("Close", false); noMoneyInfoDialog.key(Input.Keys.ENTER, false); noMoneyInfoDialog.text("You don't have enough money to buy " + player.getFirstName() + " " + player.getLastName() + "."); noMoneyInfoDialog.show(game.stage); } } private void makePurchase(Player player) { if (game.data.getMoney() >= player.getPrice()) { game.data.changeMoney(-player.getPrice()); player.setOwned(true); player.setDateAcquired(new Date()); player.setVictories(0); player.setDefeats(0); invalidate(); } else { Dialog info = new Dialog("Insufficient money!", game.skin); info.text("You don't have enough money to buy " + player.getFirstName() + " " + player.getLastName() + "."); info.button("Close", false); info.key(Input.Keys.ENTER, false); info.show(game.stage); } } private void buildFanList(Array<Fan> fans) { fansTable.clear(); Drawable odd = game.skin.newDrawable("white", Color.valueOf("C0C0C0FF")); Drawable even = game.skin.newDrawable("white", Color.valueOf("E0E0E0FF")); Drawable[] turn = new Drawable[] { odd, even }; fans.sort(fanComp.comparator); if (fanComp.isReverse) { fans.reverse(); } int counter = 0; for (Fan fan : fans) { fansTable.add(buildFanListItem(fan, turn[counter % 2])).expand().fill(); counter++; fansTable.row(); } fansPane.setWidget(fansTable); } public void buildPlayerList() { playerList = new PlayerList(game.data.getNotOwnedPlayers(), ListType.BuyAndInfo, game.skin); playerList.setSort(playerComp); playerList.setBuyButtonCallback(buyCallback); playerList.setInfoButtonCallback(infoCallback); } private Actor buildFanListItem(Fan fan, Drawable background) { TextButton buyButton = new TextButton("Buy", game.skin, "success.small"); buyButton.putExtra("fan", fan); buyButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); confirmPurchase((Player) ((TextButton) event.getListenerActor()).getExtra("fan")); } }); TextButton infoButton = new TextButton("?", game.skin, "default.small"); infoButton.putExtra("fan", fan); infoButton.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { game.getSoundController().soundPlay(1); final TextButton target = (TextButton) event.getListenerActor(); Fan f = (Fan) target.getExtra("fan"); Dialog dialog = new Dialog("Fan File", game.skin) { @Override protected void result(Object object) { boolean result = object.equals(true); if (result) { makePurchase((Player) getExtra("fan")); } else { hide(); } } }; dialog.putExtra("fan", target.getExtra("fan")); dialog.getContentTable().add().row(); dialog.text("Name: " + f.getName()); dialog.getContentTable().row(); dialog.text("Support: [" + percentFormatter.format(f.getMinSupport()) + ", " + percentFormatter.format(f.getMaxSupport()) + "]"); dialog.getContentTable().row(); dialog.text("Huff: [" + percentFormatter.format(f.getMinHuff()) + ", " + percentFormatter.format(f.getMaxHuff()) + "]"); dialog.getContentTable().row(); dialog.text("Price: $" + f.getPrice()); dialog.getContentTable().row(); dialog.button("Buy", true, game.skin.get("success.small", TextButtonStyle.class)); dialog.button("Close", false, game.skin.get("error.small", TextButtonStyle.class)); dialog.key(Keys.ENTER, true); dialog.key(Keys.ESCAPE, false); dialog.show(game.stage); } }); Table innerTable = new Table(); Table outerTable = new Table(); Label nameLabel = new Label(fan.getName(), game.skin, "medium"); Label priceLabel = new Label("$" + fan.getPrice(), game.skin, "big"); Label supportLabel = new Label("Support: [" + percentFormatter.format(fan.getMinSupport()) + ", " + percentFormatter.format(fan.getMaxSupport()) + "]", game.skin, "default"); Label huffLabel = new Label("Huff: [" + percentFormatter.format(fan.getMinHuff()) + ", " + percentFormatter.format(fan.getMaxHuff()) + "]", game.skin, "default"); innerTable.setBackground(background); outerTable.setBackground(background); innerTable.add(nameLabel).pad(4, 0, 2, 0).expandX(); innerTable.add(priceLabel).pad(4, 0, 2, 0).expandX(); innerTable.row(); innerTable.add(supportLabel).pad(2, 0, 4, 0).expandX(); innerTable.add(huffLabel).pad(2, 0, 4, 0).expandX(); innerTable.row(); outerTable.add(innerTable).expand().fill(); outerTable.add(buyButton).padRight(16); outerTable.add(infoButton).padRight(16); return outerTable; } @Override public boolean keyDown(int keycode) { if (keycode == Input.Keys.D) { rootLayout.setDebug(!rootLayout.getDebug()); } return super.keyDown(keycode); } }