/src/gui/C4GameOptions.cpp

https://bitbucket.org/randrian/openclonk2 · C++ · 281 lines · 185 code · 45 blank · 51 comment · 27 complexity · 5f1c03b82da26d69f6f10eb640a1d9b0 MD5 · raw file

  1. /*
  2. * OpenClonk, http://www.openclonk.org
  3. *
  4. * Copyright (c) 2005-2006, 2008 Sven Eberhardt
  5. * Copyright (c) 2006 Florian Gro?&#x;
  6. * Copyright (c) 2009 Peter Wortmann
  7. * Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de
  8. *
  9. * Portions might be copyrighted by other authors who have contributed
  10. * to OpenClonk.
  11. *
  12. * Permission to use, copy, modify, and/or distribute this software for any
  13. * purpose with or without fee is hereby granted, provided that the above
  14. * copyright notice and this permission notice appear in all copies.
  15. * See isc_license.txt for full license and disclaimer.
  16. *
  17. * "Clonk" is a registered trademark of Matthes Bender.
  18. * See clonk_trademark_license.txt for full license.
  19. */
  20. // Custom game options and configuration dialog
  21. #include "C4Include.h"
  22. #include "C4GameOptions.h"
  23. #include <C4Game.h>
  24. #include <C4GameControl.h>
  25. // ----------- C4GameOptionsList::Option ----------------------------------------------------------------
  26. C4GameOptionsList::Option::Option(C4GameOptionsList *pForDlg)
  27. : BaseClass(C4Rect(0,0,0,0)), pPrimarySubcomponent(NULL) { }
  28. void C4GameOptionsList::Option::InitOption(C4GameOptionsList *pForDlg)
  29. {
  30. // post-call after initialization: Adds to list box and does initial update
  31. // add to listbox (will eventually get moved)
  32. pForDlg->AddElement(this);
  33. // first-time update
  34. Update();
  35. }
  36. // ----------- C4GameOptionsList::OptionDropdown ----------------------------------------------------------------
  37. C4GameOptionsList::OptionDropdown::OptionDropdown(class C4GameOptionsList *pForDlg, const char *szCaption, bool fReadOnly)
  38. : Option(pForDlg)
  39. {
  40. CStdFont &rUseFont = C4GUI::GetRes()->TextFont;
  41. // get size of caption label
  42. bool fTabular = pForDlg->IsTabular();
  43. int32_t iCaptWidth, iCaptHeight;
  44. if (fTabular)
  45. {
  46. // tabular layout: Caption label width by largest caption
  47. rUseFont.GetTextExtent(LoadResStr("IDS_NET_RUNTIMEJOIN"), iCaptWidth, iCaptHeight, true);
  48. iCaptWidth = iCaptWidth * 5 / 4;
  49. }
  50. else
  51. {
  52. rUseFont.GetTextExtent(szCaption, iCaptWidth, iCaptHeight, true);
  53. }
  54. // calc total height for component
  55. int iHorizontalMargin = 1;
  56. int iVerticalMargin = 1;
  57. int iComboMargin = 5;
  58. int iSelComboHgt = C4GUI::ComboBox::GetDefaultHeight();
  59. SetBounds(C4Rect(0, 0, pForDlg->GetItemWidth(), (!fTabular) * (iCaptHeight + iVerticalMargin*2) + iVerticalMargin*2 + iSelComboHgt));
  60. C4GUI::ComponentAligner ca(GetContainedClientRect(), iHorizontalMargin, iVerticalMargin);
  61. // create subcomponents
  62. AddElement(pCaption = new C4GUI::Label(FormatString("%s:", szCaption).getData(), fTabular ? ca.GetFromLeft(iCaptWidth, iCaptHeight) : ca.GetFromTop(iCaptHeight), ALeft));
  63. ca.ExpandLeft(-iComboMargin);
  64. AddElement(pPrimarySubcomponent = pDropdownList = new C4GUI::ComboBox(ca.GetAll()));
  65. pDropdownList->SetReadOnly(fReadOnly);
  66. pDropdownList->SetComboCB(new C4GUI::ComboBox_FillCallback<C4GameOptionsList::OptionDropdown>(this, &C4GameOptionsList::OptionDropdown::OnDropdownFill, &C4GameOptionsList::OptionDropdown::OnDropdownSelChange));
  67. // final init
  68. InitOption(pForDlg);
  69. }
  70. // ----------- C4GameOptionsList::OptionControlMode ----------------------------------------------------------------
  71. // Unfortunately, the control mode cannot be changed in the lobby
  72. C4GameOptionsList::OptionControlMode::OptionControlMode(class C4GameOptionsList *pForDlg)
  73. : C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_TEXT_CONTROLMODE"), !::Control.isCtrlHost() || !::Control.isNetwork() || !::Control.Network.IsEnabled() || !pForDlg->IsRuntime())
  74. {
  75. SetToolTip(LoadResStr("IDS_DESC_CHANGESTHEWAYCONTROLDATAI"));
  76. }
  77. void C4GameOptionsList::OptionControlMode::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
  78. {
  79. // change possible?
  80. if (!::Control.isNetwork() || !::Control.Network.IsEnabled() || !::Control.isCtrlHost()) return;
  81. // add possible modes
  82. pFiller->AddEntry(LoadResStr("IDS_NET_CTRLMODE_CENTRAL"), CNM_Central);
  83. pFiller->AddEntry(LoadResStr("IDS_NET_CTRLMODE_DECENTRAL"), CNM_Decentral);
  84. }
  85. void C4GameOptionsList::OptionControlMode::DoDropdownSelChange(int32_t idNewSelection)
  86. {
  87. // change possible?
  88. if (!::Control.isNetwork() || !::Control.Network.IsEnabled() || !::Control.isCtrlHost()) return;
  89. // perform it
  90. ::Network.SetCtrlMode(idNewSelection);
  91. // update done in parent call
  92. }
  93. void C4GameOptionsList::OptionControlMode::Update()
  94. {
  95. const char *szControlMode;
  96. if (!::Control.isNetwork() || !::Control.Network.IsEnabled())
  97. szControlMode = LoadResStr("IDS_NET_NONET");
  98. else
  99. {
  100. switch (::Control.Network.GetCtrlMode())
  101. {
  102. case CNM_Central: szControlMode = LoadResStr("IDS_NET_CTRLMODE_CENTRAL"); break;
  103. case CNM_Decentral: szControlMode = LoadResStr("IDS_NET_CTRLMODE_DECENTRAL"); break;
  104. default: szControlMode = LoadResStr("IDS_NET_CTRLMODE_NONE"); break;
  105. }
  106. }
  107. pDropdownList->SetText(szControlMode);
  108. }
  109. // ----------- C4GameOptionsList::OptionControlRate ----------------------------------------------------------------
  110. C4GameOptionsList::OptionControlRate::OptionControlRate(class C4GameOptionsList *pForDlg)
  111. : C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_CTL_CONTROLRATE"), !::Control.isCtrlHost())
  112. {
  113. SetToolTip(LoadResStr("IDS_CTL_CONTROLRATE_DESC"));
  114. }
  115. void C4GameOptionsList::OptionControlRate::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
  116. {
  117. for (int i = 1; i < Min(C4MaxControlRate, 10); ++i)
  118. pFiller->AddEntry(FormatString("%d", i).getData(), i);
  119. }
  120. void C4GameOptionsList::OptionControlRate::DoDropdownSelChange(int32_t idNewSelection)
  121. {
  122. // adjust rate
  123. int32_t iNewRate = idNewSelection;
  124. if (!iNewRate || iNewRate == ::Control.ControlRate) return;
  125. ::Control.AdjustControlRate(iNewRate - ::Control.ControlRate);
  126. }
  127. void C4GameOptionsList::OptionControlRate::Update()
  128. {
  129. if (atoi(pDropdownList->GetText().getData()) == ::Control.ControlRate) return;
  130. pDropdownList->SetText(FormatString("%d", ::Control.ControlRate).getData());
  131. }
  132. // ----------- C4GameOptionsList::OptionRuntimeJoin ----------------------------------------------------------------
  133. C4GameOptionsList::OptionRuntimeJoin::OptionRuntimeJoin(class C4GameOptionsList *pForDlg)
  134. : C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_NET_RUNTIMEJOIN"), !::Network.isHost())
  135. {
  136. SetToolTip(LoadResStr("IDS_NET_RUNTIMEJOIN_DESC"));
  137. }
  138. void C4GameOptionsList::OptionRuntimeJoin::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
  139. {
  140. pFiller->AddEntry(LoadResStr("IDS_NET_RUNTIMEJOINBARRED"), 0);
  141. pFiller->AddEntry(LoadResStr("IDS_NET_RUNTIMEJOINFREE"), 1);
  142. }
  143. void C4GameOptionsList::OptionRuntimeJoin::DoDropdownSelChange(int32_t idNewSelection)
  144. {
  145. // adjust mode
  146. bool fAllowed = !!idNewSelection;
  147. Config.Network.NoRuntimeJoin = !fAllowed;
  148. if (Game.IsRunning) ::Network.AllowJoin(fAllowed);
  149. }
  150. void C4GameOptionsList::OptionRuntimeJoin::Update()
  151. {
  152. const char *szText;
  153. if (Config.Network.NoRuntimeJoin)
  154. szText = LoadResStr("IDS_NET_RUNTIMEJOINBARRED");
  155. else
  156. szText = LoadResStr("IDS_NET_RUNTIMEJOINFREE");
  157. pDropdownList->SetText(szText);
  158. }
  159. // ----------- C4GameOptionsList::OptionTeamDist ----------------------------------------------------------------
  160. C4GameOptionsList::OptionTeamDist::OptionTeamDist(class C4GameOptionsList *pForDlg)
  161. : C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_MSG_TEAMDIST"), !::Control.isCtrlHost())
  162. {
  163. SetToolTip(LoadResStr("IDS_MSG_TEAMDIST_DESC"));
  164. }
  165. void C4GameOptionsList::OptionTeamDist::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
  166. {
  167. Game.Teams.FillTeamDistOptions(pFiller);
  168. }
  169. void C4GameOptionsList::OptionTeamDist::DoDropdownSelChange(int32_t idNewSelection)
  170. {
  171. // adjust team distribution
  172. Game.Teams.SendSetTeamDist(C4TeamList::TeamDist(idNewSelection));
  173. }
  174. void C4GameOptionsList::OptionTeamDist::Update()
  175. {
  176. StdStrBuf sOption; sOption.Take(Game.Teams.GetTeamDistString());
  177. pDropdownList->SetText(sOption.getData());
  178. }
  179. // ----------- C4GameOptionsList::OptionTeamColors ----------------------------------------------------------------
  180. C4GameOptionsList::OptionTeamColors::OptionTeamColors(class C4GameOptionsList *pForDlg)
  181. : C4GameOptionsList::OptionDropdown(pForDlg, LoadResStr("IDS_MSG_TEAMCOLORS"), !::Control.isCtrlHost())
  182. {
  183. SetToolTip(LoadResStr("IDS_MSG_TEAMCOLORS_DESC"));
  184. }
  185. void C4GameOptionsList::OptionTeamColors::DoDropdownFill(C4GUI::ComboBox_FillCB *pFiller)
  186. {
  187. pFiller->AddEntry(LoadResStr("IDS_MSG_ENABLED"), 1);
  188. pFiller->AddEntry(LoadResStr("IDS_MSG_DISABLED"), 0);
  189. }
  190. void C4GameOptionsList::OptionTeamColors::DoDropdownSelChange(int32_t idNewSelection)
  191. {
  192. bool fEnabled = !!idNewSelection;
  193. Game.Teams.SendSetTeamColors(fEnabled);
  194. }
  195. void C4GameOptionsList::OptionTeamColors::Update()
  196. {
  197. pDropdownList->SetText(Game.Teams.IsTeamColors() ? LoadResStr("IDS_MSG_ENABLED") : LoadResStr("IDS_MSG_DISABLED"));
  198. }
  199. // ----------- C4GameOptionsList -----------------------------------------------------------------------
  200. C4GameOptionsList::C4GameOptionsList(const C4Rect &rcBounds, bool fActive, bool fRuntime)
  201. : C4GUI::ListBox(rcBounds), fRuntime(fRuntime)
  202. {
  203. // initial option fill
  204. InitOptions();
  205. if (fActive) Activate();
  206. }
  207. void C4GameOptionsList::InitOptions()
  208. {
  209. // creates option selection components
  210. new OptionControlMode(this);
  211. new OptionControlRate(this);
  212. if (::Network.isHost()) new OptionRuntimeJoin(this);
  213. if (!IsRuntime())
  214. {
  215. if (Game.Teams.HasTeamDistOptions()) new OptionTeamDist(this);
  216. if (Game.Teams.IsMultiTeams()) new OptionTeamColors(this);
  217. }
  218. }
  219. void C4GameOptionsList::Update()
  220. {
  221. // update all option items
  222. for(Option *pItem = static_cast<Option *>(pClientWindow->GetFirst()); pItem; pItem = pItem->GetNext())
  223. pItem->Update();
  224. }
  225. void C4GameOptionsList::Activate()
  226. {
  227. // register timer if necessary
  228. Application.Add(this);
  229. // force an update
  230. Update();
  231. }
  232. void C4GameOptionsList::Deactivate()
  233. {
  234. // release timer if set
  235. Application.Remove(this);
  236. }