-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyBindEditor.java
155 lines (134 loc) · 3.82 KB
/
KeyBindEditor.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
149
150
151
152
153
154
155
import greenfoot.Actor;
import greenfoot.Color;
import greenfoot.Greenfoot;
import greenfoot.GreenfootImage;
/**
* Asks User for input, and updates key-binds (Determines which keys to
* use for the four keys)
*
* @author Team APCSA 2019
* @author Will LoGiudice
* @since 2019-05-29
*/
public class KeyBindEditor extends Actor
{
/** Total spacing */
private static final int TOTAL_SPACING = Constants.SELECTION_MIN_SPACING * (Constants.NUM_COLS + 1);
/** Width */
private static final int WIDTH = (int) ((double) (Constants.WIDTH - TOTAL_SPACING) / Constants.NUM_COLS);
/** Height */
private static final int HEIGHT = 30;
/** Transparency of the background colors */
private static final int TRANSPARENCY = (int) (255 * 0.75);
/** Which one is currently being focused on? */
private static KeyBindEditor selected;
/** The column it is setting for. */
private final int column;
/**
* Construct a new key bind editor object
*
* @param column The column that it is setting for.
*/
public KeyBindEditor(int column)
{
this.column = column;
}
/**
* Initialize the image and position
*/
public void init()
{
// Calculate location
double thisSpacing = Constants.SELECTION_MIN_SPACING * (column + 1);
double x = thisSpacing + WIDTH / 2.0 + WIDTH * column;
int y = Constants.HEIGHT - 40;
// Set location
setLocation((int) x, y);
// Update image
updateImage();
}
/**
* Update Image
*/
private void updateImage()
{
// Create image
GreenfootImage image = new GreenfootImage(WIDTH, HEIGHT);
// Fill background (Blue if selected, white if not.)
image.setColor(selected == this ? new Color(132, 255, 255) : Color.WHITE);
image.setTransparency(TRANSPARENCY);
image.fill();
// Fill text
GreenfootImage textImage = new GreenfootImage(getText(), 16, null, null);
image.drawImage(textImage, (image.getWidth() - textImage.getWidth()) / 2, 15 - textImage.getHeight() / 2);
setImage(image);
}
/**
* Act: Process mouse and keyboard clicks: Takes in what user types
* and displays it back and updates key-bind.
*/
public void act()
{
// When this is selected.
if (selected == this)
{
// Click anywhere else: Unselect this.
if (Greenfoot.mouseClicked(null) && !Greenfoot.mouseClicked(this))
{
select(false);
return;
}
// Get current key.
String key = Greenfoot.getKey();
// No key
if (key == null) return;
// Escape: Unselect this
if ("escape".equals(key))
{
select(false);
return;
}
// A key: set that key to the key bind.
if (key.length() == 1)
{
Constants.KEYS[column] = key.toUpperCase();
select(false);
}
}
else
{
// Click: set this to selected.
if (Greenfoot.mouseClicked(this))
{
select(true);
}
}
}
/**
* Generate text.
*
* @return Text showing on the button.
*/
private String getText()
{
if (selected == this) return "Please enter Key " + (column + 1) + ":";
return "Key " + (column + 1) + ": " + Constants.KEYS[column];
}
/**
* Update select state.
*
* @param select Selected or not.
*/
private void select(boolean select)
{
if (select)
{
selected = this;
}
else
{
selected = null;
}
updateImage();
}
}