//===================================================================================== /// <summary> /// Draws a string right aligned to the specified position with the given /// transform matrix and specified custom font settings. /// </summary> /// /// <param name="text"> text to draw </param> /// <param name="box_pos"> position of box top left corner </param> /// <param name="box_size"> size of box </param> /// <param name="transform"> transform to use for drawing </param> /// <param name="settings"> custom font settings to use </param> //===================================================================================== public void DrawCustomStringRightAligned( string text, Vector2 pos , Matrix transform , CustomFontSettings settings ) { // Only do if everything is ok: if ( text != null && text.Length != 0 ) { // Break up into lines: string[] lines = text.Split( new char[]{ '\n' } ); // Current y offset for the text: float y_offset = 0; // Save the current font settings: float prev_size = m_size; Vector4 prev_color = m_color; float prev_char_spacing = m_char_spacing; float prev_line_spacing = m_line_spacing; Effect prev_effect = m_effect; // Apply new ones: m_size = settings.Size; m_color = settings.Color; m_char_spacing = settings.CharSpacing; m_line_spacing = settings.LineSpacing; m_effect = settings.Effect; // Draw each line: for ( int i = 0 ; i < lines.Length ; i++ ) { // Get the size of the string: Vector2 size = GetStringSize( lines[i] ); // Now figure out what x coordinate to draw it to right align: float x = pos.X - size.X; // Now just use the regular draw string function to draw at figured out position DrawString( lines[i] , new Vector2( x , pos.Y + y_offset ) , transform ); // Increment y offset for the next line: y_offset -= m_line_spacing + m_size; } // Restore old font settings: m_size = prev_size; m_color = prev_color; m_char_spacing = prev_char_spacing; m_line_spacing = prev_line_spacing; m_effect = prev_effect; } }
//===================================================================================== /// <summary> /// Draws a custom formatted string centered horizontally over the given position /// </summary> /// /// <param name="text"> Text to draw. </param> /// <param name="pos"> Position to draw the text at. </param> /// <param name="settings"> Custom font settings to use. </param> //===================================================================================== public void DrawCustomStringCentered( string text, Vector2 pos , CustomFontSettings settings ) { // Only do if everything is ok: if ( text != null && text.Length != 0 ) { // Break up into lines: string[] lines = text.Split( new char[]{ '\n' } ); // Save the current font settings: float prev_size = m_size; Vector4 prev_color = m_color; float prev_char_spacing = m_char_spacing; float prev_line_spacing = m_line_spacing; Effect prev_effect = m_effect; // Apply new ones: m_size = settings.Size; m_color = settings.Color; m_char_spacing = settings.CharSpacing; m_line_spacing = settings.LineSpacing; m_effect = settings.Effect; // Current y offset for the text: float y_offset = 0; // Draw each line: for ( int i = 0 ; i < lines.Length ; i++ ) { // Get the size of the string: Vector2 size = GetStringSize( lines[i] ); // Now figure out what x coord to draw it at to center the string horizontally: float x = pos.X - ( size.X * 0.5f ); // Create an orthographic projection matrix: Matrix projection = Matrix.CreateOrthographic ( Core.Graphics.Device.Viewport.Width , Core.Graphics.Device.Viewport.Height , 0 , 1.0f ); // Now just use the regular draw string function to draw at figured out position DrawString( lines[i] , new Vector2( x , pos.Y + y_offset ) , projection ); // Increment y offset for the next line: y_offset -= m_line_spacing + settings.Size; } // Restore old font settings: m_size = prev_size; m_color = prev_color; m_char_spacing = prev_char_spacing; m_line_spacing = prev_line_spacing; m_effect = prev_effect; } }
//========================================================================================= /// <summary> /// Draws a string at the specified location with custom font settings. /// </summary> /// /// <param name="text"> Text to draw </param> /// <param name="position"> Where to draw the text </param> /// <param name="settings"> Custom font settings to use. </param> //========================================================================================= public void DrawCustomString( string text , Vector2 position , CustomFontSettings settings ) { // Create an orthographic projection matrix: Matrix projection = Matrix.CreateOrthographic ( Core.Graphics.Device.Viewport.Width , Core.Graphics.Device.Viewport.Height , 0 , 1.0f ); // Save the current font settings: float prev_size = m_size; Vector4 prev_color = m_color; float prev_char_spacing = m_char_spacing; float prev_line_spacing = m_line_spacing; Effect prev_effect = m_effect; // Apply new ones: m_size = settings.Size; m_color = settings.Color; m_char_spacing = settings.CharSpacing; m_line_spacing = settings.LineSpacing; m_effect = settings.Effect; // Draw the given string using this matrix: DrawString( text , position , projection); // Restore old font settings: m_size = prev_size; m_color = prev_color; m_char_spacing = prev_char_spacing; m_line_spacing = prev_line_spacing; m_effect = prev_effect; }
//========================================================================================= /// <summary> /// Draws a string at the specified location with custom font settings and the given transform. /// </summary> /// /// <param name="text"> Text to draw </param> /// <param name="position"> Where to draw the text </param> /// <param name="transform"> transform matrix to use </param> /// <param name="settings"> Custom font settings to use. </param> //========================================================================================= public void DrawCustomString( string text , Vector2 position , Matrix transform , CustomFontSettings settings ) { // Save the current font settings: float prev_size = m_size; Vector4 prev_color = m_color; float prev_char_spacing = m_char_spacing; float prev_line_spacing = m_line_spacing; Effect prev_effect = m_effect; // Apply new ones: m_size = settings.Size; m_color = settings.Color; m_char_spacing = settings.CharSpacing; m_line_spacing = settings.LineSpacing; m_effect = settings.Effect; // Draw the given string using the given matrix: DrawString( text , position , transform ); // Restore old font settings: m_size = prev_size; m_color = prev_color; m_char_spacing = prev_char_spacing; m_line_spacing = prev_line_spacing; m_effect = prev_effect; }
//===================================================================================== /// <summary> /// Gets the dimensions for a given string with custom font settings. /// </summary> /// <param name="text"> String to get dimensions of when rendered </param> /// <param name="settings"> Custom font settings to use for the calculations </param> /// <returns> Total size that the string would take up when rendered. </returns> //===================================================================================== public Vector2 GetCustomStringSize( string text , CustomFontSettings settings ) { // If the string is null then return zero: if ( text == null ) return Vector2.Zero; // Split the string into lines: string[] lines = text.Split( '\n' ); // Store the maximum width of all the lines here: float max_w = 0; // Run through all the lines: for ( int i = 0 ; i < lines.Length ; i++ ) { // Store the maximum width of this line here: float line_max_w = 0; // Run through all the characters in this line: for ( int j = 0 ; j < lines[i].Length ; j++ ) { // Get this character: char c = lines[i][j]; // If the draw character is a tab then move out to the next tab space: if ( c == '\t' ) { // Calculate x value of next tab space float tab_x = (int)( ( line_max_w ) / ( settings.Size * 4.0f ) ) * ( settings.Size * 4.0f ) + ( settings.Size * 4.0f ); // Set the new max width of the line: line_max_w = tab_x; continue; } // If the draw character is a space then increment by a fixed size: if ( c == ' ' ){ line_max_w += settings.Size * SPACE_WIDTH_PERCENT; continue; } // Increase the max width of the string according to char metrics and font size / spacing line_max_w += m_metrics[c&0xff].tc_w * 16.0f * settings.Size; // If this is the last character then do not apply char spacing: if ( j < lines[i].Length - 1 ) line_max_w += settings.CharSpacing; } // If this line is longer than the current longest then save: if ( line_max_w > max_w ) max_w = line_max_w; } // Return the dimensions of the text: return new Vector2 ( max_w , lines.Length * settings.Size + MathHelper.Clamp( lines.Length - 1 , 0 , 10000000 ) * settings.LineSpacing ); }