package net.alcuria.umbracraft.engine.windows;

import net.alcuria.umbracraft.Game;
import net.alcuria.umbracraft.listeners.Listener;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;

/** An abstract window.
 * @author Andrew Keturi
 * @param <T> */
public abstract class Window<T extends WindowLayout> {
	private boolean escapeKeyCloses;
	public final T layout;

	public Window(T layout) {
		this.layout = layout;
	}

	void close(Listener listener) {
		layout.hide(listener);
	}

	/** called to dispose any resources */
	public void dispose() {

	}

	/** @return whether or not this window is touchable. default is
	 *         <code>true</code>. */
	public boolean isTouchable() {
		return true;
	}

	/** callback after the window has closed */
	public abstract void onClose();

	/** callback after the window has opened */
	public abstract void onOpen();

	void open() {
		layout.show();
		onOpen();
	}

	/** called to draw the layout */
	public void render() {
		layout.render();
	}

	/** Sets whether or not escape should close the window
	 * @param escapeKeyCloses */
	public void setEscapeKeyCloses(boolean escapeKeyCloses) {
		this.escapeKeyCloses = escapeKeyCloses;
	}

	/** called to update the layout */
	public void update() {
		layout.update();
		// check for close
		if (Gdx.input.isKeyJustPressed(Keys.ESCAPE) && escapeKeyCloses) {
			Game.windows().pop(this);
		}
	}
}

@@ -1363,31 +1363,121 @@
 %0A%09%09%09
-Game.windows().pop(this
+close(new Listener() %7B%0A%0A%09%09%09%09@Override%0A%09%09%09%09public void invoke() %7B%0A%09%09%09%09%09Game.windows().pop(Window.this);%0A%09%09%09%09%7D%0A%09%09%09%7D
 );%0A%09

Call close on escape key