Bläddra i källkod

Enables Drag&Drop Replacements by CategoryObjects

Andreas T. Meyer-Berg 6 år sedan
förälder
incheckning
f661317e33

+ 43 - 0
src/ui/controller/CanvasController.java

@@ -46,6 +46,49 @@ public class CanvasController {
 	public void addObject(AbstractCpsObject object) {
 		model.getCvsObjIdx().put(object.getId(), model.getObjectsOnCanvas().size());
 		model.getObjectsOnCanvas().add(object);
+		/**
+		 * check if we should drag & drop replace
+		 */
+		
+		/** x of the dragged Object */
+		int x = object.getPosition().x;
+		
+		/** y of the dragged Object */
+		int y = object.getPosition().y;
+		
+		/** distance treshold for replacement */
+		int treshhold = model.getScale()/4;
+		
+		/** number of Objects that might be replaced (should be 1) */
+		int replaceCounter = 0;
+		
+		/** last object that could be replaced */
+		AbstractCpsObject toBeReplaced = null;
+		
+		/** for each cps on Canvas */
+		for (AbstractCpsObject cps : model.getObjectsOnCanvas()){
+			
+			/** same object -> ignore */
+			if(cps == object)continue;
+			
+			/** x of object that might get replaced */
+			int c_x = cps.getPosition().x;
+			
+			/** y of object that might get replaced */
+			int c_y = cps.getPosition().y;
+			
+			/** if near enough */
+			if(Math.abs(x-c_x)<treshhold && Math.abs(y-c_y)<treshhold){
+				replaceCounter++;
+				toBeReplaced = cps;
+			}
+		}
+		/** if replacement of exactly one object possible */
+		if(replaceCounter == 1 && toBeReplaced != null){
+			replaceObjectOnCanvas(toBeReplaced, object);
+		}
+		
+		
 		notifyObjListeners();
 	}
 

+ 42 - 0
src/ui/controller/NodeController.java

@@ -444,6 +444,48 @@ class NodeController {
         object.setSav("" + upperNode.getId());
 		upperNode.getNodesIdx().put(object.getId(), upperNode.getNodes().size());
 		upperNode.getNodes().add(object);
+		
+		/**
+		 * check if we should drag & drop replace
+		 */
+		
+		/** x of the dragged Object */
+		int x = object.getPosition().x;
+		
+		/** y of the dragged Object */
+		int y = object.getPosition().y;
+		
+		/** distance threshold for replacement */
+		int treshhold = model.getScale()/4;
+		
+		/** number of Objects that might be replaced (should be 1) */
+		int replaceCounter = 0;
+		
+		/** last object that could be replaced */
+		AbstractCpsObject toBeReplaced = null;
+		
+		/** for each cps on Canvas */
+		for (AbstractCpsObject cps : upperNode.getNodes()){
+			
+			/** same object -> ignore */
+			if(cps == object)continue;
+			
+			/** x of object that might get replaced */
+			int c_x = cps.getPosition().x;
+			
+			/** y of object that might get replaced */
+			int c_y = cps.getPosition().y;
+			
+			/** if near enough */
+			if(Math.abs(x-c_x)<treshhold && Math.abs(y-c_y)<treshhold){
+				replaceCounter++;
+				toBeReplaced = cps;
+			}
+		}
+		/** if replacement of exactly one object possible */
+		if(replaceCounter == 1 && toBeReplaced != null){
+			replaceObjectInUpperNode(toBeReplaced, object, upperNode);
+		}
 	}
 
 	/**

+ 3 - 3
src/ui/controller/SimulationManager.java

@@ -32,8 +32,8 @@ public class SimulationManager {
 
 	/**
 	 * One Element of each HolonObject will be powered first, starting with the
-	 * smallest Demand. If alle HolonObjects have an active Element, the
-	 * simulation will try to fully suplly as many HolonObjects as possible.
+	 * smallest Demand. If ale HolonObjects have an active Element, the
+	 * simulation will try to fully supply as many HolonObjects as possible.
 	 */
 	public static final short fairnessMininumDemandFirst = 0;
 
@@ -117,7 +117,7 @@ public class SimulationManager {
 			 */
 			ArrayList<HolonObject> notSuppliedList = new ArrayList<HolonObject>();
 			/**
-			 * number of HolonObjects that need to be supplied
+			 * Number of HolonObjects that need to be supplied
 			 */
 			long numberOfConsumers = singleSubNet.getObjects().stream()
 			.filter(hl -> (hl.getState() != HolonObject.NO_ENERGY