Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 KB | None | 0 0
  1. package app;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import com.badlogic.gdx.Input.Buttons;
  7. import com.badlogic.gdx.Input.Keys;
  8. import com.badlogic.gdx.InputProcessor;
  9.  
  10. import game.Game;
  11.  
  12. public class Input implements InputProcessor {
  13.     private static final Key[] keys = new Key[255];
  14.     private static final Key[] mouse = new Key[3];
  15.     private static int lastMouseWheel;
  16.     private static int lastKeyPressed;
  17.     private static int lastMousePressed;
  18.  
  19.     public Input() {
  20.         for (int i = 0; i < keys.length; i++) {
  21.             keys[i] = new Key();
  22.         }
  23.         for (int i = 0; i < mouse.length; i++) {
  24.             mouse[i] = new Key();
  25.         }
  26.     }
  27.    
  28.     public void update() {
  29.         for (Key key : keys) key.update();
  30.         for (Key key : mouse) key.update();
  31.         lastKeyPressed = lastMousePressed = -1;
  32.         lastMouseWheel = 0;
  33.     }
  34.    
  35.     public static class Key {
  36.         public boolean down, pressed, released, clicked;
  37.         private int downTime;
  38.  
  39.         private void toggle(boolean flag) {
  40.             down = flag;
  41.             if (flag) pressed = true;
  42.             if (!flag) released = true;
  43.         }
  44.  
  45.         private void update() {
  46.             clicked = down && (pressed || ++downTime > 20);
  47.             if (!down) downTime = 0;
  48.             pressed = released = false;
  49.         }
  50.        
  51.         public static boolean down(int key) { return keys[key].down; }
  52.         public static boolean pressed(int key) { return keys[key].pressed; }
  53.         public static boolean released(int key) { return keys[key].released; }
  54.         public static boolean clicked(int key) { return keys[key].clicked; }
  55.     }
  56.    
  57.     public static class Mouse {
  58.         public static final int LEFT = Buttons.LEFT;
  59.         public static final int RIGHT = Buttons.RIGHT;
  60.         public static final int MIDDLE = Buttons.MIDDLE;
  61.  
  62.         private static final int WHEEL_UP = -1;
  63.         private static final int WHEEL_DOWN = 1;
  64.  
  65.         public static int x, y;
  66.        
  67.         public static boolean wheelUp() { return lastMouseWheel < 0; }
  68.         public static boolean wheelDown() { return lastMouseWheel > 0; }
  69.        
  70.         public static boolean down(int button) { return mouse[button].down; }
  71.         public static boolean pressed(int button) { return mouse[button].pressed; }
  72.         public static boolean released(int button) { return mouse[button].released; }
  73.         public static boolean clicked(int button) { return mouse[button].clicked; }
  74.     }
  75.    
  76.     public static class Bind {
  77.         public static final int KEY = 0;
  78.         public static final int MOUSE = 1;
  79.         public static final int WHEEL = 2;
  80.        
  81.         public static final List<Bind> binds = new ArrayList<>();
  82.        
  83.         public static final Bind up = new Bind(Lang.g("Move up", "Двигаться вверх"), KEY, Keys.W);
  84.         public static final Bind down = new Bind(Lang.g("Move down", "Двигаться вниз"), KEY, Keys.S);
  85.         public static final Bind left = new Bind(Lang.g("Move left", "Двигаться влево"), KEY, Keys.A);
  86.         public static final Bind right = new Bind(Lang.g("Move right", "Двигаться вправо"), KEY, Keys.D);
  87.         public static final Bind shoot = new Bind(Lang.g("Shoot", "Стрельба"), MOUSE, Mouse.LEFT);
  88.        
  89.         public int code;
  90.         public int type;
  91.         public Lang name;
  92.        
  93.         public Bind(Lang name, int type, int code) {
  94.             this.name = name;
  95.             this.type = type;
  96.             this.code = code;
  97.             binds.add(this);
  98.         }
  99.        
  100.         public Object getText() {
  101.             switch (type) {
  102.                 case KEY: return Keys.toString(code);
  103.                 case MOUSE: return code == Mouse.LEFT ? "LMB" : code == Mouse.RIGHT ? "RMB" : "MMB";
  104.                 case WHEEL: return code == Mouse.WHEEL_DOWN ? "Wheel down" : "Wheel up";
  105.                 default: return "";
  106.             }
  107.         }
  108.        
  109.         public boolean rebind() {
  110.             int type = -1;
  111.             if (lastKeyPressed != -1) {
  112.                 code = lastKeyPressed;
  113.                 type = KEY;
  114.             }
  115.             if (lastMousePressed != -1) {
  116.                 code = lastMousePressed;
  117.                 type = MOUSE;
  118.             }
  119.             if (lastMouseWheel != 0) {
  120.                 code = lastMouseWheel;
  121.                 type = WHEEL;
  122.             }
  123.             this.type = type;
  124.             return type != -1;
  125.         }
  126.        
  127.         public boolean down() {
  128.             switch (type) {
  129.                 case KEY: return Key.down(code);
  130.                 case MOUSE: return Mouse.down(code);
  131.                 case WHEEL: return code == lastMouseWheel;
  132.                 default: return false;
  133.             }
  134.         }
  135.        
  136.         public boolean pressed() {
  137.             switch (type) {
  138.                 case KEY: return Key.pressed(code);
  139.                 case MOUSE: return Mouse.pressed(code);
  140.                 case WHEEL: return code == lastMouseWheel;
  141.                 default: return false;
  142.             }
  143.         }
  144.        
  145.         public boolean released() {
  146.             switch (type) {
  147.                 case KEY: return Key.released(code);
  148.                 case MOUSE: return Mouse.released(code);
  149.                 case WHEEL: return code == lastMouseWheel;
  150.                 default: return false;
  151.             }
  152.         }
  153.        
  154.         public boolean clicked() {
  155.             switch (type) {
  156.                 case KEY: return Key.clicked(code);
  157.                 case MOUSE: return Mouse.clicked(code);
  158.                 case WHEEL: return code == lastMouseWheel;
  159.                 default: return false;
  160.             }
  161.         }
  162.     }
  163.  
  164.     public boolean keyDown(int keycode) {
  165.         keys[keycode].toggle(true);
  166.         lastKeyPressed = keycode;
  167.         return false;
  168.     }
  169.  
  170.     public boolean keyUp(int keycode) {
  171.         keys[keycode].toggle(false);
  172.         return false;
  173.     }
  174.  
  175.     public boolean keyTyped(char character) {
  176.         return false;
  177.     }
  178.  
  179.     public boolean touchDown(int screenX, int screenY, int pointer, int button) {
  180.         mouse[button].toggle(true);
  181.         lastMousePressed = button;
  182.         return false;
  183.     }
  184.  
  185.     public boolean touchUp(int screenX, int screenY, int pointer, int button) {
  186.         mouse[button].toggle(false);
  187.         return false;
  188.     }
  189.  
  190.     public boolean touchDragged(int screenX, int screenY, int pointer) {
  191.         mouseMoved(screenX, screenY);
  192.         return false;
  193.     }
  194.  
  195.     public boolean mouseMoved(int screenX, int screenY) {
  196.         Mouse.x = (int) (screenX / Game.SCALE);
  197.         Mouse.y = (int) (screenY / Game.SCALE);
  198.         return false;
  199.     }
  200.  
  201.     public boolean scrolled(int amount) {
  202.         lastMouseWheel = amount;
  203.         return false;
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement