-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentCursor.cs
More file actions
190 lines (173 loc) · 6.23 KB
/
DocumentCursor.cs
File metadata and controls
190 lines (173 loc) · 6.23 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.IO;
namespace maple
{
class DocumentCursor : Cursor
{
public Document Doc { get; private set; }
public DocumentCursor(Document doc, int dX, int dY) : base(dX, dY)
{
Doc = doc;
}
/// <summary>
/// Safely move the Cursor left by 1 character (Document coordinates).
/// <param name="applyPosition">Determines if the Console's cursor should be moved to the given position.</param>
/// </summary>
public void MoveLeft(bool applyPosition = true)
{
if(SX == 0 && Doc.ScrollX > 0)
{
Doc.ScrollLeft();
Editor.RefreshAllLines();
}
else
{
if(DX > 0) //can move back
Move(DX - 1, DY, applyPosition: applyPosition);
else
{
if(DY > 0)
{
MoveUp();
Move(Doc.GetLine(DY).Length, DY, applyPosition: applyPosition);
}
}
}
}
/// <summary>
/// Safely move the Cursor right by 1 character (Document coordinates).
/// <param name="applyPosition">Determines if the Console's cursor should be moved to the given position.</param>
/// </summary>
public void MoveRight(bool applyPosition = true)
{
if(SX == Printer.MaxScreenX - 1)
{
Log.WriteDebug("scrolling right", "documentcursor");
Doc.ScrollRight();
Editor.RefreshAllLines();
}
if(DX < Doc.GetLine(DY).Length) //can move forward
Move(DX + 1, DY);
else
{
if(DY < Doc.GetMaxLine())
{
MoveDown(applyPosition);
Move(0, DY, applyPosition: applyPosition);
}
}
}
/// <summary>
/// Safely move the Cursor up 1 line (Document coordinates).
/// <param name="applyPosition">Determines if the Console's cursor should be moved to the given position.</param>
/// </summary>
public void MoveUp(bool applyPosition = true)
{
if(SY == 0 && Doc.ScrollY > 0)
{
Doc.ScrollUp();
Editor.RefreshAllLines();
}
else
Move(DX, DY - 1, applyPosition: applyPosition);
}
/// <summary>
/// Safely move the Cursor down 1 line (Document coordinates).
/// <param name="applyPosition">Determines if the Console's cursor should be moved to the given position.</param>
/// </summary>
public void MoveDown(bool applyPosition = true)
{
if(SY == Printer.MaxScreenY - Footer.FooterHeight)
{
Doc.ScrollDown();
Editor.RefreshAllLines();
}
else
Move(DX, DY + 1, applyPosition: applyPosition);
}
/// <summary>
/// Force the document X and Y coordinates to fall within the Document's constraints.
/// </summary>
public void LockToDocConstraints()
{
if(DY < 0)
DY = 0;
if(DY > Doc.GetMaxLine())
DY = Doc.GetMaxLine();
if(DX < 0)
DX = 0;
if(DX > Doc.GetLine(DY).Length)
DX = Doc.GetLine(DY).Length;
}
/// <summary>
/// Safely move the Cursor to the given coordinates.
/// </summary>
/// <param name="tX">The new X coordinate (Document).</param>
/// <param name="tY">The new Y coordinate (Document).</param>
/// <param name="applyPosition">Determines if the Console's cursor should be moved to the given position.</param>
/// <param name="constrainToDoc">Lock the Cursor to the Document constraints after moving.</param>
/// <param name="constrainToScreen">Lock the Cursor to the screen constraints after moving.</param>
public void Move(int tX, int tY, bool constrainToDoc = true, bool constrainToScreen = true, bool applyPosition = true)
{
DX = tX;
DY = tY;
if(constrainToDoc)
{
LockToDocConstraints();
}
//scroll if set position is outside current viewport
bool hasScrolled = false;
while (DY - Doc.ScrollY > Printer.MaxScreenY - Footer.FooterHeight)
{
Doc.ScrollDown();
hasScrolled = true;
}
while (DY - Doc.ScrollY < Printer.MinScreenY)
{
Doc.ScrollUp();
hasScrolled = true;
}
while (DX - Doc.ScrollX + Doc.GutterWidth > Printer.MaxScreenX)
{
Doc.ScrollRight();
hasScrolled = true;
}
while (DX - Doc.ScrollX < Printer.MinScreenX)
{
Doc.ScrollLeft();
hasScrolled = true;
}
//refresh all lines if scroll was performed
if (hasScrolled)
{
Log.WriteDebug("has scrolled", "documentcursor");
Editor.RefreshAllLines();
}
SX = DX + ContentOffsetX - Doc.ScrollX;
SY = DY + ContentOffsetY - Doc.ScrollY;
if (constrainToScreen)
{
LockToScreenConstraints();
}
if (applyPosition)
{
ApplyPosition();
}
}
public void Move(Point target, bool constrainToDoc = true, bool constrainToScreen = true, bool applyPosition = true)
{
Move(target.X, target.Y, constrainToDoc, constrainToScreen, applyPosition);
}
/// <summary>
/// Update the Cursor's X offset according to the Document gutter.
/// </summary>
public void UpdateGutterOffset()
{
//get gutter width from doc
int gutterWidth = Doc.CalculateGutterWidth();
ContentOffsetX = gutterWidth;
//update position of cursor
ApplyPosition();
}
}
}