package org.egordorichev.lasttry.ui;

import org.egordorichev.lasttry.LastTry;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;

public class UiComponent {
	public enum State {
		NORMAL,
		MOUSE_IN,
		MOUSE_DOWN
	}

	public enum Origin {
		TOP_LEFT,
		TOP_RIGHT,
		BOTTOM_LEFT,
		BOTTOM_RIGHT
	}

	protected Rectangle rect;
	protected State state;
	protected Origin origin;
	protected boolean hidden;

	public UiComponent(Rectangle rectangle, Origin origin) {
		this.rect = rectangle;
		this.state = State.NORMAL;
		this.origin = origin;
		this.hidden = false;
	}

	public UiComponent(Rectangle rectangle) {
		this(rectangle, Origin.TOP_LEFT);
	}

	public void render() {
		if (this.hidden) {
			return;
		}

		this.update();
	}

	public void setPosition(int x, int y) {
		this.rect.setX(x);
		this.rect.setY(y);
		this.applyRect();
	}

	public void setSize(int width, int height) {
		this.rect.setSize(width, height);
		this.applyRect();
	}

	public void show() {
		this.hidden = false;
	}

	public void hide() {
		this.hidden = true;
	}

	public int getX() {
		switch(this.origin) {
			case TOP_LEFT: case BOTTOM_LEFT: default: return (int) this.rect.getX();
			case TOP_RIGHT: case BOTTOM_RIGHT: return (int) (LastTry.getWindowWidth() - this.getWidth() - this.rect.getX());
		}
	}

	public int getY() {
		switch(this.origin) {
			case TOP_LEFT: case TOP_RIGHT: default: return (int) this.rect.getY();
			case BOTTOM_LEFT: case BOTTOM_RIGHT: return (int) (LastTry.getWindowHeight() - this.getHeight() - this.rect.getY());
		}
	}

	public int getWidth() {
		return (int) this.rect.getWidth();
	}

	public int getHeight() {
		return (int) this.rect.getHeight();
	}

	public State getState() {
		return this.state;
	}

	public boolean isHidden() {
		return this.hidden;
	}

	protected void update() {
		if (this.hidden) {
			return;
		}

		if (this.rect.contains(LastTry.input.getMouseX(), LastTry.input.getMouseY())) {
			if (this.state != State.MOUSE_DOWN && (LastTry.input.isMousePressed(Input.MOUSE_LEFT_BUTTON) || LastTry.input.isMousePressed(Input.MOUSE_RIGHT_BUTTON))) {
				this.state = State.MOUSE_DOWN;
				this.onStateChange();
			} else if (this.state != State.MOUSE_IN) {
				this.state = State.MOUSE_IN;
				this.onStateChange();
			}
		} else if (this.state != State.NORMAL) {
			this.state = State.NORMAL;
			this.onStateChange();
		}
	}

	protected void applyRect() {

	}

	protected void onStateChange() {

	}
}
@@ -319,83 +319,328 @@
 %7D%0A%0A%09
-protected Rectangle rect;%0A%09protected State state;%0A%09protected Origin origin;
+/** %0A%09 * Holds element position and size %0A%09 */%0A%09protected Rectangle rect;%0A%09%0A%09/**%0A%09 * Holds element state%0A%09 */%0A%09protected State state;%0A%09%0A%09/**%0A%09 * Shows, from witch side of the screen position coordinates are calculated%0A%09 */%0A%09protected Origin origin;%0A%09%0A%09/**%0A%09 * Element doen't draw and react to input, if it is hidden%0A%09 */
 %0A%09pr
@@ -996,221 +996,8 @@
 %09%7D%0A%0A
-%09public void setPosition(int x, int y) %7B%0A%09%09this.rect.setX(x);%0A%09%09this.rect.setY(y);%0A%09%09this.applyRect();%0A%09%7D%0A%0A%09public void setSize(int width, int height) %7B%0A%09%09this.rect.setSize(width, height);%0A%09%09this.applyRect();%0A%09%7D%0A%0A
 %09pub
@@ -2477,8 +2477,9 @@
  %7B%0A%0A%09%7D%0A%7D
+%0A

Update UiComponent.java