Skip to content

Commit

Permalink
Refactor CUIChangeWeather and CUIChangeGameType classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Kovalenko committed Oct 3, 2014
1 parent c54d934 commit 623132b
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 150 deletions.
236 changes: 113 additions & 123 deletions src/xrGame/ui/UIChangeWeather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,151 +5,141 @@
#include "../game_cl_teamdeathmatch.h"
#include "UIKickPlayer.h"
#include "UIChangeMap.h"
//#include "UIMapList.h"

CUIChangeWeather::CUIChangeWeather(){
bkgrnd = xr_new<CUIStatic>();
bkgrnd->SetAutoDelete(true);
AttachChild(bkgrnd);

header = xr_new<CUITextWnd>();
header->SetAutoDelete(true);
AttachChild(header);

btn_cancel = xr_new<CUI3tButton>();
btn_cancel->SetAutoDelete(true);
AttachChild(btn_cancel);

for (int i = 0; i<4; i++)
{
btn[i] = xr_new<CUI3tButton>();
btn[i]->SetAutoDelete(true);
AttachChild(btn[i]);

m_data[i].m_text = xr_new<CUITextWnd>();
m_data[i].m_text->SetAutoDelete(true);
AttachChild(m_data[i].m_text);
}
#include <dinput.h>
#include "xrEngine/XR_IOConsole.h"
#include "UIMapList.h"
#include "../UIGameCustom.h"

weather_counter = 0;
ButtonListDialog::ButtonListDialog()
{
Background = xr_new<CUIStatic>();
Background->SetAutoDelete(true);
AttachChild(Background);
Header = xr_new<CUITextWnd>();
Header->SetAutoDelete(true);
AttachChild(Header);
CancelButton = xr_new<CUI3tButton>();
CancelButton->SetAutoDelete(true);
AttachChild(CancelButton);
}

void CUIChangeWeather::InitChangeWeather(CUIXml& xml_doc)
void ButtonListDialog::Initialize(int buttonCount)
{
CUIXmlInit::InitWindow(xml_doc, "change_weather", 0, this);

CUIXmlInit::InitTextWnd(xml_doc, "change_weather:header", 0, header);
CUIXmlInit::InitStatic(xml_doc, "change_weather:background", 0, bkgrnd);

string256 _path;
for (int i = 0; i<4; i++){
xr_sprintf(_path, "change_weather:btn_%d", i + 1);
CUIXmlInit::Init3tButton(xml_doc, _path, 0, btn[i]);
xr_sprintf(_path, "change_weather:txt_%d", i + 1);
CUIXmlInit::InitTextWnd(xml_doc, _path, 0, m_data[i].m_text);
}

CUIXmlInit::Init3tButton(xml_doc, "change_weather:btn_cancel", 0, btn_cancel);

ParseWeather();
buttons.reserve(buttonCount);
for (int i = 0; i < buttonCount; i++)
{
NamedButton btn;
btn.Button = xr_new<CUI3tButton>();
btn.Button->SetAutoDelete(true);
AttachChild(btn.Button);
btn.Text = xr_new<CUITextWnd>();
btn.Text->SetAutoDelete(true);
AttachChild(btn.Text);
buttons.push_back(btn);
}
}

void CUIChangeWeather::SendMessage(CUIWindow* pWnd, s16 msg, void* pData){
if (BUTTON_CLICKED == msg)
{
if (btn_cancel == pWnd)
OnBtnCancel();
for (int i=0; i<4; i++){
if (btn[i] == pWnd){
OnBtn(i);
return;
}
}
}

void ButtonListDialog::OnCancel()
{
HideDialog();
}

#include <dinput.h>

bool CUIChangeWeather::OnKeyboardAction(int dik, EUIMessages keyboard_action){
CUIDialogWnd::OnKeyboardAction(dik, keyboard_action);
if (WINDOW_KEY_PRESSED == keyboard_action){
if (DIK_ESCAPE == dik){
OnBtnCancel();
return true;
}
if (dik >= DIK_1 && dik <= DIK_4){
OnBtn(dik - DIK_1);
return true;
}
}

return false;
const ButtonListDialog::NamedButton& ButtonListDialog::GetButton(int i) const
{
return buttons[i];
}

#include "../../xrEngine/xr_ioconsole.h"

void CUIChangeWeather::OnBtn(int i)
bool ButtonListDialog::OnKeyboardAction(int dik, EUIMessages keyboardAction)
{
string1024 command;
xr_sprintf (command, "cl_votestart changeweather %s %s", *m_data[i].m_weather_name, *m_data[i].m_weather_time);
Console->Execute (command);
HideDialog ();
CUIDialogWnd::OnKeyboardAction(dik, keyboardAction);
if (WINDOW_KEY_PRESSED == keyboardAction)
{
if (DIK_ESCAPE == dik)
{
OnCancel();
return true;
}
int btnCount = buttons.size();
if (dik >= DIK_1 && dik <= DIK_1-1+btnCount && btnCount <= 9) // handle 1..9 keys only
{
OnButtonClick(dik-DIK_1);
return true;
}
}
return false;
}

void CUIChangeWeather::OnBtnCancel()
void ButtonListDialog::SendMessage(CUIWindow* wnd, s16 msg, void* data /*= nullptr */)
{
HideDialog ();
if (BUTTON_CLICKED == msg)
{
if (CancelButton == wnd)
OnCancel();
for (u32 i = 0; i < buttons.size(); i++)
{
if (buttons[i].Button == wnd)
{
OnButtonClick(i);
return;
}
}
}
}

#include "UIMapList.h"
#include "../UIGameCustom.h"

void CUIChangeWeather::ParseWeather()
void CUIChangeWeather::InitChangeWeather(CUIXml& xmlDoc)
{
weather_counter = 0;

GAME_WEATHERS game_weathers = gMapListHelper.GetGameWeathers();
GAME_WEATHERS_CIT it = game_weathers.begin();
GAME_WEATHERS_CIT it_e = game_weathers.end();
for( ;it!=it_e; ++it)
{
AddWeather ( (*it).m_weather_name, (*it).m_start_time);
}
};

void CUIChangeWeather::AddWeather(const shared_str& weather, const shared_str& time){
// m_data[weather_counter].m_text->SetTextST (weather.c_str());
m_data[weather_counter].m_weather_name = weather;
m_data[weather_counter].m_weather_time = time;
weather_counter++;
CUIXmlInit::InitWindow(xmlDoc, "change_weather", 0, this);
CUIXmlInit::InitTextWnd(xmlDoc, "change_weather:header", 0, Header);
CUIXmlInit::InitStatic(xmlDoc, "change_weather:background", 0, Background);
CUIXmlInit::Init3tButton(xmlDoc, "change_weather:btn_cancel", 0, CancelButton);
auto& gameWeathers = gMapListHelper.GetGameWeathers();
Initialize(gameWeathers.size());
weatherItems.resize(gameWeathers.size());
string256 path;
for (u32 i = 0; i < weatherItems.size(); i++)
{
xr_sprintf(path, "change_weather:btn_%d", i+1);
CUIXmlInit::Init3tButton(xmlDoc, path, 0, GetButton(i).Button);
xr_sprintf(path, "change_weather:txt_%d", i+1);
CUIXmlInit::InitTextWnd(xmlDoc, path, 0, GetButton(i).Text);
weatherItems[i].Name = gameWeathers[i].m_weather_name;
weatherItems[i].Time = gameWeathers[i].m_start_time;
}
}

void CUIChangeGameType::InitChangeGameType(CUIXml& xml_doc)
void CUIChangeWeather::OnButtonClick(int i)
{
CUIXmlInit::InitWindow(xml_doc, "change_gametype", 0, this);

CUIXmlInit::InitTextWnd(xml_doc, "change_gametype:header", 0, header);
CUIXmlInit::InitStatic(xml_doc, "change_gametype:background", 0, bkgrnd);

string256 _path;
for (int i = 0; i<4; i++)
{
xr_sprintf(_path, "change_gametype:btn_%d", i + 1);
CUIXmlInit::Init3tButton(xml_doc, _path, 0, btn[i]);
xr_sprintf(_path, "change_gametype:txt_%d", i + 1);
CUIXmlInit::InitTextWnd(xml_doc, _path, 0, m_data[i].m_text);
m_data[i].m_weather_name = xml_doc.ReadAttrib(_path,0,"id");
}
string1024 command;
xr_sprintf(command, "cl_votestart changeweather %s %s", *weatherItems[i].Name, *weatherItems[i].Time);
Console->Execute(command);
HideDialog();
}

CUIXmlInit::Init3tButton(xml_doc, "change_gametype:btn_cancel", 0, btn_cancel);
void CUIChangeGameType::InitChangeGameType(CUIXml& xmlDoc)
{
CUIXmlInit::InitWindow(xmlDoc, "change_gametype", 0, this);
CUIXmlInit::InitTextWnd(xmlDoc, "change_gametype:header", 0, Header);
CUIXmlInit::InitStatic(xmlDoc, "change_gametype:background", 0, Background);
CUIXmlInit::Init3tButton(xmlDoc, "change_gametype:btn_cancel", 0, CancelButton);
// XXX nitrocaster: get it from somewhere
const int gameTypeCount = 4;
Initialize(gameTypeCount);
gameTypeItems.resize(gameTypeCount);
string256 path;
for (u32 i = 0; i < gameTypeItems.size(); i++)
{
xr_sprintf(path, "change_gametype:btn_%d", i+1);
CUIXmlInit::Init3tButton(xmlDoc, path, 0, GetButton(i).Button);
xr_sprintf(path, "change_gametype:txt_%d", i+1);
CUIXmlInit::InitTextWnd(xmlDoc, path, 0, GetButton(i).Text);
gameTypeItems[i] = xmlDoc.ReadAttrib(path, 0, "id");
}
}

void CUIChangeGameType::OnBtn(int i)
void CUIChangeGameType::OnButtonClick(int i)
{
string1024 command;
xr_sprintf (command, "cl_votestart changegametype %s", m_data[i].m_weather_name.c_str());
Console->Execute (command);
HideDialog ();
string1024 command;
xr_sprintf(command, "cl_votestart changegametype %s", gameTypeItems[i].c_str());
Console->Execute(command);
HideDialog();
}
81 changes: 54 additions & 27 deletions src/xrGame/ui/UIChangeWeather.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,66 @@ class CUIKickPlayer;
class CUIChangeMap;
class CUIXml;

class CUIChangeWeather : public CUIDialogWnd
class ButtonListDialog : public CUIDialogWnd
{
public:
CUIChangeWeather ();
void InitChangeWeather (CUIXml& xml_doc);
protected:
struct NamedButton
{
CUI3tButton* Button;
CUITextWnd* Text;
};

virtual bool OnKeyboardAction (int dik, EUIMessages keyboard_action);
virtual void SendMessage (CUIWindow* pWnd, s16 msg, void* pData = 0);
CUITextWnd* Header;
CUIStatic* Background;
CUI3tButton* CancelButton;

virtual void OnBtn (int i);
void OnBtnCancel ();
private:
xr_vector<NamedButton> buttons;

protected:
void ParseWeather ();
void AddWeather (const shared_str& name, const shared_str& time);
u32 weather_counter;

struct SWeatherData{
CUITextWnd* m_text;
shared_str m_weather_name;
shared_str m_weather_time;
};

CUITextWnd* header;
CUI3tButton* btn[4];
SWeatherData m_data[4];
CUIStatic* bkgrnd;
CUI3tButton* btn_cancel;
ButtonListDialog();
virtual ~ButtonListDialog() {}
void Initialize(int buttonCount);
const NamedButton& GetButton(int i) const;
// CUIDialogWnd
virtual bool OnKeyboardAction(int dik, EUIMessages keyboardAction) override;
// ~CUIDialogWnd
// CUIWindow
virtual void SendMessage(CUIWindow* wnd, s16 msg, void* data = nullptr) override;
// ~CUIWindow
virtual void OnButtonClick(int i) {}
virtual void OnCancel();
};

class CUIChangeGameType :public CUIChangeWeather
class CUIChangeWeather : public ButtonListDialog
{
private:
struct WeatherDesc
{
shared_str Name;
shared_str Time;
};

xr_vector<WeatherDesc> weatherItems;

public:
void InitChangeWeather(CUIXml& xmlDoc);
// ButtonListDialog
virtual void OnButtonClick(int i) override;
// ~ButtonListDialog

private:
void ParseWeather();
};
// XXX nitrocaster: move to separate file
class CUIChangeGameType : public ButtonListDialog
{
private:
xr_vector<shared_str> gameTypeItems;

public:
void InitChangeGameType (CUIXml& xml_doc);
virtual void OnBtn (int i);
};
void InitChangeGameType(CUIXml& xmlDoc);
// ButtonListDialog
virtual void OnButtonClick(int i) override;
// ~ButtonListDialog
};

0 comments on commit 623132b

Please sign in to comment.