Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- /*
- *
- * <copyright> Merx3- Telerik </copyright>
- *
- */
- public class TestConsole
- {
- struct BufferIndex
- {
- public int Row;
- public int Column;
- public BufferIndex(int r, int c)
- {
- this.Row = r;
- this.Column = c;
- }
- }
- private int windowsWidth;
- private int windowsHeight;
- private int bufferWidth;
- private int bufferHeight;
- private BufferIndex bufferIndex;
- private string input;
- private char[,] buffer;
- private LinkedList<ConsoleKeyInfo> keysAvailable;
- private LinkedList<long> keysTimer;
- public bool KeyAvailable
- {
- get
- {
- if (keysAvailable.Count > 0)
- {
- if (keysTimer == null)
- {
- return true;
- }
- else if (keysTimer.First.Value == 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }
- public int WindowWidth
- {
- get
- {
- return this.windowsWidth;
- }
- set
- {
- if (value <= 0)
- {
- throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
- " than 0(zero) and less than the current window's width.");
- }
- if (value > bufferWidth)
- {
- BufferWidth = value;
- }
- this.windowsWidth = value;
- }
- }
- public int WindowHeight
- {
- get
- {
- return this.windowsHeight;
- }
- private set
- {
- if (value <= 0)
- {
- throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
- " than 0(zero) and less than the current window's width.");
- }
- if (value > bufferHeight)
- {
- BufferHeight = value;
- }
- this.windowsHeight = value;
- }
- }
- public int BufferWidth
- {
- get
- {
- return this.bufferWidth;
- }
- set
- {
- if (value <= 0)
- {
- throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
- " than 0(zero) and less than the current window's width.");
- }
- if (value < windowsWidth)
- {
- throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
- " than 0(zero) and less than the current window's width.");
- }
- this.bufferWidth = value;
- RecreateBuffer();
- }
- }
- public int BufferHeight
- {
- get
- {
- return this.bufferHeight;
- }
- private set
- {
- if (value <= 0)
- {
- throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
- " than 0(zero) and less than the current window's width.");
- }
- if (value < windowsHeight)
- {
- throw new ArgumentOutOfRangeException("Console buffer size must be greater" +
- " than 0(zero) and less than the current window's width.");
- }
- this.bufferWidth = value;
- RecreateBuffer();
- }
- }
- public TestConsole()
- {
- windowsWidth = 80;
- windowsHeight = 25;
- bufferWidth = 80;
- bufferHeight = 300;
- bufferIndex = new BufferIndex(0, 0);
- buffer = new char[bufferHeight, bufferWidth];
- keysAvailable = new LinkedList<ConsoleKeyInfo>();
- }
- private void RecreateBuffer()
- {
- char[,] newBuffer = new char[bufferHeight, bufferWidth];
- for (int row = 0; row < newBuffer.GetLength(0) && row < buffer.GetLength(0); row++)
- {
- for (int column = 0; column < newBuffer.GetLength(1) && column < buffer.GetLength(1); column++)
- {
- newBuffer[row, column] = buffer[row, column];
- }
- }
- if (bufferIndex.Column >= bufferWidth)
- {
- bufferIndex.Row++;
- bufferIndex.Column = 0;
- }
- if (bufferIndex.Row >= bufferHeight)
- {
- PushBufferUp(1);
- }
- }
- /// <summary>
- /// Sets the keys, which will be hit.
- /// </summary>
- /// <param name="keys"></param>
- public void SetKeys(IEnumerable<ConsoleKeyInfo> keys)
- {
- foreach (ConsoleKeyInfo key in keys)
- {
- keysAvailable.AddLast(key);
- }
- }
- /// <summary>
- /// Sets the keys, which will be hit after timer gets 0.
- /// Note: the timers are manual!
- /// </summary>
- /// <param name="keys"></param>
- /// <param name="timers"></param>
- public void SetKeys(IEnumerable<ConsoleKeyInfo> keys, IEnumerable<long> timers)
- {
- keysTimer = new LinkedList<long>();
- foreach (ConsoleKeyInfo key in keys)
- {
- keysAvailable.AddLast(key);
- }
- foreach (long time in timers)
- {
- keysTimer.AddLast(time);
- }
- if (keysTimer.Count != keysAvailable.Count)
- {
- throw new ArgumentException("Each key has to have a timer to it's pushing");
- }
- }
- /// <summary>
- /// Sets the input for the console. Each input must end with "\n" or "\r\n"
- /// </summary>
- /// <param name="input"></param>
- public void SetIn(string input)
- {
- StringBuilder inputString = new StringBuilder(input);
- for (int i = 0; i < inputString.Length; i++)
- {
- if (inputString[i] == '\n')
- {
- if (i > 0)
- {
- if (inputString[i - 1] != '\r')
- {
- inputString.Insert(i, '\r');
- i++;
- }
- }
- else
- {
- inputString.Insert(i, '\r');
- i++;
- }
- }
- }
- this.input = inputString.ToString();
- }
- /// <summary>
- /// Gets the console generated output
- /// </summary>
- /// <returns></returns>
- public string GetOutput()
- {
- StringBuilder output = new StringBuilder();
- int lastNotNullRow = 0;
- for (int row = 0; row < buffer.GetLength(0); row++)
- {
- for (int col = 0; col < buffer.GetLength(1); col++)
- {
- if (buffer[row, col] != '\0')
- {
- for (int i = lastNotNullRow; i < row; i++)
- {
- output.Append("\r\n");
- }
- lastNotNullRow = row;
- output.Append(buffer[row, col]);
- }
- else
- {
- break;
- }
- }
- }
- if (lastNotNullRow != bufferIndex.Row)
- {
- for (int i = lastNotNullRow; i < bufferIndex.Row; i++)
- {
- output.Append("\r\n");
- }
- }
- return output.ToString();
- }
- public string ReadLine()
- {
- string retValue = null;
- if (input == null)
- {
- return null;
- }
- else
- {
- int buffIndex = input.IndexOf('\n');
- if (buffIndex < 0)
- {
- throw new InvalidOperationException("Invalid input format!");
- }
- else
- {
- if (buffIndex == 0)
- {
- return "";
- }
- else
- {
- retValue = input.Substring(0, buffIndex - 1);
- input = input.Substring(buffIndex + 1);
- return retValue;
- }
- }
- }
- }
- public int Read()
- {
- if (input == null)
- {
- return -1;
- }
- else
- {
- int buffIndex = input.IndexOf('\n');
- if (buffIndex < 0)
- {
- throw new InvalidOperationException("Invalid input format!");
- }
- else
- {
- int retValue = (int)input[0];
- input = input.Substring(1);
- return retValue;
- }
- }
- }
- #region Writes
- public void Write(char c)
- {
- WriteInternal(c.ToString());
- }
- public void Write(char[] c)
- {
- WriteInternal(c.ToString());
- }
- public void Write(string s)
- {
- WriteInternal(s.ToString());
- }
- public void Write(int i)
- {
- WriteInternal(i.ToString());
- }
- public void Write(uint i)
- {
- WriteInternal(i.ToString());
- }
- public void Write(bool b)
- {
- WriteInternal(b.ToString());
- }
- public void Write(float f)
- {
- WriteInternal(f.ToString());
- }
- public void Write(long l)
- {
- WriteInternal(l.ToString());
- }
- public void Write(ulong l)
- {
- WriteInternal(l.ToString());
- }
- public void Write(decimal d)
- {
- WriteInternal(d.ToString());
- }
- public void Write(double d)
- {
- WriteInternal(d.ToString());
- }
- public void Write(byte b)
- {
- WriteInternal(b.ToString());
- }
- public void Write(object o)
- {
- WriteInternal(o.ToString());
- }
- public void Write(string format, object o)
- {
- WriteInternal(String.Format(format, o));
- }
- public void Write(string format, object o1, object o2)
- {
- WriteInternal(String.Format(format, o1, o2));
- }
- public void Write(string format, object o1, object o2, object o3)
- {
- WriteInternal(String.Format(format, o1, o2, o3));
- }
- public void Write(string format, params object[] o)
- {
- WriteInternal(String.Format(format, o));
- }
- #endregion
- #region Writelines
- public void WriteLine()
- {
- WriteInternal("\n");
- }
- public void WriteLine(char c)
- {
- WriteInternal(c.ToString() + "\n");
- }
- public void WriteLine(char[] c)
- {
- WriteInternal(c.ToString() + "\n");
- }
- public void WriteLine(string s)
- {
- WriteInternal(s.ToString() + "\n");
- }
- public void WriteLine(int i)
- {
- WriteInternal(i.ToString() + "\n");
- }
- public void WriteLine(uint i)
- {
- WriteInternal(i.ToString() + "\n");
- }
- public void WriteLine(bool b)
- {
- WriteInternal(b.ToString() + "\n");
- }
- public void WriteLine(float f)
- {
- WriteInternal(f.ToString() + "\n");
- }
- public void WriteLine(long l)
- {
- WriteInternal(l.ToString() + "\n");
- }
- public void WriteLine(ulong l)
- {
- WriteInternal(l.ToString() + "\n");
- }
- public void WriteLine(decimal d)
- {
- WriteInternal(d.ToString() + "\n");
- }
- public void WriteLine(double d)
- {
- WriteInternal(d.ToString() + "\n");
- }
- public void WriteLine(byte b)
- {
- WriteInternal(b.ToString() + "\n");
- }
- public void WriteLine(object o)
- {
- WriteInternal(o.ToString() + "\n");
- }
- public void WriteLine(string format, object o)
- {
- WriteInternal(String.Format(format, o) + "\n");
- }
- public void WriteLine(string format, object o1, object o2)
- {
- WriteInternal(String.Format(format, o1, o2) + "\n");
- }
- public void WriteLine(string format, object o1, object o2, object o3)
- {
- WriteInternal(String.Format(format, o1, o2, o3) + "\n");
- }
- public void WriteLine(string format, params object[] o)
- {
- WriteInternal(String.Format(format, o) + "\n");
- }
- #endregion
- private void WriteInternal(string value)
- {
- for (int valueIndex = 0; valueIndex < value.Length; valueIndex++)
- {
- if (value[valueIndex] == '\n')
- {
- bufferIndex.Row++;
- bufferIndex.Column = 0;
- }
- else
- {
- buffer[bufferIndex.Row, bufferIndex.Column] = value[valueIndex];
- bufferIndex.Column++;
- }
- if (bufferIndex.Column >= bufferWidth)
- {
- bufferIndex.Row++;
- bufferIndex.Column = 0;
- }
- if (bufferIndex.Row >= bufferHeight)
- {
- PushBufferUp(1);
- }
- }
- }
- private void PushBufferUp(int rows)
- {
- char[,] nextBuffer = new char[bufferHeight, bufferWidth];
- for (int row = 0; row + rows < bufferHeight; row++)
- {
- for (int col = 0; col < bufferWidth; col++)
- {
- nextBuffer[row, col] = buffer[row + rows, col];
- }
- }
- bufferIndex.Row = windowsHeight - rows;
- bufferIndex.Column = 0;
- }
- public void Clear()
- {
- buffer = new char[bufferHeight, bufferWidth];
- }
- public ConsoleKeyInfo ReadKey()
- {
- if (keysAvailable.Count == 0)
- {
- throw new InvalidOperationException("No keys available in the console keys buffer");
- }
- else
- {
- ConsoleKeyInfo key = keysAvailable.First.Value;
- keysAvailable.RemoveFirst();
- if (keysTimer != null)
- {
- keysTimer.RemoveFirst();
- }
- return key;
- }
- }
- /// <summary>
- /// Makes the key countdown timer decrease with the specified ticks
- /// </summary>
- /// <param name="ticks"></param>
- public void KeyTickTimer(int ticks)
- {
- if (keysTimer != null)
- {
- keysTimer.First.Value -= ticks;
- if (keysTimer.First.Value < 0)
- {
- keysTimer.First.Value = 0;
- }
- }
- }
- public void SetCursorPosition(int column, int row)
- {
- if (column >= bufferWidth || row >= bufferHeight ||
- column < 0 || row < 0)
- {
- throw new IndexOutOfRangeException(String.Format("Buffer index is out of range. Column must be between 0 and {0}." +
- "Row must be between 0 and {1}.", bufferWidth - 1, bufferHeight - 1));
- }
- bufferIndex.Row = row;
- bufferIndex.Column = column;
- for (int c = 0; c < bufferIndex.Column; c++)
- {
- if (buffer[bufferIndex.Row, c] == '\0')
- {
- buffer[bufferIndex.Row, c] = ' ';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement