Skip to content

Commit 84c333f

Browse files
authored
fix: don't clear on selection (AscensionGameDev#2108)
1 parent 09365eb commit 84c333f

File tree

5 files changed

+81
-83
lines changed

5 files changed

+81
-83
lines changed

Intersect (Core)/GameObjects/Maps/MapList/MapList.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public void PostLoad(DatabaseObjectLookup gameMaps, bool isServer = true, bool i
5656
{
5757
if (itm.Type == 0)
5858
{
59-
var dirItm = (MapListFolder) itm;
59+
var dirItm = (MapListFolder)itm;
6060
dirItm.PostLoad(gameMaps, isServer);
6161
}
6262
else
6363
{
64-
var mapItm = (MapListMap) itm;
64+
var mapItm = (MapListMap)itm;
6565
var removed = false;
6666
if (isServer)
6767
{
@@ -285,7 +285,7 @@ public void HandleMove(int srcType, Guid srcId, int destType, Guid destId)
285285
{
286286
if (destType == 0)
287287
{
288-
((MapListFolder) dest).Children.Items.Add(source);
288+
((MapListFolder)dest).Children.Items.Add(source);
289289
sourceList.Items.Remove(source);
290290
}
291291
else

Intersect.Editor/Forms/Controls/MapTreeList.cs

Lines changed: 67 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
using Intersect.Editor.Networking;
77
using Intersect.GameObjects.Maps.MapList;
8+
using Intersect.Logging;
89

910
namespace 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

Intersect.Editor/Forms/DockingElements/frmMapGrid.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ public FrmMapGrid()
4646
private void frmMapGrid_Load(object sender, EventArgs e)
4747
{
4848
CreateSwapChain();
49-
if (Globals.MapGrid == null)
50-
{
51-
Globals.MapGrid = new MapGrid(
52-
linkMapToolStripMenuItem, unlinkMapToolStripMenuItem, recacheMapToolStripMenuItem, contextMenuStrip, Icon
53-
);
54-
}
49+
Globals.MapGrid ??= new MapGrid(
50+
linkMapToolStripMenuItem,
51+
unlinkMapToolStripMenuItem,
52+
recacheMapToolStripMenuItem,
53+
contextMenuStrip,
54+
Icon
55+
);
5556

5657
InitLocalization();
5758
}

Intersect.Editor/Forms/frmWarpSelection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ private void InitLocalization()
7474

7575
private void NodeDoubleClick(object sender, TreeViewEventArgs e)
7676
{
77-
if (e.Node.Tag is MapListMap)
77+
if (e.Node?.Tag is MapListMap mapListMap)
7878
{
79-
SelectTile(((MapListMap) e.Node.Tag).MapId, mCurrentX, mCurrentY);
79+
SelectTile(mapListMap.MapId, mCurrentX, mCurrentY);
8080
}
8181
}
8282

Intersect.Editor/Maps/MapGrid.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public MapGrid(
8686
ToolStripMenuItem dropDownUnlink,
8787
ToolStripMenuItem recacheItem,
8888
ContextMenuStrip contextMenu,
89-
Icon icon
89+
Icon? icon
9090
)
9191
{
9292
mWorkerThread = new Thread(AsyncLoadingThread);
@@ -102,7 +102,7 @@ Icon icon
102102
Icon = icon;
103103
}
104104

105-
public Icon Icon { get; set; }
105+
public Icon? Icon { get; set; }
106106

107107
private void AsyncLoadingThread()
108108
{

0 commit comments

Comments
 (0)