forked from sbastn/com.happyprog.pairhero
/
MainView.java
223 lines (180 loc) · 6.01 KB
/
MainView.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
package com.happyprog.pairhero.views;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import com.happyprog.pairhero.Activator;
import com.happyprog.pairhero.actions.StartAction;
import com.happyprog.pairhero.actions.StopAction;
import com.happyprog.pairhero.game.Game;
import com.happyprog.pairhero.game.Programmer;
import com.happyprog.pairhero.subscribers.JUnitSubscriber;
import com.happyprog.pairhero.subscribers.RefactoringSubscriber;
import com.happyprog.pairhero.time.TimeFormatter;
import com.happyprog.pairhero.time.Timer;
public class MainView extends ViewPart {
public static final String ID = "com.happyprog.pairhero.views.MainView";
private Label timerLabel;
private Label scoreLabel;
private Label messageLabel;
private int messageDelayCounter;
private Programmer leftProgrammer;
private Programmer rightProgrammer;
private Composite parent;
private StartAction startButton;
private Game game;
private StopAction stopButton;
@Override
public void createPartControl(Composite parent) {
this.parent = parent;
// parent.setLayout(createLayout());
createStartButton();
leftProgrammer = new Programmer(parent);
rightProgrammer = new Programmer(parent);
createMessageArea(parent);
createScoreboard(parent);
parent.layout();
}
private void createMessageArea(Composite parent) {
Composite group = new Composite(parent, SWT.NONE);
group.setLayout(new GridLayout());
messageLabel = new Label(group, SWT.NONE);
messageLabel.setImage(Activator.getImageDescriptor("icons/great.png").createImage());
GridData gridData = new GridData();
gridData.horizontalSpan = 4;
messageLabel.setLayoutData(gridData);
}
private void createScoreboard(Composite parent) {
Group group = new Group(parent, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
group.setLayout(layout);
new Label(group, SWT.NONE).setText("Score:");
scoreLabel = new Label(group, SWT.NONE);
scoreLabel.setText("0 ");
new Label(group, SWT.NONE).setText("Time left:");
timerLabel = new Label(group, SWT.NONE);
timerLabel.setText(TimeFormatter.formatTime(Timer._25_MINS));
}
private RowLayout createLayout() {
RowLayout layout = new RowLayout();
layout.wrap = true;
layout.pack = true;
layout.justify = false;
layout.type = SWT.VERTICAL;
return layout;
}
private void createStartButton() {
IToolBarManager toolbarManager = getViewSite().getActionBars().getToolBarManager();
startButton = new StartAction(this);
stopButton = new StopAction(this);
stopButton.setEnabled(false);
toolbarManager.add(startButton);
toolbarManager.add(stopButton);
}
public void onStart() {
if (ableToCreatePlayers()) {
startGame();
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
parent.layout();
}
private void startGame() {
game = new Game(this, new Timer(), leftProgrammer, rightProgrammer, new JUnitSubscriber(),
new RefactoringSubscriber());
game.start();
}
private boolean ableToCreatePlayers() {
StartDialog dialog = new StartDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
dialog.open();
if (dialog.getReturnCode() == Dialog.OK) {
leftProgrammer.resetStats();
rightProgrammer.resetStats();
leftProgrammer.setName(dialog.getPlayerOneName());
rightProgrammer.setName(dialog.getPlayerTwoName());
return true;
}
return false;
}
@Override
public void setFocus() {
// TODO Auto-generated method stub
}
public void onGameFinished(String message) {
EndDialog dialog = new EndDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), message);
dialog.open();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
public void onTimeChange(int timeInSeconds) {
updateScore(timerLabel, TimeFormatter.formatTime(timeInSeconds));
updateMessageToDefault();
}
private void updateScore(final Label label, final String text) {
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
label.setText(text);
}
});
}
public void onStop() {
boolean response = MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
"Pair Hero", "Are you sure you want to stop this session?");
if (response) {
game.stop();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
public void onSwitchRole(int score, int multiplier) {
showMessageAndUpdateScore(getSwitchRoleImage(multiplier), score);
}
public void onRefactoring(int score) {
showMessageAndUpdateScore("icons/great.png", score);
}
public void onGreenTest(int score) {
showMessageAndUpdateScore("icons/okay.png", score);
}
private void showMessageAndUpdateScore(String imagePath, int score) {
updateMessage(messageLabel, Activator.getImageDescriptor(imagePath).createImage());
updateScore(score);
messageDelayCounter = 3;
}
private void updateScore(int score) {
updateScore(scoreLabel, String.format("%d", score));
}
private void updateMessageToDefault() {
if (messageDelayCounter < 0) {
updateMessage(messageLabel, Activator.getImageDescriptor("icons/blank.png").createImage());
}
messageDelayCounter--;
}
private void updateMessage(final Label label, final Image image) {
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
label.setImage(image);
}
});
}
private String getSwitchRoleImage(int multiplier) {
if (multiplier == Game._4X_MULTIPLIER) {
return "icons/insane.png";
} else if (multiplier == Game._2_MULTIPLIER) {
return "icons/great.png";
} else {
return "icons/okay.png";
}
}
}