OpenClonk
C4Scoreboard Class Reference

#include <C4Scoreboard.h>

Collaboration diagram for C4Scoreboard:
[legend]

Public Types

enum  { TitleKey = -1 }
 

Public Member Functions

 C4Scoreboard ()=default
 
 ~C4Scoreboard ()
 
void Clear ()
 
void SetCell (int32_t iColKey, int32_t iRowKey, const char *szValue, int32_t iValue)
 
const char * GetCellString (int32_t iColKey, int32_t iRowKey)
 
int32_t GetCellData (int32_t iColKey, int32_t iRowKey)
 
void RemoveCol (int32_t iColKey)
 
void RemoveRow (int32_t iRowKey)
 
bool SortBy (int32_t iColKey, bool fReverse)
 
void DoDlgShow (int32_t iChange, bool fUserToggle)
 
void HideDlg ()
 
bool ShouldBeShown ()
 
bool CanBeShown ()
 
bool KeyUserShow ()
 
void CompileFunc (StdCompiler *pComp)
 

Protected Member Functions

Entry * GetCell (int32_t iCol, int32_t iRow) const
 

Protected Attributes

class C4ScoreboardDlgpDlg {nullptr}
 
int32_t iDlgShow {0}
 

Friends

class C4ScoreboardDlg
 

Detailed Description

Definition at line 21 of file C4Scoreboard.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
TitleKey 

Definition at line 24 of file C4Scoreboard.h.

24 { TitleKey = -1 }; // value used to index the title bars

Constructor & Destructor Documentation

◆ C4Scoreboard()

C4Scoreboard::C4Scoreboard ( )
default

◆ ~C4Scoreboard()

C4Scoreboard::~C4Scoreboard ( )
inline

Definition at line 70 of file C4Scoreboard.h.

70 { Clear(); }

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ CanBeShown()

bool C4Scoreboard::CanBeShown ( )
inline

Definition at line 84 of file C4Scoreboard.h.

84 { return iDlgShow>=0 && iRows && iCols; }
int32_t iDlgShow
Definition: C4Scoreboard.h:61

References iDlgShow.

Referenced by DoDlgShow().

Here is the caller graph for this function:

◆ Clear()

void C4Scoreboard::Clear ( )

Definition at line 64 of file C4Scoreboard.cpp.

65 {
66  // del all cells
67  delete [] pEntries;
68  pEntries = nullptr; iRows=iCols=0;
69  // del dialog
70  iDlgShow = 0;
71  if (pDlg) { delete pDlg; pDlg = nullptr; }
72 }
class C4ScoreboardDlg * pDlg
Definition: C4Scoreboard.h:60

References iDlgShow, and pDlg.

Referenced by C4Game::Clear(), CompileFunc(), and ~C4Scoreboard().

Here is the caller graph for this function:

◆ CompileFunc()

void C4Scoreboard::CompileFunc ( StdCompiler pComp)

Definition at line 315 of file C4Scoreboard.cpp.

316 {
317  bool deserializing = pComp->isDeserializer();
318  if (deserializing) Clear();
319  pComp->Value(mkNamingAdapt(iRows, "Rows", 0));
320  pComp->Value(mkNamingAdapt(iCols, "Cols", 0));
321  pComp->Value(mkNamingAdapt(iDlgShow, "DlgShow", 0));
322  if (iRows * iCols)
323  {
324  if (deserializing) pEntries = new Entry[iRows * iCols];
325  for (int32_t iRow = 0; iRow < iRows; ++iRow)
326  for (int32_t iCol = 0; iCol < iCols; ++iCol)
327  {
328  Entry *pEnt = GetCell(iCol, iRow);
329  pComp->Value(mkNamingAdapt(pEnt->Text, FormatString("Cell%i_%iString", iCol, iRow).getData()));
330  pComp->Value(mkNamingAdapt(pEnt->iVal, FormatString("Cell%i_%iValue", iCol, iRow).getData()));
331  }
332  // recheck dlg show in read mode
333  // will usually not do anything, because reading is done before enetering shared mode
334  if (pComp->isDeserializer()) DoDlgShow(0, false);
335  }
336 }
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
void DoDlgShow(int32_t iChange, bool fUserToggle)
Entry * GetCell(int32_t iCol, int32_t iRow) const
Definition: C4Scoreboard.h:64
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53

References Clear(), DoDlgShow(), FormatString(), GetCell(), iDlgShow, StdCompiler::isDeserializer(), mkNamingAdapt(), and StdCompiler::Value().

Here is the call graph for this function:

◆ DoDlgShow()

void C4Scoreboard::DoDlgShow ( int32_t  iChange,
bool  fUserToggle 
)

Definition at line 285 of file C4Scoreboard.cpp.

286 {
287  if (::pGUI->IsExclusive()) return;
288  // update dlg show
289  iDlgShow += iChange;
290  if (!fUserToggle)
291  // script update: Dlg on off if iDlgShow variable passed zero
292  fUserToggle = (ShouldBeShown() == !pDlg);
293  else
294  // user pressed Tab: Always toggle except if the scoreboard cannot be shown at all
295  if (!CanBeShown() && !pDlg) fUserToggle = false;
296  if (fUserToggle)
297  {
298  if (!pDlg)
299  {
300  if (!C4GameOverDlg::IsShown()) // never show during game over dlg
301  ::pGUI->ShowRemoveDlg(pDlg = new C4ScoreboardDlg(this));
302  }
303  else
304  pDlg->Close(false);
305  }
306 }
C4GUIScreen * pGUI
Definition: C4Gui.cpp:1191
void Close(bool fOK)
bool ShowRemoveDlg(Dialog *pDlg)
bool IsExclusive()
Definition: C4Gui.h:2670
static bool IsShown()
Definition: C4GameOverDlg.h:91
bool CanBeShown()
Definition: C4Scoreboard.h:84
bool ShouldBeShown()
Definition: C4Scoreboard.h:83

References CanBeShown(), C4GUI::Dialog::Close(), iDlgShow, C4GUI::Screen::IsExclusive(), C4GameOverDlg::IsShown(), pDlg, pGUI, ShouldBeShown(), and C4GUI::Screen::ShowRemoveDlg().

Referenced by CompileFunc(), C4Game::Init(), and KeyUserShow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetCell()

Entry* C4Scoreboard::GetCell ( int32_t  iCol,
int32_t  iRow 
) const
inlineprotected

Definition at line 64 of file C4Scoreboard.h.

64 { return pEntries+iRow*iCols+iCol; }

Referenced by CompileFunc(), C4ScoreboardDlg::DrawElement(), GetCellData(), GetCellString(), SetCell(), SortBy(), and C4ScoreboardDlg::Update().

Here is the caller graph for this function:

◆ GetCellData()

int32_t C4Scoreboard::GetCellData ( int32_t  iColKey,
int32_t  iRowKey 
)

Definition at line 225 of file C4Scoreboard.cpp.

226 {
227  // get row/col
228  int32_t iCol = GetColByKey(iColKey);
229  int32_t iRow = GetRowByKey(iRowKey);
230  if (iCol<0 || iRow<0) return 0;
231  // now get value
232  Entry *pCell = GetCell(iCol, iRow);
233  return pCell->iVal;
234 }

References GetCell().

Here is the call graph for this function:

◆ GetCellString()

const char * C4Scoreboard::GetCellString ( int32_t  iColKey,
int32_t  iRowKey 
)

Definition at line 214 of file C4Scoreboard.cpp.

215 {
216  // get row/col
217  int32_t iCol = GetColByKey(iColKey);
218  int32_t iRow = GetRowByKey(iRowKey);
219  if (iCol<0 || iRow<0) return nullptr;
220  // now get value
221  Entry *pCell = GetCell(iCol, iRow);
222  return pCell->Text.getData();
223 }

References GetCell().

Here is the call graph for this function:

◆ HideDlg()

void C4Scoreboard::HideDlg ( )

Definition at line 308 of file C4Scoreboard.cpp.

309 {
310  if (::pGUI->IsExclusive()) return;
311  // hide scoreboard if it was active
312  if (pDlg) pDlg->Close(false);
313 }

References C4GUI::Dialog::Close(), C4GUI::Screen::IsExclusive(), pDlg, and pGUI.

Referenced by C4GameOverDlg::OnShown().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ KeyUserShow()

bool C4Scoreboard::KeyUserShow ( )
inline

Definition at line 86 of file C4Scoreboard.h.

86 { DoDlgShow(0, true); return true; }

References DoDlgShow().

Referenced by C4Game::InitKeyboard().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveCol()

void C4Scoreboard::RemoveCol ( int32_t  iColKey)

Definition at line 236 of file C4Scoreboard.cpp.

237 {
238  int32_t iCol = GetColByKey(iColKey);
239  if (iCol>=0) DelCol(iCol);
240  InvalidateRows(); // recalc row widths in display
241 }

◆ RemoveRow()

void C4Scoreboard::RemoveRow ( int32_t  iRowKey)

Definition at line 243 of file C4Scoreboard.cpp.

244 {
245  int32_t iRow = GetRowByKey(iRowKey);
246  if (iRow>=0) DelRow(iRow);
247  InvalidateRows(); // recalc row widths in display
248 }

◆ SetCell()

void C4Scoreboard::SetCell ( int32_t  iColKey,
int32_t  iRowKey,
const char *  szValue,
int32_t  iValue 
)

Definition at line 171 of file C4Scoreboard.cpp.

172 {
173  // ensure primary row/col exists
174  if (!iCols || !iRows)
175  {
176  if (!iCols) AddCol(0);
177  if (!iRows) AddRow(0);
178  GetCell(0, 0)->iVal = TitleKey;
179  }
180  // get row/col; create new if not yet existing
181  int32_t iCol = GetColByKey(iColKey);
182  if (iCol<0) { AddCol(iCol=iCols); GetCell(iCol, 0)->iVal = iColKey; }
183  int32_t iRow = GetRowByKey(iRowKey);
184  if (iRow<0) { AddRow(iRow=iRows); GetCell(0, iRow)->iVal = iRowKey; }
185  // now set values
186  Entry *pCell = GetCell(iCol, iRow);
187  pCell->Text.Copy(szValue);
188  if (iCol && iRow) pCell->iVal = iValue; // do NOT overwrite index keys!
189  // if an empty value was set, prune empty
190  if (!szValue || !*szValue)
191  {
192  // prune empty row (not label row)
193  int32_t i;
194  if (iRow)
195  {
196  for (i=1; i<iCols; ++i) if (GetCell(i, iRow)->Text) break;
197  if (i == iCols) DelRow(iRow);
198  }
199  // prune empty col (not label col)
200  if (iCol)
201  {
202  for (i=1; i<iRows; ++i) if (GetCell(iCol, i)->Text) break;
203  if (i == iRows) DelCol(iCol);
204  }
205  }
206  /* // prune empty board? but this would prevent boards that just sort
207  if (iRows == 1 && iCols == 1)
208  Clear(); // must not do this, because it will del pDlg
209  else*/
210  // recalc row widths in display (else done by clear)
211  InvalidateRows();
212 }

References GetCell(), and TitleKey.

Here is the call graph for this function:

◆ ShouldBeShown()

bool C4Scoreboard::ShouldBeShown ( )
inline

Definition at line 83 of file C4Scoreboard.h.

83 { return iDlgShow>0 && iRows && iCols; }

References iDlgShow.

Referenced by DoDlgShow().

Here is the caller graph for this function:

◆ SortBy()

bool C4Scoreboard::SortBy ( int32_t  iColKey,
bool  fReverse 
)

Definition at line 250 of file C4Scoreboard.cpp.

251 {
252  // get sort col
253  int32_t iCol = GetColByKey(iColKey);
254  if (iCol<0) return false;
255  // sort
256  int32_t iSortDir = fReverse ? -1 : +1;
257  int32_t iSortBegin=1, iSortEnd=iRows-1;
258  while (iSortBegin < iSortEnd)
259  {
260  int32_t iNewBorder = iSortBegin; int32_t i;
261  for (i = iSortBegin; i < iSortEnd; ++i)
262  if (GetCell(iCol, i)->iVal * iSortDir > GetCell(iCol, i+1)->iVal * iSortDir)
263  {
264  SwapRows(i, i+1);
265  iNewBorder = i;
266  }
267  iSortEnd = iNewBorder;
268  for (i = iSortEnd; i > iSortBegin; --i)
269  if (GetCell(iCol, i-1)->iVal * iSortDir > GetCell(iCol, i)->iVal * iSortDir)
270  {
271  SwapRows(i-1, i);
272  iNewBorder = i;
273  }
274  iSortBegin = iNewBorder;
275  }
276  return true;
277 }

References GetCell().

Here is the call graph for this function:

Friends And Related Function Documentation

◆ C4ScoreboardDlg

friend class C4ScoreboardDlg
friend

Definition at line 66 of file C4Scoreboard.h.

Member Data Documentation

◆ iDlgShow

int32_t C4Scoreboard::iDlgShow {0}
protected

Definition at line 61 of file C4Scoreboard.h.

Referenced by CanBeShown(), Clear(), CompileFunc(), DoDlgShow(), and ShouldBeShown().

◆ pDlg

class C4ScoreboardDlg* C4Scoreboard::pDlg {nullptr}
protected

Definition at line 60 of file C4Scoreboard.h.

Referenced by Clear(), DoDlgShow(), HideDlg(), and C4ScoreboardDlg::~C4ScoreboardDlg().


The documentation for this class was generated from the following files: