public Vec2 MeasureString(string text, int height, DrawTextFormat format) { SlimDX.Direct3D9.Font font = this.GetFont(height); Rectangle rectangle = font.MeasureString(this.textSprite, text, format); return(new Vec2(rectangle.Width, rectangle.Height)); }
public override void AllocateResources() { DisplayFont = new SlimDX.Direct3D9.Font(Panel.MainPlot.Device, new System.Drawing.Font("Lucida Console", 16)); DisplayFont.MeasureString(null, "X", DrawTextFormat.Center, ref FontSizeRect); PositionUpdated = true; }
public Vec2 AddTextWithHeight(Vec2 pos, string text, Color color, int height, DrawTextFormat format) { SlimDX.Direct3D9.Font font = this.GetFont(height); Rectangle rectangle = font.MeasureString(this.textSprite, text, format); rectangle.X += pos.X; rectangle.Y += pos.Y; font.DrawString(this.textSprite, text, rectangle, format, color); return(new Vec2(rectangle.Width, rectangle.Height)); }
public Vector2 MeasureString(Vector2 position, string text, float emSize) { Matrix matScale = Matrix.Scaling(emSize / 30.0f, emSize / 30.0f, 1); var sprite = FontManager.Sprite; var oldTransform = sprite.Transform; sprite.Transform = matScale * Matrix.Translation(position.X, position.Y, 0); var rect = mBaseFont.MeasureString(sprite, text, DrawTextFormat.Center); sprite.Transform = oldTransform; var vec = new Vector2(rect.Width, rect.Height); return(Vector2.TransformCoordinate(vec, matScale)); }
/// <summary> Draws text. </summary> /// <param name="text"> The text. </param> /// <param name="x"> The x coordinate. </param> /// <param name="y"> The y coordinate. </param> /// <param name="color"> The color. </param> /// <param name="emSize"> (optional) size of the em. </param> /// <param name="fontStyle"> (optional) the font style. </param> public static void DrawText(string text, int x, int y, Color color, float emSize = 12f, FontStyle fontStyle = FontStyle.Regular) { if (_fontSprite == null) { _fontSprite = new Sprite(Device); } _fontSprite.Begin(SpriteFlags.AlphaBlend); using (Font font = new Font(Device, new System.Drawing.Font(FontFamily.GenericMonospace, emSize, fontStyle))) { Rectangle stringRect = font.MeasureString(_fontSprite, text, DrawTextFormat.Left); Rectangle rect = new Rectangle(x, y, stringRect.Width + 1, stringRect.Height + 1); font.DrawString(_fontSprite, text, rect, DrawTextFormat.Left, color.ToArgb()); _fontSprite.End(); } }
public Vec2 AddTextWithHeightAndOutline(Vec2 pos, string text, Color color, Color outLine, int height, DrawTextFormat format, int outlineOsset = 1) { SlimDX.Direct3D9.Font font = this.GetFont(height); Rectangle rectangle = font.MeasureString(this.textSprite, text, format); rectangle.X += pos.X; rectangle.Y += pos.Y; rectangle.X -= 1 * outlineOsset; rectangle.Y -= 1 * outlineOsset; font.DrawString(this.textSprite, text, rectangle, format, outLine); rectangle.Y += 2 * outlineOsset; font.DrawString(this.textSprite, text, rectangle, format, outLine); rectangle.X += 2 * outlineOsset; font.DrawString(this.textSprite, text, rectangle, format, outLine); rectangle.Y -= 2 * outlineOsset; font.DrawString(this.textSprite, text, rectangle, format, outLine); rectangle.X -= 1 * outlineOsset; rectangle.Y += 1 * outlineOsset; font.DrawString(this.textSprite, text, rectangle, format, color); return(new Vec2(rectangle.Width, rectangle.Height)); }
private void AddLine(string text) { Rectangle stringSizeRect = new Rectangle(); DisplayFontBold.MeasureString(null, text, DrawTextFormat.Center, ref stringSizeRect); DisplayFontNormal.DrawString(null, text, TextShadowRect, DrawTextFormat.Left | DrawTextFormat.Top, (int)ShadowColor); DisplayFontNormal.DrawString(null, text, TextRect, DrawTextFormat.Left | DrawTextFormat.Top, (int)TextColor); TextRect.Y += FontSizeRect.Height; TextShadowRect.Y += FontSizeRect.Height; if (stringSizeRect.Width > Width) { Width = stringSizeRect.Width; Panel.ExpandDock(this); } if (TextRect.Y > Height) { Height = TextRect.Y; Panel.ExpandDock(this); } }
// Calculates wrapping and places in WrappedMessage. public void WrapMessage(SlimDX.Direct3D9.Font overlayFont, int width) { try { // Rectangle to store the dimensions in. Rectangle rect = new Rectangle(); // Measure the sender string. overlayFont.MeasureString(null, Sender, DrawTextFormat.SingleLine, ref rect); SenderWidth = rect.Width; width -= SenderWidth; // List containing the split lines. List <String> lines = new List <String>(); // The start index for the measured substring. int start = 0; // The length of the measured substring. int length = 1; // The last location of a space char. int lastSpace = -1; // The current substring. String text; // Split the message into lines. while (start + length - 1 < Content.Length) { // Retrieve a substring of the message. text = Content.Substring(start, length); // Check if the last char is whitespace. char c = Content[start + length - 1]; if (c == ' ') { lastSpace = length; } // Measure the substring. overlayFont.MeasureString(null, text, DrawTextFormat.SingleLine, ref rect); if (rect.Width > width) { // Split the message. // Attempt to avoid splitting words in the middle. if (length > 1) { if (lastSpace != -1) { length = lastSpace; } else { length--; } } lines.Add(Content.Substring(start, length)); start += length; length = 1; lastSpace = -1; continue; } else if (start + length >= Content.Length) { // Add the last line. lines.Add(text); break; } length++; } // Store the lines. WrappedMessage = lines.ToArray(); // Don't re-attempt the wrapping. FontUsed = overlayFont; } // Don't interrupt the drawing of the other messages. catch { // Do not re-attempt. WrappedMessage = new String[0]; FontUsed = overlayFont; } }
public override void Render() { if (!Active) { return; } int xPos = AbsoluteXPosition; int yPos = AbsoluteYPosition; if (PositionUpdated) { DisplayFont.MeasureString(null, Text, DrawTextFormat.Center, ref SizeRect); AbsoluteHeight = SizeRect.Height; AbsoluteWidth = SizeRect.Width; } if (xPos < 0) { xPos += MainPlot.DirectXWidth - AbsoluteWidth; } if (yPos < 0) { yPos += MainPlot.DirectXHeight - AbsoluteHeight; } if (PositionUpdated) { PositionUpdated = false; BorderVertexesUsed = 0; BorderVertexesUsed = BuildRectangle(BorderVertexes, BorderVertexesUsed, xPos - 7, xPos + 7 + AbsoluteWidth, yPos - 4, yPos + 4 + AbsoluteHeight, 0x7FFF0000); BorderVertexesUsed = BuildRectangle(BorderVertexes, BorderVertexesUsed, xPos - 6, xPos + 6 + AbsoluteWidth, yPos - 3, yPos + 3 + AbsoluteHeight, 0xFFFF0000); BorderVertexesUsed = BuildRectangle(BorderVertexes, BorderVertexesUsed, xPos - 5, xPos + 5 + AbsoluteWidth, yPos - 2, yPos + 2 + AbsoluteHeight, 0x7FFF0000); BodyVertexesUsed = 0; BodyVertexesUsed = BuildFilledRectangle(BodyVertexes, BodyVertexesUsed, xPos - 6, xPos + 6 + AbsoluteWidth, yPos - 3, yPos + 3 + AbsoluteHeight, 0x1FFF0000); } if (Pulsing) { double maxAngle = 10 * Math.PI; /* -1 .. 1 -> 0.5 .. 1 */ double sinVal = (Math.Sin(UpdateCounter++ / maxAngle) + 1) / 4 + 0.5; TextColor.Alpha = (float)Math.Max(0, Math.Min(1, sinVal)); ShadowColor.Alpha = (float)Math.Max(0, TextColor.Alpha - 0.5); } else { TextColor.Alpha = 1; ShadowColor.Alpha = 1; } MainPlot.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, BodyVertexes); if (BorderVertexesUsed > 0) { MainPlot.Device.DrawUserPrimitives(PrimitiveType.LineList, BorderVertexesUsed / 2, BorderVertexes); } DisplayFont.DrawString(null, Text, xPos + 2, yPos + 2, ShadowColor); DisplayFont.DrawString(null, Text, xPos, yPos, TextColor); }
public override void Render() { bool docksChanged = false; /* to speed up processing */ if (!HasDocks) { DocksChanged = false; return; } /* fade alpha value */ if (WantedTitleBarAlpha >= CurrentTitleBarAlpha) { CurrentTitleBarAlpha = WantedTitleBarAlpha; } else { if (CurrentTitleBarAlpha > 0) { CurrentTitleBarAlpha = Math.Max(0, CurrentTitleBarAlpha - 0.02f); docksChanged = true; } } int xPos = 0; int yPos = 0; switch (Orientation) { case eOrientation.RightBorder: xPos = MainPlot.DirectXWidth; yPos = DockStartY; break; case eOrientation.LeftBorder: xPos = 0; yPos = DockStartY; break; case eOrientation.TopBorder: xPos = DockStartX; yPos = 0; break; case eOrientation.BottomBorder: xPos = DockStartX; yPos = MainPlot.DirectXHeight; break; } if (DocksChanged) { lock (Docks) { Rectangle sizeRect = new Rectangle(); LeftMostPosition = MainPlot.DirectXWidth; RightMostPosition = 0; TopMostPosition = MainPlot.DirectXHeight; BottomMostPosition = 0; foreach (Dock dock in Docks) { DockPanelPrivate priv = dock.Private; priv.TitleBodyVertexesUsed = 0; priv.TitleBorderVertexesUsed = 0; priv.BackTitleBodyVertexesUsed = 0; priv.BackTitleBorderVertexesUsed = 0; DisplayFont.MeasureString(null, dock.Title, DrawTextFormat.Center, ref sizeRect); switch (Orientation) { case eOrientation.BottomBorder: case eOrientation.TopBorder: /* add some space around the text */ priv.TitleHitRect.Width = sizeRect.Width + BorderSpaceHor * 2; priv.TitleHitRect.Height = sizeRect.Height + BorderSpaceVert * 2; if (dock.HideBackTitle) { priv.BackTitleHitRect.Width = 0; priv.BackTitleHitRect.Height = 0; } else { priv.BackTitleHitRect.Width = sizeRect.Width + BorderSpaceHor * 2; priv.BackTitleHitRect.Height = sizeRect.Height + BorderSpaceVert * 2; } break; case eOrientation.LeftBorder: case eOrientation.RightBorder: /* add some space around the text */ priv.TitleHitRect.Width = sizeRect.Height + BorderSpaceVert * 2; priv.TitleHitRect.Height = sizeRect.Width + BorderSpaceHor * 2; if (dock.HideBackTitle) { priv.BackTitleHitRect.Width = 0; priv.BackTitleHitRect.Height = 0; } else { priv.BackTitleHitRect.Width = sizeRect.Height + BorderSpaceVert * 2; priv.BackTitleHitRect.Height = sizeRect.Width + BorderSpaceHor * 2; } break; } switch (dock.State) { case eDockState.Expanding: dock.PositionUpdated = true; if (dock.Private.VisiblePart > 0.99f) { dock.State = dock.WantedState; dock.Private.VisiblePart = 1; } else { dock.Private.VisiblePart += (1 - dock.Private.VisiblePart) / DockSlideFact + 0.001; } /* force update again */ docksChanged = true; break; case eDockState.Collapsing: dock.PositionUpdated = true; if (dock.Private.VisiblePart < 0.01f) { dock.State = dock.WantedState; dock.Private.VisiblePart = 0; } else { dock.Private.VisiblePart -= dock.Private.VisiblePart / DockSlideFact + 0.001; } /* force update again */ docksChanged = true; break; default: break; } /* set dock position */ dock.XPosition = DockX(xPos, dock); dock.YPosition = DockY(yPos, dock); dock.PositionUpdated = true; /* update the hit rectangle */ priv.TitleHitRect.X = DockTitleX(dock); priv.TitleHitRect.Y = DockTitleY(dock); priv.BackTitleHitRect.X = DockBackTitleX(dock); priv.BackTitleHitRect.Y = DockBackTitleY(dock); /* draw the title bar */ uint color1 = TitleColor1 | TitleBarAlpha; uint color2 = TitleColor2 | TitleBarAlpha; uint colorBorder = TitleBorder | TitleBarAlpha; if (priv.TitleHighlighted) { color1 = TitleColorHigh1 | TitleBarAlpha; color2 = TitleColorHigh2 | TitleBarAlpha; } int titleX = DockTitleX(dock); int titleY = DockTitleY(dock); int backTitleX = DockBackTitleX(dock); int backTitleY = DockBackTitleY(dock); int titleW = priv.TitleHitRect.Width; int titleH = priv.TitleHitRect.Height; int backTitleW = priv.BackTitleHitRect.Width; int backTitleH = priv.BackTitleHitRect.Height; if (FadeOutByDistance) { LeftMostPosition = Math.Min(titleX, LeftMostPosition); RightMostPosition = Math.Max(titleX, RightMostPosition); TopMostPosition = Math.Min(titleY, TopMostPosition); BottomMostPosition = Math.Max(titleY, BottomMostPosition); } priv.TitleBodyVertexesUsed = BuildFilledRectangle(priv.TitleBodyVertexes, priv.TitleBodyVertexesUsed, titleX, titleX + titleW, titleY, titleY + titleH, color1, color1, color2, color2); priv.TitleBorderVertexesUsed = BuildRectangle(priv.TitleBorderVertexes, priv.TitleBorderVertexesUsed, titleX, titleX + titleW, titleY, titleY + titleH, colorBorder); priv.BackTitleBodyVertexesUsed = BuildFilledRectangle(priv.BackTitleBodyVertexes, priv.BackTitleBodyVertexesUsed, backTitleX, backTitleX + backTitleW, backTitleY, backTitleY + backTitleH, color1, color1, color2, color2); priv.BackTitleBorderVertexesUsed = BuildRectangle(priv.BackTitleBorderVertexes, priv.BackTitleBorderVertexesUsed, backTitleX, backTitleX + backTitleW, backTitleY, backTitleY + backTitleH, colorBorder); switch (Orientation) { case eOrientation.BottomBorder: case eOrientation.TopBorder: xPos += priv.TitleHitRect.Width; break; case eOrientation.LeftBorder: case eOrientation.RightBorder: yPos += priv.TitleHitRect.Height; break; } } } } /* first render dock content */ lock (Docks) { foreach (Dock dock in Docks) { if (dock.State != eDockState.Collapsed && dock.State != eDockState.Hidden) { dock.Render(); } } } /* later render dock border stuff */ lock (Docks) { foreach (Dock dock in Docks) { if (dock.State != eDockState.Hidden) { DockPanelPrivate priv = dock.Private; if (priv.TitleBodyVertexesUsed - 2 > 0) { MainPlot.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, priv.TitleBodyVertexesUsed - 2, priv.TitleBodyVertexes); } if (priv.TitleBorderVertexesUsed > 0) { MainPlot.Device.DrawUserPrimitives(PrimitiveType.LineList, priv.TitleBorderVertexesUsed / 2, priv.TitleBorderVertexes); } /* back titles are not visible for collapsed docks */ if (dock.State != eDockState.Collapsed) { if (priv.BackTitleBodyVertexesUsed - 2 > 0) { MainPlot.Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, priv.BackTitleBodyVertexesUsed - 2, priv.BackTitleBodyVertexes); } if (priv.BackTitleBorderVertexesUsed > 0) { MainPlot.Device.DrawUserPrimitives(PrimitiveType.LineList, priv.BackTitleBorderVertexesUsed / 2, priv.BackTitleBorderVertexes); } } if (priv.TitleSprite == null) { priv.TitleSprite = new Sprite(MainPlot.Device); } priv.TitleSprite.Begin(SpriteFlags.AlphaBlend); switch (Orientation) { case eOrientation.BottomBorder: case eOrientation.TopBorder: /* draw the text */ DisplayFont.DrawString(priv.TitleSprite, dock.Title, priv.TitleHitRect.X + BorderSpaceHor, priv.TitleHitRect.Y + BorderSpaceVert, (int)(TextColor | TitleBarAlpha)); break; case eOrientation.LeftBorder: case eOrientation.RightBorder: /* rotate drawing sprite */ priv.TitleSprite.Transform = Matrix.RotationZ(-(float)Math.PI / 2); /* draw the text */ DisplayFont.DrawString(priv.TitleSprite, dock.Title, -priv.TitleHitRect.Y - priv.TitleHitRect.Height + BorderSpaceHor, priv.TitleHitRect.X + BorderSpaceVert, (int)(TextColor | TitleBarAlpha)); break; } priv.TitleSprite.End(); } } } DocksChanged = docksChanged; }