From 151262cfc79c229fe940d882acef28967331bb81 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Anian=20B=C3=BChler?=
 <anian.buehler@reutlingen-university.de>
Date: Wed, 2 Feb 2022 12:17:03 +0100
Subject: [PATCH] added infix-command-blocks

---
 .../blocks/codeblocks/InfixBlockShape.java    | 102 +++++++++++-------
 .../edu/mit/blocks/renderable/NameLabel.java  |   6 +-
 .../blocks/renderable/RenderableBlock.java    |  37 +++++++
 3 files changed, 104 insertions(+), 41 deletions(-)

diff --git a/src/main/java/edu/mit/blocks/codeblocks/InfixBlockShape.java b/src/main/java/edu/mit/blocks/codeblocks/InfixBlockShape.java
index adc4d3c..1846514 100644
--- a/src/main/java/edu/mit/blocks/codeblocks/InfixBlockShape.java
+++ b/src/main/java/edu/mit/blocks/codeblocks/InfixBlockShape.java
@@ -7,7 +7,6 @@ import java.awt.geom.Point2D;
 import java.util.ArrayList;
 
 import edu.mit.blocks.renderable.RenderableBlock;
-
 import edu.mit.blocks.codeblocks.rendering.BlockShapeUtil;
 
 /**
@@ -47,7 +46,13 @@ public class InfixBlockShape extends BlockShape {
         if (block.hasAfterConnector() && !rb.isCollapsed()) {
             //control connector if necessary
             // Trying left-aligned ports
-            Point2D p = BCS.addControlConnectorShape(gpBottom, (float) COMMAND_PORT_OFFSET + blockCornerRadius, true);
+        	
+        	//added by letsgoING
+    		//TODO: TEST
+        	BlockConnector socket = block.getSocketAt(0);
+        	int socketLabelOffset =  (int) Math.max(socket.getLabel().length(), COMMAND_PORT_OFFSET);
+      
+            Point2D p = BCS.addControlConnectorShape(gpBottom, socketLabelOffset + blockCornerRadius, true);
             rb.updateSocketPoint(block.getAfterConnector(), p);
         }
 
@@ -55,20 +60,32 @@ public class InfixBlockShape extends BlockShape {
         //for each socket in the iterator
         int socketCounter = 0; //need to use this to determine which socket we're on
         for (BlockConnector curSocket : block.getSockets()) {
-     
+        	
             //if bottom socket
-            if (curSocket.getPositionType() == BlockConnector.PositionType.BOTTOM) {
-
+            if (curSocket.getPositionType() == BlockConnector.PositionType.BOTTOM) { 
                 //move away from bottom left corner
-                if (socketCounter > 0) {
-                    gpBottom.lineTo(
-                            (float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_MIDDLE_SPACER,
-                            (float) gpBottom.getCurrentPoint().getY());
-                }else {
-                    gpBottom.lineTo(
-                            (float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_SIDE_SPACER,
-                            (float) gpBottom.getCurrentPoint().getY());
-                }
+            	if(!block.isCommandBlock()) {
+	                if (socketCounter > 0) {
+	                    gpBottom.lineTo(
+	                            (float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_MIDDLE_SPACER,
+	                            (float) gpBottom.getCurrentPoint().getY());
+	                }else {
+	                    gpBottom.lineTo(
+	                            (float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_SIDE_SPACER,
+	                            (float) gpBottom.getCurrentPoint().getY());
+	                }
+            	} else { //added by letsgoING
+            		//TODO: TEST
+            		if (socketCounter > 0) {
+	                    gpBottom.lineTo(
+	                            (float) gpBottom.getCurrentPoint().getX() + rb.getSocketLabelWidth(socketCounter),
+	                            (float) gpBottom.getCurrentPoint().getY());
+            		}else {
+	                    gpBottom.lineTo(
+	                            (float) gpBottom.getCurrentPoint().getX() + rb.getBlockLabelWidth() + rb.getSocketLabelWidth(socketCounter),
+	                            (float) gpBottom.getCurrentPoint().getY());
+                    }
+            	}
                
 
                 //move down so bevel doesn't screw up from connecting infinitely sharp corner
@@ -150,16 +167,22 @@ public class InfixBlockShape extends BlockShape {
                 //System.out.println("gpbottom starting point: "+gpBottom.getCurrentPoint());
 
                 //// draw RIGHT to create divider ////
-                if (socketCounter < block.getNumSockets() - 1) {
+                if(!block.isCommandBlock()) {
+		           if (socketCounter < block.getNumSockets() - 1) {
+	                    gpBottom.lineTo( //need to add the width of the block label.  warning: this assumes that there is only one block label
+	                            (float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_MIDDLE_SPACER + rb.accomodateLabelsWidth(),
+	                            (float) gpBottom.getCurrentPoint().getY());
+	                } else {
+	                	gpBottom.lineTo( //need to add the width of the block label.  warning: this assumes that there is only one block label
+	                            (float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_SIDE_SPACER,
+	                            (float) gpBottom.getCurrentPoint().getY());
+	                } 
+                }else {
                     gpBottom.lineTo( //need to add the width of the block label.  warning: this assumes that there is only one block label
-                            (float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_MIDDLE_SPACER + rb.accomodateLabelsWidth(),
-                            (float) gpBottom.getCurrentPoint().getY());
-                } else {
-                    gpBottom.lineTo(
-                            (float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_SIDE_SPACER,
+                            (float) gpBottom.getCurrentPoint().getX() + BOTTOM_SOCKET_MIDDLE_SPACER +5 ,
                             (float) gpBottom.getCurrentPoint().getY());
                 }
-
+	            
                 socketCounter++;
             }
         }
@@ -241,46 +264,47 @@ public class InfixBlockShape extends BlockShape {
 
         int width = super.determineBlockWidth();
         
+        int socketCounter = 0;
         int bottomSocketWidth = 0;
         
-        
+        if(block.isCommandBlock()) {
+        	bottomSocketWidth += COMMAND_PORT_OFFSET;
+        }
         for (BlockConnector socket : block.getSockets()) {
             if (socket.getPositionType() == BlockConnector.PositionType.BOTTOM) {
                 if (socket.getBlockID() == Block.NULL) {
                     //3 socket spacers = left of socket, between connectors, right of socket
                     bottomSocketWidth += BOTTOM_SOCKET_SIDE_SPACER;
-                    if(block.isCommandBlock()) {//added by letsgoING for new setVariableBlock test
-                    	//TODO get correct size of socket
-                    	bottomSocketWidth +=  rb.accomodateLabelsWidth();
-                    }
                 } else { //a block is connected to socket
                     //TODO get their assigned width from rb
                     if (rb.getSocketSpaceDimension(socket) != null) {
                         bottomSocketWidth += rb.getSocketSpaceDimension(socket).width;
-                        if(block.isCommandBlock()) {//added by letsgoING for new setVariableBlock test
-                        	//TODO get correct size of socket
-	                    	bottomSocketWidth += rb.accomodateLabelsWidth();
-	                    }
-                    }
+                    }      
                     bottomSocketWidth -= BlockConnectorShape.NORMAL_DATA_PLUG_WIDTH;
                     // if it's a mirror plug, subtract for the other side, too.
                     if (rb.getWorkspace().getEnv().getBlock(socket.getBlockID()).getPlug().getPositionType() == BlockConnector.PositionType.MIRROR) {
-                        bottomSocketWidth -= BlockConnectorShape.NORMAL_DATA_PLUG_WIDTH;
+                       bottomSocketWidth -= BlockConnectorShape.NORMAL_DATA_PLUG_WIDTH;
                     }
                 }
+                
+                bottomSocketWidth += rb.getSocketLabelWidth(socketCounter) + 1;
             }
+            bottomSocketWidth += BOTTOM_SOCKET_MIDDLE_SPACER;
+            bottomSocketWidth += BOTTOM_SOCKET_SIDE_SPACER;
+            
+            socketCounter++;
         }
 
+        //bottomSocketWidth += 2 * BOTTOM_SOCKET_MIDDLE_SPACER;  //TODO need to decide for a size of the middle spacer and how to place them
+        //bottomSocketWidth += 2 * BOTTOM_SOCKET_SIDE_SPACER;
 
-
-        bottomSocketWidth += 2 * BOTTOM_SOCKET_MIDDLE_SPACER;  //TODO need to decide for a size of the middle spacer and how to place them
-        bottomSocketWidth += 2 * BOTTOM_SOCKET_SIDE_SPACER;
-
-        if (bottomSocketWidth > width) {
+      /*  if (bottomSocketWidth > width) {
             return (bottomSocketWidth + rb.accomodateLabelsWidth());
         }
-
-        width += bottomSocketWidth;
+        */
+        
+        //width += bottomSocketWidth;
+        width = bottomSocketWidth + rb.getBlockLabelWidth();// + rb.accomodateLabelsWidth();
 
         //make sure its even
         if (width % 2 == 1) {
diff --git a/src/main/java/edu/mit/blocks/renderable/NameLabel.java b/src/main/java/edu/mit/blocks/renderable/NameLabel.java
index 6b4f14b..7b7ecae 100644
--- a/src/main/java/edu/mit/blocks/renderable/NameLabel.java
+++ b/src/main/java/edu/mit/blocks/renderable/NameLabel.java
@@ -35,13 +35,15 @@ class NameLabel extends BlockLabel {
             }
             if (rb.getBlock().isInfix()) {
             	//added by letsgoING for new setVariableBlock test
-            	if(rb.getBlock().isCommandBlock()) {
+            /*	if(rb.getBlock().isCommandBlock()) {
             		if (!rb.getBlock().getSocketAt(0).hasBlock()) {
-                        x += 60;
+                        x += 0;
                     } else {
                         x += rb.getSocketSpaceDimension(rb.getBlock().getSocketAt(0)).width+30;
                     }
             	}else {
+            	*/
+            	if(!rb.getBlock().isCommandBlock()) {
 	                if (!rb.getBlock().getSocketAt(0).hasBlock()) {
 	                    x += 30;
 	                } else {
diff --git a/src/main/java/edu/mit/blocks/renderable/RenderableBlock.java b/src/main/java/edu/mit/blocks/renderable/RenderableBlock.java
index bba7b58..4213015 100644
--- a/src/main/java/edu/mit/blocks/renderable/RenderableBlock.java
+++ b/src/main/java/edu/mit/blocks/renderable/RenderableBlock.java
@@ -693,6 +693,43 @@ public class RenderableBlock extends JComponent implements SearchableElement,
 		}
 		return width;
 	}
+	
+	/**
+	 * Determine the width necessary to accommodate for placed block labels. Used to
+	 * determine the position of sockets
+	 * 
+	 * @return int pixel width needed for the labels
+	 */
+	public int getBlockLabelWidth() {
+		int width = 0;
+		
+		if (getBlock().hasPageLabel()) {
+			width += Math.max(blockLabel.getAbstractWidth(),
+					pageLabel.getAbstractWidth());
+			width += getControlLabelsWidth();
+		} else {
+			width += blockLabel.getAbstractWidth();
+			width += getControlLabelsWidth() + 4;
+		}
+		return width;
+	}
+	
+	/**
+	 * Determine the width necessary to accommodate for placed socket labels. Used to
+	 * determine the position of sockets.
+	 * by letsgoING
+	 * @return int pixel width needed for the labels
+	 */
+	public int  getSocketLabelWidth(int socketPosition) {
+
+		int width = 0;
+
+		SocketLabel label = socketTags.get(socketPosition).getLabel();
+		if (label != null) {
+			width = label.getAbstractWidth();
+		}
+		return width;
+	}
 
 	/**
 	 * Returns the width of the page label on this block; if page label is not
-- 
GitLab