2424using Newtonsoft . Json ;
2525
2626using ORTS . Common ;
27+ using ORTS . TrackViewer . UserInterface ;
2728
2829namespace ORTS . TrackViewer . Drawing . Labels
2930{
@@ -34,7 +35,7 @@ namespace ORTS.TrackViewer.Drawing.Labels
3435 /// The list of labels can be saved or loaded to .json. This will not be done automatically.
3536 /// The labels can be modified either w.r.t. to their location (dragging) or w.r.t. their text.
3637 /// </summary>
37- class DrawLabels
38+ public class DrawLabels
3839 {
3940 #region private fields
4041 /// <summary>This is the list of labels that we need to draw and possibly modify </summary>
@@ -56,6 +57,15 @@ class DrawLabels
5657 private WorldLocation draggingStartLocation ;
5758 /// <summary>Are we currently dragging?</summary>
5859 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+
5969 #endregion
6070
6171 #region Constructor
@@ -64,9 +74,12 @@ class DrawLabels
6474 /// Constructor
6575 /// </summary>
6676 /// <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 )
6878 {
6979 this . fontHeight = fontHeight ;
80+ TrackViewer = trackViewer ;
81+
82+ CreateContextMenu ( ) ;
7083 }
7184 #endregion
7285
@@ -88,7 +101,7 @@ public void Draw(DrawArea drawArea)
88101 DrawLabel ( label ) ;
89102 }
90103 float distanceSquared = WorldLocation . GetDistanceSquared2D ( label . WorldLocation , drawArea . MouseLocation ) ;
91- if ( distanceSquared < closestDistanceSquared )
104+ if ( distanceSquared < closestDistanceSquared )
92105 {
93106 closestDistanceSquared = distanceSquared ;
94107 closestToMouseLabel = label ;
@@ -107,7 +120,7 @@ public void Draw(DrawArea drawArea)
107120 /// <param name="label">The lable to draw</param>
108121 private void DrawLabel ( StorableLabel label )
109122 {
110- drawArea . DrawExpandingString ( label . WorldLocation , label . LabelText , 0 , - fontHeight / 2 ) ;
123+ drawArea . DrawExpandingString ( label . WorldLocation , label . LabelText , 0 , - fontHeight / 2 ) ;
111124 }
112125
113126 #endregion
@@ -120,36 +133,56 @@ private void DrawLabel(StorableLabel label)
120133 /// <param name="mouseY">Current Y-location of the mouse to determine popu location</param>
121134 internal void AddLabel ( int mouseX , int mouseY )
122135 {
123- var labelInputPopup = new EditLabel ( "<label>" , mouseX , mouseY ,
136+ var labelInputPopup = new EditLabel ( "<label>" , mouseX , mouseY ,
124137 ( newLabelText ) => labels . Add ( drawArea . MouseLocation , newLabelText ) ,
125138 allowDelete : false ) ;
126139 TrackViewer . Localize ( labelInputPopup ) ;
127140 labelInputPopup . ShowDialog ( ) ;
128141 }
129142
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+
130163 /// <summary>
131164 /// Popup a context menu that allows you to edit the text of a label
132165 /// </summary>
133166 /// <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 )
136169 {
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 ;
140174
141- var contextMenu = new ContextMenu ( ) ;
175+ SetLocationMenuItem . CommandParameter = mouseLocation ;
142176
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 ;
149180
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+ }
153186 }
154187
155188 /// <summary>
@@ -266,7 +299,7 @@ private void LoadJson(string fileName)
266299 MessageBox . Show ( TrackViewer . catalog . GetString ( "The .json file could not be read properly." ) ) ;
267300 return ;
268301 }
269-
302+
270303 bool itemsWereRemoved = labelsNew . Sanitize ( ) ;
271304 int itemsLeft = labelsNew . Labels . Count ( ) ;
272305 string message = string . Empty ;
@@ -320,8 +353,8 @@ internal void OnLeftMouseMoved()
320353 //The new location is then 'original' + 'current' - 'start'.
321354 draggingStartLocation . NormalizeTo ( drawArea . MouseLocation . TileX , drawArea . MouseLocation . TileZ ) ;
322355 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 ,
325358 draggingLabelToReplace . WorldLocation . Location . X + drawArea . MouseLocation . Location . X - draggingStartLocation . Location . X ,
326359 0 ,
327360 draggingLabelToReplace . WorldLocation . Location . Z + drawArea . MouseLocation . Location . Z - draggingStartLocation . Location . Z
0 commit comments