/
dragdrop.lua
585 lines (369 loc) · 11.7 KB
/
dragdrop.lua
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
if SERVER then return end
--[[
-- hairy cocks
--]]
dragndrop = {}
function dragndrop.Clear()
dragndrop.m_Receiver = nil
dragndrop.m_ReceiverSlot = nil
dragndrop.m_HoverStart = nil
dragndrop.m_MouseCode = 0
dragndrop.m_DragWatch = nil
dragndrop.m_MouseX = 0
dragndrop.m_MouseY = 0
dragndrop.m_DraggingMain = nil
dragndrop.m_Dragging = nil
dragndrop.m_DropMenu = nil
end
function dragndrop.IsDragging()
if ( dragndrop.m_Dragging != nil ) then return true end
return false
end
function dragndrop.HandleDroppedInGame()
local panel = vgui.GetHoveredPanel()
if ( !IsValid( panel ) ) then return end
if ( panel:GetClassName() != "CGModBase" ) then return end
end
function dragndrop.Drop()
if ( dragndrop.HandleDroppedInGame() ) then
dragndrop.StopDragging()
return
end
-- Show the menu
if ( dragndrop.m_MouseCode == MOUSE_RIGHT && dragndrop.m_ReceiverSlot && dragndrop.m_ReceiverSlot.Menu ) then
local x, y = dragndrop.m_Receiver:LocalCursorPos()
local menu = DermaMenu()
for k, v in pairs( dragndrop.m_ReceiverSlot.Menu ) do
menu:AddOption( v, function()
dragndrop.CallReceiverFunction( true, k, x, y )
dragndrop.StopDragging()
end )
end
menu:Open()
dragndrop.m_DropMenu = menu
return
end
dragndrop.CallReceiverFunction( true, nil, nil, nil )
dragndrop.StopDragging()
end
function dragndrop.StartDragging()
if ( !dragndrop.m_DragWatch:IsSelected() ) then
dragndrop.m_Dragging = { dragndrop.m_DragWatch }
else
local canvas = dragndrop.m_DragWatch:GetSelectionCanvas()
dragndrop.m_Dragging = {}
for k, v in pairs( canvas:GetSelectedChildren() ) do
if ( !v.m_DragSlot ) then continue end
table.insert( dragndrop.m_Dragging, v )
end
end
for k, v in pairs( dragndrop.m_Dragging ) do
if ( !IsValid( v ) ) then continue end
v:OnStartDragging()
end
dragndrop.m_DraggingMain = dragndrop.m_DragWatch
dragndrop.m_DraggingMain:MouseCapture( true )
dragndrop.m_DragWatch = nil
end
function dragndrop.StopDragging()
if ( IsValid(dragndrop.m_Receiver) ) then
dragndrop.m_Receiver:DragHoverEnd()
dragndrop.m_Receiver = nil
end
dragndrop.Clear()
end
function dragndrop.UpdateReceiver()
local hovered = vgui.GetHoveredPanel()
local receiver = nil
local receiverslot = nil
if ( IsValid( hovered ) ) then
receiver, receiverslot = hovered:GetValidReceiverSlot()
end
if ( IsValid( dragndrop.m_Receiver ) ) then
if ( receiver == dragndrop.m_Receiver ) then return end
dragndrop.m_Receiver:DragHoverEnd()
end
if ( !IsValid( receiver ) ) then
dragndrop.m_Receiver = nil
dragndrop.m_ReceiverSlot = nil
end
dragndrop.m_Receiver = receiver
dragndrop.m_ReceiverSlot = receiverslot
end
--
-- Return all the dragged panels that match this name
--
function dragndrop.GetDroppable( name )
if ( !name ) then return dragndrop.m_Dragging end
if ( !dragndrop.m_Dragging ) then return end
local t = {}
for id, pnl in pairs( dragndrop.m_Dragging ) do
if ( pnl.m_DragSlot && pnl.m_DragSlot[ name ] ) then table.insert( t, pnl ) end
end
return t
end
function dragndrop.CallReceiverFunction( bDoDrop, command, mx, my )
if ( !dragndrop.m_ReceiverSlot ) then return end
if ( !IsValid( dragndrop.m_Receiver ) ) then return end
local x, y = dragndrop.m_Receiver:LocalCursorPos()
if ( mx ) then x = mx end
if ( my ) then y = my end
if ( dragndrop.m_ReceiverSlot.Func ) then
local droppable = dragndrop.GetDroppable( dragndrop.m_ReceiverSlot.Name )
dragndrop.m_ReceiverSlot.Func( dragndrop.m_Receiver, droppable, bDoDrop, command, x, y )
end
end
function dragndrop.Think()
if ( IsValid( dragndrop.m_DropMenu ) ) then return end
--
-- We're dragging but no mouse buttons are down..
-- So force the drop whereever it is!
--
if ( dragndrop.m_Dragging != nil && !input.IsMouseDown( MOUSE_LEFT ) && !input.IsMouseDown( MOUSE_RIGHT ) ) then
--dragndrop.m_Dragging:DragMouseRelease( dragndrop.m_MouseCode )
-- return
end
--
-- We're holding down a panel, watch for start of dragging
--
if ( IsValid( dragndrop.m_DragWatch ) ) then
local dist = math.abs( dragndrop.m_MouseX - gui.MouseX() ) + math.abs( dragndrop.m_MouseY - gui.MouseY() )
if ( dist > 20 ) then
dragndrop.StartDragging()
return
end
end
if ( dragndrop.m_Dragging != nil ) then
dragndrop.HoverThink()
dragndrop.UpdateReceiver()
if ( IsValid( dragndrop.m_Receiver ) ) then
dragndrop.CallReceiverFunction( false )
end
end
end
hook.Add( "DrawOverlay", "DragNDropPaint", function()
if ( dragndrop.m_Dragging == nil ) then return end
if ( dragndrop.m_DraggingMain == nil ) then return end
if ( IsValid( dragndrop.m_DropMenu ) ) then return end
local hold_offset_x = 2048
local hold_offset_y = 2048
-- Find the top, left most panel
for k, v in pairs( dragndrop.m_Dragging ) do
if ( !IsValid( v ) ) then continue end
hold_offset_x = math.min( hold_offset_x, v.x )
hold_offset_y = math.min( hold_offset_y, v.y )
end
DisableClipping( true )
local Alpha = 0.7
if ( IsValid( dragndrop.m_Hovered ) ) then Alpha = 0.8 end
surface.SetAlphaMultiplier( Alpha )
local ox = gui.MouseX() - hold_offset_x + 8
local oy = gui.MouseY() - hold_offset_y + 8
for k, v in pairs( dragndrop.m_Dragging ) do
if ( !IsValid( v ) ) then continue end
local dist = 512 - v:Distance( dragndrop.m_DraggingMain )
if ( dist < 0 ) then continue end
dist = dist / 512
surface.SetAlphaMultiplier( Alpha * dist )
v:PaintAt( ox + v.x - v:GetWide() / 2, oy + v.y - v:GetTall() / 2 ) // fill the gap between the top left corner and the mouse position
end
surface.SetAlphaMultiplier( 1.0 )
DisableClipping( false )
end )
hook.Add( "Think", "DragNDropThink", dragndrop.Think )
--
--
--
-- Panel Drag n Drop Extensions
--
--
--
local meta = FindMetaTable( "Panel" )
--
-- Make this panel droppable
--
function meta:Droppable( name )
self.m_DragSlot = self.m_DragSlot or {}
self.m_DragSlot[ name ] = {}
return self.m_DragSlot[ name ]
end
--
-- Make this pannel a drop target
--
function meta:Receiver( name, func, menu )
self.m_ReceiverSlot = self.m_ReceiverSlot or {}
self.m_ReceiverSlot[ name ] = {}
self.m_ReceiverSlot[ name ].Name = name
self.m_ReceiverSlot[ name ].Func = func
self.m_ReceiverSlot[ name ].Menu = menu
end
--
-- Drag parent means that when we start to
-- drag this panel, we'll really start
-- dragging the defined parent
--
function meta:SetDragParent( parent )
self.m_pDragParent = parent
end
function meta:GetValidReceiverSlot()
if ( self.m_ReceiverSlot ) then
-- Find matching slot..
for k, v in pairs( self.m_ReceiverSlot ) do
if ( !dragndrop.m_DraggingMain.m_DragSlot ) then continue end
local slot = dragndrop.m_DraggingMain.m_DragSlot[ k ]
if ( !slot ) then continue end
return self, v
end
end
if ( !IsValid( self:GetParent() ) ) then
return false
end
return self:GetParent():GetValidReceiverSlot()
end
function meta:IsDraggable()
return self.m_DragSlot != nil
end
function meta:IsDragging()
if ( !self.m_DragSlot ) then return false end
return self.Dragging
end
function meta:OnStartDragging()
self.Dragging = true
self:InvalidateLayout()
if ( self:IsSelectable() ) then
local canvas = self:GetSelectionCanvas()
if ( !self:IsSelected() ) then
canvas:UnselectAll()
end
end
end
function meta:OnStopDragging()
self.Dragging = false
end
function meta:DragMousePress( mcode )
if ( IsValid( dragndrop.m_DropMenu ) ) then return end
if ( dragndrop.IsDragging() ) then return end
if ( IsValid( self.m_pDragParent ) ) then
return self.m_pDragParent:DragMousePress( mcode )
end
if ( !self.m_DragSlot ) then return end
dragndrop.Clear()
dragndrop.m_MouseCode = mcode
dragndrop.m_DragWatch = self
dragndrop.m_MouseX = gui.MouseX()
dragndrop.m_MouseY = gui.MouseY()
end
function meta:DragClick( mcode )
self:MouseCapture( true )
-- Clicking one mouse button while dragging with another!
-- Return true to stop us clicking and selecting stuff below..
return true
end
function meta:DragMouseRelease( mcode )
if ( IsValid( dragndrop.m_DropMenu ) ) then return end
-- This wasn't the button we clicked with - so don't release drag
if ( dragndrop.IsDragging() && dragndrop.m_MouseCode != mcode ) then
return self:DragClick( mcode )
end
if ( !dragndrop.IsDragging() ) then
dragndrop.Clear()
return false
end
for k, v in pairs( dragndrop.m_Dragging ) do
if ( !IsValid( v ) ) then continue end
v:OnStopDragging()
end
dragndrop.Drop()
-- Todo.. we should only do this if we enabled it!
if ( gui.EnableScreenClicker ) then
gui.EnableScreenClicker( false )
end
self:MouseCapture( false )
return true
end
function meta:DrawDragHover( x, y, w, h )
DisableClipping( true )
surface.SetDrawColor( 255, 0, 255, 100 )
surface.DrawRect( x, y, w, h )
surface.SetDrawColor( 255, 220, 255, 230 )
surface.DrawOutlinedRect( x, y, w, h )
surface.SetDrawColor( 255, 100, 255, 50 )
surface.DrawOutlinedRect( x-1, y-1, w+2, h+2 )
DisableClipping( false )
end
function meta:SetDropTarget( x, y, w, h )
if ( !self.m_bDrawingPaintOver ) then
self.m_OldPaintOver = self.PaintOver
self.m_bDrawingPaintOver = true
end
self.PaintOver = function()
if ( self.m_OldPaintOver ) then
self:m_OldPaintOver()
end
self:DrawDragHover( x, y, w, h )
end
end
function meta:DragHoverEnd()
if ( !self.m_bDrawingPaintOver ) then return end
self.PaintOver = self.m_OldPaintOver
self.m_bDrawingPaintOver = false
end
--
-- Drag Hover
--
-- These functions are used for things like trees
-- So that when you hover over the tree while dragging something
-- it will open up the tree. This works regardless of whether the
-- is droppable or not.
--
-- Implement DragHoverClick in your panel class to get this functionality
--
function meta:DragHover( HoverTime )
//
// Call DragHoverClick if we've been hovering for 0.5 seconds..
//
if ( HoverTime < 0.1 ) then dragndrop.m_bHoverClick = false end
if ( HoverTime > 0.1 && !dragndrop.m_bHoverClick ) then
self:DragHoverClick( HoverTime )
dragndrop.m_bHoverClick = true
end
end
function meta:DragHoverClick( HoverTime )
end
function meta:DroppedOn( pnl )
-- Fpr override.
end
function meta:OnDrop()
-- We're being dropped on something
-- we can create a new panel here and return it, so that instead of
-- dropping us - it drops the new panel instead! We remain where we are!
-- By default we return ourself
return self
end
--
--
--
-- This is called to open stuff when you're hovering over it.
--
--
local LastHoverThink = nil
local LastHoverChangeTime = 0
local LastX = 0
local LastY = 0
function dragndrop.HoverThink()
local hovered = vgui.GetHoveredPanel()
local x = gui.MouseX()
local y = gui.MouseY()
-- Hovering a different panel
if ( LastHoverThink != hovered || x != LastX || y != LastY ) then
LastHoverChangeTime = SysTime()
LastHoverThink = hovered
end
-- Hovered panel might do stuff when we're hovering it
-- so give it a chance to do that now.
if ( IsValid( LastHoverThink ) ) then
LastX = x
LastY = y
LastHoverThink:DragHover( SysTime() - LastHoverChangeTime )
end
end