This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstructions.java
252 lines (213 loc) · 5.35 KB
/
Instructions.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* The instructions screen
*
* Last edit: 5/28/2020
* @author Celeste, Eric
* @version 1.0
* @since 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class Instructions extends Menu {
/**
* Contains the instructions screen graphics / GUI
*/
private MenuDrawing drawing;
/**
* Contains the actual text of the instructions
*/
private String info;
/**
* Back (to main menu) button
*/
private Button back;
/**
* No. of lines that can be displayed at once on the Register_Screen
*/
private static final int MAX_LINES = 7;
/**
* Pages of the instructions
*/
private java.util.List<String[]> slides;
/**
* Current instruction page being displayed
*/
private int pageNo;
/**
* Arrow key bindings
*/
private KeyBindings arrowKeys;
/**
* Width of the information box
*/
private static final int nodeWidth = Utility.FRAME_WIDTH-270;
/**
* Initializes and displays the drawing to the frame
*/
public Instructions() {
info = "";
try {
BufferedReader br = Utility.getBufferedReader("instructions.md");
// skip heading
br.readLine();
for (String nxt = br.readLine(); nxt != null; nxt = br.readLine()) info += nxt+'\n';
br.close();
} catch (Exception e) {System.out.println(e);}
slides = loadSlides();
pageNo = 0;
back = new ReturnButton(100,Utility.FRAME_HEIGHT-100);
arrowKeys = new KeyBindings();
drawing = new InstructionsDrawing();
Utility.changeDrawing(drawing);
bgm = new BGM("menus");
bgm.play();
}
/**
* Activates listeners
*/
public void activate() {
back.activate();
arrowKeys.activate();
}
/**
* Removes the drawing
*/
public void halt() {
CovidCashier.frame.remove(drawing);
back.deactivate();
arrowKeys.deactivate();
}
/**
* Generates a List where each element is the lines to be displayed on the instructions screen at once
* @return a List with the instructions split up per screen
*/
private java.util.List<String[]> loadSlides() {
java.util.List<String[]> slides = new ArrayList<String[]>();
String [] lines = new String[MAX_LINES];
String line = "";
int row=0;
String [] words = info.split(" ");
for (String word : words) {
if (Utility.getStringWidth(line,Utility.TEXT_FONT) > nodeWidth) {
lines[row++] = line;
line = "";
if (row == MAX_LINES) {
row = 0;
slides.add(lines);
lines = new String[MAX_LINES];
}
}
line += word+" ";
if (word.indexOf("\n") == word.length()-1) {
lines[row++] = line;
line = "";
if (row == MAX_LINES) {
row = 0;
slides.add(lines);
lines = new String[MAX_LINES];
}
}
}
if (row != 0 || !line.trim().equals("")) {
lines[row] = line;
slides.add(lines);
}
return slides;
}
/**
* GUI for the instructions
*/
public class InstructionsDrawing extends MenuDrawing {
/**
* The y-coordinate of the title
*/
protected int titleY = 90;
/**
* x-coordinate for left alignment of text
*/
protected int leftAlign = 90;
/**
* Object constructor. Uses the superclass's constructor
*/
public InstructionsDrawing() {
super();
activate();
}
/**
* GUI display of the instructions screen
* @param g the Graphics object to draw on
*/
@Override
public void display(Graphics g) {
g.drawImage(Utility.loadImage("Register_Screen.png",Utility.FRAME_WIDTH,Utility.FRAME_HEIGHT),0,0,null);
g.setColor(Color.black);
g.setFont(Utility.TITLE_FONT);
g.drawString("INSTRUCTIONS",leftAlign,titleY);
g.setFont(Utility.TEXT_FONT);
drawInfo(g);
String note = "Use your arrow keys to flip between the";
g.setFont(Utility.TEXT_FONT_SMALL);
g.drawString(note, Utility.FRAME_WIDTH-50-Utility.getStringWidth(note, g), 435);
note = "pages of the instructions";
g.drawString(note, Utility.FRAME_WIDTH-50-Utility.getStringWidth(note, g), 455);
back.draw(g);
if (back.isClicked()) {
halt();
bgm.stop();
new MainMenu();
}
}
/**
* Draws the instructions onto the screen
* @param g the Graphics object to draw on
*/
private void drawInfo(Graphics g) {
String [] lines = slides.get(pageNo);
int row = 1;
for (String line : lines) {
if (line == null) return;
g.drawString(line,leftAlign,titleY+20+(int)(row*1.5*Utility.TEXT_FONT.getSize()));
row++;
}
}
/**
* Returns the y-coordinate of the title
* @return the MainMenuDrawing object's titleY attribute
*/
@Override
public int getTitleY() {
return titleY;
}
}
/**
* Manages arrow key bindings
*/
private class KeyBindings extends ScreenMovement {
public KeyBindings() {
super("instructions");
}
@Override
protected void loadKeyBindings() {
String [] keys = {"left", "right"};
int [] strokes = {KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT};
for (int i=0; i<keys.length; i++) {
final int index = i;
movementMap.put(keys[index], new Movement(keys[index], KeyStroke.getKeyStroke(strokes[index], 0), new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (keys[index].equalsIgnoreCase("right")) {
pageNo++;
if (pageNo >= slides.size()) pageNo = 0;
} else {
pageNo--;
if (pageNo < 0) pageNo = slides.size()-1;
}
CovidCashier.frame.repaint();
}
}));
}
}
}
}