Browse Source

Ground Functions !!!Delete .config in your USER folder!!!

Tom Troppmann 6 years ago
parent
commit
93045342c5

+ 1 - 0
src/classes/HolonElement.java

@@ -491,6 +491,7 @@ public class HolonElement implements IGraphedElement, GraphEditable{
 				before = after;
 				after = iter.next();
 			}
+			//t to determine how many percentage the graphX is to the next Point needed to calc Bezier
 			//inverseLerp(valueBetween, min, max) (valueBetween - min) / (max - min)
 			// e.g. old.x = 0.4, actual.x = 0.8 and graphX = 0.6 then t is 0.5
 			double t = (after.x -before.x > 0)? (graphX - before.x) / (after.x -before.x) : 0.0;			

+ 42 - 18
src/classes/HolonSwitch.java

@@ -1,8 +1,10 @@
 package classes;
 
 import java.awt.Point;
+import java.awt.geom.Point2D;
 import java.awt.geom.Point2D.Double;
 import java.util.LinkedList;
+import java.util.ListIterator;
 
 import com.google.gson.annotations.Expose;
 import interfaces.GraphEditable;
@@ -23,8 +25,6 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 	 * The class HolonSwitch represents an Object in the system, that has the
 	 * capacity of manipulate the electricity flow. The switch can be manage
 	 * automatically through a graph or direct manually.
-	 * 
-	 * @author Gruppe14
 	 *
 	 */
 	/*
@@ -59,7 +59,7 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 	 */
 	boolean[] activeAt;
 	// Points on the UnitGraph
-	LinkedList<Point> graphPoints = new LinkedList<>();
+	LinkedList<Point2D.Double> graphPoints = new LinkedList<>();
 
 	/**
 	 * Create a new HolonSwitch with the default name ("Switch"), a default
@@ -80,7 +80,7 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 		setAutoState(true);
 		setActiveAt(true);
 		setManualMode(false);
-		setGraphPoints(new LinkedList<Point>());
+		initGraphPoints();
 	}
 
 	/**
@@ -103,9 +103,9 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 		for (int i = 0; i < activeAt.length; i++) {
 			activeAt[i] = copyObj.getState(i);
 		}
-		setGraphPoints(new LinkedList<Point>());
-		for (Point p : copyObj.getGraphPoints()) {
-			this.graphPoints.add(new Point((int) p.getX(), (int) p.getY()));
+		setGraphPoints(new LinkedList<Point2D.Double>());
+		for (Point2D.Double p : copyObj.getGraphPoints()) {
+			this.graphPoints.add(new Point2D.Double(p.getX(),p.getY()));
 		}
 		setManualMode(copyObj.getManualMode());
 	}
@@ -198,21 +198,25 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 	 * 
 	 * @return the Graph Points
 	 */
-	public LinkedList<Point> getGraphPoints() {
+	public LinkedList<Point2D.Double> getGraphPoints() {
 		return graphPoints;
 	}
 
 	/**
 	 * Set the values of the switch in the graph (auto. mode only).
 	 * 
-	 * @param points the Graph points
+	 * @param linkedList the Graph points
 	 */
-	public void setGraphPoints(LinkedList<Point> points) {
-		this.graphPoints = points;
+	public void setGraphPoints(LinkedList<Double> linkedList) {
+		this.graphPoints = linkedList;
+	}
+	//TODO: javadoc
+	private void initGraphPoints()
+	{
+		graphPoints = new LinkedList<Point2D.Double>();
+		graphPoints.add(new Point2D.Double(0.0, 1.0));
+		graphPoints.add(new Point2D.Double(1.0, 1.0));
 	}
-	
-
-
 	/**
 	 * Returns the ManualState.
 	 * 
@@ -286,13 +290,33 @@ public class HolonSwitch extends AbstractCpsObject implements IGraphedElement, G
 
 	@Override
 	public LinkedList<Double> getStateGraph() {
-		// TODO no Double List
-		return null;
+		for(Double p: graphPoints)
+		{
+			System.out.println(p);
+		}
+		return graphPoints;
 	}
 
+	private boolean[] sampleGraph(int sampleLength)
+	{
+		ListIterator<Point2D.Double> iter = this.graphPoints.listIterator();
+		Point.Double before = iter.next();
+		Point.Double after = iter.next();
+		boolean [] activeTriggerPos = new boolean[sampleLength];	
+		for(int i = 0; i<sampleLength ; i++)
+		{
+			double graphX = (double)i / (double) (sampleLength - 1); //from 0.0 to 1.0
+			if(graphX > after.x)
+			{
+				before = after;
+				after = iter.next();
+			}		
+			activeTriggerPos[i] = (before.getY() >= 0.5);
+		}
+		return activeTriggerPos;
+	}
 	@Override
 	public void sampleGraph() {
-		// TODO Auto-generated method stub
-		
+		activeAt = sampleGraph(100);
 	}
 }

+ 17 - 38
src/ui/controller/ClipboardController.java

@@ -255,54 +255,33 @@ public class ClipboardController {
         JsonObject object = jsonElement.getAsJsonObject();
         List<String> keys = load.getKeys(object);
         String p;
-        int mid, x, y;
-        System.out.println("loadcontroller:loadUnitGraph");
-        LinkedList<Point> graphpoint = new LinkedList<>();
+        int mid;
         int sav = 0;
-        // foreach Point in the graph restore it
-        if(type != GRAPHTYPE.TESTELEMENT)
-        {
-	        	for (String k : keys) {
-	            if (!k.equals("ID")) {
-	                p = object.get(k).getAsString();
-	                mid = p.indexOf(':');
-	                x = Integer.parseInt(p.substring(0, mid));
-	                y = Integer.parseInt(p.substring(mid + 1, p.length()));
-	                graphpoint.add(new Point(x, y));
-	            } else
-	                // else its an ID
-	                sav = object.get(k).getAsInt();
-	
-	        	}
-        }
+     	LinkedList<Point2D.Double> graphpointTEST = new LinkedList<>();
+        for (String k : keys) {
+            if (!k.equals("ID")) {
+                p = object.get(k).getAsString();
+                mid = p.indexOf(':');
+                double x1 = Double.parseDouble(p.substring(0, mid));
+                double y1 = Double.parseDouble(p.substring(mid + 1, p.length()));
+                graphpointTEST.add(new Point2D.Double(x1, y1));
+            } else
+                // else its an ID
+                sav = object.get(k).getAsInt();
+
+        }  
+        
        
 
         switch (type) {
             case SWITCH:
                 sav = objIDMap.get(sav);
                 HolonSwitch sw = (HolonSwitch) objDispatch.get(sav);
-                sw.setGraphPoints(graphpoint);
+                sw.setGraphPoints(graphpointTEST);
                 break;
-            case ELEMENT:
-                sav = eleIDMap.get(sav);
-                HolonElement ele = eleDispatch.get(sav);
-                ele.setGraphPoints(graphpoint);
+            case ELEMENT:                
                 break;
             case TESTELEMENT:
-            	LinkedList<Point2D.Double> graphpointTEST = new LinkedList<>();
-                for (String k : keys) {
-                    if (!k.equals("ID")) {
-                        p = object.get(k).getAsString();
-                        mid = p.indexOf(':');
-                        double x1 = Double.parseDouble(p.substring(0, mid));
-                        double y1 = Double.parseDouble(p.substring(mid + 1, p.length()));
-                        graphpointTEST.add(new Point2D.Double(x1, y1));
-                    } else
-                        // else its an ID
-                        sav = object.get(k).getAsInt();
-
-                }  
-                
                 sav = eleIDMap.get(sav);
                 HolonElement ele1 = eleDispatch.get(sav);
                 ele1.setTestGraphPoints(graphpointTEST);

+ 18 - 36
src/ui/controller/LoadController.java

@@ -372,55 +372,37 @@ public class LoadController {
      */
     private void loadUnitGraph(GRAPHTYPE type, JsonElement jsonElement, HashMap<Integer, AbstractCpsObject> objDispatch,
                                HashMap<Integer, HolonElement> eleDispatch) {
-    	//TODO Make UnitGraphChnages RDY
+    	//TODO Make UnitGraph unterscheidung hinfällig!!!!
         JsonObject object = jsonElement.getAsJsonObject();
         List<String> keys = getKeys(object);
         String p;
-        int mid, x, y;
+        int mid;
         int sav = 0;
-        LinkedList<Point> graphpoint = new LinkedList<>();
-        if(type != GRAPHTYPE.TESTELEMENT)
-        {
-	        // foreach Point in the graph restore it
-	        for (String k : keys) {
-	            if (!k.equals("ID")) {
-	                p = object.get(k).getAsString();
-	                mid = p.indexOf(':');
-	                x = Integer.parseInt(p.substring(0, mid));
-	                y = Integer.parseInt(p.substring(mid + 1, p.length()));
-	                graphpoint.add(new Point(x, y));
-	            } else
-	                // else its an ID
-	                sav = object.get(k).getAsInt();
-	        }
-        }
-        
+     	LinkedList<Point2D.Double> graphpointTEST = new LinkedList<>();
+        for (String k : keys) {
+            if (!k.equals("ID")) {
+                p = object.get(k).getAsString();
+                mid = p.indexOf(':');
+                double x1 = Double.parseDouble(p.substring(0, mid));
+                double y1 = Double.parseDouble(p.substring(mid + 1, p.length()));
+                graphpointTEST.add(new Point2D.Double(x1, y1));
+            } else
+                // else its an ID
+                sav = object.get(k).getAsInt();
+
+        }  
         
        
 
         switch (type) {
             case SWITCH:
                 HolonSwitch sw = (HolonSwitch) objDispatch.get(sav);
-                sw.setGraphPoints(graphpoint);
+                sw.setGraphPoints(graphpointTEST);
                 break;
             case ELEMENT:
-                HolonElement ele = eleDispatch.get(sav);
-                ele.setGraphPoints(graphpoint);//TODO?
-                break;
+            	System.out.println("Write me new");
+            	break;
             case TESTELEMENT:
-            	LinkedList<Point2D.Double> graphpointTEST = new LinkedList<>();
-                for (String k : keys) {
-                    if (!k.equals("ID")) {
-                        p = object.get(k).getAsString();
-                        mid = p.indexOf(':');
-                        double x1 = Double.parseDouble(p.substring(0, mid));
-                        double y1 = Double.parseDouble(p.substring(mid + 1, p.length()));
-                        graphpointTEST.add(new Point2D.Double(x1, y1));
-                    } else
-                        // else its an ID
-                        sav = object.get(k).getAsInt();
-
-                }  
                 HolonElement ele1 = eleDispatch.get(sav);
                 ele1.setTestGraphPoints(graphpointTEST);
                 break;

+ 1 - 1
src/ui/controller/SaveController.java

@@ -277,7 +277,7 @@ public class SaveController {
     /**
      * Put the UnitGraphs of Switches or Elements into Json
      */
-    void unitgraphToJson(GRAPHTYPE type, JsonObject file, int id, LinkedList<Point> graph) {
+    void unitgraphToJson(GRAPHTYPE type, JsonObject file, int id, LinkedList<Point2D.Double> graph) {
 
         JsonObject temp = new JsonObject();
         String key = null;

+ 1 - 0
src/ui/view/GUI.java

@@ -3084,6 +3084,7 @@ public class GUI implements CategoryListener {
 	}
 	
 	private void repaintGraphAux(HolonSwitch o){//TODO: 
+		unitGraph.initNewElement(o);
 //		unitGraphLocalPeriod.setText(""+unitGraph.getLocalPeriod());
 //		unitGraphStretchMode.setSelected(unitGraph.isStretching());
 	}

+ 122 - 9
src/ui/view/UnitGraph.java

@@ -90,9 +90,8 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
      */
     public void paintComponent(Graphics g) {
         super.paintComponent(g);
-        
-        //System.out.println("paint");
         Graphics2D g2D = (Graphics2D) g;
+        drawGrid(g2D);
         g2D.setColor(Color.BLACK);
         g2D.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
         g2D.setStroke(new BasicStroke(2));
@@ -109,7 +108,19 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
         g2D.setStroke(new BasicStroke(1));
         drawCurrentIterartionLine(g2D);
     }
-    //TODO -> New Section
+
+	private void drawGrid(Graphics2D g2D) {
+        g2D.setStroke(new BasicStroke(1));
+        g2D.setColor(Color.lightGray);
+        int amountOfLines = 10;
+        int width = widthWithBorder + 2 * border;
+        int height = heightWithBorder;
+        for(int i = 0; i<=amountOfLines; i++)
+        {
+        	int linehieght = (int) (((double)i/ (double) amountOfLines) * (double) height) + border;
+        	g2D.drawLine(0, linehieght, width, linehieght);
+        }
+	}
     
     private void drawCurrentIterartionLine(Graphics2D g)
     {
@@ -132,7 +143,6 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
     private Path2D.Double initBezier(Position start) {
     	//Good Source for basic understanding for Bezier Curves
         //http://www.theappguruz.com/blog/bezier-curve-in-games
-        
     	Path2D.Double path = new Path2D.Double();
     	path.moveTo(start.x, start.y);
 		return path;
@@ -148,7 +158,10 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
     private void drawUnitGraph(Graphics2D g) {
     	switch(actualGraphType) {
 		case boolGraph:
-			drawBoolGraph(g);
+			if(editMode)
+				drawBoolGraphWithEditPosition(g);
+			else
+				drawBoolGraph(g);
 			break;
 		case doubleGraph:
 			if(editMode)
@@ -183,9 +196,83 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
     }
     
     private void drawBoolGraph(Graphics2D g) {
-    	throw new NotImplementedException();
+    	System.out.println("DoImplement");
+    	if(actualGraphPoints.size() <= 1) return;
+    	LinkedList<Position> cornerPoints =  new LinkedList<Position>();
+     	ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
+     	Position actual = actualGraphPoints.getFirst().displayedPosition;
+     	Path2D.Double path = new Path2D.Double();
+     	path.moveTo(actual.x, actual.y);
+     	while (iter.hasNext())
+    	{
+    		Position target = iter.next().displayedPosition;
+    		//BooleanConnection
+    		path.lineTo(target.x, actual.y); //line to corner
+    		cornerPoints.add(new Position(target.x, actual.y)); //save corner
+    		path.lineTo(target.x, target.y); //line to next Point
+    		
+    		actual = target;
+    	}
+     	g.draw(path);
+     	//Draw the Points on the Corner that dont exist in Data but should be visual
+     	g.setColor(dotColor);
+     	for(Position p: cornerPoints)
+     	{
+     		drawDot(g, p);
+     	}
+     	
     }
     
+    private void drawBoolGraphWithEditPosition(Graphics2D g) {
+    	LinkedList<Position> before = new LinkedList<Position>();
+     	LinkedList<Position> after = new LinkedList<Position>();
+     	for(UnitGraphPoint p: actualGraphPoints)
+     	{
+     		if(p.displayedPosition.x < editPosition.x)
+     			before.add(p.displayedPosition);
+     		else
+     			after.add(p.displayedPosition);
+     	}    	
+     	g.setColor(Color.BLACK);
+     	drawBoolGraphFromList(g, before);
+     	g.setColor(Color.BLACK);
+     	drawBoolGraphFromList(g, after);
+     	//EditGraph
+     	LinkedList<Position> middle = new LinkedList<Position>();
+     	if(!before.isEmpty()) middle.add(before.getLast());
+     	middle.add(editPosition);
+    	if(!after.isEmpty()) middle.add(after.getFirst());
+    	
+    	g.setColor(editDotColor);
+    	drawBoolGraphFromList(g, middle);
+     	
+    }
+    
+	private void drawBoolGraphFromList(Graphics2D g, LinkedList<Position> list) {
+		if(list.size() <= 1) return;
+     	ListIterator<Position> iter = list.listIterator();
+      	LinkedList<Position> cornerPoints =  new LinkedList<Position>();
+     	Position actual = list.getFirst();
+     	Path2D.Double path = new Path2D.Double();
+     	path.moveTo(actual.x, actual.y);
+     	while (iter.hasNext())
+    	{
+    		Position target = iter.next();
+    		//BooleanConnection
+    		path.lineTo(target.x, actual.y); //line to corner
+    		cornerPoints.add(new Position(target.x, actual.y)); //save corner
+    		path.lineTo(target.x, target.y); //line to next Point
+    		actual = target;
+    	}
+     	g.draw(path);
+    	g.setColor(dotColor);
+     	for(Position p: cornerPoints)
+     	{
+     		drawDot(g, p);
+     	}
+	}
+    
+    
     private void drawDoubleGraph(Graphics2D g) {
     	if(actualGraphPoints.isEmpty()) throw new IndexOutOfBoundsException("A Graph Without Points is not supportet jet");
     	ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
@@ -271,7 +358,7 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
     	ListIterator<UnitGraphPoint> iter = actualGraphPoints.listIterator();
     	while (iter.hasNext())
     	{
-    		if(mPosition.squareDistance(iter.next().displayedPosition) < clickThreshholdSquared)
+    		if(near(mPosition,iter.next().displayedPosition))
     		{
     			iter.remove();
     			break;
@@ -283,15 +370,29 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
     {
     	UnitGraphPoint first = actualGraphPoints.getFirst();
     	UnitGraphPoint last = actualGraphPoints.getLast();
-    	if((mPosition.squareDistance(first.displayedPosition) < clickThreshholdSquared)) editPoint = pointType.StartPoint;
-    	else if(mPosition.squareDistance(last.displayedPosition) < clickThreshholdSquared)  editPoint = pointType.EndPoint;
+    	if(near(mPosition, first.displayedPosition)) editPoint = pointType.StartPoint;
+    	else if(near(mPosition, last.displayedPosition))  editPoint = pointType.EndPoint;
     	else editPoint = pointType.Normal;
     }
+
+	private boolean near(Position actual, Position target) {
+		switch(actualGraphType)
+		{
+		case boolGraph: //Distance only with X 
+			int xDis = target.x - actual.x;
+			return xDis * xDis  < clickThreshholdSquared;
+		case doubleGraph:
+			return actual.squareDistance(target) < clickThreshholdSquared;
+		default:
+			return false;
+		}	
+	}
     
     private void updateEditPointPosition(Position newPosition) {
     	//make it in the bounds of the UnitGraph no Point out of the Border
     	Position currentPosition = setInBounds(newPosition);
     	if(editPoint != pointType.Normal) attachToBorder(currentPosition);
+    	if(actualGraphType == Graphtype.boolGraph) snapBoolean(currentPosition);
     	this.editPosition = currentPosition;
     }
     
@@ -301,6 +402,18 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 		return p;
 	}
     
+	
+	private Position snapBoolean(Position p)
+	{
+		if (p.y < border + heightWithBorder / 2) {
+			p.y = border;
+		} else {
+			p.y = border + heightWithBorder;
+		}
+		return p;
+	}
+	
+	
 	private Point.Double getBezierPoint(double t, Point.Double p0, Point.Double p1,Point.Double p2,Point.Double p3) {
     	/*
     	 * Calculate Beziér: