-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFEEditorDragAndDropManager.cpp
More file actions
259 lines (213 loc) · 6.47 KB
/
FEEditorDragAndDropManager.cpp
File metadata and controls
259 lines (213 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include "FEEditorDragAndDropManager.h"
using namespace FocalEngine;
DragAndDropManager::DragAndDropManager() {}
DragAndDropManager::~DragAndDropManager() {}
void DragAndDropManager::InitializeResources()
{
HandCursor = RESOURCE_MANAGER.LoadPNGTexture("Resources/Images/handCursor.png", "handCursor");
RESOURCE_MANAGER.SetTag(HandCursor, EDITOR_RESOURCE_TAG);
HandCursorUnavailable = RESOURCE_MANAGER.LoadPNGTexture("Resources/Images/handCursorUnavailable.png", "handCursorUnavailable");
RESOURCE_MANAGER.SetTag(HandCursorUnavailable, EDITOR_RESOURCE_TAG);
}
DragAndDropTarget* DragAndDropManager::AddTarget(const FE_OBJECT_TYPE AcceptedType, std::function<bool(FEObject*, void**)> Callback, void** UserData, const std::string ToolTipText)
{
Targets.push_back(new DragAndDropTarget(AcceptedType, Callback, UserData, ToolTipText));
return Targets.back();
}
DragAndDropTarget* DragAndDropManager::AddTarget(std::vector<FE_OBJECT_TYPE>& AcceptedTypes, std::function<bool(FEObject*, void**)> Callback, void** UserData, std::vector<std::string>& ToolTipTexts)
{
Targets.push_back(new DragAndDropTarget(AcceptedTypes, Callback, UserData, ToolTipTexts));
return Targets.back();
}
DragAndDropTarget* DragAndDropManager::AddTarget(DragAndDropTarget* NewTarget)
{
Targets.push_back(NewTarget);
return Targets.back();
}
void DragAndDropManager::DrawToolTip() const
{
ImGui::Begin("dragAndDrop info", nullptr, ImGuiWindowFlags_Tooltip | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar);
const std::string ActionText = GetToolTipText();
ImGui::Text(ActionText.c_str());
ImGui::TextUnformatted(("Name: " + Object->GetName() + "\nType: " + FEObjectTypeToString(Object->GetType())).c_str());
if (PreviewTexture != nullptr)
ImGui::Image((void*)(intptr_t)PreviewTexture->GetTextureID(), ImVec2(128, 128), UV0, UV1);
ImGui::End();
}
std::string DragAndDropManager::GetToolTipText() const
{
std::string Result = "No action available";
if (Object == nullptr)
return Result;
for (size_t i = 0; i < Targets.size(); i++)
{
if (Targets[i]->GetActive())
{
for (size_t j = 0; j < Targets[i]->AcceptedTypes.size(); j++)
{
if (Targets[i]->AcceptedTypes[j] == Object->GetType())
{
Result = Targets[i]->ToolTipTexts[j];
break;
}
}
}
if (Result != "No action available")
break;
}
return Result;
}
void DragAndDropManager::Render() const
{
if (Object == nullptr)
return;
ImGui::SetMouseCursor(ImGuiMouseCursor_None);
const auto Cursor = ImGui::GetCurrentContext()->IO.MousePos;
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
ImGui::SetNextWindowPos(Cursor);
const auto OldImGuiColPopupBg = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
ImGui::GetStyle().Colors[ImGuiCol_PopupBg] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
const auto OldImGuiColBorder = ImGui::GetStyle().Colors[ImGuiCol_Border];
ImGui::GetStyle().Colors[ImGuiCol_Border] = ImVec4(0.0f, 0.0f, 0.0f, 0.0f);
ImGui::BeginTooltip();
if (ObjectCanBeDroped())
{
ImGui::Image((void*)(intptr_t)HandCursor->GetTextureID(), ImVec2(32, 32), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f));
}
else
{
ImGui::Image((void*)(intptr_t)HandCursorUnavailable->GetTextureID(), ImVec2(32, 32), ImVec2(0.0f, 0.0f), ImVec2(1.0f, 1.0f));
}
ImGui::End();
ImGui::GetStyle().Colors[ImGuiCol_PopupBg] = OldImGuiColPopupBg;
ImGui::GetStyle().Colors[ImGuiCol_Border] = OldImGuiColBorder;
ImGui::PopStyleVar();
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(8, 8));
ImVec2 NewPosition = Cursor;
NewPosition.x += 0;
NewPosition.y += 48;
ImGui::SetNextWindowPos(NewPosition);
DrawToolTip();
for (size_t i = 0; i < Targets.size(); i++)
Targets[i]->bActive = false;
ImGui::PopStyleVar();
}
void DragAndDropManager::DropAction()
{
if (Object == nullptr)
return;
for (size_t i = 0; i < Targets.size(); i++)
{
if (Targets[i]->bActive && Targets[i]->Accept(Object))
{
Targets[i]->Callback(Object, Targets[i]->UserData);
break;
}
}
Object = nullptr;
}
bool DragAndDropManager::ObjectCanBeDroped() const
{
for (size_t i = 0; i < Targets.size(); i++)
{
if (Targets[i]->bActive && Targets[i]->Accept(Object))
{
return true;
}
}
return false;
}
void DragAndDropManager::MouseMove()
{
bDrawDragAndDropHasAction = false;
if (Object)
{
for (size_t i = 0; i < Targets.size(); i++)
{
if (Targets[i]->GetActive() && Targets[i]->Accept(Object))
{
bDrawDragAndDropHasAction = true;
return;
}
}
}
}
void DragAndDropManager::SetObjectToDrag(FEObject* Object, FETexture* Texture, ImVec2 UV0, ImVec2 UV1)
{
this->Object = Object;
PreviewTexture = Texture;
this->UV0 = UV0;
this->UV1 = UV1;
}
FETexture* DragAndDropManager::GetToolTipTexture() const
{
return PreviewTexture;
}
bool DragAndDropManager::ObjectIsDraged() const
{
return Object != nullptr;
}
DragAndDropTarget::DragAndDropTarget()
{
this->Callback = nullptr;
}
DragAndDropTarget::DragAndDropTarget(const FE_OBJECT_TYPE AcceptedType, std::function<bool(FEObject*, void**)> Callback, void** UserData, const std::string ToolTipText)
{
AcceptedTypes.push_back(AcceptedType);
this->Callback = Callback;
this->UserData = UserData;
ToolTipTexts.push_back(ToolTipText);
}
DragAndDropTarget::DragAndDropTarget(std::vector<FE_OBJECT_TYPE>& AcceptedTypes, std::function<bool(FEObject*, void**)> Callback, void** UserData, std::vector<std::string>& ToolTipTexts)
{
this->AcceptedTypes = AcceptedTypes;
this->Callback = Callback;
this->UserData = UserData;
this->ToolTipTexts = ToolTipTexts;
}
DragAndDropTarget::~DragAndDropTarget()
{
for (size_t i = 0; i < DRAG_AND_DROP_MANAGER.Targets.size(); i++)
{
if (DRAG_AND_DROP_MANAGER.Targets[i] == this)
{
DRAG_AND_DROP_MANAGER.Targets.erase(DRAG_AND_DROP_MANAGER.Targets.begin() + i);
return;
}
}
}
void DragAndDropTarget::SetActive(const bool Active)
{
this->bActive = Active;
}
bool DragAndDropTarget::GetActive() const
{
return this->bActive;
}
bool DragAndDropTarget::Accept(const FEObject* Object) const
{
for (size_t i = 0; i < AcceptedTypes.size(); i++)
{
if (AcceptedTypes[i] == Object->GetType())
return true;
}
return false;
}
void** DragAndDropTarget::GetUserData() const
{
return UserData;
}
void DragAndDropTarget::SetNewUserData(void** NewUserData)
{
UserData = NewUserData;
}
void DragAndDropTarget::StickToItem()
{
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
SetActive(true);
}
void DragAndDropTarget::StickToCurrentWindow()
{
if (ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem))
SetActive(true);
}