User:NXTBoy/Scripts/GUI

From Legacy Roblox Wiki
Jump to navigationJump to search

A Gui framework. Untested.

function StringAndTableReciever(f, defaultString)
	return function(stringOrTable)
		if type(stringOrTable) == "string" then
			return function(t) return f(stringOrTable, t) end
		else
			return f(defaultString, stringOrTable)
		end
	end
end

function instanceMaker(object, init)
	return StringAndTableReciever(function(name, properties)
		local i = Instance.new(object)

		for prop, value in pairs(properties) do
			if type(prop) == "function" then
				i[prop]:connect(function(...)
					return prop(b, ...)
				end)
			end
		end
		(init or function() end)(i) 
		return i
	end, object)
end

HGroup = StringAndTableReciever(function(name, item)
	local group = Instance.new("Frame")
	group.Name = name

	--Space out children
	local xSize = 1 / #items
	for x, item in ipairs(items) do
		if type(item) == "function" then
			item = item({})
		end
		item.Size     = UDim2.new(xSize, 0, 1, 0)
		item.Position = UDim2.new(xSize*(x - 1), 0, 0, 0)
		item.Parent = group
	end

	--Return the container
	return group
end, "Horizontal Group")

VGroup = StringAndTableReciever(function(name, item)
	local group = Instance.new("Frame")
	group.Name = name

	--Space out children
	local ySize = 1 / #items
	for y, item in ipairs(items) do
		if type(item) == "function" then
			item = item({})
		end
		item.Size     = UDim2.new(1, 0, ySize, 0)
		item.Position = UDim2.new(0, 0, ySize*(y - 1), 0)
		item.Parent = group
	end

	--Return the container
	return group
end, "Vertical Group")



Button = instanceMaker("TextButton", function(self)
	self.Text = self.Name
end)

Text = instanceMaker("TextLabel", function(self)
	self.Text = self.Name
end)

Example usage:

VGroup "Window" {
	Text "Hello" {FontSize = Enum.FontSize.Size24},
	Text "Are you sure you want to do this?",
	HGroup "Controls" {
		Button "Yes" {MouseButton1Click = function(b) print("Yes clicked!") end},
		Button "No" {MouseButton1Click = function(b) print("No clicked!") end}
	}
}