Пример #1
0
        // Delete circuit gate
        public void deleteCircuitGate(Circuit circuit, Gate gate)
        {
            // Disconnect gate connections
            foreach (Gate input in gate.inputs)
            {
                input.outputs.Remove(gate);
            }
            foreach (Gate output in gate.outputs)
            {
                output.inputs.Remove(gate);
            }

            // Remove gate from circuit
            circuit.gates.Remove(gate);

            // Deselect gate
            _view.deselectGate();
        }
Пример #2
0
        // Key down
        void Parent_KeyDown(object sender, KeyEventArgs e)
        {
            Circuit circuit = _view.selectedCircuit;

            if (circuit == null)
            {
                return;
            }

            if (_view.keysEnabled)
            {
                if (e.KeyCode == Keys.Shift || e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.LShiftKey || e.KeyCode == Keys.RShiftKey)
                {
                    _view.controller.shift = true;
                }
                else if (e.KeyCode == Keys.Control || e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.LControlKey || e.KeyCode == Keys.RControlKey)
                {
                    _view.controller.ctrl = true;
                }

                if (e.KeyCode == Keys.Escape)
                {
                    _view.deselectGate();
                    _inputSource = null;
                }
                else if (e.KeyCode == Keys.Delete)
                {
                    if (_view.selectedGate != null)
                    {
                        // Remove selected gate
                        _controller.deleteCircuitGate(circuit, _view.selectedGate);
                    }
                    else
                    {
                        // Hit test gates and try to remove one
                        float maxDistance = 20f / _view.controller.getScale();
                        foreach (Gate gate in circuit.gates)
                        {
                            if ((_view.controller.getWorldMouse() - gate.position).Length() <= maxDistance)
                            {
                                _controller.deleteCircuitGate(circuit, gate);
                                return;
                            }
                        }

                        // Hit test connections and try to remove one
                        Gate gateA = null;
                        Gate gateB = null;
                        foreach (Gate gate in circuit.gates)
                        {
                            foreach (Gate output in gate.outputs)
                            {
                                if (connectionHitTest(gate, output))
                                {
                                    gateA = gate;
                                    gateB = output;
                                    break;
                                    //_controller.disconnectGates(gate, output);
                                }
                            }
                        }
                        if (gateA != null && gateB != null)
                        {
                            _controller.disconnectGates(gateA, gateB);
                        }
                    }
                }
            }
        }