浏览代码

Combine funktioniert

Kolja Strohm 6 年之前
父节点
当前提交
b216145846
共有 2 个文件被更改,包括 98 次插入8 次删除
  1. 97 7
      src/bk/Combine.java
  2. 1 1
      src/main/Main.java

+ 97 - 7
src/bk/Combine.java

@@ -1,8 +1,12 @@
 package bk;
 
 import java.awt.Color;
+import java.util.ArrayList;
+import java.util.Collections;
 
 import animation.AlgorithmStage;
+import animation.BackwardAction;
+import bk.ExtremalLayoutCalc.LayoutType;
 import graph.LayeredGraphNode;
 import view.MainView;
 
@@ -15,24 +19,110 @@ public class Combine implements AlgorithmStage {
 
 	LayeredGraphNode graph;
 	
+	private enum State
+	{
+		ALIGN,
+		SET_COORDINATES
+	}
+	
+	private State state;
+	private int btlOffset;
+	private int btrOffset;
+	private int tblOffset;
+	private int tbrOffset;
+	private int vIndex;
+	ArrayList< BackwardAction > actions;
+	
 	public Combine( LayeredGraphNode graph )
 	{
 		this.graph = graph;
+		state = State.ALIGN;
+		vIndex = 0;
+		actions = new ArrayList<>();
 	}
 	
 	@Override
 	public StageStatus forwardStep() {
-		MainView.frame.setSize( MainView.frame.getWidth() + 1, MainView.frame.getHeight() );
-		MainView.frame.setSize( MainView.frame.getWidth() - 1, MainView.frame.getHeight() );
-		
-		graph.setColor( Color.BLACK, null );
-		return null;
+		if( state == State.ALIGN )
+		{
+			int tblw = (int)graph.getWidth( LayoutType.TOP_BOTTOM_LEFT );
+			int tbrw = (int)graph.getWidth( LayoutType.TOP_BOTTOM_RIGHT );
+			int btlw = (int)graph.getWidth( LayoutType.BOTTOM_TOP_LEFT );
+			int btrw = (int)graph.getWidth( LayoutType.BOTTOM_TOP_RIGHT );
+			LayoutType minLayout = LayoutType.TOP_BOTTOM_LEFT;
+			int minWidth = tblw;
+			if( tbrw < minWidth )
+			{
+				minWidth = tbrw;
+				minLayout = LayoutType.TOP_BOTTOM_RIGHT;
+			}
+			if( btlw < minWidth )
+			{
+				minWidth = btlw;
+				minLayout = LayoutType.BOTTOM_TOP_LEFT;
+			}
+			if( btrw < minWidth )
+			{
+				minWidth = btrw;
+				minLayout = LayoutType.BOTTOM_TOP_RIGHT;
+			}
+			int minX = calcMinX( minLayout );
+			btlOffset = minX - calcMinX( LayoutType.BOTTOM_TOP_LEFT );
+			tblOffset = minX - calcMinX( LayoutType.TOP_BOTTOM_LEFT );
+			btrOffset = minWidth - btrw;
+			tbrOffset = minWidth - tbrw;
+			graph.setColor( Color.BLACK, null );
+			MainView.frame.setSize( MainView.frame.getWidth() + 1, MainView.frame.getHeight() );
+			MainView.frame.setSize( MainView.frame.getWidth() - 1, MainView.frame.getHeight() );
+			actions.add( () -> {
+				state = State.ALIGN;
+				graph.setColor( null, null );
+				MainView.frame.setSize( MainView.frame.getWidth() + 1, MainView.frame.getHeight() );
+				MainView.frame.setSize( MainView.frame.getWidth() - 1, MainView.frame.getHeight() );
+			});
+			state = State.SET_COORDINATES;
+		}
+		else
+		{
+			if( vIndex >= graph.getContainedNodes().size() )
+				return StageStatus.FINISHED;
+			LayeredGraphNode current = graph.getContainedNodes().get( vIndex );
+			current.setSelected( null );
+			ArrayList< Integer > positions = new ArrayList<>();
+			positions.add( (int)current.getX( LayoutType.TOP_BOTTOM_LEFT ) + tblOffset );
+			positions.add( (int)current.getX( LayoutType.TOP_BOTTOM_RIGHT ) + tbrOffset );
+			positions.add( (int)current.getX( LayoutType.BOTTOM_TOP_LEFT ) + btlOffset );
+			positions.add( (int)current.getX( LayoutType.BOTTOM_TOP_RIGHT ) + btrOffset );
+			Collections.sort( positions );
+			int oldX = (int)current.getX( LayoutType.COMBINED );
+			current.setX( (positions.get( 1 ) + positions.get( 2 )) / 2, true, LayoutType.COMBINED );
+			actions.add( () -> {
+				vIndex--;
+				current.setX( oldX, true, LayoutType.COMBINED );
+				current.setSelected( null );
+			});
+			vIndex++;
+		}		
+		return StageStatus.UNFINISHED;
+	}
+	
+	private int calcMinX(  LayoutType layout )
+	{
+		int minX = 0;
+		if( graph.getContainedNodes().size() > 0 )
+			minX = (int)graph.getContainedNodes().get( 0 ).getX( layout );
+		for( LayeredGraphNode n : graph.getContainedNodes() )
+			minX = Math.min( minX, (int)n.getX( layout ) );
+		return minX;
 	}
 
 	@Override
 	public StageStatus backwardStep() {
-		// TODO Auto-generated method stub
-		return null;
+		if( actions.size() == 0 )
+			return StageStatus.FINISHED;
+		actions.get( 0 ).reverse();
+		actions.remove( 0 );
+		return StageStatus.UNFINISHED;
 	}
 
 }

+ 1 - 1
src/main/Main.java

@@ -18,7 +18,7 @@ public class Main {
      * @param args the command line arguments, currently not in use
      */
 	public static void main(String[] args) {
-		Reader r = new Reader( "test3.json" );
+		Reader r = new Reader( "papergraph.json" );
 		LayeredGraphNode graph = r.readInputGraph();
 	    //RandomGraphGenerator r = new RandomGraphGenerator( 0.1, 0.2, 5,5, 5, 5, 1 );
 	    //LayeredGraphNode graph = r.createRandomNode( null, 0 );