| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) 1998-2000, Matthes Bender (RedWolf Design) |
| 5 | * Copyright (c) 2017-2021, The LegacyClonk 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 | |
| 17 | /* A dynamic list of C4IDs */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "C4DelegatedIterable.h" |
| 22 | #include "C4ForwardDeclarations.h" |
| 23 | #include "C4Id.h" |
| 24 | |
| 25 | #include <vector> |
| 26 | |
| 27 | class C4IDList : public C4DelegatedIterable<C4IDList> |
| 28 | { |
| 29 | public: |
| 30 | struct Entry |
| 31 | { |
| 32 | C4ID id; |
| 33 | int32_t count; |
| 34 | |
| 35 | Entry() : id{C4ID_None}, count{0} {} |
| 36 | Entry(C4ID id, int32_t count) : id{id}, count{count} {} |
| 37 | bool operator==(const Entry &other) const { return id == other.id && count == other.count; } |
| 38 | void CompileFunc(StdCompiler *compiler, bool withValues); |
| 39 | }; |
| 40 | |
| 41 | bool operator==(const C4IDList &other) const; |
| 42 | |
| 43 | // General |
| 44 | void Clear(); |
| 45 | bool IsClear() const; |
| 46 | // Access by direct index |
| 47 | C4ID GetID(size_t index, int32_t *ipCount = nullptr) const; |
| 48 | int32_t GetCount(size_t index) const; |
| 49 | bool SetCount(size_t index, int32_t count); |
| 50 | |
| 51 | // Access by ID |
| 52 | int32_t GetIDCount(C4ID id, int32_t zeroDefVal = 0) const; |
| 53 | bool SetIDCount(C4ID id, int32_t count, bool addNewID = false); |
| 54 | void IncreaseIDCount(C4ID id, bool addNewID = true, int32_t increaseBy = 1, bool removeEmpty = false); |
| 55 | |
| 56 | void DecreaseIDCount(C4ID id, bool removeEmpty = true) |
| 57 | { |
| 58 | IncreaseIDCount(id, addNewID: false, increaseBy: -1, removeEmpty); |
| 59 | } |
| 60 | |
| 61 | int32_t GetNumberOfIDs() const; |
| 62 | int32_t GetIndex(C4ID id) const; |
| 63 | |
| 64 | // Access by category-sorted index |
| 65 | C4ID GetID(C4DefList &rDefs, int32_t dwCategory, int32_t index, int32_t *ipCount = nullptr) const; |
| 66 | int32_t GetNumberOfIDs(C4DefList &rDefs, int32_t dwCategory) const; |
| 67 | // Aux |
| 68 | void ConsolidateValids(C4DefList &rDefs, int32_t dwCategory = 0); |
| 69 | void SortByValue(C4DefList &rDefs); |
| 70 | // Item operation |
| 71 | bool DeleteItem(size_t iIndex); |
| 72 | void Load(C4DefList &defs, int32_t category); |
| 73 | // Graphics |
| 74 | void Draw(C4Facet &cgo, int32_t iSelection, |
| 75 | C4DefList &rDefs, uint32_t dwCategory, |
| 76 | bool fCounts = true, int32_t iAlign = 0) const; |
| 77 | // Compiling |
| 78 | void CompileFunc(StdCompiler *pComp, bool fValues = true); |
| 79 | |
| 80 | private: |
| 81 | std::vector<Entry> content; |
| 82 | |
| 83 | public: |
| 84 | using Iterable = ConstIterableMember<&C4IDList::content>; |
| 85 | }; |
| 86 | |