From f94cf651a13c6506c482b5b287e175508af8ad52 Mon Sep 17 00:00:00 2001 From: Maik Macho Date: Fri, 18 May 2012 11:24:28 +0200 Subject: [PATCH] Joypad Support v.2.0 : - Added more accuracy to shooting; - Added more accuracy while using buttons AND keyboard; - Added mouse support ( excepting clicking ); - Added basic support; - Re-added JInput; Feel free to use , I just may add clicking to 2.0.1 . --- .classpath | 1 + res/translations/texts.txt | 26 ++++- src/com/mojang/mojam/InputHandler.java | 11 ++ src/com/mojang/mojam/JoypadHandler.java | 40 +++++-- src/com/mojang/mojam/Keys.java | 4 + src/com/mojang/mojam/MojamComponent.java | 107 ++++++++++++++---- src/com/mojang/mojam/MouseButtons.java | 27 +++++ .../mojam/entity/weapon/VenomShooter.java | 3 - src/com/mojang/mojam/gui/JoyBindingsMenu.java | 2 +- src/com/mojang/mojam/gui/KeyBindingsMenu.java | 10 +- src/com/mojang/mojam/gui/TitleMenu.java | 41 ++++++- src/com/mojang/mojam/resources/Texts.java | 4 + 12 files changed, 233 insertions(+), 43 deletions(-) diff --git a/.classpath b/.classpath index 65f00bf6e..ac4ffb9ef 100644 --- a/.classpath +++ b/.classpath @@ -12,5 +12,6 @@ + diff --git a/res/translations/texts.txt b/res/translations/texts.txt index a6d69c80d..b0dd90773 100644 --- a/res/translations/texts.txt +++ b/res/translations/texts.txt @@ -62,6 +62,28 @@ help.rails2 = PRESS 'BUILD' ON THE FIRST RAIL help.rails3 = TO BUY A RAIL-DROID (50 GOLD) help.rails4 = +keys.up = UP +keys.down = DOWN +keys.left = LEFT +keys.right= RIGHT +keys.sprint = SPRINT +keys.fire = FIRE +keys.fireUp = FIRE UP +keys.fireDown = FIRE DOWN +keys.fireLeft = FIRE LEFT +keys.fireRight = FIRE RIGHT +keys.use = USE +keys.build = BUILD +keys.upgrade = UPGRADE +keys.chat = CHAT +keys.weaponSlot = WEAPON SLOT {0} +keys.cycleLeft = NEXT WEAPON +keys.cycleRight = PREV WEAPON +keys.console = CONSOLE +keys.mousea = MOUSE {0}-AXIS +keys.walka = WALK {0}-AXIS +keys.shoota = SHOOT {0}-AXIS + keys.up = UP keys.down = DOWN keys.left = LEFT @@ -103,6 +125,8 @@ mp.client = CLIENT options.on = ON options.off = OFF options.keyBindings = KEY BINDINGS +options.joyBindings = JOYPADS +options.axeBindings = AXE BINDINGS options.characterSelect = SEL. CHARACTER options.trapmouse = TRAP MOUSE IN WINDOW options.fullscreen = FULLSCREEN @@ -117,7 +141,7 @@ options.mute = MUTE options.creative = CREATIVE MODE options.alternative = ALTERNATIVE PLAYER SKIN options.credits = CREDITS -options.openal = Sound system OpenAL +options.openal = OPENAL (RESTART REQUIRED) options.smallscale = SMALL GRAPHICS options.enableMods = ENABLE MODS (RESTART REQUIRED) diff --git a/src/com/mojang/mojam/InputHandler.java b/src/com/mojang/mojam/InputHandler.java index fb97dbc4a..19baadffa 100644 --- a/src/com/mojang/mojam/InputHandler.java +++ b/src/com/mojang/mojam/InputHandler.java @@ -103,8 +103,19 @@ private void toggle(KeyEvent ke, boolean state) { key = _key; } if (key != null) { + key.keybTick = 9; key.nextState = state; + key.nextState2 = state; ModSystem.keyEvent(key, state); } } + + public void toggleJoypad(Key key, boolean state) { + //if (key.keybTick > 2) { + // key.nextJoyState = false; + // return; + //} + //if (key.nextState && key.keybTick > 2) return; + key.nextState = (!key.nextState2)?state:key.nextState; + } } diff --git a/src/com/mojang/mojam/JoypadHandler.java b/src/com/mojang/mojam/JoypadHandler.java index d61e7f4d4..9d1e738b0 100644 --- a/src/com/mojang/mojam/JoypadHandler.java +++ b/src/com/mojang/mojam/JoypadHandler.java @@ -199,6 +199,7 @@ public void addAxis(Axis a, int i) { } public void toggleAxis(Axis a, float f) { + //Update axis status if (a.firstState == -2) { a.firstState = f; } @@ -220,19 +221,20 @@ public void toggleAxis(Axis a, float f) { InputHandler ih = ((InputHandler)MojamComponent.instance.getInputHandler()); Keys keys = MojamComponent.instance.keys; - if (a.name.equals(mouseXA.substring(shootXA.indexOf(":")+1)) && !((a.controller.getIndex()+"").equals(mouseXA.substring(mouseXA.indexOf(":")+1)))) { - int x = (int) (MojamComponent.instance.mouseButtons.getX() + f*2); - int y = MojamComponent.instance.mouseButtons.getY(); - MojamComponent.instance.mouseButtons.setPosition(x, y); + //Check if axis is X or Y axis of any special axis>key/mouse port . If it is , handle axis seperately to counterpart ( X|y , x|Y ) + if (a.name.equals(mouseXA.substring(mouseXA.indexOf(":")+1)) && !((a.controller.getIndex()+"").equals(mouseXA.substring(mouseXA.indexOf(":")+1)))) { + int x = (int) (f*5); + MojamComponent.instance.mouseButtons.jx = x; + MojamComponent.instance.joyMoved = true; } - if (a.name.equals(mouseYA.substring(shootXA.indexOf(":")+1)) && !((a.controller.getIndex()+"").equals(mouseYA.substring(mouseYA.indexOf(":")+1)))) { - int x = MojamComponent.instance.mouseButtons.getX(); - int y = (int) (MojamComponent.instance.mouseButtons.getY() + f*2); - MojamComponent.instance.mouseButtons.setPosition(x, y); + if (a.name.equals(mouseYA.substring(mouseXA.indexOf(":")+1)) && !((a.controller.getIndex()+"").equals(mouseYA.substring(mouseYA.indexOf(":")+1)))) { + int y = (int) (f*5); + MojamComponent.instance.mouseButtons.jy = y; + MojamComponent.instance.joyMoved = true; } if (a.name.equals(shootXA.substring(shootXA.indexOf(":")+1)) && !((a.controller.getIndex()+"").equals(shootXA.substring(shootXA.indexOf(":")+1)))) { - if (f > 0) { + /*if (f > 0) { ih.toggleJoypad(keys.fireRight, true); } else { ih.toggleJoypad(keys.fireRight, false); @@ -241,10 +243,19 @@ public void toggleAxis(Axis a, float f) { ih.toggleJoypad(keys.fireLeft, true); } else { ih.toggleJoypad(keys.fireLeft, false); + }*/ + // Accuracy Implementation + int x = (int) (f*20); + MojamComponent.instance.mouseButtons.sx = x; + if (x != 0) { + MojamComponent.instance.shootMoved = true; + ih.toggleJoypad(keys.fire, true); + } else { + ih.toggleJoypad(keys.fire, false); } } if (a.name.equals(shootYA.substring(shootYA.indexOf(":")+1)) && !((a.controller.getIndex()+"").equals(shootYA.substring(shootYA.indexOf(":")+1)))) { - if (f > 0) { + /*if (f > 0) { ih.toggleJoypad(keys.fireDown, true); } else { ih.toggleJoypad(keys.fireDown, false); @@ -253,6 +264,15 @@ public void toggleAxis(Axis a, float f) { ih.toggleJoypad(keys.fireUp, true); } else { ih.toggleJoypad(keys.fireUp, false); + }*/ + // Accuracy Implementation + int y = (int) (f*20); + MojamComponent.instance.mouseButtons.sy = y; + if (y != 0) { + MojamComponent.instance.shootMoved = true; + ih.toggleJoypad(keys.fire, true); + } else { + ih.toggleJoypad(keys.fire, false); } } diff --git a/src/com/mojang/mojam/Keys.java b/src/com/mojang/mojam/Keys.java index 3791938a2..fc55ce675 100644 --- a/src/com/mojang/mojam/Keys.java +++ b/src/com/mojang/mojam/Keys.java @@ -7,8 +7,10 @@ public class Keys { public final class Key { public final String name; public boolean nextState = false; + public boolean nextState2 = false; public boolean wasDown = false; public boolean isDown = false; + public int keybTick = 0; public Key(String name) { this.name = name; @@ -16,6 +18,8 @@ public Key(String name) { } public void tick() { + keybTick--; + if (keybTick < 0) keybTick = 0; wasDown = isDown; isDown = nextState; } diff --git a/src/com/mojang/mojam/MojamComponent.java b/src/com/mojang/mojam/MojamComponent.java index 689eb898f..e3292ee8b 100644 --- a/src/com/mojang/mojam/MojamComponent.java +++ b/src/com/mojang/mojam/MojamComponent.java @@ -24,6 +24,7 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Locale; import java.util.Random; @@ -35,6 +36,7 @@ import com.mojang.mojam.console.Console; import com.mojang.mojam.entity.Player; import com.mojang.mojam.entity.mob.Team; +import com.mojang.mojam.gui.AJoyBindingsMenu; import com.mojang.mojam.gui.AudioVideoMenu; import com.mojang.mojam.gui.CharacterSelectionMenu; import com.mojang.mojam.gui.CreditsScreen; @@ -44,6 +46,7 @@ import com.mojang.mojam.gui.HostingWaitMenu; import com.mojang.mojam.gui.HowToPlayMenu; import com.mojang.mojam.gui.JoinGameMenu; +import com.mojang.mojam.gui.JoyBindingsMenu; import com.mojang.mojam.gui.KeyBindingsMenu; import com.mojang.mojam.gui.LevelEditorMenu; import com.mojang.mojam.gui.LevelSelect; @@ -117,7 +120,9 @@ public class MojamComponent extends Canvas implements Runnable, MouseMotionListe private InputHandler inputHandler; private int lastX = 0; private int lastY = 0; - private boolean mouseMoved = false; + public boolean mouseMoved = false; + public boolean joyMoved = false; + public boolean shootMoved = false; private int mouseHideTime = 0; public MouseButtons mouseButtons = new MouseButtons(); public Keys keys = new Keys(); @@ -159,6 +164,24 @@ public MojamComponent() { + File.separator + "native" + File.separator; + //IGM2E BUILD 1 BETA 1+ RIPPED CODE + try { + Field field = ClassLoader.class.getDeclaredField("usr_paths"); + field.setAccessible(true); + String[] paths = (String[])field.get(null); + for (int i = 0; i < paths.length; i++) { + if (nativeLibDir.equals(paths[i])) { + return; + } + } + String[] tmp = new String[paths.length+1]; + System.arraycopy(paths,0,tmp,0,paths.length); + tmp[paths.length] = nativeLibDir; + field.set(null,tmp); + System.setProperty("java.library.path", System.getProperty("java.library.path") + File.pathSeparator + nativeLibDir); + } catch (Throwable t) { + t.printStackTrace(); + } System.setProperty("org.lwjgl.librarypath", nativeLibDir); // initialize the constants MojamComponent.constants = new Constants(); @@ -178,6 +201,7 @@ public MojamComponent() { addKeyListener(menuStack); addKeyListener(chat); addKeyListener(console); + JoypadHandler.init(); snatchClient = new SnatchClient(); snatchClient.setComponent(this); instance = this; @@ -418,7 +442,8 @@ public void run() { ModSystem.runOnce(); -while (running) { + while (running) { + JoypadHandler.tick(); ModSystem.updateTick(); if (!this.hasFocus()) { keys.release(); @@ -638,7 +663,7 @@ private void addScore(AbstractScreen screen) { private void renderMouse(AbstractScreen screen, MouseButtons mouseButtons) { - if (mouseButtons.mouseHidden) + if (!mouseButtons.renderMouse) return; int crosshairSize = 15; @@ -701,28 +726,8 @@ private void tick() { if (snatchClient != null) { snatchClient.tick(); } - - // Store virtual position of mouse (pre-scaled) - Point mousePosition = getMousePosition(); - if (mousePosition != null) { - mouseButtons.setPosition(mousePosition.x / scale, mousePosition.y / scale); - } - if (!menuStack.isEmpty()) { - menuStack.peek().tick(mouseButtons); - } - if (mouseMoved) { - mouseMoved = false; - mouseHideTime = 0; - if (mouseButtons.mouseHidden) { - mouseButtons.mouseHidden = false; - } - } - if (mouseHideTime < 60) { - mouseHideTime++; - if (mouseHideTime == 60) { - mouseButtons.mouseHidden = true; - } - } + + tickMouse(); if (sendCharacter) { synchronizer.addMessage(new CharacterMessage(localId, playerCharacter.ordinal())); @@ -814,6 +819,46 @@ private void tick() { ModSystem.afterTick(); } + private void tickMouse() { + // Store virtual position of mouse (pre-scaled) + Point mousePosition = getMousePosition(); + if (mousePosition != null) { + boolean inLevel = (level != null) && (!paused); + mouseButtons.updatePosition(mousePosition.x / scale, mousePosition.y / scale, mouseMoved, joyMoved || shootMoved, inLevel); + } + if (!menuStack.isEmpty()) { + menuStack.peek().tick(mouseButtons); + } + if (!mouseMoved && joyMoved) { + joyMoved = false; + mouseHideTime = 0; + if (mouseButtons.mouseHidden) { + mouseButtons.mouseHidden = false; + } + mouseButtons.renderMouse = true; + } + if (mouseMoved) { + mouseMoved = false; + mouseHideTime = 0; + if (mouseButtons.mouseHidden) { + mouseButtons.mouseHidden = false; + } + mouseButtons.renderMouse = true; + } + if (mouseHideTime < 60) { + mouseHideTime++; + if (mouseHideTime == 60) { + mouseButtons.mouseHidden = true; + mouseButtons.renderMouse = false; + } + } + if (shootMoved) { + shootMoved = false; + mouseButtons.mouseHidden = false; + mouseButtons.renderMouse = false; + } + } + private void tickChat() { if (chat.isOpen()) { keys.release(); @@ -1059,6 +1104,14 @@ public void handleAction(int id) { menuStack.add(new KeyBindingsMenu(keys, inputHandler)); break; + case TitleMenu.JOY_MENU: + menuStack.add(new JoyBindingsMenu(keys, inputHandler)); + break; + + case TitleMenu.AXES_MENU: + menuStack.add(new AJoyBindingsMenu()); + break; + case TitleMenu.LEVEL_EDITOR_ID: menuStack.add(new LevelEditorMenu()); break; @@ -1196,4 +1249,8 @@ public void windowClosing(WindowEvent winEvt) { } }; } + + public InputHandler getInputHandler() { + return inputHandler; + } } diff --git a/src/com/mojang/mojam/MouseButtons.java b/src/com/mojang/mojam/MouseButtons.java index fac408c61..2662be9e6 100644 --- a/src/com/mojang/mojam/MouseButtons.java +++ b/src/com/mojang/mojam/MouseButtons.java @@ -5,12 +5,19 @@ public class MouseButtons { public boolean mouseHidden = false; + public boolean renderMouse = false; public boolean[] currentState = new boolean[4]; public boolean[] nextState = new boolean[4]; + private int ox; + private int oy; private int x; private int y; + public int jx; + public int jy; + public int sx; + public int sy; public void setNextState(int button, boolean value) { if (button > 3) return; @@ -60,4 +67,24 @@ public void setPosition(int x, int y) { this.y = y; } + public void updatePosition(int nx, int ny, boolean mouseMoved, boolean joyMoved, boolean inLevel) { + ox = x; + oy = y; + if (!mouseMoved && joyMoved) { + if (sx != 0 && inLevel) { + x = MojamComponent.GAME_WIDTH/2 + sx; + } else { + x = x + jx; + } + if (sy != 0 && inLevel) { + y = (MojamComponent.GAME_HEIGHT/2 - 24) + sy; + } else { + y = y + jy; + } + } else if (mouseMoved) { + x = nx; + y = ny; + } + } + } diff --git a/src/com/mojang/mojam/entity/weapon/VenomShooter.java b/src/com/mojang/mojam/entity/weapon/VenomShooter.java index dbf87591f..39b497d13 100644 --- a/src/com/mojang/mojam/entity/weapon/VenomShooter.java +++ b/src/com/mojang/mojam/entity/weapon/VenomShooter.java @@ -16,12 +16,9 @@ public Bullet getAmmo(double xDir, double yDir) { Bullet bullet = new BulletPoison(owner, xDir, yDir, bulletDamage); if (!(owner instanceof Player)) bullet.pos.y = bullet.pos.y-19; //this will make the bullet look like its coming out of the snakes mouth -<<<<<<< HEAD else bullet.pos.y = bullet.pos.y; bullet.pos.x = bullet.pos.x; -======= ->>>>>>> origin return bullet; } diff --git a/src/com/mojang/mojam/gui/JoyBindingsMenu.java b/src/com/mojang/mojam/gui/JoyBindingsMenu.java index aadb9d700..0c34a5f2e 100644 --- a/src/com/mojang/mojam/gui/JoyBindingsMenu.java +++ b/src/com/mojang/mojam/gui/JoyBindingsMenu.java @@ -171,7 +171,7 @@ private void addButtons() { axes = addButton(new Button(TitleMenu.AXES_MENU, MojamComponent.texts.getStatic("options.axeBindings"), - 0, yOffset + numRows * BUTTON_SPACING + gameWidth - Button.BUTTON_WIDTH, yOffset + numRows * BUTTON_SPACING - Button.BUTTON_HEIGHT + 88)); back = addButton(new Button(TitleMenu.BACK_ID, MojamComponent.texts.getStatic("back"), diff --git a/src/com/mojang/mojam/gui/KeyBindingsMenu.java b/src/com/mojang/mojam/gui/KeyBindingsMenu.java index 5c66ccc4e..8527c5b4d 100644 --- a/src/com/mojang/mojam/gui/KeyBindingsMenu.java +++ b/src/com/mojang/mojam/gui/KeyBindingsMenu.java @@ -79,6 +79,8 @@ public void refresh() { private int textWidth; private int yOffset; + private ClickableComponent joystick; + private ClickableComponent back; private KeyBindingButton selectedKey = null; @@ -144,6 +146,10 @@ private void addButtons() { * BUTTON_SPACING)); + joystick = addButton(new Button(TitleMenu.JOY_MENU, MojamComponent.texts.getStatic("options.joyBindings"), + gameWidth - Button.BUTTON_WIDTH, yOffset + numRows * BUTTON_SPACING + - Button.BUTTON_HEIGHT + 88)); + back = addButton(new Button(TitleMenu.BACK_ID, MojamComponent.texts.getStatic("back"), (gameWidth - Button.BUTTON_WIDTH) / 2, yOffset + numRows * BUTTON_SPACING - Button.BUTTON_HEIGHT + 88)); @@ -186,7 +192,7 @@ public void render(AbstractScreen screen) { write(screen, txts.getStatic("keys.chat"), 1, 9); super.render(screen); ClickableComponent button = buttons.get(selectedItem); - if (button == back) { + if (button == back || button == joystick) { screen.blit(Art.getLocalPlayerArt()[0][6], back.getX() - 64, back.getY() - 8); } else { screen.blit(Art.getLocalPlayerArt()[0][6], button.getX() - textWidth - 32, @@ -207,7 +213,7 @@ public void buttonPressed(ClickableComponent button) { selectedKey.setSelected(false); selectedKey = null; } - if (button == back || swapped) { + if (button == back || button == joystick || swapped) { return; } selectedKey = (KeyBindingButton) button; diff --git a/src/com/mojang/mojam/gui/TitleMenu.java b/src/com/mojang/mojam/gui/TitleMenu.java index e132f7c92..f68141418 100644 --- a/src/com/mojang/mojam/gui/TitleMenu.java +++ b/src/com/mojang/mojam/gui/TitleMenu.java @@ -97,7 +97,46 @@ public class TitleMenu extends GuiMenu { public static final int LOCALE_SL_ID = 6011; public static final int LOCALE_SV_ID = 6012; public static final int LOCALE_AF_ID = 6013; - + + /* + * SMALL DOCUMENTATION FOR JOY_ IDS + * + * 7000 - 7999 are used for joystick buttons + * 8000 is used for axis menu + * 8001 - 8998 is used for axes + * 8999 is used for joystick button menu + * + */ + public static final int JOY_BINDINGS_ID = 7000; + public static final int JOY_UP_ID = 7001; + public static final int JOY_DOWN_ID = 7002; + public static final int JOY_LEFT_ID = 7003; + public static final int JOY_RIGHT_ID = 7004; + public static final int JOY_SPRINT_ID = 7005; + public static final int JOY_FIRE_ID = 7006; + public static final int JOY_USE_ID = 7007; + public static final int JOY_BUILD_ID = 7008; + public static final int JOY_UPGRADE_ID = 7009; + public static final int JOY_CHAT_ID = 7010; + public static final int JOY_FIRE_UP_ID = 7011; + public static final int JOY_FIRE_DOWN_ID = 7012; + public static final int JOY_FIRE_LEFT_ID = 7013; + public static final int JOY_FIRE_RIGHT_ID = 7014; + public static final int JOY_CYCLE_LEFT_ID = 7015; + public static final int JOY_CYCLE_RIGHT_ID = 7016; + public static final int JOY_WEAPON_SLOT_1_ID = 7017; + public static final int JOY_WEAPON_SLOT_2_ID = 7018; + public static final int JOY_WEAPON_SLOT_3_ID = 7019; + public static final int AXES_MENU = 8000; + public static final int JOY_MOUSEX_ID = 8001; + public static final int JOY_MOUSEY_ID = 8002; + public static final int JOY_MOVEX_ID = 8003; + public static final int JOY_MOVEY_ID = 8004; + public static final int JOY_SHOOTX_ID = 8005; + public static final int JOY_SHOOTY_ID = 8006; + public static final int JOY_MENU= 7999; + + public static LevelInformation level = null; public static GameMode defaultGameMode = new GameModeVanilla(); public static DifficultyInformation difficulty = DifficultyInformation.NORMAL; diff --git a/src/com/mojang/mojam/resources/Texts.java b/src/com/mojang/mojam/resources/Texts.java index 218adc7a3..a37cc161d 100644 --- a/src/com/mojang/mojam/resources/Texts.java +++ b/src/com/mojang/mojam/resources/Texts.java @@ -134,4 +134,8 @@ public String removeRail(int cost) { return MessageFormat.format(getStatic("build.removeRail"), cost); } + public String getFormated(String key, String arug) { + return MessageFormat.format(getStatic(key), arug); + } + } \ No newline at end of file