// Reads a line in the script as a command. protected override bool ReadCommand(string command, List<string> args) { // Create a new font. // @font [fontName] if (command == "font") { Image image = null; if (args[0].EndsWith(".png")) { image = Resources.LoadImageFromFile(Resources.ImageDirectory + args[0]); args[0] = args[0].Substring(0, args[0].LastIndexOf('.')); } else { image = Resources.LoadImage(Resources.ImageDirectory + args[0]); } SpriteSheet sheet = new SpriteSheet(image, Point2I.One, Point2I.Zero, Point2I.Zero); font = new GameFont(sheet, 1, 0, 1); finalFont = font; Resources.AddSpriteSheet(args[0], sheet); Resources.AddGameFont(args[0], font); } // Define the sprite sheet. // @grid [char_width] [char_height] [char_spacing_x] [char_spacing_y] [offset_x] [offset_y] if (command == "grid") { font.SpriteSheet.CellSize = new Point2I(Int32.Parse(args[0]), Int32.Parse(args[1])); font.SpriteSheet.Spacing = new Point2I(Int32.Parse(args[2]), Int32.Parse(args[3])); font.SpriteSheet.Offset = new Point2I(Int32.Parse(args[4]), Int32.Parse(args[5])); } // Define the font spacing. // @spacing [char_spacing] [line_spacing] [chars_per_row] if (command == "spacing") { font.CharacterSpacing = Int32.Parse(args[0]); font.LineSpacing = Int32.Parse(args[1]); font.CharactersPerRow = Int32.Parse(args[2]); } // Finish creating the current font. // @end else if (command == "end") { font = null; } // Invalid command else { return false; } return true; }
//----------------------------------------------------------------------------- // Constructor //----------------------------------------------------------------------------- public GameFontSR() { //===================================================================================== AddCommand("Font", "string name", delegate(CommandParam parameters) { String path = parameters.GetString(0); Image image = null; if (path.EndsWith(".png")) { image = Resources.LoadImageFromFile(Resources.ImageDirectory + path); path = path.Substring(0, path.LastIndexOf('.')); } else { image = Resources.LoadImage(Resources.ImageDirectory + path); } SpriteSheet sheet = new SpriteSheet(image, Point2I.One, Point2I.Zero, Point2I.Zero); font = new GameFont(sheet, 1, 0, 1); finalFont = font; Resources.AddSpriteSheet(path, sheet); Resources.AddGameFont(path, font); }); //===================================================================================== AddCommand("Grid", "(int charWidth, int charHeight), (int charSpacingX, int charSpacingY), (int offsetX, int offsetY)", delegate(CommandParam parameters) { font.SpriteSheet.CellSize = parameters.GetPoint(0); font.SpriteSheet.Spacing = parameters.GetPoint(1); font.SpriteSheet.Offset = parameters.GetPoint(2); }); //===================================================================================== AddCommand("Spacing", "int charSpacing, int lineSpacing, int charsPerRow", delegate(CommandParam parameters) { font.CharacterSpacing = parameters.GetInt(0); font.LineSpacing = parameters.GetInt(1); font.CharactersPerRow = parameters.GetInt(2); }); //===================================================================================== AddCommand("End", "", delegate(CommandParam parameters) { font = null; }); //===================================================================================== }
// Draws a wrapped game string at the specified position public void DrawWrappedString(GameFont font, string text, int width, Point2I position, Color color, float depth = 0.0f) { DrawWrappedLetterString(font, font.WrapString(text, width), width, position, color, depth); }
// Draws a formatted wrapped game string at the specified position public void DrawWrappedLetterString(GameFont font, WrappedLetterString wrappedString, int width, Point2I position, Color color, float depth = 0.0f) { for (int i = 0; i < wrappedString.Lines.Length; i++) { DrawLetterString(font, wrappedString.Lines[i], position + new Point2I(0, font.LineSpacing * i), color, depth); } }
// Draws a game string at the specified position public void DrawString(GameFont font, string text, Point2I position, Color color, float depth = 0.0f) { DrawLetterString(font, FormatCodes.FormatString(text), position, color, depth); }
// Draws a formatted game string at the specified position public void DrawLetterString(GameFont font, LetterString letterString, Point2I position, Color color, float depth = 0.0f) { for (int i = 0; i < letterString.Length; i++) { spriteBatch.Draw( font.SpriteSheet.Image, NewRect(new Rectangle2I( position.X + i * (font.SpriteSheet.CellSize.X + font.CharacterSpacing), position.Y, font.SpriteSheet.CellSize.X, font.SpriteSheet.CellSize.Y )), (Rectangle)new Rectangle2I( font.SpriteSheet.Offset.X + ((int)letterString[i].Char % font.CharactersPerRow) * (font.SpriteSheet.CellSize.X + font.SpriteSheet.Spacing.X), font.SpriteSheet.Offset.Y + ((int)letterString[i].Char / font.CharactersPerRow) * (font.SpriteSheet.CellSize.Y + font.SpriteSheet.Spacing.Y), font.SpriteSheet.CellSize.X, font.SpriteSheet.CellSize.Y ), (letterString[i].Color == Letter.DefaultColor ? color : letterString[i].Color), 0.0f, Vector2.Zero, SpriteEffects.None, depth ); } }
// Ends reading the script. protected override void EndReading() { font = null; }
//----------------------------------------------------------------------------- // Override //----------------------------------------------------------------------------- // Begins reading the script. protected override void BeginReading() { font = null; finalFont = null; }