|
Matthias@233
|
1 |
/* |
|
Matthias@260
|
2 |
* Copyright (c) 2008-2012, Matthias Mann |
|
Matthias@233
|
3 |
* |
|
Matthias@233
|
4 |
* All rights reserved. |
|
Matthias@233
|
5 |
* |
|
Matthias@233
|
6 |
* Redistribution and use in source and binary forms, with or without |
|
Matthias@233
|
7 |
* modification, are permitted provided that the following conditions are met: |
|
Matthias@233
|
8 |
* |
|
Matthias@233
|
9 |
* * Redistributions of source code must retain the above copyright notice, |
|
Matthias@233
|
10 |
* this list of conditions and the following disclaimer. |
|
Matthias@233
|
11 |
* * Redistributions in binary form must reproduce the above copyright |
|
Matthias@233
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
Matthias@233
|
13 |
* documentation and/or other materials provided with the distribution. |
|
Matthias@233
|
14 |
* * Neither the name of Matthias Mann nor the names of its contributors may |
|
Matthias@233
|
15 |
* be used to endorse or promote products derived from this software |
|
Matthias@233
|
16 |
* without specific prior written permission. |
|
Matthias@233
|
17 |
* |
|
Matthias@233
|
18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
Matthias@233
|
19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
Matthias@233
|
20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
Matthias@233
|
21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
Matthias@233
|
22 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
Matthias@233
|
23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
Matthias@233
|
24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
Matthias@233
|
25 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
Matthias@233
|
26 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
Matthias@233
|
27 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
Matthias@233
|
28 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
Matthias@233
|
29 |
*/ |
|
Matthias@233
|
30 |
package login; |
|
Matthias@233
|
31 |
|
|
Matthias@233
|
32 |
import de.matthiasmann.twl.Button; |
|
Matthias@233
|
33 |
import de.matthiasmann.twl.DialogLayout; |
|
Matthias@233
|
34 |
import de.matthiasmann.twl.EditField; |
|
Matthias@260
|
35 |
import de.matthiasmann.twl.EditField.Callback; |
|
Matthias@260
|
36 |
import de.matthiasmann.twl.Event; |
|
Matthias@233
|
37 |
import de.matthiasmann.twl.FPSCounter; |
|
Matthias@233
|
38 |
import de.matthiasmann.twl.GUI; |
|
Matthias@233
|
39 |
import de.matthiasmann.twl.Label; |
|
Matthias@233
|
40 |
import de.matthiasmann.twl.Timer; |
|
Matthias@233
|
41 |
import de.matthiasmann.twl.Widget; |
|
Matthias@233
|
42 |
import de.matthiasmann.twl.renderer.lwjgl.LWJGLRenderer; |
|
Matthias@233
|
43 |
import de.matthiasmann.twl.theme.ThemeManager; |
|
Matthias@233
|
44 |
import org.lwjgl.opengl.Display; |
|
Matthias@233
|
45 |
import org.lwjgl.opengl.DisplayMode; |
|
Matthias@233
|
46 |
import org.lwjgl.opengl.GL11; |
|
Matthias@253
|
47 |
import test.TestUtils; |
|
Matthias@233
|
48 |
|
|
Matthias@233
|
49 |
/** |
|
Matthias@233
|
50 |
* A simple login panel |
|
Matthias@233
|
51 |
* |
|
Matthias@233
|
52 |
* @author Matthias Mann |
|
Matthias@233
|
53 |
*/ |
|
Matthias@233
|
54 |
public class LoginDemo extends Widget { |
|
Matthias@233
|
55 |
|
|
Matthias@233
|
56 |
public static void main(String[] args) { |
|
Matthias@233
|
57 |
try { |
|
Matthias@233
|
58 |
Display.setDisplayMode(new DisplayMode(800, 600)); |
|
Matthias@233
|
59 |
Display.create(); |
|
Matthias@235
|
60 |
Display.setTitle("TWL Login Panel Demo"); |
|
Matthias@233
|
61 |
Display.setVSyncEnabled(true); |
|
Matthias@233
|
62 |
|
|
Matthias@233
|
63 |
LoginDemo demo = new LoginDemo(); |
|
Matthias@233
|
64 |
|
|
Matthias@233
|
65 |
LWJGLRenderer renderer = new LWJGLRenderer(); |
|
Matthias@233
|
66 |
GUI gui = new GUI(demo, renderer); |
|
Matthias@233
|
67 |
|
|
Matthias@260
|
68 |
demo.efName.requestKeyboardFocus(); |
|
Matthias@260
|
69 |
|
|
Matthias@233
|
70 |
ThemeManager theme = ThemeManager.createThemeManager( |
|
Matthias@233
|
71 |
LoginDemo.class.getResource("login.xml"), renderer); |
|
Matthias@233
|
72 |
gui.applyTheme(theme); |
|
Matthias@233
|
73 |
|
|
Matthias@233
|
74 |
while(!Display.isCloseRequested() && !demo.quit) { |
|
Matthias@233
|
75 |
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); |
|
Matthias@233
|
76 |
|
|
Matthias@233
|
77 |
gui.update(); |
|
Matthias@233
|
78 |
Display.update(); |
|
Matthias@233
|
79 |
} |
|
Matthias@233
|
80 |
|
|
Matthias@233
|
81 |
gui.destroy(); |
|
Matthias@233
|
82 |
theme.destroy(); |
|
Matthias@233
|
83 |
} catch (Exception ex) { |
|
Matthias@253
|
84 |
TestUtils.showErrMsg(ex); |
|
Matthias@233
|
85 |
} |
|
Matthias@233
|
86 |
Display.destroy(); |
|
Matthias@233
|
87 |
} |
|
Matthias@233
|
88 |
|
|
Matthias@233
|
89 |
final FPSCounter fpsCounter; |
|
Matthias@233
|
90 |
final DialogLayout loginPanel; |
|
Matthias@233
|
91 |
final EditField efName; |
|
Matthias@233
|
92 |
final EditField efPassword; |
|
Matthias@233
|
93 |
final Button btnLogin; |
|
Matthias@233
|
94 |
|
|
Matthias@233
|
95 |
boolean quit; |
|
Matthias@233
|
96 |
|
|
Matthias@233
|
97 |
public LoginDemo() { |
|
Matthias@233
|
98 |
fpsCounter = new FPSCounter(); |
|
Matthias@233
|
99 |
|
|
Matthias@233
|
100 |
loginPanel = new DialogLayout(); |
|
Matthias@233
|
101 |
loginPanel.setTheme("login-panel"); |
|
Matthias@233
|
102 |
|
|
Matthias@233
|
103 |
efName = new EditField(); |
|
Matthias@260
|
104 |
efName.addCallback(new Callback() { |
|
Matthias@260
|
105 |
public void callback(int key) { |
|
Matthias@260
|
106 |
if(key == Event.KEY_RETURN) { |
|
Matthias@260
|
107 |
efPassword.requestKeyboardFocus(); |
|
Matthias@260
|
108 |
} |
|
Matthias@260
|
109 |
} |
|
Matthias@260
|
110 |
}); |
|
Matthias@233
|
111 |
|
|
Matthias@233
|
112 |
efPassword = new EditField(); |
|
Matthias@233
|
113 |
efPassword.setPasswordMasking(true); |
|
Matthias@260
|
114 |
efPassword.addCallback(new Callback() { |
|
Matthias@260
|
115 |
public void callback(int key) { |
|
Matthias@260
|
116 |
if(key == Event.KEY_RETURN) { |
|
Matthias@260
|
117 |
emulateLogin(); |
|
Matthias@260
|
118 |
} |
|
Matthias@260
|
119 |
} |
|
Matthias@260
|
120 |
}); |
|
Matthias@233
|
121 |
|
|
Matthias@233
|
122 |
Label lName = new Label("Name"); |
|
Matthias@233
|
123 |
lName.setLabelFor(efName); |
|
Matthias@233
|
124 |
|
|
Matthias@233
|
125 |
Label lPassword = new Label("Password"); |
|
Matthias@233
|
126 |
lPassword.setLabelFor(efPassword); |
|
Matthias@233
|
127 |
|
|
Matthias@233
|
128 |
btnLogin = new Button("LOGIN"); |
|
Matthias@233
|
129 |
btnLogin.addCallback(new Runnable() { |
|
Matthias@233
|
130 |
public void run() { |
|
Matthias@233
|
131 |
emulateLogin(); |
|
Matthias@233
|
132 |
} |
|
Matthias@233
|
133 |
}); |
|
Matthias@233
|
134 |
|
|
Matthias@233
|
135 |
DialogLayout.Group hLabels = loginPanel.createParallelGroup(lName, lPassword); |
|
Matthias@233
|
136 |
DialogLayout.Group hFields = loginPanel.createParallelGroup(efName, efPassword); |
|
Matthias@233
|
137 |
DialogLayout.Group hBtn = loginPanel.createSequentialGroup() |
|
Matthias@233
|
138 |
.addGap() // right align the button by using a variable gap |
|
Matthias@233
|
139 |
.addWidget(btnLogin); |
|
Matthias@233
|
140 |
|
|
Matthias@233
|
141 |
loginPanel.setHorizontalGroup(loginPanel.createParallelGroup() |
|
Matthias@233
|
142 |
.addGroup(loginPanel.createSequentialGroup(hLabels, hFields)) |
|
Matthias@233
|
143 |
.addGroup(hBtn)); |
|
Matthias@233
|
144 |
loginPanel.setVerticalGroup(loginPanel.createSequentialGroup() |
|
Matthias@233
|
145 |
.addGroup(loginPanel.createParallelGroup(lName, efName)) |
|
Matthias@233
|
146 |
.addGroup(loginPanel.createParallelGroup(lPassword, efPassword)) |
|
Matthias@233
|
147 |
.addWidget(btnLogin)); |
|
Matthias@233
|
148 |
|
|
Matthias@233
|
149 |
add(fpsCounter); |
|
Matthias@233
|
150 |
add(loginPanel); |
|
Matthias@233
|
151 |
} |
|
Matthias@233
|
152 |
|
|
Matthias@233
|
153 |
@Override |
|
Matthias@233
|
154 |
protected void layout() { |
|
Matthias@233
|
155 |
// fpsCounter is bottom right |
|
Matthias@233
|
156 |
fpsCounter.adjustSize(); |
|
Matthias@233
|
157 |
fpsCounter.setPosition( |
|
Matthias@233
|
158 |
getInnerRight() - fpsCounter.getWidth(), |
|
Matthias@233
|
159 |
getInnerBottom() - fpsCounter.getHeight()); |
|
Matthias@233
|
160 |
|
|
Matthias@233
|
161 |
// login panel is centered |
|
Matthias@233
|
162 |
loginPanel.adjustSize(); |
|
Matthias@233
|
163 |
loginPanel.setPosition( |
|
Matthias@233
|
164 |
getInnerX() + (getInnerWidth() - loginPanel.getWidth())/2, |
|
Matthias@233
|
165 |
getInnerY() + (getInnerHeight() - loginPanel.getHeight())/2); |
|
Matthias@233
|
166 |
} |
|
Matthias@233
|
167 |
|
|
Matthias@233
|
168 |
void emulateLogin() { |
|
Matthias@233
|
169 |
GUI gui = getGUI(); |
|
Matthias@233
|
170 |
if(gui != null) { |
|
Matthias@233
|
171 |
// step 1: disable all controls |
|
Matthias@233
|
172 |
efName.setEnabled(false); |
|
Matthias@233
|
173 |
efPassword.setEnabled(false); |
|
Matthias@233
|
174 |
btnLogin.setEnabled(false); |
|
Matthias@233
|
175 |
|
|
Matthias@260
|
176 |
// step 2: get name & password from UI |
|
Matthias@260
|
177 |
String name = efName.getText(); |
|
Matthias@260
|
178 |
String pasword = efPassword.getText(); |
|
Matthias@260
|
179 |
System.out.println("Name: " + name + " with a " + pasword.length() + " character password"); |
|
Matthias@260
|
180 |
|
|
Matthias@260
|
181 |
// step 3: start a timer to simulate the process of talking to a remote server |
|
Matthias@233
|
182 |
Timer timer = gui.createTimer(); |
|
Matthias@233
|
183 |
timer.setCallback(new Runnable() { |
|
Matthias@233
|
184 |
public void run() { |
|
Matthias@233
|
185 |
// once the timer fired re-enable the controls and clear the password |
|
Matthias@233
|
186 |
efName.setEnabled(true); |
|
Matthias@233
|
187 |
efPassword.setEnabled(true); |
|
Matthias@233
|
188 |
efPassword.setText(""); |
|
Matthias@260
|
189 |
efPassword.requestKeyboardFocus(); |
|
Matthias@233
|
190 |
btnLogin.setEnabled(true); |
|
Matthias@233
|
191 |
} |
|
Matthias@233
|
192 |
}); |
|
Matthias@233
|
193 |
timer.setDelay(2500); |
|
Matthias@233
|
194 |
timer.start(); |
|
Matthias@233
|
195 |
|
|
Matthias@233
|
196 |
/* NOTE: in a real app you would need to keep a reference to the timer object |
|
Matthias@233
|
197 |
* to cancel it if the user closes the dialog which uses the timer. |
|
Matthias@233
|
198 |
* @see Widget#beforeRemoveFromGUI(de.matthiasmann.twl.GUI) |
|
Matthias@233
|
199 |
*/ |
|
Matthias@233
|
200 |
} |
|
Matthias@233
|
201 |
} |
|
Matthias@233
|
202 |
} |