-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommentBlockViewModel.cs
More file actions
196 lines (168 loc) · 6.97 KB
/
CommentBlockViewModel.cs
File metadata and controls
196 lines (168 loc) · 6.97 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
191
192
193
194
195
196
/*
Copyright 2026 University of Toronto
This file is part of XTMF2.
XTMF2 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
XTMF2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with XTMF2. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.ComponentModel;
using CommunityToolkit.Mvvm.ComponentModel;
using XTMF2.Editing;
using XTMF2.ModelSystemConstruct;
namespace XTMF2.GUI.ViewModels;
/// <summary>
/// Wraps a <see cref="CommentBlock"/> for display on the model system canvas.
/// Comment blocks are rendered as sticky-note style rectangles with wrapped text.
/// </summary>
public sealed partial class CommentBlockViewModel : ObservableObject, ICanvasElement
{
/// <summary>Default width when the model stores 0.</summary>
public const double DefaultWidth = 200.0;
/// <summary>Default height when the model stores 0.</summary>
public const double DefaultHeight = 80.0;
/// <summary>The underlying model object.</summary>
public CommentBlock UnderlyingBlock { get; }
private readonly ModelSystemSession _session;
private readonly User _user;
// ── Coordinates read directly from the underlying model (or preview during drag) ─
private double? _previewX;
private double? _previewY;
private double? _previewW;
private double? _previewH;
/// <inheritdoc/>
public double X => _previewX ?? (double)UnderlyingBlock.Location.X;
/// <inheritdoc/>
public double Y => _previewY ?? (double)UnderlyingBlock.Location.Y;
/// <summary>Rendered width; falls back to <see cref="DefaultWidth"/> when the model value is 0.</summary>
public double Width => _previewW ?? (UnderlyingBlock.Location.Width is 0 ? DefaultWidth : (double)UnderlyingBlock.Location.Width);
/// <summary>Rendered height; falls back to <see cref="DefaultHeight"/> when the model value is 0.</summary>
public double Height => _previewH ?? (UnderlyingBlock.Location.Height is 0 ? DefaultHeight : (double)UnderlyingBlock.Location.Height);
// ICanvasElement: Name maps to Comment so the property panel can reuse SelectedElementEditName.
/// <inheritdoc/>
public string Name => UnderlyingBlock.Comment;
/// <inheritdoc/>
public double CenterX => X + Width / 2.0;
/// <inheritdoc/>
public double CenterY => Y + Height / 2.0;
[ObservableProperty] private bool _isSelected;
public CommentBlockViewModel(CommentBlock block, ModelSystemSession session, User user)
{
UnderlyingBlock = block;
_session = session;
_user = user;
((INotifyPropertyChanged)block).PropertyChanged += OnModelPropertyChanged;
}
private void OnModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(CommentBlock.Comment):
OnPropertyChanged(nameof(Name));
break;
case nameof(CommentBlock.Location):
OnPropertyChanged(nameof(X));
OnPropertyChanged(nameof(Y));
OnPropertyChanged(nameof(Width));
OnPropertyChanged(nameof(Height));
OnPropertyChanged(nameof(CenterX));
OnPropertyChanged(nameof(CenterY));
break;
}
}
/// <summary>
/// Updates the visual position without touching the session (for drag preview).
/// Call <see cref="CommitMove"/> on mouse-up to persist the change.
/// </summary>
public void MoveToPreview(double x, double y)
{
_previewX = x;
_previewY = y;
OnPropertyChanged(nameof(X));
OnPropertyChanged(nameof(Y));
OnPropertyChanged(nameof(CenterX));
OnPropertyChanged(nameof(CenterY));
}
/// <summary>
/// Commits the current preview position to the session (call once on mouse-up).
/// Does nothing if no preview is active.
/// </summary>
public void CommitMove()
{
if (_previewX is null) return;
var x = _previewX.Value;
var y = _previewY!.Value;
_previewX = null;
_previewY = null;
MoveTo(x, y);
}
/// <summary>
/// Move the comment block to a new canvas position, persisting the change via the session
/// (supports undo/redo).
/// </summary>
public void MoveTo(double x, double y)
{
var loc = UnderlyingBlock.Location;
var w = loc.Width is 0 ? (float)DefaultWidth : loc.Width;
var h = loc.Height is 0 ? (float)DefaultHeight : loc.Height;
_session.SetCommentBlockLocation(_user, UnderlyingBlock,
new Rectangle((float)x, (float)y, w, h), out _);
// OnModelPropertyChanged("Location") fires automatically and propagates X/Y changes.
}
/// <summary>
/// Update the comment text, persisting the change via the session (supports undo/redo).
/// Whitespace-only text is ignored.
/// </summary>
public void SetText(string text)
{
if (string.IsNullOrWhiteSpace(text)) return;
_session.SetCommentBlockText(_user, UnderlyingBlock, text, out _);
}
/// <summary>
/// Updates the visual size without touching the session (for resize-drag preview).
/// Call <see cref="CommitResize"/> on mouse-up to persist the change.
/// Width is clamped to a minimum of 60; height to a minimum of 30.
/// </summary>
public void ResizeToPreview(double w, double h)
{
_previewW = Math.Max(60.0, w);
_previewH = Math.Max(30.0, h);
OnPropertyChanged(nameof(Width));
OnPropertyChanged(nameof(Height));
OnPropertyChanged(nameof(CenterX));
OnPropertyChanged(nameof(CenterY));
}
/// <summary>
/// Commits the current preview size to the session (call once on mouse-up).
/// Does nothing if no resize preview is active.
/// </summary>
public void CommitResize()
{
if (_previewW is null) return;
var w = _previewW.Value;
var h = _previewH!.Value;
_previewW = null;
_previewH = null;
ResizeTo(w, h);
}
/// <summary>
/// Resize the comment block, persisting the change via the session (supports undo/redo).
/// Width is clamped to a minimum of 60; height to a minimum of 30.
/// </summary>
public void ResizeTo(double w, double h)
{
const float minW = 60f;
const float minH = 30f;
var loc = UnderlyingBlock.Location;
_session.SetCommentBlockLocation(_user, UnderlyingBlock,
new Rectangle(loc.X, loc.Y, Math.Max(minW, (float)w), Math.Max(minH, (float)h)),
out _);
}
}