openclonk/src/gui/C4GameOptions.cpp

282 lines
9.6 KiB
C++
Raw Normal View History

2009-05-08 13:28:41 +00:00
/*
* OpenClonk, http://www.openclonk.org
*
* Copyright (c) 2005-2006, 2008 Sven Eberhardt
* Copyright (c) 2006 Florian Groß
* Copyright (c) 2009 Peter Wortmann
2009-05-08 13:28:41 +00:00
* Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de
*
* Portions might be copyrighted by other authors who have contributed
* to OpenClonk.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
* See isc_license.txt for full license and disclaimer.
*
* "Clonk" is a registered trademark of Matthes Bender.
* See clonk_trademark_license.txt for full license.
*/
// Custom game options and configuration dialog
#include "C4Include.h"
#include "C4GameOptions.h"
#include <C4Game.h>
2009-06-15 22:06:37 +00:00
#include <C4GameControl.h>
2009-05-08 13:28:41 +00:00
// ----------- C4GameOptionsList::Option ----------------------------------------------------------------
C4GameOptionsList::Option::Option(C4GameOptionsList *pForDlg)
2010-03-28 18:58:01 +00:00
: BaseClass(C4Rect(0,0,0,0)), pPrimarySubcomponent(NULL) { }
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::Option::InitOption(C4GameOptionsList *pForDlg)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// post-call after initialization: Adds to list box and does initial update
// add to listbox (will eventually get moved)
pForDlg->AddElement(this);
// first-time update
Update();
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// ----------- C4GameOptionsList::OptionDropdown ----------------------------------------------------------------
C4GameOptionsList::OptionDropdown::OptionDropdown(class C4GameOptionsList *pForDlg, const char *szCaption, bool fReadOnly)
2010-03-28 18:58:01 +00:00
: Option(pForDlg)
{
2009-05-08 13:28:41 +00:00
CStdFont &rUseFont = C4GUI::GetRes()->TextFont;
// get size of caption label
bool fTabular = pForDlg->IsTabular();
int32_t iCaptWidth, iCaptHeight;
if (fTabular)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// tabular layout: Caption label width by largest caption
rUseFont.GetTextExtent(LoadResStr("IDS_NET_RUNTIMEJOIN"), iCaptWidth, iCaptHeight, true);
iCaptWidth = iCaptWidth * 5 / 4;
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
else
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
rUseFont.GetTextExtent(szCaption, iCaptWidth, iCaptHeight, true);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// calc total height for component
int iHorizontalMargin = 1;
int iVerticalMargin = 1;
int iComboMargin = 5;
int iSelComboHgt = C4GUI::ComboBox::GetDefaultHeight();
SetBounds(C4Rect(0, 0, pForDlg->GetItemWidth(), (!fTabular) * (iCaptHeight + iVerticalMargin*2) + iVerticalMargin*2 + iSelComboHgt));
C4GUI::ComponentAligner ca(GetContainedClientRect(), iHorizontalMargin, iVerticalMargin);
// create subcomponents
AddElement(pCaption = new C4GUI::Label(FormatString("%s:", szCaption).getData(), fTabular ? ca.GetFromLeft(iCaptWidth, iCaptHeight) : ca.GetFromTop(iCaptHeight), ALeft));
ca.ExpandLeft(-iComboMargin);
AddElement(pPrimarySubcomponent = pDropdownList = new C4GUI::ComboBox(ca.GetAll()));
pDropdownList->SetReadOnly(fReadOnly);
pDropdownList->SetComboCB(new C4GUI::ComboBox_FillCallback<C4GameOptionsList::OptionDropdown>(this, &C4GameOptionsList::OptionDropdown::OnDropdownFill, &C4GameOptionsList::OptionDropdown::OnDropdownSelChange));
// final init
InitOption(pForDlg);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// ----------- C4GameOptionsList::OptionControlMode ----------------------------------------------------------------
// Unfortunately, the control mode cannot be changed in the lobby
C4GameOptionsList::OptionControlMode::OptionControlMode(class C4GameOptionsList *pForDlg)
2010-03-28 18:58:01 +00:00
: C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_TEXT_CONTROLMODE"), !::Control.isCtrlHost() || !::Control.isNetwork() || !::Control.Network.IsEnabled() || !pForDlg->IsRuntime())
{
2009-05-08 13:28:41 +00:00
SetToolTip(LoadResStr("IDS_DESC_CHANGESTHEWAYCONTROLDATAI"));
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionControlMode::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// change possible?
2009-06-15 22:06:37 +00:00
if (!::Control.isNetwork() || !::Control.Network.IsEnabled() || !::Control.isCtrlHost()) return;
2009-05-08 13:28:41 +00:00
// add possible modes
pFiller->AddEntry(LoadResStr("IDS_NET_CTRLMODE_CENTRAL"), CNM_Central);
pFiller->AddEntry(LoadResStr("IDS_NET_CTRLMODE_DECENTRAL"), CNM_Decentral);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionControlMode::DoDropdownSelChange(int32_t idNewSelection)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// change possible?
2009-06-15 22:06:37 +00:00
if (!::Control.isNetwork() || !::Control.Network.IsEnabled() || !::Control.isCtrlHost()) return;
2009-05-08 13:28:41 +00:00
// perform it
2009-06-05 15:19:46 +00:00
::Network.SetCtrlMode(idNewSelection);
2009-05-08 13:28:41 +00:00
// update done in parent call
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionControlMode::Update()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
const char *szControlMode;
2009-06-15 22:06:37 +00:00
if (!::Control.isNetwork() || !::Control.Network.IsEnabled())
2009-05-08 13:28:41 +00:00
szControlMode = LoadResStr("IDS_NET_NONET");
else
2010-03-28 18:58:01 +00:00
{
2009-06-15 22:06:37 +00:00
switch (::Control.Network.GetCtrlMode())
2010-03-28 18:58:01 +00:00
{
case CNM_Central: szControlMode = LoadResStr("IDS_NET_CTRLMODE_CENTRAL"); break;
case CNM_Decentral: szControlMode = LoadResStr("IDS_NET_CTRLMODE_DECENTRAL"); break;
default: szControlMode = LoadResStr("IDS_NET_CTRLMODE_NONE"); break;
2009-05-08 13:28:41 +00:00
}
}
2010-03-28 18:58:01 +00:00
pDropdownList->SetText(szControlMode);
}
2009-05-08 13:28:41 +00:00
// ----------- C4GameOptionsList::OptionControlRate ----------------------------------------------------------------
C4GameOptionsList::OptionControlRate::OptionControlRate(class C4GameOptionsList *pForDlg)
2010-03-28 18:58:01 +00:00
: C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_CTL_CONTROLRATE"), !::Control.isCtrlHost())
{
2009-05-08 13:28:41 +00:00
SetToolTip(LoadResStr("IDS_CTL_CONTROLRATE_DESC"));
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionControlRate::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
for (int i = 1; i < Min(C4MaxControlRate, 10); ++i)
pFiller->AddEntry(FormatString("%d", i).getData(), i);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionControlRate::DoDropdownSelChange(int32_t idNewSelection)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// adjust rate
int32_t iNewRate = idNewSelection;
2009-06-15 22:06:37 +00:00
if (!iNewRate || iNewRate == ::Control.ControlRate) return;
::Control.AdjustControlRate(iNewRate - ::Control.ControlRate);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionControlRate::Update()
2010-03-28 18:58:01 +00:00
{
2009-06-15 22:06:37 +00:00
if (atoi(pDropdownList->GetText().getData()) == ::Control.ControlRate) return;
pDropdownList->SetText(FormatString("%d", ::Control.ControlRate).getData());
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// ----------- C4GameOptionsList::OptionRuntimeJoin ----------------------------------------------------------------
C4GameOptionsList::OptionRuntimeJoin::OptionRuntimeJoin(class C4GameOptionsList *pForDlg)
2010-03-28 18:58:01 +00:00
: C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_NET_RUNTIMEJOIN"), !::Network.isHost())
{
2009-05-08 13:28:41 +00:00
SetToolTip(LoadResStr("IDS_NET_RUNTIMEJOIN_DESC"));
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionRuntimeJoin::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
pFiller->AddEntry(LoadResStr("IDS_NET_RUNTIMEJOINBARRED"), 0);
pFiller->AddEntry(LoadResStr("IDS_NET_RUNTIMEJOINFREE"), 1);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionRuntimeJoin::DoDropdownSelChange(int32_t idNewSelection)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// adjust mode
bool fAllowed = !!idNewSelection;
Config.Network.NoRuntimeJoin = !fAllowed;
2009-06-05 15:19:46 +00:00
if (Game.IsRunning) ::Network.AllowJoin(fAllowed);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionRuntimeJoin::Update()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
const char *szText;
if (Config.Network.NoRuntimeJoin)
szText = LoadResStr("IDS_NET_RUNTIMEJOINBARRED");
else
szText = LoadResStr("IDS_NET_RUNTIMEJOINFREE");
pDropdownList->SetText(szText);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// ----------- C4GameOptionsList::OptionTeamDist ----------------------------------------------------------------
C4GameOptionsList::OptionTeamDist::OptionTeamDist(class C4GameOptionsList *pForDlg)
2010-03-28 18:58:01 +00:00
: C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_MSG_TEAMDIST"), !::Control.isCtrlHost())
{
2009-05-08 13:28:41 +00:00
SetToolTip(LoadResStr("IDS_MSG_TEAMDIST_DESC"));
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionTeamDist::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
Game.Teams.FillTeamDistOptions(pFiller);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionTeamDist::DoDropdownSelChange(int32_t idNewSelection)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// adjust team distribution
Game.Teams.SendSetTeamDist(C4TeamList::TeamDist(idNewSelection));
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionTeamDist::Update()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
StdStrBuf sOption; sOption.Take(Game.Teams.GetTeamDistString());
pDropdownList->SetText(sOption.getData());
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// ----------- C4GameOptionsList::OptionTeamColors ----------------------------------------------------------------
C4GameOptionsList::OptionTeamColors::OptionTeamColors(class C4GameOptionsList *pForDlg)
2010-03-28 18:58:01 +00:00
: C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_MSG_TEAMCOLORS"), !::Control.isCtrlHost())
{
2009-05-08 13:28:41 +00:00
SetToolTip(LoadResStr("IDS_MSG_TEAMCOLORS_DESC"));
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionTeamColors::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
pFiller->AddEntry(LoadResStr("IDS_MSG_ENABLED"), 1);
pFiller->AddEntry(LoadResStr("IDS_MSG_DISABLED"), 0);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionTeamColors::DoDropdownSelChange(int32_t idNewSelection)
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
bool fEnabled = !!idNewSelection;
Game.Teams.SendSetTeamColors(fEnabled);
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::OptionTeamColors::Update()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
pDropdownList->SetText(Game.Teams.IsTeamColors() ? LoadResStr("IDS_MSG_ENABLED") : LoadResStr("IDS_MSG_DISABLED"));
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
// ----------- C4GameOptionsList -----------------------------------------------------------------------
C4GameOptionsList::C4GameOptionsList(const C4Rect &rcBounds, bool fActive, bool fRuntime)
2010-03-28 18:58:01 +00:00
: C4GUI::ListBox(rcBounds), fRuntime(fRuntime)
{
2009-05-08 13:28:41 +00:00
// initial option fill
InitOptions();
if (fActive) Activate();
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::InitOptions()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// creates option selection components
new OptionControlMode(this);
new OptionControlRate(this);
2009-06-05 15:19:46 +00:00
if (::Network.isHost()) new OptionRuntimeJoin(this);
2009-05-08 13:28:41 +00:00
if (!IsRuntime())
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
if (Game.Teams.HasTeamDistOptions()) new OptionTeamDist(this);
if (Game.Teams.IsMultiTeams()) new OptionTeamColors(this);
}
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::Update()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// update all option items
2010-03-28 18:58:01 +00:00
for (Option *pItem = static_cast<Option *>(pClientWindow->GetFirst()); pItem; pItem = pItem->GetNext())
2009-05-08 13:28:41 +00:00
pItem->Update();
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::Activate()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// register timer if necessary
Application.Add(this);
// force an update
Update();
2010-03-28 18:58:01 +00:00
}
2009-05-08 13:28:41 +00:00
void C4GameOptionsList::Deactivate()
2010-03-28 18:58:01 +00:00
{
2009-05-08 13:28:41 +00:00
// release timer if set
Application.Remove(this);
2010-03-28 18:58:01 +00:00
}