-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageGraphics.java
148 lines (128 loc) · 4.09 KB
/
ImageGraphics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package ch.epfl.cs107.play.game.actor;
import ch.epfl.cs107.play.math.Attachable;
import ch.epfl.cs107.play.math.Node;
import ch.epfl.cs107.play.math.Transform;
import ch.epfl.cs107.play.math.Vector;
import ch.epfl.cs107.play.window.Canvas;
import ch.epfl.cs107.play.window.Image;
/**
* Contains information to render a single image, which can be attached to any positionable.
*/
public class ImageGraphics extends Node implements Graphics {
private String name;
private float width;
private float height;
private Vector anchor;
private float alpha;
private float depth;
/**
* Creates a new image graphics.
* @param name image name, may be null
* @param width actual image width, before transformation
* @param height actual image height, before transformation
* @param anchor image anchor, not null
* @param alpha transparency, between 0 (invisible) and 1 (opaque)
* @param depth render priority, lower-values drawn first
*/
public ImageGraphics(String name, float width, float height, Vector anchor, float alpha, float depth) {
this.name = name;
this.width = width;
this.height = height;
this.anchor = anchor;
this.alpha = alpha;
this.depth = depth;
}
/**
* Creates a new image graphics.
* @param name image name, not null
* @param width actual image width, before transformation
* @param height actual image height, before transformation
* @param anchor image anchor, not null
*/
public ImageGraphics(String name, float width, float height, Vector anchor) {
this(name, width, height, anchor, 1.0f, 0.0f);
}
/**
* Creates a new image graphics.
* @param name image name, not null
* @param width actual image width, before transformation
* @param height actual image height, before transformation
*/
public ImageGraphics(String name, float width, float height) {
this(name, width, height, Vector.ZERO);
}
/**
* Sets image name.
* @param name new image name, may be null
*/
public void setName(String name) {
this.name = name;
}
/** @return image name, may be null */
public String getName() {
return name;
}
/**
* Sets actual image width, before transformation.
* @param width image width
*/
public void setWidth(float width) {
this.width = width;
}
/** @return actual image width, before transformation */
public float getWidth() {
return width;
}
/**
* Sets actual image height, before transformation.
* @param height image height
*/
public void setHeight(float height) {
this.height = height;
}
/** @return actual image height, before transformation */
public float getHeight() {
return height;
}
/**
* Sets image anchor location, i.e. where is the center of the image.
* @param anchor image anchor, not null
*/
public void setAnchor(Vector anchor) {
this.anchor = anchor;
}
/** @return image anchor, not null */
public Vector getAnchor() {
return anchor;
}
/**
* Sets transparency.
* @param alpha transparency, between 0 (invisible) and 1 (opaque)
*/
public void setAlpha(float alpha) {
this.alpha = alpha;
}
/** @return transparency, between 0 (invisible) and 1 (opaque) */
public float getAlpha() {
return alpha;
}
/**
* Sets rendering depth.
* @param depth render priority, lower-values drawn first
*/
public void setDepth(float depth) {
this.depth = depth;
}
/** @return render priority, lower-values drawn first */
public float getDepth() {
return depth;
}
@Override
public void draw(Canvas canvas) {
if (name == null)
return;
Image image = canvas.getImage(name);
Transform transform = Transform.I.translated(-anchor.x, -anchor.y).scaled(width, height).transformed(getTransform());
canvas.drawImage(image, transform, alpha, depth);
}
}