function hex2argb(hex)
	local n = tonumber(hex, 16)
	return bit.band(bit.rshift(n, 24), 0xFF)/255, bit.band(bit.rshift(n, 16), 0xFF)/255, bit.band(bit.rshift(n, 8), 0xFF)/255, bit.band(n, 0xFF)/255
end

function Horizontal(gui, x, y, ui_scale, callback)
	GuiLayoutBeginHorizontal(gui, x, y, ui_scale)
	  callback()
	GuiLayoutEnd(gui)
end


function Vertical(gui, x, y, ui_scale, callback)
	GuiLayoutBeginVertical(gui, x, y, ui_scale)
		callback()
	GuiLayoutEnd(gui)
end


function HorizontalSpacing(gui, amount)
	GuiLayoutAddHorizontalSpacing(gui, amount)
end


function VerticalSpacing(gui, amount)
	GuiLayoutAddVerticalSpacing(gui, amount)
end
  
function Grid(gui, vars, callback)
	local items = vars.items or {}
	local x = vars.x or 0
	local y = vars.y or 0

	local ui_scale = vars.ui_Scale or false
  
	local auto_size = math.max(6, math.min((#items) ^ 0.75, 12))
	local row_length = math.ceil(vars.size or auto_size);
	local row_count = math.ceil(#items / row_length)
  
	local item_pos = 1
	for row=0, row_count-1 do
	  if not items[item_pos] then break end
  
	  local y_reverse = vars.reverse and row*10 or 0
  
	  Horizontal(gui, x, y-y_reverse, ui_scale, function()
		for col = 1, row_length do
		  if not items[item_pos] then break end
		  callback(items[item_pos], item_pos)
		  item_pos = item_pos + 1
  
		  if vars.padding_x then
			HorizontalSpacing(gui, vars.padding_x)
		  end
		end
	  end)
  
	  if vars.padding_y then
		VerticalSpacing(gui, vars.padding_y)
	  end
	end
end

function DrawText(gui, font, x, y, scale, z_index, in_string, color, margin_x, margin_y)
	in_string = string.lower(in_string)
	margin_x = margin_x or 0
	margin_y = margin_y or 0
	allowed_characters = "abcdefghijklmnopqrstuvwxyz1234567890."

	GuiLayoutBeginVertical( gui, x, y, true, margin_x, margin_y )
	for string in in_string:gmatch("[^\r\n]+") do
		GuiLayoutBeginHorizontal( gui, 0, 0, true, margin_x, margin_y )
		for i = 1, #string do
			local c = string:sub(i,i)

			letter = "unknown"
			if(c ~= " " and c ~= "?")then
				for index = 1, #allowed_characters do
					local c2 = allowed_characters:sub(index,index)
					
					if(c2 == c)then
						letter = c
					end
				end
			elseif(c == " ")then
			
				letter = "space"

			elseif(c == "?")then
				letter = "question"
			end

			

			local a, r, g, b = hex2argb(color)

			GuiColorSetForNextWidget( gui, r, g, b, a )
			GuiZSetForNextWidget(gui, z_index)
			GuiImage( gui, NextID(), 0, 0, font.."/"..letter..".png", a, scale, 0, 0)

		end
		GuiLayoutEnd( gui )
	end
	GuiLayoutEnd( gui )
end

function TextSize(gui, font, x, y, scale, z_index, string, margin)
	string = string.lower(string)
	width = 0
	height = 0
	margin = margin or 0
	allowed_characters = "abcdefghijklmnopqrstuvwxyz1234567890."
	for i = 1, #string do
		local c = string:sub(i,i)

		letter = "unknown"
		if(c ~= " ")then
			for index = 1, #allowed_characters do
				local c2 = allowed_characters:sub(index,index)
				
				if(c2 == c)then
					letter = c
				end
			end
		else
		
			letter = "space"
		end

		local w2, h2 = GuiGetImageDimensions( gui, font.."/"..letter..".png", scale )

		height = h2

		width = width + margin + GuiGetImageDimensions( gui, font.."/"..letter..".png", scale )

	end
	return width, height
end