Panel/SetSteamID

From Garry's Mod
Revision as of 22:48, 12 February 2016 by Willox (Talk | contribs)
Jump to: navigation, search
 Panel:SetSteamID( )

Contents

Description

Used by AvatarImage panels to load an avatar by its 64-bit Steam ID (community ID).

Arguments

string steamid

The 64bit SteamID of the player to load avatar of

Arguments

number size

The size of the avatar to use. Acceptable sizes are 32, 64, 184.

Examples

Example

Creates a grid of randomly generated Steam avatars which link to their corresponding Steam user pages.

-- Returns a random 64-bit Steam ID between STEAM_0:0:1 and STEAM_0:1:100000000
function GetRandomSteamID()
	return "7656119"..tostring(7960265728+math.random(1, 200000000))
end

-- Create the Steam User Grid
-- Arg1: the size of each avatar
-- Arg2: the size to load each avatar (16, 32, 64, 84, 128, 184)
function CreateSteamUserGrid(av_size, av_res)

	-- Remove this block of code if you do not mind loading thousands of avatars
	if(av_size < 64) then
		Error("Avatar size cannot be less than 64 square pixels.\n")
		return
	end
	
	-- Delete existing grid
	if(SteamUserGrid) then SteamUserGrid:Remove() end
	
	-- Pink checkerboard to avatar image size ratio (184/132)
	local av_multi = 1.393939
	
	-- The amount of avatars we can fit width-wise and height-wise
	local w_count = math.floor(ScrW()/av_size)
	local h_count = math.floor((ScrH()-25)/av_size)	-- 25 = frame header size
		
	-- Container panel
	SteamUserGrid = vgui.Create("DFrame")
	SteamUserGrid:SetSize(w_count*av_size, (h_count*av_size)+25)
	SteamUserGrid:Center()
	SteamUserGrid:SetTitle("Randomly Generated Grid of Steam Users")
	SteamUserGrid:MakePopup()
	
	-- Loop variables
	local avatar, btn, random_id = nil, nil, nil
	
	-- Create enough avatars to fill up screen without overflowing
	for i = 0, (w_count*h_count)-1 do
	
		random_id = GetRandomSteamID()
		
		-- Add avatar to container panel
		avatar = vgui.Create("AvatarImage", SteamUserGrid)
		
		-- Add button to avatar
		btn = vgui.Create("DButton", avatar)
		
		-- Layout the avatars in a grid
		avatar:SetPos((i%w_count)*av_size, 25+math.floor(i/w_count)*av_size)
		
		-- Load the avatar image
		avatar:SetSteamID(random_id, av_res)
		btn.SteamID = random_id
		
		btn:SetSize(av_size, av_size)
		
		if(av_res > 64) then
			-- Clip the pink checkerboard by scaling it
			avatar:SetSize(av_size*av_multi, av_size*av_multi)
		else
			avatar:SetSize(av_size, av_size)
		end
		
		-- Hide button
		btn:SetDrawBorder(false)
		btn:SetDrawBackground(false)
		btn:SetKeyboardInputEnabled(false)
		btn:SetText("")
		
		-- Open user's Steam page on mouse click
		btn.DoClick = function(self, val)
			
			local url = "http://steamcommunity.com/profiles/"..self.SteamID
			
			gui.OpenURL(url)
			
		end
		
	end
	
end

Output:

CreateSteamUserGrid(64, 64)

AvatarImage SetSteamID example1.png

The white question mark avatars mean no custom icon used or the user hasn't set up a community profile. The blue question mark avatars mean the user doesn't exist.
Personal tools
Navigation