-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (99 loc) · 4.7 KB
/
Program.cs
File metadata and controls
116 lines (99 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Created by Ron 'Maxwolf' McDowell ([email protected])
// Timestamp 01/07/2016@7:10 PM
using System;
using System.Threading;
namespace WolfCurses.Example
{
/// <summary>
/// Example console application using wolf curses library to power interaction.
/// </summary>
internal static class Program
{
/// <summary>
/// Main entry point for the application being startup.
/// </summary>
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();
}
/// <summary>Write all text from objects to screen.</summary>
/// <param name="tuiContent">The text user interface content.</param>
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);
}
}
/// <summary>
/// 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.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
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;
}
/// <summary>
/// Forces the current simulation app to close and return control to underlying operating system.
/// </summary>
public static void Destroy()
{
ConsoleSimulationApp.Instance.Destroy();
}
}
}