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 * Generate a cover icon based upon the given {@link GuiReaderBookInfo}.
136 * the library the meta comes from
138 * the {@link GuiReaderBookInfo}
142 static public ImageIcon
generateCoverIcon(BasicLibrary lib
,
143 GuiReaderBookInfo info
) {
144 BufferedImage resizedImage
= null;
145 String id
= getIconId(info
);
147 InputStream in
= Instance
.getCache().getFromCache(id
);
150 resizedImage
= ImageUtilsAwt
.fromImage(new Image(in
));
153 } catch (IOException e
) {
154 Instance
.getTraceHandler().error(e
);
158 if (resizedImage
== null) {
160 Image cover
= info
.getBaseImage(lib
);
161 resizedImage
= new BufferedImage(SPINE_WIDTH
+ COVER_WIDTH
,
162 SPINE_HEIGHT
+ COVER_HEIGHT
+ HOFFSET
,
163 BufferedImage
.TYPE_4BYTE_ABGR
);
165 Graphics2D g
= resizedImage
.createGraphics();
167 g
.setColor(Color
.white
);
168 g
.fillRect(0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
);
171 BufferedImage coverb
= ImageUtilsAwt
.fromImage(cover
);
172 g
.drawImage(coverb
, 0, HOFFSET
, COVER_WIDTH
,
175 g
.setColor(Color
.black
);
176 g
.drawLine(0, HOFFSET
, COVER_WIDTH
, HOFFSET
178 g
.drawLine(COVER_WIDTH
, HOFFSET
, 0, HOFFSET
186 ByteArrayOutputStream out
= new ByteArrayOutputStream();
187 ImageIO
.write(resizedImage
, "png", out
);
188 byte[] imageBytes
= out
.toByteArray();
189 in
= new ByteArrayInputStream(imageBytes
);
190 Instance
.getCache().addToCache(in
, id
);
194 } catch (MalformedURLException e
) {
195 Instance
.getTraceHandler().error(e
);
196 } catch (IOException e
) {
197 Instance
.getTraceHandler().error(e
);
201 return new ImageIcon(resizedImage
);
205 * Manually clear the icon set for this item.
208 * the info about the story or source/type or author
210 static public void clearIcon(GuiReaderBookInfo info
) {
211 String id
= getIconId(info
);
212 Instance
.getCache().removeFromCache(id
);
216 * Get a unique ID from this {@link GuiReaderBookInfo} (note that it can be
217 * a story, a fake item for a source/type or a fake item for an author).
221 * @return the unique ID
223 static private String
getIconId(GuiReaderBookInfo info
) {
224 return info
.getId() + ".thumb_" + SPINE_WIDTH
+ "x" + COVER_WIDTH
+ "+"
225 + SPINE_HEIGHT
+ "+" + COVER_HEIGHT
+ "@" + HOFFSET
;