24
24
using Newtonsoft . Json ;
25
25
26
26
using ORTS . Common ;
27
+ using ORTS . TrackViewer . UserInterface ;
27
28
28
29
namespace ORTS . TrackViewer . Drawing . Labels
29
30
{
@@ -34,7 +35,7 @@ namespace ORTS.TrackViewer.Drawing.Labels
34
35
/// The list of labels can be saved or loaded to .json. This will not be done automatically.
35
36
/// The labels can be modified either w.r.t. to their location (dragging) or w.r.t. their text.
36
37
/// </summary>
37
- class DrawLabels
38
+ public class DrawLabels
38
39
{
39
40
#region private fields
40
41
/// <summary>This is the list of labels that we need to draw and possibly modify </summary>
@@ -56,6 +57,15 @@ class DrawLabels
56
57
private WorldLocation draggingStartLocation ;
57
58
/// <summary>Are we currently dragging?</summary>
58
59
private bool dragging = false ; // draggingLabelToReplace is not nullable, so we cannot use that
60
+
61
+ private TrackViewer TrackViewer ;
62
+
63
+ private ContextMenu ContextMenu ;
64
+
65
+ private MenuItem EditLabelMenuItem ;
66
+
67
+ public MenuItem SetLocationMenuItem ;
68
+
59
69
#endregion
60
70
61
71
#region Constructor
@@ -64,9 +74,12 @@ class DrawLabels
64
74
/// Constructor
65
75
/// </summary>
66
76
/// <param name="fontHeight">The height of the font so we can correct during drawing</param>
67
- public DrawLabels ( int fontHeight )
77
+ public DrawLabels ( TrackViewer trackViewer , int fontHeight )
68
78
{
69
79
this . fontHeight = fontHeight ;
80
+ TrackViewer = trackViewer ;
81
+
82
+ CreateContextMenu ( ) ;
70
83
}
71
84
#endregion
72
85
@@ -88,7 +101,7 @@ public void Draw(DrawArea drawArea)
88
101
DrawLabel ( label ) ;
89
102
}
90
103
float distanceSquared = WorldLocation . GetDistanceSquared2D ( label . WorldLocation , drawArea . MouseLocation ) ;
91
- if ( distanceSquared < closestDistanceSquared )
104
+ if ( distanceSquared < closestDistanceSquared )
92
105
{
93
106
closestDistanceSquared = distanceSquared ;
94
107
closestToMouseLabel = label ;
@@ -107,7 +120,7 @@ public void Draw(DrawArea drawArea)
107
120
/// <param name="label">The lable to draw</param>
108
121
private void DrawLabel ( StorableLabel label )
109
122
{
110
- drawArea . DrawExpandingString ( label . WorldLocation , label . LabelText , 0 , - fontHeight / 2 ) ;
123
+ drawArea . DrawExpandingString ( label . WorldLocation , label . LabelText , 0 , - fontHeight / 2 ) ;
111
124
}
112
125
113
126
#endregion
@@ -120,36 +133,56 @@ private void DrawLabel(StorableLabel label)
120
133
/// <param name="mouseY">Current Y-location of the mouse to determine popu location</param>
121
134
internal void AddLabel ( int mouseX , int mouseY )
122
135
{
123
- var labelInputPopup = new EditLabel ( "<label>" , mouseX , mouseY ,
136
+ var labelInputPopup = new EditLabel ( "<label>" , mouseX , mouseY ,
124
137
( newLabelText ) => labels . Add ( drawArea . MouseLocation , newLabelText ) ,
125
138
allowDelete : false ) ;
126
139
TrackViewer . Localize ( labelInputPopup ) ;
127
140
labelInputPopup . ShowDialog ( ) ;
128
141
}
129
142
143
+ private void CreateContextMenu ( )
144
+ {
145
+ ContextMenu = new ContextMenu ( ) ;
146
+ EditLabelMenuItem = new MenuItem
147
+ {
148
+ Header = "Edit label" ,
149
+ IsCheckable = false
150
+ } ;
151
+ EditLabelMenuItem . Click += new RoutedEventHandler ( ( sender , e ) => ModifyLabel ( closestToMouseLabel ,
152
+ TrackViewer . Window . ClientBounds . Left + TVUserInput . MouseLocationX ,
153
+ TrackViewer . Window . ClientBounds . Left + TVUserInput . MouseLocationY ) ) ;
154
+
155
+ ContextMenu . Items . Add ( EditLabelMenuItem ) ;
156
+
157
+ SetLocationMenuItem = new MenuItem ( ) { Header = "View scene here" } ;
158
+ SetLocationMenuItem . Click += new RoutedEventHandler ( ( sender , e ) => TrackViewer . menuControl . MenuSceneWindow_Click ( sender , e ) ) ;
159
+ SetLocationMenuItem . Click += new RoutedEventHandler ( async ( sender , e ) => await TrackViewer . SceneViewer ? . SetCameraLocation ( ) ) ;
160
+ ContextMenu . Items . Add ( SetLocationMenuItem ) ;
161
+ }
162
+
130
163
/// <summary>
131
164
/// Popup a context menu that allows you to edit the text of a label
132
165
/// </summary>
133
166
/// <param name="mouseX">Current X-location of the mouse to determine popup location</param>
134
- /// <param name="mouseY">Current Y-location of the mouse to determine popu location</param>
135
- internal void PopupContextMenu ( int mouseX , int mouseY )
167
+ /// <param name="mouseY">Current Y-location of the mouse to determine popup location</param>
168
+ internal void PopupContextMenu ( int mouseX , int mouseY , WorldLocation mouseLocation )
136
169
{
137
- if ( ! Properties . Settings . Default . showLabels ) return ;
138
- if ( labels . Labels . Count ( ) == 0 ) return ;
139
- var editingLabel = closestToMouseLabel ;
170
+ if ( ! Properties . Settings . Default . showLabels || labels . Labels . Count ( ) == 0 )
171
+ EditLabelMenuItem . Visibility = Visibility . Collapsed ;
172
+ else
173
+ EditLabelMenuItem . Visibility = Visibility . Visible ;
140
174
141
- var contextMenu = new ContextMenu ( ) ;
175
+ SetLocationMenuItem . CommandParameter = mouseLocation ;
142
176
143
- var editLabelMenuItem = new MenuItem
144
- {
145
- Header = "Edit label" ,
146
- IsCheckable = false
147
- } ;
148
- editLabelMenuItem . Click += new RoutedEventHandler ( ( sender , e ) => ModifyLabel ( closestToMouseLabel , mouseX , mouseY ) ) ;
177
+ var visible = false ;
178
+ foreach ( MenuItem item in ContextMenu . Items )
179
+ visible |= item . Visibility == Visibility . Visible ;
149
180
150
- contextMenu . Items . Add ( editLabelMenuItem ) ;
151
- contextMenu . PlacementRectangle = new Rect ( ( double ) mouseX , ( double ) mouseY , 20 , 20 ) ;
152
- contextMenu . IsOpen = true ;
181
+ if ( visible )
182
+ {
183
+ ContextMenu . PlacementRectangle = new Rect ( ( double ) mouseX , ( double ) mouseY , 20 , 20 ) ;
184
+ ContextMenu . IsOpen = true ;
185
+ }
153
186
}
154
187
155
188
/// <summary>
@@ -266,7 +299,7 @@ private void LoadJson(string fileName)
266
299
MessageBox . Show ( TrackViewer . catalog . GetString ( "The .json file could not be read properly." ) ) ;
267
300
return ;
268
301
}
269
-
302
+
270
303
bool itemsWereRemoved = labelsNew . Sanitize ( ) ;
271
304
int itemsLeft = labelsNew . Labels . Count ( ) ;
272
305
string message = string . Empty ;
@@ -320,8 +353,8 @@ internal void OnLeftMouseMoved()
320
353
//The new location is then 'original' + 'current' - 'start'.
321
354
draggingStartLocation . NormalizeTo ( drawArea . MouseLocation . TileX , drawArea . MouseLocation . TileZ ) ;
322
355
WorldLocation shiftedLocation = new WorldLocation (
323
- draggingLabelToReplace . WorldLocation . TileX + drawArea . MouseLocation . TileX - draggingStartLocation . TileX ,
324
- draggingLabelToReplace . WorldLocation . TileZ + drawArea . MouseLocation . TileZ - draggingStartLocation . TileZ ,
356
+ draggingLabelToReplace . WorldLocation . TileX + drawArea . MouseLocation . TileX - draggingStartLocation . TileX ,
357
+ draggingLabelToReplace . WorldLocation . TileZ + drawArea . MouseLocation . TileZ - draggingStartLocation . TileZ ,
325
358
draggingLabelToReplace . WorldLocation . Location . X + drawArea . MouseLocation . Location . X - draggingStartLocation . Location . X ,
326
359
0 ,
327
360
draggingLabelToReplace . WorldLocation . Location . Z + drawArea . MouseLocation . Location . Z - draggingStartLocation . Location . Z
0 commit comments