From 2ea9e52dd7405d28a5acf6c5af6f8d4411aec85f Mon Sep 17 00:00:00 2001 From: William Tumeo Date: Mon, 3 Oct 2016 20:56:45 -0300 Subject: [PATCH 1/2] Add split-screen modes (horizontal/vertical) --- 2d/split_screen_platformer/split_stage.gd | 51 ++++++++++++++++++--- 2d/split_screen_platformer/split_stage.tscn | 1 + 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/2d/split_screen_platformer/split_stage.gd b/2d/split_screen_platformer/split_stage.gd index 62d58cc5..50715375 100644 --- a/2d/split_screen_platformer/split_stage.gd +++ b/2d/split_screen_platformer/split_stage.gd @@ -1,8 +1,17 @@ extends Control -# class member variables go here, for example: -# var a = 2 -# var b = "textvar" +#export split screen modes +export(int, "None", "Horizontal", "Vertical") var split = 1 +var split_mode = -1 + +#split modes constants +const SPLIT_NONE = 0 +const SPLIT_HORIZONTAL = 1 +const SPLIT_VERTICAL = 2 + +#top and bottom controls +onready var top = get_node("top") +onready var bottom = get_node("bottom") func _ready(): #make bottom viewport have the same world2D as the top viewport, so both show the same @@ -17,6 +26,36 @@ func _ready(): #simple and alternatively, copy them to the other viewport, but they must be erased when level is unloaded #get_node("bottom/viewport").add_child( get_node("top/viewport/stage/parallax_bg").duplicate() ) - # Called every time the node is added to the scene. - # Initialization here - pass + #set split screen mode for the first time + set_split_mode(split) + #if used inside _process/_fixed_process, the mode can be made chageable by inspector/animation key + + +func set_split_mode(mode): + #change mode only if "split_mode" is different + if mode != split_mode and mode in [0, 1, 2]: + #get game screen size + var screen = get_viewport().get_rect().size + + #changing sizes and positions + if mode == SPLIT_HORIZONTAL: + top.set_size(Vector2(screen.x, screen.y/2)) + top.set_pos(Vector2(0, 0)) + bottom.set_size(Vector2(screen.x, screen.y/2)) + bottom.set_pos(Vector2(0, screen.y/2)) + + elif mode == SPLIT_VERTICAL: + top.set_size(Vector2(screen.x/2, screen.y)) + top.set_pos(Vector2(0, 0)) + bottom.set_size(Vector2(screen.x/2, screen.y)) + bottom.set_pos(Vector2(screen.x/2, 0)) + + elif mode == SPLIT_NONE: + top.set_size(Vector2(screen.x, screen.y)) + top.set_pos(Vector2(0, 0)) + bottom.set_hidden(true) + + if split_mode == SPLIT_NONE: + bottom.set_hidden(false) + #apply mode + split_mode = mode diff --git a/2d/split_screen_platformer/split_stage.tscn b/2d/split_screen_platformer/split_stage.tscn index e333d2d4..5470b0d9 100644 --- a/2d/split_screen_platformer/split_stage.tscn +++ b/2d/split_screen_platformer/split_stage.tscn @@ -16,6 +16,7 @@ margin/top = 0.0 margin/right = 0.0 margin/bottom = 0.0 script/script = ExtResource( 1 ) +split = 1 [node name="top" type="Control" parent="."] From c6a09b1a7e1c6340f6800a4fe2d81a5a500c6b6f Mon Sep 17 00:00:00 2001 From: William Tumeo Date: Sat, 3 Dec 2016 16:03:32 -0200 Subject: [PATCH 2/2] Add setget method and tool mode --- 2d/split_screen_platformer/split_stage.gd | 65 ++++++++++++--------- 2d/split_screen_platformer/split_stage.tscn | 2 +- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/2d/split_screen_platformer/split_stage.gd b/2d/split_screen_platformer/split_stage.gd index 50715375..fc634d8b 100644 --- a/2d/split_screen_platformer/split_stage.gd +++ b/2d/split_screen_platformer/split_stage.gd @@ -1,17 +1,18 @@ +tool extends Control #export split screen modes -export(int, "None", "Horizontal", "Vertical") var split = 1 -var split_mode = -1 +#if used with setget, the mode can be made chageable by inspector/animation key +export(int, "None", "Horizontal", "Vertical") var split_mode = 1 setget set_split_mode + +#other split_mode variable to check previous mode on split_mode set method +var _split_mode = -1 #split modes constants const SPLIT_NONE = 0 const SPLIT_HORIZONTAL = 1 const SPLIT_VERTICAL = 2 -#top and bottom controls -onready var top = get_node("top") -onready var bottom = get_node("bottom") func _ready(): #make bottom viewport have the same world2D as the top viewport, so both show the same @@ -26,36 +27,42 @@ func _ready(): #simple and alternatively, copy them to the other viewport, but they must be erased when level is unloaded #get_node("bottom/viewport").add_child( get_node("top/viewport/stage/parallax_bg").duplicate() ) - #set split screen mode for the first time - set_split_mode(split) - #if used inside _process/_fixed_process, the mode can be made chageable by inspector/animation key + #setting split mode for the first time if node is ready + set_split_mode(split_mode) func set_split_mode(mode): - #change mode only if "split_mode" is different - if mode != split_mode and mode in [0, 1, 2]: + split_mode = mode + if get_viewport() != null: + #apply mode + _set_split_mode() + + +func _set_split_mode(): + #change mode only if "_split_mode" is different + if _split_mode != split_mode: #get game screen size - var screen = get_viewport().get_rect().size + var screen = get_size() #changing sizes and positions - if mode == SPLIT_HORIZONTAL: - top.set_size(Vector2(screen.x, screen.y/2)) - top.set_pos(Vector2(0, 0)) - bottom.set_size(Vector2(screen.x, screen.y/2)) - bottom.set_pos(Vector2(0, screen.y/2)) + if split_mode == SPLIT_HORIZONTAL: + get_node("top").set_size(Vector2(screen.x, screen.y/2)) + get_node("top").set_pos(Vector2(0, 0)) + get_node("bottom").set_size(Vector2(screen.x, screen.y/2)) + get_node("bottom").set_pos(Vector2(0, screen.y/2)) - elif mode == SPLIT_VERTICAL: - top.set_size(Vector2(screen.x/2, screen.y)) - top.set_pos(Vector2(0, 0)) - bottom.set_size(Vector2(screen.x/2, screen.y)) - bottom.set_pos(Vector2(screen.x/2, 0)) + elif split_mode == SPLIT_VERTICAL: + get_node("top").set_size(Vector2(screen.x/2, screen.y)) + get_node("top").set_pos(Vector2(0, 0)) + get_node("bottom").set_size(Vector2(screen.x/2, screen.y)) + get_node("bottom").set_pos(Vector2(screen.x/2, 0)) - elif mode == SPLIT_NONE: - top.set_size(Vector2(screen.x, screen.y)) - top.set_pos(Vector2(0, 0)) - bottom.set_hidden(true) + elif split_mode == SPLIT_NONE: + get_node("top").set_size(Vector2(screen.x, screen.y)) + get_node("top").set_pos(Vector2(0, 0)) + get_node("bottom").set_hidden(true) - if split_mode == SPLIT_NONE: - bottom.set_hidden(false) - #apply mode - split_mode = mode + if _split_mode == SPLIT_NONE: + get_node("bottom").set_hidden(false) + + _split_mode = split_mode diff --git a/2d/split_screen_platformer/split_stage.tscn b/2d/split_screen_platformer/split_stage.tscn index 5470b0d9..3a832763 100644 --- a/2d/split_screen_platformer/split_stage.tscn +++ b/2d/split_screen_platformer/split_stage.tscn @@ -16,7 +16,7 @@ margin/top = 0.0 margin/right = 0.0 margin/bottom = 0.0 script/script = ExtResource( 1 ) -split = 1 +split_mode = 1 [node name="top" type="Control" parent="."]