/*
 * Copyright 2014-2015 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.esotericsoftware.kryo.serializers.TaggedFieldSerializer.Tag;
import com.google.common.eventbus.Subscribe;
import com.kotcrab.vis.editor.App;
import com.kotcrab.vis.editor.event.ToggleToolbarEvent;
import com.kotcrab.vis.editor.event.ToolbarEventType;
import com.kotcrab.vis.editor.module.InjectModule;

/** @author Kotcrab */
public class EditingSettingsModule extends EditorModule {
	@InjectModule private EditorSettingsIOModule settingsIO;

	private static final String SETTINGS_NAME = "editingProperties";

	private EditingConfig config;

	@Override
	public void init () {
		config = settingsIO.load(SETTINGS_NAME, EditingConfig.class);
		App.eventBus.register(this);
	}

	@Override
	public void postInit () {
		App.eventBus.post(new ToggleToolbarEvent(ToolbarEventType.GRID_SNAP_SETTING_CHANGED, config.snapToGrid));
	}

	@Override
	public void dispose () {
		settingsIO.save(config, SETTINGS_NAME);
		App.eventBus.unregister(this);
	}

	@Subscribe
	public void handleToggleToolbarEvent (ToggleToolbarEvent event) {
		if (event.type == ToolbarEventType.GRID_SNAP_SETTING_CHANGED) {
			config.snapToGrid = event.toggleState;
		}
	}

	public boolean isSnapToGrid () {
		return config.snapToGrid;
	}

	public boolean isSnapEnabledOrKeyPressed () {
		boolean snap = config.snapToGrid;
		if (Gdx.input.isKeyPressed(Keys.CONTROL_LEFT)) snap = !snap; //allow to override snap setting
		return snap;
	}

	public static class EditingConfig {
		@Tag(0) public boolean snapToGrid = false;
	}
}

@@ -708,16 +708,67 @@
 t.Keys;%0A
+import com.badlogic.gdx.scenes.scene2d.InputEvent;%0A
 import c
@@ -831,16 +831,16 @@
 er.Tag;%0A
-
 import c
@@ -1071,16 +1071,75 @@
 tModule;
+%0Aimport com.kotcrab.vis.editor.util.gdx.ModalInputListener;
 %0A%0A/** @a
@@ -1213,16 +1213,64 @@
 odule %7B%0A
+%09@InjectModule private InputModule inputModule;%0A
 %09@Inject
@@ -1419,16 +1419,91 @@
 onfig;%0A%0A
+%09private EditingInputListener inputListener = new EditingInputListener();%0A%0A
 %09@Overri
@@ -1619,24 +1619,66 @@
 ster(this);%0A
+%09%09inputModule.addListener(inputListener);%0A
 %09%7D%0A%0A%09@Overri
@@ -1931,16 +1931,61 @@
 (this);%0A
+%09%09inputModule.removeListener(inputListener);%0A
 %09%7D%0A%0A%09@Su
@@ -2476,16 +2476,16 @@
 onfig %7B%0A
-
 %09%09@Tag(0
@@ -2523,11 +2523,378 @@
 alse;%0A%09%7D
+%0A%0A%09private class EditingInputListener extends ModalInputListener %7B%0A%09%09@Override%0A%09%09public boolean keyDown (InputEvent event, int keycode) %7B%0A%09%09%09if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT) && keycode == Keys.NUM_5) %7B%0A%09%09%09%09App.eventBus.post(new ToggleToolbarEvent(ToolbarEventType.GRID_SNAP_SETTING_CHANGED, !isSnapToGrid()));%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%0A%09%09%09return false;%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Add missing key shortcut