package com.lucasdnd.ags.ui;
import java.util.ArrayList;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.FontUtils.Alignment;
import com.lucasdnd.ags.gameplay.Business;
import com.lucasdnd.ags.gameplay.market.Market;
import com.lucasdnd.ags.gameplay.market.MarketGame;
import com.lucasdnd.ags.system.MainState;
public class BuyGamesList extends List {
// Should we update the quantities?
private boolean updateQuantities;
// The Headers IDs
public static final int CONSOLE = 0;
public static final int GAME = 1;
public static final int QUANTITY = 2;
public static final int DEMAND = 3;
public static final int PRICE = 4;
public static final int RELEASE = 5;
/**
* Basic Constructor
* @param listHeaders
* @param listRows
*/
public BuyGamesList(float xPos, float yPos, float width, float height, Business business,
ArrayList<MarketGame> marketGames) throws SlickException {
// Super Class Constructor (for the arrow buttons)
super(xPos, yPos, width, height);
// Get the Objects that will define this List
this.xPos = xPos;
this.yPos = yPos;
this.width = width;
this.height = height;
this.currentSort = 0;
// Create the List
listRows = new ArrayList<ListRow>();
for(MarketGame g : marketGames) {
listRows.add(new BuyGamesListRow(g, business));
}
// Now let's create the Headers...
listHeaders = new ListHeader[6];
listHeaders[0] = new ListHeader(CONSOLE, "Console", Alignment.LEFT, this.xPos + leftMargin, this.yPos + topMargin + lineSize, 280f, lineSize);
listHeaders[1] = new ListHeader(GAME, "Game", Alignment.LEFT, listHeaders[0].xPos + listHeaders[0].width, this.yPos + topMargin + lineSize, 400f, lineSize);
listHeaders[2] = new ListHeader(QUANTITY, "Owned", Alignment.CENTER, listHeaders[1].xPos + listHeaders[1].width, this.yPos + topMargin + lineSize, 100f, lineSize);
listHeaders[3] = new ListHeader(DEMAND, "Demand", Alignment.CENTER, listHeaders[2].xPos + listHeaders[2].width, this.yPos + topMargin + lineSize, 100f, lineSize);
listHeaders[4] = new ListHeader(PRICE, "$", Alignment.RIGHT, listHeaders[3].xPos + listHeaders[3].width, this.yPos + topMargin + lineSize, 120f, lineSize);
listHeaders[5] = new ListHeader(RELEASE, "Release", Alignment.RIGHT, listHeaders[4].xPos + listHeaders[4].width, this.yPos + topMargin + lineSize, 150f, lineSize);
// Default sort = last column
currentSort = listHeaders.length - 1;
super.sortList(currentSort, false);
// Creates the Rectangle around the List Row using the right parameters
super.updateRowsPositions();
for(ListRow lr : listRows) {
lr.createRectangle();
}
}
/**
* Specific update method for the Buy Games List
*/
public void update(GameContainer container, StateBasedGame game, int delta, MainState mainState,
Business business, ViewGamesList viewGamesList, Market market, boolean leftMouseClicked, boolean leftMouseDown,
int mouseX, int mouseY) throws SlickException {
// Let the super class update
super.update(container, game, delta, business, leftMouseClicked, leftMouseDown, mouseX, mouseY);
// Header clicks (sorting)
checkHeaderClicks(leftMouseClicked, mouseX, mouseY);
// Updates the List Rows
for(int i = 0; i < listRows.size(); i++) {
// With this if statement, we make sure we update only what's in the current page
if((i >= page * pageSize) && (i < (page + 1) * (pageSize))) {
BuyGamesListRow lr = (BuyGamesListRow)listRows.get(i);
lr.update(container, game, delta, mainState, business, viewGamesList, market, leftMouseClicked, mouseX, mouseY, updateQuantities);
}
}
}
/**
* Updates the data in the list of games to buy
* @param business
* @param marketGames
* @throws SlickException
*/
public void updateDataSource(Business business, ArrayList<MarketGame> availableMarketGames) throws SlickException {
ArrayList<ListRow> newList = new ArrayList<ListRow>();
for(MarketGame mg : availableMarketGames) {
newList.add(new BuyGamesListRow(mg, business));
}
this.listRows = newList;
currentSort = listHeaders.length - 1;
super.sortList(currentSort, false);
super.updateRowsPositions();
for(ListRow lr : listRows) {
lr.createRectangle();
}
}
/**
* Updates the quantities of the given game
* @param marketGame
* @throws SlickException
*/
public void updateQuantities(Business business, MarketGame marketGame) throws SlickException {
// Updates the quantities
for(ListRow lr : listRows) {
BuyGamesListRow bglr = (BuyGamesListRow)lr;
if(bglr.getReferringMarketGame().getId() == marketGame.getId()) {
bglr.updateQuantityText(business);
}
}
}
/**
* Override to render the Unlock Games Button too
*/
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g, Font panelFont, Font disabledFont, Font shadowFont) throws SlickException {
// Normal Super Class Rendering
super.render(container, game, g, panelFont, disabledFont, shadowFont);
}
public boolean isUpdateQuantities() {
return updateQuantities;
}
public void setUpdateQuantities(boolean updateQuantities) {
this.updateQuantities = updateQuantities;
}
}