/
StackPanel.cs
301 lines (257 loc) · 10.5 KB
/
StackPanel.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// The MIT License(MIT)
//
// Copyright(c) 2021 Alberto Rodriguez Orozco & LiveCharts Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System.Collections.Generic;
using LiveChartsCore.Drawing;
using LiveChartsCore.Kernel;
using LiveChartsCore.Measure;
namespace LiveChartsCore.VisualElements;
/// <summary>
/// Defines the stack panel class.
/// </summary>
/// <typeparam name="TBackgroundGeometry">The type of the background geometry.</typeparam>
/// <typeparam name="TDrawingContext">The type of the drawing context.</typeparam>
public class StackPanel<TBackgroundGeometry, TDrawingContext> : VisualElement<TDrawingContext>
where TDrawingContext : DrawingContext
where TBackgroundGeometry : ISizedGeometry<TDrawingContext>, new()
{
private IPaint<TDrawingContext>? _backgroundPaint;
private Align _verticalAlignment = Align.Middle;
private Align _horizontalAlignment = Align.Middle;
private Padding _padding = new();
private double _maxWidth = double.MaxValue;
private double _maxHeight = double.MaxValue;
private ContainerOrientation _orientation;
/// <summary>
/// Initializes a new instance of the <see cref="StackPanel{TBackgroundGeometry, TDrawingContext}"/> class.
/// </summary>
public StackPanel()
{
ClippingMode = ClipMode.None;
}
/// <summary>
/// Gets the children collection.
/// </summary>
public List<VisualElement<TDrawingContext>> Children { get; } = new();
/// <summary>
/// Gets or sets the panel orientation.
/// </summary>
public ContainerOrientation Orientation { get => _orientation; set => SetProperty(ref _orientation, value); }
/// <summary>
/// Gets or sets the vertical alignment.
/// </summary>
public Align VerticalAlignment { get => _verticalAlignment; set => SetProperty(ref _verticalAlignment, value); }
/// <summary>
/// Gets or sets the horizontal alignment.
/// </summary>
public Align HorizontalAlignment { get => _horizontalAlignment; set => SetProperty(ref _horizontalAlignment, value); }
/// <summary>
/// Gets or sets the padding.
/// </summary>
public Padding Padding { get => _padding; set => SetProperty(ref _padding, value); }
/// <summary>
/// Gets or sets the background paint.
/// </summary>
public IPaint<TDrawingContext>? BackgroundPaint
{
get => _backgroundPaint;
set => SetPaintProperty(ref _backgroundPaint, value);
}
/// <summary>
/// Gets the background geometry.
/// </summary>
public TBackgroundGeometry BackgroundGeometry { get; } = new();
/// <summary>
/// Gets or sets the maximum width. When the maximum with is reached, a new row is created.
/// </summary>
public double MaxWidth { get => _maxWidth; set => SetProperty(ref _maxWidth, value); }
/// <summary>
/// Gets or sets the maximum height. When the maximum height is reached, a new column is created.
/// </summary>
public double MaxHeight { get => _maxHeight; set => SetProperty(ref _maxHeight, value); }
internal override IPaint<TDrawingContext>?[] GetPaintTasks()
{
return new[] { _backgroundPaint };
}
internal override IAnimatable?[] GetDrawnGeometries()
{
return new IAnimatable?[] { BackgroundGeometry };
}
internal override IEnumerable<VisualElement<TDrawingContext>> IsHitBy(Chart<TDrawingContext> chart, LvcPoint point)
{
var location = GetActualCoordinate();
var size = Measure(chart);
// it returns an enumerable because there are more complex types where a visual can contain more than one element
if (point.X >= location.X && point.X <= location.X + size.Width &&
point.Y >= location.Y && point.Y <= location.Y + size.Height)
{
yield return this;
}
var relativePoint = new LvcPoint(point.X - location.X, point.Y - location.Y);
foreach (var child in Children)
{
foreach (var element in child.IsHitBy(chart, relativePoint))
{
yield return element;
}
}
}
/// <inheritdoc cref="VisualElement{TDrawingContext}.OnInvalidated(Chart{TDrawingContext})"/>
protected internal override void OnInvalidated(Chart<TDrawingContext> chart)
{
var controlSize = Measure(chart);
// NOTE #20231605
// force the background to have at least an invisible geometry
// we use this geometry in the motion canvas to track the position
// of the stack panel as the time and animations elapse.
BackgroundPaint ??= LiveCharts.DefaultSettings
.GetProvider<TDrawingContext>()
.GetSolidColorPaint(new LvcColor(0, 0, 0, 0));
var clipping = Clipping.GetClipRectangle(ClippingMode, chart);
chart.Canvas.AddDrawableTask(BackgroundPaint);
BackgroundGeometry.X = (float)X;
BackgroundGeometry.Y = (float)Y;
BackgroundGeometry.Width = controlSize.Width;
BackgroundGeometry.Height = controlSize.Height;
BackgroundGeometry.RotateTransform = (float)Rotation;
BackgroundGeometry.TranslateTransform = Translate;
BackgroundPaint.AddGeometryToPaintTask(chart.Canvas, BackgroundGeometry);
BackgroundPaint.SetClipRectangle(chart.Canvas, clipping);
}
/// <inheritdoc cref="VisualElement{TDrawingContext}.SetParent(IGeometry{TDrawingContext})"/>
protected internal override void SetParent(IGeometry<TDrawingContext> parent)
{
if (BackgroundGeometry is null) return;
BackgroundGeometry.Parent = parent;
}
/// <inheritdoc cref="VisualElement{TDrawingContext}.Measure(Chart{TDrawingContext})"/>
public override LvcSize Measure(Chart<TDrawingContext> chart)
{
var xl = Padding.Left;
var yl = Padding.Top;
var rowHeight = -1f;
var columnWidth = -1f;
var mx = 0f;
var my = 0f;
List<MeasureResult> line = new();
LvcSize alignCurrentLine()
{
var mx = -1f;
var my = -1f;
foreach (var child in line)
{
if (Orientation == ContainerOrientation.Horizontal)
{
child.Visual._y = VerticalAlignment switch
{
Align.Start => yl,
Align.Middle => yl + (rowHeight - child.Size.Height) / 2f,
Align.End => yl + rowHeight - child.Size.Height,
_ => throw new System.NotImplementedException()
};
}
else
{
child.Visual._x = HorizontalAlignment switch
{
Align.Start => xl,
Align.Middle => xl + (columnWidth - child.Size.Width) / 2f,
Align.End => xl + columnWidth - child.Size.Width,
_ => throw new System.NotImplementedException()
};
}
child.Visual.OnInvalidated(chart);
child.Visual.SetParent(BackgroundGeometry);
if (child.Size.Width > mx) mx = child.Size.Width;
if (child.Size.Height > my) my = child.Size.Height;
}
line = new();
return new LvcSize(mx, my);
}
foreach (var child in Children)
{
var childSize = child.Measure(chart);
if (Orientation == ContainerOrientation.Horizontal)
{
if (xl + childSize.Width > MaxWidth)
{
var lineSize = alignCurrentLine();
xl = Padding.Left;
yl += lineSize.Height;
rowHeight = -1f;
}
if (rowHeight < childSize.Height) rowHeight = childSize.Height;
child._x = xl;
xl += childSize.Width;
}
else
{
if (yl + childSize.Height > MaxHeight)
{
var lineSize = alignCurrentLine();
yl = Padding.Top;
xl += lineSize.Width;
columnWidth = -1f;
}
if (columnWidth < childSize.Width) columnWidth = childSize.Width;
child._y = yl;
yl += childSize.Height;
}
if (xl > mx) mx = xl;
if (yl > my) my = yl;
line.Add(new MeasureResult(child, childSize));
}
if (line.Count > 0)
{
var lineSize = alignCurrentLine();
if (Orientation == ContainerOrientation.Horizontal)
{
yl += lineSize.Height;
}
else
{
xl += lineSize.Width;
}
if (xl > mx) mx = xl;
if (yl > my) my = yl;
}
return new LvcSize(mx + Padding.Right, my + Padding.Bottom);
}
/// <inheritdoc cref="ChartElement{TDrawingContext}.RemoveFromUI(Chart{TDrawingContext})"/>
public override void RemoveFromUI(Chart<TDrawingContext> chart)
{
foreach (var child in Children)
{
child.RemoveFromUI(chart);
}
base.RemoveFromUI(chart);
}
private class MeasureResult
{
public MeasureResult(VisualElement<TDrawingContext> visual, LvcSize size)
{
Visual = visual;
Size = size;
}
public VisualElement<TDrawingContext> Visual { get; set; }
public LvcSize Size { get; set; }
}
}