Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app;
- import java.util.ArrayList;
- import java.util.List;
- import com.badlogic.gdx.Input.Buttons;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.InputProcessor;
- import game.Game;
- public class Input implements InputProcessor {
- private static final Key[] keys = new Key[255];
- private static final Key[] mouse = new Key[3];
- private static int lastMouseWheel;
- private static int lastKeyPressed;
- private static int lastMousePressed;
- public Input() {
- for (int i = 0; i < keys.length; i++) {
- keys[i] = new Key();
- }
- for (int i = 0; i < mouse.length; i++) {
- mouse[i] = new Key();
- }
- }
- public void update() {
- for (Key key : keys) key.update();
- for (Key key : mouse) key.update();
- lastKeyPressed = lastMousePressed = -1;
- lastMouseWheel = 0;
- }
- public static class Key {
- public boolean down, pressed, released, clicked;
- private int downTime;
- private void toggle(boolean flag) {
- down = flag;
- if (flag) pressed = true;
- if (!flag) released = true;
- }
- private void update() {
- clicked = down && (pressed || ++downTime > 20);
- if (!down) downTime = 0;
- pressed = released = false;
- }
- public static boolean down(int key) { return keys[key].down; }
- public static boolean pressed(int key) { return keys[key].pressed; }
- public static boolean released(int key) { return keys[key].released; }
- public static boolean clicked(int key) { return keys[key].clicked; }
- }
- public static class Mouse {
- public static final int LEFT = Buttons.LEFT;
- public static final int RIGHT = Buttons.RIGHT;
- public static final int MIDDLE = Buttons.MIDDLE;
- private static final int WHEEL_UP = -1;
- private static final int WHEEL_DOWN = 1;
- public static int x, y;
- public static boolean wheelUp() { return lastMouseWheel < 0; }
- public static boolean wheelDown() { return lastMouseWheel > 0; }
- public static boolean down(int button) { return mouse[button].down; }
- public static boolean pressed(int button) { return mouse[button].pressed; }
- public static boolean released(int button) { return mouse[button].released; }
- public static boolean clicked(int button) { return mouse[button].clicked; }
- }
- public static class Bind {
- public static final int KEY = 0;
- public static final int MOUSE = 1;
- public static final int WHEEL = 2;
- public static final List<Bind> binds = new ArrayList<>();
- public static final Bind up = new Bind(Lang.g("Move up", "Двигаться вверх"), KEY, Keys.W);
- public static final Bind down = new Bind(Lang.g("Move down", "Двигаться вниз"), KEY, Keys.S);
- public static final Bind left = new Bind(Lang.g("Move left", "Двигаться влево"), KEY, Keys.A);
- public static final Bind right = new Bind(Lang.g("Move right", "Двигаться вправо"), KEY, Keys.D);
- public static final Bind shoot = new Bind(Lang.g("Shoot", "Стрельба"), MOUSE, Mouse.LEFT);
- public int code;
- public int type;
- public Lang name;
- public Bind(Lang name, int type, int code) {
- this.name = name;
- this.type = type;
- this.code = code;
- binds.add(this);
- }
- public Object getText() {
- switch (type) {
- case KEY: return Keys.toString(code);
- case MOUSE: return code == Mouse.LEFT ? "LMB" : code == Mouse.RIGHT ? "RMB" : "MMB";
- case WHEEL: return code == Mouse.WHEEL_DOWN ? "Wheel down" : "Wheel up";
- default: return "";
- }
- }
- public boolean rebind() {
- int type = -1;
- if (lastKeyPressed != -1) {
- code = lastKeyPressed;
- type = KEY;
- }
- if (lastMousePressed != -1) {
- code = lastMousePressed;
- type = MOUSE;
- }
- if (lastMouseWheel != 0) {
- code = lastMouseWheel;
- type = WHEEL;
- }
- this.type = type;
- return type != -1;
- }
- public boolean down() {
- switch (type) {
- case KEY: return Key.down(code);
- case MOUSE: return Mouse.down(code);
- case WHEEL: return code == lastMouseWheel;
- default: return false;
- }
- }
- public boolean pressed() {
- switch (type) {
- case KEY: return Key.pressed(code);
- case MOUSE: return Mouse.pressed(code);
- case WHEEL: return code == lastMouseWheel;
- default: return false;
- }
- }
- public boolean released() {
- switch (type) {
- case KEY: return Key.released(code);
- case MOUSE: return Mouse.released(code);
- case WHEEL: return code == lastMouseWheel;
- default: return false;
- }
- }
- public boolean clicked() {
- switch (type) {
- case KEY: return Key.clicked(code);
- case MOUSE: return Mouse.clicked(code);
- case WHEEL: return code == lastMouseWheel;
- default: return false;
- }
- }
- }
- public boolean keyDown(int keycode) {
- keys[keycode].toggle(true);
- lastKeyPressed = keycode;
- return false;
- }
- public boolean keyUp(int keycode) {
- keys[keycode].toggle(false);
- return false;
- }
- public boolean keyTyped(char character) {
- return false;
- }
- public boolean touchDown(int screenX, int screenY, int pointer, int button) {
- mouse[button].toggle(true);
- lastMousePressed = button;
- return false;
- }
- public boolean touchUp(int screenX, int screenY, int pointer, int button) {
- mouse[button].toggle(false);
- return false;
- }
- public boolean touchDragged(int screenX, int screenY, int pointer) {
- mouseMoved(screenX, screenY);
- return false;
- }
- public boolean mouseMoved(int screenX, int screenY) {
- Mouse.x = (int) (screenX / Game.SCALE);
- Mouse.y = (int) (screenY / Game.SCALE);
- return false;
- }
- public boolean scrolled(int amount) {
- lastMouseWheel = amount;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement