-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatrixNode.cs
74 lines (68 loc) · 2.27 KB
/
MatrixNode.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
namespace MathKeyboardEngine;
public class MatrixNode : BranchingNode
{
private readonly string _matrixType;
private readonly List<List<Placeholder>> _grid = new();
private readonly int _width;
private readonly int _height;
public MatrixNode(string matrixType, int width, int height)
: base(Enumerable.Range(0, width * height).Select(x => new Placeholder()).ToList())
{
_matrixType = matrixType;
_width = width;
_height = height;
for (var i = 0; i < _height; i++)
{
var row = new List<Placeholder>();
_grid.Add(row);
for (var j = 0; j < _width; j++)
{
row.Add(Placeholders[i * _width + j]);
}
}
}
protected override string GetLatexPart(KeyboardMemory k, LatexConfiguration latexConfiguration)
{
var latex = @"\begin{" + _matrixType + "}";
latex += string.Join(@" \\ ", _grid.Select(row => string.Join(" & ", row.Select(p => p.GetLatex(k, latexConfiguration)))));
latex += @"\end{" + _matrixType + "}";
return latex;
}
public override Placeholder? GetMoveDownSuggestion(Placeholder fromPlaceholder)
{
(var rowIndex, var columnIndex) = GetPositionOf(fromPlaceholder);
var nextRowIndex = rowIndex + 1;
if (nextRowIndex < _height)
{
return Placeholders[nextRowIndex * _width + columnIndex];
}
else
{
return null;
}
}
public override Placeholder? GetMoveUpSuggestion(Placeholder fromPlaceholder)
{
(var rowIndex, var columnIndex) = GetPositionOf(fromPlaceholder);
var previousRowIndex = rowIndex - 1;
if (previousRowIndex >= 0)
{
return Placeholders[previousRowIndex * _width + columnIndex];
}
else
{
return null;
}
}
private (int, int) GetPositionOf(Placeholder placeholder)
{
var index = Placeholders.IndexOf(placeholder);
if (index == -1)
{
throw new Exception("The provided Placeholder is not part of this MatrixNode.");
}
var rowIndex = index / _width;
var columnIndex = index - rowIndex * _width;
return (rowIndex, columnIndex);
}
}