-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathddragbase.lua
More file actions
170 lines (114 loc) · 4.36 KB
/
ddragbase.lua
File metadata and controls
170 lines (114 loc) · 4.36 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
local PANEL = {}
AccessorFunc( PANEL, "m_DNDName", "DnD" )
AccessorFunc( PANEL, "m_bLiveDrag", "UseLiveDrag" )
AccessorFunc( PANEL, "m_bReadOnly", "ReadOnly" )
function PANEL:Init()
self:SetPaintBackgroundEnabled( false )
self:SetPaintBorderEnabled( false )
self:SetMouseInputEnabled( true )
self:SetPaintBackground( false )
self:SetReadOnly( false )
self:SetDropPos( "5" )
end
--
-- Determines where we can drop stuff - relative to the other children
-- Param is a string of numpad numbers, ie "852" is top middle bottom
--
function PANEL:SetDropPos( strPos )
self.bDropLeft = string.find( strPos, "4" )
self.bDropCenter = string.find( strPos, "5" )
self.bDropRight = string.find( strPos, "6" )
self.bDropTop = string.find( strPos, "8" )
self.bDropBottom = string.find( strPos, "2" )
end
function PANEL:MakeDroppable( name, bAllowCopy )
self:SetDnD( name )
if ( bAllowCopy ) then
self:Receiver( name, self.DropAction_Copy, { copy = "#spawnmenu.menu.copy_dnd", move = "#spawnmenu.menu.move" } )
else
self:Receiver( name, self.DropAction_Normal )
end
end
-- Backwards compatibility
function PANEL:DropAction_Copy( Drops, bDoDrop, Command, x, y )
self:DropAction_Normal( Drops, bDoDrop, Command, x, y )
end
function PANEL:DropAction_Simple( Drops, bDoDrop, Command, x, y )
self:SetDropTarget( 0, 0, self:GetWide(), 2 )
if ( bDoDrop ) then
for k, v in pairs( Drops ) do
v = v:OnDrop( self )
v:SetParent( self )
end
self:OnModified()
end
end
function PANEL:DropAction_Normal( Drops, bDoDrop, Command, x, y )
local closest = self:GetClosestChild( x, y )
if ( !IsValid( closest ) ) then
return self:DropAction_Simple( Drops, bDoDrop, Command, x, y )
end
-- This panel is only meant to be copied from, not edited!
if ( self:GetReadOnly() ) then return end
local h = closest:GetTall()
local w = closest:GetWide()
local disty = y - ( closest.y + h * 0.5 )
local distx = x - ( closest.x + w * 0.5 )
local drop = 0
if ( self.bDropCenter ) then drop = 5 end
if ( disty < 0 && self.bDropTop && ( drop == 0 || math.abs( disty ) > h * 0.1 ) ) then drop = 8 end
if ( disty >= 0 && self.bDropBottom && ( drop == 0 || math.abs( disty ) > h * 0.1 ) ) then drop = 2 end
if ( distx < 0 && self.bDropLeft && ( drop == 0 || math.abs( distx ) > w * 0.1 ) ) then drop = 4 end
if ( distx >= 0 && self.bDropRight && ( drop == 0 || math.abs( distx ) > w * 0.1 ) ) then drop = 6 end
self:UpdateDropTarget( drop, closest )
if ( table.HasValue( Drops, closest ) ) then return end
if ( !bDoDrop && !self:GetUseLiveDrag() ) then return end
-- This keeps the drop order the same,
-- whether we add it before an object or after
if ( drop == 6 || drop == 2 ) then
Drops = table.Reverse( Drops )
end
for k, v in pairs( Drops ) do
-- Don't drop one of our parents onto us
-- because we'll be sucked into a vortex
if ( v:IsOurChild( self ) ) then continue end
-- Copy the panel if we are told to from the DermaMenu(), or if we are moving from a read only panel to a not read only one.
if ( ( Command && Command == "copy" || ( IsValid( v:GetParent() ) && v:GetParent().GetReadOnly && v:GetParent():GetReadOnly() && v:GetParent():GetReadOnly() != self:GetReadOnly() ) ) && v.Copy ) then v = v:Copy() end
v = v:OnDrop( self )
if ( drop == 5 ) then
closest:DroppedOn( v )
end
if ( drop == 8 || drop == 4 ) then
v:SetParent( self )
v:MoveToBefore( closest )
end
if ( drop == 2 || drop == 6 ) then
v:SetParent( self )
v:MoveToAfter( closest )
end
end
self:OnModified()
end
function PANEL:OnModified()
-- For override
end
function PANEL:UpdateDropTarget( drop, pnl )
if ( drop == 5 ) then
return self:SetDropTarget( pnl.x, pnl.y, pnl:GetWide(), pnl:GetTall() )
end
if ( drop == 8 ) then
return self:SetDropTarget( pnl.x, pnl.y - 2, pnl:GetWide(), 4 )
end
if ( drop == 2 ) then
return self:SetDropTarget( pnl.x, pnl.y + pnl:GetTall() - 2, pnl:GetWide(), 4 )
end
if ( drop == 4 ) then
return self:SetDropTarget( pnl.x - 2, pnl.y - 2, 4, pnl:GetTall() )
end
if ( drop == 6 ) then
return self:SetDropTarget( pnl.x + pnl:GetWide() - 2, pnl.y, 4, pnl:GetTall() )
end
-- Unhandled!
--self:ClearDropTarget()
end
derma.DefineControl( "DDragBase", "", PANEL, "DPanel" )