-
Notifications
You must be signed in to change notification settings - Fork 0
/
WoodokuController.java
107 lines (89 loc) · 2.52 KB
/
WoodokuController.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
import java.awt.event.*;
import javax.swing.JPanel;
public class WoodokuController implements MouseListener, MouseMotionListener{
private WoodokuView mainPanel;
private Block currentlyDraggedBlock;
private int offsetX;
private int offsetY;
private int originalX;
private int originalY;
public WoodokuController(JPanel mainPanel, WoodokuView viewPanel) {
this.mainPanel = viewPanel;
mainPanel.addMouseListener(this);
mainPanel.addMouseMotionListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
if(mainPanel.containsPoint(e.getX(),e.getY())==null) {
return;
}
else {
currentlyDraggedBlock = mainPanel.containsPoint(e.getX(),e.getY());
originalX = currentlyDraggedBlock.getX();
originalY = currentlyDraggedBlock.getY();
offsetX = currentlyDraggedBlock.getX()-e.getX();
offsetY = currentlyDraggedBlock.getY()-e.getY();
}
mainPanel.repaint();
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
if(currentlyDraggedBlock == null) {
return;
}
if(currentlyDraggedBlock.outOfBounds()) {
mainPanel.updateBlock(currentlyDraggedBlock, originalX, originalY);
}
else {
currentlyDraggedBlock.snapToGrid();
if(currentlyDraggedBlock.isSetX() == false || currentlyDraggedBlock.isSetY() == false) {
mainPanel.updateBlock(currentlyDraggedBlock, originalX, originalY);
}
else {
currentlyDraggedBlock.addCoordinates();
if(!mainPanel.checkPlacement(currentlyDraggedBlock)) {
mainPanel.updateBlock(currentlyDraggedBlock, originalX, originalY);
currentlyDraggedBlock.removeCoordinates();
}
else {
mainPanel.updateGrid(currentlyDraggedBlock);
}
}
}
currentlyDraggedBlock = null;
mainPanel.repaint();
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
if(currentlyDraggedBlock == null) {
return;
}
else {
mainPanel.updateBlock(currentlyDraggedBlock, e.getX() + offsetX, e.getY() + offsetY);
}
mainPanel.repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
public void endGame() {
mainPanel.removeMouseListener(this);
mainPanel.removeMouseMotionListener(this);
}
}