This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
3d2dvgui.lua
271 lines (212 loc) · 6.14 KB
/
3d2dvgui.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
--[[
3D2D VGUI Wrapper
Copyright (c) 2015-2017 Alexander Overvoorde, Matt Stevens
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.
]]--
local origin = Vector(0, 0, 0)
local angle = Angle(0, 0, 0)
local normal = Vector(0, 0, 0)
local scale = 0
local maxrange = 0
-- Helper functions
local function getCursorPos()
local p = util.IntersectRayWithPlane(LocalPlayer():EyePos(), LocalPlayer():GetAimVector(), origin, normal)
-- if there wasn't an intersection, don't calculate anything.
if not p then return end
if WorldToLocal(LocalPlayer():GetShootPos(), Angle(0,0,0), origin, angle).z < 0 then return end
if maxrange > 0 then
if p:Distance(LocalPlayer():EyePos()) > maxrange then
return
end
end
local pos = WorldToLocal(p, Angle(0,0,0), origin, angle)
return pos.x, -pos.y
end
local function getParents(pnl)
local parents = {}
local parent = pnl:GetParent()
while parent do
table.insert(parents, parent)
parent = parent:GetParent()
end
return parents
end
local function absolutePanelPos(pnl)
local x, y = pnl:GetPos()
local parents = getParents(pnl)
for _, parent in ipairs(parents) do
local px, py = parent:GetPos()
x = x + px
y = y + py
end
return x, y
end
local function pointInsidePanel(pnl, x, y)
local px, py = absolutePanelPos(pnl)
local sx, sy = pnl:GetSize()
if not x or not y then return end
x = x / scale
y = y / scale
return pnl:IsVisible() and x >= px and y >= py and x <= px + sx and y <= py + sy
end
-- Input
local inputWindows = {}
local usedpanel = {}
local function isMouseOver(pnl)
return pointInsidePanel(pnl, getCursorPos())
end
local function postPanelEvent(pnl, event, ...)
if not IsValid(pnl) or not pnl:IsVisible() or not pointInsidePanel(pnl, getCursorPos()) then return false end
local handled = false
for i, child in pairs(table.Reverse(pnl:GetChildren())) do
if not child:IsMouseInputEnabled() then continue end
if postPanelEvent(child, event, ...) then
handled = true
break
end
end
if not handled and pnl[event] then
pnl[event](pnl, ...)
usedpanel[pnl] = {...}
return true
else
return false
end
end
-- Always have issue, but less
local function checkHover(pnl, x, y, found)
if not (x and y) then
x, y = getCursorPos()
end
local validchild = false
for c, child in pairs(table.Reverse(pnl:GetChildren())) do
if not child:IsMouseInputEnabled() then continue end
local check = checkHover(child, x, y, found or validchild)
if check then
validchild = true
end
end
if found then
if pnl.Hovered then
pnl.Hovered = false
if pnl.OnCursorExited then pnl:OnCursorExited() end
end
else
if not validchild and pointInsidePanel(pnl, x, y) then
pnl.Hovered = true
if pnl.OnCursorEntered then pnl:OnCursorEntered() end
return true
else
pnl.Hovered = false
if pnl.OnCursorExited then pnl:OnCursorExited() end
end
end
return false
end
-- Mouse input
hook.Add("KeyPress", "VGUI3D2DMousePress", function(_, key)
if key == IN_USE then
for pnl in pairs(inputWindows) do
if IsValid(pnl) then
origin = pnl.Origin
scale = pnl.Scale
angle = pnl.Angle
normal = pnl.Normal
local key = input.IsKeyDown(KEY_LSHIFT) and MOUSE_RIGHT or MOUSE_LEFT
postPanelEvent(pnl, "OnMousePressed", key)
end
end
end
end)
hook.Add("KeyRelease", "VGUI3D2DMouseRelease", function(_, key)
if key == IN_USE then
for pnl, key in pairs(usedpanel) do
if IsValid(pnl) then
origin = pnl.Origin
scale = pnl.Scale
angle = pnl.Angle
normal = pnl.Normal
if pnl["OnMouseReleased"] then
pnl["OnMouseReleased"](pnl, key[1])
end
usedpanel[pnl] = nil
end
end
end
end)
function vgui.Start3D2D(pos, ang, res)
origin = pos
scale = res
angle = ang
normal = ang:Up()
maxrange = 0
cam.Start3D2D(pos, ang, res)
end
function vgui.MaxRange3D2D(range)
maxrange = isnumber(range) and range or 0
end
function vgui.IsPointingPanel(pnl)
origin = pnl.Origin
scale = pnl.Scale
angle = pnl.Angle
normal = pnl.Normal
return pointInsidePanel(pnl, getCursorPos())
end
local Panel = FindMetaTable("Panel")
function Panel:Paint3D2D()
if not self:IsValid() then return end
-- Add it to the list of windows to receive input
inputWindows[self] = true
-- Override gui.MouseX and gui.MouseY for certain stuff
local oldMouseX = gui.MouseX
local oldMouseY = gui.MouseY
local cx, cy = getCursorPos()
function gui.MouseX()
return (cx or 0) / scale
end
function gui.MouseY()
return (cy or 0) / scale
end
-- Override think of DFrame's to correct the mouse pos by changing the active orientation
if self.Think then
if not self.OThink then
self.OThink = self.Think
self.Think = function()
origin = self.Origin
scale = self.Scale
angle = self.Angle
normal = self.Normal
self:OThink()
end
end
end
-- Update the hover state of controls
local _, tab = checkHover(self)
-- Store the orientation of the window to calculate the position outside the render loop
self.Origin = origin
self.Scale = scale
self.Angle = angle
self.Normal = normal
-- Draw it manually
self:SetPaintedManually(false)
self:PaintManual()
self:SetPaintedManually(true)
gui.MouseX = oldMouseX
gui.MouseY = oldMouseY
end
function vgui.End3D2D()
cam.End3D2D()
end