Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package game;
- import org.newdawn.slick.AppGameContainer;
- import org.newdawn.slick.BasicGame;
- import org.newdawn.slick.Color;
- import org.newdawn.slick.GameContainer;
- import org.newdawn.slick.Graphics;
- import org.newdawn.slick.Image;
- import org.newdawn.slick.Input;
- import org.newdawn.slick.SlickException;
- import org.newdawn.slick.command.BasicCommand;
- import org.newdawn.slick.command.Command;
- import org.newdawn.slick.command.Control;
- import org.newdawn.slick.command.InputProvider;
- import org.newdawn.slick.command.KeyControl;
- import org.newdawn.slick.command.MouseButtonControl;
- import org.newdawn.slick.gui.MouseOverArea;
- /**
- * A test for using the ControlListener to configure controls
- *
- * @author snapy666
- */
- public class ControlListenerTest extends BasicGame implements ControlEvent
- {
- private static final Command COMMAND_MOVE_LEFT = new BasicCommand(
- "move_left");
- private static final Command COMMAND_MOVE_RIGHT = new BasicCommand(
- "move_right");
- private static final Command COMMAND_ATTACK = new BasicCommand("attack");
- private static final Command[] COMMANDS = {COMMAND_MOVE_LEFT,
- COMMAND_MOVE_RIGHT, COMMAND_ATTACK};
- private static final KeyControl DEFAULT_CONTROL_MOVE_LEFT = new KeyControl(
- Input.KEY_LEFT);
- private static final KeyControl DEFAULT_CONTROL_MOVE_RIGHT = new KeyControl(
- Input.KEY_RIGHT);
- private static final MouseButtonControl DEFAULT_CONTROL_ATTACK = new MouseButtonControl(
- 1);
- private static final Control[] DEFAULT_CONTROLS = {
- DEFAULT_CONTROL_MOVE_LEFT, DEFAULT_CONTROL_MOVE_RIGHT,
- DEFAULT_CONTROL_ATTACK};
- // the following controls have to be ignored by the ControlListener
- private static final MouseButtonControl CONTROL_CLICK = new MouseButtonControl(
- 0);
- private static final KeyControl CONTROL_ABORT = new KeyControl(
- Input.KEY_ESCAPE);
- private InputProvider provider;
- private ControlListener control_listener;
- private MouseOverArea[] areas = new MouseOverArea[3];
- private int active_command;
- /**
- * Create a new image rendering test
- */
- public ControlListenerTest()
- {
- super("ControlListener Test");
- }
- /**
- * @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
- */
- public void init(GameContainer container) throws SlickException
- {
- provider = new InputProvider(container.getInput());
- control_listener = new ControlListener(this);
- container.getInput().addListener(control_listener);
- Image image = new Image("res/simple_button.png");
- for (int i = 0; i < COMMANDS.length; i++)
- {
- areas[i] = new MouseOverArea(container, image, 200, 50 + (i * 60),
- 100, 50);
- areas[i].setMouseOverColor(Color.gray);
- areas[i].setMouseDownColor(Color.darkGray);
- provider.bindCommand(DEFAULT_CONTROLS[i], COMMANDS[i]);
- }
- }
- /**
- * @see org.newdawn.slick.BasicGame#render(org.newdawn.slick.GameContainer,
- * org.newdawn.slick.Graphics)
- */
- public void render(GameContainer container, Graphics g)
- {
- for (int i = 0; i < COMMANDS.length; i++)
- {
- String control_name = "other";
- g.setColor(Color.white);
- g.drawString(COMMANDS[i].toString() + ":", 10, 65 + (i * 60));
- Control c = (Control) provider.getControlsFor(COMMANDS[i]).get(0);
- if (i == active_command && control_listener.isActive())
- {
- control_name = "?";
- }
- else if (c instanceof KeyControl)
- {
- KeyControl key_control = (KeyControl) c;
- control_name = Input.getKeyName(key_control.hashCode());
- }
- else if (c instanceof MouseButtonControl)
- {
- MouseButtonControl mouse_button_control = (MouseButtonControl) c;
- int mouse_button = mouse_button_control.hashCode();
- if (mouse_button == 0)
- {
- control_name = "MOUSE_0";
- }
- else if (mouse_button == 1)
- {
- control_name = "MOUSE_1";
- }
- else if (mouse_button == 2)
- {
- control_name = "MOUSE_2";
- }
- }
- areas[i].render(container, g);
- if (provider.isCommandControlDown(COMMANDS[i]))
- {
- g.setColor(Color.red);
- }
- else
- {
- g.setColor(Color.black);
- }
- g.drawString(control_name, 210, 65 + (i * 60));
- }
- }
- public void update(GameContainer container, int delta)
- {
- if (container.getInput().isKeyDown(Input.KEY_ESCAPE))
- {
- control_listener.setActive(false);
- }
- for (int i = 0; i < COMMANDS.length; i++)
- {
- if (areas[i].isMouseOver()
- && container.getInput().isMousePressed(0))
- {
- active_command = i;
- control_listener.setActive(true);
- }
- }
- }
- @Override
- public void gotControl(Control control)
- {
- boolean is_allowed_control = true;
- if (control.equals(CONTROL_CLICK) || control.equals(CONTROL_ABORT))
- {
- is_allowed_control = false;
- }
- boolean is_unique_control = true;
- for (int i = 0; i < COMMANDS.length; i++)
- {
- if (provider.getControlsFor(COMMANDS[i]).get(0).equals(control))
- {
- is_unique_control = false;
- break;
- }
- }
- if (is_allowed_control && is_unique_control)
- {
- control_listener.setActive(false);
- provider.clearCommand(COMMANDS[active_command]);
- provider.bindCommand(control, COMMANDS[active_command]);
- }
- }
- public static void main(String[] argv)
- {
- try
- {
- AppGameContainer container = new AppGameContainer(
- new ControlListenerTest());
- container.setDisplayMode(800, 600, false);
- container.start();
- }
- catch (SlickException e)
- {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement