Esempio n. 1
0
        public InputHandler(SFML.Graphics.RenderWindow window)
        {
            //Defining the Commands here
            _go         = new GoCommand();
            _jump       = new JumpCommand();
            turnCommand = new TurnCommand();
            attack      = new AttackCommand();
            //Building the Commandlist
            commandList     = new List <Command>();
            GameCommandList = new List <GameCommand>();
            restartCommand  = new restartCommand();

            //Setup the Joysticksettings
            joystickNr = 0;
            setupJoystick(joystickNr);
            //Setup the Keys of the Keyboard
            setupKeyboard();

            caX   = new CommandAttributes(1);
            caY   = new CommandAttributes(0, 1);
            caXN  = new CommandAttributes(-1);
            caYN  = new CommandAttributes(0, -1);
            timed = new CommandAttributes(Time.FromSeconds(.5f));

            mousePosition = Mouse.GetPosition();
            this.window   = window;
        }
        /// <summary>
        /// Renders the cached surface from a previous call to the constructor or the <see cref="Update(ITextSurfaceRendered)"/> method.
        /// </summary>
        /// <param name="surface">Only used for tinting and calculation the position from the font.</param>
        /// <param name="position">Calculates the rendering position on the screen based on the size of the <paramref name="surface"/> parameter.</param>
        /// <param name="usePixelPositioning">Ignores the <paramref name="surface"/> font for positioning and instead treats the <paramref name="position"/> parameter in pixels.</param>
        public void Render(ITextSurfaceRendered surface, Point position, bool usePixelPositioning = false)
        {
            Matrix matrix;

            if (usePixelPositioning)
            {
                //if (oldAbsolutePosition != position)
                //{
                //    absolutePositionTransform = GetPositionTransform(position, surface.Font.Size, true);
                //    oldAbsolutePosition = position;
                //}

                //matrix = absolutePositionTransform;

                matrix = GetPositionTransform(position, surface.Font.Size, true);
            }
            else
            {
                //if (position != oldPosition)
                //{
                //    positionTransform = GetPositionTransform(position, surface.Font.Size, false);
                //    oldPosition = position;
                //}

                //matrix = positionTransform;

                matrix = GetPositionTransform(position, surface.Font.Size, false);
            }

            Render(surface, matrix);
        }
Esempio n. 3
0
        public override bool ProcessMouse(MouseInfo info)
        {
            var worldLocation   = info.ScreenLocation.WorldLocationToConsole(textSurface.Font.Size.X, textSurface.Font.Size.Y);
            var consoleLocation = new Point(worldLocation.X - _position.X, worldLocation.Y - _position.Y);

            // Check if mouse is within the upper/lower bounds of the console
            if (info.ConsoleLocation.Y >= 0 && info.ConsoleLocation.Y <= textSurface.RenderArea.Height - 1)
            {
                bool isHexRow = info.ConsoleLocation.Y % 2 == 1;
                // Check if mouse is on an alternating row
                if (isHexRow)
                {
                    info.ScreenLocation.X -= textSurface.Font.Size.X / 2;
                }

                info.Fill(this);

                if (info.Console == this)
                {
                    FillHexes(lastCell, 176, lastCellHexRow);
                    lastCell       = info.ConsoleLocation.ToIndex(textSurface.Width);
                    lastCellHexRow = isHexRow;
                    FillHexes(lastCell, 45, isHexRow);

                    return(true);
                }
            }

            //base.ProcessMouse(info);

            FillHexes(lastCell, 176, lastCellHexRow);

            return(true);
        }
Esempio n. 4
0
        public HexConsole(int width, int height) : base(new HexTextSurface(width, height))
        {
            IsVisible        = false;
            Position         = new Point(3, 2);
            textSurface.Font = Engine.LoadFont("Cheepicus12.font").GetFont(SadConsole.Font.FontSizes.One);
            Fill(Color.Red, null, glyph: 176);

            for (int x = 0; x < width; x++)
            {
                bool isHexColumn = x % 2 == 1;

                for (int y = 0; y < height; y++)
                {
                    bool isHexRow = y % 2 == 1;

                    if (isHexColumn && isHexRow)
                    {
                        SetForeground(x, y, ColorHelper.Blue);
                    }
                    else if (isHexRow)
                    {
                        SetForeground(x, y, ColorHelper.AliceBlue);
                    }
                    else if (isHexColumn)
                    {
                        SetForeground(x, y, ColorHelper.DarkRed);
                    }
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Sets the WorldLocation and ConsoleLocation properties based on the cell size of the provided console. If absolute positioning is used on the console, then the properties will represent pixels.
        /// </summary>
        /// <param name="data">The console to get the data from.</param>
        /// <remarks>This method alters the data of the mouse information based on the provided console. It </remarks>
        public void Fill(IConsole data)
        {
            if (data.UsePixelPositioning)
            {
                WorldLocation.X = ScreenLocation.X - data.Position.X;
                WorldLocation.Y = ScreenLocation.Y - data.Position.Y;
                ConsoleLocation = WorldLocation.WorldLocationToConsole(data.TextSurface.Font.Size.X, data.TextSurface.Font.Size.Y);

                if (WorldLocation.X < 0)
                {
                    ConsoleLocation.X -= 1;
                }
                if (WorldLocation.Y < 0)
                {
                    ConsoleLocation.Y -= 1;
                }
            }
            else
            {
                WorldLocation   = ScreenLocation.WorldLocationToConsole(data.TextSurface.Font.Size.X, data.TextSurface.Font.Size.Y);
                ConsoleLocation = new Point(WorldLocation.X - data.Position.X, WorldLocation.Y - data.Position.Y);
            }

            //TODO: Need to translate mouse coords by the render transform used by the console!!

            // If the mouse is on a console, then we need to fill out the mouse information with the console information.
            if (ConsoleLocation.X >= 0 && ConsoleLocation.X <= data.TextSurface.RenderArea.Width - 1 &&
                ConsoleLocation.Y >= 0 && ConsoleLocation.Y <= data.TextSurface.RenderArea.Height - 1)
            {
                ConsoleLocation = new Point(ConsoleLocation.X + data.TextSurface.RenderArea.Left, ConsoleLocation.Y + data.TextSurface.RenderArea.Top);
                Cell            = data.TextSurface.Cells[ConsoleLocation.Y * data.TextSurface.RenderArea.Width + ConsoleLocation.X];
                Console         = data;

                // Other console previously had mouse, we'll properly tell it that it has loss it.
                if (Engine.LastMouseConsole != data)
                {
                    if (Engine.LastMouseConsole != null)
                    {
                        var info       = this.Clone();
                        var oldConsole = Engine.LastMouseConsole;
                        Engine.LastMouseConsole = data;

                        if (oldConsole is Console)
                        {
                            ((Console)oldConsole).SkipMouseDataFill = true;
                            oldConsole.ProcessMouse(info);
                            ((Console)oldConsole).SkipMouseDataFill = false;
                        }
                        else
                        {
                            oldConsole.ProcessMouse(info);
                        }
                    }
                    else
                    {
                        Engine.LastMouseConsole = data;
                    }
                }
            }
        }
Esempio n. 6
0
        private bool MousePositionChanged()
        {
            SFML.System.Vector2i currentPos = Mouse.GetPosition();
            Vector2f             worldPos   = window.MapPixelToCoords(currentPos);

            if (currentPos.X != mousePosition.X || currentPos.Y != mousePosition.Y)
            {
                mousePosition = currentPos;
                return(true);
            }
            return(false);
        }
Esempio n. 7
0
        //public static bool operator == (MouseInfo left, MouseInfo right)
        //{
        //    return left.Console == right.Console && left.X == right.X && left.Y == right.Y;
        //}

        //public static bool operator !=(MouseInfo left, MouseInfo right)
        //{
        //    return left.Console != right.Console || left.X != right.X || left.Y != right.Y;
        //}

        /// <summary>
        /// Fills out the state of the mouse.
        /// </summary>
        /// <param name="gameTime"></param>
        public void ProcessMouse(GameTime gameTime)
        {
#if SFML
            bool leftDown  = SFML.Window.Mouse.IsButtonPressed(SFML.Window.Mouse.Button.Left);
            bool rightDown = SFML.Window.Mouse.IsButtonPressed(SFML.Window.Mouse.Button.Right);

            ScreenLocation = SFML.Window.Mouse.GetPosition(Engine.Device);
#elif MONOGAME
            MouseState currentState = Mouse.GetState();

            bool leftDown  = currentState.LeftButton == ButtonState.Pressed;
            bool rightDown = currentState.RightButton == ButtonState.Pressed;

            ScrollWheelValueChange = ScrollWheelValue - currentState.ScrollWheelValue;
            ScrollWheelValue       = currentState.ScrollWheelValue;

            ScreenLocation = new Point(currentState.X, currentState.Y);
#endif
            bool newLeftClicked  = LeftButtonDown && !leftDown;
            bool newRightClicked = RightButtonDown && !rightDown;

            if (!newLeftClicked)
            {
                LeftDoubleClicked = false;
            }
            if (!newRightClicked)
            {
                RightDoubleClicked = false;
            }

            if (LeftClicked && newLeftClicked && gameTime.ElapsedGameTime.TotalMilliseconds < 1000)
            {
                LeftDoubleClicked = true;
            }
            if (RightClicked && newRightClicked && gameTime.ElapsedGameTime.TotalMilliseconds < 1000)
            {
                RightDoubleClicked = true;
            }

            LeftClicked           = newLeftClicked;
            RightClicked          = newRightClicked;
            _leftLastClickedTime  = gameTime.ElapsedGameTime;
            _rightLastClickedTime = gameTime.ElapsedGameTime;
            LeftButtonDown        = leftDown;
            RightButtonDown       = rightDown;
        }
Esempio n. 8
0
        public bool IsOccupied(Sfs.Vector2i position, bool[,] matrix)
        {
            var result = false;

            EachBlock(position, (x, y) =>
            {
                if (x < 0 || x >= World.WorldBounds.X || y < 0 || y >= World.WorldBounds.Y)
                {
                    result = true;
                }
                else if (matrix[y, x])
                {
                    result = true;
                }
            });
            return(result);
        }
Esempio n. 9
0
        public void EachBlock(Sfs.Vector2i position, Action <int, int> action)
        {
            const int shapeMaxBound = 4;
            var       row           = 0;
            var       col           = 0;

            for (var bit = 0x8000; bit > 0; bit = bit >> 1)
            {
                if ((Shapes[Direction] & bit) != 0)
                {
                    action(position.X + col, position.Y + row);
                }

                if (++col == shapeMaxBound)
                {
                    col = 0;
                    row++;
                }
            }
        }
Esempio n. 10
0
        public World(int width, int height)
        {
            WorldBounds = new Sfs.Vector2i(width, height);
            Matrix      = new bool[height, width];
            Matrix.FillArray(false);

            var terminoFactory = new TerminoFactory();

            TerminoPrototypes = new[]
            {
                terminoFactory.Create(ETerminoName.I),
                terminoFactory.Create(ETerminoName.J),
                terminoFactory.Create(ETerminoName.L),
                terminoFactory.Create(ETerminoName.O),
                terminoFactory.Create(ETerminoName.S),
                terminoFactory.Create(ETerminoName.T),
                terminoFactory.Create(ETerminoName.Z)
            };
            CurrentPiece = NextTermino();
            WorldEnded   = false;
        }
        /// <summary>
        /// Gets the Matrix transform that positions the console on the screen.
        /// </summary>
        /// <returns>The transform.</returns>
        public virtual Matrix GetPositionTransform(Point position, Point CellSize, bool absolutePositioning)
        {
            Point worldLocation;

            if (absolutePositioning)
            {
                worldLocation = position;
            }
            else
            {
                worldLocation = position.ConsoleLocationToWorld(CellSize.X, CellSize.Y);
            }

#if SFML
            var transform = Matrix.Identity;
            transform.Translate(worldLocation.X, worldLocation.Y);

            return(transform);
#elif MONOGAME
            return(Matrix.CreateTranslation(worldLocation.X, worldLocation.Y, 0f));
#endif
        }
Esempio n. 12
0
        public SplashScreen()
            : base(80, 25)
        {
            IsVisible      = false;
            effectsManager = new EffectsManager(this.TextSurface);
            // Setup the console text background
            string textTemplate = "sole SadCon";

            System.Text.StringBuilder text = new System.Text.StringBuilder(2200);

            for (int i = 0; i < TextSurface.Width * TextSurface.Height; i++)
            {
                text.Append(textTemplate);
            }
            Print(0, 0, text.ToString(), Color.Black, Color.Transparent);

            // Load the logo
#if MONOGAME
            System.IO.Stream imageStream = System.IO.File.OpenRead("sad.png");
            var image = Texture2D.FromStream(Engine.Device, imageStream);
            imageStream.Dispose();
#elif SFML
            var image = new Texture("sad.png");
#endif
            // Configure the logo
            _consoleImage         = image.ToSurface(Engine.DefaultFont, false);
            _consoleImagePosition = new Point(TextSurface.Width / 2 - _consoleImage.Width / 2, -1);
            _consoleImage.Tint    = Color.Black;

            // Configure the animations
            _animation = new InstructionSet();
            _animation.Instructions.AddLast(new Wait()
            {
                Duration = 0.3f
            });

            // Animation to move the angled gradient spotlight effect.
            var moveGradientInstruction = new CodeInstruction();
            moveGradientInstruction.CodeCallback = (inst) =>
            {
                _x += 1;

                if (_x > TextSurface.Width + 50)
                {
                    inst.IsFinished = true;
                }

                Color[] colors     = new Color[] { Color.Black, Color.Blue, Color.White, Color.Blue, Color.Black };
                float[] colorStops = new float[] { 0f, 0.2f, 0.5f, 0.8f, 1f };

                Algorithms.GradientFill(TextSurface.Font.Size, new Point(_x, 12), 10, 45, new Rectangle(0, 0, TextSurface.Width, TextSurface.Height), new ColorGradient(colors, colorStops), SetForeground);
            };
            _animation.Instructions.AddLast(moveGradientInstruction);

            // Animation to clear the SadConsole text.
            _animation.Instructions.AddLast(new CodeInstruction()
            {
                CodeCallback = (i) => { Fill(Color.Black, Color.Transparent, 0, null); i.IsFinished = true; }
            });

            // Animation for the logo text.
            var logoText = new ColorGradient(new Color[] { Color.Magenta, Color.Yellow }, new float[] { 0.0f, 1f }).ToColoredString("[| Powered by SadConsole |]");
            logoText.SetEffect(new SadConsole.Effects.Fade()
            {
                DestinationForeground = Color.Blue, FadeForeground = true, FadeDuration = 1f, Repeat = false, RemoveOnFinished = true, Permanent = true, CloneOnApply = true
            });
            _animation.Instructions.AddLast(new DrawString(this)
            {
                Position = new Point(26, this.TextSurface.Height - 1), Text = logoText, TotalTimeToPrint = 1f
            });

            // Animation for fading in the logo picture.
            _animation.Instructions.AddLast(new FadeTextSurfaceTint(_consoleImage, new ColorGradient(Color.Black, Color.Transparent), new TimeSpan(0, 0, 0, 0, 2000)));

            // Animation to blink SadConsole in the logo text
            _animation.Instructions.AddLast(new CodeInstruction()
            {
                CodeCallback = (i) =>
                {
                    SadConsole.Effects.Fade fadeEffect = new SadConsole.Effects.Fade();
                    fadeEffect.AutoReverse             = true;
                    fadeEffect.DestinationForeground   = new ColorGradient(Color.Blue, Color.Yellow);
                    fadeEffect.FadeForeground          = true;
                    fadeEffect.UseCellForeground       = false;
                    fadeEffect.Repeat           = true;
                    fadeEffect.FadeDuration     = 0.7f;
                    fadeEffect.RemoveOnFinished = true;

                    List <Cell> cells = new List <Cell>();
                    for (int index = 0; index < 10; index++)
                    {
                        var point = new Point(26, this.TextSurface.Height - 1).ToIndex(this.TextSurface.Width) + 14 + index;
                        cells.Add(textSurface.Cells[point]);
                        cellindexes.Add(point);
                    }

                    effectsManager.SetEffect(cells, fadeEffect);
                    i.IsFinished = true;
                }
            });

            // Animation to delay, keeping the logo and all on there for 2 seconds, then destroy itself.
            _animation.Instructions.AddLast(new Wait()
            {
                Duration = 2.5f
            });
            _animation.Instructions.AddLast(new FadeTextSurfaceTint(_consoleImage, new ColorGradient(Color.Transparent, Color.Black), new TimeSpan(0, 0, 0, 0, 2000)));
            _animation.Instructions.AddLast(new CodeInstruction()
            {
                CodeCallback = (i) =>
                {
                    SplashCompleted?.Invoke();

                    i.IsFinished = true;
                }
            });
        }
Esempio n. 13
0
 // Vector2i
 public static int LengthSquared(this SFML.System.Vector2i vec) => vec.X * vec.X + vec.Y * vec.Y;
Esempio n. 14
0
 public static Vector2f ToFloat(this SFML.System.Vector2i vec) => new Vector2f(vec.X, vec.Y);
Esempio n. 15
0
 public static float Length(this SFML.System.Vector2i vec) => (float)Math.Sqrt(LengthSquared(vec));
        /// <summary>
        /// Renders a surface to the screen.
        /// </summary>
        /// <param name="surface">The surface to render.</param>
        /// <param name="renderingMatrix">Display matrix for the rendered console.</param>
        public virtual void Render(ITextSurfaceRendered surface, Matrix renderingMatrix)
        {
#if SFML
            Batch.Reset(Engine.Device, RenderStates.Default, renderingMatrix);

            BeforeRenderCallback?.Invoke(Batch);

            if (surface.Tint.A != 255)
            {
                Cell cell;

                if (surface.DefaultBackground.A != 0)
                {
                    Batch.DrawQuad(surface.AbsoluteArea, surface.Font.SolidGlyphRectangle, surface.DefaultBackground, surface.Font.FontImage);
                }

                for (int i = 0; i < surface.RenderCells.Length; i++)
                {
                    cell = surface.RenderCells[i];

                    if (cell.IsVisible)
                    {
                        Batch.DrawCell(cell, surface.RenderRects[i], surface.Font.SolidGlyphRectangle, surface.DefaultBackground, surface.Font);
                    }
                }

                #region Control Render
                int     cellCount;
                IntRect rect;
                Point   point;
                Controls.ControlBase control;

                // For each control
                for (int i = 0; i < Controls.Count; i++)
                {
                    if (Controls[i].IsVisible)
                    {
                        control   = Controls[i];
                        cellCount = control.TextSurface.Cells.Length;

                        var font = control.AlternateFont == null ? surface.Font : control.AlternateFont;

                        // Draw background of each cell for the control
                        for (int cellIndex = 0; cellIndex < cellCount; cellIndex++)
                        {
                            cell = control[cellIndex];

                            if (cell.IsVisible)
                            {
                                point = Consoles.TextSurface.GetPointFromIndex(cellIndex, control.TextSurface.Width);
                                point = new Point(point.X + control.Position.X, point.Y + control.Position.Y);

                                if (surface.RenderArea.Contains(point.X, point.Y))
                                {
                                    point = new Point(point.X - surface.RenderArea.Left, point.Y - surface.RenderArea.Top);
                                    rect  = surface.RenderRects[Consoles.TextSurface.GetIndexFromPoint(point, surface.Width)];

                                    Batch.DrawCell(cell, rect, font.SolidGlyphRectangle, Color.Transparent, font);
                                }
                            }
                        }
                    }
                }


                #endregion
            }
            AfterRenderCallback?.Invoke(Batch);

            if (surface.Tint.A != 0)
            {
                Batch.DrawQuad(surface.AbsoluteArea, surface.Font.SolidGlyphRectangle, surface.Tint, surface.Font.FontImage);
            }

            if (CallBatchEnd)
            {
                Batch.End();
            }
#elif MONOGAME
            Batch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.DepthRead, RasterizerState.CullNone, null, renderingMatrix);

            BeforeRenderCallback?.Invoke(Batch);

            if (surface.Tint.A != 255)
            {
                Cell cell;


                if (surface.DefaultBackground.A != 0)
                {
                    Batch.Draw(surface.Font.FontImage, surface.AbsoluteArea, surface.Font.GlyphIndexRects[surface.Font.SolidGlyphIndex], surface.DefaultBackground, 0f, PrimitiveStatic.Vector2Zero, SpriteEffects.None, 0.2f);
                }

                for (int i = 0; i < surface.RenderCells.Length; i++)
                {
                    cell = surface.RenderCells[i];

                    if (cell.IsVisible)
                    {
                        if (cell.ActualBackground != Color.Transparent && cell.ActualBackground != surface.DefaultBackground)
                        {
                            Batch.Draw(surface.Font.FontImage, surface.RenderRects[i], surface.Font.GlyphIndexRects[surface.Font.SolidGlyphIndex], cell.ActualBackground, 0f, Vector2.Zero, SpriteEffects.None, 0.3f);
                        }

                        if (cell.ActualForeground != Color.Transparent)
                        {
                            Batch.Draw(surface.Font.FontImage, surface.RenderRects[i], surface.Font.GlyphIndexRects[cell.ActualGlyphIndex], cell.ActualForeground, 0f, Vector2.Zero, cell.ActualSpriteEffect, 0.4f);
                        }
                    }
                }

                #region Control Render


                int                  cellCount;
                Rectangle            rect;
                Point                point;
                Controls.ControlBase control;

                // For each control
                for (int i = 0; i < Controls.Count; i++)
                {
                    if (Controls[i].IsVisible)
                    {
                        control   = Controls[i];
                        cellCount = control.TextSurface.Cells.Length;

                        var font = control.AlternateFont == null ? surface.Font : control.AlternateFont;

                        // Draw background of each cell for the control
                        for (int cellIndex = 0; cellIndex < cellCount; cellIndex++)
                        {
                            cell = control[cellIndex];

                            if (cell.IsVisible)
                            {
                                point = Consoles.TextSurface.GetPointFromIndex(cellIndex, control.TextSurface.Width);
                                point = new Point(point.X + control.Position.X, point.Y + control.Position.Y);

                                if (surface.RenderArea.Contains(point.X, point.Y))
                                {
                                    point = new Point(point.X - surface.RenderArea.Left, point.Y - surface.RenderArea.Top);
                                    rect  = surface.RenderRects[Consoles.TextSurface.GetIndexFromPoint(point, surface.Width)];

                                    if (cell.ActualBackground != Color.Transparent)
                                    {
                                        Batch.Draw(font.FontImage, rect, font.GlyphIndexRects[font.SolidGlyphIndex], cell.ActualBackground, 0f, Vector2.Zero, SpriteEffects.None, 0.23f);
                                    }
                                    if (cell.ActualForeground != Color.Transparent)
                                    {
                                        Batch.Draw(font.FontImage, rect, font.GlyphIndexRects[cell.ActualGlyphIndex], cell.ActualForeground, 0f, Vector2.Zero, cell.ActualSpriteEffect, 0.26f);
                                    }
                                }
                            }
                        }
                    }
                }


                #endregion

                if (surface.Tint.A != 0)
                {
                    Batch.Draw(surface.Font.FontImage, surface.AbsoluteArea, surface.Font.GlyphIndexRects[surface.Font.SolidGlyphIndex], surface.Tint, 0f, Vector2.Zero, SpriteEffects.None, 0.5f);
                }
            }
            else
            {
                Batch.Draw(surface.Font.FontImage, surface.AbsoluteArea, surface.Font.GlyphIndexRects[surface.Font.SolidGlyphIndex], surface.Tint, 0f, Vector2.Zero, SpriteEffects.None, 0.5f);
            }

            AfterRenderCallback?.Invoke(Batch);

            if (CallBatchEnd)
            {
                Batch.End();
            }
#endif
        }