-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathElementController.java
More file actions
351 lines (325 loc) · 13 KB
/
ElementController.java
File metadata and controls
351 lines (325 loc) · 13 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package edu.rpi.legup.controller;
import static edu.rpi.legup.app.GameBoardFacade.*;
import edu.rpi.legup.app.GameBoardFacade;
import edu.rpi.legup.app.LegupPreferences;
import edu.rpi.legup.history.AutoCaseRuleCommand;
import edu.rpi.legup.history.EditDataCommand;
import edu.rpi.legup.history.ICommand;
import edu.rpi.legup.model.Puzzle;
import edu.rpi.legup.model.elements.Element;
import edu.rpi.legup.model.gameboard.Board;
import edu.rpi.legup.model.gameboard.CaseBoard;
import edu.rpi.legup.model.gameboard.GridBoard;
import edu.rpi.legup.model.gameboard.PuzzleElement;
import edu.rpi.legup.model.tree.TreeElement;
import edu.rpi.legup.model.tree.TreeElementType;
import edu.rpi.legup.model.tree.TreeTransition;
import edu.rpi.legup.puzzle.treetent.TreeTentBoard;
import edu.rpi.legup.ui.DynamicView;
import edu.rpi.legup.ui.boardview.BoardView;
import edu.rpi.legup.ui.boardview.ElementSelection;
import edu.rpi.legup.ui.boardview.ElementView;
import edu.rpi.legup.ui.boardview.SelectionItemView;
import edu.rpi.legup.ui.proofeditorui.treeview.*;
import java.awt.*;
import java.awt.event.*;
public class ElementController
implements MouseListener, MouseMotionListener, ActionListener, KeyListener {
protected BoardView boardView;
private Element selectedElement;
/**
* ElementController Constructor controller to handles ui events associated interacting with a
* {@link BoardView}
*/
public ElementController() {
this.boardView = null;
this.selectedElement = null;
}
public void setSelectedElement(Element selectedElement) {
this.selectedElement = selectedElement;
}
/**
* Sets the {@link BoardView}
*
* @param boardView board view
*/
public void setBoardView(BoardView boardView) {
this.boardView = boardView;
}
/**
* Invoked when the mouse button has been clicked (pressed and released) on a component.
*
* @param e the event to be processed
*/
@Override
public void mouseClicked(MouseEvent e) {}
/**
* Invoked when a mouse button has been pressed on a component.
*
* @param e the event to be processed
*/
@Override
public void mousePressed(MouseEvent e) {}
/**
* Invoked when a mouse button has been released on a component.
*
* @param e the event to be processed
*/
@Override
public void mouseReleased(MouseEvent e) {
TreePanel treePanel = GameBoardFacade.getInstance().getLegupUI().getTreePanel();
TreeView treeView = null;
if (treePanel != null) {
treeView = treePanel.getTreeView();
}
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
Board board = boardView.getBoard();
ElementView elementView = boardView.getElement(e.getPoint());
TreeViewSelection selection = null;
if (treeView != null) {
selection = treeView.getSelection();
}
// funny
if (elementView != null) {
if (board instanceof CaseBoard) {
CaseBoard caseBoard = (CaseBoard) board;
AutoCaseRuleCommand autoCaseRuleCommand =
new AutoCaseRuleCommand(
elementView, selection, caseBoard.getCaseRule(), caseBoard, e);
if (autoCaseRuleCommand.canExecute()) {
autoCaseRuleCommand.execute();
getInstance().getHistory().pushChange(autoCaseRuleCommand);
if (treePanel != null) {
treePanel.updateError("");
}
} else {
if (treePanel != null) {
treePanel.updateError(autoCaseRuleCommand.getError());
}
}
} else {
if (selection != null) {
ICommand edit = new EditDataCommand(elementView, selection, e);
if (edit.canExecute()) {
edit.execute();
getInstance().getHistory().pushChange(edit);
if (treePanel != null) {
treePanel.updateError("");
}
} else {
if (treePanel != null) {
treePanel.updateError(edit.getError());
}
}
}
}
}
// if (selectedElement != null) {
GridBoard b = (GridBoard) this.boardView.getBoard();
Point point = e.getPoint();
Point scaledPoint =
new Point(
(int) Math.floor(point.x / (30 * this.boardView.getScale())),
(int) Math.floor(point.y / (30 * this.boardView.getScale())));
if (this.boardView.getBoard() instanceof TreeTentBoard) {
scaledPoint.setLocation(scaledPoint.getX() - 1, scaledPoint.getY() - 1);
}
System.out.printf(
"selected Element is NOT null, attempting to change board at (%d, %d)\n",
scaledPoint.x, scaledPoint.y);
// System.out.println("Before: " + b.getCell(scaledPoint.x,
// scaledPoint.y).getData());
b.setCell(scaledPoint.x, scaledPoint.y, this.selectedElement, e);
// System.out.println("After: " + b.getCell(scaledPoint.x,
// scaledPoint.y).getData());
// } else {
// System.out.println("selected Element is null!");
// }
boardView.repaint();
}
/**
* Invoked when the mouse enters a component.
*
* @param e the event to be processed
*/
@Override
public void mouseEntered(MouseEvent e) {
boardView.setFocusable(true);
boardView.requestFocusInWindow();
TreeElement treeElement = boardView.getTreeElement();
DynamicView dynamicView = getInstance().getLegupUI().getDynamicBoardView();
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
if (dynamicView == null) {
dynamicView = getInstance().getLegupUI().getEditorDynamicBoardView();
}
Board board = boardView.getBoard();
ElementView elementView = boardView.getElement(e.getPoint());
ElementSelection selection = boardView.getSelection();
String error = null;
if (elementView != null) {
selection.newHover(elementView);
if (LegupPreferences.LegupPreference.SHOW_MISTAKES.asBoolean()) {
PuzzleElement element = elementView.getPuzzleElement();
if (treeElement != null
&& treeElement.getType() == TreeElementType.TRANSITION
&& board.getModifiedData().contains(element)) {
TreeTransition transition = (TreeTransition) treeElement;
if (transition.isJustified() && !transition.isCorrect()) {
error = transition.getRule().checkRuleAt(transition, element);
}
}
if (error != null) {
dynamicView.updateError(error);
} else {
dynamicView.resetStatus();
}
}
boardView.repaint();
}
}
/**
* Invoked when the mouse exits a component.
*
* @param e the event to be processed
*/
@Override
public void mouseExited(MouseEvent e) {
boardView.setFocusable(false);
DynamicView dynamicView = getInstance().getLegupUI().getDynamicBoardView();
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
if (dynamicView == null) {
dynamicView = getInstance().getLegupUI().getEditorDynamicBoardView();
}
ElementView element = boardView.getElement(e.getPoint());
if (element != null) {
boardView.getSelection().clearHover();
dynamicView.resetStatus();
boardView.repaint();
}
}
/**
* Invoked when the mouse dragged
*
* @param e the event to be processed
*/
@Override
public void mouseDragged(MouseEvent e) {}
/**
* Invoked when the mouse moved
*
* @param e the event to be processed
*/
@Override
public void mouseMoved(MouseEvent e) {
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
Board board = boardView.getBoard();
TreeElement treeElement = boardView.getTreeElement();
DynamicView dynamicView = getInstance().getLegupUI().getDynamicBoardView();
if (dynamicView == null) {
dynamicView = getInstance().getLegupUI().getEditorDynamicBoardView();
}
ElementView elementView = boardView.getElement(e.getPoint());
ElementSelection selection = boardView.getSelection();
String error = null;
if (elementView != null && elementView != selection.getHover()) {
selection.newHover(elementView);
if (LegupPreferences.LegupPreference.SHOW_MISTAKES.asBoolean()) {
PuzzleElement element = elementView.getPuzzleElement();
if (treeElement != null
&& treeElement.getType() == TreeElementType.TRANSITION
&& board.getModifiedData().contains(element)) {
TreeTransition transition = (TreeTransition) treeElement;
if (transition.isJustified() && !transition.isCorrect()) {
error = transition.getRule().checkRuleAt(transition, element);
}
}
if (error != null) {
dynamicView.updateError(error);
} else {
dynamicView.resetStatus();
}
}
boardView.repaint();
}
}
public void changeCell(MouseEvent e, PuzzleElement data) {}
/**
* Invoked when an action occurs.
*
* @param e the event to be processed
*/
@SuppressWarnings("Unchecked")
@Override
public void actionPerformed(ActionEvent e) {
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
ElementView selectedElement = boardView.getSelection().getFirstSelection();
PuzzleElement puzzleElement = selectedElement.getPuzzleElement();
TreeView treeView = GameBoardFacade.getInstance().getLegupUI().getTreePanel().getTreeView();
TreeViewSelection selection = treeView.getSelection();
TreeElementView selectedView = selection.getFirstSelection();
TreeTransitionView transitionView = (TreeTransitionView) selectedView;
Board prevBord = transitionView.getTreeElement().getParents().get(0).getBoard();
int value = (Integer) ((SelectionItemView) e.getSource()).getData().getData();
puzzleElement.setData(value);
if (puzzleElement.equalsData(prevBord.getPuzzleElement(puzzleElement))) {
puzzleElement.setModified(false);
} else {
puzzleElement.setModified(true);
}
transitionView.getTreeElement().propagateChange(puzzleElement);
boardView.repaint();
boardView.getSelection().clearSelection();
}
/**
* Invoked when a key has been typed. See the class description for {@link KeyEvent} for a
* definition of a key typed event.
*
* @param e the event to be processed
*/
@Override
public void keyTyped(KeyEvent e) {}
/**
* Invoked when a key has been pressed. See the class description for {@link KeyEvent} for a
* definition of a key pressed event.
*
* @param e the event to be processed
*/
@Override
public void keyPressed(KeyEvent e) {}
/**
* Invoked when a key has been released. See the class description for {@link KeyEvent} for a
* definition of a key released event.
*
* @param e the event to be processed
*/
@Override
public void keyReleased(KeyEvent e) {
Puzzle puzzle = GameBoardFacade.getInstance().getPuzzleModule();
BoardView boardView = getInstance().getLegupUI().getBoardView();
if (boardView == null) {
boardView = getInstance().getLegupUI().getEditorBoardView();
}
Board board = boardView.getBoard();
if (board instanceof CaseBoard) {
CaseBoard caseBoard = (CaseBoard) board;
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
puzzle.notifyBoardListeners(listener -> listener.onCaseBoardAdded(caseBoard));
}
}
}
}