// Created by Ron 'Maxwolf' McDowell (ron.mcdowell@gmail.com)
// Timestamp 01/07/2016@7:10 PM
using System;
using System.Threading;
namespace WolfCurses.Example
{
///
/// Example console application using wolf curses library to power interaction.
///
internal static class Program
{
///
/// Main entry point for the application being startup.
///
private static void Main()
{
// Create console with title, no cursor, make CTRL-C act as input.
Console.Title = "WolfCurses Console Application";
Console.WriteLine("Starting...");
Console.CursorVisible = false;
Console.CancelKeyPress += Console_CancelKeyPress;
// Entry point for the entire simulation.
ConsoleSimulationApp.Create();
// Hook event to know when screen buffer wants to redraw the entire console screen.
ConsoleSimulationApp.Instance.SceneGraph.ScreenBufferDirtyEvent += Simulation_ScreenBufferDirtyEvent;
// Prevent console session from closing.
while (ConsoleSimulationApp.Instance != null)
{
// Simulation takes any numbers of pulses to determine seconds elapsed.
ConsoleSimulationApp.Instance.OnTick(true);
// Check if a key is being pressed, without blocking thread.
if (Console.KeyAvailable)
{
// GetModule the key that was pressed, without printing it to console.
var key = Console.ReadKey(true);
// If enter is pressed, pass whatever we have to simulation.
// ReSharper disable once SwitchStatementMissingSomeCases
switch (key.Key)
{
case ConsoleKey.Enter:
ConsoleSimulationApp.Instance.InputManager.SendInputBufferAsCommand();
break;
case ConsoleKey.Backspace:
ConsoleSimulationApp.Instance.InputManager.RemoveLastCharOfInputBuffer();
break;
default:
ConsoleSimulationApp.Instance.InputManager.AddCharToInputBuffer(key.KeyChar);
break;
}
}
// Do not consume all of the CPU, allow other messages to occur.
Thread.Sleep(1);
}
// Make user press any key to close out the simulation completely, this way they know it closed without error.
Console.Clear();
Console.WriteLine("Goodbye!");
Console.WriteLine("Press ANY KEY to close this window...");
Console.ReadKey();
}
/// Write all text from objects to screen.
/// The text user interface content.
private static void Simulation_ScreenBufferDirtyEvent(string tuiContent)
{
var tuiContentSplit = tuiContent.Split(new[] {Environment.NewLine}, StringSplitOptions.None);
for (var index = 0; index < Console.WindowHeight - 1; index++)
{
Console.CursorLeft = 0;
Console.SetCursorPosition(0, index);
var emptyStringData = new string(' ', Console.WindowWidth);
if (tuiContentSplit.Length > index)
{
emptyStringData = tuiContentSplit[index].PadRight(Console.WindowWidth);
}
Console.Write(emptyStringData);
}
}
///
/// Fired when the user presses CTRL-C on their keyboard, this is only relevant to operating system tick and this view
/// of simulation. If moved into another framework like game engine this statement would be removed and just destroy
/// the simulation when the engine is destroyed using its overrides.
///
/// The sender.
/// The e.
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
// Destroy the simulation.
ConsoleSimulationApp.Instance.Destroy();
// Stop the operating system from killing the entire process.
e.Cancel = true;
}
///
/// Forces the current simulation app to close and return control to underlying operating system.
///
public static void Destroy()
{
ConsoleSimulationApp.Instance.Destroy();
}
}
}