/* Copyright 2010-2012 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut. If not, see <http://www.gnu.org/licenses/>.
*/
package ponkOut.input;
import java.util.LinkedList;
import org.lwjgl.input.Mouse;
import org.lwjgl.util.vector.Vector2f;
import ponkOut.Settings;
public class MouseInput implements InputDevice {
/** number of old movement values included for smoothing */
private static final int SMOOTHING_COUNT = 10;
/** weight value i as smoothingWeight[i] (oldest first) */
private static float smoothingWeight[] = new float[SMOOTHING_COUNT];
private float speed;
private InputManager inputManager;
private LinkedList<Vector2f> movementHistory = new LinkedList<Vector2f>();
static {
// use Gaussian function for weighting
float sum = 0.0f;
for (int i = 0; i < SMOOTHING_COUNT; ++i) {
double x = i / (SMOOTHING_COUNT - 1.0);
smoothingWeight[i] = (float) Math.exp(-20.0 * (x - 1.0) * (x - 1.0));
sum += smoothingWeight[i];
}
// normalize weights
for (int i = 0; i < SMOOTHING_COUNT; ++i)
smoothingWeight[i] /= sum;
}
public MouseInput(int player, InputManager inputManager) {
Settings settings = Settings.getInstance();
this.inputManager = inputManager;
speed = settings.getMouseSpeed(player) * 0.007f;
// init history
for (int i = 0; i < SMOOTHING_COUNT; ++i)
movementHistory.add(new Vector2f(0.0f, 0.0f));
}
@Override
public Vector2f getMovement(float elapsedTime) {
Vector2f cur = new Vector2f(inputManager.getMouseMovementX(), inputManager.getMouseMovementY());
// remove oldest element from list and add current
movementHistory.remove();
movementHistory.add(cur);
// computed smoothed value
Vector2f v = new Vector2f(0.0f, 0.0f);
int i = 0;
for (Vector2f vi : movementHistory) {
v.x += vi.x * smoothingWeight[i];
v.y += vi.y * smoothingWeight[i];
++i;
}
v.scale(speed / elapsedTime);
return v;
}
@Override
public boolean isButtonDown() {
return Mouse.isButtonDown(0);
}
}