Skip to content

Commit e7a425b

Browse files
committed
Feat: Added Reticle Ui
1 parent 122b98b commit e7a425b

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package engine.components;
2+
3+
import math.Color;
4+
import workspace.ui.Graphics;
5+
6+
/**
7+
* A component that renders a round reticle in the center of the screen. The reticle consists of an
8+
* outer circle and a smaller, filled inner circle, both customizable in terms of radius and color.
9+
*/
10+
public class RoundReticle extends AbstractComponent implements RenderableComponent {
11+
12+
private float outerRadius;
13+
14+
private float innerRadius;
15+
16+
private Color color;
17+
18+
/**
19+
* Creates a default RoundReticle with preset values. Default configuration: - Outer radius: 30 -
20+
* Inner radius: 6 - Color: White
21+
*/
22+
public RoundReticle() {
23+
this(30, 6, Color.WHITE);
24+
}
25+
26+
/**
27+
* Creates a RoundReticle with specified outer radius, inner radius, and color.
28+
*
29+
* @param outerRadius the radius of the outer circle.
30+
* @param innerRadius the radius of the inner filled circle.
31+
* @param color the color of the reticle.
32+
* @throws IllegalArgumentException if any radius is non-positive or color is null.
33+
*/
34+
public RoundReticle(float outerRadius, float innerRadius, Color color) {
35+
setOuterRadius(outerRadius);
36+
setInnerRadius(innerRadius);
37+
setColor(color);
38+
}
39+
40+
/**
41+
* Renders the reticle on the screen, centered in the viewport. The reticle includes an outer
42+
* circle and a filled inner circle.
43+
*
44+
* @param g the {@link Graphics} context used for rendering.
45+
*/
46+
@Override
47+
public void render(Graphics g) {
48+
int centerX = g.getWidth() / 2;
49+
int centerY = g.getHeight() / 2;
50+
51+
// Render reticle circles
52+
g.setColor(color);
53+
renderCenteredOval(g, centerX, centerY, outerRadius, false);
54+
renderCenteredOval(g, centerX, centerY, innerRadius, true);
55+
}
56+
57+
/**
58+
* Draws a centered oval at the specified position.
59+
*
60+
* @param g the {@link Graphics} context used for rendering.
61+
* @param centerX the x-coordinate of the oval's center.
62+
* @param centerY the y-coordinate of the oval's center.
63+
* @param radius the radius of the oval.
64+
* @param filled whether the oval should be filled.
65+
*/
66+
private void renderCenteredOval(
67+
Graphics g, int centerX, int centerY, float radius, boolean filled) {
68+
int diameter = (int) radius;
69+
int topLeftX = centerX - diameter / 2;
70+
int topLeftY = centerY - diameter / 2;
71+
72+
if (filled) {
73+
g.fillOval(topLeftX, topLeftY, diameter, diameter);
74+
} else {
75+
g.drawOval(topLeftX, topLeftY, diameter, diameter);
76+
}
77+
}
78+
79+
/**
80+
* Updates the component. Currently, this method does nothing.
81+
*
82+
* @param tpf time per frame, used for animations or updates.
83+
*/
84+
@Override
85+
public void update(float tpf) {}
86+
87+
/** Called when the component is attached to a {@link engine.SceneNode}. */
88+
@Override
89+
public void onAttach() {}
90+
91+
/** Called when the component is detached from a {@link engine.SceneNode}. */
92+
@Override
93+
public void onDetach() {}
94+
95+
/**
96+
* Gets the outer radius of the reticle.
97+
*
98+
* @return the outer radius.
99+
*/
100+
public float getOuterRadius() {
101+
return outerRadius;
102+
}
103+
104+
/**
105+
* Sets the outer radius of the reticle.
106+
*
107+
* @param outerRadius the new outer radius.
108+
* @throws IllegalArgumentException if the radius is non-positive.
109+
*/
110+
public void setOuterRadius(float outerRadius) {
111+
if (outerRadius <= 0) {
112+
throw new IllegalArgumentException("Outer radius must be greater than 0.");
113+
}
114+
this.outerRadius = outerRadius;
115+
}
116+
117+
/**
118+
* Gets the inner radius of the reticle.
119+
*
120+
* @return the inner radius.
121+
*/
122+
public float getInnerRadius() {
123+
return innerRadius;
124+
}
125+
126+
/**
127+
* Sets the inner radius of the reticle.
128+
*
129+
* @param innerRadius the new inner radius.
130+
* @throws IllegalArgumentException if the radius is non-positive.
131+
*/
132+
public void setInnerRadius(float innerRadius) {
133+
if (innerRadius <= 0) {
134+
throw new IllegalArgumentException("Inner radius must be greater than 0.");
135+
}
136+
this.innerRadius = innerRadius;
137+
}
138+
139+
/**
140+
* Gets the color of the reticle.
141+
*
142+
* @return the color.
143+
*/
144+
public Color getColor() {
145+
return color;
146+
}
147+
148+
/**
149+
* Sets the color of the reticle.
150+
*
151+
* @param color the new color.
152+
* @throws IllegalArgumentException if the color is null.
153+
*/
154+
public void setColor(Color color) {
155+
if (color == null) {
156+
throw new IllegalArgumentException("Color cannot be null.");
157+
}
158+
this.color = color;
159+
}
160+
}

0 commit comments

Comments
 (0)