LkEngine 0.1.2
 
Loading...
Searching...
No Matches
SelectionContext.h
1/******************************************************************
2 * SelectionContext
3 *
4 *
5 *******************************************************************/
6#pragma once
7
8#include "LkEngine/Core/Delegate/Delegate.h"
9#include "LkEngine/Core/Delegate/DelegateTraits.h"
10
12#include "LkEngine/Scene/Entity.h"
13
14
15namespace LkEngine {
16
23 {
24 Global = 0,
25 Scene,
26 ContentBrowser,
27 };
28
29 namespace Enum
30 {
31 static constexpr const char* ToString(const ESelectionContext Context)
32 {
33 switch (Context)
34 {
35 case ESelectionContext::Global: return "Global";
36 case ESelectionContext::Scene: return "Scene";
37 case ESelectionContext::ContentBrowser: return "ContentBrowser";
38 }
39 LK_CORE_ASSERT(false, "Enum::ToString failed for ESelectionContext with value: {}", static_cast<int>(Context));
40 return nullptr;
41 }
42 }
43
51 {
52 public:
54 ~LSelectionContext() = default;
55
56 static LSelectionContext& Get();
57
58 FORCEINLINE static void Select(const ESelectionContext Context, const LUUID Handle)
59 {
60 LK_CORE_ASSERT(SelectionContextMap.contains(Context), "Selection context '{}' is missing", Enum::ToString(Context));
61 auto& Selection = SelectionContextMap.at(Context);
62
63 auto Iter = std::find(Selection.begin(), Selection.end(), Handle);
64 if (Iter != Selection.end())
65 {
66 (*Iter) = Handle;
67 }
68 else
69 {
70 Selection.push_back(Handle);
71 }
72
73 SelectedHandle = Handle;
74 }
75
76 FORCEINLINE static void Deselect(const ESelectionContext Context, const LUUID Handle)
77 {
78 LK_CORE_ASSERT(SelectionContextMap.contains(Context), "Selection context '{}' is missing", Enum::ToString(Context));
79 LK_CORE_TRACE_TAG("SelectionContext", "Deselecting: {}", Handle);
80 if (Context == ESelectionContext::Global)
81 {
82 for (auto& [Context, Selection] : SelectionContextMap)
83 {
84 const std::size_t Erased = std::erase_if(Selection, [Handle](const LUUID& CurrentHandle)
85 {
86 return (Handle == CurrentHandle);
87 });
88
89 /*
90 * TODO: Not sure about to exit early here or no, depends on if the same
91 * UUID can appear in multiple contexts.
92 * Disabled for now.
93 */
94 #if 0
95 if (Erased > 0)
96 {
97 return;
98 }
99 #endif
100 }
101 }
102 else
103 {
104 LK_CORE_ASSERT(SelectionContextMap.contains(Context), "Selection context '{}' is missing", Enum::ToString(Context));
105 std::vector<LUUID>& SelectionContext = SelectionContextMap.at(Context);
106 std::erase_if(SelectionContext, [Handle](const LUUID& CurrentHandle)
107 {
108 return (Handle == CurrentHandle);
109 });
110 }
111
112 }
113
117 FORCEINLINE static void DeselectAll()
118 {
119 LK_CORE_TRACE_TAG("SelectionContext", "DeselectAll");
120 for (auto& [Context, Selection] : SelectionContextMap)
121 {
122 Selection.clear();
123 }
124 }
125
129 FORCEINLINE static void DeselectAll(const ESelectionContext Context)
130 {
131 LK_CORE_TRACE_TAG("SelectionContext", "DeselectAll: {}", Enum::ToString(Context));
132 LK_CORE_ASSERT(SelectionContextMap.contains(Context), "Selection context '{}' is missing", Enum::ToString(Context));
133 if (Context != ESelectionContext::Global)
134 {
135 LK_CORE_ASSERT(SelectionContextMap.contains(Context), "Selection context '{}' is missing", Enum::ToString(Context));
136 SelectionContextMap.at(Context).clear();
137 }
138 else
139 {
140 for (auto& [Context, Selection] : SelectionContextMap)
141 {
142 Selection.clear();
143 }
144 }
145 }
146
147 FORCEINLINE static bool IsSelected(const ESelectionContext Context, const LUUID Handle)
148 {
149 LK_CORE_ASSERT(Handle > 0, "Handle is 0");
150 const auto& Selection = SelectionContextMap.at(Context);
151 if (Context != ESelectionContext::Global)
152 {
153 LK_CORE_ASSERT(SelectionContextMap.contains(Context), "Selection context '{}' is missing", Enum::ToString(Context));
154 return (std::find(Selection.begin(), Selection.end(), Handle) != Selection.end());
155 }
156 else
157 {
158 for (auto& [Context, Selection] : SelectionContextMap)
159 {
160 if (std::find(Selection.begin(), Selection.end(), Handle) != Selection.end())
161 {
162 return true;
163 }
164 }
165
166 return false;
167 }
168 }
169
170 FORCEINLINE static const std::vector<LUUID>& GetSelected(const ESelectionContext Context)
171 {
172 if (Context == ESelectionContext::Global)
173 {
174 static std::vector<LUUID> Combined;
175 Combined.clear();
176
177 auto AllRanges = SelectionContextMap | std::views::values | std::views::join;
178 Combined.reserve(std::ranges::distance(AllRanges));
179 std::ranges::copy(AllRanges, std::back_inserter(Combined));
180 return Combined;
181 }
182 else
183 {
184 LK_CORE_ASSERT(SelectionContextMap.contains(Context), "Selection context '{}' is missing", Enum::ToString(Context));
185 return SelectionContextMap.at(Context);
186 }
187 }
188
189 FORCEINLINE static LUUID GetSelected(const ESelectionContext Context, const std::size_t Index)
190 {
191 LK_CORE_ASSERT(Context != ESelectionContext::Global, "Global selection context cannot be used in GetSelected when using indices");
192 LK_CORE_ASSERT(SelectionContextMap.contains(Context), "Selection context '{}' is missing", Enum::ToString(Context));
193 auto& Selection = SelectionContextMap.at(Context);
194 LK_CORE_VERIFY((Index >= 0) && (Index < Selection.size()));
195
196 return Selection.at(Index);
197 }
198
202 FORCEINLINE static std::size_t GetSelectionCount(const ESelectionContext Context)
203 {
204 if (Context == ESelectionContext::Global)
205 {
206 auto AllRanges = SelectionContextMap | std::views::values | std::views::join;
207 return std::ranges::distance(AllRanges);
208 }
209 else
210 {
211 LK_CORE_ASSERT(SelectionContextMap.contains(Context), "Selection context '{}' is missing", Enum::ToString(Context));
212 return SelectionContextMap[Context].size();
213 }
214 }
215
216 FORCEINLINE static bool IsAnySelected(const ESelectionContext Context)
217 {
218 return (GetSelectionCount(Context) > 0);
219 }
220
221 public:
222 static LUUID SelectedHandle; /* TODO: Remove this and replace it by using sorting of the vectors instead. */
223
224 static std::unordered_map<ESelectionContext, std::vector<LUUID>> SelectionContextMap;
225
226 private:
227 LCLASS(LSelectionContext)
228 };
229
230}
LObject implementation.
Definition Object.h:46
Definition SelectionContext.h:51
static FORCEINLINE void DeselectAll()
Definition SelectionContext.h:117
static FORCEINLINE void DeselectAll(const ESelectionContext Context)
Definition SelectionContext.h:129
static FORCEINLINE std::size_t GetSelectionCount(const ESelectionContext Context)
Definition SelectionContext.h:202
#define LCLASS(Class)
Definition CoreMacros.h:226
Definition Asset.h:11
ESelectionContext
Definition SelectionContext.h:23
Definition Globals.h:37
Definition UUID.h:13