1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2005-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2013-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // script-controlled InGame dialog to show player infos
17 
18 #ifndef INC_C4Scoreboard
19 #define INC_C4Scoreboard
20 
21 class C4Scoreboard
22 {
23 public:
24 	enum { TitleKey = -1 }; // value used to index the title bars
25 
26 private:
27 	struct Entry
28 	{
29 		StdStrBuf Text;
30 		int32_t iVal{0};
31 
EntryEntry32 		Entry() : Text() { }
GrabFromEntry33 		void GrabFrom(Entry *pFrom) // grab data w/o copy
34 		{ Text.Take(std::move(pFrom->Text)); iVal = pFrom->iVal; }
35 		void SwapWith(Entry *pSwap);
36 	};
37 private:
38 	// array - row/col zero are row/coloumn headers
39 	int32_t iRows{0},iCols{0};
40 	Entry *pEntries{nullptr};
41 
42 	// realloc arrays, copy stuff
43 	void AddRow(int32_t iInsertBefore);
44 	void AddCol(int32_t iInsertBefore);
45 	void DelRow(int32_t iDelIndex);
46 	void DelCol(int32_t iDelIndex);
47 
48 	// search row/coloumn by key value
49 	int32_t GetColByKey(int32_t iKey) const;
50 	int32_t GetRowByKey(int32_t iKey) const;
51 
52 	// exchange two rows completely
53 	void SwapRows(int32_t iRow1, int32_t iRow2);
54 
55 	// dialog control
56 	void InvalidateRows();// recalculate row sizes
57 
58 protected:
59 	// displaying dialog
60 	class C4ScoreboardDlg *pDlg{nullptr}; // NO-SAVE
61 	int32_t iDlgShow{0}; // ref counter for dialog show
62 
63 	// not bounds-checked!
GetCell(int32_t iCol,int32_t iRow)64 	Entry *GetCell(int32_t iCol, int32_t iRow) const { return pEntries+iRow*iCols+iCol; }
65 
66 	friend class C4ScoreboardDlg;
67 
68 public:
69 	C4Scoreboard() = default;
~C4Scoreboard()70 	~C4Scoreboard() { Clear(); }
71 
72 	void Clear();
73 
74 	void SetCell(int32_t iColKey, int32_t iRowKey, const char *szValue, int32_t iValue); // change cell value
75 	const char *GetCellString(int32_t iColKey, int32_t iRowKey);
76 	int32_t GetCellData(int32_t iColKey, int32_t iRowKey);
77 	void RemoveCol(int32_t iColKey);
78 	void RemoveRow(int32_t iRowKey);
79 	bool SortBy(int32_t iColKey, bool fReverse);
80 
81 	void DoDlgShow(int32_t iChange, bool fUserToggle);
82 	void HideDlg();
ShouldBeShown()83 	bool ShouldBeShown() { return iDlgShow>0 && iRows && iCols; }
CanBeShown()84 	bool CanBeShown() { return iDlgShow>=0 && iRows && iCols; }
85 
KeyUserShow()86 	bool KeyUserShow() { DoDlgShow(0, true); return true; }
87 
88 	void CompileFunc(StdCompiler *pComp);
89 };
90 
91 
92 #endif // INC_C4Scoreboard
93