Skip to content

Added option SplitScreenBorderWidth on the viewport to add thick borders between player viewports in splitscreen #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 85 additions & 11 deletions Source/GameBaseFramework/Private/Engine/GBFGameViewportClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,83 @@ FGBFViewPortPlayerOffset::FGBFViewPortPlayerOffset() :
{
}

UGBFGameViewportClient::UGBFGameViewportClient()
UGBFGameViewportClient::UGBFGameViewportClient() :
SplitScreenBorderWidth( 0 )
{
}

void UGBFGameViewportClient::LayoutPlayers()
{
Super::LayoutPlayers();

if ( !SplitScreenLayoutOffset.IsValid() )
if ( SplitScreenBorderWidth == 0 && !SplitScreenLayoutOffset.IsValid() )
{
return;
}

FVector2D viewport_size;
GetViewportSize( viewport_size );
const auto current_split = GetCurrentSplitscreenConfiguration();

const auto & player_list = GetOuterUEngine()->GetGamePlayers( this );
const auto player_count = player_list.Num();

int top_left_x = viewport_size.X * SplitScreenLayoutOffset.TopLeftX;
int top_left_y = viewport_size.Y * SplitScreenLayoutOffset.TopLeftY;
int bottom_right_x = viewport_size.X * SplitScreenLayoutOffset.BottomRightX;
int bottom_right_y = viewport_size.Y * SplitScreenLayoutOffset.BottomRightY;

FVector2D border_negative_offset( 0.0f );

switch ( current_split )
{
case ESplitScreenType::None:
{
}
break;
case ESplitScreenType::TwoPlayer_Horizontal:
case ESplitScreenType::ThreePlayer_Horizontal:
case ESplitScreenType::FourPlayer_Horizontal:
{
border_negative_offset.X = SplitScreenBorderWidth * ( player_count - 1 );
}
break;
case ESplitScreenType::TwoPlayer_Vertical:
case ESplitScreenType::ThreePlayer_Vertical:
case ESplitScreenType::FourPlayer_Vertical:
{
border_negative_offset.Y = SplitScreenBorderWidth * ( player_count - 1 );
}
break;
case ESplitScreenType::ThreePlayer_FavorTop:
case ESplitScreenType::ThreePlayer_FavorBottom:
case ESplitScreenType::FourPlayer_Grid:
{
border_negative_offset.X = SplitScreenBorderWidth;
border_negative_offset.Y = SplitScreenBorderWidth;
}
break;
case ESplitScreenType::SplitTypeCount:
{
checkNoEntry()
}
break;
default:
{
}
}

const FVector2D new_viewport_size(
viewport_size.X - top_left_x - bottom_right_x,
viewport_size.Y - top_left_y - bottom_right_y );
viewport_size.X - top_left_x - bottom_right_x - border_negative_offset.X,
viewport_size.Y - top_left_y - bottom_right_y - border_negative_offset.Y );

const auto viewport_ratio = new_viewport_size / viewport_size;
const FVector2D origin_offset(
static_cast< float >( top_left_x ) / viewport_size.X,
static_cast< float >( top_left_y ) / viewport_size.Y );

const auto & player_list = GetOuterUEngine()->GetGamePlayers( this );
const auto player_count = player_list.Num();
const auto current_split = GetCurrentSplitscreenConfiguration();
const FVector2D half_border_offset(
static_cast< float >( border_negative_offset.X ) / viewport_size.X,
static_cast< float >( border_negative_offset.Y ) / viewport_size.Y );

for ( auto * player : player_list )
{
Expand All @@ -67,6 +111,11 @@ void UGBFGameViewportClient::LayoutPlayers()
{
player->Origin.X = origin_offset.X;
player->Origin.Y = viewport_ratio.Y / player_count * player_index + origin_offset.Y;

if ( player_index > 0 )
{
player->Origin.Y += half_border_offset.Y;
}
}
break;
case ESplitScreenType::TwoPlayer_Vertical:
Expand All @@ -75,6 +124,11 @@ void UGBFGameViewportClient::LayoutPlayers()
{
player->Origin.X = viewport_ratio.X / player_count * player_index + origin_offset.X;
player->Origin.Y = origin_offset.Y;

if ( player_index > 0 )
{
player->Origin.X += half_border_offset.X;
}
}
break;
case ESplitScreenType::ThreePlayer_FavorTop:
Expand All @@ -86,7 +140,12 @@ void UGBFGameViewportClient::LayoutPlayers()
else
{
player->Origin.X = viewport_ratio.X / 2 * ( player_index - 1 ) + origin_offset.X;
player->Origin.Y = viewport_ratio.Y / 2 + origin_offset.Y;
player->Origin.Y = viewport_ratio.Y / 2 + origin_offset.Y + half_border_offset.Y;

if ( player_index == 2 )
{
player->Origin.X += half_border_offset.X;
}
}
}
break;
Expand All @@ -95,12 +154,17 @@ void UGBFGameViewportClient::LayoutPlayers()
if ( player_index == 2 )
{
player->Origin.X = origin_offset.X;
player->Origin.Y = viewport_ratio.Y / 2 + origin_offset.Y;
player->Origin.Y = viewport_ratio.Y / 2 + origin_offset.Y + half_border_offset.Y;
}
else
{
player->Origin.X = viewport_ratio.X / 2 * player_index + origin_offset.X;
player->Origin.Y = origin_offset.Y;

if ( player_index == 1 )
{
player->Origin.X += half_border_offset.X;
}
}
}
break;
Expand All @@ -114,7 +178,12 @@ void UGBFGameViewportClient::LayoutPlayers()
else
{
player->Origin.X = viewport_ratio.X / 2 * ( player_index - 2 ) + origin_offset.X;
player->Origin.Y = viewport_ratio.Y / 2 + origin_offset.Y;
player->Origin.Y = viewport_ratio.Y / 2 + origin_offset.Y + half_border_offset.Y;
}

if ( player_index % 2 != 0 )
{
player->Origin.X += half_border_offset.X;
}
}
break;
Expand Down Expand Up @@ -150,3 +219,8 @@ void UGBFGameViewportClient::RemapControllerInput( FInputKeyEventArgs & key_even
GEngine->RemapGamepadControllerIdForPIE( this, key_event.ControllerId );
}
}

void UGBFGameViewportClient::SetSplitScreenBorderWidth( int border_width )
{
SplitScreenBorderWidth = FMath::Max( 0.0f, border_width );
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ void UGBFLocalMultiplayerSubsystem::SetForceDisableSplitscreen( bool disable )
}
}

void UGBFLocalMultiplayerSubsystem::SetSplitScreenBorderWidth( int width )
{
if ( const auto * world = GetWorld() )
{
if ( auto * viewport = Cast< UGBFGameViewportClient >( world->GetGameViewport() ) )
{
viewport->SetSplitScreenBorderWidth( width );
}
}
}

void UGBFLocalMultiplayerSubsystem::Tick( float delta_time )
{
Super::Tick( delta_time );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ class GAMEBASEFRAMEWORK_API UGBFGameViewportClient : public UCommonGameViewportC
const FGBFViewPortPlayerOffset & GetSplitScreenLayoutOffset() const;
void LayoutPlayers() override;
void RemapControllerInput( FInputKeyEventArgs & key_event ) override;
void SetSplitScreenBorderWidth( int border_width );

private:
FGBFViewPortPlayerOffset SplitScreenLayoutOffset;
int SplitScreenBorderWidth;
};

FORCEINLINE void UGBFGameViewportClient::SetSplitScreenLayoutOffset( const FGBFViewPortPlayerOffset & offset )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class GAMEBASEFRAMEWORK_API UGBFLocalMultiplayerSubsystem final : public UTickab
UFUNCTION( BlueprintCallable )
void SetForceDisableSplitscreen( bool disable );

UFUNCTION( BlueprintCallable )
void SetSplitScreenBorderWidth( int width );

void Tick( float delta_time ) override;
TStatId GetStatId() const override;

Expand Down
Loading