Skip to content

Commit 623132b

Browse files
author
Pavel Kovalenko
committed
Refactor CUIChangeWeather and CUIChangeGameType classes.
1 parent c54d934 commit 623132b

File tree

2 files changed

+167
-150
lines changed

2 files changed

+167
-150
lines changed

src/xrGame/ui/UIChangeWeather.cpp

Lines changed: 113 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -5,151 +5,141 @@
55
#include "../game_cl_teamdeathmatch.h"
66
#include "UIKickPlayer.h"
77
#include "UIChangeMap.h"
8-
//#include "UIMapList.h"
9-
10-
CUIChangeWeather::CUIChangeWeather(){
11-
bkgrnd = xr_new<CUIStatic>();
12-
bkgrnd->SetAutoDelete(true);
13-
AttachChild(bkgrnd);
14-
15-
header = xr_new<CUITextWnd>();
16-
header->SetAutoDelete(true);
17-
AttachChild(header);
18-
19-
btn_cancel = xr_new<CUI3tButton>();
20-
btn_cancel->SetAutoDelete(true);
21-
AttachChild(btn_cancel);
22-
23-
for (int i = 0; i<4; i++)
24-
{
25-
btn[i] = xr_new<CUI3tButton>();
26-
btn[i]->SetAutoDelete(true);
27-
AttachChild(btn[i]);
28-
29-
m_data[i].m_text = xr_new<CUITextWnd>();
30-
m_data[i].m_text->SetAutoDelete(true);
31-
AttachChild(m_data[i].m_text);
32-
}
8+
#include <dinput.h>
9+
#include "xrEngine/XR_IOConsole.h"
10+
#include "UIMapList.h"
11+
#include "../UIGameCustom.h"
3312

34-
weather_counter = 0;
13+
ButtonListDialog::ButtonListDialog()
14+
{
15+
Background = xr_new<CUIStatic>();
16+
Background->SetAutoDelete(true);
17+
AttachChild(Background);
18+
Header = xr_new<CUITextWnd>();
19+
Header->SetAutoDelete(true);
20+
AttachChild(Header);
21+
CancelButton = xr_new<CUI3tButton>();
22+
CancelButton->SetAutoDelete(true);
23+
AttachChild(CancelButton);
3524
}
3625

37-
void CUIChangeWeather::InitChangeWeather(CUIXml& xml_doc)
26+
void ButtonListDialog::Initialize(int buttonCount)
3827
{
39-
CUIXmlInit::InitWindow(xml_doc, "change_weather", 0, this);
40-
41-
CUIXmlInit::InitTextWnd(xml_doc, "change_weather:header", 0, header);
42-
CUIXmlInit::InitStatic(xml_doc, "change_weather:background", 0, bkgrnd);
43-
44-
string256 _path;
45-
for (int i = 0; i<4; i++){
46-
xr_sprintf(_path, "change_weather:btn_%d", i + 1);
47-
CUIXmlInit::Init3tButton(xml_doc, _path, 0, btn[i]);
48-
xr_sprintf(_path, "change_weather:txt_%d", i + 1);
49-
CUIXmlInit::InitTextWnd(xml_doc, _path, 0, m_data[i].m_text);
50-
}
51-
52-
CUIXmlInit::Init3tButton(xml_doc, "change_weather:btn_cancel", 0, btn_cancel);
53-
54-
ParseWeather();
28+
buttons.reserve(buttonCount);
29+
for (int i = 0; i < buttonCount; i++)
30+
{
31+
NamedButton btn;
32+
btn.Button = xr_new<CUI3tButton>();
33+
btn.Button->SetAutoDelete(true);
34+
AttachChild(btn.Button);
35+
btn.Text = xr_new<CUITextWnd>();
36+
btn.Text->SetAutoDelete(true);
37+
AttachChild(btn.Text);
38+
buttons.push_back(btn);
39+
}
5540
}
5641

57-
void CUIChangeWeather::SendMessage(CUIWindow* pWnd, s16 msg, void* pData){
58-
if (BUTTON_CLICKED == msg)
59-
{
60-
if (btn_cancel == pWnd)
61-
OnBtnCancel();
62-
for (int i=0; i<4; i++){
63-
if (btn[i] == pWnd){
64-
OnBtn(i);
65-
return;
66-
}
67-
}
68-
}
69-
42+
void ButtonListDialog::OnCancel()
43+
{
44+
HideDialog();
7045
}
7146

72-
#include <dinput.h>
73-
74-
bool CUIChangeWeather::OnKeyboardAction(int dik, EUIMessages keyboard_action){
75-
CUIDialogWnd::OnKeyboardAction(dik, keyboard_action);
76-
if (WINDOW_KEY_PRESSED == keyboard_action){
77-
if (DIK_ESCAPE == dik){
78-
OnBtnCancel();
79-
return true;
80-
}
81-
if (dik >= DIK_1 && dik <= DIK_4){
82-
OnBtn(dik - DIK_1);
83-
return true;
84-
}
85-
}
86-
87-
return false;
47+
const ButtonListDialog::NamedButton& ButtonListDialog::GetButton(int i) const
48+
{
49+
return buttons[i];
8850
}
8951

90-
#include "../../xrEngine/xr_ioconsole.h"
91-
92-
void CUIChangeWeather::OnBtn(int i)
52+
bool ButtonListDialog::OnKeyboardAction(int dik, EUIMessages keyboardAction)
9353
{
94-
string1024 command;
95-
xr_sprintf (command, "cl_votestart changeweather %s %s", *m_data[i].m_weather_name, *m_data[i].m_weather_time);
96-
Console->Execute (command);
97-
HideDialog ();
54+
CUIDialogWnd::OnKeyboardAction(dik, keyboardAction);
55+
if (WINDOW_KEY_PRESSED == keyboardAction)
56+
{
57+
if (DIK_ESCAPE == dik)
58+
{
59+
OnCancel();
60+
return true;
61+
}
62+
int btnCount = buttons.size();
63+
if (dik >= DIK_1 && dik <= DIK_1-1+btnCount && btnCount <= 9) // handle 1..9 keys only
64+
{
65+
OnButtonClick(dik-DIK_1);
66+
return true;
67+
}
68+
}
69+
return false;
9870
}
9971

100-
void CUIChangeWeather::OnBtnCancel()
72+
void ButtonListDialog::SendMessage(CUIWindow* wnd, s16 msg, void* data /*= nullptr */)
10173
{
102-
HideDialog ();
74+
if (BUTTON_CLICKED == msg)
75+
{
76+
if (CancelButton == wnd)
77+
OnCancel();
78+
for (u32 i = 0; i < buttons.size(); i++)
79+
{
80+
if (buttons[i].Button == wnd)
81+
{
82+
OnButtonClick(i);
83+
return;
84+
}
85+
}
86+
}
10387
}
10488

105-
#include "UIMapList.h"
106-
#include "../UIGameCustom.h"
107-
108-
void CUIChangeWeather::ParseWeather()
89+
void CUIChangeWeather::InitChangeWeather(CUIXml& xmlDoc)
10990
{
110-
weather_counter = 0;
111-
112-
GAME_WEATHERS game_weathers = gMapListHelper.GetGameWeathers();
113-
GAME_WEATHERS_CIT it = game_weathers.begin();
114-
GAME_WEATHERS_CIT it_e = game_weathers.end();
115-
116-
for( ;it!=it_e; ++it)
117-
{
118-
AddWeather ( (*it).m_weather_name, (*it).m_start_time);
119-
}
120-
};
121-
122-
void CUIChangeWeather::AddWeather(const shared_str& weather, const shared_str& time){
123-
// m_data[weather_counter].m_text->SetTextST (weather.c_str());
124-
m_data[weather_counter].m_weather_name = weather;
125-
m_data[weather_counter].m_weather_time = time;
126-
weather_counter++;
91+
CUIXmlInit::InitWindow(xmlDoc, "change_weather", 0, this);
92+
CUIXmlInit::InitTextWnd(xmlDoc, "change_weather:header", 0, Header);
93+
CUIXmlInit::InitStatic(xmlDoc, "change_weather:background", 0, Background);
94+
CUIXmlInit::Init3tButton(xmlDoc, "change_weather:btn_cancel", 0, CancelButton);
95+
auto& gameWeathers = gMapListHelper.GetGameWeathers();
96+
Initialize(gameWeathers.size());
97+
weatherItems.resize(gameWeathers.size());
98+
string256 path;
99+
for (u32 i = 0; i < weatherItems.size(); i++)
100+
{
101+
xr_sprintf(path, "change_weather:btn_%d", i+1);
102+
CUIXmlInit::Init3tButton(xmlDoc, path, 0, GetButton(i).Button);
103+
xr_sprintf(path, "change_weather:txt_%d", i+1);
104+
CUIXmlInit::InitTextWnd(xmlDoc, path, 0, GetButton(i).Text);
105+
weatherItems[i].Name = gameWeathers[i].m_weather_name;
106+
weatherItems[i].Time = gameWeathers[i].m_start_time;
107+
}
127108
}
128109

129-
void CUIChangeGameType::InitChangeGameType(CUIXml& xml_doc)
110+
void CUIChangeWeather::OnButtonClick(int i)
130111
{
131-
CUIXmlInit::InitWindow(xml_doc, "change_gametype", 0, this);
132-
133-
CUIXmlInit::InitTextWnd(xml_doc, "change_gametype:header", 0, header);
134-
CUIXmlInit::InitStatic(xml_doc, "change_gametype:background", 0, bkgrnd);
135-
136-
string256 _path;
137-
for (int i = 0; i<4; i++)
138-
{
139-
xr_sprintf(_path, "change_gametype:btn_%d", i + 1);
140-
CUIXmlInit::Init3tButton(xml_doc, _path, 0, btn[i]);
141-
xr_sprintf(_path, "change_gametype:txt_%d", i + 1);
142-
CUIXmlInit::InitTextWnd(xml_doc, _path, 0, m_data[i].m_text);
143-
m_data[i].m_weather_name = xml_doc.ReadAttrib(_path,0,"id");
144-
}
112+
string1024 command;
113+
xr_sprintf(command, "cl_votestart changeweather %s %s", *weatherItems[i].Name, *weatherItems[i].Time);
114+
Console->Execute(command);
115+
HideDialog();
116+
}
145117

146-
CUIXmlInit::Init3tButton(xml_doc, "change_gametype:btn_cancel", 0, btn_cancel);
118+
void CUIChangeGameType::InitChangeGameType(CUIXml& xmlDoc)
119+
{
120+
CUIXmlInit::InitWindow(xmlDoc, "change_gametype", 0, this);
121+
CUIXmlInit::InitTextWnd(xmlDoc, "change_gametype:header", 0, Header);
122+
CUIXmlInit::InitStatic(xmlDoc, "change_gametype:background", 0, Background);
123+
CUIXmlInit::Init3tButton(xmlDoc, "change_gametype:btn_cancel", 0, CancelButton);
124+
// XXX nitrocaster: get it from somewhere
125+
const int gameTypeCount = 4;
126+
Initialize(gameTypeCount);
127+
gameTypeItems.resize(gameTypeCount);
128+
string256 path;
129+
for (u32 i = 0; i < gameTypeItems.size(); i++)
130+
{
131+
xr_sprintf(path, "change_gametype:btn_%d", i+1);
132+
CUIXmlInit::Init3tButton(xmlDoc, path, 0, GetButton(i).Button);
133+
xr_sprintf(path, "change_gametype:txt_%d", i+1);
134+
CUIXmlInit::InitTextWnd(xmlDoc, path, 0, GetButton(i).Text);
135+
gameTypeItems[i] = xmlDoc.ReadAttrib(path, 0, "id");
136+
}
147137
}
148138

149-
void CUIChangeGameType::OnBtn(int i)
139+
void CUIChangeGameType::OnButtonClick(int i)
150140
{
151-
string1024 command;
152-
xr_sprintf (command, "cl_votestart changegametype %s", m_data[i].m_weather_name.c_str());
153-
Console->Execute (command);
154-
HideDialog ();
141+
string1024 command;
142+
xr_sprintf(command, "cl_votestart changegametype %s", gameTypeItems[i].c_str());
143+
Console->Execute(command);
144+
HideDialog();
155145
}

src/xrGame/ui/UIChangeWeather.h

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,66 @@ class CUIKickPlayer;
99
class CUIChangeMap;
1010
class CUIXml;
1111

12-
class CUIChangeWeather : public CUIDialogWnd
12+
class ButtonListDialog : public CUIDialogWnd
1313
{
14-
public:
15-
CUIChangeWeather ();
16-
void InitChangeWeather (CUIXml& xml_doc);
14+
protected:
15+
struct NamedButton
16+
{
17+
CUI3tButton* Button;
18+
CUITextWnd* Text;
19+
};
1720

18-
virtual bool OnKeyboardAction (int dik, EUIMessages keyboard_action);
19-
virtual void SendMessage (CUIWindow* pWnd, s16 msg, void* pData = 0);
21+
CUITextWnd* Header;
22+
CUIStatic* Background;
23+
CUI3tButton* CancelButton;
2024

21-
virtual void OnBtn (int i);
22-
void OnBtnCancel ();
25+
private:
26+
xr_vector<NamedButton> buttons;
2327

2428
protected:
25-
void ParseWeather ();
26-
void AddWeather (const shared_str& name, const shared_str& time);
27-
u32 weather_counter;
28-
29-
struct SWeatherData{
30-
CUITextWnd* m_text;
31-
shared_str m_weather_name;
32-
shared_str m_weather_time;
33-
};
34-
35-
CUITextWnd* header;
36-
CUI3tButton* btn[4];
37-
SWeatherData m_data[4];
38-
CUIStatic* bkgrnd;
39-
CUI3tButton* btn_cancel;
29+
ButtonListDialog();
30+
virtual ~ButtonListDialog() {}
31+
void Initialize(int buttonCount);
32+
const NamedButton& GetButton(int i) const;
33+
// CUIDialogWnd
34+
virtual bool OnKeyboardAction(int dik, EUIMessages keyboardAction) override;
35+
// ~CUIDialogWnd
36+
// CUIWindow
37+
virtual void SendMessage(CUIWindow* wnd, s16 msg, void* data = nullptr) override;
38+
// ~CUIWindow
39+
virtual void OnButtonClick(int i) {}
40+
virtual void OnCancel();
4041
};
4142

42-
class CUIChangeGameType :public CUIChangeWeather
43+
class CUIChangeWeather : public ButtonListDialog
44+
{
45+
private:
46+
struct WeatherDesc
47+
{
48+
shared_str Name;
49+
shared_str Time;
50+
};
51+
52+
xr_vector<WeatherDesc> weatherItems;
53+
54+
public:
55+
void InitChangeWeather(CUIXml& xmlDoc);
56+
// ButtonListDialog
57+
virtual void OnButtonClick(int i) override;
58+
// ~ButtonListDialog
59+
60+
private:
61+
void ParseWeather();
62+
};
63+
// XXX nitrocaster: move to separate file
64+
class CUIChangeGameType : public ButtonListDialog
4365
{
66+
private:
67+
xr_vector<shared_str> gameTypeItems;
68+
4469
public:
45-
void InitChangeGameType (CUIXml& xml_doc);
46-
virtual void OnBtn (int i);
47-
};
70+
void InitChangeGameType(CUIXml& xmlDoc);
71+
// ButtonListDialog
72+
virtual void OnButtonClick(int i) override;
73+
// ~ButtonListDialog
74+
};

0 commit comments

Comments
 (0)