Minimum VIewport Control

Godot Version

4.2.2

Question

I have created a script that allows me to enforce a min height and width the only problem I have is that a user can still shrink it below the values and it pushes back to the min sizes. Is there a way to stop this from happening?

This is the script.

extends Node2D

var min_size = Vector2i(640, 480)
var resize_speed = 20.0 # Adjust this value for faster or slower resizing

func _ready():
# Set initial window size to 1200x600
DisplayServer.window_set_size(Vector2i(1200, 600))

func _process(delta):
# Continuously enforce minimum window size
var current_size = DisplayServer.window_get_size()
var new_size = current_size

if current_size.x < min_size.x:
	new_size.x = lerp(current_size.x, min_size.x, resize_speed * delta)
if current_size.y < min_size.y:
	new_size.y = lerp(current_size.y, min_size.y, resize_speed * delta)

if new_size != current_size:
	DisplayServer.window_set_size(new_size)

func lerp(a: float, b: float, t: float) → float:
return a + (b - a) * clamp(t, 0.0, 1.0)

Check out https://docs.godotengine.org/de/4.x/classes/class_window.html#class-window-property-min-size
This should do exactly what you want

This script allow the project size to be any height and width but enforces a min height and width without any bounce back.

extends Control

# Set and Enforce a Minimum Wdith & Height by adjusting these two values 1024 x 600
# This will not change your overall project settings merely enforce a project settings minimum size 
var min_size = Vector2i(1024, 600)

func _ready():
	# Set the minimum size in project settings
	ProjectSettings.set_setting("display/window/size/min_width", min_size.x)
	ProjectSettings.set_setting("display/window/size/min_height", min_size.y)
	
	# Enforce the minimum window size using DisplayServer
	DisplayServer.window_set_min_size(min_size)

func _process(delta):
	# Get the current window size
	var current_size = DisplayServer.window_get_size()

	# Only enforce minimum size without adjusting the window size if it's above the min size
	if current_size.x < min_size.x or current_size.y < min_size.y:
		DisplayServer.window_set_size(Vector2i(max(current_size.x, min_size.x), max(current_size.y, min_size.y)))