| 1 | package be.nikiroo.jvcard.tui; |
| 2 | |
| 3 | import java.awt.Dimension; |
| 4 | import java.awt.Image; |
| 5 | |
| 6 | import be.nikiroo.jvcard.launcher.Main; |
| 7 | import be.nikiroo.utils.ImageText; |
| 8 | import be.nikiroo.utils.ImageText.Mode; |
| 9 | |
| 10 | import com.googlecode.lanterna.TerminalSize; |
| 11 | import com.googlecode.lanterna.gui2.BorderLayout; |
| 12 | import com.googlecode.lanterna.gui2.Panel; |
| 13 | import com.googlecode.lanterna.gui2.TextBox; |
| 14 | |
| 15 | /** |
| 16 | * A {@link Panel} containing an {@link ImageText} rendering. |
| 17 | * |
| 18 | * @author niki |
| 19 | * |
| 20 | */ |
| 21 | public class ImageTextControl extends Panel { |
| 22 | private ImageText image; |
| 23 | private TextBox txt; |
| 24 | private int mode; |
| 25 | |
| 26 | /** |
| 27 | * Create a new {@link ImageTextControl} for the given {@link Image} and |
| 28 | * {@link TerminalSize}. |
| 29 | * |
| 30 | * @param image |
| 31 | * the {@link Image} to render |
| 32 | * @param size |
| 33 | * the target size of this control |
| 34 | */ |
| 35 | public ImageTextControl(Image image, TerminalSize size) { |
| 36 | Mode mode = Mode.DOUBLE_DITHERING; |
| 37 | if (!Main.isUnicode()) { |
| 38 | mode = Mode.ASCII; |
| 39 | } |
| 40 | |
| 41 | Mode[] modes = Mode.values(); |
| 42 | for (int i = 0; i < modes.length; i++) { |
| 43 | if (mode == modes[i]) |
| 44 | this.mode = i; |
| 45 | } |
| 46 | |
| 47 | this.setLayoutManager(new BorderLayout()); |
| 48 | setSize(size); |
| 49 | setImage(new ImageText(image, new Dimension(size.getColumns(), |
| 50 | size.getRows()), mode, false)); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Cycle through the available rendering modes if possible. |
| 55 | * |
| 56 | * @return TRUE if it was possible to switch modes |
| 57 | */ |
| 58 | public boolean switchMode() { |
| 59 | if (image == null || !Main.isUnicode()) |
| 60 | return false; |
| 61 | |
| 62 | Mode[] modes = Mode.values(); |
| 63 | mode++; |
| 64 | if (mode >= modes.length) |
| 65 | mode = 0; |
| 66 | |
| 67 | image.setMode(modes[mode]); |
| 68 | setImage(image); |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Invert the colours. |
| 75 | */ |
| 76 | public void invertColor() { |
| 77 | if (image != null) { |
| 78 | image.setColorInvert(!image.isColorInvert()); |
| 79 | setImage(image); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public synchronized Panel setSize(TerminalSize size) { |
| 85 | if (image != null) |
| 86 | image.setSize(new Dimension(size.getColumns(), size.getRows())); |
| 87 | |
| 88 | super.setSize(size); |
| 89 | |
| 90 | setImage(image); |
| 91 | |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Set/reset the {@link ImageText} to render. |
| 97 | * |
| 98 | * @param image |
| 99 | * the new {@link ImageText} |
| 100 | */ |
| 101 | private void setImage(ImageText image) { |
| 102 | this.image = image; |
| 103 | removeAllComponents(); |
| 104 | txt = null; |
| 105 | if (image != null) { |
| 106 | txt = new TextBox(image.getText()); |
| 107 | this.addComponent(txt, BorderLayout.Location.CENTER); |
| 108 | } |
| 109 | } |
| 110 | } |