A Keyboard controlled camera. Standard First-Person camera controls with the arrow keys + orbit while holding down shift. Smooths out attack and release of the keys.
| 19 | * release of the keys. |
| 20 | */ |
| 21 | public class KeyboardCamera implements Function<Window.Event<KeyboardState>, Boolean> { |
| 22 | |
| 23 | private final Camera target; |
| 24 | private Window w; |
| 25 | |
| 26 | |
| 27 | public class Applicator { |
| 28 | float amount; |
| 29 | BiFunction<Camera.State, Float, Camera.State> apply; |
| 30 | |
| 31 | public Applicator(float amount, BiFunction<Camera.State, Float, Camera.State> apply) { |
| 32 | this.amount = amount; |
| 33 | this.apply = apply; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | Map<KeyBinding.KeyName, Applicator> bindings = new LinkedHashMap<>(); |
| 38 | |
| 39 | public float rotationAmount = 0.02f; |
| 40 | public float translationAmount = 0.02f; |
| 41 | |
| 42 | public float decay = 0.9f; |
| 43 | public float onset = 0.4f; |
| 44 | |
| 45 | public void standardMap() { |
| 46 | bindings.put(new KeyBinding.KeyName("page_up"), new Applicator(0, (state, amount) -> state |
| 47 | .lookUp(rotationAmount * amount))); |
| 48 | bindings.put(new KeyBinding.KeyName("shift-page_up"), new Applicator(0, (state, amount) -> state |
| 49 | .orbitUp(rotationAmount * amount))); |
| 50 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_PAGE_DOWN, false, false, false, false), new Applicator(0, (state, amount) -> state |
| 51 | .lookUp(rotationAmount * -amount))); |
| 52 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_PAGE_DOWN, true, false, false, false), new Applicator(0, (state, amount) -> state |
| 53 | .orbitUp(rotationAmount * -amount))); |
| 54 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_LEFT, false, false, false, false), new Applicator(0, (state, amount) -> state |
| 55 | .lookLeft(rotationAmount * amount))); |
| 56 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_LEFT, true, false, false, false), new Applicator(0, (state, amount) -> state |
| 57 | .orbitLeft(rotationAmount * amount))); |
| 58 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_RIGHT, false, false, false, false), new Applicator(0, (state, amount) -> state |
| 59 | .lookLeft(-rotationAmount * amount))); |
| 60 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_RIGHT, true, false, false, false), new Applicator(0, (state, amount) -> state |
| 61 | .orbitLeft(-rotationAmount * amount))); |
| 62 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_UP, false, false, false, false), new Applicator(0, (state, amount) -> state |
| 63 | .translateIn(translationAmount * amount))); |
| 64 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_UP, true, false, false, false), new Applicator(0, (state, amount) -> state |
| 65 | .dollyIn(translationAmount * amount))); |
| 66 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_DOWN, false, false, false, false), new Applicator(0, (state, amount) -> state |
| 67 | .translateIn(-translationAmount * amount))); |
| 68 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_DOWN, true, false, false, false), new Applicator(0, (state, amount) -> state |
| 69 | .dollyIn(-translationAmount * amount))); |
| 70 | |
| 71 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_LEFT_BRACKET, false, false, false, false), new Applicator(0, (state, amount) -> state |
| 72 | .roll(-rotationAmount * amount))); |
| 73 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_RIGHT_BRACKET, false, false, false, false), new Applicator(0, (state, amount) -> state |
| 74 | .roll(rotationAmount * amount))); |
| 75 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_LEFT_BRACKET, true, false, false, false), new Applicator(0, (state, amount) -> state |
| 76 | .zoomOut(amount))); |
| 77 | bindings.put(new KeyBinding.KeyName(GLFW_KEY_RIGHT_BRACKET, true, false, false, false), new Applicator(0, (state, amount) -> state |
| 78 | .zoomIn(amount))); |
no outgoing calls
no test coverage detected