package org.newdawn.slick.command; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.newdawn.slick.Input; import org.newdawn.slick.util.InputAdapter; /** * The central provider that maps real device input into abstract commands * defined by the developer. Registering a control against an command with this * class will cause the provider to produce an event for the command when the * input is pressed and released. * * @author joverton */ public class InputProvider { /** The commands that have been defined */ private HashMap commands; /** The list of listeners that may be listening */ private ArrayList listeners = new ArrayList(); /** The input context we're responding to */ private Input input; /** The command input states */ private HashMap commandState = new HashMap(); /** True if this provider is actively sending events */ private boolean active = true; /** * Create a new input proider which will provide abstract input descriptions * based on the input from the supplied context. * * @param input * The input from which this provider will receive events */ public InputProvider(Input input) { this.input = input; input.addListener(new InputListenerImpl()); commands = new HashMap(); } /** * Get the list of commands that have been registered with the provider, * i.e. the commands that can be issued to the listeners * * @return The list of commands (@see Command) that can be issued from this * provider */ public List getUniqueCommands() { List uniqueCommands = new ArrayList(); for (Iterator it = commands.values().iterator(); it.hasNext();) { Command command = (Command) it.next(); if (!uniqueCommands.contains(command)) { uniqueCommands.add(command); } } return uniqueCommands; } /** * Get a list of the registered controls (@see Control) that can cause a * particular command to be invoked * * @param command * The command to be invoked * @return The list of controls that can cause the command (@see Control) */ public List getControlsFor(Command command) { List controlsForCommand = new ArrayList(); for (Iterator it = commands.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); Control key = (Control) entry.getKey(); Command value = (Command) entry.getValue(); if (value == command) { controlsForCommand.add(key); } } return controlsForCommand; } /** * Indicate whether this provider should be sending events * * @param active * True if this provider should be sending events */ public void setActive(boolean active) { this.active = active; } /** * Check if this provider should be sending events * * @return True if this provider should be sending events */ public boolean isActive() { return active; } /** * Add a listener to the provider. This listener will be notified of * commands detected from the input. * * @param listener * The listener to be added */ public void addListener(InputProviderListener listener) { listeners.add(listener); } /** * Remove a listener from this provider. The listener will no longer be * provided with notification of commands performe. * * @param listener * The listener to be removed */ public void removeListener(InputProviderListener listener) { listeners.remove(listener); } /** * Bind an command to a control. * * @param command * The command to bind to * @param control * The control that is pressed/released to represent the command */ public void bindCommand(Control control, Command command) { commands.put(control, command); if (commandState.get(command) == null) { commandState.put(command, new CommandState()); } } /** * Clear all the controls that have been configured for a given command * * @param command The command whose controls should be unbound */ public void clearCommand(Command command) { List controls = getControlsFor(command); for (int i=0;i