private void FillBuffer(string text, int row, int col, int colLen) { //TODO Move this code out of here to ColorMap. ConsoleColor foregroundCopy = ConsoleColor.White; ConsoleColor backgroundCopy = ConsoleColor.Black; bool fcInMap = Renderer.ColorMap.TryGetMappedColor(ForegroundColor, out foregroundCopy); bool bcInMap = Renderer.ColorMap.TryGetMappedColor(BackgroundColor, out backgroundCopy); if (fcInMap == false) //add this color to map. { Renderer.ColorMap.AddColor(ForegroundColor); Renderer.ColorMap.TryGetMappedColor(ForegroundColor, out foregroundCopy); } if (bcInMap == false) { Renderer.ColorMap.AddColor(BackgroundColor); Renderer.ColorMap.TryGetMappedColor(BackgroundColor, out backgroundCopy); } int textIndex = 0; for (int idx = col; idx < col + colLen; idx++) { if (idx >= Options.BufferSize.X) { break; } //Clear this char if (text == null || text.Length == 0) { this.contentBuffer.Cells[row, idx].Char = 0; this.contentBuffer.Cells[row, idx].Attributes = 0; } else { if (textIndex < text.Length) { this.contentBuffer.Cells[row, idx].Char = (ushort)text[textIndex]; } this.contentBuffer.Cells[row, idx].Attributes = (ushort)DotConsoleNative.ToNativeConsoleColor(foregroundCopy, backgroundCopy); } textIndex++; } }
/// <summary> /// Writes lines of text into the output buffer at a specified coordinates. /// </summary> /// <param name="orgin"></param> /// <param name="content"></param> public void WriteOutput(Coordinates orgin, string[] content) { int lineId = 0; int charId = 0; //Find the widest line and set the OutputCell matrix to such width var max = content.Max(x => x.Length); CellBuffer buffer = new CellBuffer(content.Length, max); foreach (var line in content) { charId = 0; foreach (var c in line) { buffer.Cells[lineId, charId].Char = (ushort)c; buffer.Cells[lineId, charId].Attributes = (ushort)DotConsoleNative.ToNativeConsoleColor(ForegroundColor, BackgroundColor); charId++; } } WriteOutput(orgin, buffer); }
public void WriteOutput(Coordinates orgin, CellBuffer cellBuffer) { var handle = GetOutputBuffer(); //Get len of X coordinate, the plan here is to partition by Y var sizeOfX = cellBuffer.GetLengthOfX() * 4; //partition by Y coordinate. int partitionY = (int)Math.Ceiling((decimal)(maxBufferSize / sizeOfX)); var sizeOfY = cellBuffer.GetLengthOfY(); //if Y is smaller then the partition by Y then we need //to set the partiton size to Y size. if (sizeOfY < partitionY) { partitionY = sizeOfY; } //Get partitoned buffer size. int charBufferSize = (int)(partitionY * cellBuffer.GetLengthOfX()); int cursor = 0; int i = 0; do { i += partitionY; //If we exceeded the maximum size of the buffer we need to substract to the size of the remaining buffer. if (i > sizeOfY) { int diff = i - sizeOfY; i = i - diff; } //Fill the buffer part. ConsoleHostNativeMethods.CHAR_INFO[] buffer = new ConsoleHostNativeMethods.CHAR_INFO[charBufferSize]; int idx = 0; for (int y = cursor; y < i; y++) { for (int x = 0; x < cellBuffer.GetLengthOfX(); x++) { var cellToWrite = cellBuffer[y, x]; if (cellToWrite.Attributes == 0) { //Don't set the foreground color for empty cells this will force the ConsoleHost //to do a very expensive calculation for each cell and slow things down. cellToWrite.Attributes = (ushort)DotConsoleNative.ToNativeConsoleColor(0, BackgroundColor); } buffer[idx].Attributes = cellToWrite.Attributes; buffer[idx].UnicodeChar = cellToWrite.Char; idx++; } } ConsoleHostNativeMethods.COORD bufferSize = new ConsoleHostNativeMethods.COORD(); bufferSize.X = (short)cellBuffer.GetLengthOfX(); bufferSize.Y = (short)partitionY; ConsoleHostNativeMethods.COORD bufferCoord = new ConsoleHostNativeMethods.COORD(); bufferCoord.X = 0; bufferCoord.Y = 0; ConsoleHostNativeMethods.SMALL_RECT writeRegion = new ConsoleHostNativeMethods.SMALL_RECT(); writeRegion.Left = (short)orgin.X; writeRegion.Top = (short)(orgin.Y + cursor); writeRegion.Right = (short)(orgin.X + bufferSize.X); writeRegion.Bottom = (short)(orgin.Y + cursor + bufferSize.Y); DotConsoleNative.WriteConsoleOutput(handle, buffer, bufferSize, bufferCoord, ref writeRegion); cursor = i; }while (i < sizeOfY); }