-
Notifications
You must be signed in to change notification settings - Fork 0
/
Column.cs
175 lines (143 loc) · 4.51 KB
/
Column.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
170
171
172
173
174
175
using Android.Graphics;
using System;
namespace Escorp.Android.Views
{
internal class Column
{
private CharacterList[] characterLists;
private readonly DrawMetrics metrics;
private char currentChar = Utils.EmptyChar;
private char targetChar = Utils.EmptyChar;
private char[] currentCharacterList;
private int startIndex;
private int endIndex;
private int bottomCharIndex;
private float bottomDelta;
private float charHeight;
private float sourceWidth, currentWidth, targetWidth, minimumRequiredWidth;
private float currentBottomDelta;
private float previousBottomDelta;
private int directionAdjustment;
internal Column(CharacterList[] characterLists, DrawMetrics metrics)
{
this.characterLists = characterLists;
this.metrics = metrics;
}
internal virtual CharacterList[] CharacterLists
{
set => characterLists = value;
}
internal virtual char TargetChar
{
set
{
targetChar = value;
sourceWidth = currentWidth;
targetWidth = metrics.GetCharWidth(value);
minimumRequiredWidth = Math.Max(sourceWidth, targetWidth);
SetCharacterIndices();
bool scrollDown = endIndex >= startIndex;
directionAdjustment = scrollDown ? 1 : -1;
previousBottomDelta = currentBottomDelta;
currentBottomDelta = 0f;
}
get => targetChar;
}
internal virtual char CurrentChar => currentChar;
internal virtual float CurrentWidth
{
get
{
CheckForDrawMetricsChanges();
return currentWidth;
}
}
internal virtual float MinimumRequiredWidth
{
get
{
CheckForDrawMetricsChanges();
return minimumRequiredWidth;
}
}
private void SetCharacterIndices()
{
currentCharacterList = null;
for (int i = 0; i < characterLists.Length; i++)
{
CharacterIndices indices = characterLists[i].GetCharacterIndices(currentChar, targetChar, metrics.PreferredScrollingDirection);
if (indices != null)
{
currentCharacterList = characterLists[i].List;
startIndex = indices.startIndex;
endIndex = indices.endIndex;
}
}
if (currentCharacterList == null)
{
if (currentChar == targetChar)
{
currentCharacterList = new char[] { currentChar };
startIndex = endIndex = 0;
}
else
{
currentCharacterList = new char[] { currentChar, targetChar };
startIndex = 0;
endIndex = 1;
}
}
}
internal virtual void OnAnimationEnd()
{
CheckForDrawMetricsChanges();
minimumRequiredWidth = currentWidth;
}
private void CheckForDrawMetricsChanges()
{
float currentTargetWidth = metrics.GetCharWidth(targetChar);
if (currentWidth == targetWidth && targetWidth != currentTargetWidth) minimumRequiredWidth = currentWidth = targetWidth = currentTargetWidth;
}
internal virtual float AnimationProgress
{
set
{
if (value == 1f)
{
currentChar = targetChar;
currentBottomDelta = 0f;
previousBottomDelta = 0f;
}
float charHeight = metrics.CharHeight;
float totalHeight = charHeight * Math.Abs(endIndex - startIndex);
float currentBase = value * totalHeight;
float bottomCharPosition = currentBase / charHeight;
float bottomCharOffsetPercentage = bottomCharPosition - (int)bottomCharPosition;
float additionalDelta = previousBottomDelta * (1f - value);
bottomDelta = bottomCharOffsetPercentage * charHeight * directionAdjustment + additionalDelta;
bottomCharIndex = startIndex + ((int)bottomCharPosition * directionAdjustment);
this.charHeight = charHeight;
currentWidth = sourceWidth + (targetWidth - sourceWidth) * value;
}
}
internal virtual void Draw(Canvas canvas, Paint textPaint)
{
if (DrawText(canvas, textPaint, currentCharacterList, bottomCharIndex, bottomDelta))
{
if (bottomCharIndex >= 0) currentChar = currentCharacterList[bottomCharIndex];
currentBottomDelta = bottomDelta;
}
DrawText(canvas, textPaint, currentCharacterList, bottomCharIndex + 1, bottomDelta - charHeight);
DrawText(canvas, textPaint, currentCharacterList, bottomCharIndex - 1, bottomDelta + charHeight);
}
private bool DrawText(Canvas canvas, Paint textPaint, char[] characterList, int index, float verticalOffset)
{
if (index >= 0 && index < characterList.Length)
{
canvas.DrawText(characterList, index, 1, 0f, verticalOffset, textPaint);
return true;
}
return false;
}
}
}