-
Notifications
You must be signed in to change notification settings - Fork 21
/
imgui.lua
401 lines (318 loc) · 11.7 KB
/
imgui.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
local imgui = {}
imgui.skin = {
background = Color(0, 0, 0, 0),
backgroundHover = Color(0, 0, 0, 0),
border = Color(255, 255, 255),
borderHover = Color(255, 127, 0),
foreground = Color(255, 255, 255),
foregroundHover = Color(255, 127, 0),
foregroundPress = Color(220, 80, 0),
}
local devCvar = GetConVar("developer")
function imgui.IsDeveloperMode()
return not imgui.DisableDeveloperMode and devCvar:GetInt() > 0
end
local _devMode = false -- cached local variable updated once in a while
function imgui.Hook(name, id, callback)
local hookUniqifier = debug.getinfo(1).short_src
hook.Add(name, "IMGUI / " .. id .. " / " .. hookUniqifier, callback)
end
local gState = {}
local function shouldAcceptInput()
-- don't process input during non-main renderpass
if render.GetRenderTarget() ~= nil then
return false
end
-- don't process input if we're doing VGUI stuff (and not in context menu)
if vgui.CursorVisible() and vgui.GetHoveredPanel() ~= g_ContextMenu then
return false
end
return true
end
imgui.Hook("PreRender", "Input", function()
-- calculate mouse state
if shouldAcceptInput() then
local wasPressing = gState.pressing
gState.pressing = input.IsMouseDown(MOUSE_LEFT) or input.IsKeyDown(KEY_E)
gState.pressed = not wasPressing and gState.pressing
end
end)
hook.Add("NotifyShouldTransmit", "IMGUI / ClearRenderBounds", function(ent, shouldTransmit)
if shouldTransmit and ent._imguiRBExpansion then
ent._imguiRBExpansion = nil
end
end)
local traceResultTable = {}
local traceQueryTable = { output = traceResultTable }
local function isObstructed(eyepos, hitPos)
local q = traceQueryTable
q.start = eyepos
q.endpos = hitPos
if not q.filter then
q.filter = { LocalPlayer() }
end
local tr = util.TraceLine(q)
if tr.Hit then
return true, tr.Entity
else
return false
end
end
function imgui.Start3D2D(pos, angles, scale, distanceHide, distanceFadeStart)
if gState.shutdown == true then
return
end
if gState.rendering == true then
print("[IMGUI] Starting a new IMGUI context when previous one is still rendering. Shutting down rendering pipeline to prevent crashes..")
gState.shutdown = true
return false
end
_devMode = imgui.IsDeveloperMode()
local eyePos = LocalPlayer():EyePos()
local eyePosToPos = pos - eyePos
-- OPTIMIZATION: Test that we are in front of the UI
do
local normal = angles:Up()
local dot = eyePosToPos:Dot(normal)
if _devMode then gState._devDot = dot end
-- since normal is pointing away from surface towards viewer, dot<0 is visible
if dot >= 0 then
return false
end
end
-- OPTIMIZATION: Distance based fade/hide
if distanceHide then
local distance = eyePosToPos:Length()
if distance > distanceHide then
return false
end
if _devMode then
gState._devDist = distance
gState._devHideDist = distanceHide
end
if distanceHide and distanceFadeStart and distance > distanceFadeStart then
local blend = math.min(math.Remap(distance, distanceFadeStart, distanceHide, 1, 0), 1)
render.SetBlend(blend)
surface.SetAlphaMultiplier(blend)
end
end
gState.rendering = true
gState.pos = pos
gState.angles = angles
gState.scale = scale
cam.Start3D2D(pos, angles, scale)
-- calculate mousepos
if not vgui.CursorVisible() or vgui.IsHoveringWorld() then
local tr = LocalPlayer():GetEyeTrace()
local eyepos = tr.StartPos
local eyenormal
if vgui.CursorVisible() and vgui.IsHoveringWorld() then
eyenormal = gui.ScreenToVector(gui.MousePos())
else
eyenormal = tr.Normal
end
local planeNormal = angles:Up()
local hitPos = util.IntersectRayWithPlane(eyepos, eyenormal, pos, planeNormal)
if hitPos then
local obstructed, obstructer = isObstructed(eyepos, hitPos)
if obstructed then
gState.mx = nil
gState.my = nil
if _devMode then gState._devInputBlocker = "collision " .. obstructer:GetClass() .. "/" .. obstructer:EntIndex() end
else
local diff = pos - hitPos
-- This cool code is from Willox's keypad CalculateCursorPos
local x = diff:Dot(-angles:Forward()) / scale
local y = diff:Dot(-angles:Right()) / scale
gState.mx = x
gState.my = y
end
else
gState.mx = nil
gState.my = nil
if _devMode then gState._devInputBlocker = "not looking at plane" end
end
else
gState.mx = nil
gState.my = nil
if _devMode then gState._devInputBlocker = "not hovering world" end
end
if _devMode then gState._renderStarted = SysTime() end
return true
end
function imgui.Entity3D2D(ent, lpos, lang, scale, ...)
gState.entity = ent
return imgui.Start3D2D(ent:LocalToWorld(lpos), ent:LocalToWorldAngles(lang), scale, ...)
end
local function calculateRenderBounds(x, y, w, h)
local pos = gState.pos
local fwd, right = gState.angles:Forward(), gState.angles:Right()
local scale = gState.scale
local firstCorner, secondCorner =
pos + fwd * x * scale + right * y * scale,
pos + fwd * (x+w) * scale + right * (y+h) * scale
local minrb, maxrb = Vector(math.huge, math.huge, math.huge), Vector(-math.huge, -math.huge, -math.huge)
minrb.x = math.min(minrb.x, firstCorner.x, secondCorner.x)
minrb.y = math.min(minrb.y, firstCorner.y, secondCorner.y)
minrb.z = math.min(minrb.z, firstCorner.z, secondCorner.z)
maxrb.x = math.max(maxrb.x, firstCorner.x, secondCorner.x)
maxrb.y = math.max(maxrb.y, firstCorner.y, secondCorner.y)
maxrb.z = math.max(maxrb.z, firstCorner.z, secondCorner.z)
return minrb, maxrb
end
function imgui.ExpandRenderBoundsFromRect(x, y, w, h)
local ent = gState.entity
if IsValid(ent) then
-- make sure we're not applying same expansion twice
local expansion = ent._imguiRBExpansion
if expansion then
local ex, ey, ew, eh = unpack(expansion)
if ex == x and ey == y and ew == w and eh == h then
return
end
end
local minrb, maxrb = calculateRenderBounds(x, y, w, h)
ent:SetRenderBoundsWS(minrb, maxrb)
if _devMode then
print("[IMGUI] Updated renderbounds of ", ent, " to ", minrb, "x", maxrb)
end
ent._imguiRBExpansion = {x, y, w, h}
else
if _devMode then
print("[IMGUI] Attempted to update renderbounds when entity is not valid!! ", debug.traceback())
end
end
end
local function drawDeveloperInfo()
local ang = LocalPlayer():EyeAngles()
ang:RotateAroundAxis(ang:Right(), 90)
ang:RotateAroundAxis(ang:Up(), -90)
cam.IgnoreZ(true)
cam.Start3D2D(gState.pos + Vector(0, 0, 30), ang, 0.15)
surface.SetDrawColor(0, 0, 0, 200)
surface.DrawRect(-100, 0, 200, 140)
draw.SimpleText("imgui developer", "DefaultFixedDropShadow", 0, 5, Color(78, 205, 196), TEXT_ALIGN_CENTER, nil)
surface.SetDrawColor(78, 205, 196)
surface.DrawLine(-50, 16, 50, 16)
local mx, my = gState.mx, gState.my
if mx and my then
draw.SimpleText(string.format("mouse: hovering %d x %d", mx, my), "DefaultFixedDropShadow", 0, 20, Color(0, 255, 0), TEXT_ALIGN_CENTER, nil)
else
draw.SimpleText(string.format("mouse: %s", gState._devInputBlocker or ""), "DefaultFixedDropShadow", 0, 20, Color(255, 0, 0), TEXT_ALIGN_CENTER, nil)
end
local pos = gState.pos
draw.SimpleText(string.format("pos: %.2f %.2f %.2f", pos.x, pos.y, pos.z), "DefaultFixedDropShadow", 0, 40, nil, TEXT_ALIGN_CENTER, nil)
draw.SimpleText(string.format("distance %.2f / %.2f", gState._devDist or 0, gState._devHideDist or 0), "DefaultFixedDropShadow", 0, 53, Color(200, 200, 200, 200), TEXT_ALIGN_CENTER, nil)
local ang = gState.angles
draw.SimpleText(string.format("ang: %.2f %.2f %.2f", ang.p, ang.y, ang.r), "DefaultFixedDropShadow", 0, 75, nil, TEXT_ALIGN_CENTER, nil)
draw.SimpleText(string.format("dot %d", gState._devDot or 0), "DefaultFixedDropShadow", 0, 88, Color(200, 200, 200, 200), TEXT_ALIGN_CENTER, nil)
local angToEye = (pos - LocalPlayer():EyePos()):Angle()
angToEye:RotateAroundAxis(ang:Up(), -90)
angToEye:RotateAroundAxis(ang:Right(), 90)
draw.SimpleText(string.format("angle to eye (%d,%d,%d)", angToEye.p, angToEye.y, angToEye.r), "DefaultFixedDropShadow", 0, 100, Color(200, 200, 200, 200), TEXT_ALIGN_CENTER, nil)
draw.SimpleText(string.format("rendertime avg: %.2fms", (gState._devBenchAveraged or 0) * 1000), "DefaultFixedDropShadow", 0, 120, nil, TEXT_ALIGN_CENTER, nil)
cam.End3D2D()
cam.IgnoreZ(false)
local ent = gState.entity
if IsValid(ent) and ent._imguiRBExpansion then
local ex, ey, ew, eh = unpack(ent._imguiRBExpansion)
local minrb, maxrb = calculateRenderBounds(ex, ey, ew, eh)
render.DrawWireframeBox(Vector(0, 0, 0), Angle(0, 0, 0), minrb, maxrb, Color(0, 0, 255))
end
end
function imgui.End3D2D()
if gState then
if _devMode then
local renderTook = SysTime() - gState._renderStarted
gState._devBenchTests = (gState._devBenchTests or 0) + 1
gState._devBenchTaken = (gState._devBenchTaken or 0) + renderTook
if gState._devBenchTests == 100 then
gState._devBenchAveraged = gState._devBenchTaken / 100
gState._devBenchTests = 0
gState._devBenchTaken = 0
end
end
gState.rendering = false
cam.End3D2D()
render.SetBlend(1)
surface.SetAlphaMultiplier(1)
if _devMode then
drawDeveloperInfo()
end
end
end
function imgui.CursorPos()
local mx, my = gState.mx, gState.my
return mx, my
end
function imgui.IsHovering(x, y, w, h)
local mx, my = gState.mx, gState.my
return mx and my and mx >= x and mx <= (x+w) and my >= y and my <= (y+h)
end
function imgui.IsPressing()
return shouldAcceptInput() and gState.pressing
end
function imgui.IsPressed()
return shouldAcceptInput() and gState.pressed
end
-- The cache that has String->Bool mappings telling if font has been created
local _createdFonts = {}
local EXCLAMATION_BYTE = string.byte("!")
function imgui.xFont(font, defaultSize)
-- special font
if string.byte(font, 1) == EXCLAMATION_BYTE then
-- Font not cached; parse the font
local name, size = font:match("!([^@]+)@(.+)")
if size then size = tonumber(size) end
if not size and defaultSize then
name = font:match("^!([^@]+)$")
size = defaultSize
end
local fontName = string.format("IMGUI_%s_%d", name, size)
if not _createdFonts[fontName] then
surface.CreateFont(fontName, {
font = name,
size = size
})
_createdFonts[fontName] = true
end
return fontName
end
return font
end
local colorMat = Material("color")
function imgui.xButton(x, y, w, h, borderWidth)
borderWidth = borderWidth or 1
local bgColor = imgui.IsHovering(x, y, w, h) and imgui.skin.backgroundHover or imgui.skin.background
local borderColor = imgui.IsHovering(x, y, w, h) and imgui.skin.borderHover or imgui.skin.border
surface.SetDrawColor(bgColor)
surface.DrawRect(x, y, w, h)
if borderWidth > 0 then
surface.SetDrawColor(borderColor)
surface.SetMaterial(colorMat)
surface.DrawTexturedRect(x, y, w, borderWidth)
surface.DrawTexturedRect(x, y, borderWidth, h)
surface.DrawTexturedRect(x, y+h-borderWidth, w, borderWidth)
surface.DrawTexturedRect(x+w-borderWidth, y, borderWidth, h)
end
return shouldAcceptInput() and imgui.IsHovering(x, y, w, h) and gState.pressed
end
function imgui.xCursor(x, y, w, h)
local fgColor = imgui.IsPressing() and imgui.skin.foregroundPress or imgui.skin.foreground
local mx, my = gState.mx, gState.my
if not mx or not my then return end
if x and w and (mx < x or mx > x + w) then return end
if y and h and (my < y or my > y + h) then return end
local cursorSize = math.ceil(0.3 / gState.scale)
surface.SetDrawColor(fgColor)
surface.DrawLine(mx - cursorSize, my, mx + cursorSize, my)
surface.DrawLine(mx, my - cursorSize, mx, my + cursorSize)
end
function imgui.xTextButton(text, font, x, y, w, h)
local fgColor = imgui.IsHovering(x, y, w, h) and imgui.skin.foregroundHover or imgui.skin.foreground
local clicked = imgui.xButton(x, y, w, h)
font = imgui.xFont(font, math.floor(h * 0.618))
draw.SimpleText(text, font, x+w/2, y+h/2, fgColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
return clicked
end
return imgui