package envoy.client.data.shortcuts; import java.util.*; import javafx.scene.input.KeyCombination; import envoy.client.ui.SceneContext.SceneInfo; /** * Contains all keyboard shortcuts used throughout the application. * * @author Leon Hofmeister * @since Envoy Client v0.3-beta */ public final class GlobalKeyShortcuts { private final EnumMap> shortcuts = new EnumMap<>(SceneInfo.class); private static GlobalKeyShortcuts instance = new GlobalKeyShortcuts(); private GlobalKeyShortcuts() { for (final var sceneInfo : SceneInfo.values()) shortcuts.put(sceneInfo, new HashMap()); } /** * @return the instance of global keyboard shortcuts. * @since Envoy Client v0.3-beta */ public static GlobalKeyShortcuts getInstance() { return instance; } /** * Adds the given keyboard shortcut and its action to all scenes. * * @param keys the keys to press to perform the given action * @param action the action to perform * @since Envoy Client v0.3-beta */ public void add(KeyCombination keys, Runnable action) { shortcuts.values().forEach(collection -> collection.put(keys, action)); } /** * Adds the given keyboard shortcut and its action to all scenes that are not part of exclude. * * @param keys the keys to press to perform the given action * @param action the action to perform * @param exclude the scenes that should be excluded from receiving this keyboard shortcut * @since Envoy Client v0.3-beta */ public void addForNotExcluded(KeyCombination keys, Runnable action, SceneInfo... exclude) { // Computing the remaining sceneInfos final var include = new SceneInfo[SceneInfo.values().length - exclude.length]; int index = 0; outer: for (final var sceneInfo : SceneInfo.values()) { for (final var excluded : exclude) if (sceneInfo.equals(excluded)) continue outer; include[index++] = sceneInfo; } // Adding the action to the remaining sceneInfos for (final var sceneInfo : include) shortcuts.get(sceneInfo).put(keys, action); } /** * Returns all stored keyboard shortcuts for the given scene constant. * * @param sceneInfo the currently loading scene * @return all stored keyboard shortcuts for this scene * @since Envoy Client v0.3-beta */ public Map getKeyboardShortcuts(SceneInfo sceneInfo) { return shortcuts.get(sceneInfo); } }