1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/*
* $Id: pad.d,v 1.2 2005/07/03 07:05:23 kenta Exp $
*
* Copyright 2004 Kenta Cho. Some rights reserved.
*/
module abagames.util.sdl.pad;
private import std.string;
private import std.stream;
private import SDL;
private import abagames.util.sdl.input;
private import abagames.util.sdl.recordableinput;
/**
* Joystick and keyboard input.
*/
public class Pad: Input {
public:
Uint8 *keys;
bool buttonReversed = false;
private:
SDL_Joystick *stick = null;
const int JOYSTICK_AXIS = 16384;
PadState state;
public this() {
state = new PadState;
}
public SDL_Joystick* openJoystick(SDL_Joystick *st = null) {
if (st == null) {
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
return null;
stick = SDL_JoystickOpen(0);
} else {
stick = st;
}
return stick;
}
public void handleEvent(SDL_Event *event) {
keys = SDL_GetKeyState(null);
}
public PadState getState() {
int x = 0, y = 0;
state.dir = 0;
if (stick) {
x = SDL_JoystickGetAxis(stick, 0);
y = SDL_JoystickGetAxis(stick, 1);
}
if (keys[SDLK_RIGHT] == SDL_PRESSED || keys[SDLK_KP6] == SDL_PRESSED ||
keys[SDLK_d] == SDL_PRESSED || keys[SDLK_l] == SDL_PRESSED ||
x > JOYSTICK_AXIS)
state.dir |= PadState.Dir.RIGHT;
if (keys[SDLK_LEFT] == SDL_PRESSED || keys[SDLK_KP4] == SDL_PRESSED ||
keys[SDLK_a] == SDL_PRESSED || keys[SDLK_j] == SDL_PRESSED ||
x < -JOYSTICK_AXIS)
state.dir |= PadState.Dir.LEFT;
if (keys[SDLK_DOWN] == SDL_PRESSED || keys[SDLK_KP2] == SDL_PRESSED ||
keys[SDLK_s] == SDL_PRESSED || keys[SDLK_k] == SDL_PRESSED ||
y > JOYSTICK_AXIS)
state.dir |= PadState.Dir.DOWN;
if (keys[SDLK_UP] == SDL_PRESSED || keys[SDLK_KP8] == SDL_PRESSED ||
keys[SDLK_w] == SDL_PRESSED || keys[SDLK_i] == SDL_PRESSED ||
y < -JOYSTICK_AXIS)
state.dir |= PadState.Dir.UP;
state.button = 0;
int btn1 = 0, btn2 = 0;
float leftTrigger = 0, rightTrigger = 0;
if (stick) {
btn1 = SDL_JoystickGetButton(stick, 0) + SDL_JoystickGetButton(stick, 3) +
SDL_JoystickGetButton(stick, 4) + SDL_JoystickGetButton(stick, 7) +
SDL_JoystickGetButton(stick, 8) + SDL_JoystickGetButton(stick, 11);
btn2 = SDL_JoystickGetButton(stick, 1) + SDL_JoystickGetButton(stick, 2) +
SDL_JoystickGetButton(stick, 5) + SDL_JoystickGetButton(stick, 6) +
SDL_JoystickGetButton(stick, 9) + SDL_JoystickGetButton(stick, 10);
}
if (keys[SDLK_z] == SDL_PRESSED || keys[SDLK_PERIOD] == SDL_PRESSED ||
keys[SDLK_LCTRL] == SDL_PRESSED || keys[SDLK_RCTRL] == SDL_PRESSED ||
btn1) {
if (!buttonReversed)
state.button |= PadState.Button.A;
else
state.button |= PadState.Button.B;
}
if (keys[SDLK_x] == SDL_PRESSED || keys[SDLK_SLASH] == SDL_PRESSED ||
keys[SDLK_LALT] == SDL_PRESSED || keys[SDLK_RALT] == SDL_PRESSED ||
keys[SDLK_LSHIFT] == SDL_PRESSED || keys[SDLK_RSHIFT] == SDL_PRESSED ||
keys[SDLK_RETURN] == SDL_PRESSED ||
btn2) {
if (!buttonReversed)
state.button |= PadState.Button.B;
else
state.button |= PadState.Button.A;
}
return state;
}
public PadState getNullState() {
state.clear();
return state;
}
}
public class PadState {
public:
static enum Dir {
UP = 1, DOWN = 2, LEFT = 4, RIGHT = 8,
};
static enum Button {
A = 16, B = 32, ANY = 48,
};
int dir, button;
private:
public static PadState newInstance() {
return new PadState;
}
public static PadState newInstance(PadState s) {
return new PadState(s);
}
public this() {
}
public this(PadState s) {
this();
set(s);
}
public void set(PadState s) {
dir = s.dir;
button = s.button;
}
public void clear() {
dir = button = 0;
}
public void read(File fd) {
int s;
fd.read(s);
dir = s & (Dir.UP | Dir.DOWN | Dir.LEFT | Dir.RIGHT);
button = s & Button.ANY;
}
public void write(File fd) {
int s = dir | button;
fd.write(s);
}
public bool equals(PadState s) {
if (dir == s.dir && button == s.button)
return true;
else
return false;
}
}
public class RecordablePad: Pad {
mixin RecordableInput!(PadState);
private:
public override PadState getState() {
return getState(true);
}
public PadState getState(bool doRecord) {
PadState s = super.getState();
if (doRecord)
record(s);
return s;
}
}
|