Advertisement
Guest User

ControlListenerTest.java

a guest
Jul 17th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.40 KB | None | 0 0
  1. package game;
  2.  
  3. import org.newdawn.slick.AppGameContainer;
  4. import org.newdawn.slick.BasicGame;
  5. import org.newdawn.slick.Color;
  6. import org.newdawn.slick.GameContainer;
  7. import org.newdawn.slick.Graphics;
  8. import org.newdawn.slick.Image;
  9. import org.newdawn.slick.Input;
  10. import org.newdawn.slick.SlickException;
  11. import org.newdawn.slick.command.BasicCommand;
  12. import org.newdawn.slick.command.Command;
  13. import org.newdawn.slick.command.Control;
  14. import org.newdawn.slick.command.InputProvider;
  15. import org.newdawn.slick.command.KeyControl;
  16. import org.newdawn.slick.command.MouseButtonControl;
  17. import org.newdawn.slick.gui.MouseOverArea;
  18.  
  19. /**
  20.  * A test for using the ControlListener to configure controls
  21.  *
  22.  * @author snapy666
  23.  */
  24. public class ControlListenerTest extends BasicGame implements ControlEvent
  25. {
  26.     private static final Command COMMAND_MOVE_LEFT = new BasicCommand(
  27.             "move_left");
  28.     private static final Command COMMAND_MOVE_RIGHT = new BasicCommand(
  29.             "move_right");
  30.     private static final Command COMMAND_ATTACK = new BasicCommand("attack");
  31.     private static final Command[] COMMANDS = {COMMAND_MOVE_LEFT,
  32.             COMMAND_MOVE_RIGHT, COMMAND_ATTACK};
  33.  
  34.     private static final KeyControl DEFAULT_CONTROL_MOVE_LEFT = new KeyControl(
  35.             Input.KEY_LEFT);
  36.     private static final KeyControl DEFAULT_CONTROL_MOVE_RIGHT = new KeyControl(
  37.             Input.KEY_RIGHT);
  38.     private static final MouseButtonControl DEFAULT_CONTROL_ATTACK = new MouseButtonControl(
  39.             1);
  40.     private static final Control[] DEFAULT_CONTROLS = {
  41.             DEFAULT_CONTROL_MOVE_LEFT, DEFAULT_CONTROL_MOVE_RIGHT,
  42.             DEFAULT_CONTROL_ATTACK};
  43.  
  44.     // the following controls have to be ignored by the ControlListener
  45.     private static final MouseButtonControl CONTROL_CLICK = new MouseButtonControl(
  46.             0);
  47.     private static final KeyControl CONTROL_ABORT = new KeyControl(
  48.             Input.KEY_ESCAPE);
  49.  
  50.     private InputProvider provider;
  51.     private ControlListener control_listener;
  52.  
  53.     private MouseOverArea[] areas = new MouseOverArea[3];
  54.     private int active_command;
  55.  
  56.     /**
  57.      * Create a new image rendering test
  58.      */
  59.     public ControlListenerTest()
  60.     {
  61.         super("ControlListener Test");
  62.     }
  63.  
  64.     /**
  65.      * @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
  66.      */
  67.     public void init(GameContainer container) throws SlickException
  68.     {
  69.         provider = new InputProvider(container.getInput());
  70.         control_listener = new ControlListener(this);
  71.         container.getInput().addListener(control_listener);
  72.  
  73.         Image image = new Image("res/simple_button.png");
  74.         for (int i = 0; i < COMMANDS.length; i++)
  75.         {
  76.             areas[i] = new MouseOverArea(container, image, 200, 50 + (i * 60),
  77.                     100, 50);
  78.             areas[i].setMouseOverColor(Color.gray);
  79.             areas[i].setMouseDownColor(Color.darkGray);
  80.            
  81.             provider.bindCommand(DEFAULT_CONTROLS[i], COMMANDS[i]);
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * @see org.newdawn.slick.BasicGame#render(org.newdawn.slick.GameContainer,
  87.      *      org.newdawn.slick.Graphics)
  88.      */
  89.     public void render(GameContainer container, Graphics g)
  90.     {
  91.         for (int i = 0; i < COMMANDS.length; i++)
  92.         {
  93.             String control_name = "other";
  94.  
  95.             g.setColor(Color.white);
  96.             g.drawString(COMMANDS[i].toString() + ":", 10, 65 + (i * 60));
  97.  
  98.             Control c = (Control) provider.getControlsFor(COMMANDS[i]).get(0);
  99.             if (i == active_command && control_listener.isActive())
  100.             {
  101.                 control_name = "?";
  102.             }
  103.             else if (c instanceof KeyControl)
  104.             {
  105.                 KeyControl key_control = (KeyControl) c;
  106.                 control_name = Input.getKeyName(key_control.hashCode());
  107.             }
  108.             else if (c instanceof MouseButtonControl)
  109.             {
  110.                 MouseButtonControl mouse_button_control = (MouseButtonControl) c;
  111.                 int mouse_button = mouse_button_control.hashCode();
  112.                 if (mouse_button == 0)
  113.                 {
  114.                     control_name = "MOUSE_0";
  115.                 }
  116.                 else if (mouse_button == 1)
  117.                 {
  118.                     control_name = "MOUSE_1";
  119.                 }
  120.                 else if (mouse_button == 2)
  121.                 {
  122.                     control_name = "MOUSE_2";
  123.                 }
  124.             }
  125.  
  126.             areas[i].render(container, g);
  127.  
  128.             if (provider.isCommandControlDown(COMMANDS[i]))
  129.             {
  130.                 g.setColor(Color.red);
  131.             }
  132.             else
  133.             {
  134.                 g.setColor(Color.black);
  135.             }
  136.             g.drawString(control_name, 210, 65 + (i * 60));
  137.         }
  138.     }
  139.  
  140.     public void update(GameContainer container, int delta)
  141.     {
  142.         if (container.getInput().isKeyDown(Input.KEY_ESCAPE))
  143.         {
  144.             control_listener.setActive(false);
  145.         }
  146.         for (int i = 0; i < COMMANDS.length; i++)
  147.         {
  148.             if (areas[i].isMouseOver()
  149.                     && container.getInput().isMousePressed(0))
  150.             {
  151.                 active_command = i;
  152.                 control_listener.setActive(true);
  153.             }
  154.         }
  155.     }
  156.  
  157.     @Override
  158.     public void gotControl(Control control)
  159.     {
  160.         boolean is_allowed_control = true;
  161.         if (control.equals(CONTROL_CLICK) || control.equals(CONTROL_ABORT))
  162.         {
  163.             is_allowed_control = false;
  164.         }
  165.  
  166.         boolean is_unique_control = true;
  167.         for (int i = 0; i < COMMANDS.length; i++)
  168.         {
  169.             if (provider.getControlsFor(COMMANDS[i]).get(0).equals(control))
  170.             {
  171.                 is_unique_control = false;
  172.                 break;
  173.             }
  174.         }
  175.  
  176.         if (is_allowed_control && is_unique_control)
  177.         {
  178.             control_listener.setActive(false);
  179.             provider.clearCommand(COMMANDS[active_command]);
  180.             provider.bindCommand(control, COMMANDS[active_command]);
  181.         }
  182.     }
  183.  
  184.     public static void main(String[] argv)
  185.     {
  186.         try
  187.         {
  188.             AppGameContainer container = new AppGameContainer(
  189.                     new ControlListenerTest());
  190.             container.setDisplayMode(800, 600, false);
  191.             container.start();
  192.         }
  193.         catch (SlickException e)
  194.         {
  195.             e.printStackTrace();
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement