package bwyap.familyfeud.render.component;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

import bwyap.familyfeud.render.RenderingPanel;
import bwyap.gridgame.res.ResourceLoader;

/**
 * A container for a string that can be rendered to a RenderPanel.
 * @author bwyap
 *
 */
public class RenderableString extends Renderable {

	private String text; 
	private Color color;
	private int text_width = -1;
	
	private String fontname;
	private int style;
	private boolean centered = true;
	private int size;
	
	
	/**
	 * Create a new renderable string at the specified location.
	 * @param text the string to render
	 * @param x the x position
	 * @param y the y position
	 * @param fontname the font name
	 * @param style the font style
	 * @param size the font size
	 * @param color the font color
	 * @param centered whether the font is centered on the screen (horizontally)
	 */
	public RenderableString(String text, int x, int y, String fontname, int style, int size, Color color, boolean centered) {
		super(x, y, 0, 0);
		this.text = text;
		this.fontname = fontname;
		this.style = style;
		this.size = size;
		this.color = color;
		this.centered = centered;
	}
	
	/**
	 * Create a new renderable string at the specified location.
	 * The string is centered horizontally by default and has a black color.
	 * @param text the string to render
	 * @param y the y position
	 * @param fontname the font name
	 * @param size the font size
	 */
	public RenderableString(String text, int y, String fontname, int size) {
		this(text, 0, y, fontname, Font.PLAIN, size, Color.BLACK, true);
	}
	
	/**
	 * Create a new renderable string at the specified location.
	 * @param text the string to render
	 * @param x the x position
	 * @param y the y position
	 * @param fontname the font name
	 * @param size the font size
	 */
	public RenderableString(String text, int x, int y, String fontname, int size) {
		this(text, x, y, fontname, Font.PLAIN, size, Color.BLACK, false);
	}
	
	/**
	 * Check if the font is rendered in the center of the screen (horizontally)
	 * @return
	 */
	public boolean isCentered() {
		return centered;
	}
	
	/**
	 * Set whether the font is to be rendered in the center of the screen (horizontally)
	 * @param centered
	 */
	public void setCentered(boolean centered) {
		this.centered = centered;
	}
	
	/**
	 * Set the font size
	 * @param size
	 */
	public void setSize(int size) {
		this.size = size;
	}
	
	/**
	 * Get the font size
	 * @return
	 */
	public int getSize() {
		return size;
	}
	
	/**
	 * Set the font style
	 * @param style
	 */
	public void setStyle(int style) {
		this.style = style;
	}
	
	/**
	 * Get the font style
	 * @return
	 */
	public int getStyle() {
		return style;
	}
	
	/**
	 * Set the name of the font used to render the string
	 * @param fontname
	 */
	public void setFontname(String fontname) {
		this.fontname = fontname;
	}
	
	/**
	 * Get the name of the font used to render the string
	 * @return
	 */
	public String getFontname() {
		return fontname;
	}
	
	/**
	 * Set the color of the string
	 * @param color
	 */
	public void setColor(Color color) {
		this.color = color;
	}
	
	/**
	 * Get the colur of the string
	 * @return
	 */
	public Color getColour() {
		return color;
	}
	
	@Override
	public void render(RenderingPanel panel, Graphics g) {
		g.setFont(new Font(ResourceLoader.getFontName(fontname), style, size));
		g.setColor(color);
		if (centered) {
			if (text_width == -1) {
				text_width = g.getFontMetrics().stringWidth(text);
			}
			g.drawString(text, (panel.getWidth() - text_width)/2, y);
		}
		else g.drawString(text, x, y);
	}

}

@@ -395,38 +395,8 @@
 or;%0A
-%09private int text_width = -1;%0A
 %09%0A%09p
@@ -493,16 +493,43 @@
  size;%0A%09
+private int textWidth = -1;
 %0A%09%0A%09/**%0A
@@ -1988,24 +1988,283 @@
 ;%0A%09%7D%0A%09%0A%09/**%0A
+%09 * Set the to be rendered%0A%09 * @param text%0A%09 */%0A%09public void setText(String text) %7B%0A%09%09this.text = text;%0A%09%09textWidth = -1; // recalculate text width%0A%09%7D%0A%09%0A%09/**%0A%09 * Get the text being rendered%0A%09 * @return%0A%09 */%0A%09public String getText() %7B%0A%09%09return text;%0A%09%7D%0A%09%0A%09/**%0A
 %09 * Check if
@@ -3706,20 +3706,17 @@
 text
-_w
+W
 idth 
-== -1
+%3C 0
 ) %7B%0A
@@ -3719,26 +3719,25 @@
 ) %7B%0A%09%09%09%09text
-_w
+W
 idth = g.get
@@ -3768,16 +3768,20 @@
 h(text);
+%09%09%09%09
 %0A%09%09%09%7D%0A%09%09
@@ -3828,10 +3828,9 @@
 text
-_w
+W
 idth

Add getter and setter for the text in RenderableString