└── addons └── scaling_canvas_layer ├── LICENSE.md ├── plugin.cfg ├── plugin.gd ├── scaling_canvas_layer.gd ├── scaling_canvas_layer.svg └── scaling_canvas_layer.svg.import /addons/scaling_canvas_layer/LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright © 2021 Nicholas Yang 4 | Copyright © 2021 BarelyAliveMau5 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /addons/scaling_canvas_layer/plugin.cfg: -------------------------------------------------------------------------------- 1 | [plugin] 2 | 3 | name="Scaling CanvasLayer" 4 | description="This Godot plugin provides a custom CanvasLayer that scales the viewport contents to always cover the full window with no black bars, no content reveal, and no distortions. Child CanvasItem nodes (i.e., UI) can also be scaled, independently of the content." 5 | author="Nicholas Yang (nyanginator)" 6 | version="1.0.0" 7 | script="plugin.gd" 8 | -------------------------------------------------------------------------------- /addons/scaling_canvas_layer/plugin.gd: -------------------------------------------------------------------------------- 1 | # Copyright © 2021 Nicholas Yang and contributors - MIT License 2 | # See `LICENSE.md` included in the source distribution for details. 3 | 4 | tool 5 | extends EditorPlugin 6 | 7 | 8 | func _enter_tree(): 9 | add_custom_type( 10 | "ScalingCanvasLayer", 11 | "CanvasLayer", 12 | preload("scaling_canvas_layer.gd"), 13 | preload("scaling_canvas_layer.svg") 14 | ) 15 | 16 | 17 | func _exit_tree(): 18 | remove_custom_type("ScalingCanvasLayer") 19 | -------------------------------------------------------------------------------- /addons/scaling_canvas_layer/scaling_canvas_layer.gd: -------------------------------------------------------------------------------- 1 | # Copyright © 2021 Nicholas Yang and contributors - MIT License 2 | # See `LICENSE.md` included in the source distribution for details. 3 | 4 | extends CanvasLayer 5 | 6 | # The shrink ratio to add on top of scaled content and UI. 7 | export (float) var stretch_shrink := 1.0 8 | 9 | # Toggles content scaling on/off. 10 | export (bool) var scale_content := true 11 | 12 | # Ideal resolution for the content. 13 | export (Vector2) var content_desired_resolution := Vector2.ZERO 14 | 15 | # Path to the Camera2D that will be used for content scaling. 16 | export (NodePath) var camera_path 17 | 18 | # Toggles UI scaling on/off. 19 | export (bool) var scale_ui := true 20 | 21 | # Ideal resolution for the UI. 22 | export (Vector2) var ui_desired_resolution := Vector2.ZERO 23 | 24 | # Factor that determines how much to scale UI by width and how much by height 25 | # (0.0 = width, 1.0 = height). 26 | export (float, 0.0, 1.0) var ui_width_height_factor := 0.5 27 | 28 | # Reference to camera object. 29 | var _camera: Camera2D = null 30 | 31 | 32 | func _ready(): 33 | # Check for a valid camera. 34 | if camera_path != null and has_node(camera_path): 35 | _camera = get_node(camera_path) 36 | if not _camera is Camera2D: 37 | _camera = null 38 | 39 | if not _camera == null: 40 | # Camera must be active in order for results to be visible. 41 | _camera.current = true 42 | else: 43 | # Cannot scale content if there is no camera assigned. 44 | scale_content = false 45 | 46 | # If the desired resolution is not set, refer to Project Settings > Display > Window. 47 | if content_desired_resolution == Vector2.ZERO: 48 | content_desired_resolution = Vector2( 49 | ProjectSettings.get_setting("display/window/size/width"), 50 | ProjectSettings.get_setting("display/window/size/height") 51 | ) 52 | if ui_desired_resolution == Vector2.ZERO: 53 | ui_desired_resolution = Vector2( 54 | ProjectSettings.get_setting("display/window/size/width"), 55 | ProjectSettings.get_setting("display/window/size/height") 56 | ) 57 | 58 | # Initial call and setup. 59 | _do_scaling() 60 | get_tree().connect("screen_resized", self, "_do_scaling") 61 | 62 | 63 | func _do_scaling() -> void: 64 | var window_size := OS.get_window_size() 65 | 66 | if scale_content or scale_ui: 67 | # Disable's Godot's stretch but apply the given shrink ratio. 68 | get_tree().set_screen_stretch(0, 0, Vector2.ZERO, stretch_shrink) 69 | 70 | if scale_content: 71 | # We want a "Mode: expand" effect (basically "Keep Width" combined with "Keep Height") 72 | # to prevent black bars from appearing. If the window is taller than the desired aspect, 73 | # we scale everything according to the width (i.e., "Keep Width", or expand vertically). 74 | # If the window is wider, we scale according to the height (i.e., "Keep Height", or expand 75 | # horizontally). Afterwards, to remove content reveal, we scale the result by a correction 76 | # factor, the percent difference between the current and desired aspects. 77 | var desired_aspect := content_desired_resolution.aspect() 78 | var current_aspect := window_size.aspect() 79 | var black_bars_scale := 1.0 80 | var content_reveal_scale := 1.0 81 | 82 | # Window is taller than the desired resolution. 83 | if current_aspect < desired_aspect: 84 | black_bars_scale = content_desired_resolution.x / window_size.x 85 | content_reveal_scale = current_aspect / desired_aspect 86 | 87 | # Window is wider than the desired resolution. 88 | else: 89 | black_bars_scale = content_desired_resolution.y / window_size.y 90 | content_reveal_scale = desired_aspect / current_aspect 91 | 92 | # Scale by adjusting zoom, which allows pixel fractions and thus cannot be pixel-pefect. 93 | if not _camera == null: 94 | # Calculate the scaling factor. When it is less than 1, the camera zooms in. 95 | # When it is greater than 1, the camera zooms out. 96 | var content_scale := black_bars_scale * content_reveal_scale 97 | 98 | _camera.zoom = Vector2.ONE * content_scale 99 | 100 | if scale_ui: 101 | # Get weighted ratios of the current window size to the desired resolution. 102 | var width_ratio := window_size.x / ui_desired_resolution.x 103 | var height_ratio := window_size.y / ui_desired_resolution.y 104 | var width_weight := 1.0 - ui_width_height_factor 105 | var height_weight := ui_width_height_factor 106 | 107 | # Calculate the amount to scale the UI (i.e., this CanvasLayer's scale). 108 | var ui_scale := (width_ratio * width_weight) + (height_ratio * height_weight) 109 | 110 | scale = Vector2.ONE * ui_scale 111 | -------------------------------------------------------------------------------- /addons/scaling_canvas_layer/scaling_canvas_layer.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /addons/scaling_canvas_layer/scaling_canvas_layer.svg.import: -------------------------------------------------------------------------------- 1 | [remap] 2 | 3 | importer="texture" 4 | type="StreamTexture" 5 | path="res://.import/scaling_canvas_layer.svg-f0306e93d4e3599699d63088fe347237.stex" 6 | metadata={ 7 | "vram_texture": false 8 | } 9 | 10 | [deps] 11 | 12 | source_file="res://addons/scaling_canvas_layer/scaling_canvas_layer.svg" 13 | dest_files=[ "res://.import/scaling_canvas_layer.svg-f0306e93d4e3599699d63088fe347237.stex" ] 14 | 15 | [params] 16 | 17 | compress/mode=0 18 | compress/lossy_quality=0.7 19 | compress/hdr_mode=0 20 | compress/bptc_ldr=0 21 | compress/normal_map=0 22 | flags/repeat=0 23 | flags/filter=true 24 | flags/mipmaps=false 25 | flags/anisotropic=false 26 | flags/srgb=2 27 | process/fix_alpha_border=true 28 | process/premult_alpha=false 29 | process/HDR_as_SRGB=false 30 | process/invert_color=false 31 | stream=false 32 | size_limit=0 33 | detect_3d=true 34 | svg/scale=1.0 35 | --------------------------------------------------------------------------------