This repository was archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 587
Expand file tree
/
Copy pathUIDragDrop.cpp
More file actions
268 lines (205 loc) · 7.03 KB
/
Copy pathUIDragDrop.cpp
File metadata and controls
268 lines (205 loc) · 7.03 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
260
261
262
263
264
265
266
267
268
//
// Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include <ThirdParty/TurboBadger/tb_widgets.h>
#include "../IO/Log.h"
#include "../Input/Input.h"
#include "../Input/InputEvents.h"
#include "UI.h"
#include "UIEvents.h"
#include "UIWidget.h"
#include "UILayout.h"
#include "UIFontDescription.h"
#include "UITextField.h"
#include "UIImageWidget.h"
#include "UISelectList.h"
#include "UIDragDrop.h"
#include "UIDragObject.h"
#ifdef ATOMIC_PLATFORM_OSX
#include "UIDragDropMac.h"
#elif ATOMIC_PLATFORM_WINDOWS
#include "UIDragDropWindows.h"
#else
namespace Atomic { void InitDragAndDrop(UIDragDrop *dragAndDrop) {} }
#endif
using namespace tb;
namespace Atomic
{
UIDragDrop::UIDragDrop(Context* context) : Object(context)
{
SubscribeToEvent(E_UIUPDATE, ATOMIC_HANDLER(UIDragDrop, HandleUIUpdate));
SubscribeToEvent(E_MOUSEMOVE, ATOMIC_HANDLER(UIDragDrop,HandleMouseMove));
SubscribeToEvent(E_MOUSEBUTTONUP, ATOMIC_HANDLER(UIDragDrop,HandleMouseUp));
SharedPtr<UIFontDescription> fd(new UIFontDescription(context));
fd->SetId("Vera");
fd->SetSize(12);
dragText_ = new UITextField(context);
dragText_->SetFontDescription(fd);
dragText_->SetGravity(UI_GRAVITY_TOP);
dragLayout_ = new UILayout(context);
dragLayout_->AddChild(dragText_);
dragLayout_->SetLayoutSize(UI_LAYOUT_SIZE_PREFERRED);
dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_GONE);
// put into hierarchy so we aren't pruned
TBWidget* root = GetSubsystem<UI>()->GetRootWidget();
root->AddChild(dragLayout_->GetInternalWidget());
InitDragAndDrop(this);
}
UIDragDrop::~UIDragDrop()
{
}
void UIDragDrop::DragEnd()
{
SharedPtr<UIDragObject> dragObject = dragObject_;
SharedPtr<UIWidget> currentTargetWidget(currentTargetWidget_);
SharedPtr<UIWidget> dragSourceWidget(dragSourceWidget_);
// clean up
currentTargetWidget_ = 0;
dragObject_ = 0;
dragSourceWidget_ = 0;
dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_GONE);
if (currentTargetWidget.Null())
{
return;
}
VariantMap dropData;
dropData[DragEnded::P_TARGET] = currentTargetWidget;
dropData[DragEnded::P_DRAGOBJECT] = dragObject;
currentTargetWidget->SendEvent(E_DRAGENDED, dropData);
}
void UIDragDrop::HandleUIUpdate(StringHash eventType, VariantMap& eventData)
{
Input* input = GetSubsystem<Input>();
if (dragSourceWidget_.NotNull() || !input->IsMouseVisible() || !input->GetMouseButtonDown(MOUSEB_LEFT))
{
return;
}
if (TBWidget::hovered_widget)
{
// see if we have a widget with a drag object
TBWidget* tbw = TBWidget::hovered_widget;
UIWidget* widget = nullptr;
while(tbw)
{
if (tbw->GetDelegate())
{
widget = (UIWidget*) tbw->GetDelegate();
if (widget->GetDragObject())
{
// TODO: check if we're in widget bounds
// this is going to need to be updated for drag/drop multiselect
break;
}
widget = nullptr;
}
tbw = tbw->GetParent();
}
if (!widget)
return;
currentTargetWidget_ = widget;
dragSourceWidget_ = widget;
mouseDownPosition_ = input->GetMousePosition();
}
}
void UIDragDrop::HandleMouseUp(StringHash eventType, VariantMap& eventData)
{
using namespace MouseButtonUp;
if (dragObject_.Null())
{
dragSourceWidget_ = 0;
currentTargetWidget_ = 0;
return;
}
if (!(eventData[P_BUTTON].GetInt() == MOUSEB_LEFT))
return;
DragEnd();
}
void UIDragDrop::HandleMouseMove(StringHash eventType, VariantMap& eventData)
{
if (dragObject_.Null() && dragSourceWidget_.Null())
return;
if (dragObject_.Null())
{
dragObject_ = dragSourceWidget_->GetDragObject();
if (dragObject_.Null())
{
dragSourceWidget_ = 0;
return;
}
}
using namespace MouseMove;
int x = eventData[P_X].GetInt();
int y = eventData[P_Y].GetInt();
// tolerance to 8 pixels to start drag/drop operation
IntVector2 mousePos(x, y);
mousePos -= mouseDownPosition_;
if (Abs(mousePos.x_) < 8 && Abs(mousePos.y_) < 8)
return;
// initialize if necessary
if (dragLayout_->GetVisibility() == UI_WIDGET_VISIBILITY_GONE)
{
dragLayout_->GetInternalWidget()->SetZ(WIDGET_Z_TOP);
dragLayout_->SetVisibility(UI_WIDGET_VISIBILITY_VISIBLE);
dragText_->SetText(dragObject_->GetText());
UIPreferredSize* sz = dragLayout_->GetPreferredSize();
dragLayout_->SetRect(IntRect(0, 0, sz->GetMinWidth(), sz->GetMinHeight()));
}
// see if we have a widget
TBWidget* tbw = TBWidget::hovered_widget;
while(tbw && (!tbw->GetDelegate() || tbw->IsOfType<TBLayout>()))
{
tbw = tbw->GetParent();
}
if (!tbw || !tbw->GetParent())
return;
UIWidget* hoverWidget = (UIWidget*) tbw->GetDelegate();
if (!hoverWidget->GetInternalWidget())
return;
if (hoverWidget != currentTargetWidget_)
{
if (currentTargetWidget_)
{
VariantMap exitData;
exitData[DragExitWidget::P_WIDGET] = currentTargetWidget_;
exitData[DragExitWidget::P_DRAGOBJECT] = dragObject_;
currentTargetWidget_->SendEvent(E_DRAGEXITWIDGET, exitData);
}
currentTargetWidget_ = hoverWidget;
VariantMap enterData;
enterData[DragEnterWidget::P_WIDGET] = currentTargetWidget_;
enterData[DragEnterWidget::P_DRAGOBJECT] = dragObject_;
currentTargetWidget_->SendEvent(E_DRAGENTERWIDGET, enterData);
}
dragLayout_->SetPosition(x, y - 20);
}
void UIDragDrop::FileDragEntered()
{
dragObject_ = new UIDragObject(context_);
}
void UIDragDrop::FileDragAddFile(const String& filename)
{
dragObject_->AddFilename(filename);
}
void UIDragDrop::FileDragConclude()
{
DragEnd();
}
}