/*
* Date: 11/4/2005
* Time: 7:14 PM
*
* Copyright 2005, Static Boy Productions
*/
using System;
namespace Jessie.Utils
{
///
/// jtbvTextRect represents an area of text that is of a certain height and width
///
public class jtbvTextRect
{
string[] buffer;
#region Sizing
private int height = 4;
private int width = 4;
///
/// The height of the text rect (number of lines)
///
public int Height
{
get {
return height;
}
set {
if (value > 0)
height = value;
ResizeBuffer();
}
}
///
/// The width of the text rect (number of characters per line)
///
public int Width
{
get {
return width;
}
set {
if (value > 0)
width = value;
ResizeBuffer();
}
}
#endregion Sizing
///
/// Creates a new jtbvTextRect with the parameters specified
///
/// The width of the text rect (in characters)
/// The height of the text rect (in characters)
public jtbvTextRect(int windowWidth, int windowHeight)
{
width = windowWidth;
height = windowHeight;
Clear();
}
///
/// Clears the text rect to contain all spaces.
///
public void Clear()
{
buffer = new string[height];
for (int cnt = 0; cnt < height; cnt++)
{
buffer[cnt] = jtbvUtils.FixedWidth("", width);
}
}
///
/// This function takes care of resizing the buffer when height/width changes occur.
///
private void ResizeBuffer()
{
string[] _buffer = buffer;
buffer = new string[height];
for (int cnt = 0; cnt < height; cnt++)
{
if ((_buffer == null) || (cnt < _buffer.Length))
{
string newString = "";
if (_buffer != null)
if (_buffer[cnt] != null)
newString = _buffer[cnt];
if (newString.Length > width)
{
newString = newString.Substring(width, newString.Length - width);
}
else if (newString.Length < width)
{
newString = jtbvUtils.FixedWidth(newString, width);
//newString = newString.PadRight(width, '$');
}
// SetString(0, cnt, newString);
SetLine(cnt, newString);
}
else
{
SetString(0, cnt, jtbvUtils.FixedWidth("", width));
//SetString(0, cnt, ("FDSA").PadRight(width, '^'));
}
}
}
///
/// Gets a character from the buffer at a specified location
///
/// The X position to retrieve the character at
/// The Y position to retrieve the character at
/// A character at the specified position
public string GetChar(int left, int top)
{
if ((left < width) && (left >= 0))
if ((top < height) && (top >= 0))
return buffer[top][left].ToString();
return " ";
}
///
/// Sets a character at a specific position
///
/// The x position where the character should be placed
/// The top
///
public void SetChar(int left, int top, string newChar)
{
if (newChar.Length > 1)
newChar = newChar.Substring(0, 1);
if (newChar.Length > 0)
if ((left < width) && (left >= 0))
if ((top < height) && (top >= 0))
buffer[top] = buffer[top].Remove(left, newChar.Length).Insert(left, newChar);
// buffer[top][left] = newChar;
}
///
/// Returns a specific line from the text rect
///
/// The y-value (row) of the line to retrieve
/// The line stored at the y-value specified
public string GetLine(int top)
{
if ((top < height) && (top >= 0))
{
if (buffer[top] != null)
{
return jtbvUtils.FixedWidth(buffer[top], Width);
//return jtbvUtils.FixedWidth(buffer[top], Width);
}
else
{
// Console.WriteLine("Buffer[" + top.ToString() + "] == null!!!");
// for (int cnt = 0; cnt < buffer.Length; cnt++)
// {
// Console.WriteLine(" [" + cnt.ToString() + "]{\"" + buffer[cnt] + "\"}");
// }
}
}
return (jtbvUtils.FixedWidth("", width));
}
///
/// Sets the line at a specific row
///
/// The y-value (row) to set
/// The new text to set for the line. If this string is longer/shorter than the width of the text rect, it will be trimmed/extended to length.
public void SetLine(int top, string newText)
{
if ((top < height) && (top >= 0))
{
buffer[top] = jtbvUtils.FixedWidth(newText, width);
}
}
///
/// Gets a string from a specified x/y position in the text rect
///
/// The x (column) coordinate of the start of the desired string
/// The y (row) coordinate of the desired string
/// The number of characters to retrieve from this location
/// The string at the specified location
public string GetString(int left, int top, int length)
{
int startInx = jtbvUtils.CountDisplayChars(GetLine(top), 0, length);
string retVal = "";
if (startInx >= GetLine(top).Length)
{
retVal = GetLine(top).Substring(startInx);
}
retVal = jtbvUtils.FixedWidth(retVal, length);
return retVal;
}
///
/// Sets the string at a specific location in the text rect
///
/// The x position to place the beginning of the string
/// The y position to place the string
/// The text to insert
public void SetString(int left, int top, string newText)
{
if ((left < width) && (top < height) && (left >= 0) && (top >= 0))
{
string oldLine = GetLine(top);
// Console.WriteLine("Old line width = " + oldLine.Length.ToString());
int leftInx = jtbvUtils.CountDisplayChars(oldLine, 0, left);
int newTextDisplayLen = jtbvUtils.StringDisplayWidth(newText);
int rightInx = jtbvUtils.CountDisplayChars(oldLine, leftInx, newTextDisplayLen);
string newLine = oldLine.Remove(leftInx, rightInx - leftInx).Insert(leftInx, newText);
newLine = jtbvUtils.FixedWidth(newLine, width);
// Console.WriteLine("New line width = " + oldLine.Length.ToString());
SetLine(top, newLine);
}
}
public string[] GetRect(int left, int top, int length, int height)
{
string [] retVal;
if (height >= 0)
{
retVal = new string[height];
if (length >= 0)
{
for (int cnt = 0; cnt < height; cnt++)
{
retVal[cnt] = this.GetString(left, top + cnt, length);
}
}
}
else
{
retVal = new string[1];
retVal[0] = "";
}
return retVal;
}
public void SetRect(int left, int top, string[] rect)
{
for (int cnt = 0; cnt < rect.Length; cnt++)
{
this.SetString(left, top + cnt, rect[cnt]);
}
}
///
/// Shifts the contents of the buffer to a particular direction, trimming excess and filling room with whitespace
///
/// A positive value shifts contents to the left, a negative value shifts contents to the right
/// A positive value shifts contents upwards, a negative value shifts contents downwards
public void ShiftBuffer(int horiz, int vert)
{
string[] _buffer = buffer;
buffer = new string[height];
for (int cnt = 0; cnt < height; cnt++)
{
if (((cnt + vert) < _buffer.Length) && ((cnt + vert) > 0))
{
if (horiz < 0)
_buffer[cnt + vert] = jtbvUtils.FixedWidth( _buffer[cnt + vert].Remove(0, -horiz), width, 0);
// _buffer[cnt + vert] = _buffer[cnt + vert].Substring(-horiz, width + horiz).PadRight(width);
else if (horiz > 0)
_buffer[cnt + vert] = jtbvUtils.FixedWidth( _buffer[cnt + vert].Remove(width - horiz, horiz), width, 1);
// _buffer[cnt + vert] = _buffer[cnt + vert].Substring(0, width - horiz).PadLeft(width);
buffer[cnt] = _buffer[cnt + vert];
}
else
{
SetLine(cnt, jtbvUtils.FixedWidth("", this.width));
// SetString(0, cnt, jtbvUtils.FixedWidth("", width));
}
}
}
}
}