1 package be
.nikiroo
.fanfix_swing
.gui
.book
;
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
;
17 import be
.nikiroo
.fanfix
.Instance
;
18 import be
.nikiroo
.fanfix
.library
.BasicLibrary
;
19 import be
.nikiroo
.fanfix
.reader
.ui
.GuiReaderBookInfo
;
20 import be
.nikiroo
.utils
.Image
;
21 import be
.nikiroo
.utils
.ui
.ImageUtilsAwt
;
22 import be
.nikiroo
.utils
.ui
.UIUtils
;
25 * This class can create a cover icon ready to use for the graphical
30 class BookCoverImager
{
31 // TODO: export some of the configuration options?
32 static final int COVER_WIDTH
= 100;
33 static final int COVER_HEIGHT
= 150;
34 static final int SPINE_WIDTH
= 5;
35 static final int SPINE_HEIGHT
= 5;
36 static final int HOFFSET
= 20;
37 static final Color SPINE_COLOR_BOTTOM
= new Color(180, 180, 180);
38 static final Color SPINE_COLOR_RIGHT
= new Color(100, 100, 100);
39 static final Color BORDER
= Color
.black
;
41 public static final Color UNCACHED_ICON_COLOR
= Color
.green
.darker();
42 // new Color(0, 80, 220);
44 public static final int TEXT_HEIGHT
= 50;
45 public static final int TEXT_WIDTH
= COVER_WIDTH
+ 40;
49 static public Color
getBackground(boolean enabled
, boolean selected
,
51 Color color
= new Color(255, 255, 255, 0);
53 } else if (selected
&& !hovered
) {
54 color
= new Color(80, 80, 100, 40);
55 } else if (!selected
&& hovered
) {
56 color
= new Color(230, 230, 255, 100);
57 } else if (selected
&& hovered
) {
58 color
= new Color(200, 200, 255, 100);
65 * Draw a partially transparent overlay if needed depending upon the
66 * selection and mouse-hover states on top of the normal component, as well
67 * as a possible "cached" icon if the item is cached.
70 * the {@link Graphics} to paint onto
72 * draw an enabled overlay
74 * draw a selected overlay
76 * draw a hovered overlay
78 * draw a non-cached overlay if needed
80 static public void paintOverlay(Graphics g
, boolean enabled
,
81 boolean selected
, boolean hovered
, boolean cached
) {
82 Rectangle clip
= g
.getClipBounds();
83 if (clip
.getWidth() <= 0 || clip
.getHeight() <= 0) {
89 int xOffset
= (TEXT_WIDTH
- COVER_WIDTH
) - 1;
90 int yOffset
= HOFFSET
;
95 g
.drawRect(xOffset
, yOffset
, COVER_WIDTH
, COVER_HEIGHT
);
102 int[] xs
= new int[] { xOffset
, xOffset
+ SPINE_WIDTH
,
103 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
104 int[] ys
= new int[] { yOffset
+ h
, yOffset
+ h
+ SPINE_HEIGHT
,
105 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
106 g
.setColor(SPINE_COLOR_BOTTOM
);
107 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
108 xs
= new int[] { xOffset
+ w
, xOffset
+ w
+ SPINE_WIDTH
,
109 xOffset
+ w
+ SPINE_WIDTH
, xOffset
+ w
};
110 ys
= new int[] { yOffset
, yOffset
+ SPINE_HEIGHT
,
111 yOffset
+ h
+ SPINE_HEIGHT
, yOffset
+ h
};
112 g
.setColor(SPINE_COLOR_RIGHT
);
113 g
.fillPolygon(new Polygon(xs
, ys
, xs
.length
));
115 Color color
= getBackground(enabled
, selected
, hovered
);
118 g
.fillRect(clip
.x
, clip
.y
, clip
.width
, clip
.height
);
120 UIUtils
.drawEllipse3D(g
, UNCACHED_ICON_COLOR
,
121 COVER_WIDTH
+ HOFFSET
+ 30, 10, 20, 20, cached
);
125 * The width of a cover image.
129 static public int getCoverWidth() {
130 return SPINE_WIDTH
+ COVER_WIDTH
;
134 * The height of a cover image.
138 static public int getCoverHeight() {
139 return COVER_HEIGHT
+ HOFFSET
;
143 * Generate a cover icon based upon the given {@link GuiReaderBookInfo}.
146 * the library the meta comes from (can be NULL)
148 * the {@link GuiReaderBookInfo}
152 static public java
.awt
.Image
generateCoverImage(BasicLibrary lib
,
154 BufferedImage resizedImage
= null;
155 String id
= getIconId(info
);
157 InputStream in
= Instance
.getInstance().getCache().getFromCache(id
);
160 resizedImage
= ImageUtilsAwt
.fromImage(new Image(in
));
163 } catch (IOException e
) {
164 Instance
.getInstance().getTraceHandler().error(e
);
168 if (resizedImage
== null) {
172 cover
= info
.getBaseImage(lib
);
175 resizedImage
= new BufferedImage(getCoverWidth(),
176 getCoverHeight(), BufferedImage
.TYPE_4BYTE_ABGR
);
178 Graphics2D g
= resizedImage
.createGraphics();
180 if (info
!= null && info
.supportsCover()) {
181 g
.setColor(Color
.white
);
182 g
.fillRect(0, HOFFSET
, COVER_WIDTH
, COVER_HEIGHT
);
185 BufferedImage coverb
= ImageUtilsAwt
187 g
.drawImage(coverb
, 0, HOFFSET
, COVER_WIDTH
,
190 g
.setColor(Color
.black
);
191 g
.drawLine(0, HOFFSET
, COVER_WIDTH
,
192 HOFFSET
+ COVER_HEIGHT
);
193 g
.drawLine(COVER_WIDTH
, HOFFSET
, 0,
194 HOFFSET
+ COVER_HEIGHT
);
201 // Only save image with a cover, not the X thing
202 if (id
!= null && cover
!= null) {
203 ByteArrayOutputStream out
= new ByteArrayOutputStream();
204 ImageIO
.write(resizedImage
, "png", out
);
205 byte[] imageBytes
= out
.toByteArray();
206 in
= new ByteArrayInputStream(imageBytes
);
207 Instance
.getInstance().getCache().addToCache(in
, id
);
211 } catch (MalformedURLException e
) {
212 Instance
.getInstance().getTraceHandler().error(e
);
213 } catch (IOException e
) {
214 Instance
.getInstance().getTraceHandler().error(e
);
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(BookInfo info
) {
228 String id
= getIconId(info
);
229 Instance
.getInstance().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).
237 * the info or NULL for a generic (non unique!) ID
238 * @return the unique ID
240 static private String
getIconId(BookInfo info
) {
241 return (info
== null ?
"" : info
.getId() + ".") + "book-thumb_"
242 + SPINE_WIDTH
+ "x" + COVER_WIDTH
+ "+" + SPINE_HEIGHT
+ "+"
243 + COVER_HEIGHT
+ "@" + HOFFSET
;