// 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(); }
// Select gate public void selectGate(Gate gate) { _selectedGate = gate; gateAddButton.Enabled = false; }
// Deselect gate public void deselectGate() { _selectedGate = null; gateAddButton.Enabled = true; }
// Add gate button clicked private void gateAddButton_Click(object sender, EventArgs e) { System.Diagnostics.Debug.Assert(selectedCircuit != null); SelectGateType selectGateTypeForm = new SelectGateType(); if (selectGateTypeForm.ShowDialog() == DialogResult.OK) { Gate gate = new Gate(selectedCircuit, _controller.getUnusedGateID(selectedCircuit), selectGateTypeForm.gateType, _controller.getWorldMouse()); selectedCircuit.gates.Add(gate); _selectedGate = gate; } }
// Selected circuit changed private void circuitsList_SelectedValueChanged(object sender, EventArgs e) { // Clear any selected gates _selectedGate = null; // Enable buttons circuitDeleteButton.Enabled = selectedCircuit != null; gateAddButton.Enabled = selectedCircuit != null && _selectedGate == null; }
public CircuitOutputConnection(EditorCircuitActor circuitActor, EditorActor actor, Gate gate, GameEventType onEnabledEvent, GameEventType onDisabledEvent) : base(circuitActor, actor, gate, "output") { _onEnabledEvent = onEnabledEvent; _onDisabledEvent = onDisabledEvent; }
// 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); } } } }
// Hit test connections private bool connectionHitTest(Gate gateA, Gate gateB) { Vector2 worldMouse = _controller.getWorldMouse(); // Treat line as a thin box Vector2 boxCenter = (gateA.position + gateB.position) / 2; Vector2 linkRelative = gateB.position - gateA.position; float boxHalfWidth = linkRelative.Length() / 2; float boxHalfHeight = 0.15f; float angle = (float)Math.Atan2(linkRelative.Y, linkRelative.X); // Get the mouse position relative to the box's center Vector2 relative = boxCenter - worldMouse; // Rotate the relative mouse position by the negative angle of the box Vector2 alignedRelative = Vector2.Transform(relative, Matrix.CreateRotationZ(-angle)); // Get the local, cartiasian-aligned bounding-box Vector2 topLeft = -(new Vector2(boxHalfWidth, boxHalfHeight)); Vector2 bottomRight = -topLeft; // Check if the relative mouse point is inside the bounding box Vector2 d1, d2; d1 = alignedRelative - topLeft; d2 = bottomRight - alignedRelative; // One of these components will be less than zero if the alignedRelative position is outside of the bounds if (d1.X < 0 || d1.Y < 0) return false; if (d2.X < 0 || d2.Y < 0) return false; return true; }
// Mouse down void CircuitDisplay_MouseDown(object sender, MouseEventArgs e) { Circuit circuit = _view.selectedCircuit; if (circuit == null) return; // Hit test gates Gate target = hitTestGates(circuit, _controller.getWorldMouse()); if (e.Button == System.Windows.Forms.MouseButtons.Left) { if (_view.selectedGate == null && target != null) { // Select target _view.selectGate(target); } else if (_view.selectedGate != null) { // Drop selected gate _view.deselectGate(); } } else if (e.Button == System.Windows.Forms.MouseButtons.Right) { if (target != null) { if (_inputSource == null) { // Set input source _inputSource = target; } else { // Set output bool setOutput = false; switch (target.type) { case "input": setOutput = false; break; case "output": setOutput = target.inputs.Count < 1; break; case "and": setOutput = target.inputs.Count < 2; break; case "or": setOutput = target.inputs.Count < 2; break; case "not": setOutput = target.inputs.Count < 1; break; } if (setOutput) { _inputSource.outputs.Add(target); target.inputs.Add(_inputSource); _inputSource = null; } } } else { // Clear input source _inputSource = null; } } }
// Disconect gates public void disconnectGates(Gate gateA, Gate gateB) { gateA.outputs.Remove(gateB); gateB.inputs.Remove(gateA); }