namespace Terminus;
///
/// Renderer class.
///
public sealed class Renderer : IDisposable
{
private nint renderer;
///
/// Callback called every time the debug menu is drawn.
///
///
///
/// Use this function to call in order to add debug information to the debug menu.
///
public static Action DebugMenuCallback = (renderer) => {};
///
/// Window title.
///
///
///
/// Whether or not to enable vertical sync.
///
///
///
/// Path to a .TTF font to use instead of the default.
///
///
///
/// The size, in points, of the font.
///
public Renderer(string title, bool vsync = true, string? font_path = null, float font_size = 20f)
{
renderer = NativeMethods.Terminus_Init(title, vsync, font_path ?? "", font_size);
NativeMethods.Terminus_SetDebugMenuCallback(renderer, Handler);
}
///
public void Dispose()
{
NativeMethods.Terminus_Destroy(renderer);
}
///
/// Present a frame to the screen.
///
///
///
/// Call this method in a loop.
///
///
///
/// Some methods call this method internally.
///
public void Present()
{
NativeMethods.Terminus_Present(renderer);
}
///
/// Toggle debug mode.
///
///
///
/// Debug mode is separate from the debug menu.
///
public void SetDebug(bool debug)
{
NativeMethods.Terminus_SetDebug(renderer, debug);
}
///
/// Draw a single cell.
///
///
///
/// The cell to draw.
///
///
///
/// The cell's position.
///
public void SetCell(Cell cell, Bound pos)
{
NativeMethods.Terminus_SetCell(renderer, cell, pos);
}
///
/// Draw a single cell.
///
///
///
/// The cell to draw.
///
///
///
/// The cell's X-position.
///
///
///
/// The cell's Y-position.
///
public void SetCell(Cell cell, int x, int y)
{
SetCell(cell, new Bound(x, y));
}
///
/// Write a single character to the cursor position. Also advance the cursor position.
///
public void Write(Cell cell)
{
NativeMethods.Terminus_WriteCell(renderer, cell);
}
///
/// Clears the screen.
///
public void Clear()
{
NativeMethods.Terminus_Clear(renderer);
}
///
/// Sets the cursor position.
///
public void SetCursor(Bound pos)
{
NativeMethods.Terminus_SetCursor(renderer, pos);
}
///
/// Get the size of the canvas in cells (rows × columns).
///
public Bound GetCanvasSize()
{
return NativeMethods.Terminus_GetCanvasSize(renderer);
}
///
/// Fill a row with a cell.
///
public void FillRow(Cell cell, int row)
{
NativeMethods.Terminus_FillRow(renderer, cell, row);
}
///
/// Fill a column with a cell.
///
public void FillCol(Cell cell, int col)
{
NativeMethods.Terminus_FillCol(renderer, cell, col);
}
///
/// Fill the screen with a cell.
///
public void FillScreen(Cell cell)
{
NativeMethods.Terminus_FillScreen(renderer, cell);
}
///
/// Read a single key from the keyboard.
///
///
///
/// Calls Present() internally.
///
public KeyPress ReadKey()
{
return NativeMethods.Terminus_ReadKey(renderer);
}
///
/// Writes a single character to the screen.
///
public void Write(char x)
{
Write(new Cell(x, Color.White, Color.Black, Style.Normal));
}
///
/// Writes a string to the screen.
///
public void Write(string text)
{
foreach (var x in text)
Write(x);
}
///
/// Moves the cursor to the next line.
///
public void WriteLine()
{
}
///
/// Writes a string to the screen and moves the cursor to the next line.
///
public void WriteLine(string text)
{
Write(text);
WriteLine();
}
///
/// Toggles the debug menu.
///
public void DrawDebugMenu(bool debug)
{
NativeMethods.Terminus_DrawDebugMenu(renderer, debug);
}
///
/// Polls the state of the mouse.
///
public MouseClick PollMouse()
{
return NativeMethods.Terminus_PollMouse(renderer);
}
///
/// Waits for a mouse click and returns the state of the mouse.
///
///
///
/// Calls Present() internally.
///
public MouseClick ReadMouse()
{
return NativeMethods.Terminus_ReadMouse(renderer);
}
private void Handler(nint renderer)
{
DebugMenuCallback(this);
}
///
/// Adds a line of text to the debug menu.
///
///
///
/// You should only call this in .
///
public void AddDebug(string text)
{
NativeMethods.Terminus_AddDebugString(renderer, text);
}
///
/// Return a string containing a dump of the contents of the screen.
///
///
///
/// Contains ANSI escape codes.
///
public string DumpScreen()
{
using var buf = new NativeString(NativeMethods.Terminus_DumpScreen(renderer));
return buf.ToString();
}
///
/// Get the name of the font family in use.
///
public string GetFontName()
{
using var buf = new NativeString(NativeMethods.Terminus_GetFontName(renderer));
return buf.ToString();
}
///
/// Get the size, in points, of the font in use.
///
///
///
/// Default font size is 20pt as of Terminus v0.2.
///
public float GetFontSize()
{
return NativeMethods.Terminus_GetFontSize(renderer);
}
}