package com.lucasdnd.ags.ui;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import com.lucasdnd.ags.gameplay.Business;
import com.lucasdnd.ags.gameplay.market.MarketConsole;
import com.lucasdnd.ags.system.MainState;
public class BuyConsolesList extends List {
// The sizes, padding, margins, etc.
protected float lineSize = 130f; // The size of one row in the list
private float previousLineSize = 25f; // The row size in the other Lists
protected final float leftMargin = 19f; // Left margin, between the List and the left border of the Panel
protected final float topMargin = 3f; // Top margin, between the List and the Tile Bar
/**
* Basic Constructor
* @param listHeaders
* @param listRows
*/
public BuyConsolesList(float xPos, float yPos, float width, float height, Business business, ArrayList<MarketConsole> marketConsoles) throws SlickException {
super(xPos, yPos, width, height);
pageSize = 4;
// Let's create the List of Consoles
listRows = new ArrayList<ListRow>();
for(int i = 0; i < marketConsoles.size(); i++) {
listRows.add(new BuyConsolesListRow(marketConsoles.get(i), business));
}
// Order by date descending
Collections.sort(listRows, new ConsoleListSorter());
// Creates the Rectangle around the List Row using the right parameters
this.updateRowsPositions();
for(ListRow lr : listRows) {
lr.createRectangle();
}
}
/**
* Specific update method for the Buy Consoles List. This one gets a Game State reference, since it can change the
* control mode.
* @param container
* @param game
* @param delta
* @param business
* @param mapState
* @param leftMouseClicked
* @param mouseX
* @param mouseY
* @throws SlickException
*/
public void update(GameContainer container, StateBasedGame game, int delta, Business business, MainState mapState,
boolean leftMouseClicked, boolean leftMouseDown, int mouseX, int mouseY) throws SlickException {
super.update(container, game, delta, business, leftMouseClicked, leftMouseDown, 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))) {
BuyConsolesListRow bclr = (BuyConsolesListRow)listRows.get(i);
bclr.update(container, game, delta, business, mapState, leftMouseClicked, mouseX, mouseY);
}
}
}
/**
* Updates the position and size of the List Rows.
* This should be called every time the user changes the page he's browsing
*/
@Override
public void updateRowsPositions() {
// Goes through each List Row
for(int i = 0; i < listRows.size(); i++) {
// Define the size and dimension of each List Row
listRows.get(i).setxPos(this.getxPos() - 2f);
listRows.get(i).setyPos(this.getyPos() + topMargin + previousLineSize + (i - (page * pageSize)) * lineSize);
listRows.get(i).setWidth(this.getWidth() + 4f);
listRows.get(i).setHeight(lineSize);
}
}
/**
* Should be called every time the list of Consoles has changed. It will do stuff like updating the source list, reposition rows, etc
* @throws SlickException
*/
public void updateDataSource(Business business, ArrayList<MarketConsole> marketConsoles) throws SlickException {
// Create everything
ArrayList<ListRow> newList = new ArrayList<ListRow>();
for(MarketConsole mc : marketConsoles) {
newList.add(new BuyConsolesListRow(mc, business));
}
Collections.sort(newList, new ConsoleListSorter());
this.listRows = newList;
this.updateRowsPositions();
for(ListRow lr : listRows) {
lr.createRectangle();
}
}
}
class ConsoleListSorter implements Comparator<ListRow> {
public ConsoleListSorter() {}
/**
* Compares two "Buy Console" List Rows
* @param arg1
* @param arg2
* @return
*/
@Override
public int compare(ListRow arg1, ListRow arg2) {
// Get the dates
long date1 = ((BuyConsolesListRow)arg1).getReferringMarketConsole().getReleaseDate();
long date2 = ((BuyConsolesListRow)arg2).getReferringMarketConsole().getReleaseDate();
// Compare!
if(date1 > date2) {
return -1;
} else if(date2 > date1) {
return 1;
} else {
return 0;
}
}
}