Repository: Yukitty/godot-addon-integer_resolution_handler Branch: 3.X Commit: c67b1da84497 Files: 6 Total size: 7.6 KB Directory structure: gitextract_68lyncy5/ ├── .gitignore ├── LICENSE ├── README.md └── addons/ └── integer_resolution_handler/ ├── integer_resolution_handler.gd ├── plugin.cfg └── plugin.gd ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Godot-specific ignores .import/ export.cfg export_presets.cfg # Imported translations (automatically generated from CSV files) *.translation # Mono-specific ignores .mono/ data_*/ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2020 Yukita Mayako Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Integer Resolution Handler Alternative stretch handler for low resolution (pixel art) games in high resolution windows. Restricts the game resolution to integer steps, keeping pixels square. ## Usage 1. Enable the plugin. Close Project Settings. 2. Navigate Project Settings to the `display/window` category. 3. In the new section "Integer Resolution Handler", set Base Width and Base Height to your game's native pixel resolution. The IntegerResolutionHandler also works with all of the existing `stretch` settings, so fiddle there if you don't like how it behaves. Notably, setting `stretch/aspect` to "Keep" will enforce strict screen resolutions, while "Expand" will allow the viewable area to extend dramatically in all directions between scale steps. If you set Base Width and Base Height to a 4:3 aspect ratio and use the "Keep Height" or "Expand" aspect handling modes, your game will extend horizontally to support widescreen aspects as well. Just make sure your game is fully playable at its base resolution and GUI elements properly stretch and move, the same as you would for a non-pixel art game. ================================================ FILE: addons/integer_resolution_handler/integer_resolution_handler.gd ================================================ extends Node # IntegerResolutionHandler autoload. # Watches for window size changes and handles # game screen scaling with exact integer # multiples of a base resolution in mind. const SETTING_BASE_WIDTH = "display/window/integer_resolution_handler/base_width" const SETTING_BASE_HEIGHT = "display/window/integer_resolution_handler/base_height" var base_resolution := Vector2(320, 240) var stretch_mode: int var stretch_aspect: int onready var stretch_shrink: float = ProjectSettings.get_setting("display/window/stretch/shrink") onready var _root: Viewport = get_node("/root") func _ready(): # Parse project settings if ProjectSettings.has_setting(SETTING_BASE_WIDTH): base_resolution.x = ProjectSettings.get_setting(SETTING_BASE_WIDTH) if ProjectSettings.has_setting(SETTING_BASE_HEIGHT): base_resolution.y = ProjectSettings.get_setting(SETTING_BASE_HEIGHT) match ProjectSettings.get_setting("display/window/stretch/mode"): "2d": stretch_mode = SceneTree.STRETCH_MODE_2D "viewport": stretch_mode = SceneTree.STRETCH_MODE_VIEWPORT _: stretch_mode = SceneTree.STRETCH_MODE_DISABLED match ProjectSettings.get_setting("display/window/stretch/aspect"): "keep": stretch_aspect = SceneTree.STRETCH_ASPECT_KEEP "keep_height": stretch_aspect = SceneTree.STRETCH_ASPECT_KEEP_HEIGHT "keep_width": stretch_aspect = SceneTree.STRETCH_ASPECT_KEEP_WIDTH "expand": stretch_aspect = SceneTree.STRETCH_ASPECT_EXPAND _: stretch_aspect = SceneTree.STRETCH_ASPECT_IGNORE # Enforce minimum resolution. OS.min_window_size = base_resolution # Remove default stretch behavior. var tree: SceneTree = get_tree() tree.set_screen_stretch(SceneTree.STRETCH_MODE_DISABLED, SceneTree.STRETCH_ASPECT_IGNORE, base_resolution, 1) # Start tracking resolution changes and scaling the screen. update_resolution() # warning-ignore:return_value_discarded tree.connect("screen_resized", self, "update_resolution") func update_resolution(): var video_mode: Vector2 = OS.window_size if OS.window_fullscreen: video_mode = OS.get_screen_size() var scale := int(max(floor(min(video_mode.x / base_resolution.x, video_mode.y / base_resolution.y)), 1)) var screen_size: Vector2 = base_resolution var viewport_size: Vector2 = screen_size * scale var overscan: Vector2 = ((video_mode - viewport_size) / scale).floor() var margin: Vector2 var margin2: Vector2 match stretch_aspect: SceneTree.STRETCH_ASPECT_KEEP_WIDTH: screen_size.y += overscan.y SceneTree.STRETCH_ASPECT_KEEP_HEIGHT: screen_size.x += overscan.x SceneTree.STRETCH_ASPECT_EXPAND, SceneTree.STRETCH_ASPECT_IGNORE: screen_size += overscan viewport_size = screen_size * scale margin = (video_mode - viewport_size) / 2 margin2 = margin.ceil() margin = margin.floor() match stretch_mode: SceneTree.STRETCH_MODE_VIEWPORT: _root.set_size((screen_size / stretch_shrink).floor()) _root.set_attach_to_screen_rect(Rect2(margin, viewport_size)) _root.set_size_override_stretch(false) _root.set_size_override(false) SceneTree.STRETCH_MODE_2D, _: _root.set_size((viewport_size / stretch_shrink).floor()) _root.set_attach_to_screen_rect(Rect2(margin, viewport_size)) _root.set_size_override_stretch(true) _root.set_size_override(true, (screen_size / stretch_shrink).floor()) if margin.x < 0: margin.x = 0 if margin.y < 0: margin.y = 0 if margin2.x < 0: margin2.x = 0 if margin2.y < 0: margin2.y = 0 VisualServer.black_bars_set_margins(int(margin.x), int(margin.y), int(margin2.x), int(margin2.y)) ================================================ FILE: addons/integer_resolution_handler/plugin.cfg ================================================ [plugin] name="IntegerResolutionHandler" description="Alternative stretch handler for low resolution (pixel art) games in high resolution windows. Restricts the game resolution to integer steps, keeping pixels square." author="Yukitty" version="1.1.1" script="plugin.gd" ================================================ FILE: addons/integer_resolution_handler/plugin.gd ================================================ tool extends EditorPlugin const SETTING_BASE_WIDTH := "display/window/integer_resolution_handler/base_width" const SETTING_BASE_HEIGHT := "display/window/integer_resolution_handler/base_height" const DEFAULT_BASE_WIDTH: int = 320 const DEFAULT_BASE_HEIGHT: int = 240 func _enter_tree(): add_autoload_singleton("IntegerResolutionHandler", "res://addons/integer_resolution_handler/integer_resolution_handler.gd") if not ProjectSettings.has_setting(SETTING_BASE_WIDTH): ProjectSettings.set_setting(SETTING_BASE_WIDTH, DEFAULT_BASE_WIDTH) ProjectSettings.set_initial_value(SETTING_BASE_WIDTH, DEFAULT_BASE_WIDTH) ProjectSettings.add_property_info({ "name": SETTING_BASE_WIDTH, "type": TYPE_INT, "hint": PROPERTY_HINT_RANGE, "hint_string": "1,1024,1,or_greater" }) if not ProjectSettings.has_setting(SETTING_BASE_HEIGHT): ProjectSettings.set_setting(SETTING_BASE_HEIGHT, DEFAULT_BASE_HEIGHT) ProjectSettings.set_initial_value(SETTING_BASE_HEIGHT, DEFAULT_BASE_HEIGHT) ProjectSettings.add_property_info({ "name": SETTING_BASE_HEIGHT, "type": TYPE_INT, "hint": PROPERTY_HINT_RANGE, "hint_string": "1,600,1,or_greater" }) var order: int = ProjectSettings.get_order("display/window/size/width") - 2 ProjectSettings.set_order(SETTING_BASE_WIDTH, order) ProjectSettings.set_order(SETTING_BASE_HEIGHT, order + 1) ProjectSettings.save() func disable_plugin(): remove_autoload_singleton("IntegerResolutionHandler") ProjectSettings.clear("display/window/integer_resolution_handler/base_width") ProjectSettings.clear("display/window/integer_resolution_handler/base_height") ProjectSettings.save()