
package tmcm.xLogicCircuits;

import java.awt.*;
import java.util.Vector;



abstract class IONub extends CircuitItem {
   static final int INPUT = 0, OUTPUT = 1, TACK = 2;
   final static Color lineDestinationColor = new Color(0,150, 0);
   final static Color lineSourceColor = new Color(150,0,150);
   int kind;
   int connect_x, connect_y;
   Line source;
   boolean changed; // used only in circuitCanvas.run() to determine whether to draw an output nub
   Vector destination = new Vector();  // vector of lines
   void drawWithLines(Graphics g) { 
      draw(g);
      if (source != null && kind != INPUT)  // don't draw source line for inputs, because the line is in the containing circuit
         source.draw(g);
      if (kind != OUTPUT)  // for outputs, destination lines are in containing circuit
         for (int i = 0; i < destination.size(); i++)
            ((Line)destination.elementAt(i)).draw(g);
   }
   void selectConnectedLines(boolean select) { 
      if (source != null)
         source.selected = select;
      for (int i = 0; i < destination.size(); i++)
         ((Line)destination.elementAt(i)).selected = select;
   }
   Rectangle getCopyOfBoundingBox(boolean addInLines) { 
      FloatRect r = new FloatRect(boundingBox.x,boundingBox.y,boundingBox.width,boundingBox.height);
      if (addInLines) {
         if (source != null)
            r.add(source.boundingBox);
         for (int i = 0; i < destination.size(); i++)
            r.add(((Line)destination.elementAt(i)).boundingBox);
      }
      r.grow(3,3);
      return r.getIntRect();
   }
   IONub getLineSource(int x, int y) {
      if (kind == OUTPUT)
         return null;
      if (x < boundingBox.x - 2 || x > boundingBox.x + boundingBox.width + 2 ||
             y < boundingBox.y - 2|| y > boundingBox.y + boundingBox.height + 2)
         return null;
      return this;
   }
   IONub getLineDestination(int x, int y) {
      if (kind == INPUT || source != null)
         return null;
      if (x < boundingBox.x - 2 || x > boundingBox.x + boundingBox.width + 2 ||
             y < boundingBox.y - 2|| y > boundingBox.y + boundingBox.height + 2)
         return null;
      return this;
   }
}


