Skip to content

Commit

Permalink
Fix the way screen resizing works, closes #79, maybe #80 too
Browse files Browse the repository at this point in the history
  • Loading branch information
bojidar-bg committed May 5, 2016
1 parent 70fb553 commit ca74fb8
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 59 deletions.
2 changes: 1 addition & 1 deletion engine.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ version_build=""

config="res://shared/config.gd"
SaveManager="*res://shared/save_manager.gd"
ScreenManager="res://shared/screen_manager.gd"
ScreenManager="*res://shared/screen_manager.gd"
ScenesManager="*res://shared/scenes_manager.gd"
SettingsManager="*res://shared/settings_manager.gd"
FileManager="*res://shared/file_manager.gd"
Expand Down
2 changes: 2 additions & 0 deletions main/gui.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ onready var level_holder = get_node("../level_holder")
onready var player = get_node("../player_holder/player")

func _ready():
ScreenManager.set_minimum_size(Vector2(0, 0))

var nodes_left = get_node("popup/popup_node/body/container").get_children()

# Removes the focus from the buttons
Expand Down
2 changes: 2 additions & 0 deletions main/level_holder.gd
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func load_level(pack, level): # Load level from pack

func window_resize():
var new_size = get_node("/root").get_size_override()
if !level_node:
return
var tilemap = level_node.get_node("tilemap")
for i in range(ceil(new_size.x/2/64)):
tilemap.set_cell(tile_map_acid_x_start - i, tile_map_acid_y, 2)
Expand Down
3 changes: 2 additions & 1 deletion main/main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ size_flags/vertical = 2
margin/left = 8.0
margin/top = 8.0
margin/right = 8.0
margin/bottom = 68.0
margin/bottom = 76.0
alignment = 0
__meta__ = { "_editor_collapsed":true }

Expand Down Expand Up @@ -799,6 +799,7 @@ transform/pos = Vector2( 32, 32 )
[node name="player" parent="player_holder" instance=ExtResource( 16 )]

transform/pos = Vector2( 228.814, 197.081 )
collision/margin = 0.08

[node name="sample_player" type="SamplePlayer" parent="."]

Expand Down
40 changes: 29 additions & 11 deletions menu/menu.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ func _ready():
if node.has_node("back"):
node.get_node("back").connect("pressed", self, "go_to_target", ["start"])

# Screen size
var minimum_size = get_minimum_size()
for child in get_children():
if child extends Control:
var child_minimum_size = child.get_minimum_size()
minimum_size.x = max(minimum_size.x, child_minimum_size.x)
minimum_size.y = max(minimum_size.y, child_minimum_size.y)
ScreenManager.set_minimum_size(minimum_size)

# Splash fadeout
if ScenesManager.is_first_load:
get_node("initial_splash/animation_player").play("SplashFade")
get_node("animation_player").play("SplashFade")

# Prepare to move thing when the aspect ratio changes
connect("resized", self, "reposition_screens")
Expand All @@ -38,7 +47,6 @@ func reposition_screens():

if size == old_size:
return

old_size = size

levels.set_margin(MARGIN_LEFT, size.x)
Expand All @@ -50,15 +58,21 @@ func reposition_screens():
credits.set_margin(MARGIN_TOP, size.y)
credits.set_margin(MARGIN_BOTTOM, -size.y)

var scale = size.x/1024
if scale > 1:
var initial_size = ScreenManager.get_original_size()
var scale_vector = size / initial_size
var scale = max(scale_vector.x, scale_vector.y)

if scale > 1:
get_node("background_layer").set_scale(Vector2(scale,scale))
get_node("background_layer").set_offset(Vector2(0,-(scale*768-768)))
get_node("initial_splash").set_scale(Vector2(scale,scale))
get_node("initial_splash").set_offset(Vector2(0,-(scale*768-768)/2))
go_to_target(current_screen)
get_node("initial_splash").set_offset((size - initial_size) * Vector2(0.5, 0))
if scale_vector.y / scale_vector.x < 1:
get_node("background_layer").set_offset(Vector2(0, (1 - scale_vector.x / scale_vector.y) * initial_size.y))
else:
get_node("background_layer").set_offset(Vector2(0, 0))
tween.remove_all()
go_to_target(current_screen, false)

func go_to_target(var screen = "start"):
func go_to_target(screen = "start", animate = true):
current_screen = screen

var target_coordinates = Vector2(0, 0) # By default, use the 0, 0 coordinates
Expand All @@ -70,8 +84,12 @@ func go_to_target(var screen = "start"):
var time = distance/screen_move_speed

if time > 0:
tween.interpolate_property(self, "rect/pos", current_coordinates, -target_coordinates, time, Tween.TRANS_EXPO, Tween.EASE_OUT, 0)
tween.start()
tween.remove_all()
if animate:
tween.interpolate_property(self, "rect/pos", current_coordinates, -target_coordinates, time, Tween.TRANS_EXPO, Tween.EASE_OUT, 0)
tween.start()
else:
set("rect/pos", -target_coordinates)

func quit():
get_tree().quit() # Exit the game
74 changes: 39 additions & 35 deletions menu/menu.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ tracks/0/keys = { "cont":true, "times":FloatArray( 0, 8 ), "transitions":FloatAr

[sub_resource type="Animation" id=2]

resource/name = "SplashFade"
length = 2.0
loop = false
step = 0.1
tracks/0/type = "value"
tracks/0/path = NodePath("image:visibility/opacity")
tracks/0/interp = 1
tracks/0/keys = { "cont":true, "times":FloatArray( 0, 1, 2 ), "transitions":FloatArray( 1, 1, 1 ), "values":[ 1.0, 1.0, 0.0 ] }

[sub_resource type="Animation" id=3]

length = 1.0
loop = true
step = 0.1
Expand All @@ -51,6 +40,20 @@ tracks/1/path = NodePath(".:transform/scale")
tracks/1/interp = 1
tracks/1/keys = { "cont":true, "times":FloatArray( 0, 0.5 ), "transitions":FloatArray( 1, 1 ), "values":[ Vector2( 1, 1 ), Vector2( 1.2, 1.2 ) ] }

[sub_resource type="Animation" id=3]

length = 2.0
loop = false
step = 0.1
tracks/0/type = "value"
tracks/0/path = NodePath("initial_splash/image:visibility/opacity")
tracks/0/interp = 1
tracks/0/keys = { "cont":true, "times":FloatArray( 0, 1, 2 ), "transitions":FloatArray( 1, 1, 1 ), "values":[ 1.0, 1.0, 0.0 ] }
tracks/1/type = "value"
tracks/1/path = NodePath("background_layer/background:visibility/opacity")
tracks/1/interp = 1
tracks/1/keys = { "cont":true, "times":FloatArray( 0, 2 ), "transitions":FloatArray( 1, 1 ), "values":[ 0.0, 1.0 ] }

[node name="menu" type="Control"]

anchor/right = 1
Expand Down Expand Up @@ -117,17 +120,6 @@ margin/bottom = 768.0
texture = ExtResource( 4 )
expand = true

[node name="animation_player" type="AnimationPlayer" parent="initial_splash"]

playback/process_mode = 1
playback/default_blend_time = 0.0
root/root = NodePath("..")
anims/SplashFade = SubResource( 2 )
playback/active = true
playback/speed = 3.0
blend_times = [ ]
autoplay = ""

[node name="main" type="VBoxContainer" parent="."]

anchor/right = 1
Expand All @@ -139,7 +131,7 @@ size_flags/horizontal = 3
size_flags/vertical = 3
margin/left = 0.0
margin/top = 16.0
margin/right = 0.0
margin/right = -4.0
margin/bottom = 0.0
custom_constants/separation = 20
alignment = 0
Expand All @@ -151,17 +143,18 @@ focus/stop_mouse = true
size_flags/horizontal = 1
size_flags/vertical = 1
size_flags/stretch_ratio = 3.0
margin/left = 198.0
margin/left = 200.0
margin/top = 26.0
margin/right = 825.0
margin/right = 827.0
margin/bottom = 179.0
texture = ExtResource( 5 )
__meta__ = { "_editor_collapsed":true }

[node name="splash" type="Node2D" parent="main/logo"]

transform/pos = Vector2( 618.213, 157.035 )
transform/pos = Vector2( 602.573, 151.655 )
transform/rot = 15.0502
transform/scale = Vector2( 1, 1 )
z/z = 4
script/script = ExtResource( 6 )
__meta__ = { "_editor_collapsed":true }
Expand Down Expand Up @@ -194,7 +187,7 @@ max_lines_visible = -1
playback/process_mode = 1
playback/default_blend_time = 0.0
root/root = NodePath("..")
anims/loop = SubResource( 3 )
anims/loop = SubResource( 2 )
playback/active = true
playback/speed = 0.2
blend_times = [ ]
Expand All @@ -220,9 +213,9 @@ focus/stop_mouse = false
size_flags/horizontal = 1
size_flags/vertical = 1
size_flags/stretch_ratio = 3.0
margin/left = 312.0
margin/left = 314.0
margin/top = 226.0
margin/right = 712.0
margin/right = 714.0
margin/bottom = 526.0
custom_constants/vseparation = 10
custom_constants/hseparation = 10
Expand Down Expand Up @@ -294,7 +287,7 @@ size_flags/vertical = 1
size_flags/stretch_ratio = 2.0
margin/left = 0.0
margin/top = 564.0
margin/right = 1024.0
margin/right = 1028.0
margin/bottom = 664.0
custom_constants/separation = 40
alignment = 1
Expand All @@ -307,9 +300,9 @@ focus/ignore_mouse = true
focus/stop_mouse = true
size_flags/horizontal = 2
size_flags/vertical = 1
margin/left = 237.0
margin/left = 239.0
margin/top = 0.0
margin/right = 514.0
margin/right = 516.0
margin/bottom = 100.0
texture = ExtResource( 7 )
expand = true
Expand All @@ -321,9 +314,9 @@ focus/ignore_mouse = true
focus/stop_mouse = true
size_flags/horizontal = 2
size_flags/vertical = 2
margin/left = 554.0
margin/left = 556.0
margin/top = 0.0
margin/right = 787.0
margin/right = 789.0
margin/bottom = 100.0
texture = ExtResource( 8 )
expand = true
Expand All @@ -336,7 +329,7 @@ focus/stop_mouse = true
size_flags/horizontal = 2
margin/left = 0.0
margin/top = 703.0
margin/right = 1024.0
margin/right = 1028.0
margin/bottom = 751.0
custom_colors/font_color = Color( 0, 0, 0, 1 )
text = "© 2015-2016 KOBUGE Games - kobuge-games.github.io/minilens"
Expand Down Expand Up @@ -711,4 +704,15 @@ playback/active = false
playback/repeat = false
playback/speed = 1.0

[node name="animation_player" type="AnimationPlayer" parent="."]

playback/process_mode = 1
playback/default_blend_time = 0.0
root/root = NodePath("..")
anims/SplashFade = SubResource( 3 )
playback/active = true
playback/speed = 3.0
blend_times = [ ]
autoplay = ""


34 changes: 23 additions & 11 deletions shared/screen_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,36 @@ extends Node
onready var viewport = get_viewport()
onready var initial_size = viewport.get_rect().size

var minimum_size = Vector2(0, 0)

func _ready():
if Globals.has("display/width"):
initial_size.x = Globals.get("display/width")
if Globals.has("display/height"):
initial_size.y = Globals.get("display/height")

viewport.connect("size_changed", self, "window_resize")
window_resize()

func set_minimum_size(s):
if minimum_size != s:
minimum_size = s
window_resize()

func get_original_size():
return initial_size

func window_resize():
var current_size = OS.get_window_size()

var changed = false
if current_size.x < 100:
current_size.x = 100
changed = true
if current_size.y < 100:
current_size.y = 100
changed = true

if changed:
OS.set_window_size(current_size)

var scale_factor = initial_size.y/current_size.y
var new_size = Vector2(current_size.x*scale_factor, initial_size.y)

if new_size.y < minimum_size.y:
scale_factor = minimum_size.y/new_size.y
new_size = Vector2(new_size.x*scale_factor, minimum_size.y)
if new_size.x < minimum_size.x:
scale_factor = minimum_size.x/new_size.x
new_size = Vector2(minimum_size.x, new_size.y*scale_factor)

viewport.set_size_override(true, new_size)

0 comments on commit ca74fb8

Please sign in to comment.