Jump to content

Unity style settings launcher for Leadwerks


Einlander
 Share

Recommended Posts

This is a launcher that starts before your game allowing players to set the game's graphical options. This was created to solve the issue of Leadwerks starting at the highest resolution of the monitor, instead of the current desktop settings. It uses a quick and dirty method to get the desktop resolution.  THIS HAS NOT BEEN TESTED ON LINUX!! If this works on linux let me know, or if you made any changes to make it work.

The script is to be loaded with your game and launched before the main window is created. There are some modifications to main.lua but they are very simple.

Here is the script. Save it as Launcher.lua in the same folder as Menu.lua

 

-- Settings Launcher by Einlander

function LauncherMenu() -- It provides it's own context
	local LauncherMenu = {}	
	local LauncherWindow = Window:Create(((title ~= nil) and title or System.AppName),0,0,400,650,Window.Titlebar + Window.Center)	
	local LauncherContext = Context:Create(LauncherWindow)
	local LauncherScale = 1
	local LauncherBorder = 10
	local LauncherMenu = GUI:Create(LauncherContext)	
	local LauncherMenuClosed = false
	

	LauncherMenu:SetScale(LauncherScale)
	LauncherMenu:GetBase():SetScript("Scripts/GUI/Panel.lua")
	
	LauncherMenu.tabber = Widget:Tabber(LauncherBorder,LauncherBorder,LauncherMenu:GetBase():GetClientSize().x-(LauncherBorder*2),LauncherMenu:GetBase():GetClientSize().y-(LauncherBorder*2)-28,LauncherMenu:GetBase())
	LauncherMenu.tabber:AddItem("Options",true)
	LauncherMenu.panel = Widget:Panel(0,0,LauncherMenu.tabber:GetClientSize().x,LauncherMenu.tabber:GetClientSize().y,LauncherMenu.tabber)
	
	local y=LauncherBorder
	local sep=40
	
	LauncherMenu.screenreslabel =  Widget:Label("Screen Resolution",20,y,200,16,LauncherMenu.panel)
	y=y+LauncherMenu.screenreslabel:GetSize().y
	LauncherMenu.screenres = Widget:ChoiceBox(20,y,200,30,LauncherMenu.panel)
	local count = System:CountGraphicsModes()	
	for n=0,count-1 do
		local gfx = System:GetGraphicsMode(n)
		local selected=false		
		LauncherMenu.screenres:AddItem( gfx.x.."x"..gfx.y,selected)
	end
	y=y+sep
	
	--Antialias
	LauncherMenu.aalabel = Widget:Label("Antialias",20,y,200,16,LauncherMenu.panel)
	y=y+LauncherMenu.aalabel:GetSize().y
	LauncherMenu.antialias = Widget:ChoiceBox(20,y,200,30,LauncherMenu.panel)
	LauncherMenu.antialias:AddItem("None")
	LauncherMenu.antialias:AddItem("2x")
	LauncherMenu.antialias:AddItem("4x")
	y=y+sep
	
	--Texture quality
	LauncherMenu.texturequalitylabel = Widget:Label("Texture Detail",20,y,200,16,LauncherMenu.panel)
	y=y+16
	LauncherMenu.texturequality = Widget:ChoiceBox(20,y,200,30,LauncherMenu.panel)
	LauncherMenu.texturequality:AddItem("Low")
	LauncherMenu.texturequality:AddItem("Medium")
	LauncherMenu.texturequality:AddItem("High")
	LauncherMenu.texturequality:AddItem("Very High")
	y=y+sep
	
	--Lighting quality
	LauncherMenu.lightqualitylabel = Widget:Label("Lighting Quality",20,y,200,16,LauncherMenu.panel)
	y=y+16
	LauncherMenu.lightquality = Widget:ChoiceBox(20,y,200,30,LauncherMenu.panel)
	LauncherMenu.lightquality:AddItem("Low")
	LauncherMenu.lightquality:AddItem("Medium")
	LauncherMenu.lightquality:AddItem("High")
	y=y+sep
	
	--Terrain quality
	LauncherMenu.terrainqualitylabel = Widget:Label("Terrain Quality",20,y,200,16,LauncherMenu.panel)
	y=y+16
	LauncherMenu.terrainquality = Widget:ChoiceBox(20,y,200,30,LauncherMenu.panel)
	LauncherMenu.terrainquality:AddItem("Low")
	LauncherMenu.terrainquality:AddItem("Medium")
	LauncherMenu.terrainquality:AddItem("High")
	y=y+sep
	
	--Water quality	
	LauncherMenu.waterqualitylabel = Widget:Label("Water Quality",20,y,200,16,LauncherMenu.panel)
	y=y+16
	LauncherMenu.waterquality = Widget:ChoiceBox(20,y,200,30,LauncherMenu.panel)
	LauncherMenu.waterquality:AddItem("Low")
	LauncherMenu.waterquality:AddItem("Medium")
	LauncherMenu.waterquality:AddItem("High")
	y=y+sep
	
	--Anisotropy
	LauncherMenu.afilterlabel = Widget:Label("Anisotropic Filter",20,y,200,16,LauncherMenu.panel)
	y=y+16
	LauncherMenu.afilter = Widget:ChoiceBox(20,y,200,30,LauncherMenu.panel)
	LauncherMenu.afilter:AddItem("None")
	LauncherMenu.afilter:AddItem("2x")
	LauncherMenu.afilter:AddItem("4x")
	LauncherMenu.afilter:AddItem("8x")
	LauncherMenu.afilter:AddItem("16x")
	LauncherMenu.afilter:AddItem("32x")
	y=y+sep
	
	--Screen Mode		
	LauncherMenu.waterqualitylabel = Widget:Label("Window Mode",20,y,200,16,LauncherMenu.panel)
	y=y+16
	LauncherMenu.windowmode = Widget:ChoiceBox(20,y,200,30,LauncherMenu.panel)
	LauncherMenu.windowmode:AddItem("Windowed")
	LauncherMenu.windowmode:AddItem("Borderless")
	LauncherMenu.windowmode:AddItem("Full Screen")	
	y=y+sep
	
	--Create a checkbox
	LauncherMenu.tfilter = Widget:Button("Trilinear Filter",20,y,200,30,LauncherMenu.panel)
	LauncherMenu.tfilter:SetString("style","Checkbox")
	y=y+sep
	
	--Create a checkbox
	LauncherMenu.vsync = Widget:Button("Vertical Sync",20,y,200,30,LauncherMenu.panel)
	LauncherMenu.vsync:SetString("style","Checkbox")
	y=y+sep
	
	
	LauncherMenu.closeoptions = Widget:Button("Close",LauncherMenu:GetBase():GetClientSize().x-72-LauncherBorder,LauncherMenu:GetBase():GetClientSize().y-28-5,72,28,LauncherMenu:GetBase())
	LauncherMenu.applyoptions = Widget:Button("Apply",LauncherMenu:GetBase():GetClientSize().x-72*2-4-LauncherBorder,LauncherMenu:GetBase():GetClientSize().y-28-5,72,28,LauncherMenu:GetBase())


	local function GetDesktopResolution() --Vec2()
		-- get current desktop resolution ; Super quick and dirty method, works on windows dont know about linux
		local launchertestwindow = Window:Create(System.AppName,0,0,1024,768,Window.Tool)
		launchertestwindow:Maximize()
		local desktopres = Vec2(launchertestwindow:GetWidth(),launchertestwindow:GetHeight())	
		launchertestwindow:Release()
		return desktopres
	end

	local function splitstring(str,sep)
		local array = {}
		local reg = string.format("([^%s]+)",sep)
		for mem in string.gmatch(str,reg) do
			table.insert(array, mem)
		end
		return array
	end
	
	local function LoadSettings()
		
		--Graphics mode
		-- Are there both screen settings?
		if (tonumber((System:GetProperty("screenwidth"))) ~= nil) and (tonumber((System:GetProperty("screenheight"))) ~= nil) then
			--Set the screen res to the saved settings
			local count = System:CountGraphicsModes()	
			for n=0,count-1 do
				local gfx = System:GetGraphicsMode(n)
				local selected=false
				if tonumber((System:GetProperty("screenwidth"))) == gfx.x and tonumber((System:GetProperty("screenheight"))) ==gfx.y then					
					LauncherMenu.screenres:SelectItem(n)
					break
				end								
			end
		else
			-- Default
			System:Print("setting not found")
			--No setting found, use desktop resolution
			local desktopres = GetDesktopResolution()
			local count = System:CountGraphicsModes()
			for n=0,count-1 do
				local gfx = System:GetGraphicsMode(n)
				local selected=false									
				if desktopres.x ==gfx.x and desktopres.y ==gfx.y then
					LauncherMenu.screenres:SelectItem(n)	
					break
				end															
			end
		end

		
		-- antialias
		local num = 0
		num = tonumber((System:GetProperty("antialias")))
		if num ~= nil then
			if num > 0 then				
				if num < 5 then
					LauncherMenu.antialias:SelectItem(num/2)
				else					
					LauncherMenu.antialias:SetText(num)
				end
			else
				LauncherMenu.antialias:SelectItem(1)
			end
		else 
		-- deafult
			LauncherMenu.antialias:SelectItem(1)
		end
		
		--Texture detail
		num = tonumber((System:GetProperty("texturedetail")))		
		if num~=nil then			
			if num >= 0 then
				if num <= 3 then
					LauncherMenu.texturequality:SelectItem(3 - num)
				else
					LauncherMenu.texturequality:SelectItem(3)
				end
			else
				LauncherMenu.texturequality:SelectItem(0)
			end
		else
			-- default
			LauncherMenu.texturequality:SelectItem(3)
		end
		
		--Lighting Quality
		num = tonumber((System:GetProperty("lightquality")))		
		if num~=nil then			
			if num >= 0 then
				if num <= 2 then
					LauncherMenu.lightquality:SelectItem(num)
				else
					LauncherMenu.lightquality:SelectItem(2)
				end
			else
				LauncherMenu.lightquality:SelectItem(0)
			end
		else
			-- Default
			LauncherMenu.lightquality:SelectItem(1)
		end
		
		--Terrain Quality
		num = tonumber((System:GetProperty("terrainquality")))
		if num ~= nil then
			if num >= 0 then				
				if num <= 3 then
					LauncherMenu.terrainquality:SelectItem(num/2)
				end
			else
				LauncherMenu.terrainquality:SelectItem(1)
			end
		else 
		-- deafult
			LauncherMenu.terrainquality:SelectItem(1)
		end
		
		--Water Quality
		num = tonumber((System:GetProperty("waterquality")))
		if num ~= nil then
			if num >= 0 then				
				if num <= 3 then
					LauncherMenu.waterquality:SelectItem(num)
				end
			else
				LauncherMenu.waterquality:SelectItem(1)
			end
		else 
		-- deafult
			LauncherMenu.waterquality:SelectItem(1)
		end
		
		--Anisotropic Filter
		num = tonumber((System:GetProperty("anisotropicfilter")))
		if num ~= nil then
			num = num .. "x"
			if anisotropy=="0x" then anisotropy="None" end
			for n=0,LauncherMenu.afilter:CountItems()-1 do
				LauncherMenu.afilter:SetText(num)
				if num==LauncherMenu.afilter:GetItemText(n) then
					LauncherMenu.afilter:SelectItem(n)
					break
				end
			end
		else 
		-- deafult
			LauncherMenu.afilter:SelectItem(1)
		end
		
		num = tonumber((System:GetProperty("windowmode")))
		if num ~= nil then
			if num >= 0 then				
				if num > 2 then
					LauncherMenu.windowmode:SelectItem(2)
				else
					LauncherMenu.windowmode:SelectItem(num)
				end
			else
				LauncherMenu.windowmode:SelectItem(2)
			end
		else
		-- default
			LauncherMenu.windowmode:SelectItem(2)
		end
		
		num = tonumber((System:GetProperty("trilinearfilter")))
		if num ~= nil then
			if num > 0 then
				LauncherMenu.tfilter:SetState(true)
			end
		else
			--Default
			LauncherMenu.tfilter:SetState(true)
		end
		
		num = tonumber((System:GetProperty("verticalsync")))
		if num ~= nil then
			if num > 0 then
				LauncherMenu.vsync:SetState(true)
			end
		else
			--Default
			LauncherMenu.vsync:SetState(true)
		end


		
	end
	
	
	local function ApplySettings()		
		-- Screen Resolution 		
		local gfxmode = splitstring(LauncherMenu.screenres:GetItemText(LauncherMenu.screenres:GetSelectedItem()),"x")		
		System:SetProperty("screenwidth",tonumber(gfxmode[1]))
		System:SetProperty("screenheight",tonumber(gfxmode[2]))
		
		-- Anti-aliasing		
		if LauncherMenu.antialias:GetSelectedItem() > -1 then
			
			if string.lower(LauncherMenu.antialias:GetItemText(LauncherMenu.antialias:GetSelectedItem())) == "none" then
				System:SetProperty("antialias",0)
			else			
				System:SetProperty("antialias",(string.gsub(LauncherMenu.antialias:GetItemText(LauncherMenu.antialias:GetSelectedItem()),"x","")))
			end
		else			
			System:SetProperty("antialias",LauncherMenu.antialias:GetText())
		end
		
		-- Texture Quality
		System:SetProperty("texturedetail",LauncherMenu.texturequality:CountItems()-1-LauncherMenu.texturequality:GetSelectedItem())
		
		-- Lighting Quality
		System:SetProperty("lightquality",LauncherMenu.lightquality:GetSelectedItem())		
		
		-- Terrain Quality		
		System:SetProperty("terrainquality",LauncherMenu.terrainquality:GetSelectedItem())		
		
		-- Water Quality		
		System:SetProperty("waterquality",LauncherMenu.waterquality:GetSelectedItem())		
		
		-- Anistropic Filtering
		if LauncherMenu.afilter:GetSelectedItem() > -1 then
			System:SetProperty("anisotropicfilter",(string.gsub(LauncherMenu.afilter:GetItemText(LauncherMenu.afilter:GetSelectedItem()),"x","")))
			else			
			System:SetProperty("anisotropicfilter",(string.gsub(LauncherMenu.afilter:GetText(),"x","")))
		end
		
		--Anisotropic Filtering
		System:SetProperty("trilinearfilter",((LauncherMenu.tfilter:GetState() == true) and 1 or 0))		
		
		-- Vsync
		System:SetProperty("verticalsync",((LauncherMenu.vsync:GetState() == true) and 1 or 0))
		
		-- Window Mode
		if LauncherMenu.windowmode:GetSelectedItem() > -1 then			
			System:SetProperty("windowmode",LauncherMenu.windowmode:GetSelectedItem())
			if LauncherMenu.windowmode:GetSelectedItem() == 0 then			
				LauncherWindowMode = Window.Center + Window.Titlebar				
			elseif LauncherMenu.windowmode:GetSelectedItem() == 1 then
				LauncherWindowMode = Window.Center + Window.Tool
			elseif LauncherMenu.windowmode:GetSelectedItem() == 2 then
				LauncherWindowMode = Window.FullScreen
			else
				LauncherWindowMode = Window.FullScreen
				System:SetProperty("windowmode",2)
			end
			
		else
			LauncherWindowMode = Window.FullScreen
			System:SetProperty("windowmode",2)
		end		
	end
	
	
	local function ApplyScreenMode()
		-- Window Mode
		
		num = tonumber((System:GetProperty("windowmode")))
		if num ~= nil then
			if num >= 0 then				
				if num > 2 then
					LauncherMenu.windowmode:SelectItem(2)
				else
					LauncherMenu.windowmode:SelectItem(num)
				end
			else
				LauncherMenu.windowmode:SelectItem(2)
			end
		else
		-- default
			LauncherMenu.windowmode:SelectItem(2)
		end
		
		if LauncherMenu.windowmode:GetSelectedItem() > -1 then						
			if LauncherMenu.windowmode:GetSelectedItem() == 0 then			
				LauncherWindowMode = Window.Center + Window.Titlebar				
			elseif LauncherMenu.windowmode:GetSelectedItem() == 1 then
				LauncherWindowMode = Window.Center + Window.Tool
			elseif LauncherMenu.windowmode:GetSelectedItem() == 2 then
				LauncherWindowMode = Window.FullScreen
			else
				LauncherWindowMode = Window.FullScreen				
			end			
		else
			LauncherWindowMode = Window.FullScreen			
		end		
	end
	
	local function ProcessEvents(event)					
		if event.source == LauncherMenu.applyoptions then
			ApplySettings()
			ProcessEvents(Event(Event.WidgetAction,LauncherMenu.closeoptions))
		end
		if event.source == LauncherMenu.closeoptions then
			LauncherMenuClosed = true			
		end		
	end
	
	
	LoadSettings()
	ApplyScreenMode()
	while LauncherWindow:Closed()==false do
		while EventQueue:Peek() do
			ProcessEvents(EventQueue:Wait())
		end
		if VSyncMode==nil then VSyncMode=true end				
		LauncherContext:Sync(VSyncMode)
		if LauncherMenuClosed == true then break end
	end
	
	LauncherWindow:Release()
end

 

 

To use it, first add:

import("Scripts/Launcher.lua")

to the top of Main.lua.

 

Next add 

LauncherMenu()

After the title line.

 

Lastly, change:

window=Window:Create(title,0,0,gfxmode.x,gfxmode.y,windowstyle)

to 

window=Window:Create(title,0,0,gfxmode.x,gfxmode.y, LauncherWindowMode)

 

This is all that needs to be done.

hCfTXNj.png

 

In action:

hsA3Lgd.gif

  • Like 1
  • Thanks 1
  • Upvote 3
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...