Starfall-chips/libs/hud_lib.txt
2025-03-07 13:14:36 +03:00

138 lines
5.4 KiB
Text

--@name hud lib
--@client
--@owneronly
enableHud(nil,true)
function createWindow(name, x, y, w, h)
return {
name = name,
x = x,
y = y,
w = w,
h = h,
draw = false,
child = {},
dragging = false,
dragOffset = {0, 0}
}
end
local curx, cury = input.getCursorPos()
local cursorpos = {curx, cury}
local mousedown = false
local lastmousedown = false
timer.create("cursor", 0.01, 0, function()
curx, cury = input.getCursorPos()
cursorpos = {curx, cury}
mousedown = input.isMouseDown(MOUSE.MOUSE1)
end)
local function isCursorOver(x, y, w, h)
return cursorpos[1] > x and cursorpos[1] < x + w and cursorpos[2] > y and cursorpos[2] < y + h
end
function toggleWindow(window)
window.draw = not window.draw
input.enableCursor(window.draw)
end
function addChild(window, child)
table.insert(window.child, child)
end
function createLabel(text, x, y)
return { type = "label", text = text, x = x, y = y }
end
function createButton(text, x, y, w, h, onclick)
return { type = "button", text = text, x = x, y = y, w = w, h = h, onclick = onclick }
end
function createEntry(x, y, w, h, text)
return { type = "entry", x = x, y = y, w = w, h = h, text = text or "", active = false }
end
function createCheckbox(x, y, checked)
return { type = "checkbox", x = x, y = y, checked = checked or false }
end
function createSlider(x, y, w, min, max, value)
return { type = "slider", x = x, y = y, w = w, min = min, max = max, value = value or min }
end
function createDropdown(x, y, w, options, selected)
return { type = "dropdown", x = x, y = y, w = w, options = options, selected = selected or 1, expanded = false }
end
hook.add("inputPressed", "entryInput", function(key)
for _, window in pairs(windows or {}) do
for _, child in ipairs(window.child) do
if child.type == "entry" and child.active then
if key == KEY.BACKSPACE then
child.text = child.text:sub(1, -2)
elseif key >= 32 and key <= 126 then
child.text = child.text .. string.char(key)
end
end
end
end
end)
function drawWindow(window)
if not window.draw then return end
render.setColor(Color(80, 80, 80))
render.drawRect(window.x, window.y, window.w, window.h)
for _, child in ipairs(window.child) do
if child.type == "label" then
render.drawSimpleText(window.x + child.x, window.y + child.y, child.text, 0, 1)
elseif child.type == "button" then
local hovered = isCursorOver(window.x + child.x, window.y + child.y, child.w, child.h)
local color = hovered and Color(0, 180, 220) or Color(0, 150, 200)
render.setColor(color)
render.drawRect(window.x + child.x, window.y + child.y, child.w, child.h)
render.setColor(Color(255, 255, 255))
render.drawSimpleText(window.x + child.x + child.w / 2, window.y + child.y + child.h / 2, child.text, 1, 1)
if hovered and mousedown and not lastmousedown then child.onclick() end
elseif child.type == "checkbox" then
render.setColor(child.checked and Color(0, 255, 0) or Color(255, 0, 0))
render.drawRect(window.x + child.x, window.y + child.y, 15, 15)
if isCursorOver(window.x + child.x, window.y + child.y, 15, 15) and mousedown and not lastmousedown then
child.checked = not child.checked
end
elseif child.type == "slider" then
render.setColor(Color(150, 150, 150))
render.drawRect(window.x + child.x, window.y + child.y, child.w, 5)
local sliderPos = child.x + ((child.value - child.min) / (child.max - child.min)) * child.w
render.setColor(Color(255, 255, 255))
render.drawRect(window.x + sliderPos - 5, window.y + child.y - 5, 10, 15)
if isCursorOver(window.x + sliderPos - 5, window.y + child.y - 5, 10, 15) and mousedown then
child.value = math.clamp(((cursorpos[1] - window.x - child.x) / child.w) * (child.max - child.min) + child.min, child.min, child.max)
end
elseif child.type == "dropdown" then
render.setColor(Color(200, 200, 200))
render.drawRect(window.x + child.x, window.y + child.y, child.w, 20)
render.setColor(Color(0, 0, 0))
render.drawSimpleText(window.x + child.x + 5, window.y + child.y + 5, child.options[child.selected], 0, 1)
if isCursorOver(window.x + child.x, window.y + child.y, child.w, 20) and mousedown and not lastmousedown then
child.expanded = not child.expanded
end
if child.expanded then
for i, option in ipairs(child.options) do
render.setColor(Color(180, 180, 180))
render.drawRect(window.x + child.x, window.y + child.y + 20 * i, child.w, 20)
render.setColor(Color(0, 0, 0))
render.drawSimpleText(window.x + child.x + 5, window.y + child.y + 5 + 20 * i, option, 0, 1)
if isCursorOver(window.x + child.x, window.y + child.y + 20 * i, child.w, 20) and mousedown then
child.selected = i
child.expanded = false
end
end
end
end
end
lastmousedown = mousedown
end