コード例 #1
0
ファイル: FontAsset.cs プロジェクト: madninjaskillz/Breeze
        public static Vector2 DrawFontAsset(
            SmartSpriteBatch spriteBatch,
            ScreenAbstractor screen,
            float opacity,
            string text,
            Color?fontColor,
            FloatRectangle?position,
            float fontSize,
            float?lineHeight,
            string fontName,
            bool pseudoAntiAlias,
            bool wordWrap,
            FontJustification justification,
            float fontMargin,
            Thickness margin      = null,
            Color?outlineColor    = null,
            FloatRectangle?clip   = null,
            Texture2D bgTexture   = null,
            Vector2?scrollOffset  = null,
            int?caretPos          = null,
            bool multiLine        = false,
            Rectangle?scissorRect = null)
        {
            if (position == null)
            {
                return(Vector2.Zero);
            }

            if (string.IsNullOrWhiteSpace(text))
            {
                return(Vector2.Zero);
            }

            List <string> textValues = new List <string> {
                text
            };

            if (multiLine)
            {
                textValues = text.ToList();
            }

            if (fontSize == 0)
            {
                fontSize = 24;
            }

            float lHeight = fontSize * 1.25f;

            if (lineHeight.HasValue)
            {
                lHeight = lineHeight.Value;
            }

            float      translatedBaseSize = screen.Translate(new Vector2(1f, 1f)).Y;
            float      fontSizeRatio      = (translatedBaseSize / 1080f);
            float      translatedFontSize = fontSizeRatio * fontSize;
            FontFamily fontFamily         = Solids.Instance.Fonts.Fonts[fontName];

            if (fontFamily == null)
            {
                return(Vector2.Zero);
            }

            string currentKey = translatedFontSize.ToString() + fontFamily.ToString();


            BMFont myFont = fontFamily.GetFont((int)translatedFontSize);

            Vector2 sizeOfI = myFont.MeasureString("Igj'#" + FontSystem.MDL2Symbols.Download.AsChar());

            float translatedWidth = screen.Translate(position).Value.Width;

            float wordWrapWidth = translatedWidth;

            if (margin != null)
            {
                wordWrapWidth = wordWrapWidth - screen.Translate(new Vector2(margin.Left + margin.Right, 0)).X;
                position      = new FloatRectangle(position.Value.X, position.Value.Y + margin.Top, position.Value.Width, position.Value.Height);
            }

            Vector2 size = myFont.MeasureString(text);

            float scale = translatedFontSize / sizeOfI.Y;

            if (wordWrap && multiLine)
            {
                List <string> lines = textValues.ToList();

                textValues = new List <string>();

                string reconstructedLine = "";
                foreach (string line in lines)
                {
                    string[] words = line.Split(' ');

                    for (int i = 0; i < words.Length; i++)
                    {
                        string withoutCurrentWord = reconstructedLine;
                        reconstructedLine = reconstructedLine + words[i] + " ";

                        Vector2 wwSize = myFont.MeasureString(reconstructedLine);
                        if (wwSize.X * scale > wordWrapWidth)
                        {
                            textValues.Add(withoutCurrentWord);
                            reconstructedLine = "";
                            i = i - 1;
                        }
                    }
                }

                textValues.Add(reconstructedLine);
            }



            float   ulHeight = lHeight / translatedBaseSize;
            Vector2 calcSize = Vector2.Zero;

            for (int i = 0; i < textValues.Count; i++)
            {
                FloatRectangle positionValue = new FloatRectangle(position.Value.X, position.Value.Y + (ulHeight * i), position.Value.Width, ulHeight);
                FloatRectangle fontPos       = new FloatRectangle(positionValue.X, positionValue.Y + fontMargin, positionValue.Width, positionValue.Height - (fontMargin * 2f));

                string textValue = textValues[i];

                FloatRectangle?tclip = screen.Translate(clip);

                FloatRectangle positionRectangle = new FloatRectangle();

                FloatRectangle translatedPosition = screen.Translate(fontPos).Value;

                if (tclip.HasValue)
                {
                    if (translatedPosition.Right < tclip.Value.X || translatedPosition.X > tclip.Value.Right)
                    {
                        return(Vector2.Zero);
                    }
                }

                if (string.IsNullOrWhiteSpace(fontName))
                {
                    return(Vector2.Zero);
                }

                float width = size.X * scale;

                switch (justification)
                {
                case FontJustification.Left:
                {
                    positionRectangle = new FloatRectangle(translatedPosition.X, translatedPosition.Y, width, translatedPosition.Height);
                    break;
                }

                case FontJustification.Right:
                {
                    positionRectangle = new FloatRectangle(translatedPosition.Right - width, translatedPosition.Y, width, translatedPosition.Height);
                    break;
                }

                case FontJustification.Center:
                {
                    positionRectangle = new FloatRectangle(translatedPosition.X + (translatedPosition.Width / 2f) - (width / 2f), translatedPosition.Y, width, translatedPosition.Height);
                    break;
                }
                }

                if (caretPos != null)
                {
                    using (new SmartSpriteBatchManager(Solids.Instance.SpriteBatch, scissorRect: tclip))
                    {
                        float actualCarotPos = (width / text.Length) * caretPos.Value;

                        float oppax = (DateTime.Now.Millisecond % 250) / 250f;
                        spriteBatch.DrawLine(
                            new Vector2(positionRectangle.X + actualCarotPos, positionRectangle.Y),
                            new Vector2(positionRectangle.X + actualCarotPos, positionRectangle.Bottom),
                            Color.White * oppax, tclip, 1f);
                    }
                }

                Rectangle source = new Rectangle(0, 0, (int)size.X, (int)size.Y);

                tclip = tclip?.Clamp(scissorRect);

                (Rectangle position, Rectangle? source)translator = TextureHelpers.GetAdjustedDestAndSourceAfterClip(positionRectangle, source, tclip);

                Solids.Instance.SpriteBatch.Scissor = tclip.ToRectangle();

                Rectangle tmp = Solids.Instance.Bounds;
                if (tclip.HasValue)
                {
                    tmp = tclip.Clamp(scissorRect).ToRectangle;
                }

                Solids.Instance.SpriteBatch.Scissor = tmp;

                using (new SmartSpriteBatchManager(Solids.Instance.SpriteBatch))
                {
                    if ((pseudoAntiAlias || outlineColor.HasValue))
                    {
                        Color?cl = fontColor * opacity * 0.3f;

                        if (outlineColor.HasValue)
                        {
                            cl = outlineColor.Value * opacity;
                        }

                        for (int y = -1; y <= 1; y = y + 2)
                        {
                            for (int x = -1; x <= 1; x = x + 2)
                            {
                                myFont.DrawText(Solids.Instance.SpriteBatch, translator.position.X + x, translator.position.Y + y, textValue, cl, scale);
                            }
                        }
                    }

                    myFont.DrawText(Solids.Instance.SpriteBatch, translator.position.X, translator.position.Y, textValue, fontColor * opacity, scale);
                    calcSize = new Vector2(position.Value.Width, calcSize.Y + lHeight);
                }

                Solids.Instance.SpriteBatch.Scissor = null;
            }


            float actHeight = screen.Untranslate(calcSize.Y);

            if (margin != null)
            {
                calcSize.Y = calcSize.Y + margin.Bottom;
            }

            return(new Vector2(position.Value.Width, actHeight));
        }
コード例 #2
0
ファイル: Room.cs プロジェクト: xdinos/SDL2Experiments
        public void Render(Renderer renderer, Size screenSize, Vector2 cameraPos, BMFont font)
        {
            var scale = 1f * Vector2.One;

            var hss = new Size((int)(screenSize.Width / 2f),
                               (int)(screenSize.Height / 2f));

            //var key = 0;
            foreach (var key in _layers.Keys.OrderByDescending(x => x))
            {
                var items = _layers[key];

                foreach (var layerItem in items)
                {
                    var parallax = layerItem.Parallax;
                    var pos      = new Vector2(
                        (hss.Width - cameraPos.X) * parallax.X - hss.Width,
                        (hss.Height - cameraPos.Y) * parallax.Y - hss.Height);

                    var p = new Vector2(
                        hss.Width + pos.X + (hss.Width - hss.Width * parallax.X),
                        hss.Height + pos.Y + (hss.Height - hss.Height * parallax.Y));

                    layerItem.Draw(renderer, p, scale);
                }

                //foreach (var layer in items)
                //{
                //	var parallax = layer.Parallax;
                //	var pos = new Vector2(
                //		(hss.Width - cameraPos.X) * parallax.X - hss.Width,
                //		(hss.Height - cameraPos.Y) * parallax.Y - hss.Height);

                //	var p = new Vector2(
                //		hss.Width+pos.X + (hss.Width - hss.Width * parallax.X),
                //		hss.Height+ pos.Y + (hss.Height - hss.Height * parallax.Y));


                //	layer.Sprite.Draw(renderer, p, scale);

                //	//renderer.Draw(layer.Sprite.Texture,
                //	//              layer.Sprite.Offset,
                //	//              Vector2.Zero, 0, scale);
                //}
            }

            var textY = Size.Y * scale.Y + 40f;

            //foreach (var obj in _objects.OrderBy(x => x.ZSort))
            //foreach (var obj in _objects.Skip(0).Take(1))
            foreach (var obj in _objects.OrderByDescending(x => x.ZSort))
            //foreach (var obj in _objects.Where(x=>x.Name== "chucksTombButton"))
            {
                if (obj.Sprite != null)
                {
                    obj.Sprite.Draw(renderer,
                                    new Vector2(hss.Width - cameraPos.X + obj.Position.X * scale.X,
                                                hss.Height - cameraPos.Y + obj.Position.Y * scale.Y),
                                    scale);
                    //var pos = new Vector2(obj.Position + obj.Sprite.Offset, pos.Y);
                    ////var pos = new Vector2(obj.Position.X, Size.Y - obj.Position.Y);
                    ////var pos = obj.Position + obj.Sprite.Offset;
                    //renderer.Draw(obj.Sprite.Texture,
                    //			  pos,
                    //			  Vector2.Zero, 0, scale);
                }

                var x = obj.Position.X + obj.HotSpot.X;
                var y = obj.Position.Y + obj.HotSpot.Y;

                if (!obj.Prop && !obj.Spot && !obj.Trigger)
                {
                    renderer.SetDrawColor(0xff, 0xff, 0xff, 0xff);
                    renderer.DrawRect(x, y,
                                      obj.HotSpot.Width,
                                      obj.HotSpot.Height,
                                      scale);
                }

                if (!obj.Prop && !obj.Spot && !obj.Trigger &&
                    (Mouse.X >= x * scale.X && Mouse.X <= (x + obj.HotSpot.Width) * scale.X ||
                     Mouse.X >= (x + obj.HotSpot.Width) * scale.X && Mouse.X <= x * scale.X) &&
                    (Mouse.Y >= y * scale.Y && Mouse.Y <= (y + obj.HotSpot.Height) * scale.Y ||
                     Mouse.Y >= (y + obj.HotSpot.Height) * scale.Y) && Mouse.Y <= y * scale.Y)
                {
                    renderer.SetDrawColor(0x00, 0xff, 0x00, 0xff);
                    renderer.DrawRect(x, y,
                                      obj.HotSpot.Width,
                                      obj.HotSpot.Height,
                                      scale);
                    renderer.SetDrawColor(0xff, 0xff, 0xff, 0xff);
                    renderer.DrawRect(obj.Position.X - 1, obj.Position.Y - 1, 2, 2, scale);

                    font.DrawText(renderer, obj.Name, 0, textY, 0xff, 0xff, 0xff);

                    renderer.SetDrawColor(0x00, 0xff, 0x00, 0xff);

                    var usePos = new Vector2(obj.Position.X + obj.UsePosition.X,
                                             obj.Position.Y - obj.UsePosition.Y);
                    renderer.DrawRect(usePos.X - 1, usePos.Y - 1, 2, 2, scale);

                    renderer.DrawLine(obj.Position, usePos, scale);

                    textY += 20f;
                }
            }

            renderer.SetDrawColor(0x00, 0xff, 0x00, 0xff);
            foreach (var walkBox in _walkBoxes)
            {
                var pts = walkBox.Polygon.Points
                          .Select(p => new Vector2(hss.Width - cameraPos.X + p.X * scale.X,
                                                   hss.Height - cameraPos.Y + p.Y * scale.Y))
                          .ToList();
                renderer.DrawLines(pts, Vector2.One);
            }

            font.DrawText(renderer, $"({Mouse.X}, {Mouse.Y})", 0, Size.Y * scale.Y + 20f, 0xff, 0xff, 0xff);

            renderer.SetDrawColor(0xff, 0x00, 0x00, 0xff);
            renderer.DrawRect(Size.X / 2 - 1, Size.Y / 2 - 1, 2, 2, scale);
        }
コード例 #3
0
        public void Draw(SmartSpriteBatch spriteBatch, bool showDebug)
        {
            BMFont font = Solids.Instance.AssetLibrary.GetFont("consolas");

            if (font != null)
            {
                if (showDebug)
                {
                    var bounds = spriteBatch.GraphicsDevice.Viewport.Bounds;
                    spriteBatch.DrawSolidRectangle(new FloatRectangle(0, 0, bounds.Width, (int)(bounds.Height * 0.8f)), Color.CornflowerBlue * 0.75f);
                }
                var scale = new Vector2(0.75f, 0.75f);
                int mxh   = 0;
                int mxw   = 0;
                foreach (var i in DebugObjects)
                {
                    var r = font.MeasureString(i.CurrentText) * scale;
                    if (r.X > mxw)
                    {
                        mxw = (int)r.X;
                    }
                    if (r.Y > mxh)
                    {
                        mxh = (int)r.Y;
                    }
                }
                int width = 100 + mxw + 20;

                if (showDebug)
                {
                    spriteBatch.DrawSolidRectangle(new FloatRectangle(spriteBatch.GraphicsDevice.Viewport.Bounds.Width - (int)width - 20, 2, (int)width, (int)mxh * DebugObjects.Count), Color.Black);
                }

                int   ct = 0;
                float mx = 1;

                if (showDebug && false)
                {
                    Vector2 fontscale = new Vector2(0.4f, 0.4f);
                    int     ps        = 50;
                    font.DrawText(spriteBatch, new Vector2(10, 20), "Key", Color.White, fontscale);
                    font.DrawText(spriteBatch, new Vector2(300, 20), "Runs", Color.White, fontscale);
                    font.DrawText(spriteBatch, new Vector2(450, 20), "Avg", Color.White, fontscale);
                    font.DrawText(spriteBatch, new Vector2(600, 20), "Min", Color.White, fontscale);
                    font.DrawText(spriteBatch, new Vector2(750, 20), "Max", Color.White, fontscale);
                    font.DrawText(spriteBatch, new Vector2(900, 20), "Total", Color.White, fontscale);
                    foreach (KeyValuePair <string, BenchMarkDetails> benchMarkDetailse in BenchMarkProvider.BenchMarks)
                    {
                        font.DrawText(spriteBatch, new Vector2(10, ps), benchMarkDetailse.Key, Color.White, new Vector2(0.5f, 0.5f));
                        font.DrawText(spriteBatch, new Vector2(300, ps), benchMarkDetailse.Value.NumberOfTimesRun.ToString(), Color.White, fontscale);
                        font.DrawText(spriteBatch, new Vector2(450, ps), benchMarkDetailse.Value.AverageTime.ToString("c"), Color.White, fontscale);
                        font.DrawText(spriteBatch, new Vector2(600, ps), benchMarkDetailse.Value.ShortestTime.ToString("c"), Color.White, fontscale);
                        font.DrawText(spriteBatch, new Vector2(750, ps), benchMarkDetailse.Value.LongestTime.ToString("c"), Color.White, fontscale);
                        font.DrawText(spriteBatch, new Vector2(900, ps), benchMarkDetailse.Value.TotalTime.ToString("c"), Color.White, fontscale);

                        ps = ps + 30;
                    }
                }

                if (showDebug && true)
                {
                    Vector2 fontscale = new Vector2(0.4f, 0.4f);
                    int     ps        = 50 + (BreezeDebug.ConsoleHistory.Count * 30);

                    if (ps > 700)
                    {
                        ps = 700;
                    }

                    foreach (string history in BreezeDebug.ConsoleHistory)
                    {
                        if (ps > 0)
                        {
                            font.DrawText(spriteBatch, new Vector2(10, ps), history, Color.White, new Vector2(0.5f, 0.5f));
                            ps = ps - 30;
                        }
                    }
                }

                if (false)
                {
                    foreach (var i in DebugObjects)
                    {
                        if (showDebug)
                        {
                            font.DrawText(spriteBatch,
                                          new Vector2(spriteBatch.GraphicsDevice.Viewport.Bounds.Width - width - 10, mxh * ct),
                                          i.CurrentText, Color.White, scale);
                            if (Solids.Instance.FrameCounter.CurrentFramesPerSecond > 59)
                            {
                                if (pointer % 500 == 0)
                                {
                                    mx = Math.Max(i.HistoricValues.Max(), 1);

                                    if (mx > i.MXValue)
                                    {
                                        i.MXValue = mx;
                                    }
                                    else
                                    {
                                        i.MXValue = i.MXValue * 0.995f;
                                    }
                                }

                                mx = i.MXValue;

                                int xct = 0;
                                for (int x = pointer; x < pointer + 49; x++)
                                {
                                    float h1 = (i.HistoricValues[x % 50] / mx) * mxh;
                                    float h2 = (i.HistoricValues[(x + 1) % 50] / mx) * mxh;

                                    int ps = spriteBatch.GraphicsDevice.Viewport.Bounds.Width - 90 + (xct * 2);

                                    spriteBatch.DrawLine(new Vector2(ps, h1 + (ct * mxh)),
                                                         new Vector2(ps + 2, h2 + (ct * mxh)), Color.White);

                                    xct++;
                                }
                            }

                            ct++;
                        }

                        i.HistoricValues[pointer % 50] = i.CurrentValue;
                    }
                }

                pointer++;
            }
        }