55
66using Intersect . Editor . Networking ;
77using Intersect . GameObjects . Maps . MapList ;
8+ using Intersect . Logging ;
89
910namespace Intersect . Editor . Forms . Controls
1011{
1112
1213 public partial class MapTreeList : UserControl
1314 {
15+ private readonly Dictionary < MapListItem , TreeNode > _nodeLookup = new ( ) ;
1416
1517 //Cross Thread Delegates
1618 public delegate void TryUpdateMapList ( Guid selectMap , List < Guid > restrictMaps = null ) ;
@@ -23,7 +25,7 @@ public partial class MapTreeList : UserControl
2325
2426 private List < Guid > mOpenFolders = new List < Guid > ( ) ;
2527
26- private List < Guid > mRestrictMapIds ;
28+ private List < Guid > ? mRestrictMapIds ;
2729
2830 private System . Drawing . Point mScrollPoint ;
2931
@@ -76,7 +78,7 @@ private void treeMapList_DragDrop(object sender, DragEventArgs e)
7678 var targetNode = list . GetNodeAt ( targetPoint ) ;
7779
7880 // Retrieve the node that was dragged.
79- var draggedNode = ( TreeNode ) e . Data . GetData ( typeof ( TreeNode ) ) ;
81+ var draggedNode = ( TreeNode ) e . Data . GetData ( typeof ( TreeNode ) ) ;
8082 int srcType ;
8183 Guid srcId ;
8284 switch ( draggedNode . Tag )
@@ -106,7 +108,7 @@ private void treeMapList_DragDrop(object sender, DragEventArgs e)
106108 parent = parent . Parent ;
107109 }
108110
109- // Confirm that the node at the drop location is not
111+ // Confirm that the node at the drop location is not
110112 // the dragged node and that target node isn't null
111113 // (for example if you drag outside the control)
112114 if ( ! draggedNode . Equals ( targetNode ) && targetNode != null )
@@ -116,7 +118,7 @@ private void treeMapList_DragDrop(object sender, DragEventArgs e)
116118 case MapListMap targetMap :
117119 PacketSender . SendMapListMove ( srcType , srcId , 1 , targetMap . MapId ) ;
118120
119- // Remove the node from its current
121+ // Remove the node from its current
120122 // location and add it to the node at the drop location.
121123 draggedNode . Remove ( ) ;
122124
@@ -129,20 +131,20 @@ private void treeMapList_DragDrop(object sender, DragEventArgs e)
129131 targetNode . Parent . Nodes . Insert ( targetNode . Index , draggedNode ) ;
130132 }
131133
132- // Expand the node at the location
134+ // Expand the node at the location
133135 // to show the dropped node.
134136 targetNode . Expand ( ) ;
135137 break ;
136138
137139 case MapListFolder targetFolder :
138140 PacketSender . SendMapListMove ( srcType , srcId , 0 , targetFolder . FolderId ) ;
139141
140- // Remove the node from its current
142+ // Remove the node from its current
141143 // location and add it to the node at the drop location.
142144 draggedNode . Remove ( ) ;
143145 targetNode . Nodes . Add ( draggedNode ) ;
144146
145- // Expand the node at the location
147+ // Expand the node at the location
146148 // to show the dropped node.
147149 targetNode . Expand ( ) ;
148150 break ;
@@ -154,7 +156,7 @@ private void treeMapList_DragDrop(object sender, DragEventArgs e)
154156 var destId = Guid . Empty ;
155157 PacketSender . SendMapListMove ( srcType , srcId , destType , destId ) ;
156158
157- // Remove the node from its current
159+ // Remove the node from its current
158160 // location and add it to the node at the drop location.
159161 draggedNode . Remove ( ) ;
160162 list . Nodes . Add ( draggedNode ) ;
@@ -257,47 +259,56 @@ private void BeginEdit(TreeNode node)
257259 node . BeginEdit ( ) ;
258260 }
259261
260- public void UpdateMapList ( Guid selectMapId = new Guid ( ) , List < Guid > restrictMaps = null )
262+ public void UpdateMapList ( Guid selectMapId = default , List < Guid > ? restrictMaps = null )
261263 {
262- list . Nodes . Clear ( ) ;
263- mRestrictMapIds = restrictMaps ;
264- AddMapListToTree ( MapList . List , null , selectMapId , mRestrictMapIds ) ;
264+ Log . Info ( "Updating list" ) ;
265+ var selectedMapListMap = selectMapId == default ? default : MapList . List . FindMap ( selectMapId ) ;
266+ if ( selectedMapListMap != default && _nodeLookup . TryGetValue ( selectedMapListMap , out var treeNode ) )
267+ {
268+ list . SelectedNode = treeNode ;
269+ }
270+ else
271+ {
272+ list . Nodes . Clear ( ) ;
273+ _nodeLookup . Clear ( ) ;
274+ mRestrictMapIds = restrictMaps ;
275+ AddMapListToTree ( MapList . List , null , selectMapId , mRestrictMapIds ) ;
276+ }
265277 }
266278
267279 private void AddMapListToTree (
268280 MapList mapList ,
269- TreeNode parent ,
270- Guid selectMapId = new Guid ( ) ,
271- List < Guid > restrictMaps = null
281+ TreeNode ? parent ,
282+ Guid selectMapId = default ,
283+ List < Guid > ? restrictMaps = null
272284 )
273285 {
274286 TreeNode tmpNode ;
275287 if ( Chronological )
276288 {
277- for ( var i = 0 ; i < MapList . OrderedMaps . Count ; i ++ )
289+ foreach ( var map in MapList . OrderedMaps )
278290 {
279- if ( restrictMaps == null || restrictMaps . Contains ( MapList . OrderedMaps [ i ] . MapId ) )
291+ if ( restrictMaps != null && ! restrictMaps . Contains ( map . MapId ) )
280292 {
281- tmpNode = list . Nodes . Add ( MapList . OrderedMaps [ i ] . Name ) ;
282- tmpNode . Tag = MapList . OrderedMaps [ i ] ;
283- tmpNode . ImageIndex = 1 ;
284- tmpNode . SelectedImageIndex = 1 ;
285- if ( selectMapId != Guid . Empty )
286- {
287- if ( MapList . OrderedMaps [ i ] . MapId == selectMapId )
288- {
289- list . SelectedNode = tmpNode ;
290- list . Focus ( ) ;
291- }
292- }
293- else
294- {
295- if ( mSelectionType == 0 && mSelectedMap == MapList . OrderedMaps [ i ] . MapId )
296- {
297- list . SelectedNode = tmpNode ;
298- list . Focus ( ) ;
299- }
300- }
293+ continue ;
294+ }
295+
296+ tmpNode = list . Nodes . Add ( map . Name ) ;
297+ _nodeLookup [ map ] = tmpNode ;
298+ tmpNode . Tag = map ;
299+ tmpNode . ImageIndex = 1 ;
300+ tmpNode . SelectedImageIndex = 1 ;
301+
302+ var selectedId = selectMapId ;
303+ if ( selectedId == default && mSelectionType == 0 )
304+ {
305+ selectedId = mSelectedMap ;
306+ }
307+
308+ if ( map . MapId == selectMapId )
309+ {
310+ list . SelectedNode = tmpNode ;
311+ list . Focus ( ) ;
301312 }
302313 }
303314 }
@@ -308,29 +319,17 @@ private void AddMapListToTree(
308319 switch ( item )
309320 {
310321 case MapListFolder folder :
311- if ( parent == null )
312- {
313- tmpNode = list . Nodes . Add ( item . Name ) ;
314- tmpNode . Tag = ( MapListFolder ) item ;
315- AddMapListToTree (
316- ( ( MapListFolder ) item ) . Children , tmpNode , selectMapId , restrictMaps
317- ) ;
318- }
319- else
320- {
321- tmpNode = parent . Nodes . Add ( item . Name ) ;
322- tmpNode . Tag = ( MapListFolder ) item ;
323- AddMapListToTree (
324- ( ( MapListFolder ) item ) . Children , tmpNode , selectMapId , restrictMaps
325- ) ;
326- }
322+ tmpNode = ( parent ? . Nodes ?? list . Nodes ) . Add ( item . Name ) ;
323+ _nodeLookup [ item ] = tmpNode ;
324+ tmpNode . Tag = item ;
325+ AddMapListToTree ( folder . Children , tmpNode , selectMapId , restrictMaps ) ;
327326
328- if ( mOpenFolders . Contains ( ( ( MapListFolder ) item ) . FolderId ) )
327+ if ( mOpenFolders . Contains ( folder . FolderId ) )
329328 {
330329 tmpNode . Expand ( ) ;
331330 }
332331
333- if ( mSelectionType == 1 && mSelectedMap == ( ( MapListFolder ) item ) . FolderId )
332+ if ( mSelectionType == 1 && mSelectedMap == folder . FolderId )
334333 {
335334 list . SelectedNode = tmpNode ;
336335 list . Focus ( ) ;
@@ -341,31 +340,29 @@ private void AddMapListToTree(
341340 break ;
342341
343342 case MapListMap map :
344- if ( restrictMaps == null || restrictMaps . Contains ( map . MapId ) )
343+ if ( restrictMaps ? . Contains ( map . MapId ) ?? true )
345344 {
346345 tmpNode = ( parent ? . Nodes ?? list . Nodes ) . Add ( item . Name ) ;
346+ _nodeLookup [ item ] = tmpNode ;
347347 tmpNode . Tag = map ;
348348
349- if ( selectMapId != Guid . Empty )
349+ var selectedId = selectMapId ;
350+ if ( selectedId == default && mSelectionType == 0 )
350351 {
351- if ( map . MapId == selectMapId )
352- {
353- list . SelectedNode = tmpNode ;
354- list . Focus ( ) ;
355- }
352+ selectedId = mSelectedMap ;
356353 }
357- else
354+
355+ if ( map . MapId == selectMapId )
358356 {
359- if ( mSelectionType == 0 && mSelectedMap == map . MapId )
360- {
361- list . SelectedNode = tmpNode ;
362- list . Focus ( ) ;
363- }
357+ list . SelectedNode = tmpNode ;
358+ list . Focus ( ) ;
364359 }
365360
366361 tmpNode . ImageIndex = 1 ;
367362 tmpNode . SelectedImageIndex = 1 ;
363+ break ;
368364 }
365+
369366 break ;
370367 }
371368 }
@@ -419,12 +416,12 @@ public static void Scroll(Control control)
419416 if ( pt . Y + 20 > control . Height )
420417 {
421418 // scroll down
422- SendMessage ( control . Handle , 277 , ( IntPtr ) 1 , ( IntPtr ) 0 ) ;
419+ SendMessage ( control . Handle , 277 , ( IntPtr ) 1 , ( IntPtr ) 0 ) ;
423420 }
424421 else if ( pt . Y < 20 )
425422 {
426423 // scroll up
427- SendMessage ( control . Handle , 277 , ( IntPtr ) 0 , ( IntPtr ) 0 ) ;
424+ SendMessage ( control . Handle , 277 , ( IntPtr ) 0 , ( IntPtr ) 0 ) ;
428425 }
429426 }
430427
0 commit comments