Skip to content

Commit 26f3b1c

Browse files
committed
Implement game control management in the UI. Added functionality to enable/disable game-related controls based on game state. Introduced methods to initialize and manage UI elements that are dependent on the game's status, enhancing user experience during gameplay.
1 parent 9042746 commit 26f3b1c

File tree

2 files changed

+97
-2
lines changed

2 files changed

+97
-2
lines changed

Form1.cs

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Drawing;
33
using System.Windows.Forms;
44
using System.Linq;
5+
using System.Collections.Generic;
56

67
namespace WinFormsApp1
78
{
@@ -24,6 +25,11 @@ public partial class Form1 : Form
2425
private Button quickInventoryButton;
2526
private Button quickMapButton;
2627

28+
// UI elements that should be disabled until game starts
29+
private List<Control> gameRequiredControls;
30+
private List<ToolStripItem> gameRequiredMenuItems;
31+
private List<ToolStripItem> gameRequiredToolbarItems;
32+
2733
public Form1()
2834
{
2935
InitializeComponent();
@@ -78,10 +84,81 @@ private void InitializeGameUI()
7884
// Apply default theme
7985
ApplyTheme("Classic");
8086

87+
// Initialize game-required controls tracking
88+
InitializeGameStateControls();
89+
8190
// Set focus to input
8291
inputTextBox.Focus();
8392
}
8493

94+
private void InitializeGameStateControls()
95+
{
96+
gameRequiredControls = new List<Control>();
97+
gameRequiredMenuItems = new List<ToolStripItem>();
98+
gameRequiredToolbarItems = new List<ToolStripItem>();
99+
100+
// Initially disable game-dependent UI elements
101+
SetGameControlsEnabled(false);
102+
}
103+
104+
private void SetGameControlsEnabled(bool enabled)
105+
{
106+
// Find and manage toolbar items
107+
foreach (ToolStripItem item in toolStrip.Items)
108+
{
109+
if (item.Text == "Save" || item.Text == "Load" || item.Text == "Inventory" || item.Text == "Map")
110+
{
111+
item.Enabled = enabled;
112+
}
113+
}
114+
115+
// Find and manage menu items
116+
foreach (ToolStripMenuItem mainMenu in menuStrip.Items)
117+
{
118+
if (mainMenu.Text.Contains("Character") || mainMenu.Text.Contains("World"))
119+
{
120+
mainMenu.Enabled = enabled;
121+
}
122+
else if (mainMenu.Text.Contains("Game"))
123+
{
124+
foreach (ToolStripItem subItem in mainMenu.DropDownItems)
125+
{
126+
if (subItem.Text.Contains("Save") || subItem.Text.Contains("Load"))
127+
{
128+
subItem.Enabled = enabled;
129+
}
130+
}
131+
}
132+
}
133+
134+
// Manage quick action buttons
135+
if (quickInventoryButton != null) quickInventoryButton.Enabled = enabled;
136+
if (quickMapButton != null) quickMapButton.Enabled = enabled;
137+
138+
// Find and manage side panel quick action buttons
139+
var quickActionsPanel = sidePanel?.Controls.OfType<GroupBox>().FirstOrDefault(g => g.Text == "Quick Actions");
140+
if (quickActionsPanel != null)
141+
{
142+
foreach (Control control in quickActionsPanel.Controls)
143+
{
144+
if (control is TableLayoutPanel layout)
145+
{
146+
foreach (Control button in layout.Controls)
147+
{
148+
if (button is Button btn && (btn.Text == "Stats" || btn.Text == "Save" || btn.Text == "Load"))
149+
{
150+
btn.Enabled = enabled;
151+
}
152+
}
153+
}
154+
}
155+
}
156+
157+
// Enable/disable input controls
158+
if (inputTextBox != null) inputTextBox.Enabled = enabled;
159+
if (submitButton != null) submitButton.Enabled = enabled;
160+
}
161+
85162
private void CreateMenuStrip()
86163
{
87164
menuStrip = new MenuStrip();
@@ -363,10 +440,11 @@ private GroupBox CreateStatsPanel()
363440
AutoSizeMode = AutoSizeMode.GrowAndShrink,
364441
ColumnCount = 2,
365442
RowCount = 8, // Increased to accommodate more stats
366-
Dock = DockStyle.Top
443+
Location = new Point(0, 0),
444+
Width = statsContainer.Width - 10 // Account for padding
367445
};
368446

369-
// Set column styles
447+
// Set column styles to prevent overflow
370448
statsLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
371449
statsLayout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
372450

@@ -997,5 +1075,15 @@ protected override void OnFormClosing(FormClosingEventArgs e)
9971075
}
9981076
base.OnFormClosing(e);
9991077
}
1078+
1079+
public void EnableGameControls(bool enabled)
1080+
{
1081+
if (this.InvokeRequired)
1082+
{
1083+
this.Invoke(new Action(() => EnableGameControls(enabled)));
1084+
return;
1085+
}
1086+
SetGameControlsEnabled(enabled);
1087+
}
10001088
}
10011089
}

GameEngine.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ public void StartNewGame()
7474
currentLocation = locations["village"];
7575
HasUnsavedChanges = false;
7676

77+
// Enable game controls now that a game has started
78+
gameForm.EnableGameControls(true);
79+
7780
ShowLocation();
7881
gameForm.UpdateStatus($"Health: {player.Health}/{player.MaxHealth} | Level: {player.Level} | Gold: {player.Gold}");
7982
}
@@ -595,6 +598,10 @@ public void LoadGame(string saveName = null)
595598
gameForm.ClearScreen();
596599
gameForm.DisplayText($"Game loaded from '{saveName}'.", Color.Green);
597600
gameForm.DisplayText("");
601+
602+
// Enable game controls since we now have a loaded game
603+
gameForm.EnableGameControls(true);
604+
598605
ShowLocation();
599606
HasUnsavedChanges = false;
600607
}

0 commit comments

Comments
 (0)