package com.lucasdnd.ags.ui;
import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;
import com.lucasdnd.ags.gameplay.Business;
import com.lucasdnd.ags.ui.components.Clickable;
/**
* One of the items in a list.
* @author tulio
*
*/
public abstract class ListRow extends UIComponent implements Clickable {
// Mouse over rectangle and if it's visible or not
protected Rectangle rectangle;
protected boolean isRectangleVisible = false;
// The columns of this List Item
protected ListColumn[] columns;
// Indicates if this item is available for purchase
protected boolean isAvailable;
/**
* Update. Where we check for mouse overs. Inside their subclasses, we check for clicks.
* @param container
* @param game
* @param delta
* @param business
* @param leftMouseClicked
* @param mouseX
* @param mouseY
* @throws SlickException
*/
public void update(GameContainer container, StateBasedGame game, int delta, Business business, boolean leftMouseClicked, int mouseX, int mouseY,
boolean isAvailable) throws SlickException {
// Check if the game needs to render the mouse over Rectangle
isRectangleVisible = isMouseOver(mouseX, mouseY);
// If this item is available or not
this.isAvailable = isAvailable;
}
/**
* Rendering
* @param container
* @param game
* @param g
* @throws SlickException
*/
public void render(GameContainer container, StateBasedGame game, Graphics g, Font panelFont, Font disabledFont, Font shadowFont) throws SlickException {
// Render the columns
if(columns != null) {
for(int i = 0; i < columns.length; i++) {
columns[i].isAvailable = this.isAvailable;
columns[i].render(container, game, g, panelFont, disabledFont, shadowFont);
}
}
// Render the rectangle around if the user has the mouse over the row
if(isAvailable) {
if(isRectangleVisible) {
g.setLineWidth(2f);
Color previousColor = g.getColor();
Color strokeColor = new Color(0, 0, 0);
g.setColor(strokeColor);
g.draw(rectangle);
Color fillColor = new Color(0, 10, 100, 0.1f);
g.setColor(fillColor);
g.setColor(previousColor);
}
}
}
/**
* Creates the rectangle around the List Row
*/
public void createRectangle() {
rectangle = new Rectangle(this.xPos, this.yPos, this.width, this.height);
}
public boolean isMouseOver(int mouseX, int mouseY) {
if((mouseX >= this.xPos && mouseX <= this.xPos + this.width) &&
(mouseY >= this.yPos && mouseY <= this.yPos + this.height))
return true;
return false;
}
@Override
public boolean gotLeftClicked(boolean leftMouseClicked, int mouseX, int mouseY) {
if((leftMouseClicked) &&
(mouseX >= this.xPos && mouseX <= this.xPos + this.width) &&
(mouseY >= this.yPos && mouseY <= this.yPos + this.height)
)
return true;
return false;
}
@Override
public boolean gotRightClicked(boolean rightMouseClicked, int mouseX, int mouseY) {
if((rightMouseClicked) &&
(mouseX >= this.xPos && mouseX <= this.xPos + this.width) &&
(mouseY >= this.yPos && mouseY <= this.yPos + this.height)
)
return true;
return false;
}
public ListColumn[] getListColumns() {
return columns;
}
public void setListColumns(ListColumn[] listColumns) {
this.columns = listColumns;
}
public boolean isAvailable() {
return isAvailable;
}
public void setAvailable(boolean isAvailable) {
this.isAvailable = isAvailable;
}
}