-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.java
More file actions
113 lines (106 loc) · 3.12 KB
/
Button.java
File metadata and controls
113 lines (106 loc) · 3.12 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* This is a superclass for all UI Buttons.
* <p>
* The class is responsible for setting a button's state and applying it's function.
*
* @see Fixed
*
* @author Roșca Paul-Teodor
* @version 1.0 (22/12/2020)
*/
public class Button extends Fixed
{
/**
* Flags that tell us if the user clicked this button or if the mouse is
* hovering over it
*/
private boolean pressed=false,focused=false;
/**
* The world in which the button is present
*/
protected Background world;
/**
* The size of the button
*/
private int xScale,yScale;
/**
* Default constructor that sets the button's world
* @param myWorld the world in which the button is present
*/
public Button(Background myWorld)
{
world=myWorld;
}
/**
* Method that continiously updates the button's state
*/
public void act()
{
focused=false;
CheckMouse();
}
/**
* Method that sets the button's state according to mouse information
*/
private void CheckMouse()
{
MouseInfo mouse=Greenfoot.getMouseInfo();
if(mouse!=null)
{
// We get all the objects that the mouse currently points at
List objects = getWorld().getObjectsAt(mouse.getX(), mouse.getY(), Button.class);
for (Object object : objects)
{
if (object == this)// If the mouse is hovering over this object
{
focused=true;// We flag it as focused
if(!pressed)// Unless it's also pressed
setFocused();// We set it as focused
break;
}
}
}
if(Greenfoot.mousePressed(this))// If the user pressed on this object
{
pressed=true;// We flag it as pressed
setPressed();// We set it as pressed
}
else if(Greenfoot.mouseClicked(this))// If the user clicked on this object
{
pressed=false;// We flag it as not pressed
setNormal();// We set it to normal
applyFunction();// We apply the button's fucntion
}
else if(!focused)// If the button isn't focused
{
pressed=false;// We flag it as not pressed also
setNormal();// We set it to normal
}
}
/**
* Method called when the button is in normal mode (<b>to be overriden</b>)
*/
public void setNormal(){}
/**
* Method called when the button is in focused mode (<b>to be overriden</b>)
*/
public void setFocused(){}
/**
* Method called when the button is in pressed mode (<b>to be overriden</b>)
*/
public void setPressed(){}
/**
* Method that applies the function of the button.
* It calls {@link function}
*/
protected void applyFunction()
{
function("");
}
/**
* Method called when the user clicks on the button (<b>to be overriden</b>)
*/
public void function(String k){}
}