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); } }
/// <summary> /// Handles initialization of the GameScreen. This will be invoked after the GameScreen has been /// completely and successfully added to the ScreenManager. It is highly recommended that you /// use this instead of the constructor. This is invoked only once. /// </summary> public override void Initialize() { BasicConfigurator.Configure(_logger); ScreenManager.Updated -= ScreenManager_Updated; ScreenManager.Updated += ScreenManager_Updated; _consoleFont = ScreenManager.Content.LoadFont("Font/Courier New", 14, ContentLevel.GameScreen); _cScreen = new Panel(GUIManager, Vector2.Zero, ScreenManager.ScreenSize) { Border = ControlBorder.Empty }; // The main console output textbox _txtOutput = new TextBox(_cScreen, Vector2.Zero, _cScreen.Size) { Font = _consoleFont, Size = _cScreen.Size, Border = ControlBorder.Empty, MaxBufferSize = 200, BufferTruncateSize = 80, CanFocus = false, CanDrag = false, IsMultiLine = true, IsEnabled = false }; _txtOutput.Resized += delegate { UpdateConsoleBufferSize(); }; _txtOutput.FontChanged += delegate { UpdateConsoleBufferSize(); }; // Create the panel that holds the settings options var settingsButtonSize = _consoleFont.MeasureString("Show Settings") + new Vector2(10, 4); _btnShowSettings = new Button(_cScreen, new Vector2(_cScreen.Size.X, 0), settingsButtonSize) { Font = _consoleFont, Text = "Hide Settings" }; _btnShowSettings.Position += new Vector2(-4, 4); _btnShowSettings.Clicked += btnShowSettings_Clicked; var settingsPanelSize = new Vector2(400, 400); _cSettingsPanel = new Panel(_cScreen, new Vector2(_cScreen.Size.X - settingsPanelSize.X - 4, _btnShowSettings.Position.Y + _btnShowSettings.Size.Y + 8), settingsPanelSize) { IsVisible = true, CanDrag = false }; // Create the logging level checkboxes _logLevelCheckBoxes.Add(CreateLogLevelCheckBox(Level.Fatal, 0)); _logLevelCheckBoxes.Add(CreateLogLevelCheckBox(Level.Error, 1)); _logLevelCheckBoxes.Add(CreateLogLevelCheckBox(Level.Warn, 2)); _logLevelCheckBoxes.Add(CreateLogLevelCheckBox(Level.Info, 3)); _logLevelCheckBoxes.Add(CreateLogLevelCheckBox(Level.Debug, 4)); // Disable debug by default _logLevelCheckBoxes.First(x => (Level)x.Tag == Level.Debug).Value = false; }
public static List<List<StyledText>> ToMultiline(IEnumerable<StyledText> texts, bool putInputOnNewLines, Font font, int maxLineLength) { var lines = ToMultiline(texts, putInputOnNewLines); var ret = new List<List<StyledText>>(); var currentLineText = new StringBuilder(); var linePartsStack = new Stack<StyledText>(); foreach (var line in lines) { currentLineText.Length = 0; var retLine = new List<StyledText>(); // Add all the pieces in the line to the line parts stack for (var i = line.Count - 1; i >= 0; i--) { linePartsStack.Push(line[i]); } while (linePartsStack.Count > 0) { var current = linePartsStack.Pop(); currentLineText.Append(current.Text); // Check if the line has become too long if (font.MeasureString(currentLineText).X <= maxLineLength) retLine.Add(current); else { var s = currentLineText.ToString(); var lastFittingChar = FindLastFittingChar(s, font, maxLineLength); var splitAt = FindIndexToSplitAt(s, lastFittingChar - 1); splitAt -= (s.Length - current.Text.Length); // Don't try to split farther back than the current styled text block unless the character is larger // than the maximum line length. In which case, we just throw the character into the line (not much // else we can do other than just show nothing, and that is no better...) if (retLine.Count == 0 && splitAt < 0) splitAt = 0; if (splitAt + 1 == current.Text.Length) retLine.Add(current); else { var left = current.Substring(0, splitAt + 1); var right = current.Substring(splitAt + 1); // Split like normal if (left.Text.Length > 0) retLine.Add(left); linePartsStack.Push(right); } // Add the line to the return list, create the new line buffer, and reset the line string if (retLine.Count > 0) { ret.Add(retLine); retLine = new List<StyledText>(); } currentLineText.Length = 0; } } if (retLine.Count > 0) { Debug.Assert(ret.Count == 0 || ret.Last() != retLine, "Uh-oh, we added the same line twice..."); ret.Add(retLine); } } return ret; }
/// <summary> /// Gets the width of this <see cref="StyledText"/> for the given <paramref name="font"/>. /// </summary> /// <param name="font">The <see cref="Font"/> to use to measure.</param> /// <returns>The width of this <see cref="StyledText"/> for the given <paramref name="font"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="font" /> is <c>null</c>.</exception> public float GetWidth(Font font) { if (font == null) throw new ArgumentNullException("font"); // Re-measure (or measure for the first time if (_fontUsedToMeasure == null || _fontUsedToMeasure != font) { _width = font.MeasureString(Text).X; _fontUsedToMeasure = font; } return _width; }
/// <summary> /// Finds the 0-based index of the last character that will fit into a single line. /// </summary> /// <param name="text">The text to check.</param> /// <param name="font">The <see cref="Font"/> used to measure the length.</param> /// <param name="maxLineLength">The maximum allowed line length in pixels.</param> /// <returns>The 0-based index of the last character that will fit into a single line.</returns> public static int FindLastFittingChar(string text, Font font, int maxLineLength) { var sb = new StringBuilder(text); for (var i = 0; i < text.Length; i++) { var length = font.MeasureString(sb).X; if (length <= maxLineLength) return sb.Length; sb.Length--; } return 0; }
/// <summary> /// Initializes a new instance of the <see cref="InfoBoxItem"/> struct. /// </summary> /// <param name="time">Current time.</param> /// <param name="msg">Message to display.</param> /// <param name="color">Color of the text.</param> /// <param name="font"><see cref="Font"/> that will be used to calculate the Width.</param> public InfoBoxItem(TickCount time, string msg, Color color, Font font) { CreatedTime = time; Message = msg; Color = color; Width = font.MeasureString(msg).X; }