public SegmentDisplayRenderer(SegmentDisplay gate)
        {
            this.gate = gate;

            Panel.SetZIndex(top, 4);
            Panel.SetZIndex(topLeft, 4);
            Panel.SetZIndex(topRight, 4);
            Panel.SetZIndex(center, 4);
            Panel.SetZIndex(botLeft, 4);
            Panel.SetZIndex(botRight, 4);
            Panel.SetZIndex(bot, 4);

            gate.OnRenderedChanged += OnRenderedChanged;

            if (gate.IsRendered)
            {
                OnRenderedChanged();
            }
        }
Exemple #2
0
        public static Gate Deserialize(SerializedGate storageObject)
        {
            Gate gate;

            switch (storageObject.Type)
            {
            case "ContextGate":
                gate = new ContextGate();
                break;

            case "AndGate":
                gate = new AndGate();
                break;

            case "OrGate":
                gate = new OrGate();
                break;

            case "NopGate":
                gate = new NopGate();
                break;

            case "InputSwitch":
                gate = new InputSwitch();
                break;

            case "OutputLight":
                gate = new OutputLight();
                break;

            case "SegmentDisplay":
                gate = new SegmentDisplay();
                break;

            default:
                throw new InvalidOperationException("Unknown type");
            }

            gate.Name     = storageObject.Name;
            gate.Tag      = storageObject.Tag;
            gate.Position = storageObject.Position;
            gate.Size     = storageObject.Size;

            if (storageObject.InputConnections == null)
            {
                storageObject.InputConnections = new int[0];
            }
            if (storageObject.OutputConnections == null)
            {
                storageObject.OutputConnections = new int[0];
            }

            if (storageObject.Type == "ContextGate")
            {
                ContextGate contextGate  = gate as ContextGate;
                var         idToNode     = new Dictionary <int, ConnectionNode>();
                var         inputStores  = new List <SerializedGate>();
                var         outputStores = new List <SerializedGate>();
                var         gateStores   = new List <SerializedGate>();

                foreach (SerializedGate innerStore in storageObject.Context)
                {
                    switch (innerStore.Type)
                    {
                    case "InputSwitch":
                        inputStores.Add(innerStore);
                        break;

                    case "OutputLight":
                        outputStores.Add(innerStore);
                        break;

                    default:
                        gateStores.Add(innerStore);
                        break;
                    }
                }

                inputStores.Sort(ComparePosition);
                outputStores.Sort(ComparePosition);

                foreach (SerializedGate gateStore in gateStores)
                {
                    Gate innerGate = Deserialize(gateStore);
                    contextGate.Context.Add(innerGate);
                    for (int i = 0; i < gateStore.OutputConnections.Count(); i++)
                    {
                        int id = gateStore.OutputConnections[i];
                        if (id != 0)
                        {
                            idToNode[id] = innerGate.Output[i];
                        }
                    }
                }
                foreach (SerializedGate inputStore in inputStores)
                {
                    int id        = inputStore.OutputConnections.First();
                    var inputNode = new InputNode(contextGate);
                    inputNode.Name = inputStore.Name;
                    contextGate.Input.Add(inputNode);
                    if (id != 0)
                    {
                        idToNode[id] = inputNode;
                    }
                }

                for (int i = 0; i < gateStores.Count; i++)
                {
                    SerializedGate innerStore = gateStores[i];
                    Gate           innerGate  = contextGate.Context[i];
                    for (int j = 0; j < innerStore.InputConnections.Count(); j++)
                    {
                        int id = innerStore.InputConnections[j];
                        if (id != 0)
                        {
                            if (!idToNode.ContainsKey(id))
                            {
                                throw new InvalidOperationException("Invalid connection");
                            }
                            ConnectionNode thisNode  = innerGate.Input[j];
                            ConnectionNode otherNode = idToNode[id];
                            otherNode.NextConnectedTo.Add(thisNode);
                            thisNode.BackConnectedTo = otherNode;
                            //otherNode.IsEmpty = false;
                            //thisNode.IsEmpty = false;
                            thisNode.State = thisNode.IsInverted ? !otherNode.State : otherNode.State;
                        }
                    }
                }
                foreach (SerializedGate outputStore in outputStores)
                {
                    int        id          = outputStore.InputConnections.First();
                    OutputNode contextNode = new OutputNode(contextGate);
                    contextNode.Name = outputStore.Name;
                    contextGate.Output.Add(contextNode);
                    if (id != 0)
                    {
                        if (!idToNode.ContainsKey(id))
                        {
                            throw new InvalidOperationException("Invalid connection");
                        }
                        ConnectionNode otherNode = idToNode[id];
                        otherNode.NextConnectedTo.Add(contextNode);
                        contextNode.BackConnectedTo = otherNode;
                        //otherNode.IsEmpty = false;
                        //contextNode.IsEmpty = false;
                        contextNode.State = otherNode.State;
                    }
                }
            }
            else
            {
                foreach (int id in storageObject.InputConnections)
                {
                    gate.Input.Add(new InputNode(gate));
                }
                foreach (int id in storageObject.OutputConnections)
                {
                    gate.Output.Add(new OutputNode(gate));
                }
            }

            if (storageObject.InvertedInputs != null)
            {
                foreach (int index in storageObject.InvertedInputs)
                {
                    gate.Input[index].Invert();
                    gate.Input[index].State = !gate.Input[index].State;
                }
            }
            if (storageObject.InvertedOutputs != null)
            {
                foreach (int index in storageObject.InvertedOutputs)
                {
                    gate.Output[index].Invert();
                }
            }

            if (storageObject.InitialActiveOutputs != null)
            {
                foreach (int index in storageObject.InitialActiveOutputs)
                {
                    gate.Output[index].State = true;
                }
            }

            if (storageObject.RisingEdgeInputs != null)
            {
                foreach (int index in storageObject.RisingEdgeInputs)
                {
                    gate.Input[index].IsRisingEdge = true;
                }
            }

            if (storageObject.MasterSlaveOutputs != null)
            {
                foreach (int index in storageObject.MasterSlaveOutputs)
                {
                    gate.Output[index].IsMasterSlave = true;
                }
            }

            if (storageObject.CenteredInputs != null)
            {
                foreach (int index in storageObject.CenteredInputs)
                {
                    gate.Input[index].IsCentered = true;
                }
            }

            if (storageObject.CenteredOutputs != null)
            {
                foreach (int index in storageObject.CenteredOutputs)
                {
                    gate.Output[index].IsCentered = true;
                }
            }

            if (storageObject.Type == "InputSwitch")
            {
                ((InputSwitch)gate).State = gate.Output.First().IsInverted ? !gate.Output.First().State : gate.Output.First().State;
            }

            return(gate);
        }