Initial commit
This commit is contained in:
389
addons/air_selector/lua/selector/client.lua
Normal file
389
addons/air_selector/lua/selector/client.lua
Normal file
@@ -0,0 +1,389 @@
|
||||
|
||||
-- Addon rendu publique part Heing Michel.
|
||||
|
||||
air_selector = air_selector or {}
|
||||
|
||||
air_selector.MAX_COLUMNS = 6
|
||||
air_selector.MAX_CELLS = 32
|
||||
|
||||
air_selector.columns = air_selector.columns or {}
|
||||
|
||||
-- here i fill all columns with empty cells...
|
||||
function air_selector.clear_columns(initial)
|
||||
for i = 1, air_selector.MAX_COLUMNS do
|
||||
air_selector.columns[i] = air_selector.columns[i] or {}
|
||||
|
||||
for slot_id = 1, air_selector.MAX_CELLS do
|
||||
if initial then
|
||||
air_selector.columns[i][slot_id] = {
|
||||
x = 0, y = 0,
|
||||
w = 0, h = 0,
|
||||
weapon = nil,
|
||||
}
|
||||
else
|
||||
air_selector.columns[i][slot_id].weapon = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
air_selector.clear_columns(true)
|
||||
|
||||
local last_update = 0
|
||||
function air_selector.update_weapons()
|
||||
if last_update > CurTime() then
|
||||
return
|
||||
end
|
||||
|
||||
last_update = CurTime() + 0.25
|
||||
|
||||
air_selector.clear_columns()
|
||||
|
||||
local weapons = LocalPlayer():GetWeapons()
|
||||
local weapons_count = #weapons
|
||||
|
||||
for i = 1, weapons_count do
|
||||
local weapon = weapons[i]
|
||||
local slot = weapon:GetSlot() + 1
|
||||
--local pos = weapon:GetSlotPos() + 1
|
||||
|
||||
if slot > air_selector.MAX_COLUMNS then
|
||||
slot = air_selector.MAX_COLUMNS
|
||||
end
|
||||
|
||||
--if pos > air_selector.MAX_CELLS then
|
||||
-- pos = air_selector.MAX_CELLS
|
||||
--end
|
||||
|
||||
local pos
|
||||
for cell = 1, air_selector.MAX_CELLS do
|
||||
if not air_selector.columns[slot][cell].weapon then
|
||||
pos = cell
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if pos then
|
||||
air_selector.columns[slot][pos].weapon = weapon
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
air_selector.selected_column = 1
|
||||
air_selector.selected_cell = 0
|
||||
|
||||
function air_selector.is_valid_cell(column, cell)
|
||||
if not column then
|
||||
column = air_selector.selected_column
|
||||
end
|
||||
|
||||
if not cell then
|
||||
cell = air_selector.selected_cell
|
||||
end
|
||||
|
||||
if not air_selector.columns[column] then
|
||||
return false
|
||||
end
|
||||
|
||||
if not air_selector.columns[column][cell] then
|
||||
return false
|
||||
end
|
||||
|
||||
if not air_selector.columns[column][cell].weapon then
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
-- Addon rendu publique part Heing Michel.
|
||||
---@param column number
|
||||
---@return number filled_cells
|
||||
function air_selector.get_filled_cells(column)
|
||||
local filled_cells = 0
|
||||
|
||||
if not air_selector.columns[column] then
|
||||
return 1
|
||||
end
|
||||
|
||||
for cell = 1, air_selector.MAX_CELLS do
|
||||
if air_selector.columns[column][cell].weapon then
|
||||
filled_cells = filled_cells + 1
|
||||
end
|
||||
end
|
||||
|
||||
if filled_cells == 0 then
|
||||
filled_cells = 1, false
|
||||
end
|
||||
|
||||
return filled_cells
|
||||
end
|
||||
|
||||
function air_selector.increment_column()
|
||||
air_selector.selected_column = air_selector.selected_column + 1
|
||||
air_selector.selected_cell = 1
|
||||
|
||||
if air_selector.selected_column > air_selector.MAX_COLUMNS then
|
||||
air_selector.selected_column = 1
|
||||
elseif not air_selector.is_valid_cell() then
|
||||
air_selector.increment_column()
|
||||
return
|
||||
end
|
||||
|
||||
air_selector.last_change = CurTime()
|
||||
end
|
||||
|
||||
function air_selector.decrement_column()
|
||||
air_selector.selected_column = air_selector.selected_column - 1
|
||||
air_selector.selected_cell = air_selector.get_filled_cells(air_selector.selected_column)
|
||||
|
||||
if air_selector.selected_column < 1 then
|
||||
air_selector.selected_column = air_selector.MAX_COLUMNS
|
||||
elseif not air_selector.is_valid_cell() then
|
||||
air_selector.decrement_column()
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
air_selector.last_change = CurTime()
|
||||
end
|
||||
|
||||
function air_selector.increment_cell(increment_column)
|
||||
air_selector.selected_cell = air_selector.selected_cell + 1
|
||||
|
||||
local filled_cells = air_selector.get_filled_cells(air_selector.selected_column)
|
||||
|
||||
if air_selector.selected_cell > filled_cells then
|
||||
if increment_column == false then
|
||||
air_selector.selected_cell = 1
|
||||
else
|
||||
air_selector.increment_column()
|
||||
end
|
||||
end
|
||||
|
||||
air_selector.last_change = CurTime()
|
||||
end
|
||||
|
||||
function air_selector.decrement_cell()
|
||||
air_selector.selected_cell = air_selector.selected_cell - 1
|
||||
|
||||
if air_selector.selected_cell < 1 then
|
||||
air_selector.decrement_column()
|
||||
end
|
||||
|
||||
air_selector.last_change = CurTime()
|
||||
end
|
||||
|
||||
---@param column number
|
||||
function air_selector.select_column(column)
|
||||
if air_selector.selected_column == column then
|
||||
air_selector.increment_cell(false)
|
||||
return
|
||||
end
|
||||
|
||||
air_selector.selected_column = column
|
||||
air_selector.selected_cell = 1
|
||||
|
||||
air_selector.last_change = CurTime()
|
||||
end
|
||||
-- Addon rendu publique part Heing Michel.
|
||||
-- selector controller i think...
|
||||
hook.Add("PlayerBindPress", "air_selector.binds", function(ply, bind, pressed, key)
|
||||
if not pressed then
|
||||
return
|
||||
end
|
||||
|
||||
if IsValid(LocalPlayer():GetVehicle()) then
|
||||
return
|
||||
end
|
||||
|
||||
if bind == "invprev" then
|
||||
air_selector.decrement_cell()
|
||||
return true
|
||||
end
|
||||
|
||||
if bind == "invnext" then
|
||||
air_selector.increment_cell()
|
||||
return true
|
||||
end
|
||||
|
||||
if key >= 2 and key <= air_selector.MAX_COLUMNS + 1 then
|
||||
air_selector.select_column(key - 1)
|
||||
return true
|
||||
end
|
||||
|
||||
if air_selector.lerp <= 1 then
|
||||
return
|
||||
end
|
||||
|
||||
if bind == "+attack" then
|
||||
local wep = air_selector.columns[air_selector.selected_column][air_selector.selected_cell].weapon
|
||||
if wep and LocalPlayer():GetActiveWeapon() ~= wep then
|
||||
input.SelectWeapon(wep)
|
||||
LocalPlayer():EmitSound("common/wpn_hudoff.wav", 25, 90)
|
||||
air_selector.last_change = CurTime() - 5
|
||||
air_selector.lerp = 0
|
||||
return true
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
air_selector.start_x = 470
|
||||
air_selector.start_y = 40
|
||||
|
||||
air_selector.scale = ScrH() == 2160 and 2 or ScrH() / 1080
|
||||
air_selector.s = air_selector.scale
|
||||
|
||||
air_selector.visible = false
|
||||
air_selector.lerp = 0
|
||||
air_selector.last_change = 0
|
||||
|
||||
surface.CreateFont("air_selector.font", {
|
||||
font = "Montserrat Medium",
|
||||
size = air_selector.s * 26,
|
||||
weight = 700,
|
||||
extended = true,
|
||||
})
|
||||
|
||||
surface.CreateFont("air_selector.weapon_icons", {
|
||||
font = "HalfLife2",
|
||||
size = air_selector.s * 100,
|
||||
weight = 550,
|
||||
})
|
||||
|
||||
local long_box = 240 * air_selector.s
|
||||
local small_box = 100 * air_selector.s
|
||||
|
||||
air_selector.icons = {
|
||||
["weapon_smg1"] = "a",
|
||||
["weapon_shotgun"] = "b",
|
||||
["weapon_crowbar"] = "c",
|
||||
["weapon_pistol"] = "d",
|
||||
["weapon_357"] = "e",
|
||||
["weapon_crossbow"] = "g",
|
||||
["weapon_physgun"] = "h",
|
||||
["weapon_rpg"] = "i",
|
||||
["weapon_bugbait"] = "j",
|
||||
["weapon_frag"] = "k",
|
||||
["weapon_ar2"] = "l",
|
||||
["weapon_physcannon"] = "m",
|
||||
["weapon_stunstick"] = "n",
|
||||
["weapon_slam"] = "o",
|
||||
}
|
||||
|
||||
---@param x number
|
||||
---@param y number
|
||||
---@param column number
|
||||
---@param cell boolean
|
||||
---@return number @y
|
||||
function air_selector.draw_cell(x, y, column, cell)
|
||||
local w, h = small_box, small_box
|
||||
local col = Color(255, 255, 255, 150)
|
||||
local text_col = color_black
|
||||
|
||||
if air_selector.selected_column == column then
|
||||
w = long_box
|
||||
h = 50 * air_selector.s
|
||||
|
||||
if air_selector.selected_cell == cell then
|
||||
h = 138 * air_selector.s
|
||||
col = Color(3, 179, 252, 150)
|
||||
text_col = Color(255, 255, 255, 255)
|
||||
end
|
||||
end
|
||||
-- Addon rendu publique part Heing Michel.
|
||||
local cell_table = air_selector.columns[column][cell]
|
||||
cell_table.x = Lerp(FrameTime() * 10, cell_table.x, x)
|
||||
cell_table.y = Lerp(FrameTime() * 10, cell_table.y, y)
|
||||
cell_table.w = Lerp(FrameTime() * 10, cell_table.w, w)
|
||||
cell_table.h = Lerp(FrameTime() * 10, cell_table.h, h)
|
||||
|
||||
local real_y = y
|
||||
local real_h = h
|
||||
|
||||
local x = cell_table.x
|
||||
local y = cell_table.y
|
||||
local w = cell_table.w
|
||||
local h = cell_table.h
|
||||
|
||||
local wep = air_selector.columns[column][cell].weapon
|
||||
local name
|
||||
if wep then
|
||||
name = wep:GetPrintName()
|
||||
--print(wep:GetClass())
|
||||
end
|
||||
|
||||
col.a = math.Clamp(air_selector.lerp, 0, 150)
|
||||
text_col.a = air_selector.lerp
|
||||
|
||||
draw.RoundedBox(8, x, y, w, h, col)
|
||||
|
||||
if cell == 1 then
|
||||
draw.SimpleText(column, "air_selector.font", x + 15 * air_selector.s, y + 10 * air_selector.s, text_col)
|
||||
end
|
||||
|
||||
if column == air_selector.selected_column and name then
|
||||
draw.SimpleText(name, "air_selector.font", x + w / 2, y + h - 10 * air_selector.s, text_col, 1, 4)
|
||||
|
||||
if air_selector.selected_cell == cell then
|
||||
draw.RoundedBox(8, x + w / 2 - 56 * air_selector.s, y, 112 * air_selector.s, 4 * air_selector.s, Color(5, 179, 252, air_selector.lerp))
|
||||
|
||||
if wep and air_selector.icons[wep:GetClass()] then
|
||||
local x = x + w / 2
|
||||
local y = y + h / 2
|
||||
|
||||
draw.SimpleText(air_selector.icons[wep:GetClass()], "air_selector.weapon_icons", x, y, text_col, 1, 1)
|
||||
end
|
||||
else
|
||||
draw.RoundedBox(8, x, y + h / 2 - 15 * air_selector.s, 4 * air_selector.s, 30 * air_selector.s,col)
|
||||
end
|
||||
end
|
||||
|
||||
return real_y + real_h + 10 * air_selector.s
|
||||
end
|
||||
|
||||
function air_selector.draw_columns()
|
||||
local x = air_selector.start_x
|
||||
|
||||
for column = 1, air_selector.MAX_COLUMNS do
|
||||
local cells = air_selector.selected_column == column and 6 or 1
|
||||
local y = air_selector.start_y
|
||||
y = air_selector.draw_cell(x, y, column, 1)
|
||||
|
||||
if air_selector.selected_column == column then
|
||||
for cell = 2, air_selector.MAX_CELLS do
|
||||
if air_selector.columns[column][cell].weapon then
|
||||
y = air_selector.draw_cell(x, y, column, cell)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if air_selector.selected_column == column then
|
||||
x = x + long_box + 10 * air_selector.s
|
||||
else
|
||||
x = x + small_box + 10 * air_selector.s
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Addon rendu publique part Heing Michel.
|
||||
local last_cell = 1
|
||||
local last_column = 1
|
||||
hook.Add("DrawOverlay", "air_selector.paint", function()
|
||||
local to = (air_selector.last_change > CurTime() - 1) and 255 or 0
|
||||
air_selector.lerp = Lerp(FrameTime() * 10, air_selector.lerp, to)
|
||||
|
||||
if air_selector.lerp <= 1 then
|
||||
air_selector.selected_column = 1
|
||||
air_selector.selected_cell = 0
|
||||
return
|
||||
end
|
||||
|
||||
if last_cell ~= air_selector.selected_cell or last_column ~= air_selector.selected_column then
|
||||
LocalPlayer():EmitSound("common/wpn_select.wav", 20, 100)
|
||||
|
||||
last_cell = air_selector.selected_cell
|
||||
last_column = air_selector.selected_column
|
||||
end
|
||||
|
||||
air_selector.update_weapons()
|
||||
air_selector.draw_columns()
|
||||
end)
|
||||
Reference in New Issue
Block a user