updated web site publishing script to work with latest javainspect
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / humaninput / KeyboardFocusStack.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.gui.humaninput;
6
7 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
8
9 import java.util.Stack;
10
11 public class KeyboardFocusStack {
12
13     private final ViewPanel viewPanel;
14     private WorldNavigationUserInputTracker defaultInputHandler = new WorldNavigationUserInputTracker();
15     private Stack<KeyboardInputHandler> inputHandlers = new Stack<>();
16     private KeyboardInputHandler currentUserInputHandler;
17
18     public KeyboardFocusStack(final ViewPanel viewPanel) {
19         this.viewPanel = viewPanel;
20         pushFocusOwner(defaultInputHandler);
21     }
22
23     public KeyboardInputHandler getCurrentFocusOwner() {
24         return currentUserInputHandler;
25     }
26
27     public void popFocusOwner() {
28         if (currentUserInputHandler != null)
29             currentUserInputHandler.focusLost(viewPanel);
30
31         if (inputHandlers.isEmpty())
32             return;
33
34         currentUserInputHandler = inputHandlers.pop();
35         currentUserInputHandler.focusReceived(viewPanel);
36     }
37
38     public boolean pushFocusOwner(final KeyboardInputHandler newInputHandler) {
39         boolean updateNeeded = false;
40
41         if (currentUserInputHandler == newInputHandler)
42             return false;
43
44         if (currentUserInputHandler != null) {
45             updateNeeded = currentUserInputHandler.focusLost(viewPanel);
46             inputHandlers.push(currentUserInputHandler);
47         }
48
49         currentUserInputHandler = newInputHandler;
50         updateNeeded |= currentUserInputHandler.focusReceived(viewPanel);
51
52         return updateNeeded;
53     }
54 }