1 package be
.nikiroo
.fanfix
.reader
.ui
;
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
;
15 import javax
.imageio
.ImageIO
;
16 import javax
.swing
.ImageIcon
;
18 import be
.nikiroo
.fanfix
.Instance
;
19 import be
.nikiroo
.fanfix
.data
.MetaData
;
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
;
26 * This class can create a cover icon ready to use for the graphical
31 class GuiReaderCoverImager
{
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
;
43 public static final int TEXT_HEIGHT
= 50;
44 public static final int TEXT_WIDTH
= COVER_WIDTH
+ 40;
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.
54 * the {@link Graphics} to paint onto
56 * draw an enabled overlay
58 * draw a selected overlay
60 * draw a hovered overlay
62 * draw a cached overlay
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) {
73 int xOffset
= (TEXT_WIDTH
- COVER_WIDTH
) - 1;
74 int yOffset
= HOFFSET
;
79 g
.drawRect(xOffset
, yOffset
, COVER_WIDTH
, COVER_HEIGHT
);
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
));
99 Color color
= new Color(255, 255, 255, 0);
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);
110 g
.fillRect(clip
.x
, clip
.y
, clip
.width
, clip
.height
);
113 UIUtils
.drawEllipse3D(g
, Color
.green
.darker(), COVER_WIDTH
114 + HOFFSET
+ 30, 10, 20, 20);
119 * Generate a cover icon based upon the given {@link MetaData}.
122 * the library the meta comes from
124 * the {@link MetaData}
128 static public ImageIcon
generateCoverIcon(BasicLibrary lib
, MetaData meta
) {
129 return generateCoverIcon(lib
, GuiReaderBookInfo
.fromMeta(meta
));
133 * The width of a cover image.
137 static public int getCoverWidth() {
138 return SPINE_WIDTH
+ COVER_WIDTH
;
142 * The height of a cover image.
146 static public int getCoverHeight() {
147 return COVER_HEIGHT
+ HOFFSET
;
151 * Generate a cover icon based upon the given {@link GuiReaderBookInfo}.
154 * the library the meta comes from
156 * the {@link GuiReaderBookInfo}
160 static public ImageIcon
generateCoverIcon(BasicLibrary lib
,
161 GuiReaderBookInfo info
) {
162 BufferedImage resizedImage
= null;
163 String id
= getIconId(info
);
165 InputStream in
= Instance
.getCache().getFromCache(id
);
168 resizedImage
= ImageUtilsAwt
.fromImage(new Image(in
));
171 } catch (IOException e
) {
172 Instance
.getTraceHandler().error(e
);
176 if (resizedImage
== null) {
178 Image cover
= info
.getBaseImage(lib
);
179 resizedImage
= new BufferedImage(getCoverWidth(),
180 getCoverHeight(), BufferedImage
.TYPE_4BYTE_ABGR
);
182 Graphics2D g
= resizedImage
.createGraphics();
184 g
.setColor(Color
.white
);
185 g
.fillRect(0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
);
188 BufferedImage coverb
= ImageUtilsAwt
.fromImage(cover
);
189 g
.drawImage(coverb
, 0, HOFFSET
, COVER_WIDTH
,
192 g
.setColor(Color
.black
);
193 g
.drawLine(0, HOFFSET
, COVER_WIDTH
, HOFFSET
195 g
.drawLine(COVER_WIDTH
, HOFFSET
, 0, HOFFSET
203 ByteArrayOutputStream out
= new ByteArrayOutputStream();
204 ImageIO
.write(resizedImage
, "png", out
);
205 byte[] imageBytes
= out
.toByteArray();
206 in
= new ByteArrayInputStream(imageBytes
);
207 Instance
.getCache().addToCache(in
, id
);
211 } catch (MalformedURLException e
) {
212 Instance
.getTraceHandler().error(e
);
213 } catch (IOException e
) {
214 Instance
.getTraceHandler().error(e
);
218 return new ImageIcon(resizedImage
);
222 * Manually clear the icon set for this item.
225 * the info about the story or source/type or author
227 static public void clearIcon(GuiReaderBookInfo info
) {
228 String id
= getIconId(info
);
229 Instance
.getCache().removeFromCache(id
);
233 * Get a unique ID from this {@link GuiReaderBookInfo} (note that it can be
234 * a story, a fake item for a source/type or a fake item for an author).
238 * @return the unique ID
240 static private String
getIconId(GuiReaderBookInfo info
) {
241 return info
.getId() + ".thumb_" + SPINE_WIDTH
+ "x" + COVER_WIDTH
+ "+"
242 + SPINE_HEIGHT
+ "+" + COVER_HEIGHT
+ "@" + HOFFSET
;