Exemple #1
0
        /// <summary>
        /// Updates the TextGeometries to display the new Names
        /// </summary>
        /// <param name="oldNames"></param>
        /// <param name="newNames"></param>
        public void UpdateLabel(Dictionary <string, string> newNameDict)
        {
            // Update our geometries
            foreach (string key in newNameDict.Keys)
            {
                DrawingGroup.Children.Remove(labels[key]);

                // Update the meshDict
                List <int> value;
                meshDict.TryGetValue(key, out value);
                meshDict.Remove(key);
                meshDict.Add(newNameDict[key], value);

                if (debug)
                {
                    Console.WriteLine("Key removed is " + key);
                }

                CircuitElement elem = circuitElements[key];

                bool input = (elem.Type == "Input");

                DrawLabel(elem, input);
                labels.Remove(key);
            }
            // Update Image
            Draw(DrawingGroup);
        }
Exemple #2
0
        /// <summary>
        /// Writes the Circuit to the file.
        /// </summary>
        public void writeCircuit()
        {
            writer.WriteStartElement("circuit");
            writer.WriteAttributeString("name", project.UniqueIdentifier);

            writer.WriteStartElement("a");
            writer.WriteAttributeString("name", project.UniqueIdentifier);
            writer.WriteEndElement();

            foreach (var shapeAndElement in circuit.ShapesToElements)
            {
                CircuitElement element = shapeAndElement.Value;
                if (element is INPUT)
                {
                    addInput((INPUT)element);
                }
                else if (element is OUTPUT)
                {
                    addOutput((OUTPUT)element);
                }
                else if (element is Gate)
                {
                    addGate((Gate)element);
                }
                else
                {
                    throw new Exception("invalid type");
                }
                writeWiresIntoElement(element);
            }
            writer.WriteEndElement();
        }
    public void Assembling_Draft()
    {
        SendDestroyMessage();
        GameObject elem_prefab = Resources.Load("Elements/DraftElement") as GameObject;

        Transform Plate = Instantiate((Resources.Load("Elements/DraftPlate") as GameObject).transform,
                                      new Vector3(PP.Width / 2, -0.5f, PP.Height / 2), Quaternion.identity) as Transform;

        NewPlate(Plate);

        Transform Elem;

        for (int i = 0; i < DataStorage.N; i++)
        {
            CircuitElement t = DataStorage.cm[i];
            Elem = Instantiate(elem_prefab.transform) as Transform;

            Elem.Find("Scale").transform.localScale = new Vector3(t.Width - 3, t.Height - 3, 1);

            Elem.parent = Rotator;

            Elem.localPosition = new Vector3(t.x + t.Width / 2, t.y + t.Height / 2, 0);

            Elem.localEulerAngles = Vector3.zero;

            Elem.gameObject.name = i.ToString();
            Elem.Find("Label").GetComponent <TextMesh>().text = DataStorage.cm[i].Name;
        }

        HiResScreenShots.TakeHiResShot(new Vector3(PP.Width / 2, PP.Height / 2));
        SaveButton.interactable = false;
    }
Exemple #4
0
 /// <summary>
 /// Return the width of the CircuitElement. Necessary for calculating location of inputs
 /// relative to the gate.
 /// </summary>
 private int Width(CircuitElement element)
 {
     if (element is INPUT || element is OUTPUT)
     {
         return(20);
     }
     else if (element is SubCircuit)
     {
         return(30);
     }
     else if (element is NOT || element is NOTBUBBLE)
     {
         return(30);
     }
     else if (element is AND || element is OR)
     {
         return(50);
     }
     else if (element is NAND || element is NOR || element is XOR)
     {
         return(60);
     }
     else if (element is XNOR)
     {
         return(70);
     }
     else
     {
         throw new Exception("Unknown element width!");
     }
 }
Exemple #5
0
        /// <summary>
        /// Updates our meshes drawn and our meshDictionary
        /// </summary>
        /// <param name="mesh"></param>
        /// <param name="child"></param>
        private void UpdateMeshes(List <GeometryDrawing> mesh, CircuitElement elem)
        {
            Sketch.Shape theShape = getShapeByElement(elem);

            // ID number of this mesh
            int index = meshesDrawn.Keys.Count;

            // Update our parent meshes
            foreach (var outputs in elem.Outputs.Values)
            {
                foreach (CircuitElement output in outputs.Keys)
                {
                    Sketch.Shape parentShape = getShapeByElement(output);

                    if (!meshDict.ContainsKey(parentShape))
                    {
                        meshDict[parentShape] = new List <int>();
                    }
                    meshDict[parentShape].Add(index);
                }
            }

            if (!meshDict.ContainsKey(theShape))
            {
                meshDict[theShape] = new List <int>();
            }
            meshDict[theShape].Add(index);

            // Add to meshes drawn
            meshesDrawn[index] = mesh;
        }
Exemple #6
0
        /// <summary>
        /// Traverses back to the inputs to see how many gates precede this gate (worst case), not currently used
        /// </summary>
        /// <param name="gate"></param>
        /// <returns></returns>
        private int FindGateGeneration(CircuitElement gate, string origGate)
        {
            int maxGateGen = 0;

            /*
             * foreach (CircuitElement elem in gate.Inputs.Keys)
             * {
             *  // Check to make sure we have not entered a loop
             *  if (elem.Name == origGate)
             *      return -1;
             *
             *  if (elem.Type == "Gate")// && ((Gate)elem).GateType != "NOTBUBBLE")
             *  {
             *      int gen = FindGateGeneration((Gate)elem, origGate);
             *      if (gen == -1)
             *      {
             *          // Do nothing
             *      }
             *      else if (gen + 1 > maxGateGen)
             *          maxGateGen = gen + 1;
             *  }
             * }
             */
            return(maxGateGen);
        }
Exemple #7
0
 public void DisconnectFromLeft(GameObject other)
 {
     leftSide = null;
     if (rightSide == null || leftSide == null)
     {
         isLocked = false;
     }
 }
Exemple #8
0
    public void ConnectElement(GameObject edge)
    {
        CircuitElement connectEle = edge.GetComponentInParent <CircuitElement>();

        ItemList.AddLast(connectEle);
        edge.GetComponent <EdgeHandler>().ConnectToParentNonRec(gameObject);

        isLocked = true;
    }
    /// <summary>
    /// 获取引擎运行时数据
    /// </summary>
    public static void ResetCircuitRunData(int ObjID)
    {
        CircuitElement cir = GetCircuit(ObjID);

        if (cir != null)
        {
            cir.reset();
        }
    }
Exemple #10
0
    /// <summary>
    /// 构建电路
    /// </summary>
    public void JionCircuit(ref Circuit sim)
    {
        if (sim == null)
        {
            return;
        }
        foreach (EleLine line in m_linkEleLine)
        {
            if (line == null || line.ConnectLink != true)
            {
                continue;
            }
            NDCircuitLeap other = line.GetOtherElementLeap(this);
            if (other == null || other.m_Parent == null)
            {
                continue;
            }

            CircuitElement myCircuit    = LabObjectDataFactory.GetCircuit(m_Parent.LabObjID);
            CircuitElement OtherCircuit = LabObjectDataFactory.GetCircuit(other.m_Parent.LabObjID);


            if (myCircuit == null || OtherCircuit == null)
            {
                return;
            }

            if (m_Type == ElementLeapType.leadOut)
            {
                if (other.m_Type == ElementLeapType.leadIn)
                {
                    sim.Connect(myCircuit.leadOut, OtherCircuit.leadIn);
                    Debug.Log("[" + m_Parent.LabObjID + "]" + "[leadOut]" + "------->" + "[" + other.m_Parent.LabObjID + "]" + "[leadIn]");
                }
                else
                {
                    sim.Connect(myCircuit.leadOut, OtherCircuit.leadOut);
                    Debug.Log("[" + m_Parent.LabObjID + "]" + "[leadOut]" + "------->" + "[" + other.m_Parent.LabObjID + "]" + "[leadOut]");
                }
            }
            else
            {
                if (other.m_Type == ElementLeapType.leadIn)
                {
                    sim.Connect(myCircuit.leadIn, OtherCircuit.leadIn);
                    Debug.Log("[" + m_Parent.LabObjID + "]" + "[leadIn]" + "------->" + "[" + other.m_Parent.LabObjID + "]" + "[leadIn]");
                }
                else
                {
                    sim.Connect(myCircuit.leadIn, OtherCircuit.leadOut);
                    Debug.Log("[" + m_Parent.LabObjID + "]" + "[leadIn]" + "------->" + "[" + other.m_Parent.LabObjID + "]" + "[leadOut]");
                }
            }
        }
    }
Exemple #11
0
 /// <summary>
 /// Get the location of the output port for the given element and given output index.
 /// </summary>
 private Point getOutpoint(CircuitElement element, int outputNo)
 {
     if (outputNo == 0)
     {
         return(getLocation(element)); // In Logisim, the first output's location is the gate's location.
     }
     else
     {
         return(getOutpoint(element, outputNo - 1) + 10 * inToIn(element.Angle));
     }
 }
Exemple #12
0
 private Sketch.Shape getShapeByElement(CircuitElement element)
 {
     foreach (var shapeAndElement in circuitElements)
     {
         if (shapeAndElement.Value == element)
         {
             return(shapeAndElement.Key);
         }
     }
     throw new Exception("could not find Shape for given CircuitElement!");
 }
Exemple #13
0
    /// <summary>
    /// 获取引擎运行时数据
    /// </summary>
    public static void GetCircuitRunData(int ObjID, ref float runCurrent, ref float runVoltage, ref float runPower)
    {
        CircuitElement cir = GetCircuit(ObjID);

        if (cir != null)
        {
            cir.calculateCurrent();
            runCurrent = -1 * (float)cir.getCurrent();
            runVoltage = -1 * (float)cir.getVoltageDelta();
            runPower   = runCurrent * runVoltage;
        }
    }
Exemple #14
0
    public void DisconnectElement(GameObject edge)
    {
        CircuitElement connectEle = edge.GetComponentInParent <CircuitElement>();

        ItemList.Remove(connectEle);
        //Disconnect other element

        if (ItemList.Count == 0)
        {
            isLocked = false;
        }
    }
Exemple #15
0
 public void DisconnectFrom()
 {
     if (connectedNode == null)
     {
         Debug.LogWarning("Tried to disconnect null element from earthling\nDisconnect function error!");
     }
     else
     {
         connectedNode = null;
         isLocked      = false;
     }
 }
Exemple #16
0
        /// <summary>
        /// Writes all the incoming wires for the given element.
        /// </summary>
        private void writeWiresIntoElement(CircuitElement element)
        {
            foreach (var inputIndexAndOutputPort in element.InputPorts)
            {
                var   outputPort = inputIndexAndOutputPort.Value;
                Point startPoint = getOutpoint(outputPort.Item1, outputPort.Item2);

                int   inputIndex = inputIndexAndOutputPort.Key;
                Point endPoint   = getInpoint(element, inputIndex);
                addWire(startPoint, endPoint);
            }
        }
Exemple #17
0
    /// <summary>
    /// 设置电灯数据
    /// </summary>
    public static void SetLight(int ObjID, float Volatage, float Power)
    {
        CircuitElement cir = GetCircuit(ObjID);

        if (cir == null)
        {
            return;
        }
        if (cir is Resistor)
        {
            (cir as Resistor).resistance = Volatage * Volatage / Power;
        }
    }
Exemple #18
0
    /// <summary>
    /// 设置电源数据
    /// </summary>
    public static void SetPower(int ObjID, float Volatage)
    {
        CircuitElement cir = GetCircuit(ObjID);

        if (cir == null)
        {
            return;
        }
        if (cir is DCVoltageSource)
        {
            (cir as DCVoltageSource).maxVoltage = Volatage;
        }
    }
Exemple #19
0
        /// <summary>
        /// Updates our meshes drawn and our meshDictionary
        /// </summary>
        /// <param name="mesh"></param>
        /// <param name="child"></param>
        private void UpdateMeshes(List <GeometryDrawing> mesh, CircuitElement elem)
        {
            // NotBubble Case
            if (elem.Type == "Gate" && ((Gate)elem).GateType == LogicDomain.NOTBUBBLE)
            {
                elem = elem.Outputs[0][0];
            }

            // ID number of this mesh
            int index = meshesDrawn.Keys.Count;

            // Update our parent meshes
            foreach (List <CircuitElement> list in elem.Outputs.Values)

            {
                foreach (CircuitElement parent in list)
                {
                    string ParentName = parent.Name;
                    if (parent.Type == "Gate" && ((Gate)parent).GateType == LogicDomain.NOTBUBBLE && parent.Outputs.Count > 0)
                    {
                        ParentName = parent.Outputs[0][0].Name;
                    }

                    if (meshDict.ContainsKey(ParentName))
                    {
                        meshDict[ParentName].Add(index);
                    }
                    else
                    {
                        List <int> newMeshList = new List <int>();
                        newMeshList.Add(index);
                        meshDict[ParentName] = newMeshList;
                    }
                }
            }

            // Update our elem meshes
            if (meshDict.ContainsKey(elem.Name))
            {
                meshDict[elem.Name].Add(index);
            }
            else
            {
                List <int> newMeshList = new List <int>();
                newMeshList.Add(index);
                meshDict[elem.Name] = newMeshList;
            }

            // Add to meshes drawn
            meshesDrawn[index] = mesh;
        }
Exemple #20
0
        /// <summary>
        /// Get the location of the input port for the element and given input index.
        /// </summary>
        private Point getInpoint(CircuitElement element, int inputNo)
        {
            if (element is INPUT)
            {
                throw new Exception("Input shouldn't be getting an input!");
            }

            else if (element is OUTPUT)
            {
                if (inputNo != 0)
                {
                    throw new Exception("Output should have only one input!");
                }
                return(getLocation(element));
            }

            else if (element is NOT || element is NOTBUBBLE)
            {
                if (inputNo != 0)
                {
                    throw new Exception("Not should only have one input!");
                }
                return(getLocation(element) + Width(element) * outToIn(element.Angle));
            }

            else if (inputNo == 0)
            {
                // Calculate the offset between the location of the element and the first input.
                // The offset is based on the difference between the number of inputs and the number of outputs.
                // (the height of the gate depends on max(numInputs, numOutputs), and the inputs and outputs are
                // centred vertically.

                // In LogiSim, every gate (except NOT and subcircuits) has five inputs and one output.
                int numInputs  = 5;
                int numOutputs = 1;
                if (element is SubCircuit)
                {
                    numInputs  = element.InputPorts.Count;
                    numOutputs = element.Outputs.Count;
                }
                int diff = (numInputs - numOutputs) / 2;

                return(getLocation(element) + Width(element) * outToIn(element.Angle) - diff * 10 * inToIn(element.Angle));
            }

            else // inputNo > 0
            {
                return(getInpoint(element, inputNo - 1) + 10 * inToIn(element.Angle));
            }
        }
Exemple #21
0
    /// <summary>
    /// 设置开关的关闭
    /// </summary>
    public static bool GetSwitchState(int ObjID)
    {
        CircuitElement cir = GetCircuit(ObjID);

        if (cir == null)
        {
            return(false);
        }
        if (cir is SwitchSPST)
        {
            return((cir as SwitchSPST).IsOpen);
        }
        return(false);
    }
Exemple #22
0
    /// <summary>
    /// 设置开关的关闭
    /// </summary>
    public static void SetSwitch(int ObjID, bool IsOpen)
    {
        CircuitElement cir = GetCircuit(ObjID);

        if (cir == null)
        {
            return;
        }
        if (cir is SwitchSPST)
        {
            //(cir as SwitchSPST).toggle();
            (cir as SwitchSPST).IsOpen = IsOpen;
        }
    }
Exemple #23
0
 /// <summary>
 /// Updates the TextGeometries to display the new Names
 /// </summary>
 /// <param name="oldNames"></param>
 /// <param name="newNames"></param>
 public void UpdateLabel(Dictionary <Sketch.Shape, string> newNameDict)
 {
     // Update our geometries
     foreach (var oldNameAndNewName in newNameDict)
     {
         Sketch.Shape shape   = oldNameAndNewName.Key;
         string       newName = oldNameAndNewName.Value;
         DrawingGroup.Children.Remove(labels[shape]);
         CircuitElement elem = circuitElements[shape];
         elem.Name = newName;
         DrawLabel(shape, elem, elem.Type == "Input");
     }
     // Update Image
     Draw(DrawingGroup);
 }
Exemple #24
0
 /// <summary>
 ///  添加元器件引擎数据
 /// </summary>
 public static void AddCircuit(int ObjID, LabObjType Type)
 {
     if (g_CircuitData.ContainsKey(ObjID) == true)
     {
         return;
     }
     else
     {
         CircuitElement ele = CreateCircuit(Type);
         if (ele != null)
         {
             g_CircuitData.Add(ObjID, ele);
         }
     }
 }
Exemple #25
0
        /// <summary>
        /// Creates a text label for an input/output that can be modified
        /// </summary>
        /// <param name="elem"></param>
        /// <param name="point"></param>
        private void DrawLabel(CircuitElement elem, bool input)
        {
            if (labels.ContainsKey(elem.Name))
            {
                DrawingGroup.Children.Remove(labels[elem.Name]);
            }

            FormattedText text = new FormattedText(elem.Name, System.Globalization.CultureInfo.GetCultureInfo("en-us"),
                                                   FlowDirection.LeftToRight, new Typeface("Verdana"), elem.Bounds.Height, Brushes.Salmon);
            Geometry        textGeometry = text.BuildGeometry(elem.Bounds.TopLeft);
            GeometryDrawing drawing      = new GeometryDrawing(Brushes.Salmon, new Pen(), textGeometry);

            labels[elem.Name] = drawing;
            elem.CleanBounds  = drawing.Bounds;
            DrawingGroup.Children.Add(drawing);
        }
Exemple #26
0
        /// <summary>
        /// Sets the pen color (based on circuit value) and thickness
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        private Pen GetPen(CircuitElement source, int index)
        {
            Pen pen = new Pen();

            pen.Thickness = WireThickness;
            int value = source.Output[index];

            if (value == 0)
            {
                pen.Brush = Brushes.MidnightBlue;
            }
            else
            {
                pen.Brush = Brushes.SkyBlue;
            }
            return(pen);
        }
Exemple #27
0
        /// <summary>
        /// Sets the pen color (based on circuit value) and thickness
        /// </summary>
        /// <param name="elem"></param>
        /// <returns></returns>
        private Pen GetPen(CircuitElement elem, int index = 0)
        {
            Pen pen = new Pen();

            pen.Thickness = RegularLineThickness;
            int value = elem.Output[index];

            if (value == 0)
            {
                pen.Brush = Brushes.MidnightBlue;
            }
            else
            {
                pen.Brush = Brushes.SkyBlue;
            }
            return(pen);
        }
Exemple #28
0
    public void Select()
    {
        RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

        if (hit.collider == null || (hit.collider != null && !hit.collider.GetComponent <EditableOut>()))
        {
            InputField.text = string.Empty;
        }

        if (hit.collider != null && hit.collider.GetComponent <CircuitElement>())
        {
            selected = hit.collider.GetComponent <CircuitElement>();
            if (selected is EditableOut)
            {
                InputField.text = (selected as EditableOut).value.ToString();
            }
        }
    }
Exemple #29
0
    /// <summary>
    /// 设置电阻数据
    /// </summary>
    public static void SetResistance(int ObjID, float resistance)
    {
        CircuitElement cir = GetCircuit(ObjID);

        if (cir == null)
        {
            Debug.Log("ObjID = " + ObjID + " not found!");
            return;
        }
        Resistor r = cir as Resistor;

        if (r != null)
        {
            r.resistance = resistance;
        }
        else
        {
            Debug.Log("ObjID = " + ObjID + "is not a Resistor!");
        }
    }
Exemple #30
0
    //In progress
    public void DisconnectFromParentNonRec(GameObject otherElement)
    {
        CircuitElement parent = gameObject.GetComponentInParent <CircuitElement>();

        if (parent != null)
        {
            if (LeftEdge)
            {
                parent.DisconnectFromLeft(otherElement);
            }
            else
            {
                parent.DisconnectFromRight(otherElement);
            }
            Desnap(otherElement.transform.position);
            edgeCollider.enabled = true;
        }
        else
        {
            Debug.LogWarning("Found an edge circle with disconnect problem");
        }
    }