예제 #1
0
        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;
            }
        }
예제 #2
0
        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);
            }
        }