Skip to content

Commit

Permalink
Joypad Support v.2.0 : - Added more accuracy to shooting; - Added mor…
Browse files Browse the repository at this point in the history
…e 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 .
  • Loading branch information
0x0ade committed May 18, 2012
1 parent bd870a0 commit f94cf65
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 43 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
<classpathentry kind="lib" path="lib/lwjgl.jar"/>
<classpathentry kind="lib" path="lib/SoundSystem.jar"/>
<classpathentry kind="lib" path="lib/kryonet-1.04-all.jar"/>
<classpathentry kind="lib" path="lib/jinput.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
26 changes: 25 additions & 1 deletion res/translations/texts.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
11 changes: 11 additions & 0 deletions src/com/mojang/mojam/InputHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
40 changes: 30 additions & 10 deletions src/com/mojang/mojam/JoypadHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/com/mojang/mojam/Keys.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ 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;
all.add(this);
}

public void tick() {
keybTick--;
if (keybTick < 0) keybTick = 0;
wasDown = isDown;
isDown = nextState;
}
Expand Down
107 changes: 82 additions & 25 deletions src/com/mojang/mojam/MojamComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -178,6 +201,7 @@ public MojamComponent() {
addKeyListener(menuStack);
addKeyListener(chat);
addKeyListener(console);
JoypadHandler.init();
snatchClient = new SnatchClient();
snatchClient.setComponent(this);
instance = this;
Expand Down Expand Up @@ -418,7 +442,8 @@ public void run() {
ModSystem.runOnce();


while (running) {
while (running) {
JoypadHandler.tick();
ModSystem.updateTick();
if (!this.hasFocus()) {
keys.release();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1196,4 +1249,8 @@ public void windowClosing(WindowEvent winEvt) {
}
};
}

public InputHandler getInputHandler() {
return inputHandler;
}
}
Loading

0 comments on commit f94cf65

Please sign in to comment.