Page 1 of 1

[UMod] Hi-Res UI Scaling

Posted: Sat Jan 16, 2021 2:28 am
by 7Soul
A very basic mod to scale the game UI past 1x, for high resolution monitors

Scales all in-game UI including text and damage numbers

My monitor is 1080p so I couldn't test this very well. Feel free to make changes if you want

Preview:
SpoilerShow
Image
How to use:

1 - First you have to be on the new beta branch. On steam, right click the game > Properties > Betas. Add the code "ggllooeegggg" to unlock the secret "nutcracker" beta

2 - Go to "\Documents\Almost Human\legend of grimrock 2". Once the beta is downloaded, you'll see a file named "mods.cfg" and a "Mods" folder

3 - In the Mods folder, create a text file and paste the code from the end of this post, name it "hires.lua" (or any name you want). Confirm when windows ask if you want to change the extension

4 - Add the mod to mods.cfg so it looks like this:

Code: Select all

mods = {
	"hires.lua",
}
5 - Edit the .lua file to add your resolution to the list

Mod code:

Code: Select all

-- This file contains sections of Legend of Grimrock 2 source code; anything you
-- do with this file must comply with the Grimrock modding terms:
-- http://www.grimrock.net/modding_log1/modding-and-asset-usage-terms/
--
-- You are free to alter this mod or reuse its code in other Grimrock mods.

--[=[
=== UModManager Info Section ===

id = "HighResScaling"

name = "Hi-Res UI Scaling"

description = [[Forces the in-game UI to scale past 1x.

Made by 7Soul (henriquelazarini@gmail.com)]]

version = "2.0.0"

priority = 10

overwrittenFields = {"Gui.draw", "Gui.presetScaleFactors", "Gui.drawTextParagraph, "ToolTip.drawGeneric"}

=== End of Mod Info ===
]=]

-- Add your resolution to the table here (has to match the resolution that shows in the options menu)
Gui.presetScaleFactors = {
	["1600 x 1200"] = 1,
	["1920 x 1080"] = 1,
	["1920 x 1200"] = 1.1,
	["2715 x 1527"] = 1.3,
}

function GameMode:startGame()
	self.requestNewGame = true
	local uiScaleFactor = Gui.presetScaleFactors[config.resolution]
    if not uiScaleFactor then
        uiScaleFactor = config.height / 1080
    end

	-- dispose all scaled fonts
	for k,v in pairs(FontType) do
		if string.match(k, ".*Scaled$") then
			v:dispose()
		end
	end

	FontType.PalatinoScaled = Font.loadTrueType("assets/fonts/palab.ttf", 24 * uiScaleFactor, "stroke")
	FontType.PalatinoPlainScaled = Font.loadTrueType("assets/fonts/pala.ttf", 24 * uiScaleFactor, "stroke")
	FontType.PalatinoLargeScaled = Font.loadTrueType("assets/fonts/palab.ttf", 30 * uiScaleFactor, "stroke")
	FontType.ScrollScaled = Font.loadTrueType("assets/fonts/palai.ttf", 18 * uiScaleFactor)
	FontType.ScrollTitleScaled = Font.loadTrueType("assets/fonts/palai.ttf", 28 * uiScaleFactor)
	
	-- create small font
	-- HACK: size 12 looks terrible in 1280x720
	local size = math.floor(18 * uiScaleFactor + 0.5)
	if size == 12 then size = 13 end
	self.emulateSmallFontStroke = (size < 14)
	if self.emulateSmallFontStroke then
		--print("emulating small stroked font")
		FontType.PalatinoSmallScaled = Font.loadTrueType("assets/fonts/palab.ttf", size)
	else
		FontType.PalatinoSmallScaled = Font.loadTrueType("assets/fonts/palab.ttf", size, "stroke")
	end
	
	FontType.PalatinoSmallPlainScaled = Font.loadTrueType("assets/fonts/pala.ttf", size, "stroke")

	-- create tiny font
	local size = 16 * uiScaleFactor
	self.emulateTinyFontStroke = (size < 14)	
	if self.emulateTinyFontStroke then
		--print("emulating tiny stroked font")
		FontType.PalatinoTinyScaled = Font.loadTrueType("assets/fonts/palab.ttf", size)
	else
		FontType.PalatinoTinyScaled = Font.loadTrueType("assets/fonts/palab.ttf", size, "stroke")
	end
			
	self.fontStrokeColor = {0,0,0,170}
end

--
function Gui:draw()
	self:updateLayout()
	self:updateColors()
	
	ImmediateMode.beginDraw()

	ImmediateMode.setTextureFilterMode("Linear_MipLinear")

    local uiScaleFactor = Gui.presetScaleFactors[config.resolution]
    if not uiScaleFactor then
		uiScaleFactor = config.height / 1080
    end
	
	-- setup ui scaling transform for resolutions other than 1920x1080
	self:setGuiScaling(uiScaleFactor, 0, 0)
	
	-- draw mouse item info
	if self.mouseItem then
		local x,y = 70,100
		self:drawItemIcon(self.mouseItem, x, y, uiScaleFactor, false)
		gui:drawText(self.mouseItem:getFormattedName(), x + 75, y + 45, FontType.PalatinoScaled)
	end
	
	-- arrows & action icons
	-- scale wrt bottom-right screen corner
	self:setGuiScaling(uiScaleFactor, config.width, config.height)
	if config.arrowIcons and not charSheet:isVisible() then
		self:updateMovementButtons()
	end
	self.attackPanel:update()

	-- scale wrt upper-right screen corner
	self:setGuiScaling(uiScaleFactor, config.width, 0)
	charSheet:update()
	
	self:resetGuiTransform()
		
	self:updateDragging()
	self:updateFloatingTexts()
	self:updateHudPrints()
	self:updateScrambleText()

	if party:isHookRegistered("onDrawGui") then
		party:callHook("onDrawGui", gui:createCustomGuiContext())
	end

	-- draw tool tip
	ToolTip.setStyle("rounded_rect")
	ToolTip.setRect(0,0, 600,500)
	ToolTip.update()
	ToolTip.setHint("skill_list", false)
	
	-- draw mouse item
	if self.mouseItem then
		local mx,my = sys.mousePos()
		mx = mx - gameMode.viewport[1]
        my = my - gameMode.viewport[2]
        local iconSize = 75 * uiScaleFactor -- Item icons don't usually scale, so we need to take it into account when positioning it on the mouse
		self:drawItemIcon(self.mouseItem, mx - iconSize/2, my - iconSize/2, uiScaleFactor)
	end

	self:updateCelestialBodies()
		
	if party:isResting() then
		self:drawTextCentered(party.restingText or "Zzzz...", config.width/2, config.height/2, FontType.PalatinoSmall)
	end

	ImmediateMode.endDraw()
end

-- Scales text paragraphs in order to scale text boxes
local oldGuiDrawTextParagraph = Gui.drawTextParagraph
function Gui:drawTextParagraph(text, x, y, width, font, color)
	local uiScaleFactor = self.presetScaleFactors[config.resolution]
    if not uiScaleFactor then
        uiScaleFactor = config.height / 1080
    end
	local w,h = oldGuiDrawTextParagraph(self, text, x, y, width*uiScaleFactor, font, color)
	return w,h
end

Re: [UMod] Hi-Res UI Scaling

Posted: Tue Feb 23, 2021 5:00 am
by okamiyazaki
Works very well, thank you!

Re: [UMod] Hi-Res UI Scaling

Posted: Wed Jul 20, 2022 7:32 pm
by 7Soul
I figured out how to scale text so here's version 2.0 of the mod:

Code: Select all

-- This file contains sections of Legend of Grimrock 2 source code; anything you
-- do with this file must comply with the Grimrock modding terms:
-- http://www.grimrock.net/modding_log1/modding-and-asset-usage-terms/
--
-- You are free to alter this mod or reuse its code in other Grimrock mods.

--[=[
=== UModManager Info Section ===

id = "HighResScaling"

name = "Hi-Res UI Scaling"

description = [[Forces the in-game UI to scale past 1x.

Made by 7Soul (henriquelazarini@gmail.com)]]

version = "2.0.0"

priority = 10

overwrittenFields = {"Gui.draw", "Gui.presetScaleFactors", "Gui.drawTextParagraph, "ToolTip.drawGeneric"}

=== End of Mod Info ===
]=]

-- Add your resolution to the table here (has to match the resolution that shows in the options menu)
Gui.presetScaleFactors = {
	["1600 x 1200"] = 1,
	["1920 x 1080"] = 1,
	["1920 x 1200"] = 1.1,
	["2715 x 1527"] = 1.3,
}

function GameMode:startGame()
	self.requestNewGame = true
	local uiScaleFactor = Gui.presetScaleFactors[config.resolution]
    if not uiScaleFactor then
        uiScaleFactor = config.height / 1080
    end

	-- dispose all scaled fonts
	for k,v in pairs(FontType) do
		if string.match(k, ".*Scaled$") then
			v:dispose()
		end
	end

	FontType.PalatinoScaled = Font.loadTrueType("assets/fonts/palab.ttf", 24 * uiScaleFactor, "stroke")
	FontType.PalatinoPlainScaled = Font.loadTrueType("assets/fonts/pala.ttf", 24 * uiScaleFactor, "stroke")
	FontType.PalatinoLargeScaled = Font.loadTrueType("assets/fonts/palab.ttf", 30 * uiScaleFactor, "stroke")
	FontType.ScrollScaled = Font.loadTrueType("assets/fonts/palai.ttf", 18 * uiScaleFactor)
	FontType.ScrollTitleScaled = Font.loadTrueType("assets/fonts/palai.ttf", 28 * uiScaleFactor)
	
	-- create small font
	-- HACK: size 12 looks terrible in 1280x720
	local size = math.floor(18 * uiScaleFactor + 0.5)
	if size == 12 then size = 13 end
	self.emulateSmallFontStroke = (size < 14)
	if self.emulateSmallFontStroke then
		--print("emulating small stroked font")
		FontType.PalatinoSmallScaled = Font.loadTrueType("assets/fonts/palab.ttf", size)
	else
		FontType.PalatinoSmallScaled = Font.loadTrueType("assets/fonts/palab.ttf", size, "stroke")
	end
	
	FontType.PalatinoSmallPlainScaled = Font.loadTrueType("assets/fonts/pala.ttf", size, "stroke")

	-- create tiny font
	local size = 16 * uiScaleFactor
	self.emulateTinyFontStroke = (size < 14)	
	if self.emulateTinyFontStroke then
		--print("emulating tiny stroked font")
		FontType.PalatinoTinyScaled = Font.loadTrueType("assets/fonts/palab.ttf", size)
	else
		FontType.PalatinoTinyScaled = Font.loadTrueType("assets/fonts/palab.ttf", size, "stroke")
	end
			
	self.fontStrokeColor = {0,0,0,170}
end

--
function Gui:draw()
	self:updateLayout()
	self:updateColors()
	
	ImmediateMode.beginDraw()

	ImmediateMode.setTextureFilterMode("Linear_MipLinear")

    local uiScaleFactor = Gui.presetScaleFactors[config.resolution]
    if not uiScaleFactor then
		uiScaleFactor = config.height / 1080
    end
	
	-- setup ui scaling transform for resolutions other than 1920x1080
	self:setGuiScaling(uiScaleFactor, 0, 0)
	
	-- draw mouse item info
	if self.mouseItem then
		local x,y = 70,100
		self:drawItemIcon(self.mouseItem, x, y, uiScaleFactor, false)
		gui:drawText(self.mouseItem:getFormattedName(), x + 75, y + 45, FontType.PalatinoScaled)
	end
	
	-- arrows & action icons
	-- scale wrt bottom-right screen corner
	self:setGuiScaling(uiScaleFactor, config.width, config.height)
	if config.arrowIcons and not charSheet:isVisible() then
		self:updateMovementButtons()
	end
	self.attackPanel:update()

	-- scale wrt upper-right screen corner
	self:setGuiScaling(uiScaleFactor, config.width, 0)
	charSheet:update()
	
	self:resetGuiTransform()
		
	self:updateDragging()
	self:updateFloatingTexts()
	self:updateHudPrints()
	self:updateScrambleText()

	if party:isHookRegistered("onDrawGui") then
		party:callHook("onDrawGui", gui:createCustomGuiContext())
	end

	-- draw tool tip
	ToolTip.setStyle("rounded_rect")
	ToolTip.setRect(0,0, 600,500)
	ToolTip.update()
	ToolTip.setHint("skill_list", false)
	
	-- draw mouse item
	if self.mouseItem then
		local mx,my = sys.mousePos()
		mx = mx - gameMode.viewport[1]
        my = my - gameMode.viewport[2]
        local iconSize = 75 * uiScaleFactor -- Item icons don't usually scale, so we need to take it into account when positioning it on the mouse
		self:drawItemIcon(self.mouseItem, mx - iconSize/2, my - iconSize/2, uiScaleFactor)
	end

	self:updateCelestialBodies()
		
	if party:isResting() then
		self:drawTextCentered(party.restingText or "Zzzz...", config.width/2, config.height/2, FontType.PalatinoSmall)
	end

	ImmediateMode.endDraw()
end

-- Scales text paragraphs in order to scale text boxes
local oldGuiDrawTextParagraph = Gui.drawTextParagraph
function Gui:drawTextParagraph(text, x, y, width, font, color)
	local uiScaleFactor = self.presetScaleFactors[config.resolution]
    if not uiScaleFactor then
        uiScaleFactor = config.height / 1080
    end
	local w,h = oldGuiDrawTextParagraph(self, text, x, y, width*uiScaleFactor, font, color)
	return w,h
end

Re: [UMod] Hi-Res UI Scaling

Posted: Wed Sep 21, 2022 2:42 am
by daverd
Hello and thanks for spending time on this mod. I can't seem to get it to work though. I'm definitely on the correct beta branch, and the game crashes on launch if I delete my Mods\hires.lua file so I'm fairly sure that mod file is being loaded, but the game looks exactly the same.

I tried setting

Code: Select all

["3840 x 2160"] = 2 
in the presetScaleFactors array and I also tried find/replacing every line that references that array and setting a value directly, e.g.

Code: Select all

local uiScaleFactor = 2
but nothing ever changes in the game.

Any ideas? Thanks.

-----------------------------

EDIT: Nevermind, it actually was working! It's just that the main menu doesn't change, but once you start a new game then it scales as expected.

Re: [UMod] Hi-Res UI Scaling

Posted: Sun Mar 05, 2023 7:31 pm
by Whisper
Not working for me, please help.

I have 2560 x 1440 resolution, nothing changes.

Re: [UMod] Hi-Res UI Scaling

Posted: Mon Mar 20, 2023 12:04 am
by 7Soul
Whisper wrote: Sun Mar 05, 2023 7:31 pm Not working for me, please help.

I have 2560 x 1440 resolution, nothing changes.
Did you add your resolution to the mod?

At the beginning of the code you should see this:

Code: Select all

Gui.presetScaleFactors = {
	["1600 x 1200"] = 1,
	["1920 x 1080"] = 1,
	["1920 x 1200"] = 1.1,
	["2715 x 1527"] = 1.3,
}
Add your resolution there