1//**************************************************************************
2//**
3//**    ##   ##    ##    ##   ##   ####     ####   ###     ###
4//**    ##   ##  ##  ##  ##   ##  ##  ##   ##  ##  ####   ####
5//**     ## ##  ##    ##  ## ##  ##    ## ##    ## ## ## ## ##
6//**     ## ##  ########  ## ##  ##    ## ##    ## ##  ###  ##
7//**      ###   ##    ##   ###    ##  ##   ##  ##  ##       ##
8//**       #    ##    ##    #      ####     ####   ##       ##
9//**
10//**    $Id: MenuScreen.vc 4333 2010-09-17 12:59:40Z firebrand_kh $
11//**
12//**    Copyright (C) 1999-2006 Jānis Legzdiņš
13//**
14//**    This program is free software; you can redistribute it and/or
15//**  modify it under the terms of the GNU General Public License
16//**  as published by the Free Software Foundation; either version 2
17//**  of the License, or (at your option) any later version.
18//**
19//**    This program is distributed in the hope that it will be useful,
20//**  but WITHOUT ANY WARRANTY; without even the implied warranty of
21//**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22//**  GNU General Public License for more details.
23//**
24//**************************************************************************
25
26class MenuScreen : Widget;
27
28const int MAX_MENU_ITEMS		= 32;
29
30ClientGameShared		ClGame;
31
32MenuChoice				Items[MAX_MENU_ITEMS];
33int						NumItems;
34int						CursorPos;
35int						CursorPrev;
36
37int						ChoicesStartX;
38int						ChoicesStartY;
39
40class<Widget>			SelectorType;
41Widget					Selector;
42
43string					Title;
44int						TitleX;
45int						TitleY;
46
47//==========================================================================
48//
49//	CreateMenu
50//
51//==========================================================================
52
53void CreateMenu()
54{
55	CreateTitle();
56	DrawBackButton();
57	CreateChoices();
58	CreateSelector();
59	SetDefaultChoice();
60}
61
62//==========================================================================
63//
64//	OnChildAdded
65//
66//==========================================================================
67
68void OnChildAdded(Widget Child)
69{
70	if (MenuChoice(Child) && !MenuChoice_BackButton(Child))
71	{
72		if (NumItems >= MAX_MENU_ITEMS)
73		{
74			FatalError("Too many menu items");
75		}
76		Items[NumItems++] = MenuChoice(Child);
77	}
78	::OnChildAdded(Child);
79}
80
81//==========================================================================
82//
83//	CreateTitle
84//
85//==========================================================================
86
87void CreateTitle()
88{
89	MenuTitleText newTitle;
90
91	if (Title)
92	{
93		newTitle = MenuTitleText(NewChild(MenuTitleText));
94		newTitle.Text = Title;
95		newTitle.SetOrigin(TitleX, TitleY);
96	}
97}
98
99//==========================================================================
100//
101//	DrawBackButton
102//
103//==========================================================================
104
105void DrawBackButton()
106{
107	//  Draw the back button on the top left corner of the menu screen
108	MenuChoice_BackButton	BackButton;
109
110	BackButton =  MenuChoice_BackButton(NewChild(MenuChoice_BackButton));
111	BackButton.SetOrigin(Width - 20, 1);
112}
113
114//==========================================================================
115//
116//	CreateChoices
117//
118//==========================================================================
119
120void CreateChoices()
121{
122}
123
124//==========================================================================
125//
126//	CreateSelector
127//
128//==========================================================================
129
130void CreateSelector()
131{
132	if (SelectorType)
133	{
134		Selector = NewChild(SelectorType);
135	}
136}
137
138//==========================================================================
139//
140//	CursorMoved
141//
142//==========================================================================
143
144void CursorMoved()
145{
146	if (CursorPrev == CursorPos || MenuChoice_BackButton(Items[CursorPos]))
147		return;
148
149	if (CursorPos >= 0 && CursorPos < NumItems)
150	{
151		SetCurrentFocusChild(Items[CursorPos]);
152	}
153
154	if (Selector)
155	{
156		Selector.SetOrigin(Items[CursorPos].X + Items[CursorPos].CursorXOffs,
157			Items[CursorPos].Y + Items[CursorPos].CursorYOffs);
158	}
159}
160
161//==========================================================================
162//
163//	SetDefaultChoice
164//
165//==========================================================================
166
167void SetDefaultChoice()
168{
169	int i;
170
171	// default cursor position
172	CursorPos = 0;
173	CursorPrev = 0;
174
175	// force first available item to have focus
176	for (i = 0; i < NumItems; i++)
177	{
178		if (Items[i].Focusable && Items[i].Enabled && !MenuChoice_BackButton(Items[i]))
179		{
180			CursorPrev = -1;
181			CursorPos = i;
182			CursorMoved();
183			break;
184		}
185	}
186}
187
188//==========================================================================
189//
190//	CyclePrevChoice
191//
192//==========================================================================
193
194void CyclePrevChoice()
195{
196	CursorPrev = CursorPos;
197	do
198	{
199		if (!CursorPos)
200			CursorPos = NumItems - 1;
201		else
202			CursorPos--;
203	}
204	while ((!Items[CursorPos].Focusable || !Items[CursorPos].Enabled) &&
205		!MenuChoice_BackButton(Items[CursorPos]) && CursorPrev != CursorPos);
206	CursorMoved();
207}
208
209//==========================================================================
210//
211//	CycleNextChoice
212//
213//==========================================================================
214
215void CycleNextChoice()
216{
217	CursorPrev = CursorPos;
218	do
219	{
220		if (CursorPos == NumItems - 1)
221			CursorPos = 0;
222		else
223			CursorPos++;
224	}
225	while ((!Items[CursorPos].Focusable || !Items[CursorPos].Enabled) &&
226		!MenuChoice_BackButton(Items[CursorPos]) && CursorPrev != CursorPos);
227	CursorMoved();
228}
229
230//==========================================================================
231//
232//	OnKeyDown
233//
234//==========================================================================
235
236bool OnKeyDown(int key)
237{
238	switch (key)
239	{
240	case K_DOWNARROW:
241	case K_MWHEELDOWN:
242		CycleNextChoice();
243		LocalSound('menu/cursor');
244		return true;
245
246	case K_UPARROW:
247	case K_MWHEELUP:
248		CyclePrevChoice();
249		LocalSound('menu/cursor');
250		return true;
251
252	case K_ESCAPE:
253	case K_MOUSE2:
254		ClGame.PopMenu();
255		return true;
256	}
257
258	return false;
259}
260
261//==========================================================================
262//
263//	OnDraw
264//
265//  Fade all the screen buffer, so that the menu is more readable,
266// especially now that we use the small hudfont in the menus...
267//
268//==========================================================================
269
270void OnDraw()
271{
272	ShadeRect(0, 0, 640, 480, GetCvarF('menu_darkening'));
273}
274
275defaultproperties
276{
277	X = 160;
278	Y = 140;
279	Width = 320;
280	Height = 200;
281	TitleX = 160;
282	TitleY = 24;
283	Focusable = true;
284}
285