2424using Newtonsoft . Json ;
2525
2626using ORTS . Common ;
27- using ORTS . TrackViewer . UserInterface ;
2827
2928namespace ORTS . TrackViewer . Drawing . Labels
3029{
@@ -35,7 +34,7 @@ namespace ORTS.TrackViewer.Drawing.Labels
3534 /// The list of labels can be saved or loaded to .json. This will not be done automatically.
3635 /// The labels can be modified either w.r.t. to their location (dragging) or w.r.t. their text.
3736 /// </summary>
38- public class DrawLabels
37+ class DrawLabels
3938 {
4039 #region private fields
4140 /// <summary>This is the list of labels that we need to draw and possibly modify </summary>
@@ -57,15 +56,6 @@ public class DrawLabels
5756 private WorldLocation draggingStartLocation ;
5857 /// <summary>Are we currently dragging?</summary>
5958 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-
6959 #endregion
7060
7161 #region Constructor
@@ -74,12 +64,9 @@ public class DrawLabels
7464 /// Constructor
7565 /// </summary>
7666 /// <param name="fontHeight">The height of the font so we can correct during drawing</param>
77- public DrawLabels ( TrackViewer trackViewer , int fontHeight )
67+ public DrawLabels ( int fontHeight )
7868 {
7969 this . fontHeight = fontHeight ;
80- TrackViewer = trackViewer ;
81-
82- CreateContextMenu ( ) ;
8370 }
8471 #endregion
8572
@@ -101,7 +88,7 @@ public void Draw(DrawArea drawArea)
10188 DrawLabel ( label ) ;
10289 }
10390 float distanceSquared = WorldLocation . GetDistanceSquared2D ( label . WorldLocation , drawArea . MouseLocation ) ;
104- if ( distanceSquared < closestDistanceSquared )
91+ if ( distanceSquared < closestDistanceSquared )
10592 {
10693 closestDistanceSquared = distanceSquared ;
10794 closestToMouseLabel = label ;
@@ -120,7 +107,7 @@ public void Draw(DrawArea drawArea)
120107 /// <param name="label">The lable to draw</param>
121108 private void DrawLabel ( StorableLabel label )
122109 {
123- drawArea . DrawExpandingString ( label . WorldLocation , label . LabelText , 0 , - fontHeight / 2 ) ;
110+ drawArea . DrawExpandingString ( label . WorldLocation , label . LabelText , 0 , - fontHeight / 2 ) ;
124111 }
125112
126113 #endregion
@@ -133,58 +120,36 @@ private void DrawLabel(StorableLabel label)
133120 /// <param name="mouseY">Current Y-location of the mouse to determine popu location</param>
134121 internal void AddLabel ( int mouseX , int mouseY )
135122 {
136- var labelInputPopup = new EditLabel ( "<label>" , mouseX , mouseY ,
123+ var labelInputPopup = new EditLabel ( "<label>" , mouseX , mouseY ,
137124 ( newLabelText ) => labels . Add ( drawArea . MouseLocation , newLabelText ) ,
138125 allowDelete : false ) ;
139126 TrackViewer . Localize ( labelInputPopup ) ;
140127 labelInputPopup . ShowDialog ( ) ;
141128 }
142129
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 . Icon = TrackViewer . menuControl . ThreeDSceneIcon ;
159- SetLocationMenuItem . Click += new RoutedEventHandler ( ( sender , e ) => TrackViewer . menuControl . MenuSceneWindow_Click ( sender , e ) ) ;
160- SetLocationMenuItem . Click += new RoutedEventHandler ( async ( sender , e ) => await TrackViewer . SceneView ? . SetCameraLocation (
161- SetLocationMenuItem . CommandParameter as WorldLocation ? ?? new WorldLocation ( ) ) ) ;
162- ContextMenu . Items . Add ( SetLocationMenuItem ) ;
163- }
164-
165130 /// <summary>
166131 /// Popup a context menu that allows you to edit the text of a label
167132 /// </summary>
168133 /// <param name="mouseX">Current X-location of the mouse to determine popup location</param>
169- /// <param name="mouseY">Current Y-location of the mouse to determine popup location</param>
170- internal void PopupContextMenu ( int mouseX , int mouseY , WorldLocation mouseLocation )
134+ /// <param name="mouseY">Current Y-location of the mouse to determine popu location</param>
135+ internal void PopupContextMenu ( int mouseX , int mouseY )
171136 {
172- if ( ! Properties . Settings . Default . showLabels || labels . Labels . Count ( ) == 0 )
173- EditLabelMenuItem . Visibility = Visibility . Collapsed ;
174- else
175- EditLabelMenuItem . Visibility = Visibility . Visible ;
176-
177- SetLocationMenuItem . CommandParameter = mouseLocation ;
137+ if ( ! Properties . Settings . Default . showLabels ) return ;
138+ if ( labels . Labels . Count ( ) == 0 ) return ;
139+ var editingLabel = closestToMouseLabel ;
178140
179- var visible = false ;
180- foreach ( MenuItem item in ContextMenu . Items )
181- visible |= item . Visibility == Visibility . Visible ;
141+ var contextMenu = new ContextMenu ( ) ;
182142
183- if ( visible )
143+ var editLabelMenuItem = new MenuItem
184144 {
185- ContextMenu . PlacementRectangle = new Rect ( ( double ) mouseX , ( double ) mouseY , 20 , 20 ) ;
186- ContextMenu . IsOpen = true ;
187- }
145+ Header = "Edit label" ,
146+ IsCheckable = false
147+ } ;
148+ editLabelMenuItem . Click += new RoutedEventHandler ( ( sender , e ) => ModifyLabel ( closestToMouseLabel , mouseX , mouseY ) ) ;
149+
150+ contextMenu . Items . Add ( editLabelMenuItem ) ;
151+ contextMenu . PlacementRectangle = new Rect ( ( double ) mouseX , ( double ) mouseY , 20 , 20 ) ;
152+ contextMenu . IsOpen = true ;
188153 }
189154
190155 /// <summary>
@@ -301,7 +266,7 @@ private void LoadJson(string fileName)
301266 MessageBox . Show ( TrackViewer . catalog . GetString ( "The .json file could not be read properly." ) ) ;
302267 return ;
303268 }
304-
269+
305270 bool itemsWereRemoved = labelsNew . Sanitize ( ) ;
306271 int itemsLeft = labelsNew . Labels . Count ( ) ;
307272 string message = string . Empty ;
@@ -355,8 +320,8 @@ internal void OnLeftMouseMoved()
355320 //The new location is then 'original' + 'current' - 'start'.
356321 draggingStartLocation . NormalizeTo ( drawArea . MouseLocation . TileX , drawArea . MouseLocation . TileZ ) ;
357322 WorldLocation shiftedLocation = new WorldLocation (
358- draggingLabelToReplace . WorldLocation . TileX + drawArea . MouseLocation . TileX - draggingStartLocation . TileX ,
359- draggingLabelToReplace . WorldLocation . TileZ + drawArea . MouseLocation . TileZ - draggingStartLocation . TileZ ,
323+ draggingLabelToReplace . WorldLocation . TileX + drawArea . MouseLocation . TileX - draggingStartLocation . TileX ,
324+ draggingLabelToReplace . WorldLocation . TileZ + drawArea . MouseLocation . TileZ - draggingStartLocation . TileZ ,
360325 draggingLabelToReplace . WorldLocation . Location . X + drawArea . MouseLocation . Location . X - draggingStartLocation . Location . X ,
361326 0 ,
362327 draggingLabelToReplace . WorldLocation . Location . Z + drawArea . MouseLocation . Location . Z - draggingStartLocation . Location . Z
0 commit comments