d7951b6b3b5287159bc2d4a19aeab84e7fad6f72
[fanfix.git] / src / jexer / io / AWTScreen.java
1 /**
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer.io;
32
33 import jexer.bits.Cell;
34 import jexer.bits.CellAttributes;
35 import jexer.session.AWTSessionInfo;
36
37 import java.awt.Color;
38 import java.awt.Cursor;
39 import java.awt.Font;
40 import java.awt.FontMetrics;
41 import java.awt.Frame;
42 import java.awt.Graphics;
43 import java.awt.Insets;
44 import java.awt.Point;
45 import java.awt.Rectangle;
46 import java.awt.Toolkit;
47 import java.awt.geom.Rectangle2D;
48 import java.awt.image.BufferedImage;
49 import java.io.InputStream;
50
51 /**
52 * This Screen implementation draws to a Java AWT Frame.
53 */
54 public final class AWTScreen extends Screen {
55
56 private static Color MYBLACK;
57 private static Color MYRED;
58 private static Color MYGREEN;
59 private static Color MYYELLOW;
60 private static Color MYBLUE;
61 private static Color MYMAGENTA;
62 private static Color MYCYAN;
63 private static Color MYWHITE;
64
65 private static Color MYBOLD_BLACK;
66 private static Color MYBOLD_RED;
67 private static Color MYBOLD_GREEN;
68 private static Color MYBOLD_YELLOW;
69 private static Color MYBOLD_BLUE;
70 private static Color MYBOLD_MAGENTA;
71 private static Color MYBOLD_CYAN;
72 private static Color MYBOLD_WHITE;
73
74 private static boolean dosColors = false;
75
76 /**
77 * Setup AWT colors to match DOS color palette.
78 */
79 private static void setDOSColors() {
80 if (dosColors) {
81 return;
82 }
83 MYBLACK = new Color(0x00, 0x00, 0x00);
84 MYRED = new Color(0xa8, 0x00, 0x00);
85 MYGREEN = new Color(0x00, 0xa8, 0x00);
86 MYYELLOW = new Color(0xa8, 0x54, 0x00);
87 MYBLUE = new Color(0x00, 0x00, 0xa8);
88 MYMAGENTA = new Color(0xa8, 0x00, 0xa8);
89 MYCYAN = new Color(0x00, 0xa8, 0xa8);
90 MYWHITE = new Color(0xa8, 0xa8, 0xa8);
91 MYBOLD_BLACK = new Color(0x54, 0x54, 0x54);
92 MYBOLD_RED = new Color(0xfc, 0x54, 0x54);
93 MYBOLD_GREEN = new Color(0x54, 0xfc, 0x54);
94 MYBOLD_YELLOW = new Color(0xfc, 0xfc, 0x54);
95 MYBOLD_BLUE = new Color(0x54, 0x54, 0xfc);
96 MYBOLD_MAGENTA = new Color(0xfc, 0x54, 0xfc);
97 MYBOLD_CYAN = new Color(0x54, 0xfc, 0xfc);
98 MYBOLD_WHITE = new Color(0xfc, 0xfc, 0xfc);
99
100 dosColors = true;
101 }
102
103 /**
104 * AWTFrame is our top-level hook into the AWT system.
105 */
106 class AWTFrame extends Frame {
107
108 /**
109 * The terminus font resource filename.
110 */
111 private static final String FONTFILE = "terminus-ttf-4.39/TerminusTTF-Bold-4.39.ttf";
112
113 /**
114 * The TUI Screen data.
115 */
116 AWTScreen screen;
117
118 /**
119 * Width of a character cell.
120 */
121 private int textWidth = 1;
122
123 /**
124 * Height of a character cell.
125 */
126 private int textHeight = 1;
127
128 /**
129 * Descent of a character cell.
130 */
131 private int maxDescent = 0;
132
133 /**
134 * Top pixel value.
135 */
136 private int top = 30;
137
138 /**
139 * Left pixel value.
140 */
141 private int left = 30;
142
143 /**
144 * Convert a CellAttributes foreground color to an AWT Color.
145 *
146 * @param attr the text attributes
147 * @return the AWT Color
148 */
149 private Color attrToForegroundColor(final CellAttributes attr) {
150 /*
151 * TODO:
152 * reverse
153 * blink
154 * underline
155 */
156 if (attr.getBold()) {
157 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
158 return MYBOLD_BLACK;
159 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
160 return MYBOLD_RED;
161 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
162 return MYBOLD_BLUE;
163 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
164 return MYBOLD_GREEN;
165 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
166 return MYBOLD_YELLOW;
167 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
168 return MYBOLD_CYAN;
169 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
170 return MYBOLD_MAGENTA;
171 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
172 return MYBOLD_WHITE;
173 }
174 } else {
175 if (attr.getForeColor().equals(jexer.bits.Color.BLACK)) {
176 return MYBLACK;
177 } else if (attr.getForeColor().equals(jexer.bits.Color.RED)) {
178 return MYRED;
179 } else if (attr.getForeColor().equals(jexer.bits.Color.BLUE)) {
180 return MYBLUE;
181 } else if (attr.getForeColor().equals(jexer.bits.Color.GREEN)) {
182 return MYGREEN;
183 } else if (attr.getForeColor().equals(jexer.bits.Color.YELLOW)) {
184 return MYYELLOW;
185 } else if (attr.getForeColor().equals(jexer.bits.Color.CYAN)) {
186 return MYCYAN;
187 } else if (attr.getForeColor().equals(jexer.bits.Color.MAGENTA)) {
188 return MYMAGENTA;
189 } else if (attr.getForeColor().equals(jexer.bits.Color.WHITE)) {
190 return MYWHITE;
191 }
192 }
193 throw new IllegalArgumentException("Invalid color: " + attr.getForeColor().getValue());
194 }
195
196 /**
197 * Convert a CellAttributes background color to an AWT Color.
198 *
199 * @param attr the text attributes
200 * @return the AWT Color
201 */
202 private Color attrToBackgroundColor(final CellAttributes attr) {
203 /*
204 * TODO:
205 * reverse
206 * blink
207 * underline
208 */
209 if (attr.getBackColor().equals(jexer.bits.Color.BLACK)) {
210 return MYBLACK;
211 } else if (attr.getBackColor().equals(jexer.bits.Color.RED)) {
212 return MYRED;
213 } else if (attr.getBackColor().equals(jexer.bits.Color.BLUE)) {
214 return MYBLUE;
215 } else if (attr.getBackColor().equals(jexer.bits.Color.GREEN)) {
216 return MYGREEN;
217 } else if (attr.getBackColor().equals(jexer.bits.Color.YELLOW)) {
218 return MYYELLOW;
219 } else if (attr.getBackColor().equals(jexer.bits.Color.CYAN)) {
220 return MYCYAN;
221 } else if (attr.getBackColor().equals(jexer.bits.Color.MAGENTA)) {
222 return MYMAGENTA;
223 } else if (attr.getBackColor().equals(jexer.bits.Color.WHITE)) {
224 return MYWHITE;
225 }
226 throw new IllegalArgumentException("Invalid color: " + attr.getBackColor().getValue());
227 }
228
229 /**
230 * Public constructor.
231 *
232 * @param screen the Screen that Backend talks to
233 */
234 public AWTFrame(final AWTScreen screen) {
235 this.screen = screen;
236 setDOSColors();
237
238 setTitle("Jexer Application");
239 setBackground(java.awt.Color.black);
240 // setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
241 // setFont(new Font("Liberation Mono", Font.BOLD, 16));
242 // setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
243
244 try {
245 // Always try to use Terminus, the one decent font.
246 ClassLoader loader = Thread.currentThread().getContextClassLoader();
247 InputStream in = loader.getResourceAsStream(FONTFILE);
248 Font terminusRoot = Font.createFont(Font.TRUETYPE_FONT, in);
249 Font terminus = terminusRoot.deriveFont(Font.PLAIN, 22);
250 setFont(terminus);
251 } catch (Exception e) {
252 e.printStackTrace();
253 // setFont(new Font("Liberation Mono", Font.PLAIN, 24));
254 setFont(new Font(Font.MONOSPACED, Font.PLAIN, 24));
255 }
256 setVisible(true);
257 resizeToScreen();
258
259 // Kill the X11 cursor
260 // Transparent 16 x 16 pixel cursor image.
261 BufferedImage cursorImg = new BufferedImage(16, 16,
262 BufferedImage.TYPE_INT_ARGB);
263
264 // Create a new blank cursor.
265 Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
266 cursorImg, new Point(0, 0), "blank cursor");
267 setCursor(blankCursor);
268 }
269
270 /**
271 * Resize to font dimensions.
272 */
273 public void resizeToScreen() {
274 Graphics gr = getGraphics();
275 FontMetrics fm = gr.getFontMetrics();
276 maxDescent = fm.getMaxDescent();
277 Rectangle2D bounds = fm.getMaxCharBounds(gr);
278 int leading = fm.getLeading();
279 textWidth = (int)Math.round(bounds.getWidth());
280 textHeight = (int)Math.round(bounds.getHeight()) - maxDescent;
281 // This also produces the same number, but works better for ugly
282 // monospace.
283 textHeight = fm.getMaxAscent() + maxDescent - leading;
284
285 // Figure out the thickness of borders and use that to set the
286 // final size.
287 Insets insets = getInsets();
288 left = insets.left;
289 top = insets.top;
290
291 setSize(textWidth * screen.width + insets.left + insets.right,
292 textHeight * screen.height + insets.top + insets.bottom);
293
294 /*
295 System.err.printf("W: %d H: %d MD: %d L: %d\n", textWidth,
296 textHeight, maxDescent, leading);
297 */
298 }
299
300 /**
301 * Update redraws the whole screen.
302 *
303 * @param gr the AWT Graphics context
304 */
305 @Override
306 public void update(final Graphics gr) {
307 // The default update clears the area. Don't do that, instead
308 // just paint it directly.
309 paint(gr);
310 }
311
312 /**
313 * Paint redraws the whole screen.
314 *
315 * @param gr the AWT Graphics context
316 */
317 @Override
318 public void paint(final Graphics gr) {
319 Rectangle bounds = gr.getClipBounds();
320
321 for (int y = 0; y < screen.height; y++) {
322 for (int x = 0; x < screen.width; x++) {
323 int xPixel = x * textWidth + left;
324 int yPixel = y * textHeight + top;
325
326 Cell lCell = screen.logical[x][y];
327 Cell pCell = screen.physical[x][y];
328
329 boolean inBounds = true;
330 if (bounds != null) {
331 if (bounds.contains(xPixel, yPixel)
332 || bounds.contains(xPixel + textWidth, yPixel)
333 || bounds.contains(xPixel, yPixel + textHeight)
334 || bounds.contains(xPixel + textWidth,
335 yPixel + textHeight)
336 ) {
337 // This area is damaged and will definitely be
338 // redrawn.
339 inBounds = true;
340 }
341 }
342
343 if (!lCell.equals(pCell) || inBounds) {
344 // Draw the background rectangle, then the foreground
345 // character.
346 gr.setColor(attrToBackgroundColor(lCell));
347 gr.fillRect(xPixel, yPixel, textWidth, textHeight);
348 gr.setColor(attrToForegroundColor(lCell));
349 char [] chars = new char[1];
350 chars[0] = lCell.getChar();
351 gr.drawChars(chars, 0, 1, xPixel,
352 yPixel + textHeight - maxDescent);
353
354 // Physical is always updated
355 physical[x][y].setTo(lCell);
356 }
357 }
358 }
359
360 // Draw the cursor if it is visible
361 if ((cursorVisible)
362 && (cursorY <= screen.height - 1)
363 && (cursorX <= screen.width - 1)
364 ) {
365 int xPixel = cursorX * textWidth + left;
366 int yPixel = cursorY * textHeight + top;
367 Cell lCell = screen.logical[cursorX][cursorY];
368 gr.setColor(attrToForegroundColor(lCell));
369 gr.fillRect(xPixel, yPixel + textHeight - 2, textWidth, 2);
370 }
371 }
372 }
373
374 /**
375 * The raw AWT Frame. Note package private access.
376 */
377 AWTFrame frame;
378
379 /**
380 * Public constructor.
381 */
382 public AWTScreen() {
383 frame = new AWTFrame(this);
384 }
385
386 /**
387 * Create the AWTSessionInfo. Note package private access.
388 *
389 * @return the sessionInfo
390 */
391 AWTSessionInfo getSessionInfo() {
392 AWTSessionInfo sessionInfo = new AWTSessionInfo(frame, frame.textWidth,
393 frame.textHeight);
394 return sessionInfo;
395 }
396
397 /**
398 * Push the logical screen to the physical device.
399 */
400 @Override
401 public void flushPhysical() {
402 // Request a repaint, let the frame's repaint/update methods do the
403 // right thing.
404 // Find the minimum-size damaged region.
405 int xMin = frame.getWidth();
406 int xMax = 0;
407 int yMin = frame.getHeight();
408 int yMax = 0;
409 for (int y = 0; y < height; y++) {
410 for (int x = 0; x < width; x++) {
411 Cell lCell = logical[x][y];
412 Cell pCell = physical[x][y];
413
414 int xPixel = x * frame.textWidth + frame.left;
415 int yPixel = y * frame.textHeight + frame.top;
416
417 if (!lCell.equals(pCell)
418 || ((x == cursorX) && (y == cursorY))
419 ) {
420 if (xPixel < xMin) {
421 xMin = xPixel;
422 }
423 if (xPixel + frame.textWidth > xMax) {
424 xMax = xPixel + frame.textWidth;
425 }
426 if (yPixel < yMin) {
427 yMin = yPixel;
428 }
429 if (yPixel + frame.textHeight > yMax) {
430 yMax = yPixel + frame.textHeight;
431 }
432 }
433 }
434 }
435
436 // Ask for a repaint sometime in the next 10 millis.
437 frame.repaint(10, xMin, yMin, xMax - xMin, yMax - yMin);
438 }
439
440 /**
441 * Put the cursor at (x,y).
442 *
443 * @param visible if true, the cursor should be visible
444 * @param x column coordinate to put the cursor on
445 * @param y row coordinate to put the cursor on
446 */
447 @Override
448 public void putCursor(final boolean visible, final int x, final int y) {
449 if ((cursorVisible)
450 && (cursorY <= height - 1)
451 && (cursorX <= width - 1)
452 ) {
453 // Make the current cursor position dirty
454 if (physical[cursorX][cursorY].getChar() == ' ') {
455 physical[cursorX][cursorY].setChar('X');
456 } else {
457 physical[cursorX][cursorY].setChar(' ');
458 }
459 }
460
461 super.putCursor(visible, x, y);
462 }
463
464 }