- 9,190
- 12,511
Описание: Диаграмма в виде полосочки
Пример использования:
Код:
Пример использования:
Lua:
local players = {};
local colorsNames = {
[-1] = u8'Неизвестно',
[368966908] = u8'Без организации',
[2566951719] = u8'Grove Street',
[2580667164] = u8'Los Santos Vagos',
[2580283596] = u8'East Side Ballas',
[2566979554] = u8'Varrios Los Aztecas',
[2573625087] = u8'The Rifa',
[2157523814] = u8'La Cosa Nostra',
[2159694877] = u8'Warlock MC',
[23486046] = u8'Night Wolves',
[2150852249] = u8'Russian Mafia',
[2157314562] = u8'Yakuza',
[2160918272] = u8'Правительство',
[2152104628] = u8'Страховая компания',
[2150206647] = u8'Банк',
[2164221491] = u8'Инструктор',
[2164227710] = u8'Больница',
[2157536819] = u8'Армия/ТСР',
[2164228096] = u8'TV студия',
[2164212992] = u8'Пожарный',
[2147502591] = u8'Полиция'
};
local function getPlayersColorsStatistics()
local function explodeArgb(argb)
local a = bit.band(bit.rshift(argb, 24), 0xFF)
local r = bit.band(bit.rshift(argb, 16), 0xFF)
local g = bit.band(bit.rshift(argb, 8), 0xFF)
local b = bit.band(argb, 0xFF)
return a, r, g, b
end
local result, colors, totalPlayers = {}, {}, 0;
for id = 0, 1003 do
if (sampIsPlayerConnected(id)) then
totalPlayers = totalPlayers + 1;
local playerColor = sampGetPlayerColor(id);
local key = colorsNames[playerColor] and playerColor or -1;
colors[key] = (colors[key] or 0) + 1;
end
end
for color, playersCount in pairs(colors) do
local a, r, g, b = explodeArgb(color);
table.insert(result, {
label = colorsNames[color] or u8'Неизвестно',
color = imgui.ImVec4(r / 255, g / 255, b / 255, 1),
precent = (playersCount / totalPlayers) * 100
});
end
return result;
end
-- Frame
imgui.Text(u8'Игроки в организциях');
imgui.PushStyleColor(imgui.Col.ChildBg, imgui.ImVec4(1, 1, 1, 0.1));
imgui.DiagramBar(
imgui.GetWindowWidth() - 20,
10,
players,
true
);
imgui.PopStyleColor();
if (imgui.Button('Update')) then
players = getPlayersColorsStatistics();
end
Lua:
---@param width number
---@param barHeight number
---@param bars {label: string, precent: number, color: ImVec4}[]
---@param drawLabels boolean
function imgui.DiagramBar(width, barHeight, bars, drawLabels)
local style = imgui.GetStyle();
local dl = imgui.GetWindowDrawList();
local barPos = imgui.GetCursorScreenPos();
local barSize = imgui.ImVec2(width, barHeight);
imgui.Dummy(barSize);
dl:AddRectFilled(barPos, barPos + barSize, imgui.GetColorU32(imgui.Col.FrameBg), style.FrameRounding);
local currentPrecent, onePrecentSize = 0, width / 100;
for index, bar in ipairs(bars) do
local startPos = barPos + imgui.ImVec2(currentPrecent * onePrecentSize, 0);
local endPos = barPos + imgui.ImVec2((currentPrecent + bar.precent) * onePrecentSize, barHeight);
local roundingCorners = currentPrecent == 0 and 1 + 4 or (currentPrecent + bar.precent >= 100 and 2 + 8 or 0);
dl:AddRectFilled(
startPos,
endPos,
imgui.GetColorU32Vec4(bar.color),
style.FrameRounding,
roundingCorners
);
if (not imgui.IsAnyItemHovered() and imgui.IsMouseHoveringRect(startPos, endPos)) then
dl:AddRect(startPos, endPos, imgui.GetColorU32(imgui.Col.Border), style.FrameRounding, roundingCorners, 2);
imgui.BeginTooltip();
imgui.TextColored(bar.color, ('%s - %d%%%%'):format(bar.label, bar.precent));
imgui.EndTooltip();
end
if (drawLabels) then
imgui.PushStyleColor(imgui.Col.Text, bar.color);
imgui.Bullet();
imgui.PopStyleColor();
imgui.SameLine();
local x = imgui.GetCursorPosX() + imgui.CalcTextSize(bar.label).x;
imgui.Text(bar.label);
local nextBar = bars[index + 1];
if (nextBar) then
local nextLabelSize = imgui.CalcTextSize(nextBar.label).x + 5 + style.ItemSpacing.x * 2;
if (x + nextLabelSize < imgui.GetContentRegionMax().x - style.WindowPadding.x) then
imgui.SameLine();
end
end
end
currentPrecent = currentPrecent + bar.precent;
end
end