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; | |
20 | import be.nikiroo.fanfix.data.Story; | |
21 | import be.nikiroo.fanfix.library.BasicLibrary; | |
22 | import be.nikiroo.utils.Image; | |
23 | import be.nikiroo.utils.ui.ImageUtilsAwt; | |
24 | import be.nikiroo.utils.ui.UIUtils; | |
25 | ||
26 | /** | |
27 | * This class can create a cover icon ready to use for the graphical | |
28 | * application. | |
29 | * | |
30 | * @author niki | |
31 | */ | |
32 | class GuiReaderCoverImager { | |
33 | ||
34 | // TODO: export some of the configuration options? | |
35 | private static final int COVER_WIDTH = 100; | |
36 | private static final int COVER_HEIGHT = 150; | |
37 | private static final int SPINE_WIDTH = 5; | |
38 | private static final int SPINE_HEIGHT = 5; | |
39 | private static final int HOFFSET = 20; | |
40 | private static final Color SPINE_COLOR_BOTTOM = new Color(180, 180, 180); | |
41 | private static final Color SPINE_COLOR_RIGHT = new Color(100, 100, 100); | |
42 | private static final Color BORDER = Color.black; | |
43 | ||
44 | public static final int TEXT_HEIGHT = 50; | |
45 | public static final int TEXT_WIDTH = COVER_WIDTH + 40; | |
46 | ||
47 | // | |
48 | ||
49 | /** | |
50 | * Draw a partially transparent overlay if needed depending upon the | |
51 | * selection and mouse-hover states on top of the normal component, as well | |
52 | * as a possible "cached" icon if the item is cached. | |
53 | * | |
54 | * @param g | |
55 | * the {@link Graphics} to paint onto | |
56 | * @param enabled | |
57 | * draw an enabled overlay | |
58 | * @param selected | |
59 | * draw a selected overlay | |
60 | * @param hovered | |
61 | * draw a hovered overlay | |
62 | * @param cached | |
63 | * draw a cached overlay | |
64 | */ | |
65 | static public void paintOverlay(Graphics g, boolean enabled, | |
66 | boolean selected, boolean hovered, boolean cached) { | |
67 | Rectangle clip = g.getClipBounds(); | |
68 | if (clip.getWidth() <= 0 || clip.getHeight() <= 0) { | |
69 | return; | |
70 | } | |
71 | ||
72 | int h = COVER_HEIGHT; | |
73 | int w = COVER_WIDTH; | |
74 | int xOffset = (TEXT_WIDTH - COVER_WIDTH) - 1; | |
75 | int yOffset = HOFFSET; | |
76 | ||
77 | if (BORDER != null) { | |
78 | if (BORDER != null) { | |
79 | g.setColor(BORDER); | |
80 | g.drawRect(xOffset, yOffset, COVER_WIDTH, COVER_HEIGHT); | |
81 | } | |
82 | ||
83 | xOffset++; | |
84 | yOffset++; | |
85 | } | |
86 | ||
87 | int[] xs = new int[] { xOffset, xOffset + SPINE_WIDTH, | |
88 | xOffset + w + SPINE_WIDTH, xOffset + w }; | |
89 | int[] ys = new int[] { yOffset + h, yOffset + h + SPINE_HEIGHT, | |
90 | yOffset + h + SPINE_HEIGHT, yOffset + h }; | |
91 | g.setColor(SPINE_COLOR_BOTTOM); | |
92 | g.fillPolygon(new Polygon(xs, ys, xs.length)); | |
93 | xs = new int[] { xOffset + w, xOffset + w + SPINE_WIDTH, | |
94 | xOffset + w + SPINE_WIDTH, xOffset + w }; | |
95 | ys = new int[] { yOffset, yOffset + SPINE_HEIGHT, | |
96 | yOffset + h + SPINE_HEIGHT, yOffset + h }; | |
97 | g.setColor(SPINE_COLOR_RIGHT); | |
98 | g.fillPolygon(new Polygon(xs, ys, xs.length)); | |
99 | ||
100 | Color color = new Color(255, 255, 255, 0); | |
101 | if (!enabled) { | |
102 | } else if (selected && !hovered) { | |
103 | color = new Color(80, 80, 100, 40); | |
104 | } else if (!selected && hovered) { | |
105 | color = new Color(230, 230, 255, 100); | |
106 | } else if (selected && hovered) { | |
107 | color = new Color(200, 200, 255, 100); | |
108 | } | |
109 | ||
110 | g.setColor(color); | |
111 | g.fillRect(clip.x, clip.y, clip.width, clip.height); | |
112 | ||
113 | if (cached) { | |
114 | UIUtils.drawEllipse3D(g, Color.green.darker(), COVER_WIDTH | |
115 | + HOFFSET + 30, 10, 20, 20); | |
116 | } | |
117 | } | |
118 | ||
119 | /** | |
120 | * Generate a cover icon based upon the given {@link MetaData}. | |
121 | * | |
122 | * @param lib | |
123 | * the library the meta comes from | |
124 | * @param meta | |
125 | * the {@link MetaData} | |
126 | * | |
127 | * @return the icon | |
128 | */ | |
129 | static public ImageIcon generateCoverIcon(BasicLibrary lib, MetaData meta) { | |
130 | BufferedImage resizedImage = null; | |
131 | String id = getIconId(meta); | |
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 { | |
146 | Image cover = null; | |
147 | if (meta.getLuid() != null) { | |
148 | cover = lib.getCover(meta.getLuid()); | |
149 | } | |
150 | if (cover == null) { | |
151 | cover = lib.getSourceCover(meta.getSource()); | |
152 | } | |
153 | ||
154 | resizedImage = new BufferedImage(SPINE_WIDTH + COVER_WIDTH, | |
155 | SPINE_HEIGHT + COVER_HEIGHT + HOFFSET, | |
156 | BufferedImage.TYPE_4BYTE_ABGR); | |
157 | Graphics2D g = resizedImage.createGraphics(); | |
158 | g.setColor(Color.white); | |
159 | g.fillRect(0, HOFFSET, COVER_WIDTH, COVER_HEIGHT); | |
160 | if (cover != null) { | |
161 | BufferedImage coverb = ImageUtilsAwt.fromImage(cover); | |
162 | g.drawImage(coverb, 0, HOFFSET, COVER_WIDTH, COVER_HEIGHT, | |
163 | null); | |
164 | } else { | |
165 | g.setColor(Color.black); | |
166 | g.drawLine(0, HOFFSET, COVER_WIDTH, HOFFSET + COVER_HEIGHT); | |
167 | g.drawLine(COVER_WIDTH, HOFFSET, 0, HOFFSET + COVER_HEIGHT); | |
168 | } | |
169 | g.dispose(); | |
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 | * | |
193 | * @param meta | |
194 | * the meta of the story or source (if luid is null) | |
195 | */ | |
196 | static public void clearIcon(MetaData meta) { | |
197 | String id = getIconId(meta); | |
198 | Instance.getCache().removeFromCache(id); | |
199 | } | |
200 | ||
201 | /** | |
202 | * Get a unique ID from this meta (note that if the luid is null, it is | |
203 | * considered a source and not a {@link Story}). | |
204 | * | |
205 | * @param meta | |
206 | * the meta | |
207 | * @return the unique ID | |
208 | */ | |
209 | static private String getIconId(MetaData meta) { | |
210 | String id = null; | |
211 | ||
212 | String key = meta.getUuid(); | |
213 | if (meta.getLuid() == null) { | |
214 | // a fake meta (== a source) | |
215 | key = "source_" + meta.getSource(); | |
216 | } | |
217 | ||
218 | id = key + ".thumb_" + SPINE_WIDTH + "x" + COVER_WIDTH + "+" | |
219 | + SPINE_HEIGHT + "+" + COVER_HEIGHT + "@" + HOFFSET; | |
220 | ||
221 | return id; | |
222 | } | |
223 | } |