2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.gui.humaninput;
7 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
9 import java.util.Stack;
11 public class KeyboardFocusStack {
13 private final ViewPanel viewPanel;
14 private WorldNavigationUserInputTracker defaultInputHandler = new WorldNavigationUserInputTracker();
15 private Stack<KeyboardInputHandler> inputHandlers = new Stack<>();
16 private KeyboardInputHandler currentUserInputHandler;
18 public KeyboardFocusStack(final ViewPanel viewPanel) {
19 this.viewPanel = viewPanel;
20 pushFocusOwner(defaultInputHandler);
23 public KeyboardInputHandler getCurrentFocusOwner() {
24 return currentUserInputHandler;
27 public void popFocusOwner() {
28 if (currentUserInputHandler != null)
29 currentUserInputHandler.focusLost(viewPanel);
31 if (inputHandlers.isEmpty())
34 currentUserInputHandler = inputHandlers.pop();
35 currentUserInputHandler.focusReceived(viewPanel);
38 public boolean pushFocusOwner(final KeyboardInputHandler newInputHandler) {
39 boolean updateNeeded = false;
41 if (currentUserInputHandler == newInputHandler)
44 if (currentUserInputHandler != null) {
45 updateNeeded = currentUserInputHandler.focusLost(viewPanel);
46 inputHandlers.push(currentUserInputHandler);
49 currentUserInputHandler = newInputHandler;
50 updateNeeded |= currentUserInputHandler.focusReceived(viewPanel);