public static void ScheduleReupdate(Components.Wire w) { for (int i = 0; i < SubCircuits.Count; i++) { for (int j = 0; j < SubCircuits[i].wires.Count; j++) { if (w == SubCircuits[i].wires[j]) { for (int k = 0; k < reUpdateList.Count; k++) { if (reUpdateList[k] == SubCircuits[i]) { return; } } reUpdateList.Add(SubCircuits[i]); SubCircuits[i].Recalculate = true; return; } } } }
public void GenNewComponent() { Random r = new Random(); var a = GraphicsEngine.camera.VisibleRectangle; int type = r.Next(3);//0 = component, 1,2 = wire if (type == 0)//component { int ci = r.Next(0, GUIEngine.s_componentSelector.components.Count); GUIEngine.s_componentSelector.components[ci].avalable = 1; var c = GUIEngine.s_componentSelector.components[ci].GetNewInstance() as Components.Component; if (c == null || c is Components.Cursor || c is Components.Wire || c is Components.Properties.IDragDropPlacable || c is Components.Properties.ICore) return; var rots = c.Graphics.GetPossibleRotations(); if (rots != null && rots.Length > 0) { int rot = r.Next(0, rots.Length); c.ComponentRotation = rots[rot]; } int x = r.Next(a.X - 100, a.X + a.Width + 100); int y = r.Next(a.Y - 100, a.Y + a.Height + 100); if (!GraphicsEngine.CanDrawGhostComponent(x-16, y-16, (int)c.Graphics.GetSize().X+32, (int)c.Graphics.GetSize().Y+32)) return; c.Graphics.Position = new Vector2(x, y); //components.Add(c); c.Initialize(); c.InitAddChildComponents(); c.Tag = 0; Components.ComponentsManager.Add(c); } else//wire { if (Components.ComponentsManager.Components.Count == 0) return; var c1 = Components.ComponentsManager.GetComponent(r.Next(0, Components.ComponentsManager.Components.Count)); var c2 = Components.ComponentsManager.GetComponent(r.Next(0, Components.ComponentsManager.Components.Count)); if (c1 == null || c2 == null) return; var cj1 = c1.getJoints(); var cj2 = c2.getJoints(); if (cj1 == null || cj2 == null || cj1.Length == 0 || cj2.Length == 0) return; var j1 = Components.ComponentsManager.GetComponent(cj1[r.Next(cj1.Length)]); var j2 = Components.ComponentsManager.GetComponent(cj2[r.Next(cj2.Length)]); if (j1 == null || j2 == null || !(j1 is Components.Joint) || !(j2 is Components.Joint) || !(j1 as Components.Joint).Graphics.Visible || !(j2 as Components.Joint).Graphics.Visible) return; Components.Wire w = new Components.Wire(j1 as Components.Joint, j2 as Components.Joint); w.Initialize(); w.InitAddChildComponents(); w.Tag = 0; Components.ComponentsManager.Add(w); } ticksSinceLastComponentAdd = 0; }
public static void SplitWire(Components.Wire w, Components.Joint j) { if (!w.IsRemovable) return; bool hasPoint = false; int point = -1; var g = w.Graphics as Components.Graphics.WireGraphics; if (GridHelper.ArePointsInSameCell(g.DrawPath[0], j.Graphics.Position)) { hasPoint = true; point = 0; } for (int i = 0; i < g.DrawPath.Count - 1 && !hasPoint; i++) { if (GridHelper.ArePointsInSameCell(g.DrawPath[i + 1], j.Graphics.Position)) { g.DrawPath.Insert(i + 1, j.Graphics.Center); hasPoint = true; point = i + 2;//dunno why break; } if (GridHelper.IsPointInLineSegment(g.DrawPath[i], g.DrawPath[i + 1], j.Graphics.Position)) { g.DrawPath.Insert(i + 1, j.Graphics.Center); g.DrawPath.Insert(i + 1, j.Graphics.Center); hasPoint = true; point = i + 2; break; } } if (w == null || j == null) return; var w2 = new Components.Wire(w.J2, j); w.J2.ConnectedWires.Remove(w); w.J2 = j; j.ConnectedWires.Add(w); w2.Initialize(); if (!hasPoint) { w.Graphics.Initialize(); } else { var a = g.DrawPath.GetRange(point, g.DrawPath.Count - point); g.DrawPath.RemoveRange(point, a.Count); g.DrawPath.Reverse(); (w2.Graphics as Components.Graphics.WireGraphics).DrawPath = g.DrawPath; g.DrawPath = a; g.GenerateElectrons(); (w2.Graphics as Components.Graphics.WireGraphics).GenerateElectrons(); } w2.AddComponentToManager(); }
public static void onButtonDown(InputEngine.MouseArgs e) { //create placable area if (Graphics.GUI.Scene.PlacableAreasCreator.create) { int x1 = e.curState.X; int y1 = e.curState.Y; GridHelper.GridCoords(ref x1, ref y1); pendingWireP1 = new Vector2(x1, y1); pendingWireP2 = new Vector2(pendingWireP1.X, pendingWireP1.Y); isPlacableAreaPending = true; return; } //delete placable area if (Graphics.GUI.Scene.PlacableAreasCreator.delete) { if (PlacableAreasManager.Remove(e.curState.X, e.curState.Y) && !InputEngine.Shift) Graphics.GUI.Scene.PlacableAreasCreator.delete = false; } //Resize if (e.button == 0 && MouseOverComponent != null && MouseOverComponent is Components.Properties.IResizable) { int x1 = e.curState.X; int y1 = e.curState.Y; GridHelper.GridCoords(ref x1, ref y1); var a = (MouseOverComponent as Components.Properties.IResizable).CanResize(new Vector2(x1, y1)); if (a != Direction.None) { resizeLastPoint = new Vector2(InputEngine.curMouse.X, InputEngine.curMouse.Y); resizeType = a; resizeComponent = MouseOverComponent as Components.Properties.IResizable; (MouseOverComponent as Components.Properties.IResizable).OnResizeStart(a, new Vector2(x1, y1)); return; } } //Cancel resize if (e.button == 1 && resizeComponent != null) { resizeComponent.CancelResize(); resizeComponent = null; resizeType = Direction.None; return; } //DnD component if (e.button == 0 && Graphics.GUI.GUIEngine.ContainsHUDScene(Graphics.GUI.GUIEngine.s_subComponentButtons) && Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent.isIn(e.curState.X, e.curState.Y) && Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent.CanDragDrop() && Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent.IsMovable()) { if (Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent is Components.Joint) { if (!InputEngine.Control) { goto DnDPass; } } //if (Settings.GameState == Settings.GameStates.Stopped) { isDragDrop = true; canMove = true; DragDropComponent = Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent; DragDropStart = DragDropComponent.Graphics.Position; DnDStartOffset = new Vector2(e.curState.X, e.curState.Y) - DragDropStart; DragDropDelta = new Vector2(); } //else //{ // Graphics.OverlayManager.HighlightStop(); //} return; DnDPass: ; } //cancel DnD if (isDragDrop && e.button == 1) { isDragDrop = false; DragDropComponent = null; } if (isComponentDnD && e.button == 1) { isComponentDnD = false; DnDComponent = null; } //shift-placing wires if (e.button == 0 && InputEngine.Shift && Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent != null && Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent is Components.Joint) { int x1 = e.curState.X; int y1 = e.curState.Y; GridHelper.GridCoords(ref x1, ref y1); var a = HasJointAtCoord(x1, y1); if (a != null) { bool b = false; foreach (var wr in (Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent as Components.Joint).ConnectedWires) { if ((wr.J1.ID == Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent.ID && wr.J2.ID == a.ID) || (wr.J2.ID == Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent.ID && wr.J1.ID == a.ID)) { b = true; break; } } if (!b) { Components.Wire w = new Components.Wire(Graphics.GUI.GUIEngine.s_subComponentButtons.SelectedComponent as Components.Joint, a); w.Initialize(); Graphics.GUI.GUIEngine.s_componentSelector.DecreaseComponentAvilability("Wire"); Components.ComponentsManager.Add(w); w.OnPlaced(); IgnoreNextClick = true; if (Settings.GameState != Settings.GameStates.Stopped) { CircuitManager.ReCreate(); } return; } } } //placing wire if (Graphics.GUI.GUIEngine.s_componentSelector.GetComponent("Wire").Enabled && e.button == 0) { //if (Settings.GameState != Settings.GameStates.Stopped) //{ //Graphics.OverlayManager.HighlightStop(); // return; //} if (Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponent.Text == "Wire") { isLine = true; } else { goto PostWire; } int x1 = e.curState.X; int y1 = e.curState.Y; GridHelper.GridCoords(ref x1, ref y1); if (isLine) { pendingWirePath.Clear(); if (PlacableAreasManager.IsPlacable(x1, y1)) { if (HasJointAtCoord(x1, y1) == null && Components.ComponentsManager.VisibilityMap.GetAStarValue(x1, y1) == 0)//smth in the way return; //isLine = true; pendingWirePath.Clear(); pendingWireP1 = new Vector2(x1, y1); pendingWireP2 = new Vector2(pendingWireP1.X, pendingWireP1.Y); Components.Joint jj; Components.ComponentsManager.IgnoreAS1 = null; Components.ComponentsManager.IgnoreAS2 = null; if ((jj = HasJointAtCoord(x1, y1)) != null) { Components.ComponentsManager.IgnoreAS1 = jj; } } else { isLine = false; } } return; PostWire: ; //j1 = GetJointForCoord(e); } //canceling wire if (isLine && e.button == 1) { isLine = false; } //DnD-placing component if (Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponent.IsDragDropPlacement && !isLine && !isComponentDnD) { //if (Settings.GameState != Settings.GameStates.Stopped) //{ // Graphics.OverlayManager.HighlightStop(); // return; //} DnDComponent = Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponent.component.GetNewInstance() as Components.Properties.IDragDropPlacable; if (DnDComponent == null) return; int x1 = e.curState.X; int y1 = e.curState.Y; GridHelper.GridCoords(ref x1, ref y1); if (DnDComponent.CanStart(x1, y1)) { pendingWireP1 = new Vector2(x1, y1); pendingWireP2 = new Vector2(pendingWireP1.X, pendingWireP1.Y); isComponentDnD = true; } else { DnDComponent = null; isComponentDnD = false; } return; } //dragging wire from joint Components.Joint j; if (!InputEngine.Control && (j = HasJointAtCoord(e.curState.X, e.curState.Y)) != null && Graphics.GUI.GUIEngine.s_componentSelector.GetComponent("Wire").Enabled) { //if (Settings.GameState != Settings.GameStates.Stopped) //{ // Graphics.OverlayManager.HighlightStop(); // return; //} int x1 = e.curState.X; int y1 = e.curState.Y; GridHelper.GridCoords(ref x1, ref y1); if (PlacableAreasManager.IsPlacable(x1, y1)) { isLine = true; pendingWirePath.Clear(); pendingWireP1 = new Vector2(x1, y1); pendingWireP2 = new Vector2(pendingWireP1.X, pendingWireP1.Y); Components.ComponentsManager.IgnoreAS1 = null; Components.ComponentsManager.IgnoreAS2 = null; } return; } //DnD w/o selection if (e.button == 0) { LastLeftClick = new Vector2(e.curState.X, e.curState.Y); } else { LastLeftClick = null; } }
public static void onButtonUp(InputEngine.MouseArgs e) { if (Graphics.GUI.Scene.PlacableAreasCreator.create) { Graphics.GUI.Scene.PlacableAreasCreator.create = false; int x2 = e.curState.X; int y2 = e.curState.Y; GridHelper.GridCoords(ref x2, ref y2); isPlacableAreaPending = false; PlacableAreasManager.Add(new Rectangle((int)pendingWireP1.X, (int)pendingWireP1.Y, (int)(x2 - pendingWireP1.X), (int)(y2 - pendingWireP1.Y))); return; } if (resizeComponent != null) { resizeComponent.OnResizeFinished(resizeType); resizeComponent = null; resizeType = Direction.None; return; } if (isDragDrop) { isDragDrop = false; DragDropComponent = null; return; } if (isLine) { int x2 = e.curState.X; int y2 = e.curState.Y; PlacableAreasManager.MakePlacable(ref x2, ref y2); GridHelper.GridCoords(ref x2, ref y2); if (pendingWireP1.X == x2 && pendingWireP1.Y == y2) { isLine = false; pendingWireP1 = new Vector2(); return; } Components.Joint tj1 = HasJointAtCoord((int)pendingWireP1.X, (int)pendingWireP1.Y), tj2 = HasJointAtCoordForWire(x2, y2); var a1 = Components.ComponentsManager.VisibilityMap.GetAStarValue((int)pendingWireP1.X + 2, (int)pendingWireP1.Y + 2); var a2 = tj2 == null ? Components.ComponentsManager.VisibilityMap.GetAStarValue((int)x2 + 2, (int)y2 + 2) : 0; if (tj2 == null) { tj2 = GetJointForCoordIfCan(x2, y2); if (tj2 == null) { isLine = false; pendingWireP1 = new Vector2(); pendingWireP2 = new Vector2(pendingWireP1.X, pendingWireP1.Y); return; } } if ((pendingWireP1.X != tj2.Graphics.Position.X || pendingWireP1.Y != tj2.Graphics.Position.Y) && !Components.Graphics.WireGraphics.CanFindPath(tj1, tj2, (int)pendingWireP1.X + 4, (int)pendingWireP1.Y + 4, (int)pendingWireP2.X + 4, (int)pendingWireP2.Y + 4)) { isLine = false; pendingWireP1 = new Vector2(); pendingWireP2 = new Vector2(pendingWireP1.X, pendingWireP1.Y); return; } if (pendingWireP1.X != tj2.Graphics.Position.X || pendingWireP1.Y != tj2.Graphics.Position.Y) { MicroWorld.Logics.ChangeHistory.Push(); Statistics.ElementsPlaced++; j1 = GetJointForCoord(pendingWireP1); Components.Joint j2 = GetJointForCoord((int)tj2.Graphics.Position.X, (int)tj2.Graphics.Position.Y); //w Components.Wire w = new Components.Wire(j1, j2); w.Initialize(); Graphics.GUI.GUIEngine.s_componentSelector.DecreaseComponentAvilability("Wire"); Components.ComponentsManager.Add(w); w.OnPlaced(); if (a1 == Components.VisibilityMap.WIRE) { var a = GetWiresForCoord((int)pendingWireP1.X, (int)pendingWireP1.Y); for (int i = 0; i < a.Count; i++) { if (a[i] != w && a[i].J1 != j1 && a[i].J2 != j1) SplitWire(a[i], j1); } } if (a2 == Components.VisibilityMap.WIRE) { var a = GetWiresForCoord((int)tj2.Graphics.Position.X, (int)tj2.Graphics.Position.Y); for (int i = 0; i < a.Count; i++) { if (a[i] != w && a[i].J1 != j2 && a[i].J2 != j2) SplitWire(a[i], j2); } } CircuitManager.ReCreate(); } isLine = false; return; } if (isComponentDnD) { int x2 = e.curState.X; int y2 = e.curState.Y; PlacableAreasManager.MakePlacable(ref x2, ref y2); GridHelper.GridCoords(ref x2, ref y2); if (pendingWireP1.X == x2 && pendingWireP1.Y == y2) { isComponentDnD = false; pendingWireP1 = new Vector2(); pendingWireP2 = new Vector2(pendingWireP1.X, pendingWireP1.Y); DnDComponent = null; return; } if (!DnDComponent.CanEnd((int)pendingWireP1.X, (int)pendingWireP1.Y, x2, y2)) { isComponentDnD = false; pendingWireP1 = new Vector2(); pendingWireP2 = new Vector2(pendingWireP1.X, pendingWireP1.Y); DnDComponent = null; return; } MicroWorld.Logics.ChangeHistory.Push(); DnDComponent.Place((int)pendingWireP1.X, (int)pendingWireP1.Y, x2, y2); MicroWorld.Graphics.GUI.GUIEngine.s_componentSelector.DecreaseComponentAvilability((DnDComponent as Components.Component).Graphics.GetCSToolTip()); CircuitManager.ReCreate(); isComponentDnD = false; pendingWireP1 = new Vector2(); pendingWireP2 = new Vector2(pendingWireP1.X, pendingWireP1.Y); DnDComponent = null; return; } if (e.button == 0 && InputEngine.LeftMouseButtonDownPos != null && MicroWorld.Graphics.GUI.GUIEngine.s_componentSelector.SelectedComponent.IsIn( (int)InputEngine.LeftMouseButtonDownPos.Value.X, (int)InputEngine.LeftMouseButtonDownPos.Value.Y)) { TryPlaceComponent(ref e); } }
public void GenNewComponent() { Random r = new Random(); var a = GraphicsEngine.camera.VisibleRectangle; int type = r.Next(3); //0 = component, 1,2 = wire if (type == 0) //component { int ci = r.Next(0, GUIEngine.s_componentSelector.components.Count); GUIEngine.s_componentSelector.components[ci].avalable = 1; var c = GUIEngine.s_componentSelector.components[ci].GetNewInstance() as Components.Component; if (c == null || c is Components.Cursor || c is Components.Wire || c is Components.Properties.IDragDropPlacable || c is Components.Properties.ICore) { return; } var rots = c.Graphics.GetPossibleRotations(); if (rots != null && rots.Length > 0) { int rot = r.Next(0, rots.Length); c.ComponentRotation = rots[rot]; } int x = r.Next(a.X - 100, a.X + a.Width + 100); int y = r.Next(a.Y - 100, a.Y + a.Height + 100); if (!GraphicsEngine.CanDrawGhostComponent(x - 16, y - 16, (int)c.Graphics.GetSize().X + 32, (int)c.Graphics.GetSize().Y + 32)) { return; } c.Graphics.Position = new Vector2(x, y); //components.Add(c); c.Initialize(); c.InitAddChildComponents(); c.Tag = 0; Components.ComponentsManager.Add(c); } else//wire { if (Components.ComponentsManager.Components.Count == 0) { return; } var c1 = Components.ComponentsManager.GetComponent(r.Next(0, Components.ComponentsManager.Components.Count)); var c2 = Components.ComponentsManager.GetComponent(r.Next(0, Components.ComponentsManager.Components.Count)); if (c1 == null || c2 == null) { return; } var cj1 = c1.getJoints(); var cj2 = c2.getJoints(); if (cj1 == null || cj2 == null || cj1.Length == 0 || cj2.Length == 0) { return; } var j1 = Components.ComponentsManager.GetComponent(cj1[r.Next(cj1.Length)]); var j2 = Components.ComponentsManager.GetComponent(cj2[r.Next(cj2.Length)]); if (j1 == null || j2 == null || !(j1 is Components.Joint) || !(j2 is Components.Joint) || !(j1 as Components.Joint).Graphics.Visible || !(j2 as Components.Joint).Graphics.Visible) { return; } Components.Wire w = new Components.Wire(j1 as Components.Joint, j2 as Components.Joint); w.Initialize(); w.InitAddChildComponents(); w.Tag = 0; Components.ComponentsManager.Add(w); } ticksSinceLastComponentAdd = 0; }