-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbilityButton.java
More file actions
120 lines (111 loc) · 4.25 KB
/
AbilityButton.java
File metadata and controls
120 lines (111 loc) · 4.25 KB
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
/**
* Final Game AbilityButtton Class
* @Author Ilya Kononov
* @Date = January 22 2023
* This class is a button that has an image ontop of it
* This type of button is used with abilities
*/
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JFrame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class AbilityButton extends Button {
private Image image;
private int extraDimensions;
private boolean enabled;
private String name;
private Image description;
public AbilityButton(JFrame window, Image image, Color borderColor, String name, Image description, int centerX, int y, boolean enabled) {
super(window, null, borderColor, null, centerX, y, image.getWidth(), image.getHeight(), 0);
this.image = image;
this.extraDimensions = 0;
this.name = name;
this.description = description;
this.enabled = enabled;
}
// Getters and Setters
public boolean getEnabled(){
return this.enabled;
}
public void setEnabled(boolean enabled){
this.enabled = enabled;
if (!(enabled)){
resetStayHeld();
}
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public Image getDescription(){
return this.description;
}
public void setDescription(Image description){
this.description = description;
}
public Image getImage(){
return this.image;
}
public void setImage(Image image){
if(image.getWidth() == this.image.getWidth() && image.getHeight() == this.image.getHeight()){ // To avoid changing dimensions
this.image = image;
}
}
/**
* This method draws the button onto a component/panel/window.
* @param graphics The graphics of the component to be drawn onto.
*/
public void draw(Graphics graphics) {
// Draw the button body.
if(mouseInside || stayHeld){ // If the button is being hovered over or held draw a larger border to show the client some indication its selected/hovered
this.extraDimensions = Const.ABILITY_BUTTON_BORDER;
}else if (this.extraDimensions != 0){
this.extraDimensions = 0;
}
// Draw the button border.
graphics.setColor(this.borderColor);
graphics.fillRoundRect(this.getXVal() - extraDimensions, this.getYVal() - extraDimensions, this.getWidthVal() + extraDimensions * 2, this.getHeightVal() + extraDimensions * 2,
0, 0);
// Draw the button text.
this.image.draw(graphics, this.getXVal(), this.getYVal());
}
// Mouse clicking actions
public class BasicMouseListener implements MouseListener{
public void mouseClicked(MouseEvent event) {
// If mouse clicks button switch to correct screen
if(enabled){
if (mouseInside) {
stayHeld = !stayHeld;
}
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}
// Interface is used to change colour of button and to find out weather mouse coordinates are inside the button
public class InsideButtonMotionListener implements MouseMotionListener {
public void mouseMoved(MouseEvent event) {
boolean mouseRegisteredInside = (event.getX() >= getXVal()) && (event.getX() <= (getXVal() + getWidthVal())) && (event.getY() >= getYVal()) && (event.getY() <= (getYVal() + getHeightVal()));
if (mouseRegisteredInside && !mouseInside && enabled) {
// Redraw the button if mouse just moved onto button.
mouseInside = true;
window.repaint();
} else if (!mouseRegisteredInside && mouseInside) {
// Redraw the button if mouse just moved off button.
mouseInside = false;
window.repaint();
}
}
public void mouseDragged(MouseEvent e) {}
}
}