MCPcopy Create free account
hub / github.com/Card-Forge/forge / CardAvatarImage

Class CardAvatarImage

forge-gui-mobile/src/forge/card/CardAvatarImage.java:8–60  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6import forge.item.PaperCard;
7
8public class CardAvatarImage implements FImage {
9 private final String imageKey;
10 private FImage image;
11
12 public CardAvatarImage(PaperCard card0) {
13 this(card0.getImageKey(false));
14 }
15 public CardAvatarImage(String imageKey0) {
16 imageKey = imageKey0;
17 }
18
19 @Override
20 public float getWidth() {
21 return getHeight(); //image will be drawn at its height
22 }
23
24 @Override
25 public float getHeight() {
26 if (image != null) {
27 return image.getHeight();
28 }
29 return ImageCache.getInstance().getDefaultImage().getHeight() * CardRenderer.CARD_ART_HEIGHT_PERCENTAGE;
30 }
31
32 @Override
33 public void draw(Graphics g, float x, float y, float w, float h) {
34 //force to get the avatar since the the cardartcache & loadingcache is always cleared on screen change or the battle bar will display black
35 image = CardRenderer.getCardArt(imageKey, false, false, false, false, false, false, false, false, true, false);
36 if (image == null) {
37 return; //can't draw anything if can't be loaded yet
38 }
39
40 //draw scaled image into clipped region so it fills box while maintain aspect ratio
41 g.startClip(x, y, w, h);
42
43 float aspectRatio = w / h;
44 float imageAspectRatio = image.getWidth() / image.getHeight();
45 if (imageAspectRatio > aspectRatio) {
46 float w0 = w * imageAspectRatio / aspectRatio;
47 x -= (w0 - w) / 2;
48 w = w0;
49 }
50 else {
51 float h0 = h * aspectRatio / imageAspectRatio;
52 y -= (h0 - h) / 2;
53 h = h0;
54 }
55
56 image.draw(g, x, y, w, h);
57
58 g.endClip();
59 }
60}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected