Skip to content

Commit

Permalink
[HaCreator] Fix NullReferenceException when attempting to close the l…
Browse files Browse the repository at this point in the history
…ast map editor tab

ref #113
  • Loading branch information
lastbattle committed Jun 27, 2021
1 parent 090d53c commit 5abb50c
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 28 deletions.
3 changes: 2 additions & 1 deletion HaCreator/GUI/HaEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
<!-- Side panel-->
<ScrollViewer Grid.Row="1" Grid.Column="1"
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Visible">
VerticalScrollBarVisibility="Visible"
x:Name="editorPanel">
<StackPanel Orientation="Vertical">

<Expander Header="Tile"
Expand Down
4 changes: 3 additions & 1 deletion HaCreator/GUI/HaEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ private void HaEditor2_Loaded(object sender, RoutedEventArgs e)
{
// helper classes
handler = new InputHandler(multiBoard);
hcsm = new HaCreatorStateManager(multiBoard, ribbon, tabControl1, handler, textblock_CursorX, textblock_CursorY, textblock_RCursorX, textblock_RCursorY, textblock_selectedItem);
hcsm = new HaCreatorStateManager(
multiBoard, ribbon, tabControl1, handler, editorPanel,
textblock_CursorX, textblock_CursorY, textblock_RCursorX, textblock_RCursorY, textblock_selectedItem);
hcsm.CloseRequested += Hcsm_CloseRequested;
hcsm.FirstMapLoaded += Hcsm_FirstMapLoaded;

Expand Down
2 changes: 1 addition & 1 deletion HaCreator/GUI/Save.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace HaCreator.GUI
{
public partial class Save : Form
{
private Board board;
private readonly Board board;

public Save(Board board)
{
Expand Down
26 changes: 21 additions & 5 deletions HaCreator/MapEditor/HaCreatorStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public class HaCreatorStateManager
private readonly InputHandler input;
private TilePanel tilePanel;
private ObjPanel objPanel;
private System.Windows.Controls.ScrollViewer editorPanel;
public readonly BackupManager backupMan;

public HaCreatorStateManager(MultiBoard multiBoard, HaRibbon ribbon, System.Windows.Controls.TabControl tabs, InputHandler input,
public HaCreatorStateManager(MultiBoard multiBoard, HaRibbon ribbon, System.Windows.Controls.TabControl tabs, InputHandler input, System.Windows.Controls.ScrollViewer editorPanel,
SystemWinCtl.TextBlock textblock_CursorX, SystemWinCtl.TextBlock textblock_CursorY, SystemWinCtl.TextBlock textblock_RCursorX, SystemWinCtl.TextBlock textblock_RCursorY, SystemWinCtl.TextBlock textblock_selectedItem)
{
this.multiBoard = multiBoard;
Expand All @@ -61,6 +62,7 @@ public HaCreatorStateManager(MultiBoard multiBoard, HaRibbon ribbon, System.Wind
this.ribbon = ribbon;
this.tabs = tabs;
this.input = input;
this.editorPanel = editorPanel;

// Status bar
this.textblock_CursorX = textblock_CursorX;
Expand Down Expand Up @@ -442,18 +444,18 @@ private void MapAddMinimap(object sender, EventArgs e)
/// <param name="e"></param>
private void CloseMapTab(object sender, EventArgs e)
{
if (tabs.Items.Count <= 1) // at least 1 tabs for now
if (tabs.Items.Count <= 0) // at least 1 tabs for now
{
return;
}
if (MessageBox.Show("Are you sure you want to close this map?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
return;

System.Windows.Controls.MenuItem item = (System.Windows.Controls.MenuItem)sender;
System.Windows.Controls.MenuItem item = (System.Windows.Controls.MenuItem)sender;
if (item == null)
return;

System.Windows.Controls.TabItem tabItem = (System.Windows.Controls.TabItem) item.Tag;
System.Windows.Controls.TabItem tabItem = (System.Windows.Controls.TabItem)item.Tag;
TabItemContainer container = (TabItemContainer)tabItem.Tag;
Board selectedBoard = container.Board;
lock (selectedBoard.ParentControl)
Expand All @@ -463,10 +465,23 @@ private void CloseMapTab(object sender, EventArgs e)

selectedBoard.Dispose();
}

UpdateEditorPanelVisibility();
}

/// <summary>
/// If there's no more tabs, disable the ability for the user to select any new map objects to be added
/// </summary>
public void UpdateEditorPanelVisibility()
{
editorPanel.IsEnabled = tabs.Items.Count > 0; // at least 1 tabs for now
}

private void Tabs_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (multiBoard.SelectedBoard == null)
return;

lock (multiBoard)
{
MultiBoard_ReturnToSelectionState();
Expand All @@ -482,11 +497,12 @@ private void Tabs_SelectionChanged(object sender, System.Windows.Controls.Select
ribbon.SetLayers(multiBoard.SelectedBoard.Layers);
ribbon.SetSelectedLayer(multiBoard.SelectedBoard.SelectedLayerIndex, multiBoard.SelectedBoard.SelectedPlatform, multiBoard.SelectedBoard.SelectedAllLayers, multiBoard.SelectedBoard.SelectedAllPlatforms);
ribbon.SetHasMinimap(multiBoard.SelectedBoard.MinimapRectangle != null);

ParseVisibleEditedTypes();
} else
{
multiBoard.SelectedBoard = null;
}
ParseVisibleEditedTypes();
multiBoard.Focus();
}
}
Expand Down
35 changes: 27 additions & 8 deletions HaCreator/MapEditor/MultiBoard.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,23 @@ public void RenderFrame()
#else
sprite.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied, null, null, null, null, Matrix.CreateScale(1.0f));
#endif
lock (this)

if (selectedBoard != null) // No map selected to draw on
{
selectedBoard.RenderBoard(sprite);
if (selectedBoard.MapSize.X < _CurrentDXWindowSize.Width)
{
DrawLine(sprite, new Vector2(MapSize.X, 0), new Vector2(MapSize.X, _CurrentDXWindowSize.Height), Color.Black);
}
if (selectedBoard.MapSize.Y < _CurrentDXWindowSize.Height)
lock (this)
{
DrawLine(sprite, new Vector2(0, MapSize.Y), new Vector2(_CurrentDXWindowSize.Width, MapSize.Y), Color.Black);
if (selectedBoard != null) // check again
{
selectedBoard.RenderBoard(sprite);
if (selectedBoard.MapSize.X < _CurrentDXWindowSize.Width)
{
DrawLine(sprite, new Vector2(MapSize.X, 0), new Vector2(MapSize.X, _CurrentDXWindowSize.Height), Color.Black);
}
if (selectedBoard.MapSize.Y < _CurrentDXWindowSize.Height)
{
DrawLine(sprite, new Vector2(0, MapSize.Y), new Vector2(_CurrentDXWindowSize.Width, MapSize.Y), Color.Black);
}
}
}
}
#if FPS_TEST
Expand Down Expand Up @@ -672,6 +679,9 @@ private void DxContainer_MouseWheel(object sender, System.Windows.Forms.MouseEve
/// <param name="e"></param>
private void DxContainer_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (selectedBoard == null)
return;

// If the mouse has not been moved while we were in focus (e.g. when clicking on the editor while another window focused), this event will be sent without a mousemove event preceding it.
// We will move it to its correct position by invoking the move event handler manually.
if (selectedBoard.Mouse.X != e.X || selectedBoard.Mouse.Y != e.Y)
Expand Down Expand Up @@ -700,6 +710,9 @@ private void DxContainer_MouseDown(object sender, System.Windows.Forms.MouseEven
/// <param name="e"></param>
private void DxContainer_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (selectedBoard == null)
return;

selectedBoard.Mouse.IsDown = false;
if (e.Button == System.Windows.Forms.MouseButtons.Left && LeftMouseUp != null)
{
Expand All @@ -720,6 +733,9 @@ private void DxContainer_MouseUp(object sender, System.Windows.Forms.MouseEventA
/// <param name="e"></param>
public void DxContainer_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (selectedBoard == null)
return;

lock (this)
{
if (ShortcutKeyPressed != null)
Expand Down Expand Up @@ -750,6 +766,9 @@ public void DxContainer_KeyDown(object sender, System.Windows.Forms.KeyEventArgs
/// <param name="e"></param>
private void DxContainer_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (selectedBoard == null)
return;

lock (this)
{
Point realPosition = new Point(e.X, e.Y);
Expand Down
29 changes: 20 additions & 9 deletions HaCreator/Wz/MapLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -784,33 +784,41 @@ public static void LoadMisc(WzImage mapImage, Board mapBoard)
public static System.Windows.Controls.ContextMenu CreateStandardMapMenu(System.Windows.RoutedEventHandler[] rightClickHandler)
{
System.Windows.Controls.ContextMenu menu = new System.Windows.Controls.ContextMenu();

System.Windows.Controls.MenuItem menuItem1 = new System.Windows.Controls.MenuItem();
menuItem1.Header = "Edit map info...";

System.Windows.Controls.MenuItem menuItem1 = new System.Windows.Controls.MenuItem
{
Header = "Edit map info..."
};
menuItem1.Click += rightClickHandler[0];
menuItem1.Icon = new System.Windows.Controls.Image
{
Source = BitmapHelper.Convert(Properties.Resources.mapEditMenu, System.Drawing.Imaging.ImageFormat.Png)
};

System.Windows.Controls.MenuItem menuItem2 = new System.Windows.Controls.MenuItem();
menuItem2.Header = "Add VR";
System.Windows.Controls.MenuItem menuItem2 = new System.Windows.Controls.MenuItem
{
Header = "Add VR"
};
menuItem2.Click += rightClickHandler[1];
menuItem2.Icon = new System.Windows.Controls.Image
{
Source = BitmapHelper.Convert(Properties.Resources.mapEditMenu, System.Drawing.Imaging.ImageFormat.Png)
};

System.Windows.Controls.MenuItem menuItem3 = new System.Windows.Controls.MenuItem();
menuItem3.Header = "Add Minimap";
System.Windows.Controls.MenuItem menuItem3 = new System.Windows.Controls.MenuItem
{
Header = "Add Minimap"
};
menuItem3.Click += rightClickHandler[2];
menuItem3.Icon = new System.Windows.Controls.Image
{
Source = BitmapHelper.Convert(Properties.Resources.mapEditMenu, System.Drawing.Imaging.ImageFormat.Png)
};

System.Windows.Controls.MenuItem menuItem4 = new System.Windows.Controls.MenuItem();
menuItem4.Header = "Close";
System.Windows.Controls.MenuItem menuItem4 = new System.Windows.Controls.MenuItem
{
Header = "Close"
};
menuItem4.Click += rightClickHandler[3];
menuItem4.Icon = new System.Windows.Controls.Image
{
Expand Down Expand Up @@ -1021,6 +1029,9 @@ public static void CreateMap(string streetName, string mapName, int mapId, strin
{
item.Tag = newTabPage;
}


multiBoard.HaCreatorStateManager.UpdateEditorPanelVisibility();
}
}

Expand Down
8 changes: 5 additions & 3 deletions HaCreator/Wz/MapSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public MapSaver(Board board)

private void CreateImage()
{
string name = "";
string name;
switch (board.MapInfo.mapType)
{
case MapType.RegularMap:
Expand All @@ -49,8 +49,10 @@ private void CreateImage()
default:
throw new Exception("Unknown map type");
}
this.image = new WzImage(name + ".img");
this.image.Parsed = true;
this.image = new WzImage(name + ".img")
{
Parsed = true
};
}

private void InsertImage()
Expand Down

0 comments on commit 5abb50c

Please sign in to comment.