public void DeleteSelectedComponents() { foreach (DrawableComponent dc in SelectedComponents) { foreach (CircuitInput ci in dc.Inputs) { if (ci.Signal != null) { DrawableWire dw = (DrawableWire)ci.Signal; dw.From.RemoveOutputWire(dw.FromIndex, dw); RemoveWire((DrawableWire)ci.Signal); } } foreach (CircuitOutput co in dc.Outputs) { co.Signals.ForEach(x => { DrawableWire dw = (DrawableWire)x; dw.To.RemoveInputWire(dw.ToIndex); RemoveWire((DrawableWire)x); }); } AllComponents.Remove(dc); } SelectedComponents.Clear(); }
public void RemoveWire(DrawableWire wire) { if (AllWires.Contains(wire)) { AllWires.Remove(wire); } }
public override void Update() { // Update ImGui, and submit UI. controller.Update(Raylib.GetFrameTime()); SubmitUI(); // TODO: Move this to more suitable place currentMousePos = Raylib.GetMousePosition(); // This comes from being able to turn off simulation in the main menu if (_simulationOn) { currentProject.Simulation.UpdateLogic(GetMousePositionInWorld()); } // Should always update all updateable components though, like being able // to turn off and on switches during sim-off. currentProject.Simulation.Update(GetMousePositionInWorld()); // Get the hovered component, is null if nothing is hovered. DrawableComponent hovered = currentProject.Simulation.GetComponentFromPosition(GetMousePositionInWorld()); // Hovering inputs & outputs Tuple <int, DrawableComponent> hoveredInput = currentProject.Simulation.GetComponentAndInputFromPos(GetMousePositionInWorld()); Tuple <int, DrawableComponent> hoveredOutput = currentProject.Simulation.GetComponentAndOutputFromPos(GetMousePositionInWorld()); // Allow for camera zooming with the mouse wheel. #region Mouse Wheel Camera Zoom if (!ImGui.GetIO().WantCaptureMouse) { if (Raylib.GetMouseWheelMove() > 0) { cam.zoom *= 1.05f; } if (Raylib.GetMouseWheelMove() < 0) { cam.zoom *= 1 / 1.05f; } } #endregion #region Decide Which Editor State if (state != EditorState.SelectingProjectFile && state != EditorState.IncludeCollection && state != EditorState.IncludeDescription && state != EditorState.SavingProjectFile) { if (!ImGui.GetIO().WantCaptureMouse || newComponent != null) { if (Raylib.IsKeyDown(KeyboardKey.KEY_LEFT_SHIFT) && Raylib.IsKeyPressed(KeyboardKey.KEY_D)) { CopySelectedComponentsNewOffsetPos(new Vector2(100, 100)); } if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_MIDDLE_BUTTON) && state == EditorState.None) { state = EditorState.MovingCamera; } if (Raylib.IsMouseButtonReleased(MouseButton.MOUSE_MIDDLE_BUTTON) && state == EditorState.MovingCamera) { state = EditorState.None; } if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON) && state == EditorState.None) { if (hovered == null) { state = EditorState.RectangleSelecting; startPos = GetMousePositionInWorld(); } else if (currentProject.Simulation.SelectedComponents.Contains(hovered)) { state = EditorState.MovingSelection; } } if (Raylib.IsMouseButtonReleased(MouseButton.MOUSE_LEFT_BUTTON) && (state == EditorState.MovingSelection || state == EditorState.RectangleSelecting)) { state = EditorState.None; newComponent = null; } if (state == EditorState.None) { if (hoveredInput != null) { state = EditorState.HoveringInput; } else if (hoveredOutput != null) { state = EditorState.HoveringOutput; } } else if (state.HasFlag(EditorState.HoveringInput) || state.HasFlag(EditorState.HoveringOutput)) { if (hoveredInput == null && hoveredOutput == null && state != EditorState.OutputToInput) { state = EditorState.None; } } if (state == EditorState.HoveringUI) { state = EditorState.None; } } else { if (state != EditorState.MovingSelection) { state = EditorState.HoveringUI; } } } #endregion #region Perform Editor State if (state == EditorState.MovingCamera) { cam.target -= (currentMousePos - previousMousePos) * 1f / cam.zoom; } if (state == EditorState.MovingSelection) { foreach (DrawableComponent dc in currentProject.Simulation.SelectedComponents) { dc.Position += (currentMousePos - previousMousePos) / cam.zoom; } } if (state == EditorState.RectangleSelecting) { Vector2 current = GetMousePositionInWorld(); if (current.Y < startPos.Y && current.X > startPos.X) { // First quadrant selectionRec = new Rectangle(startPos.X, current.Y, current.X - startPos.X, startPos.Y - current.Y); } else if (current.Y < startPos.Y && current.X < startPos.X) { // Second quadrant selectionRec = new Rectangle(current.X, current.Y, startPos.X - current.X, startPos.Y - current.Y); } else if (current.Y > startPos.Y && current.X < startPos.X) { // Third quadrant selectionRec = new Rectangle(current.X, startPos.Y, startPos.X - current.X, current.Y - startPos.Y); } else { // Fourth quadrant selectionRec = new Rectangle(startPos.X, startPos.Y, current.X - startPos.X, current.Y - startPos.Y); } currentProject.Simulation.SelectComponentsInRectangle(selectionRec); } if (state == EditorState.HoveringOutput) { if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) { tempOutput = hoveredOutput; state = EditorState.OutputToInput; } } if (state == EditorState.HoveringInput) { if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_RIGHT_BUTTON)) { if (hoveredInput.Item2.Inputs[hoveredInput.Item1].Signal != null) { DrawableWire dw = (DrawableWire)hoveredInput.Item2.Inputs[hoveredInput.Item1].Signal; hoveredInput.Item2.Inputs[hoveredInput.Item1].RemoveSignal(); dw.From.RemoveOutputWire(dw.FromIndex, dw); currentProject.Simulation.RemoveWire(dw); } } } if (state == EditorState.OutputToInput) { if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON) && hoveredInput != null) { if (hoveredInput.Item2.Inputs[hoveredInput.Item1].Signal == null) { DrawableWire dw = new DrawableWire(tempOutput.Item2, hoveredInput.Item2, tempOutput.Item1, hoveredInput.Item1); currentProject.Simulation.AddWire(dw); tempOutput.Item2.AddOutputWire(tempOutput.Item1, dw); hoveredInput.Item2.SetInputWire(hoveredInput.Item1, dw); tempOutput = null; state = EditorState.None; } } } #endregion #region Delete Components if (Raylib.IsKeyPressed(KeyboardKey.KEY_DELETE)) { currentProject.Simulation.DeleteSelectedComponents(); } #endregion #region Select Components With Clicking // If we press LMB, then we have the following scenario: // // If we aren't hovering anything -> Clear Selected Components // If we are hovering something // If we're holding shift -> Add Selected Component // If not -> Clear Selected Components, Add Hovered Component to Selected // + Bonus: By setting state to MovingSelection, you can move any component you simply click. if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON) && state == EditorState.None) { if (hovered != null) { if (Raylib.IsKeyDown(KeyboardKey.KEY_LEFT_SHIFT)) { currentProject.Simulation.AddSelectedComponent(hovered); } else { if (!currentProject.Simulation.SelectedComponents.Contains(hovered)) { currentProject.Simulation.ClearSelectedComponents(); } currentProject.Simulation.AddSelectedComponent(hovered); // Allow for moving instantly! state = EditorState.MovingSelection; } } else { currentProject.Simulation.ClearSelectedComponents(); } } #endregion previousMousePos = currentMousePos; }
public void AddWire(DrawableWire wire) { AllWires.Add(wire); }
public Tuple <List <DrawableComponent>, List <DrawableWire> > GenerateDrawables(List <ICDescription> availableDescriptions, Vector2 offset) { DrawableComponent[] components = new DrawableComponent[Components.Count]; for (int i = 0; i < Components.Count; i++) { WorkspaceComponent wc = Components[i]; DrawableComponent dc = null; switch (wc.Type) { case "ANDGateLogic": dc = new DrawableLogicGate(wc.Position + offset, "AND", new ANDGateLogic(), false); break; case "NANDGateLogic": dc = new DrawableLogicGate(wc.Position + offset, "NAND", new NANDGateLogic(), false); break; case "ORGateLogic": dc = new DrawableLogicGate(wc.Position + offset, "OR", new ORGateLogic(), false); break; case "NORGateLogic": dc = new DrawableLogicGate(wc.Position + offset, "NOR", new NORGateLogic(), false); break; case "XORGateLogic": dc = new DrawableLogicGate(wc.Position + offset, "XOR", new XORGateLogic(), false); break; case "XNORGateLogic": dc = new DrawableLogicGate(wc.Position + offset, "XNOR", new XNORGateLogic(), false); break; case "NOTGateLogic": dc = new DrawableLogicGate(wc.Position + offset, "NOT", new NOTGateLogic(), false); break; case "Switch": dc = new DrawableCircuitSwitch(wc.Position + offset, false) { ID = wc.ID }; break; case "Lamp": dc = new DrawableCircuitLamp(wc.Position + offset, false) { ID = wc.ID }; break; default: ICDescription icd = GetDescription(wc.Type, availableDescriptions); if (icd != null) { dc = new DrawableIC(wc.Position + offset, icd.Name, icd, false); } else { break; } break; } components[i] = dc; } List <DrawableWire> wires = new List <DrawableWire>(); for (int i = 0; i < Components.Count; i++) { WorkspaceComponent iccd = Components[i]; foreach (WorkspaceComponentConnection connection in iccd.ConnectedTo) { try { DrawableWire cw = new DrawableWire(components[i], components[connection.To], connection.OutIndex, connection.InIndex); components[i].AddOutputWire(connection.OutIndex, cw); components[connection.To].SetInputWire(connection.InIndex, cw); wires.Add(cw); } catch { } } } return(new Tuple <List <DrawableComponent>, List <DrawableWire> >(components.ToList(), wires)); }