示例#1
0
        public override void Render(SpriteBatch batch, Camera camera, Viewport viewport)
        {
            if (batch == null || camera == null)
            {
                return;
            }

            Vector2 extents = Datastructures.SafeMeasure(Font, Text);


            Vector3 unprojected = viewport.Project(Position, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);

            if (unprojected.Z > 0.999f)
            {
                return;
            }

            Rectangle rect = new Rectangle((int)(unprojected.X - extents.X / 2.0f - StrokeWeight), (int)(unprojected.Y - extents.Y / 2.0f - StrokeWeight),
                                           (int)(extents.X + StrokeWeight + 5), (int)(extents.Y + StrokeWeight + 5));

            Drawer2D.FillRect(batch, rect, FillColor);
            Drawer2D.DrawRect(batch, rect, RectStrokeColor, StrokeWeight);
            Drawer2D.SafeDraw(batch, Text, Font, StrokeColor, new Vector2(unprojected.X + 1, unprojected.Y) - extents / 2.0f, Vector2.Zero);
            Drawer2D.SafeDraw(batch, Text, Font, StrokeColor, new Vector2(unprojected.X - 1, unprojected.Y) - extents / 2.0f, Vector2.Zero);

            Drawer2D.SafeDraw(batch, Text, Font, StrokeColor, new Vector2(unprojected.X, unprojected.Y + 1) - extents / 2.0f, Vector2.Zero);
            Drawer2D.SafeDraw(batch, Text, Font, StrokeColor, new Vector2(unprojected.X, unprojected.Y - 1) - extents / 2.0f, Vector2.Zero);

            Drawer2D.SafeDraw(batch, Text, Font, TextColor, new Vector2(unprojected.X, unprojected.Y) - extents / 2.0f, Vector2.Zero);
        }
示例#2
0
        public override void Render(DwarfTime time, SpriteBatch batch)
        {
            string text = Text;

            if (WordWrap)
            {
                text = DwarfGUI.WrapLines(Text, LocalBounds, TextFont);
            }

            if (Truncate)
            {
                Vector2 measure  = Datastructures.SafeMeasure(TextFont, text);
                Vector2 wMeasure = Datastructures.SafeMeasure(TextFont, "W");
                if (measure.X > GlobalBounds.Width)
                {
                    int numLetters = GlobalBounds.Width / (int)wMeasure.X;
                    text = Text.Substring(0, Math.Min(numLetters, Text.Length)) + "...";
                }
            }

            if (StrokeColor.A > 0)
            {
                Drawer2D.DrawAlignedStrokedText(batch, text, TextFont, TextColor, StrokeColor, Alignment, GlobalBounds);
            }
            else
            {
                Drawer2D.DrawAlignedText(batch, text, TextFont, TextColor, Alignment, GlobalBounds);
            }
            base.Render(time, batch);
        }
示例#3
0
        public override void Render(SpriteBatch batch, Camera camera, Viewport viewport)
        {
            if (camera == null)
            {
                return;
            }
            Vector2 extents = Datastructures.SafeMeasure(Font, Text);

            Vector3 unprojected = viewport.Project(Position, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity);

            if (unprojected.Z > 0 && viewport.Bounds.Contains((int)unprojected.X, (int)unprojected.Y))
            {
                Drawer2D.SafeDraw(batch, Text, Font, StrokeColor,
                                  new Vector2(unprojected.X + 1, unprojected.Y) - extents / 2.0f, Vector2.Zero);
                Drawer2D.SafeDraw(batch, Text, Font, StrokeColor,
                                  new Vector2(unprojected.X - 1, unprojected.Y) - extents / 2.0f, Vector2.Zero);

                Drawer2D.SafeDraw(batch, Text, Font, StrokeColor,
                                  new Vector2(unprojected.X, unprojected.Y + 1) - extents / 2.0f, Vector2.Zero);
                Drawer2D.SafeDraw(batch, Text, Font, StrokeColor,
                                  new Vector2(unprojected.X, unprojected.Y - 1) - extents / 2.0f, Vector2.Zero);

                Drawer2D.SafeDraw(batch, Text, Font, TextColor, new Vector2(unprojected.X, unprojected.Y) - extents / 2.0f,
                                  Vector2.Zero);
            }
        }
示例#4
0
        public override void Render(DwarfTime time, SpriteBatch batch)
        {
            Rectangle globalBounds = GlobalBounds;

            Color c = TextColor;

            if (IsMouseOver)
            {
                c = HoverTextColor;
            }

            Rectangle checkboxBounds = new Rectangle(GlobalBounds.Right - 32, GlobalBounds.Top + 1, 32, 32);

            GUI.Skin.RenderCheckbox(checkboxBounds, Checked, batch);
            Vector2 measure = Datastructures.SafeMeasure(GUI.DefaultFont, Text);


            Drawer2D.DrawStrokedText(batch, Text,
                                     GUI.DefaultFont,
                                     new Vector2(GlobalBounds.Right - measure.X - 32, GlobalBounds.Top + 5),
                                     c, StrokeColor);


            base.Render(time, batch);
        }
示例#5
0
        private void CreateSubtreeRecursive(Act root, ref Vector2 lastPosition, ref Vector2 size)
        {
            if (root == null)
            {
                return;
            }
            else
            {
                ActElement element = new ActElement();
                element.act      = root;
                element.position = lastPosition;

                Elements.Add(element);

                lastPosition.Y += ElementHeight;
                lastPosition.X += ElementWidth;

                size   += Datastructures.SafeMeasure(GUI.DefaultFont, element.act.Name);
                size.X += ElementWidth;
                size.Y += ElementHeight;

                if (root.Children != null && root.Enumerator.Current != Act.Status.Success)
                {
                    foreach (Act child in root.Children)
                    {
                        CreateSubtreeRecursive(child, ref lastPosition, ref size);
                    }
                }

                lastPosition.X -= ElementWidth;
            }
        }
示例#6
0
        public void RenderSliderHorizontal(SpriteFont font, Rectangle boundingRect, float value, float minvalue, float maxValue, Slider.SliderMode mode, bool drawLabel, bool invertValue, SpriteBatch spriteBatch)
        {
            const int padding = 5;

            if (invertValue)
            {
                value = maxValue - value;
            }

            int       fieldSize = Math.Max(Math.Min((int)(0.2f * boundingRect.Width), 150), 64);
            Rectangle rect      = new Rectangle(boundingRect.X + padding, boundingRect.Y + boundingRect.Height / 2 - TileHeight / 2, boundingRect.Width - fieldSize - padding * 2, boundingRect.Height / 2);
            Rectangle fieldRect = new Rectangle(boundingRect.Right - fieldSize, boundingRect.Y + boundingRect.Height / 2 - TileHeight / 2, fieldSize, boundingRect.Height / 2);
            int       maxX      = rect.X + rect.Width;
            int       diffX     = rect.Width % TileWidth;
            int       right     = maxX;
            int       left      = rect.X;
            int       top       = rect.Y;


            for (int x = left; x <= right; x += TileWidth)
            {
                spriteBatch.Draw(Texture, new Rectangle(x, rect.Y, TileWidth, TileHeight), GetSourceRect(Tile.Track), Color.White);
            }

            spriteBatch.Draw(Texture, new Rectangle(maxX - diffX, rect.Y, diffX, TileHeight), GetSourceRect(Tile.Track), Color.White);

            int sliderX = (int)((value - minvalue) / (maxValue - minvalue) * rect.Width + rect.X);

            spriteBatch.Draw(Texture, new Rectangle(sliderX - TileWidth / 2, rect.Y, TileWidth, TileHeight), GetSourceRect(Tile.SliderTex), Color.White);

            if (!drawLabel)
            {
                return;
            }
            RenderField(fieldRect, spriteBatch);

            float v = 0.0f;

            if (invertValue)
            {
                value = value - maxValue;
            }
            if (mode == Slider.SliderMode.Float)
            {
                v = (float)Math.Round(value, 2);
            }
            else
            {
                v = (int)value;
            }

            string toDraw = "" + v;

            Vector2 origin = Datastructures.SafeMeasure(font, toDraw) * 0.5f;

            Drawer2D.SafeDraw(spriteBatch, toDraw, font, Color.Black, new Vector2(fieldRect.X + fieldRect.Width / 2, fieldRect.Y + 16), origin);
        }
示例#7
0
        private Vector2 MeasureColumn(IEnumerable <Entry> column)
        {
            Vector2 toReturn = Vector2.Zero;

            foreach (Entry s in column)
            {
                toReturn.Y += Datastructures.SafeMeasure(Font, s.LocalName).Y;
                toReturn.X  = (float)Math.Max(toReturn.X, Datastructures.SafeMeasure(Font, s.LocalName).X);
            }
            return(toReturn);
        }
示例#8
0
        private Vector2 MeasureColumn(List <string> column)
        {
            Vector2 toReturn = Vector2.Zero;

            foreach (string s in column)
            {
                toReturn.Y += Datastructures.SafeMeasure(GUI.DefaultFont, s).Y;
                toReturn.X  = (float)Math.Max(toReturn.X, Datastructures.SafeMeasure(GUI.DefaultFont, s).X);
            }
            return(toReturn);
        }
示例#9
0
        public static void DrawAlignedStrokedText(SpriteBatch batch, string origText, SpriteFont font, Color textColor, Color strokeColor, Alignment align, Rectangle bounds, bool wrap = false)
        {
            string text = origText;

            if (wrap)
            {
                text = DwarfGUI.WrapLines(text, bounds, font);
            }

            Vector2 size = Datastructures.SafeMeasure(font, text);

            Vector2 pos    = new Vector2((int)(bounds.X + bounds.Width / 2), (int)(bounds.Y + bounds.Height / 2));
            Vector2 origin = size * 0.5f;

            if (align.HasFlag(Alignment.Left))
            {
                origin.X += bounds.Width / 2 - size.X / 2;
            }

            if (align.HasFlag(Alignment.Right))
            {
                origin.X -= bounds.Width / 2 - size.X / 2;
            }

            if (align.HasFlag(Alignment.Top))
            {
                origin.Y += bounds.Height / 2 - size.Y / 2;
            }

            if (align.HasFlag(Alignment.Bottom))
            {
                origin.Y -= bounds.Height / 2 - size.Y / 2;
            }

            if (align.HasFlag(Alignment.Under))
            {
                origin.Y -= bounds.Height - size.Y;
            }

            origin.X = (int)origin.X;
            origin.Y = (int)origin.Y;
            if (textColor.A > 0 && strokeColor.A > 0)
            {
                SafeDraw(batch, text, font, strokeColor, pos - new Vector2(1, 0), origin, false);
                SafeDraw(batch, text, font, strokeColor, pos + new Vector2(1, 0), origin, false);
                SafeDraw(batch, text, font, strokeColor, pos - new Vector2(0, 1), origin, false);
                SafeDraw(batch, text, font, strokeColor, pos - new Vector2(0, 1), origin, false);
            }
            SafeDraw(batch, text, font, textColor, pos, origin);
        }
示例#10
0
        public override void Render(DwarfTime time, Microsoft.Xna.Framework.Graphics.SpriteBatch batch)
        {
            if (Drawn)
            {
                if (!GUI.DrawAfter.Contains(this))
                {
                    GUI.DrawAfter.Add(this);
                    Drawn = false;
                }
            }
            else
            {
                GUI.Skin.RenderButton(GlobalBounds, batch);


                int x = 0;
                foreach (List <string> column in Columns)
                {
                    if (column.Count == 0)
                    {
                        continue;
                    }

                    float columnMeasure = MeasureColumn(column).Y;
                    PixelsPerValue = (int)columnMeasure / column.Count;
                    int h = 0;

                    foreach (string s in column)
                    {
                        Vector2 measure = Datastructures.SafeMeasure(GUI.DefaultFont, s);

                        Color c = Color.Black;

                        if (s == CurrentValue)
                        {
                            c = Color.DarkRed;
                        }

                        Drawer2D.DrawAlignedText(batch, s, GUI.DefaultFont, c, Drawer2D.Alignment.Left, new Rectangle(GlobalBounds.X + 10 + x, GlobalBounds.Y + h + 5, GlobalBounds.Width, (int)measure.Y + 5));

                        h += PixelsPerValue;
                    }

                    x += ColumnWidth;
                }
                Drawn = true;
                base.Render(time, batch);
            }
        }
示例#11
0
        public static string WrapLines(string text, Rectangle bounds, SpriteFont textFont)
        {
            Vector2 measurement = Datastructures.SafeMeasure(textFont, text);

            if (measurement.X < bounds.Width)
            {
                return(text);
            }

            string[] originalWords = text.Split(' ');

            List <string> wrappedLines = new List <string>();

            StringBuilder actualLine  = new StringBuilder();
            double        actualWidth = 0;

            foreach (var item in originalWords)
            {
                Vector2 itemMeasure = Datastructures.SafeMeasure(textFont, item + " ");
                actualLine.Append(item + " ");
                actualWidth += (int)itemMeasure.X;

                if (actualWidth >= bounds.Width - itemMeasure.X)
                {
                    wrappedLines.Add(actualLine.ToString());
                    actualLine.Clear();
                    actualWidth = 0;
                }
            }

            if (actualLine.Length > 0)
            {
                wrappedLines.Add(actualLine.ToString());
            }

            string toReturn = "";

            foreach (var line in wrappedLines)
            {
                toReturn += line + "\n";
            }

            return(toReturn);
        }
示例#12
0
        public void RenderTip(GraphicsDevice device, SpriteBatch batch, string tip, Point mouse, TipType tipType)
        {
            Rectangle viewBounds = device.Viewport.Bounds;

            Vector2 stringMeasure = Datastructures.SafeMeasure(GUI.SmallFont, tip);

            Rectangle bounds;

            if (tipType == TipType.BottomRight)
            {
                bounds = new Rectangle(mouse.X + 24, mouse.Y + 24, (int)(stringMeasure.X + 15), (int)(stringMeasure.Y + 15));
            }
            else
            {
                bounds = new Rectangle(mouse.X - (int)stringMeasure.X - 15, mouse.Y - (int)stringMeasure.Y - 15, (int)(stringMeasure.X + 15), (int)(stringMeasure.Y + 15));
            }

            if (bounds.Left < viewBounds.Left)
            {
                bounds.X = viewBounds.X;
            }

            if (bounds.Right > viewBounds.Right)
            {
                bounds.X = viewBounds.Right - bounds.Width;
            }

            if (bounds.Top < viewBounds.Top)
            {
                bounds.Y = viewBounds.Y;
            }

            if (bounds.Bottom > viewBounds.Bottom)
            {
                bounds.Y = viewBounds.Bottom - bounds.Height;
            }

            GUI.Skin.RenderToolTip(bounds, batch, Color.White);
            Drawer2D.DrawAlignedText(batch, tip, GUI.SmallFont, Color.White, Drawer2D.Alignment.Center, bounds);
        }
示例#13
0
        public static void DrawAlignedText(SpriteBatch batch, string origText, SpriteFont font, Color textColor, Alignment align, Rectangle bounds, bool wrap = false)
        {
            string text = origText;

            if (wrap)
            {
                text = DwarfGUI.WrapLines(text, bounds, font);
            }
            Vector2 size = Datastructures.SafeMeasure(font, text);

            Vector2 pos    = new Vector2(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2);
            Vector2 origin = size * 0.5f;

            if (align.HasFlag(Alignment.Left))
            {
                pos.X -= bounds.Width / 2 - size.X / 2;
            }

            if (align.HasFlag(Alignment.Right))
            {
                pos.X += bounds.Width / 2 - size.X / 2;
            }

            if (align.HasFlag(Alignment.Top))
            {
                pos.Y -= bounds.Height / 2 - size.Y / 2;
            }

            if (align.HasFlag(Alignment.Bottom))
            {
                pos.Y += bounds.Height / 2 - size.Y / 2;
            }

            if (align.HasFlag(Alignment.Under))
            {
                pos.Y += bounds.Height / 2 + size.Y / 2;
            }
            SafeDraw(batch, text, font, textColor, pos, origin);
        }
示例#14
0
        private string GetSubstringToShow()
        {
            Vector2 measure = Datastructures.SafeMeasure(GUI.DefaultFont, Text);

            if (measure.X < GlobalBounds.Width - 15)
            {
                return(Text);
            }
            else
            {
                for (int i = 0; i < Text.Length; i++)
                {
                    string subtext = Text.Substring(i);
                    measure = Datastructures.SafeMeasure(GUI.DefaultFont, subtext);

                    if (measure.X < GlobalBounds.Width - 15)
                    {
                        return("..." + subtext);
                    }
                }
            }

            return(Text);
        }
示例#15
0
        public ComboBoxSelector(DwarfGUI gui, ComboBox parent, List <string> values, string currentValue) :
            base(gui, parent)
        {
            Columns      = new List <List <string> >();
            Values       = values;
            CurrentValue = currentValue;

            int height = 0;

            Columns.Add(new List <string>());
            int currentColumn = 0;
            int columnWidth   = parent.GlobalBounds.Width - 37;

            ColumnWidth = columnWidth;
            int bestHeight = 0;

            foreach (string s in values)
            {
                List <string> column  = Columns[currentColumn];
                Vector2       measure = Datastructures.SafeMeasure(GUI.DefaultFont, s);
                height += (int)measure.Y;
                column.Add(s);

                if (height > bestHeight)
                {
                    bestHeight = height;
                }

                if (height >= MaxHeight)
                {
                    height = 0;
                    Columns.Add(new List <string>());
                    currentColumn++;
                    columnWidth += ColumnWidth;
                }
            }

            List <List <string> > removals = new List <List <string> >();

            foreach (List <string> column in Columns)
            {
                if (column.Count == 0)
                {
                    removals.Add(column);
                }
            }

            foreach (List <string> column in removals)
            {
                Columns.Remove(column);
                columnWidth -= ColumnWidth;
            }

            bestHeight += 15;

            LocalBounds = new Rectangle(0, parent.GlobalBounds.Height / 2 + GUI.Skin.TileHeight / 2, columnWidth, bestHeight);
            Box         = parent;

            ClickTimer = new Timer(0.1f, true, Timer.TimerMode.Real);
            InputManager.MouseClickedCallback += InputManager_MouseClickedCallback;
            Drawn = true;
        }
示例#16
0
        public void GetLocalBoundsFromText()
        {
            Vector2 measure = Datastructures.SafeMeasure(TextFont, Text);

            LocalBounds = new Rectangle(LocalBounds.X, LocalBounds.Y, (int)measure.X + 4, (int)measure.Y + 4);
        }
示例#17
0
        public override void Render(DwarfTime gameTime)
        {
            try
            {
                DwarfGame.SafeSpriteBatchBegin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointWrap,
                                               DepthStencilState.Default, RasterizerState.CullNone, null, Matrix.Identity);
                float y = -CurrentScroll;
                int   w = GameState.Game.GraphicsDevice.Viewport.Width;
                int   h = GameState.Game.GraphicsDevice.Viewport.Height;
                Drawer2D.FillRect(DwarfGame.SpriteBatch, new Rectangle(Padding - 30, 0, w - Padding * 2 + 30, h), new Color(5, 5, 5, 150));
                foreach (CreditEntry entry in Entries)
                {
                    if (entry.Divider)
                    {
                        y += EntryHeight;
                        continue;
                    }

                    if (y + EntryHeight < -EntryHeight * 2 ||
                        y + EntryHeight > GameState.Game.GraphicsDevice.Viewport.Height + EntryHeight * 2)
                    {
                        y += EntryHeight;
                        continue;
                    }
                    Color color = entry.Color;

                    if (entry.RandomFlash)
                    {
                        color = new Color(MathFunctions.RandVector3Box(-1, 1, -1, 1, -1, 1) * 0.5f + color.ToVector3());
                    }
                    DwarfGame.SpriteBatch.DrawString(CreditsFont, entry.Role, new Vector2(w / 2 - Datastructures.SafeMeasure(CreditsFont, entry.Role).X - 5, y), color);
                    DwarfGame.SpriteBatch.DrawString(CreditsFont, entry.Name, new Vector2(w / 2 + 5, y), color);

                    y += EntryHeight;
                }
            }
            finally
            {
                DwarfGame.SpriteBatch.End();
            }
            base.Render(gameTime);
        }
示例#18
0
        public ComboBoxSelector(DwarfGUI gui, ComboBox parent, List <Entry> values, int posX = -1, int posY = -1) :
            base(gui, parent)
        {
            MaxHeight    = 500;
            Font         = gui.DefaultFont;
            Columns      = new List <List <Entry> >();
            Values       = values;
            CurrentValue = values.FirstOrDefault();

            int height = 0;

            Columns.Add(new List <Entry>());
            int currentColumn = 0;
            int columnWidth   = parent.GlobalBounds.Width - 37;

            ColumnWidth = columnWidth;
            int bestHeight = 0;
            int x          = posX > 0 ? posX : 0;

            Box = parent;
            int y = posY > 0 ? posY : parent.GlobalBounds.Height / 2 + GUI.Skin.TileHeight / 2;

            foreach (Entry s in values)
            {
                List <Entry> column  = Columns[currentColumn];
                Vector2      measure = Datastructures.SafeMeasure(Font, s.LocalName);
                height += (int)measure.Y;
                column.Add(s);

                if (height > bestHeight)
                {
                    bestHeight = height;
                }

                if (height >= MaxHeight || height + y + Box.GlobalBounds.Y + 32 > GameSettings.Default.ResolutionY)
                {
                    height = 0;
                    Columns.Add(new List <Entry>());
                    currentColumn++;
                    columnWidth += ColumnWidth;
                }
            }

            List <List <Entry> > removals = new List <List <Entry> >();

            foreach (List <Entry> column in Columns)
            {
                if (column.Count == 0)
                {
                    removals.Add(column);
                }
            }

            foreach (List <Entry> column in removals)
            {
                Columns.Remove(column);
                columnWidth -= ColumnWidth;
            }

            bestHeight += 15;

            LocalBounds = new Rectangle(x, y, columnWidth, bestHeight);

            ClickTimer = new Timer(0.1f, true, Timer.TimerMode.Real);
            InputManager.MouseClickedCallback += InputManager_MouseClickedCallback;
            Drawn = true;
        }
示例#19
0
        public void RenderSliderVertical(SpriteFont font, Rectangle boundingRect, float value, float minvalue, float maxValue, Slider.SliderMode mode, bool drawLabel, bool invert, SpriteBatch spriteBatch)
        {
            const int padding = 5;

            if (invert)
            {
                value = maxValue - value;
            }


            int       fieldSize = Math.Max(Math.Min((int)(0.2f * boundingRect.Width), 150), 64);
            Rectangle rect      = new Rectangle(boundingRect.X + boundingRect.Width / 2 - TileWidth / 2, boundingRect.Y + padding, boundingRect.Width, boundingRect.Height - TileHeight - padding * 2);
            Rectangle fieldRect = new Rectangle(boundingRect.Right - fieldSize, boundingRect.Y + boundingRect.Height - TileHeight / 2, fieldSize, TileHeight);

            int maxY   = rect.Y + rect.Height;
            int diffY  = rect.Height % TileHeight;
            int bottom = maxY;
            int left   = rect.X;
            int top    = rect.Y;


            for (int y = top; y <= bottom; y += TileHeight)
            {
                spriteBatch.Draw(Texture, new Rectangle(rect.X, y, TileWidth, TileHeight), GetSourceRect(Tile.TrackVert), Color.White);
            }

            spriteBatch.Draw(Texture, new Rectangle(rect.X, maxY - diffY, TileWidth, diffY), GetSourceRect(Tile.TrackVert), Color.White);

            float d = (value - minvalue) / (maxValue - minvalue);

            int sliderY = (int)((d) * rect.Height + rect.Y);

            spriteBatch.Draw(Texture, new Rectangle(rect.X, sliderY - TileHeight / 2, TileWidth, TileHeight), GetSourceRect(Tile.SliderVertical), Color.White);

            if (!drawLabel)
            {
                return;
            }

            RenderField(fieldRect, spriteBatch);

            if (invert)
            {
                value = -(value - maxValue);
            }

            float v = 0.0f;

            if (mode == Slider.SliderMode.Float)
            {
                v = (float)Math.Round(value, 2);
            }
            else
            {
                v = (int)value;
            }

            string toDraw = "" + v;

            Vector2 origin = Datastructures.SafeMeasure(font, toDraw) * 0.5f;

            Drawer2D.SafeDraw(spriteBatch, toDraw, font, Color.Black, new Vector2(fieldRect.X + fieldRect.Width / 2, fieldRect.Y + 16), origin);
        }