/*
 * Copyright 2014-2015 Pawel Pastuszak
 *
 * This file is part of VisEditor.
 *
 * VisEditor is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VisEditor is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VisEditor.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.kotcrab.vis.editor.module.scene;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.utils.Array;
import com.kotcrab.vis.editor.App;
import com.kotcrab.vis.editor.event.StatusBarEvent;

public class UndoModule extends SceneModule {
	private Array<UndoableAction> undoList;
	private Array<UndoableAction> redoList;

	@Override
	public void added () {
		undoList = new Array<>();
		redoList = new Array<>();
	}

	@Override
	public void init () {
	}

	@Override
	public void dispose () {
	}

	public void undo () {
		if (undoList.size > 0) {
			UndoableAction action = undoList.pop();
			action.undo();
			redoList.add(action);
		} else
			App.eventBus.post(new StatusBarEvent("Can't undo more!"));
	}

	public void redo () {
		if (redoList.size > 0) {
			UndoableAction action = redoList.pop();
			action.execute();
			undoList.add(action);
		} else
			App.eventBus.post(new StatusBarEvent("Can't redo more!"));
	}

	public void execute (UndoableAction action) {
		action.execute();
		undoList.add(action);
		redoList.clear();
	}

	@Override
	public boolean keyDown (InputEvent event, int keycode) {
		if (Gdx.input.isKeyPressed(Keys.CONTROL_LEFT)) {
			if (keycode == Keys.Z) undo();
			if (keycode == Keys.Y) redo();

			return true;
		}

		return false;
	}
}

@@ -880,16 +880,70 @@
 tEvent;%0A
+import com.badlogic.gdx.scenes.scene2d.InputListener;%0A
 import c
@@ -1105,16 +1105,45 @@
 odule %7B%0A
+%09private InputModule input;%0A%0A
 %09private
@@ -1217,16 +1217,45 @@
 oList;%0A%0A
+%09private boolean tabActive;%0A%0A
 %09@Overri
@@ -1341,91 +1341,105 @@
 ();%0A
-%09%7D%0A%0A%09@Override%0A%09public void init () %7B%0A%09%7D%0A%0A%09@Override%0A%09public void dispose () %7B
+%0A%09%09input = containter.get(InputModule.class);%0A%09%09input.addListener(new UndoInputListener());
 %0A%09%7D%0A%0A
+%0A
 %09pub
@@ -1977,32 +1977,200 @@
 %0A%09%7D%0A%0A%09@Override%0A
+%09public void onHide () %7B%0A%09%09tabActive = false;%0A%09%7D%0A%0A%09@Override%0A%09public void onShow () %7B%0A%09%09tabActive = true;%0A%09%7D%0A%0A%09private class UndoInputListener extends InputListener %7B%0A%09
 %09public boolean 
@@ -2207,24 +2207,45 @@
  keycode) %7B%0A
+%09%09%09if(tabActive) %7B%0A%09%09
 %09%09if (Gdx.in
@@ -2282,24 +2282,26 @@
 LEFT)) %7B%0A%09%09%09
+%09%09
 if (keycode 
@@ -2322,16 +2322,18 @@
 o();%0A%09%09%09
+%09
+%09
 if (keyc
@@ -2356,16 +2356,18 @@
 edo();%0A%0A
+%09%09
 %09%09%09retur
@@ -2378,13 +2378,21 @@
 ue;%0A
+%09%09%09%09%7D%0A%09
 %09%09%7D%0A%0A
+%09
 %09%09re
@@ -2403,13 +2403,17 @@
  false;%0A
+%09%09%7D%0A
 %09%7D%0A%7D%0A

Undo now uses InputModule