-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainInterface.java
More file actions
202 lines (157 loc) · 6.56 KB
/
MainInterface.java
File metadata and controls
202 lines (157 loc) · 6.56 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
package sample;
import com.sun.javafx.geom.Vec2d;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import java.io.IOException;
import java.util.ArrayList;
/**
* Created by Jordan on 30/07/2017.
*/
public class MainInterface {
private Controller controller;
private Sidemenu sidemenu;
private Node sidemenuNode;
private ArrayList<MapObject> mapObjects;
private Point2D currentOffset;
private double zoomScale;
private SelectableMapObject currentSelectedMapObject;
MainInterface(Controller controller){
this.controller = controller;
controller.setMainInterface(this);
this.mapObjects = new ArrayList<MapObject>();
//temp canvas location
this.currentOffset = new Point2D(0,0);
this.zoomScale = 10;
createCreatorMenu();
//test mapobject ****************************************************************
Point2D testLocation = new Point2D(10,10);
Vec2d testSize = new Vec2d(40,40);
SelectableMapObject test = new SelectableMapObject("test",testLocation, this, testSize, 10){
@Override
public void draw(GraphicsContext gc) {
gc.setFill(Color.GREEN);
Point2D localLocation = this.canvasLocation();
gc.fillRect(localLocation.getX(), localLocation.getY(), testSize.x*this.getMainInterface().getZoomScale(), testSize.y*this.getMainInterface().getZoomScale());
}
};
mapObjects.add(test);
Point2D testLocation2 = new Point2D(20,20);
Vec2d testSize2 = new Vec2d(20,20);
SelectableMapObject test2 = new SelectableMapObject("test2", testLocation2, this, testSize2, 20){
@Override
public void draw(GraphicsContext gc) {
gc.setFill(Color.BLUE);
Point2D localLocation = this.canvasLocation();
gc.fillRect(localLocation.getX(), localLocation.getY(), 20*this.getMainInterface().getZoomScale(), 20*this.getMainInterface().getZoomScale());
}
};
mapObjects.add(test2);
Point2D testLocation3 = new Point2D(-20,-20);
Vec2d testSize3 = new Vec2d(5,5);
SelectableMapObject test3 = new SelectableMapObject("test3", testLocation3, this, testSize3, 30){
@Override
public void draw(GraphicsContext gc) {
gc.setFill(Color.RED);
Point2D localLocation = this.canvasLocation();
gc.fillRect(localLocation.getX(), localLocation.getY(), 5*this.getMainInterface().getZoomScale(), 5*this.getMainInterface().getZoomScale());
}
};
mapObjects.add(test3);
mapObjects.add(new TrackNode(new Point2D(49,45), this));
mapObjects.add(new TrackNode(new Point2D(80,49), this));
mapObjects.add(new TrackNode(new Point2D(80,50), this));
//********************************************************************************************
controller.setMapObjects(mapObjects);
}
public Point2D getCanvasOffset(){
return currentOffset;
}
public Vec2d getCanvasSize() {
return controller.getCanvasSize();
}
public double getZoomScale(){
return zoomScale;
}
public void sendClickEvent(MouseEvent mEvent, Cursor cursor){
if(cursor == Cursor.CROSSHAIR)
selectMapObject(mEvent);
}
public void setZoomScale(double zoomScale){
this.zoomScale = zoomScale;
System.out.println(zoomScale);
}
public void adjustCurrentOffset(double x, double y){
this.currentOffset = this.currentOffset.add(x/getZoomScale(), y/getZoomScale());
}
public void setCurrentOffset(int x, int y){
this.currentOffset = new Point2D(x, y);
}
public SelectableMapObject getCurrentSelectedMapObject(){
return currentSelectedMapObject;
}
private boolean selectMapObject(MouseEvent mEvent){
boolean objectSelected = false;
ArrayList<SelectableMapObject> objectsInBounds = new ArrayList<SelectableMapObject>();
for(MapObject mapObject: mapObjects){
if(mapObject.selectable.Value) {
SelectableMapObject selectableMapObject = (SelectableMapObject) mapObject;
if(selectableMapObject.isTouched(XYtoPoint((int) mEvent.getX(), (int) mEvent.getY()))){
objectsInBounds.add(selectableMapObject);
objectSelected = true;
}
}
}
if(objectSelected) {
SelectableMapObject smallest = objectsInBounds.get(0);
for(SelectableMapObject currentObject: objectsInBounds){
if(vectorMagnitude(currentObject.size.Value) < vectorMagnitude(smallest.size.Value)){
smallest = currentObject;
}
}
currentSelectedMapObject = smallest;
}else{
currentSelectedMapObject = null;
}
updateSideMenu(currentSelectedMapObject);
return objectSelected;
}
public static double vectorMagnitude (Vec2d vector){
return Math.sqrt(vector.x * vector.x + vector.y * vector.y);
}
private Point2D XYtoPoint(int x, int y){
Point2D newPoint = new Point2D(x, y);
return newPoint;
}
private void updateSideMenu(MapObject target){
if(this.currentSelectedMapObject == null){
controller.setSideListNodes(new Node[]{sidemenuNode});
}
else
{
controller.setSideListNodes(ControlCreator.CreateNode(target.getProperties()));
}
}
private void createCreatorMenu(){
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("sidemenu.fxml"));
sidemenuNode = loader.load();
sidemenu = loader.getController();
sidemenu.setMainInterface(this);
}catch (IOException e){
}
}
public void addNewMapObject(MapObject mapObject){
this.mapObjects.add(mapObject);
controller.setMapObjects(mapObjects);
}
public void removeMapObject(MapObject mapObject){
this.mapObjects.remove(mapObject);
controller.setMapObjects(mapObjects);
}
}