/*
* Date: 11/25/2005
* Time: 7:38 PM
*
* Copyright 2005, Static Boy Productions
*/
using System;
using System.Drawing;
using Jessie.GameState;
namespace Jessie.Utils
{
///
/// Description of jtbvUtils.
///
public class jtbvUtils
{
private jtbvUtils() {}
///
/// Returns a string that is of the display width specified, whether it needs to add spaces or trim it down.
///
/// Note that this function is sensitive to VT100 coloring escape codes, and therefore if a string contains VT100 coloring, it may return a string longer than the length specified. However, this string will have a *display* length that is correct when it is sent to a user's screen.
/// The string to format
/// The desired display width of the string
/// A modified version of inputStr which is of the correct display width
public static string FixedWidth(string inputStr, int length)
{
return FixedWidth(inputStr, length, 0);
}
///
/// Returns a string that is of the display width specified, whether it needs to add spaces or trim it down.
///
/// Note that this function is sensitive to VT100 coloring escape codes, and therefore if a string contains VT100 coloring, it may return a string longer than the length specified. However, this string will have a *display* length that is correct when it is sent to a user's screen.
/// The string to format
/// The desired display width of the string
/// This parameter specifies how the text should be justified. 0 is left-justified, 0.5 is center, 1 is right-justified. Numbers in between are valid as well.
/// A modified version of inputStr which is of the correct display width
public static string FixedWidth(string inputStr, int length, double xOffset)
{
string result = "";
int targetLength = length;
for (int cnt = 0; cnt < targetLength; cnt++)
{
char testChar;
if (cnt >= inputStr.Length)
testChar = ' ';
else
testChar = inputStr[cnt];
if (testChar == (char) 0x1B) // If we find a color code
targetLength += 10; // Increase our target length by 10
result += testChar;
}
return result;
}
///
/// Gets the display length of a string, accounting for VT100 coloring escape codes.
///
/// The string to test for display length
/// The number of characters that this sting will display as on a client's Telnet screen
public static int StringDisplayWidth(string inputStr)
{
int retVal = 0;
for (int cnt = 0; cnt < inputStr.Length; cnt++)
{
if (inputStr[cnt] == (char) 0x1B) // If we find a color code
cnt += 9; // Skip the next 9 characters
else
retVal++;
}
return retVal;
}
///
/// This function looks at a string, counts from the start index over the number of display characters specified, and then returns that end index.
///
/// This is used by the jtbvTextRect.SetString() function
/// The string to process
/// The index to start counting from
/// The number of display characters to count
/// The index that is the specified number of display characters away from the starting index
public static int CountDisplayChars(string inputStr, int startInx, int countChars)
{
int len = 0; // How many characters we've counted thus far
int inx = 0; // The current index we're looking at
for (inx = startInx; len < countChars; inx++)
{
if (inputStr[inx] == (char) 0x1B) // If we find a color code
inx += 9; // Skip the next 9 characters
else
len++;
}
return inx;
}
///
/// Prints the information of a string to the console for debug purposes
///
/// This function is intended ONLY for debug purposes
/// The string to print byte-by-byte
public static void HexPrint(string printStr)
{
for (int cnt = 0; cnt < printStr.Length; cnt++)
{
char tmp = printStr[cnt];
Console.Write ("(" + ((int) tmp).ToString() + ") ");
if ((tmp > 32) && (tmp < 126))
{
Console.WriteLine (tmp);
}
else
{
Console.WriteLine();
}
}
}
public static void HexPrint(byte[] printStr, int numBytes)
{
for (int cnt = 0; (cnt < printStr.Length) && (cnt < numBytes); cnt++)
{
char tmp = (char) printStr[cnt];
Console.Write ("(" + (printStr[cnt]).ToString() + ") ");
if ((tmp > 32) && (tmp < 126))
{
Console.WriteLine (tmp);
}
else
{
Console.WriteLine();
}
}
}
public static string GetDirectionFromPoint(Point Start, Point Target)
{
string retVal = ".";
if ((Target.X < Start.X) &&
(Target.Y < Start.Y))
retVal = "y";
else if ((Target.X > Start.X) &&
(Target.Y < Start.Y))
retVal = "u";
else if ((Target.X < Start.X) &&
(Target.Y > Start.Y))
retVal = "b";
else if ((Target.X > Start.X) &&
(Target.Y > Start.Y))
retVal = "n";
else if (Target.X < Start.X)
retVal = "h";
else if (Target.X > Start.X)
retVal = "l";
else if (Target.Y < Start.Y)
retVal = "k";
else if (Target.Y > Start.Y)
retVal = "j";
return retVal;
}
public static bool PointsAreSquared ( jtbvMapSpace a, jtbvMapSpace b )
{
return ((a.Loc.X == b.Loc.X) || (a.Loc.Y == b.Loc.Y));
}
}
}