Optimized frame repainting. Fixed mouse click processing.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / humaninput / KeyboardFocusStack.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.gui.humaninput;
11
12 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
13
14 import java.util.Stack;
15
16 public class KeyboardFocusStack {
17
18     private final ViewPanel viewPanel;
19     private WorldNavigationUserInputTracker defaultInputHandler = new WorldNavigationUserInputTracker();
20     private Stack<UserInputHandler> inputHandlers = new Stack<>();
21     private UserInputHandler currentUserInputHandler;
22
23     public KeyboardFocusStack(final ViewPanel viewPanel) {
24         this.viewPanel = viewPanel;
25         pushFocusOwner(defaultInputHandler);
26     }
27
28     public UserInputHandler getCurrentFocusOwner() {
29         return currentUserInputHandler;
30     }
31
32     public void popFocusOwner() {
33         if (currentUserInputHandler != null)
34             currentUserInputHandler.focusLost(viewPanel);
35
36         if (inputHandlers.isEmpty())
37             return;
38
39         currentUserInputHandler = inputHandlers.pop();
40         currentUserInputHandler.focusReceived(viewPanel);
41     }
42
43     public boolean pushFocusOwner(final UserInputHandler newInputHandler) {
44         boolean updateNeeded = false;
45
46         if (currentUserInputHandler == newInputHandler)
47             return false;
48
49         if (currentUserInputHandler != null) {
50             updateNeeded = currentUserInputHandler.focusLost(viewPanel);
51             inputHandlers.push(currentUserInputHandler);
52         }
53
54         currentUserInputHandler = newInputHandler;
55         updateNeeded |= currentUserInputHandler.focusReceived(viewPanel);
56
57         return updateNeeded;
58     }
59 }