1/*
2 * LegacyClonk
3 *
4 * Copyright (c) RedWolf Design
5 * Copyright (c) 2005, Sven2
6 * Copyright (c) 2017-2021, The LegacyClonk Team and contributors
7 *
8 * Distributed under the terms of the ISC license; see accompanying file
9 * "COPYING" for details.
10 *
11 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12 * See accompanying file "TRADEMARK" for details.
13 *
14 * To redistribute this file separately, substitute the full license texts
15 * for the above references.
16 */
17
18// script-controlled InGame dialog to show player infos
19
20#pragma once
21
22#include "C4Gui.h"
23#include "C4GuiDialogs.h"
24#include "C4StringTable.h"
25
26class C4Scoreboard
27{
28public:
29 enum { TitleKey = -1, }; // value used to index the title bars
30
31private:
32 struct Entry
33 {
34 StdStrBuf Text;
35 int32_t iVal{0};
36 };
37
38private:
39 // array - row/col zero are row/coloumn headers
40 int32_t iRows, iCols;
41 Entry *pEntries;
42
43 // realloc arrays, copy stuff
44 void AddRow(int32_t iInsertBefore);
45 void AddCol(int32_t iInsertBefore);
46 void DelRow(int32_t iDelIndex);
47 void DelCol(int32_t iDelIndex);
48
49 // search row/coloumn by key value
50 int32_t GetColByKey(int32_t iKey) const;
51 int32_t GetRowByKey(int32_t iKey) const;
52
53 // exchange two rows completely
54 void SwapRows(int32_t iRow1, int32_t iRow2);
55
56 // dialog control
57 void InvalidateRows(); // recalculate row sizes
58
59protected:
60 // displaying dialog
61 class C4ScoreboardDlg *pDlg; // NO-SAVE
62 int32_t iDlgShow; // ref counter for dialog show
63
64 // not bounds-checked!
65 Entry *GetCell(int32_t iCol, int32_t iRow) const { return pEntries + iRow * iCols + iCol; }
66
67 friend class C4ScoreboardDlg;
68
69public:
70 C4Scoreboard() : iRows(0), iCols(0), pEntries(nullptr), pDlg(nullptr), iDlgShow(0) {}
71 ~C4Scoreboard() { Clear(); }
72
73 void Clear();
74
75 void SetCell(int32_t iColKey, int32_t iRowKey, const char *szValue, int32_t iValue); // change cell value
76 const char *GetCellString(int32_t iColKey, int32_t iRowKey);
77 int32_t GetCellData(int32_t iColKey, int32_t iRowKey);
78 bool SortBy(int32_t iColKey, bool fReverse);
79
80 void DoDlgShow(int32_t iChange, bool fUserToggle);
81 void HideDlg();
82 bool ShouldBeShown() { return iDlgShow > 0 && iRows && iCols; }
83 bool CanBeShown() { return iDlgShow >= 0 && iRows && iCols; }
84
85 bool KeyUserShow() { DoDlgShow(iChange: 0, fUserToggle: true); return true; }
86
87 void CompileFunc(StdCompiler *pComp);
88};
89
90class C4ScoreboardDlg : public C4GUI::Dialog
91{
92private:
93 int32_t *piColWidths;
94 C4Scoreboard *pBrd;
95
96 enum { XIndent = 4, YIndent = 4, XMargin = 3, YMargin = 3, };
97
98public:
99 C4ScoreboardDlg(C4Scoreboard *pForScoreboard);
100 ~C4ScoreboardDlg();
101
102protected:
103 void InvalidateRows() { delete[] piColWidths; piColWidths = nullptr; }
104 void Update(); // update row widths and own size and caption
105
106 virtual bool DoPlacement(C4GUI::Screen *pOnScreen, const C4Rect &rPreferredDlgRect) override;
107 virtual void Draw(C4FacetEx &cgo) override;
108 virtual void DrawElement(C4FacetEx &cgo) override;
109
110 virtual const char *GetID() override { return "Scoreboard"; }
111
112 virtual bool IsMouseControlled() override { return false; }
113
114 friend class C4Scoreboard;
115};
116