Parcourir la source

Fixes Control+Click on unselected SmartDevices

Andreas T. Meyer-Berg il y a 6 ans
Parent
commit
b1248a6cc5

+ 19 - 24
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -99,6 +99,7 @@ public class VisualisationInteractor implements MouseInputListener,
 	 */
 	public LinkedList<SmartDevice> selectedDevices = new LinkedList<SmartDevice>();
 	public LinkedList<SmartDevice> selectedDevicesDrag = new LinkedList<SmartDevice>();
+	private boolean draggedDeviceWasSelected = false;
 	
 	public static final byte NOTHING = 0; 
 	public static final byte RIGHTCLICK_MENU = 1; 
@@ -240,13 +241,12 @@ public class VisualisationInteractor implements MouseInputListener,
 		clicked = getSmartDeviceAtPosition(e.getX(), e.getY());
 		
 		switch (mode) {
-		case SELECTED_DRAG:
-			// Finish Drag Device operations
-			finishDrag();
-			break;
 		case DRAG_CONNECTION:
 			finishConnectionCreation();			
 			break;
+		case SELECTED_DRAG:
+			// Finish Drag Device operations
+			finishDrag();
 		case SELECTED:			
 		case NOTHING:
 			if(!selectedDevices.isEmpty()&&!controlDown){
@@ -256,8 +256,11 @@ public class VisualisationInteractor implements MouseInputListener,
 				}
 			}
 			if(clicked!=null){
-				if(!controlDown || clicked != null && !selectedDevices.remove(clicked))
+				if(!controlDown)
+					selectedDevices.add(clicked);
+				else if(selectedDevices.remove(clicked)&&!draggedDeviceWasSelected){
 					selectedDevices.add(clicked);
+				}
 				mode = SELECTED;
 				panel.repaint();
 			}else{
@@ -321,9 +324,9 @@ public class VisualisationInteractor implements MouseInputListener,
 		SmartDevice pressedOn = getSmartDeviceAtPosition(dragged_x, dragged_y);
 		switch (mode) {
 		case SELECTED:
-		case RIGHTCLICK_MENU:
 			if(!controlDown)
 				mode = NOTHING;
+		case RIGHTCLICK_MENU:
 		case NOTHING:
 			if (pressedOn == null){
 				//Click drag - multi select
@@ -332,11 +335,13 @@ public class VisualisationInteractor implements MouseInputListener,
 				mode = DRAG_SELECT;
 				if(!controlDown)
 					selectedDevices.clear();
-				return;
+				break;
 			} else if (e.getButton() == MouseEvent.BUTTON1){// && connectionFrom == null) {
 				// Recognize possible drag operation
-				if(!selectedDevices.contains(pressedOn)){
-					selectedDevices.clear();
+				draggedDeviceWasSelected = selectedDevices.contains(pressedOn);
+				if(!draggedDeviceWasSelected){
+					if(!controlDown)
+						selectedDevices.clear();
 					selectedDevices.add(pressedOn);
 				}
 				dragged = pressedOn;
@@ -368,6 +373,7 @@ public class VisualisationInteractor implements MouseInputListener,
 			break;
 		case SELECTED_DRAG:
 			finishDrag();
+			
 			break;
 		case DRAG_CONNECTION:
 			finishConnectionCreation();
@@ -445,25 +451,13 @@ public class VisualisationInteractor implements MouseInputListener,
 			}
 			break;
 		case SELECTED_DRAG:
-			/**
-			if (e.getX() <= control.getDevice_visualization_radius())
-				dragged_x = control.getDevice_visualization_radius();
-			else if (e.getX() >= model.getWidth() - control.getDevice_visualization_radius())
-				dragged_x = model.getWidth() - control.getDevice_visualization_radius();
-			else
-				dragged_x = e.getX();
-			// move dragged object on screen along y Axis
-			if (e.getY() <= control.getDevice_visualization_radius())
-				dragged_y = control.getDevice_visualization_radius();
-			else if (e.getY() >= model.getHeight() - control.getDevice_visualization_radius())
-				dragged_y = model.getHeight() - control.getDevice_visualization_radius();
-			else
-				dragged_y = e.getY();*/
+			//new Position of the dragged Device
 			dragged_x = e.getX();
 			dragged_y = e.getY();
+			//offset of the new Position - old position
 			int x_offset = dragged_x - dragged.getX();
 			int y_offset = dragged_y - dragged.getY();
-			//Validate for all moved devices
+			//Validate for all moved devices, that they are within the model, if not change offset
 			for(SmartDevice d:selectedDevices){
 				if (d.getX() + x_offset <= control.getDevice_visualization_radius())
 					x_offset = control.getDevice_visualization_radius()-d.getX();
@@ -474,6 +468,7 @@ public class VisualisationInteractor implements MouseInputListener,
 				else if (d.getY() + y_offset >= model.getHeight() - control.getDevice_visualization_radius())
 					y_offset = model.getHeight() - control.getDevice_visualization_radius()-d.getY();
 			}
+			//update the dragged position, if the offset changed
 			dragged_x =dragged.getX() + x_offset;
 			dragged_y =dragged.getY() + y_offset;
 			break;