TButton animate for keyboard
[fanfix.git] / src / jexer / TButton.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2017 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer;
30
31 import jexer.bits.CellAttributes;
32 import jexer.bits.Color;
33 import jexer.bits.GraphicsChars;
34 import jexer.bits.MnemonicString;
35 import jexer.event.TKeypressEvent;
36 import jexer.event.TMouseEvent;
37 import static jexer.TKeypress.*;
38
39 /**
40 * TButton implements a simple button. To make the button do something, pass
41 * a TAction class to its constructor.
42 *
43 * @see TAction#DO()
44 */
45 public final class TButton extends TWidget {
46
47 /**
48 * The shortcut and button text.
49 */
50 private MnemonicString mnemonic;
51
52 /**
53 * Get the mnemonic string for this button.
54 *
55 * @return mnemonic string
56 */
57 public MnemonicString getMnemonic() {
58 return mnemonic;
59 }
60
61 /**
62 * Remember mouse state.
63 */
64 private TMouseEvent mouse;
65
66 /**
67 * True when the button is being pressed and held down.
68 */
69 private boolean inButtonPress = false;
70
71 /**
72 * The action to perform when the button is clicked.
73 */
74 private TAction action;
75
76 /**
77 * The time at which dispatch() was called.
78 */
79 private long dispatchTime;
80
81 /**
82 * How long to animate dispatch of the event in millis.
83 */
84 private static final long DISPATCH_TIME = 75;
85
86 /**
87 * Act as though the button was pressed. This is useful for other UI
88 * elements to get the same action as if the user clicked the button.
89 */
90 public void dispatch() {
91 if (action != null) {
92 long now = System.currentTimeMillis();
93 if (now - dispatchTime > DISPATCH_TIME) {
94 action.DO();
95 dispatchTime = now;
96 }
97 }
98 }
99
100 /**
101 * Private constructor.
102 *
103 * @param parent parent widget
104 * @param text label on the button
105 * @param x column relative to parent
106 * @param y row relative to parent
107 */
108 private TButton(final TWidget parent, final String text,
109 final int x, final int y) {
110
111 // Set parent and window
112 super(parent);
113
114 mnemonic = new MnemonicString(text);
115
116 setX(x);
117 setY(y);
118 setHeight(2);
119 setWidth(mnemonic.getRawLabel().length() + 3);
120 }
121
122 /**
123 * Public constructor.
124 *
125 * @param parent parent widget
126 * @param text label on the button
127 * @param x column relative to parent
128 * @param y row relative to parent
129 * @param action to call when button is pressed
130 */
131 public TButton(final TWidget parent, final String text,
132 final int x, final int y, final TAction action) {
133
134 this(parent, text, x, y);
135 this.action = action;
136 }
137
138 /**
139 * Returns true if the mouse is currently on the button.
140 *
141 * @return if true the mouse is currently on the button
142 */
143 private boolean mouseOnButton() {
144 int rightEdge = getWidth() - 1;
145 if (inButtonPress) {
146 rightEdge++;
147 }
148 if ((mouse != null)
149 && (mouse.getY() == 0)
150 && (mouse.getX() >= 0)
151 && (mouse.getX() < rightEdge)
152 ) {
153 return true;
154 }
155 return false;
156 }
157
158 /**
159 * Draw a button with a shadow.
160 */
161 @Override
162 public void draw() {
163 CellAttributes buttonColor;
164 CellAttributes menuMnemonicColor;
165 CellAttributes shadowColor = new CellAttributes();
166 shadowColor.setTo(getWindow().getBackground());
167 shadowColor.setForeColor(Color.BLACK);
168 shadowColor.setBold(false);
169
170 long now = System.currentTimeMillis();
171 boolean inDispatch = false;
172 if (now - dispatchTime < DISPATCH_TIME) {
173 inDispatch = true;
174 }
175
176 if (!isEnabled()) {
177 buttonColor = getTheme().getColor("tbutton.disabled");
178 menuMnemonicColor = getTheme().getColor("tbutton.disabled");
179 } else if (isAbsoluteActive()) {
180 buttonColor = getTheme().getColor("tbutton.active");
181 menuMnemonicColor = getTheme().getColor("tbutton.mnemonic.highlighted");
182 } else {
183 buttonColor = getTheme().getColor("tbutton.inactive");
184 menuMnemonicColor = getTheme().getColor("tbutton.mnemonic");
185 }
186
187 if (inButtonPress || inDispatch) {
188 getScreen().putCharXY(1, 0, ' ', buttonColor);
189 getScreen().putStringXY(2, 0, mnemonic.getRawLabel(), buttonColor);
190 getScreen().putCharXY(getWidth() - 1, 0, ' ', buttonColor);
191 } else {
192 getScreen().putCharXY(0, 0, ' ', buttonColor);
193 getScreen().putStringXY(1, 0, mnemonic.getRawLabel(), buttonColor);
194 getScreen().putCharXY(getWidth() - 2, 0, ' ', buttonColor);
195
196 getScreen().putCharXY(getWidth() - 1, 0,
197 GraphicsChars.CP437[0xDC], shadowColor);
198 getScreen().hLineXY(1, 1, getWidth() - 1,
199 GraphicsChars.CP437[0xDF], shadowColor);
200 }
201 if (mnemonic.getShortcutIdx() >= 0) {
202 if (inButtonPress || inDispatch) {
203 getScreen().putCharXY(2 + mnemonic.getShortcutIdx(), 0,
204 mnemonic.getShortcut(), menuMnemonicColor);
205 } else {
206 getScreen().putCharXY(1 + mnemonic.getShortcutIdx(), 0,
207 mnemonic.getShortcut(), menuMnemonicColor);
208 }
209
210 }
211 }
212
213 /**
214 * Handle mouse button presses.
215 *
216 * @param mouse mouse button event
217 */
218 @Override
219 public void onMouseDown(final TMouseEvent mouse) {
220 this.mouse = mouse;
221
222 if ((mouseOnButton()) && (mouse.isMouse1())) {
223 // Begin button press
224 inButtonPress = true;
225 }
226 }
227
228 /**
229 * Handle mouse button releases.
230 *
231 * @param mouse mouse button release event
232 */
233 @Override
234 public void onMouseUp(final TMouseEvent mouse) {
235 this.mouse = mouse;
236
237 if (inButtonPress && mouse.isMouse1()) {
238 inButtonPress = false;
239 // Dispatch the event
240 dispatch();
241 }
242
243 }
244
245 /**
246 * Handle mouse movements.
247 *
248 * @param mouse mouse motion event
249 */
250 @Override
251 public void onMouseMotion(final TMouseEvent mouse) {
252 this.mouse = mouse;
253
254 if (!mouseOnButton()) {
255 inButtonPress = false;
256 }
257 }
258
259 /**
260 * Handle keystrokes.
261 *
262 * @param keypress keystroke event
263 */
264 @Override
265 public void onKeypress(final TKeypressEvent keypress) {
266 if (keypress.equals(kbEnter)
267 || keypress.equals(kbSpace)
268 ) {
269 // Dispatch
270 dispatch();
271 return;
272 }
273
274 // Pass to parent for the things we don't care about.
275 super.onKeypress(keypress);
276 }
277
278 }