Example #1
0
        public static void DrawSpawn(MapSpawnValues spawn, ISpriteBatch spriteBatch, IDrawableMap map, Font font)
        {
            var spawnArea = spawn.SpawnArea;

            // Only draw if it does not cover the whole map
            if (!spawnArea.X.HasValue && !spawnArea.Y.HasValue && !spawnArea.Width.HasValue && !spawnArea.Height.HasValue)
                return;

            // Draw spawn area
            Vector2 cameraOffset = map.Camera.Min;
            Rectangle rect = spawnArea.ToRectangle(map);
            rect.X -= (int)cameraOffset.X;
            rect.Y -= (int)cameraOffset.Y;
            RenderRectangle.Draw(spriteBatch, rect, new Color(0, 255, 0, 75), new Color(0, 0, 0, 125), 2);

            // Draw name
            CharacterTemplate charTemp = CharacterTemplateManager.Instance[spawn.CharacterTemplateID];
            if (charTemp != null)
            {
                string text = string.Format("{0}x {1} [{2}]", spawn.SpawnAmount, charTemp.TemplateTable.Name, spawn.CharacterTemplateID);

                Vector2 textPos = new Vector2(rect.X, rect.Y);
                textPos -= new Vector2(0, font.MeasureString(text).Y);
                textPos = textPos.Round();

                spriteBatch.DrawStringShaded(font, text, textPos, Color.White, Color.Black);
            }
        }
Example #2
0
 public static void DrawChatLines(SpriteBatch spriteBatch, SpriteFont font, IEnumerable<WrappedTextList.Line> messageLines, Vector2 textPos)
 {
     foreach (var line in messageLines)
     {
         if (line.ContainsPretext)
         {
             var splitIndex = line.Text.IndexOf('>');
             if (splitIndex < 0) throw new ApplicationException("Pretext char not found");
             var pretext = line.Text.Substring(0, splitIndex + 1);
             var properText = line.Text.Substring(splitIndex + 1);
             ModelRenderer.DrawBorderedText(spriteBatch, font, pretext, textPos.Round(), AW2.Game.PlayerMessage.PRETEXT_COLOR, 1, 1);
             var properPos = textPos + new Vector2(font.MeasureString(pretext).X, 0);
             ModelRenderer.DrawBorderedText(spriteBatch, font, properText, properPos.Round(), line.Color, 1, 1);
         }
         else
             ModelRenderer.DrawBorderedText(spriteBatch, font, line.Text, textPos.Round(), line.Color, 1, 1);
         textPos.Y += font.LineSpacing;
     }
 }
Example #3
0
 private void DrawStaticText()
 {
     _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
     var versionText = "Assault Wing " + MiscHelper.Version;
     _spriteBatch.DrawString(MenuContent.FontSmall, versionText,
         new Vector2(10, ViewportHeight - MenuContent.FontSmall.LineSpacing).Round(), Color.White);
     if (IsHelpTextVisible)
     {
         var helpTextPos = new Vector2(
             (int)(((float)ViewportWidth - MenuContent.FontSmall.MeasureString(ActiveComponent.HelpText).X) / 2),
             ViewportHeight - MenuContent.FontSmall.LineSpacing);
         _spriteBatch.DrawString(MenuContent.FontSmall, ActiveComponent.HelpText, helpTextPos.Round(), Color.White);
     }
     var copyrightText = "Studfarm Studios";
     var copyrightTextPos = new Vector2(
         ViewportWidth - (int)MenuContent.FontSmall.MeasureString(copyrightText).X - 10,
         ViewportHeight - MenuContent.FontSmall.LineSpacing);
     _spriteBatch.DrawString(MenuContent.FontSmall, copyrightText, copyrightTextPos.Round(), Color.White);
     _spriteBatch.End();
 }
Example #4
0
        private void DrawArenaInfo(Vector2 view, SpriteBatch spriteBatch)
        {
            var infoDisplayPos = MenuComponent.Pos - view + new Vector2(595, 220);
            var currentPos = infoDisplayPos;
            var lineHeight = new Vector2(0, 20);
            var infoWidth = new Vector2(320, 0);
            var arenaName = MenuEngine.Game.SelectedArenaName;
            var arenaInfo = ((AW2.Game.Arena)MenuEngine.Game.DataEngine.GetTypeTemplate((CanonicalString)arenaName)).Info;
            var content = MenuEngine.Game.Content;
            string previewName = content.Exists<Texture2D>(arenaInfo.PreviewName) ? arenaInfo.PreviewName : "no_preview";
            var previewTexture = content.Load<Texture2D>(previewName);

            spriteBatch.DrawString(Content.FontBig, "Arena info", currentPos.Round(), Color.White);
            currentPos += new Vector2(0, 50);
            spriteBatch.DrawString(Content.FontSmall, "Current arena:", currentPos.Round(), Color.White);
            spriteBatch.DrawString(Content.FontSmall, arenaName, (currentPos + new Vector2(Content.FontSmall.MeasureString("Current arena:  ").X, 0)).Round(), Color.GreenYellow);
            if (IsArenaSelectable) spriteBatch.DrawString(Content.FontSmall, "Press Enter to change.", (currentPos + infoWidth + new Vector2(10, 0)).Round(), new Color(218, 159, 33));
            currentPos += new Vector2(0, Content.FontSmall.LineSpacing);
            spriteBatch.Draw(previewTexture, currentPos, null, Color.White, 0, new Vector2(0, 0), 0.6f, SpriteEffects.None, 0);
        }
        private void RenderWords(
            RendererParameters parameters,
            SpriteFont font,
            Matrix transformMatrix,
            int lineIndex,
            IList<MessageTextWord> words,
            Color shadowColor,
            ref Vector2 position,
            ref Color textColor)
        {
            parameters.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, new ScissoringRasterizerState(), null, transformMatrix);

            MessageTextAlignment alignment = _formatter.GetAlignmentByLine(lineIndex);
            Vector2 lineSize = _formatter.GetLineSizeByLine(lineIndex);

            if (alignment == MessageTextAlignment.Center)
            {
                position.X += (Window.AbsoluteClientRectangle.Width - lineSize.X) / 2;
            }

            for (int wordIndex = 0; wordIndex < words.Count; wordIndex++)
            {
                MessageTextWord word = words[wordIndex];
                Engine.Common.Color color;

                if (_formatter.TryGetColorByWordCoordinate(new Coordinate(wordIndex, lineIndex), out color))
                {
                    textColor = color.ToXnaColor() * Alpha;
                }
                if (word.PrependSpace)
                {
                    position.X += _formatter.SpaceWord.Size.X;
                }

                parameters.SpriteBatch.DrawStringWithShadow(font, word.Text, position.Round(), textColor, shadowColor, Vector2.One);
                position.X += word.Size.X;
            }

            position.X = Window.AbsoluteClientRectangle.X;
            position.Y += lineSize.Y;

            parameters.SpriteBatch.End();
        }
        private void RenderAnswers(
            RendererParameters parameters,
            WindowTexture selectedAnswerWindowTexture,
            SpriteFont font,
            Matrix transformMatrix,
            IEnumerable<MessageTextAnswer> answers,
            Color shadowColor,
            Vector2 position)
        {
            answers = answers.ToArray();

            int answerCount = answers.Count();
            int totalAnswerPadding = ((answerCount - 1) * TextAdventure.Xna.Constants.MessageRenderer.AnswerHorizontalPadding);
            float lineWidth = answers.Sum(arg => arg.Size.X + (selectedAnswerWindowTexture.SpriteWidth * 2) + (TextAdventure.Xna.Constants.MessageRenderer.AnswerHorizontalTextPadding * 2)) + totalAnswerPadding;
            float lineHeight = answers.Max(arg => arg.Size.Y);

            position.X += (Window.AbsoluteClientRectangle.Width - lineWidth) / 2;

            foreach (MessageTextAnswer answer in answers)
            {
                var window = new BorderedWindow(
                    new Rectangle(
                        position.X.Round(),
                        position.Y.Round(),
                        (answer.Size.X + (selectedAnswerWindowTexture.SpriteWidth * 2) + (TextAdventure.Xna.Constants.MessageRenderer.AnswerHorizontalTextPadding * 2)).Round(),
                        (lineHeight + (selectedAnswerWindowTexture.SpriteHeight * 2)).Round()),
                    selectedAnswerWindowTexture.Padding);

                if (answer.Answer == _answerSelectionManager.SelectedAnswer)
                {
                    _messageAnswerRenderer.Update(
                        window.WindowRectangle,
                        selectedAnswerWindowTexture.Padding,
                        answer.SelectedAnswerBackgroundColor.ToXnaColor(),
                        Alpha,
                        Window.AbsoluteClientRectangle,
                        transformMatrix);
                    _messageAnswerRenderer.Render(parameters);
                }

                var textPosition = new Vector2(
                    window.AbsoluteClientRectangle.X + ((window.AbsoluteClientRectangle.Width - answer.Size.X) / 2),
                    window.AbsoluteClientRectangle.Y + ((window.AbsoluteClientRectangle.Height - lineHeight) / 2));
                Color textColor = answer.Answer == _answerSelectionManager.SelectedAnswer ? answer.SelectedAnswerForegroundColor.ToXnaColor() : answer.UnselectedAnswerForegroundColor.ToXnaColor();

                parameters.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.None, new ScissoringRasterizerState(), null, transformMatrix);

                parameters.SpriteBatch.DrawStringWithShadow(
                    font,
                    answer.Text,
                    textPosition.Round(),
                    textColor * Alpha,
                    shadowColor,
                    Vector2.One);

                parameters.SpriteBatch.End();

                position.X += window.WindowRectangle.Width + TextAdventure.Xna.Constants.MessageRenderer.AnswerHorizontalPadding;
            }
        }
Example #7
0
 public override void Draw2D(Matrix gameToScreen, SpriteBatch spriteBatch, float scale, Player viewer)
 {
     // Draw player name
     if (Owner == null || Owner.IsLocal) return;
     var screenPos = Vector2.Transform(Pos + DrawPosOffset, gameToScreen);
     var playerNameSize = PlayerNameFont.MeasureString(Owner.Name);
     var playerNamePos = new Vector2(screenPos.X - playerNameSize.X / 2, screenPos.Y + 35);
     var nameAlpha = (IsHiding ? Alpha : 1) * 0.8f;
     var nameColor = Color.Multiply(Owner.Color, nameAlpha);
     spriteBatch.DrawString(PlayerNameFont, Owner.Name, playerNamePos.Round(), nameColor);
 }
Example #8
0
        public void update(GameTime gt)
        {
            if (level == null) return;

            oldkstate = kstate;
            oldmstate = mstate;
            kstate = Keyboard.GetState();
            mstate = Mouse.GetState();
            int mwheeldelta = mstate.ScrollWheelValue - oldmstate.ScrollWheelValue;
            if (mwheeldelta > 0 /* && kstate.IsKeyDown(Keys.LeftControl)*/)
            {
                float zoom = (float)Math.Round(camera.Scale * 10) * 10.0f + 10.0f;
                MainForm.Instance.zoomcombo.Text = zoom.ToString() + "%";
                camera.Scale = zoom / 100.0f;
            }
            if (mwheeldelta < 0 /* && kstate.IsKeyDown(Keys.LeftControl)*/)
            {
                float zoom = (float)Math.Round(camera.Scale * 10) * 10.0f - 10.0f;
                if (zoom <= 0.0f) return;
                MainForm.Instance.zoomcombo.Text = zoom.ToString() + "%";
                camera.Scale = zoom / 100.0f;
            }

            //Camera movement
            float delta;
            if (kstate.IsKeyDown(Keys.LeftShift)) delta = Constants.Instance.CameraFastSpeed * (float)gt.ElapsedGameTime.TotalSeconds;
                else delta = Constants.Instance.CameraSpeed * (float)gt.ElapsedGameTime.TotalSeconds;
            if (kstate.IsKeyDown(Keys.W) && kstate.IsKeyUp(Keys.LeftControl)) camera.Position += (new Vector2(0, -delta));
            if (kstate.IsKeyDown(Keys.S) && kstate.IsKeyUp(Keys.LeftControl)) camera.Position += (new Vector2(0, +delta));
            if (kstate.IsKeyDown(Keys.A) && kstate.IsKeyUp(Keys.LeftControl)) camera.Position += (new Vector2(-delta, 0));
            if (kstate.IsKeyDown(Keys.D) && kstate.IsKeyUp(Keys.LeftControl)) camera.Position += (new Vector2(+delta, 0));

            if (kstate.IsKeyDown(Keys.Subtract))
            {
                float zoom = (float)(camera.Scale * 0.995);
                MainForm.Instance.zoomcombo.Text = (zoom * 100).ToString("###0.0") + "%";
                camera.Scale = zoom;
            }
            if (kstate.IsKeyDown(Keys.Add))
            {
                float zoom = (float)(camera.Scale * 1.005);
                MainForm.Instance.zoomcombo.Text = (zoom * 100).ToString("###0.0") + "%";
                camera.Scale = zoom;
            }

            //get mouse world position considering the ScrollSpeed of the current layer
            Vector2 maincameraposition = camera.Position;
            if (SelectedLayer != null) camera.Position *= SelectedLayer.ScrollSpeed;
            mouseworldpos = Vector2.Transform(new Vector2(mstate.X, mstate.Y), Matrix.Invert(camera.matrix));
            mouseworldpos = mouseworldpos.Round();
            MainForm.Instance.toolStripStatusLabel3.Text = "Mouse: (" + mouseworldpos.X + ", " + mouseworldpos.Y + ")";
            camera.Position = maincameraposition;

            if (state == EditorState.idle)
            {
                //get item under mouse cursor
                Item item = getItemAtPos(mouseworldpos);
                if (item != null)
                {
                    MainForm.Instance.toolStripStatusLabel1.Text = item.Name;
                    item.onMouseOver(mouseworldpos);
                    if (kstate.IsKeyDown(Keys.LeftControl)) MainForm.Instance.pictureBox1.Cursor = cursorDup;
                }
                else
                {
                    MainForm.Instance.toolStripStatusLabel1.Text = "";
                }
                if (item != lastitem && lastitem != null) lastitem.onMouseOut();
                lastitem = item;

                //LEFT MOUSE BUTTON CLICK
                if ((mstate.LeftButton == ButtonState.Pressed && oldmstate.LeftButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D1) && oldkstate.IsKeyUp(Keys.D1)))
                {
                    if (item != null) item.onMouseButtonDown(mouseworldpos);
                    if (kstate.IsKeyDown(Keys.LeftControl) && item != null)
                    {
                        if (!SelectedItems.Contains(item)) selectitem(item);

                        beginCommand("Add Item(s)");

                        List<Item> selecteditemscopy = new List<Item>();
                        foreach (Item selitem in SelectedItems)
                        {
                            Item i2 = (Item)selitem.clone();
                            selecteditemscopy.Add(i2);
                        }
                        foreach (Item selitem in selecteditemscopy)
                        {
                            selitem.Name = selitem.getNamePrefix() + level.getNextItemNumber();
                            addItem(selitem);
                        }
                        selectitem(selecteditemscopy[0]);
                        updatetreeview();
                        for (int i = 1; i < selecteditemscopy.Count; i++) SelectedItems.Add(selecteditemscopy[i]);
                        startMoving();
                    }
                    else if (kstate.IsKeyDown(Keys.LeftShift) && item != null)
                    {
                        if (SelectedItems.Contains(item)) SelectedItems.Remove(item);
                        else SelectedItems.Add(item);
                    }
                    else if (SelectedItems.Contains(item))
                    {
                        beginCommand("Change Item(s)");
                        startMoving();
                    }
                    else if (!SelectedItems.Contains(item))
                    {
                        selectitem(item);
                        if (item != null)
                        {
                            beginCommand("Change Item(s)");
                            startMoving();
                        }
                        else
                        {
                            grabbedpoint = mouseworldpos;
                            selectionrectangle = Rectangle.Empty;
                            state = EditorState.selecting;
                        }

                    }
                }

                //MIDDLE MOUSE BUTTON CLICK
                if ((mstate.MiddleButton == ButtonState.Pressed && oldmstate.MiddleButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(AdesExtensions.KEY_ROTATE) && oldkstate.IsKeyUp(AdesExtensions.KEY_ROTATE)))
                {
                    if (item != null) item.onMouseOut();
                    if (kstate.IsKeyDown(Keys.LeftControl))
                    {
                        grabbedpoint = new Vector2(mstate.X, mstate.Y);
                        initialcampos = camera.Position;
                        state = EditorState.cameramoving;
                        MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.SizeAll;
                    }
                    else
                    {
                        if (SelectedItems.Count > 0)
                        {
                            grabbedpoint = mouseworldpos - SelectedItems[0].pPosition;

                            //save the initial rotation for each item
                            initialrot.Clear();
                            foreach (Item selitem in SelectedItems)
                            {
                                if (selitem.CanRotate())
                                {
                                    initialrot.Add(selitem.getRotation());
                                }
                            }

                            state = EditorState.rotating;
                            MainForm.Instance.pictureBox1.Cursor = cursorRot;

                            beginCommand("Rotate Item(s)");
                        }
                    }
                }

                //RIGHT MOUSE BUTTON CLICK
                if ((mstate.RightButton == ButtonState.Pressed && oldmstate.RightButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D3) && oldkstate.IsKeyUp(Keys.D3)))

                {
                    if (item != null) item.onMouseOut();
                    if (SelectedItems.Count > 0)
                    {
                        grabbedpoint = mouseworldpos - SelectedItems[0].pPosition;

                        //save the initial scale for each item
                        initialscale.Clear();
                        foreach (Item selitem in SelectedItems)
                        {
                            if (selitem.CanScale())
                            {
                                initialscale.Add(selitem.getScale());
                            }
                        }

                        state = EditorState.scaling;
                        MainForm.Instance.pictureBox1.Cursor = cursorScale;

                        beginCommand("Scale Item(s)");
                    }
                }

                if (kstate.IsKeyDown(Keys.H) && oldkstate.GetPressedKeys().Length == 0 && SelectedItems.Count > 0)
                {
                    beginCommand("Flip Item(s) Horizontally");
                    foreach (Item selitem in SelectedItems)
                    {
                        if (selitem is TextureItem)
                        {
                            TextureItem ti = (TextureItem)selitem;
                            ti.FlipHorizontally = !ti.FlipHorizontally;
                        }
                    }
                    MainForm.Instance.propertyGrid1.Refresh();
                    endCommand();
                }
                if (kstate.IsKeyDown(Keys.V) && oldkstate.GetPressedKeys().Length == 0 && SelectedItems.Count > 0)
                {
                    beginCommand("Flip Item(s) Vertically");
                    foreach (Item selitem in SelectedItems)
                    {
                        if (selitem is TextureItem)
                        {
                            TextureItem ti = (TextureItem)selitem;
                            ti.FlipVertically = !ti.FlipVertically;
                        }
                    }
                    MainForm.Instance.propertyGrid1.Refresh();
                    endCommand();
                }
            }

            if (state == EditorState.moving)
            {
                int i = 0;
                foreach (Item selitem in SelectedItems)
                {
                    newPosition = initialpos[i] + mouseworldpos - grabbedpoint;
                    if (AdesExtensions.shouldSnapPosition(kstate) || kstate.IsKeyDown(Keys.G)) newPosition = snapToGrid(newPosition);
                    drawSnappedPoint = false;
                    selitem.setPosition(newPosition);
                    i++;
                }
                MainForm.Instance.propertyGrid1.Refresh();
                if ((mstate.LeftButton == ButtonState.Released && oldmstate.LeftButton == ButtonState.Pressed) ||
                    (kstate.IsKeyUp(Keys.D1) && oldkstate.IsKeyDown(Keys.D1)))
                {

                    foreach (Item selitem in SelectedItems) selitem.onMouseButtonUp(mouseworldpos);

                    state = EditorState.idle;
                    MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.Default;
                    if (mouseworldpos != grabbedpoint) endCommand(); else abortCommand();
                }
            }

            if (state == EditorState.rotating)
            {
                Vector2 newpos = mouseworldpos - SelectedItems[0].pPosition;
                float deltatheta = (float)Math.Atan2(grabbedpoint.Y, grabbedpoint.X) - (float)Math.Atan2(newpos.Y, newpos.X);
                int i = 0;
                foreach (Item selitem in SelectedItems)
                {
                    if (selitem.CanRotate())
                    {
                        selitem.setRotation(initialrot[i] - deltatheta);
                        if (AdesExtensions.shouldSnapRotation(kstate))
                        {
                            selitem.setRotation((float)Math.Round(selitem.getRotation() / MathHelper.PiOver4) * MathHelper.PiOver4);
                        }
                        i++;
                    }
                }
                MainForm.Instance.propertyGrid1.Refresh();
                if ((mstate.MiddleButton == ButtonState.Released && oldmstate.MiddleButton == ButtonState.Pressed) ||
                    (kstate.IsKeyUp(AdesExtensions.KEY_ROTATE) && oldkstate.IsKeyDown(AdesExtensions.KEY_ROTATE)))
                {
                    state = EditorState.idle;
                    MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.Default;
                    endCommand();
                }
            }

            if (state == EditorState.scaling)
            {
                Vector2 newdistance = mouseworldpos - SelectedItems[0].pPosition;
                float factor = newdistance.Length() / grabbedpoint.Length();
                int i = 0;
                foreach (Item selitem in SelectedItems)
                {
                    if (selitem.CanScale())
                    {
                        if (selitem is TextureItem)
                        {
                            MainForm.Instance.toolStripStatusLabel1.Text = "Hold down [X] or [Y] to limit scaling to the according dimension.";
                        }

                        Vector2 newscale = initialscale[i];
                        if (!kstate.IsKeyDown(Keys.Y)) newscale.X = initialscale[i].X * (((factor - 1.0f) * 0.5f) + 1.0f);
                        if (!kstate.IsKeyDown(Keys.X)) newscale.Y = initialscale[i].Y * (((factor - 1.0f) * 0.5f) + 1.0f);
                        selitem.setScale(newscale);

                        if (kstate.IsKeyDown(Keys.LeftControl))
                        {
                            Vector2 scale;
                            scale.X = (float)Math.Round(selitem.getScale().X * 10) / 10;
                            scale.Y = (float)Math.Round(selitem.getScale().Y * 10) / 10;
                            selitem.setScale(scale);
                        }
                        i++;
                    }
                }
                MainForm.Instance.propertyGrid1.Refresh();
                if ((mstate.RightButton == ButtonState.Released && oldmstate.RightButton == ButtonState.Pressed) ||
                    (kstate.IsKeyUp(Keys.D3) && oldkstate.IsKeyDown(Keys.D3)))
                {
                    state = EditorState.idle;
                    MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.Default;
                    endCommand();
                }
            }

            if (state == EditorState.cameramoving)
            {
                Vector2 newpos = new Vector2(mstate.X, mstate.Y);
                Vector2 distance = (newpos - grabbedpoint) / camera.Scale;
                if (distance.Length() > 0)
                {
                    camera.Position = initialcampos - distance;
                }
                if (mstate.MiddleButton == ButtonState.Released)
                {
                    state = EditorState.idle;
                    MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.Default;
                }
            }

            if (state == EditorState.selecting)
            {
                if (SelectedLayer == null) return;
                Vector2 distance = mouseworldpos - grabbedpoint;
                if (distance.Length() > 0)
                {
                    SelectedItems.Clear();
                    selectionrectangle = Extensions.RectangleFromVectors(grabbedpoint, mouseworldpos);
                    foreach (Item i in SelectedLayer.Items)
                    {
                        if (i.Visible && selectionrectangle.Contains((int)i.pPosition.X, (int)i.pPosition.Y)) SelectedItems.Add(i);
                    }
                    updatetreeviewselection();
                }
                if (mstate.LeftButton == ButtonState.Released)
                {
                    state = EditorState.idle;
                    MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.Default;
                }
            }

            if (state == EditorState.brush)
            {
                if (Constants.Instance.SnapToGrid || kstate.IsKeyDown(Keys.G))
                {
                    mouseworldpos = snapToGrid(mouseworldpos);
                }
                if (mstate.RightButton == ButtonState.Pressed && oldmstate.RightButton == ButtonState.Released) state = EditorState.idle;
                if (mstate.LeftButton == ButtonState.Pressed && oldmstate.LeftButton == ButtonState.Released) paintTextureBrush(true);
            }

            if (state == EditorState.brush_primitive)
            {

                if (Constants.Instance.SnapToGrid || kstate.IsKeyDown(Keys.G)) mouseworldpos = snapToGrid(mouseworldpos);

                if (kstate.IsKeyDown(Keys.LeftControl) && primitivestarted && currentprimitive == PrimitiveType.Rectangle)
                {
                    Vector2 distance = mouseworldpos - clickedPoints[0];
                    float squareside = Math.Max(distance.X, distance.Y);
                    mouseworldpos = clickedPoints[0] + new Vector2(squareside, squareside);
                }
                if ((mstate.LeftButton == ButtonState.Pressed && oldmstate.LeftButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D1) && oldkstate.IsKeyUp(Keys.D1)))
                {
                    clickedPoints.Add(mouseworldpos);
                    if (primitivestarted == false)
                    {
                        primitivestarted = true;
                        switch (currentprimitive)
                        {
                            case PrimitiveType.Rectangle:
                                MainForm.Instance.toolStripStatusLabel1.Text = Resources.Rectangle_Started;
                                break;
                            case PrimitiveType.Circle:
                                MainForm.Instance.toolStripStatusLabel1.Text = Resources.Circle_Started;
                                break;
                            case PrimitiveType.Path:
                                MainForm.Instance.toolStripStatusLabel1.Text = Resources.Path_Started;
                                break;
                        }
                    }
                    else
                    {
                        if (currentprimitive != PrimitiveType.Path)
                        {
                            paintPrimitiveBrush();
                            clickedPoints.Clear();
                            primitivestarted = false;
                        }
                    }
                }
                if (kstate.IsKeyDown(Keys.Back) && oldkstate.IsKeyUp(Keys.Back))
                {
                    if (currentprimitive == PrimitiveType.Path && clickedPoints.Count > 1)
                    {
                        clickedPoints.RemoveAt(clickedPoints.Count-1);
                    }
                }

                if ((mstate.MiddleButton == ButtonState.Pressed && oldmstate.MiddleButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D2) && oldkstate.IsKeyUp(Keys.D2)))
                {
                    if (currentprimitive == PrimitiveType.Path && primitivestarted)
                    {
                        paintPrimitiveBrush();
                        clickedPoints.Clear();
                        primitivestarted = false;
                        MainForm.Instance.toolStripStatusLabel1.Text = Resources.Path_Entered;
                    }
                }
                if ((mstate.RightButton == ButtonState.Pressed && oldmstate.RightButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D3) && oldkstate.IsKeyUp(Keys.D3)))
                {
                    if (primitivestarted)
                    {
                        clickedPoints.Clear();
                        primitivestarted = false;
                        switch (currentprimitive)
                        {
                            case PrimitiveType.Rectangle:
                                MainForm.Instance.toolStripStatusLabel1.Text = Resources.Rectangle_Entered;
                                break;
                            case PrimitiveType.Circle:
                                MainForm.Instance.toolStripStatusLabel1.Text = Resources.Circle_Entered;
                                break;
                            case PrimitiveType.Path:
                                MainForm.Instance.toolStripStatusLabel1.Text = Resources.Path_Entered;
                                break;
                        }
                    }
                    else
                    {
                        destroyPrimitiveBrush();
                        clickedPoints.Clear();
                        primitivestarted = false;
                    }
                }
            }
        }
Example #9
0
 static void adjustWorldPosition( Camera camera )
 {
     _mouseWorldPos = Vector2.Transform( ScreenPosition, Matrix.Invert( camera.Matrix ) ) ;
     _mouseWorldPos = _mouseWorldPos.Round( ) ;
 }
Example #10
0
        public void RoundTest()
        {
            const int max = 1000;

            var r = new Random(578);

            for (var i = 0; i < 30; i++)
            {
                var v = new Vector2(r.NextFloat() * max, r.NextFloat() * max);
                var c = v.Round();
                Assert.AreEqual(Math.Round(v.X), c.X);
                Assert.AreEqual(Math.Round(v.Y), c.Y);
            }
        }
Example #11
0
 protected override void DrawContent(SpriteBatch spriteBatch)
 {
     var messageY = SHADOW_THICKNESS;
     foreach (var item in Messages.Take(VISIBLE_LINES))
     {
         float alpha = GetMessageAlpha(item);
         if (alpha == 0) continue;
         var preTextSize = _chatBoxFont.MeasureString(item.Message.PreText);
         var textSize = _chatBoxFont.MeasureString(item.Message.Text);
         var preTextPos = new Vector2((Dimensions.X - textSize.X - preTextSize.X) / 2, messageY);
         var textPos = preTextPos + new Vector2(preTextSize.X, 0);
         ModelRenderer.DrawBorderedText(spriteBatch, _chatBoxFont, item.Message.PreText, preTextPos.Round(), PlayerMessage.PRETEXT_COLOR, alpha, SHADOW_THICKNESS);
         ModelRenderer.DrawBorderedText(spriteBatch, _chatBoxFont, item.Message.Text, textPos.Round(), item.Message.TextColor, alpha, SHADOW_THICKNESS);
         messageY += _chatBoxFont.LineSpacing;
     }
 }
 public void DrawText(string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, bool cameraTransform = false)
 {
     beginSpriteBatch();
     if (cameraTransform)
     {
         translatePosition(ref position);
         updateScale(ref scale);
     }
     float fontSize;
     SpriteFont spriteFont = Font.GetBestSizeSpriteFont(Font.DefaultSize * Math.Abs(scale.Y), out fontSize);
     SpriteEffects spriteEffects = getSpriteEffectsFromScale(ref scale);
     spriteBatch.DrawString(spriteFont, text, position.Round(), color, rotation, origin.Round(), Vector2.One, spriteEffects, 0.0f);
 }
Example #13
0
 private Color DrawHealthPercentage(SpriteBatch spriteBatch, float relativeHealth, float alpha)
 {
     var halfColor = Color.Multiply(Color.White, alpha * 1f);
     var halfBlackColor = Color.Multiply(Color.Black, alpha * 0.6f);
     var healthText = ((int)Math.Ceiling(relativeHealth * 100)).ToString() + "%";
     var textSize = _healthFont.MeasureString(healthText);
     var textPos = new Vector2((Dimensions.X - textSize.X) / 2, (_barBackgroundTexture.Height / 2) - (textSize.Y / 2) + 1);
     spriteBatch.DrawString(_healthFont, healthText, textPos.Round() - Vector2.One, halfBlackColor);
     spriteBatch.DrawString(_healthFont, healthText, textPos.Round() + new Vector2(1, -1), halfBlackColor);
     spriteBatch.DrawString(_healthFont, healthText, textPos.Round() + Vector2.One, halfBlackColor);
     spriteBatch.DrawString(_healthFont, healthText, textPos.Round() + new Vector2(-1, 1), halfBlackColor);
     spriteBatch.DrawString(_healthFont, healthText, textPos.Round(), halfColor);
     return halfColor;
 }
Example #14
0
 private Vector2 GetCustomAlignment()
 {
     if (_player.Ship == null) return Vector2.Zero;
     // Note: Screen Y axis points down and game Y axis points up.
     var newCustomAlignment = new Vector2(0, 40) + (_player.Ship.Pos + _player.Ship.DrawPosOffset - Viewport.CurrentLookAt).MirrorY();
     var newCustomAlignmentPoint = newCustomAlignment.Round().ToPoint();
     if (!_previousCustomAlignment.HasValue || !_customAlignmentDampBox.Contains(newCustomAlignmentPoint))
     {
         _customAlignmentDampBox = _customAlignmentDampBox.MoveToContain(newCustomAlignmentPoint);
         _previousCustomAlignment = newCustomAlignment;
         return newCustomAlignment;
     }
     return _previousCustomAlignment.Value;
 }
Example #15
0
 public void Draw(SpriteBatch spriteBatch, Vector2 offset)
 {
     Vector2 destination = Position.Round().ToVector2() - offset.Round();
     spriteBatch.Draw(CurrentFrame, destination, null, Color.White, Angle, Origin.ToVector2(), 1f, SpriteEffects.None, 1f);
     spriteBatch.Draw(GameSession.Current.Pixel, new Rectangle((int)destination.X + BoundingBox.X, (int)destination.Y + BoundingBox.Y, BoundingBox.Width, BoundingBox.Height), Color.Lime * 0.3f);
 }
Example #16
0
        public void Update(GameTime gameTime)
        {
            if (map == null)
                return;

            oldkstate = kstate;
            oldmstate = mstate;
            kstate = Keyboard.GetState();
            mstate = Mouse.GetState();
            int mwheeldelta = mstate.ScrollWheelValue - oldmstate.ScrollWheelValue;

            if (mwheeldelta > 0)
            {
                float zoom = (float)Math.Round(camera.Scale * 10) * 10.0f + 10.0f;
                MainWindow.Instance.zoomCombo.Text = zoom.ToString() + "%";
                camera.Scale = zoom / 100.0f;
            }
            if (mwheeldelta < 0 )
            {
                float zoom = (float)Math.Round(camera.Scale * 10) * 10.0f - 10.0f;
                if (zoom <= 0.0f) return;
                MainWindow.Instance.zoomCombo.Text = zoom.ToString() + "%";
                camera.Scale = zoom / 100.0f;
            }

            //Camera movement
            float delta;
            if (kstate.IsKeyDown(Keys.LeftShift))
                delta = Preferences.Instance.CameraFastSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            else
                delta = Preferences.Instance.CameraSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (kstate.IsKeyDown(Keys.W) && kstate.IsKeyUp(Keys.LeftControl))
                camera.Position += (new Vector2(0, -delta));
            if (kstate.IsKeyDown(Keys.S) && kstate.IsKeyUp(Keys.LeftControl))
                camera.Position += (new Vector2(0, +delta));
            if (kstate.IsKeyDown(Keys.A) && kstate.IsKeyUp(Keys.LeftControl))
                camera.Position += (new Vector2(-delta, 0));
            if (kstate.IsKeyDown(Keys.D) && kstate.IsKeyUp(Keys.LeftControl))
                camera.Position += (new Vector2(+delta, 0));

            if (kstate.IsKeyDown(Keys.Subtract))
            {
                float zoom = (float)(camera.Scale * 0.995);
                MainWindow.Instance.zoomCombo.Text = (zoom * 100).ToString("###0.0") + "%";
                camera.Scale = zoom;
            }
            if (kstate.IsKeyDown(Keys.Add))
            {
                float zoom = (float)(camera.Scale * 1.005);
                MainWindow.Instance.zoomCombo.Text = (zoom * 100).ToString("###0.0") + "%";
                camera.Scale = zoom;
            }

            //get mouse world position considering the ScrollSpeed of the current layer
            Vector2 maincameraposition = camera.Position;
            if (SelectedLayer != null)
                camera.Position *= SelectedLayer.ScrollSpeed;
            mouseworldpos = Vector2.Transform(new Vector2(mstate.X, mstate.Y), Matrix.Invert(camera.matrix));
            mouseworldpos = mouseworldpos.Round();
            camera.Position = maincameraposition;

            if (state == EditorState.idle)
            {
                //get item under mouse cursor
                Tile item = getItemAtPos(mouseworldpos);
                if (item != null)
                {
                    item.onMouseOver(mouseworldpos);
                    if (kstate.IsKeyDown(Keys.LeftControl))
                        MainWindow.Instance.drawingBox.Cursor = cursorDup;
                }
                if (item != lastTile && lastTile != null)
                    lastTile.onMouseOut();
                lastTile = item;

                //LEFT MOUSE BUTTON CLICK
                if ((mstate.LeftButton == ButtonState.Pressed && oldmstate.LeftButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D1) && oldkstate.IsKeyUp(Keys.D1)))
                {
                    if (item != null) item.onMouseButtonDown(mouseworldpos);
                    if (kstate.IsKeyDown(Keys.LeftControl) && item != null)
                    {
                        if (!SelectedTiles.Contains(item))
                            selectTile(item);

                        beginCommand("Add Item(s)");

                        List<Tile> selecteditemscopy = new List<Tile>();
                        foreach (Tile selitem in SelectedTiles)
                        {
                            Tile i2 = (Tile)selitem.clone();
                            selecteditemscopy.Add(i2);
                        }
                        foreach (Tile selitem in selecteditemscopy)
                        {
                            selitem.Name = selitem.getNamePrefix() + map.getNextItemNumber();
                            addItem(selitem);
                        }
                        selectTile(selecteditemscopy[0]);
                        updateTreeView();
                        for (int i = 1; i < selecteditemscopy.Count; i++) SelectedTiles.Add(selecteditemscopy[i]);
                        startMoving();
                    }
                    else if (kstate.IsKeyDown(Keys.LeftShift) && item != null)
                    {
                        if (SelectedTiles.Contains(item)) SelectedTiles.Remove(item);
                        else SelectedTiles.Add(item);
                    }
                    else if (SelectedTiles.Contains(item))
                    {
                        beginCommand("Change Item(s)");
                        startMoving();
                    }
                    else if (!SelectedTiles.Contains(item))
                    {
                        selectTile(item);
                        if (item != null)
                        {
                            beginCommand("Change Item(s)");
                            startMoving();
                        }
                        else
                        {
                            grabbedpoint = mouseworldpos;
                            selectionrectangle = Rectangle.Empty;
                            state = EditorState.selecting;
                        }

                    }
                }

                //MIDDLE MOUSE BUTTON CLICK
                if ((mstate.MiddleButton == ButtonState.Pressed && oldmstate.MiddleButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D2) && oldkstate.IsKeyUp(Keys.D2)))
                {
                    if (item != null) item.onMouseOut();
                    if (kstate.IsKeyDown(Keys.LeftControl))
                    {
                        grabbedpoint = new Vector2(mstate.X, mstate.Y);
                        initialcampos = camera.Position;
                        state = EditorState.cameramoving;
                        MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.SizeAll;
                    }
                    else
                    {
                        if (SelectedTiles.Count > 0)
                        {
                            grabbedpoint = mouseworldpos - SelectedTiles[0].pPosition;

                            //save the initial rotation for each item
                            initialrot.Clear();
                            foreach (Tile selitem in SelectedTiles)
                            {
                                initialrot.Add(selitem.Rotation);
                            }

                            state = EditorState.rotating;
                            MainWindow.Instance.drawingBox.Cursor = cursorRot;

                            beginCommand("Rotate Item(s)");
                        }
                    }
                }

                //RIGHT MOUSE BUTTON CLICK
                if ((mstate.RightButton == ButtonState.Pressed && oldmstate.RightButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D3) && oldkstate.IsKeyUp(Keys.D3)))
                {
                    if (item != null) item.onMouseOut();
                    if (SelectedTiles.Count > 0)
                    {
                        grabbedpoint = mouseworldpos - SelectedTiles[0].pPosition;

                        //save the initial scale for each item
                        initialscale.Clear();
                        foreach (Tile selitem in SelectedTiles)
                        {
                            initialscale.Add(selitem.Scale);
                        }

                        state = EditorState.scaling;
                        MainWindow.Instance.drawingBox.Cursor = cursorScale;

                        beginCommand("Scale Item(s)");
                    }
                }
            }

            if (state == EditorState.moving)
            {
                int i = 0;
                foreach (Tile selitem in SelectedTiles)
                {
                    newPosition = initialpos[i] + mouseworldpos - grabbedpoint;
                    if (Preferences.Instance.SnapToGrid || kstate.IsKeyDown(Keys.G))
                        newPosition = snapToGrid(newPosition);
                    drawSnappedPoint = false;
                    selitem.pPosition = newPosition;
                    i++;
                }
                MainWindow.Instance.propertyGrid1.Refresh();
                if ((mstate.LeftButton == ButtonState.Released && oldmstate.LeftButton == ButtonState.Pressed) ||
                    (kstate.IsKeyUp(Keys.D1) && oldkstate.IsKeyDown(Keys.D1)))
                {

                    foreach (Tile selitem in SelectedTiles)
                        selitem.onMouseButtonUp(mouseworldpos);

                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                    if (mouseworldpos != grabbedpoint) endCommand(); else abortCommand();
                }
            }

            if (state == EditorState.rotating)
            {
                Vector2 newpos = mouseworldpos - SelectedTiles[0].pPosition;
                float deltatheta = (float)Math.Atan2(grabbedpoint.Y, grabbedpoint.X) - (float)Math.Atan2(newpos.Y, newpos.X);
                int i = 0;
                foreach (Tile selitem in SelectedTiles)
                {
                        selitem.pRotation = initialrot[i] - deltatheta;

                        if (kstate.IsKeyDown(Keys.LeftControl))
                        {
                            selitem.pRotation = ((float)Math.Round(selitem.pRotation / MathHelper.PiOver4) * MathHelper.PiOver4);
                        }
                        i++;
                }
                MainWindow.Instance.propertyGrid1.Refresh();
                if ((mstate.MiddleButton == ButtonState.Released && oldmstate.MiddleButton == ButtonState.Pressed) ||
                    (kstate.IsKeyUp(Keys.D2) && oldkstate.IsKeyDown(Keys.D2)))
                {
                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                    endCommand();
                }
            }

            if (state == EditorState.scaling)
            {
                Vector2 newdistance = mouseworldpos - SelectedTiles[0].pPosition;
                float factor = newdistance.Length() / grabbedpoint.Length();
                int i = 0;
                foreach (Tile selitem in SelectedTiles)
                {
                        Vector2 newscale = initialscale[i];
                        if (!kstate.IsKeyDown(Keys.Y)) newscale.X = initialscale[i].X * (((factor - 1.0f) * 0.5f) + 1.0f);
                        if (!kstate.IsKeyDown(Keys.X)) newscale.Y = initialscale[i].Y * (((factor - 1.0f) * 0.5f) + 1.0f);
                        selitem.pScale = (newscale);

                        if (kstate.IsKeyDown(Keys.LeftControl))
                        {
                            Vector2 scale;
                            scale.X = (float)Math.Round(selitem.pScale.X * 10) / 10;
                            scale.Y = (float)Math.Round(selitem.pScale.Y * 10) / 10;
                            selitem.pScale = (scale);
                        }
                        i++;
                }
                MainWindow.Instance.propertyGrid1.Refresh();
                if ((mstate.RightButton == ButtonState.Released && oldmstate.RightButton == ButtonState.Pressed) ||
                    (kstate.IsKeyUp(Keys.D3) && oldkstate.IsKeyDown(Keys.D3)))
                {
                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                    endCommand();
                }
            }

            if (state == EditorState.cameramoving)
            {
                Vector2 newpos = new Vector2(mstate.X, mstate.Y);
                Vector2 distance = (newpos - grabbedpoint) / camera.Scale;
                if (distance.Length() > 0)
                {
                    camera.Position = initialcampos - distance;
                }
                if (mstate.MiddleButton == ButtonState.Released)
                {
                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                }
            }

            if (state == EditorState.selecting)
            {
                if (SelectedLayer == null) return;
                Vector2 distance = mouseworldpos - grabbedpoint;
                if (distance.Length() > 0)
                {
                    SelectedTiles.Clear();
                    selectionrectangle = Extensions.RectangleFromVectors(grabbedpoint, mouseworldpos);
                    foreach (Tile i in SelectedLayer.Tiles)
                    {
                        if (i.Visible && selectionrectangle.Contains((int)i.pPosition.X, (int)i.pPosition.Y)) SelectedTiles.Add(i);
                    }
                    updateTreeViewSelection();
                }
                if (mstate.LeftButton == ButtonState.Released)
                {
                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                }
            }

            if (state == EditorState.brush)
            {
                if (Preferences.Instance.SnapToGrid || kstate.IsKeyDown(Keys.G))
                {
                    mouseworldpos = snapToGrid(mouseworldpos);
                }
                if (mstate.RightButton == ButtonState.Pressed && oldmstate.RightButton == ButtonState.Released) state = EditorState.idle;
                if (mstate.LeftButton == ButtonState.Pressed && oldmstate.LeftButton == ButtonState.Released) paintTextureBrush(true);
            }
        }