-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
68 lines (62 loc) · 2.8 KB
/
Node.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
import greenfoot.Greenfoot;
import greenfoot.World;
import greenfoot.GreenfootImage;
import greenfoot.MouseInfo;
import greenfoot.Color;
public class Node extends MapElements { // Node display - Developer mode only
private int id;
private int xOffset;
private int yOffset;
private int active = -1; // Current node selected for connection adding
private double zoom;
private boolean mouseDown = false;
private boolean already = true;
private GreenfootImage image = new GreenfootImage(15,15);
private Map map;
public Node(int id) {
this.id = id;
image = new GreenfootImage(15,15);
image.setColor(new Color(255,255,255,190));
image.fillOval(-15,-15,30,30); // New node object is made for each node in droplet shape
image.setColor(new Color(0,0,0,255));
image.setFont(image.getFont().deriveFont(9f));
image.drawString(Integer.toString(id), 0, 10);
setImage(image);
}
protected void addedToWorld(World world) {
updateLocation(); // Set location once added to the world
}
public int getId() {
return id;
}
public void updateLocation() {
Map map = ((Directions)getWorld()).map; // Get data from map and set location and size based on this
zoom = map.getZoom();
xOffset = map.getXOffset()-1100;
yOffset = map.getYOffset()-629;
setLocation((int)((map.getNodeX(id)+xOffset-wid)*zoom)+wid, (int)((map.getNodeY(id)+yOffset-hei)*zoom)+hei);
if (mouseDown && (Greenfoot.mouseDragEnded(null) || Greenfoot.mouseClicked(null))) {
mouseDown = false;
((Directions)getWorld()).map.setMap(true); // When dragging, update connection markings on the map
}
if (Greenfoot.mouseClicked(this) && Greenfoot.isKeyDown("n")) { // Create new connection when node clicked and "n" key pressed
if (map.getActive() != -1 && map.getActive() != id) { // Second node clicked will intiate the add node connection function
map.addConnection(id,map.getActive());
map.setMap(true); // Update map with new connection present
map.setActive(-1); // Reset active variable
} else {
map.setActive(id); // First node clicked is the active one
}
}
if (!mouseDown && Greenfoot.mousePressed(this) && Greenfoot.isKeyDown("m")) {
mouseDown = true; // When "m" key pressed, allow for node to be moved
}
if (mouseDown) {
MouseInfo mouse = Greenfoot.getMouseInfo(); // When moving, set node location to mouse coordinates
map.changeNode(id,(int)((mouse.getX()-wid)/zoom)+wid-xOffset,(int)((mouse.getY()-hei)/zoom)+hei-yOffset);
}
}
public void act() {
updateLocation();
}
}