-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowWriter.cs
169 lines (167 loc) · 5.23 KB
/
WindowWriter.cs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Threading;
using System.Collections.Generic;
namespace BasicGUI
{
public class WindowWriter
{
private (int left, int top) _position;
public IWindow Window { get; set; }
public (int left, int top) CurrentPosition
{
get
{
return this._position;
}
set
{
this._position = value;
Console.SetCursorPosition(this._position.left, this._position.top);
}
}
public Typewriter Typewriter { get; set; }
public bool CursorReset => this.CurrentPosition.top >= this.Window.LastLine;
public WindowWriter(IWindow window)
{
this.Window = window;
this.Typewriter = new Typewriter(this);
this.ResetPosition();
}
public void Activate()
{
Console.SetCursorPosition(this.CurrentPosition.left, this.CurrentPosition.top);
}
public void Write(string text)
{
this.Activate();
if ( text.Length <= this.Window.BufferWidth )
{
Thread.Sleep(GUI.DebugSleepTime);
Console.WriteLine(text);
this.AdvanceLine();
}
else
{
IEnumerable<string> messageLines = this.WordSplit(text, this.Window.BufferWidth);
foreach ( string messageLine in messageLines )
{
Thread.Sleep(GUI.DebugSleepTime);
Console.WriteLine(messageLine);
this.AdvanceLine();
}
}
}
public void Write(IEnumerable<string> text)
{
foreach (string line in text)
{
this.Write(line);
}
}
public void PrintDialogue(List<string> dx)
{
this.Typewriter.PrintSubtle("Press 'Enter' to continue...");
foreach ( string line in dx )
{
this.Typewriter.PrintChars(line);
this.Typewriter.Pause();
}
}
public void PrintToConsole(List<string> text)
{
foreach ( string line in text )
{
this.Typewriter.PrintChars(line);
}
}
public void PrintPromptToConsole(string text)
{
this.Typewriter.PromptChars(text);
}
public void PrintToConsole(string text)
{
this.Typewriter.PrintChars(text);
}
public void PrintPause(int num)
{
this.Activate();
for ( int i = 0; i < num; i++ )
{
Console.Write(".");
Thread.Sleep(300);
}
this.AdvanceLine();
}
public IEnumerable<string> Split(string str, int maxLength)
{
if (String.IsNullOrEmpty(str) || maxLength < 1)
{
throw new ArgumentException();
}
int index = 0;
while ( index + maxLength < str.Length )
{
yield return str.Substring(index, maxLength);
index += maxLength;
}
yield return str.Substring(index);
}
public IEnumerable<string> WordSplit(string str, int maxLength)
{
if (String.IsNullOrEmpty(str))
{
throw new ArgumentException();
}
string[] words = str.Split(' ');
string result = "";
int index = 0;
while (index < words.Length)
{
if ( (result + words[index]).Length > maxLength )
{
index++;
IEnumerable<string> singleWordLines = this.Split(words[index], maxLength);
foreach (string line in singleWordLines)
{
yield return line;
continue;
}
}
while ( index < words.Length && !( (result + words[index] + " ").Length > maxLength ) )
{
result += words[index] + " ";
index++;
}
yield return result;
result = "";
}
}
public void AdvanceLine()
{
if (this.CursorReset == true)
{
this.Clear();
}
else
{
this.CurrentPosition = (this.CurrentPosition.left, this.CurrentPosition.top + 1);
}
}
public void ResetPosition()
{
this.CurrentPosition = (this.Window.BufferLeft, this.Window.BufferTop);
}
public void Clear()
{
this.ResetPosition();
string blankLine = new string(' ', this.Window.BufferWidth);
for ( int i = 0; i < this.Window.BufferHeight; i++ )
{
Console.Write(blankLine);
Thread.Sleep(GUI.DebugSleepTime);
this.CurrentPosition = (this.CurrentPosition.left, ( this.CurrentPosition.top + 1 ));
}
this.ResetPosition();
}
}
}