1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Drawing ;
4+ using System . Linq ;
5+ using System . Windows . Forms ;
6+ using System . ComponentModel ;
7+
8+ namespace WinFormsApp1 . Controls
9+ {
10+ public partial class GameInputControl : UserControl
11+ {
12+ private TextBox inputTextBox ;
13+ private Button submitButton ;
14+ private Button quickInventoryButton ;
15+ private Label historyLabel ;
16+ private ComboBox commandHistoryComboBox ;
17+ private Button helpButton ;
18+
19+ private List < string > commandHistory ;
20+ private int historyIndex ;
21+
22+ public event EventHandler < string > CommandSubmitted ;
23+ public event EventHandler InventoryRequested ;
24+ public event EventHandler HelpRequested ;
25+
26+ [ DesignerSerializationVisibility ( DesignerSerializationVisibility . Hidden ) ]
27+ public bool InputEnabled
28+ {
29+ get => inputTextBox . Enabled ;
30+ set
31+ {
32+ inputTextBox . Enabled = value ;
33+ submitButton . Enabled = value ;
34+ commandHistoryComboBox . Enabled = value ;
35+ }
36+ }
37+
38+ public GameInputControl ( )
39+ {
40+ commandHistory = new List < string > ( ) ;
41+ historyIndex = - 1 ;
42+ InitializeComponent ( ) ;
43+ }
44+
45+ private void InitializeComponent ( )
46+ {
47+ this . Size = new Size ( 600 , 100 ) ;
48+ this . BackColor = Color . DarkGray ;
49+ this . Padding = new Padding ( 5 ) ;
50+
51+ // Main layout
52+ TableLayoutPanel mainLayout = new TableLayoutPanel
53+ {
54+ Dock = DockStyle . Fill ,
55+ ColumnCount = 4 ,
56+ RowCount = 2 ,
57+ Padding = new Padding ( 2 )
58+ } ;
59+
60+ // Set column styles
61+ mainLayout . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 50F ) ) ; // Input
62+ mainLayout . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 15F ) ) ; // Submit
63+ mainLayout . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 15F ) ) ; // Inventory
64+ mainLayout . ColumnStyles . Add ( new ColumnStyle ( SizeType . Percent , 20F ) ) ; // History/Help
65+
66+ // Set row styles
67+ mainLayout . RowStyles . Add ( new RowStyle ( SizeType . Percent , 60F ) ) ; // Main input row
68+ mainLayout . RowStyles . Add ( new RowStyle ( SizeType . Percent , 40F ) ) ; // History/help row
69+
70+ // Create input controls
71+ CreateInputControls ( mainLayout ) ;
72+
73+ this . Controls . Add ( mainLayout ) ;
74+ }
75+
76+ private void CreateInputControls ( TableLayoutPanel mainLayout )
77+ {
78+ // Command input textbox
79+ inputTextBox = new TextBox
80+ {
81+ Dock = DockStyle . Fill ,
82+ Font = new Font ( "Consolas" , 10 ) ,
83+ PlaceholderText = "Enter command..." ,
84+ BackColor = Color . White ,
85+ ForeColor = Color . Black
86+ } ;
87+ inputTextBox . KeyDown += InputTextBox_KeyDown ;
88+ mainLayout . Controls . Add ( inputTextBox , 0 , 0 ) ;
89+
90+ // Submit button
91+ submitButton = new Button
92+ {
93+ Text = "Submit" ,
94+ Dock = DockStyle . Fill ,
95+ BackColor = Color . DarkBlue ,
96+ ForeColor = Color . White ,
97+ Font = new Font ( "Arial" , 9 , FontStyle . Bold ) ,
98+ FlatStyle = FlatStyle . Flat
99+ } ;
100+ submitButton . Click += SubmitButton_Click ;
101+ mainLayout . Controls . Add ( submitButton , 1 , 0 ) ;
102+
103+ // Quick inventory button
104+ quickInventoryButton = new Button
105+ {
106+ Text = "Inventory" ,
107+ Dock = DockStyle . Fill ,
108+ BackColor = Color . DarkGreen ,
109+ ForeColor = Color . White ,
110+ Font = new Font ( "Arial" , 9 , FontStyle . Bold ) ,
111+ FlatStyle = FlatStyle . Flat
112+ } ;
113+ quickInventoryButton . Click += QuickInventoryButton_Click ;
114+ mainLayout . Controls . Add ( quickInventoryButton , 2 , 0 ) ;
115+
116+ // Help button
117+ helpButton = new Button
118+ {
119+ Text = "Help (F1)" ,
120+ Dock = DockStyle . Fill ,
121+ BackColor = Color . DarkOrange ,
122+ ForeColor = Color . White ,
123+ Font = new Font ( "Arial" , 8 , FontStyle . Bold ) ,
124+ FlatStyle = FlatStyle . Flat
125+ } ;
126+ helpButton . Click += HelpButton_Click ;
127+ mainLayout . Controls . Add ( helpButton , 3 , 0 ) ;
128+
129+ // Command history dropdown
130+ commandHistoryComboBox = new ComboBox
131+ {
132+ Dock = DockStyle . Fill ,
133+ DropDownStyle = ComboBoxStyle . DropDownList ,
134+ Font = new Font ( "Consolas" , 8 ) ,
135+ BackColor = Color . LightGray
136+ } ;
137+ commandHistoryComboBox . SelectedIndexChanged += CommandHistoryComboBox_SelectedIndexChanged ;
138+ mainLayout . Controls . Add ( commandHistoryComboBox , 0 , 1 ) ;
139+
140+ // History label
141+ historyLabel = new Label
142+ {
143+ Text = "Use ↑↓ for command history | Recent commands →" ,
144+ Dock = DockStyle . Fill ,
145+ TextAlign = ContentAlignment . MiddleLeft ,
146+ ForeColor = Color . DarkBlue ,
147+ Font = new Font ( "Arial" , 8 ) ,
148+ AutoEllipsis = true
149+ } ;
150+ mainLayout . SetColumnSpan ( historyLabel , 3 ) ;
151+ mainLayout . Controls . Add ( historyLabel , 1 , 1 ) ;
152+
153+ // Add tooltips
154+ var toolTip = new ToolTip ( ) ;
155+ toolTip . SetToolTip ( inputTextBox , "Type game commands here. Use arrow keys for history." ) ;
156+ toolTip . SetToolTip ( submitButton , "Submit command (Enter)" ) ;
157+ toolTip . SetToolTip ( quickInventoryButton , "Open inventory (Tab)" ) ;
158+ toolTip . SetToolTip ( helpButton , "Show help (F1)" ) ;
159+ toolTip . SetToolTip ( commandHistoryComboBox , "Select from recent commands" ) ;
160+ }
161+
162+ private void InputTextBox_KeyDown ( object sender , KeyEventArgs e )
163+ {
164+ switch ( e . KeyCode )
165+ {
166+ case Keys . Enter :
167+ ProcessInput ( ) ;
168+ e . Handled = true ;
169+ break ;
170+
171+ case Keys . Up :
172+ NavigateHistory ( - 1 ) ;
173+ e . Handled = true ;
174+ break ;
175+
176+ case Keys . Down :
177+ NavigateHistory ( 1 ) ;
178+ e . Handled = true ;
179+ break ;
180+
181+ case Keys . Tab :
182+ InventoryRequested ? . Invoke ( this , EventArgs . Empty ) ;
183+ e . Handled = true ;
184+ break ;
185+
186+ case Keys . F1 :
187+ HelpRequested ? . Invoke ( this , EventArgs . Empty ) ;
188+ e . Handled = true ;
189+ break ;
190+
191+ case Keys . Escape :
192+ inputTextBox . Clear ( ) ;
193+ e . Handled = true ;
194+ break ;
195+ }
196+ }
197+
198+ private void SubmitButton_Click ( object sender , EventArgs e )
199+ {
200+ ProcessInput ( ) ;
201+ }
202+
203+ private void QuickInventoryButton_Click ( object sender , EventArgs e )
204+ {
205+ InventoryRequested ? . Invoke ( this , EventArgs . Empty ) ;
206+ }
207+
208+ private void HelpButton_Click ( object sender , EventArgs e )
209+ {
210+ HelpRequested ? . Invoke ( this , EventArgs . Empty ) ;
211+ }
212+
213+ private void CommandHistoryComboBox_SelectedIndexChanged ( object sender , EventArgs e )
214+ {
215+ if ( commandHistoryComboBox . SelectedItem != null )
216+ {
217+ inputTextBox . Text = commandHistoryComboBox . SelectedItem . ToString ( ) ;
218+ inputTextBox . SelectionStart = inputTextBox . Text . Length ;
219+ inputTextBox . Focus ( ) ;
220+ }
221+ }
222+
223+ private void ProcessInput ( )
224+ {
225+ string input = inputTextBox . Text . Trim ( ) ;
226+ if ( ! string . IsNullOrEmpty ( input ) )
227+ {
228+ AddToHistory ( input ) ;
229+ CommandSubmitted ? . Invoke ( this , input ) ;
230+ inputTextBox . Clear ( ) ;
231+ historyIndex = - 1 ;
232+ }
233+ }
234+
235+ private void AddToHistory ( string command )
236+ {
237+ // Remove if already exists to avoid duplicates
238+ commandHistory . Remove ( command ) ;
239+
240+ // Add to beginning of list
241+ commandHistory . Insert ( 0 , command ) ;
242+
243+ // Keep only last 20 commands
244+ if ( commandHistory . Count > 20 )
245+ {
246+ commandHistory . RemoveAt ( commandHistory . Count - 1 ) ;
247+ }
248+
249+ // Update dropdown
250+ UpdateHistoryDropdown ( ) ;
251+ }
252+
253+ private void UpdateHistoryDropdown ( )
254+ {
255+ commandHistoryComboBox . Items . Clear ( ) ;
256+ commandHistoryComboBox . Items . AddRange ( commandHistory . Take ( 10 ) . ToArray ( ) ) ;
257+
258+ // Update history label
259+ if ( commandHistory . Any ( ) )
260+ {
261+ historyLabel . Text = $ "Recent: { string . Join ( ", " , commandHistory . Take ( 3 ) ) } ...";
262+ }
263+ }
264+
265+ private void NavigateHistory ( int direction )
266+ {
267+ if ( ! commandHistory . Any ( ) ) return ;
268+
269+ historyIndex += direction ;
270+
271+ if ( historyIndex < 0 )
272+ {
273+ historyIndex = - 1 ;
274+ inputTextBox . Clear ( ) ;
275+ }
276+ else if ( historyIndex >= commandHistory . Count )
277+ {
278+ historyIndex = commandHistory . Count - 1 ;
279+ }
280+
281+ if ( historyIndex >= 0 && historyIndex < commandHistory . Count )
282+ {
283+ inputTextBox . Text = commandHistory [ historyIndex ] ;
284+ inputTextBox . SelectionStart = inputTextBox . Text . Length ;
285+ }
286+ }
287+
288+ public void FocusInput ( )
289+ {
290+ inputTextBox . Focus ( ) ;
291+ }
292+
293+ public void ClearInput ( )
294+ {
295+ inputTextBox . Clear ( ) ;
296+ }
297+
298+ public void SetInputText ( string text )
299+ {
300+ inputTextBox . Text = text ;
301+ inputTextBox . SelectionStart = inputTextBox . Text . Length ;
302+ }
303+
304+ public void ClearHistory ( )
305+ {
306+ commandHistory . Clear ( ) ;
307+ UpdateHistoryDropdown ( ) ;
308+ historyLabel . Text = "Use ↑↓ for command history | Recent commands →" ;
309+ }
310+
311+ public void SetQuickButtonsEnabled ( bool enabled )
312+ {
313+ quickInventoryButton . Enabled = enabled ;
314+ }
315+
316+ public void ApplyTheme ( string themeName )
317+ {
318+ switch ( themeName . ToLower ( ) )
319+ {
320+ case "classic" :
321+ inputTextBox . BackColor = Color . Black ;
322+ inputTextBox . ForeColor = Color . LimeGreen ;
323+ this . BackColor = Color . DarkGreen ;
324+ break ;
325+ case "modern" :
326+ inputTextBox . BackColor = Color . White ;
327+ inputTextBox . ForeColor = Color . Black ;
328+ this . BackColor = Color . LightGray ;
329+ break ;
330+ case "fantasy" :
331+ inputTextBox . BackColor = Color . DarkSlateBlue ;
332+ inputTextBox . ForeColor = Color . Gold ;
333+ this . BackColor = Color . Purple ;
334+ break ;
335+ case "dark" :
336+ inputTextBox . BackColor = Color . DarkGray ;
337+ inputTextBox . ForeColor = Color . White ;
338+ this . BackColor = Color . Black ;
339+ break ;
340+ case "high contrast" :
341+ inputTextBox . BackColor = Color . Black ;
342+ inputTextBox . ForeColor = Color . Yellow ;
343+ this . BackColor = Color . DarkBlue ;
344+ break ;
345+ default :
346+ inputTextBox . BackColor = Color . White ;
347+ inputTextBox . ForeColor = Color . Black ;
348+ this . BackColor = Color . DarkGray ;
349+ break ;
350+ }
351+ }
352+ }
353+ }
0 commit comments