Commit | Line | Data |
---|---|---|
bb0cff4b NR |
1 | package be.nikiroo.fanfix.reader.ui; |
2 | ||
3 | import java.awt.Color; | |
4 | import java.awt.Graphics; | |
5 | import java.awt.Graphics2D; | |
6 | import java.awt.Polygon; | |
7 | import java.awt.Rectangle; | |
8 | import java.awt.image.BufferedImage; | |
9 | import java.io.ByteArrayInputStream; | |
10 | import java.io.ByteArrayOutputStream; | |
11 | import java.io.IOException; | |
12 | import java.io.InputStream; | |
13 | import java.net.MalformedURLException; | |
14 | ||
15 | import javax.imageio.ImageIO; | |
16 | import javax.swing.ImageIcon; | |
17 | ||
18 | import be.nikiroo.fanfix.Instance; | |
19 | import be.nikiroo.fanfix.data.MetaData; | |
bb0cff4b NR |
20 | import be.nikiroo.fanfix.library.BasicLibrary; |
21 | import be.nikiroo.utils.Image; | |
22 | import be.nikiroo.utils.ui.ImageUtilsAwt; | |
23 | import be.nikiroo.utils.ui.UIUtils; | |
24 | ||
25 | /** | |
26 | * This class can create a cover icon ready to use for the graphical | |
27 | * application. | |
28 | * | |
29 | * @author niki | |
30 | */ | |
31 | class GuiReaderCoverImager { | |
32 | ||
33 | // TODO: export some of the configuration options? | |
34 | private static final int COVER_WIDTH = 100; | |
35 | private static final int COVER_HEIGHT = 150; | |
36 | private static final int SPINE_WIDTH = 5; | |
37 | private static final int SPINE_HEIGHT = 5; | |
38 | private static final int HOFFSET = 20; | |
39 | private static final Color SPINE_COLOR_BOTTOM = new Color(180, 180, 180); | |
40 | private static final Color SPINE_COLOR_RIGHT = new Color(100, 100, 100); | |
41 | private static final Color BORDER = Color.black; | |
42 | ||
43 | public static final int TEXT_HEIGHT = 50; | |
44 | public static final int TEXT_WIDTH = COVER_WIDTH + 40; | |
45 | ||
46 | // | |
47 | ||
48 | /** | |
49 | * Draw a partially transparent overlay if needed depending upon the | |
50 | * selection and mouse-hover states on top of the normal component, as well | |
51 | * as a possible "cached" icon if the item is cached. | |
52 | * | |
53 | * @param g | |
54 | * the {@link Graphics} to paint onto | |
55 | * @param enabled | |
56 | * draw an enabled overlay | |
57 | * @param selected | |
58 | * draw a selected overlay | |
59 | * @param hovered | |
60 | * draw a hovered overlay | |
61 | * @param cached | |
62 | * draw a cached overlay | |
63 | */ | |
64 | static public void paintOverlay(Graphics g, boolean enabled, | |
65 | boolean selected, boolean hovered, boolean cached) { | |
66 | Rectangle clip = g.getClipBounds(); | |
67 | if (clip.getWidth() <= 0 || clip.getHeight() <= 0) { | |
68 | return; | |
69 | } | |
70 | ||
71 | int h = COVER_HEIGHT; | |
72 | int w = COVER_WIDTH; | |
73 | int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1; | |
74 | int yOffset = HOFFSET; | |
75 | ||
76 | if (BORDER != null) { | |
77 | if (BORDER != null) { | |
78 | g.setColor(BORDER); | |
79 | g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT); | |
80 | } | |
81 | ||
82 | xOffset++; | |
83 | yOffset++; | |
84 | } | |
85 | ||
86 | int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH, | |
87 | xOffset + w + SPINE_WIDTH, xOffset + w }; | |
88 | int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT, | |
89 | yOffset + h + SPINE_HEIGHT, yOffset + h }; | |
90 | g.setColor(SPINE_COLOR_BOTTOM); | |
91 | g.fillPolygon(new Polygon(xs, ys, xs.length)); | |
92 | xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH, | |
93 | xOffset + w + SPINE_WIDTH, xOffset + w }; | |
94 | ys = new int[] { yOffset, yOffset + SPINE_HEIGHT, | |
95 | yOffset + h + SPINE_HEIGHT, yOffset + h }; | |
96 | g.setColor(SPINE_COLOR_RIGHT); | |
97 | g.fillPolygon(new Polygon(xs, ys, xs.length)); | |
98 | ||
99 | Color color = new Color(255, 255, 255, 0); | |
100 | if (!enabled) { | |
101 | } else if (selected && !hovered) { | |
102 | color = new Color(80, 80, 100, 40); | |
103 | } else if (!selected && hovered) { | |
104 | color = new Color(230, 230, 255, 100); | |
105 | } else if (selected && hovered) { | |
106 | color = new Color(200, 200, 255, 100); | |
107 | } | |
108 | ||
109 | g.setColor(color); | |
110 | g.fillRect(clip.x, clip.y, clip.width, clip.height); | |
111 | ||
112 | if (cached) { | |
113 | UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH | |
114 | + HOFFSET + 30, 10, 20, 20); | |
115 | } | |
116 | } | |
117 | ||
118 | /** | |
119 | * Generate a cover icon based upon the given {@link MetaData}. | |
120 | * | |
121 | * @param lib | |
122 | * the library the meta comes from | |
79a99506 | 123 | * @param info |
bb0cff4b NR |
124 | * the {@link MetaData} |
125 | * | |
126 | * @return the icon | |
127 | */ | |
79a99506 NR |
128 | static public ImageIcon generateCoverIcon(BasicLibrary lib, |
129 | GuiReaderBookInfo info) { | |
bb0cff4b | 130 | BufferedImage resizedImage = null; |
79a99506 | 131 | String id = getIconId(info); |
bb0cff4b NR |
132 | |
133 | InputStream in = Instance.getCache().getFromCache(id); | |
134 | if (in != null) { | |
135 | try { | |
136 | resizedImage = ImageUtilsAwt.fromImage(new Image(in)); | |
137 | in.close(); | |
138 | in = null; | |
139 | } catch (IOException e) { | |
140 | Instance.getTraceHandler().error(e); | |
141 | } | |
142 | } | |
143 | ||
144 | if (resizedImage == null) { | |
145 | try { | |
79a99506 | 146 | Image cover = info.getBaseImage(lib); |
bb0cff4b NR |
147 | resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH, |
148 | SPINE_HEIGHT + COVER_HEIGHT + HOFFSET, | |
149 | BufferedImage.TYPE_4BYTE_ABGR); | |
79a99506 | 150 | |
bb0cff4b | 151 | Graphics2D g = resizedImage.createGraphics(); |
79a99506 NR |
152 | try { |
153 | g.setColor(Color.white); | |
154 | g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT); | |
155 | ||
156 | if (cover != null) { | |
157 | BufferedImage coverb = ImageUtilsAwt.fromImage(cover); | |
158 | g.drawImage(coverb, 0, HOFFSET, COVER_WIDTH, | |
159 | COVER_HEIGHT, null); | |
160 | } else { | |
161 | g.setColor(Color.black); | |
162 | g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET | |
163 | + COVER_HEIGHT); | |
164 | g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET | |
165 | + COVER_HEIGHT); | |
166 | } | |
167 | } finally { | |
168 | g.dispose(); | |
bb0cff4b | 169 | } |
bb0cff4b NR |
170 | |
171 | if (id != null) { | |
172 | ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
173 | ImageIO.write(resizedImage, "png", out); | |
174 | byte[] imageBytes = out.toByteArray(); | |
175 | in = new ByteArrayInputStream(imageBytes); | |
176 | Instance.getCache().addToCache(in, id); | |
177 | in.close(); | |
178 | in = null; | |
179 | } | |
180 | } catch (MalformedURLException e) { | |
181 | Instance.getTraceHandler().error(e); | |
182 | } catch (IOException e) { | |
183 | Instance.getTraceHandler().error(e); | |
184 | } | |
185 | } | |
186 | ||
187 | return new ImageIcon(resizedImage); | |
188 | } | |
189 | ||
190 | /** | |
191 | * Manually clear the icon set for this item. | |
192 | * | |
79a99506 NR |
193 | * @param info |
194 | * the info about the story or source/type or author | |
bb0cff4b | 195 | */ |
79a99506 NR |
196 | static public void clearIcon(GuiReaderBookInfo info) { |
197 | String id = getIconId(info); | |
bb0cff4b NR |
198 | Instance.getCache().removeFromCache(id); |
199 | } | |
200 | ||
201 | /** | |
79a99506 NR |
202 | * Get a unique ID from this {@link GuiReaderBookInfo} (note that it can be |
203 | * a story, a fake item for a source/type or a fake item for an author). | |
bb0cff4b | 204 | * |
79a99506 NR |
205 | * @param info |
206 | * the info | |
bb0cff4b NR |
207 | * @return the unique ID |
208 | */ | |
79a99506 NR |
209 | static private String getIconId(GuiReaderBookInfo info) { |
210 | return info.getId() + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+" | |
bb0cff4b | 211 | + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET; |
bb0cff4b NR |
212 | } |
213 | } |