Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
8.9 kB
4
Indexable
Never
using { /UnrealEngine.com/Temporary/UI }
using { /Fortnite.com/UI }
using { /UnrealEngine.com/Temporary/SpatialMath }
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Colors }
using { /Verse.org/Colors/NamedColors }

operator'+'(M1:margin, M2:margin):margin =
    margin:
        Left := M1.Left + M2.Left
        Top := M1.Top + M2.Top
        Right := M1.Right + M2.Right
        Bottom := M1.Bottom + M2.Bottom

shadowed_text_block := class():
    TextBlock : text_block
    ShadowBlock : text_block

    SetText(InText:message):void =
        for(Block : array{TextBlock, ShadowBlock}):
            Block.SetText(InText)

ranged_float:= type{_X:float where 0.000000 <= _X, _X <= 1.000000}
MakeShadowedTextBlock<constructor>(Text: string, Color: color, ShadowColor: color, Opacity: ranged_float, ShadowOpacity: ranged_float) := shadowed_text_block:

    TextBlock := text_block:
        DefaultText := BuildMessage(Text)
        DefaultTextColor := Color
        DefaultTextOpacity := Opacity # Text opacity

    ShadowBlock := text_block:
        DefaultText := BuildMessage(Text)
        DefaultTextColor := ShadowColor
        DefaultTextOpacity := ShadowOpacity # Shadow opacity


ui_scoreboard := class():
    
    ############################################
    ############# CAN BE MDOFIED ###############
    ############################################
    TeamOneName : string = "RED"
    TeamTwoName : string = "BLUE"

    TeamOneColor : color = NamedColors.Red
    TeamTwoColor : color = NamedColors.Blue

    GlobalTopOffset      : float        = 40.0 # Distance between the first to the top of the screen
    LineHeightOffset     : margin       = margin{Top := 45.0} # Vertical distance between first and second line
    TeamOffsetFromCenter : float        = 150.0 # Horizontal distance between center of texts and center of screen

    TextOpacity          : ranged_float = 1.0
    ShadowOpacity        : ranged_float = 0.4
    ShadowColor          : color        = NamedColors.Black
    ShadowOffset         : margin       = margin{Top := 3.0, Left:= 2.0}





    var BuiltCanvas : [player]tuple(widget, shadowed_text_block, shadowed_text_block) = map{} 

    var TeamOneScore<private> : int = 0
    var TeamTwoScore<private> : int = 0
    
    IncrementScore(TeamIndex: type {I:int where I >= 0, I <= 1}, ?Value :int = 1):void =
        Print("Increment {TeamIndex} by {Value}")
        if(TeamIndex = 0):
            set TeamOneScore += Value
        else:
            set TeamTwoScore += Value

        RefreshHUD()
    

    RefreshHUD():void =
        for(Player -> __ : BuiltCanvas):
            RefreshHUD(Player)

    # Refreshes HUD, even if it's hidden upon call
    RefreshHUD(Player: player):void =
        PlayerCanvas := GetCanvas(Player)
        TeamOneScoreBlock := PlayerCanvas(1)
        TeamTwoScoreBlock := PlayerCanvas(2)

        TeamOneScoreBlock.SetText(BuildMessage("{TeamOneScore}"))
        TeamTwoScoreBlock.SetText(BuildMessage("{TeamTwoScore}"))



    Show(Agent: agent) : void =
        if (Player := player[Agent], PlayerUI := GetPlayerUI[Player]):

            # Remove canvas if already shown (so we can refresh it)
            Hide(Player)
            
            Canvas := GetCanvas(Player)
            RefreshHUD(Player)
            
            PlayerUI.AddWidget(Canvas(0))

    Hide(Agent: agent) : void =
        if (Player := player[Agent], PlayerUI := GetPlayerUI[Player], FoundCanvas := BuiltCanvas[Player]):
            PlayerUI.RemoveWidget(FoundCanvas(0))

            
    ShowAll() : void =
        for(Player -> __ : BuiltCanvas):
            Show(Player)

    HideAll() : void =
        for(Player -> __ : BuiltCanvas):
            Hide(Player)



    GetCanvas(Player: player) : tuple(widget, shadowed_text_block, shadowed_text_block) =
        if(ExistingCanvas := BuiltCanvas[Player]):
            return ExistingCanvas

        ShadowedTextBlock_TeamOne := MakeShadowedTextBlock(TeamOneName, TeamOneColor, ShadowColor, TextOpacity, ShadowOpacity)
        ShadowedTextBlock_VS := MakeShadowedTextBlock("VS", NamedColors.White, ShadowColor, TextOpacity, ShadowOpacity)
        ShadowedTextBlock_TeamTwo := MakeShadowedTextBlock(TeamTwoName, TeamTwoColor, ShadowColor, TextOpacity, ShadowOpacity)
        ShadowedTextBlock_ScoreOne := MakeShadowedTextBlock("{TeamOneScore}", NamedColors.White, TeamOneColor, TextOpacity, ShadowOpacity)
        ShadowedTextBlock_ScoreTwo := MakeShadowedTextBlock("{TeamTwoScore}", NamedColors.White, TeamTwoColor, TextOpacity, ShadowOpacity)

        Canvas : canvas = canvas:
            Slots := array:
                canvas_slot:
                    Anchors := anchors{Minimum := vector2{X:=0.5}, Maximum := vector2{X:=0.5}}
                    Offsets := margin{Top := GlobalTopOffset}
                    Alignment := vector2{X := 0.5}
                    SizeToContent := false
                    Widget := canvas:
                        Slots := array:
                            # VS
                            canvas_slot:
                                Offsets := ShadowOffset
                                Alignment := vector2{X := 0.5}
                                Widget := ShadowedTextBlock_VS.ShadowBlock
                            canvas_slot:
                                Alignment := vector2{X := 0.5}
                                Widget := ShadowedTextBlock_VS.TextBlock
                            # TEAM ONE
                            canvas_slot:
                                # Seems that this text_block doesn't have fixed bounds (thus Right offset won't work)
                                Offsets := margin{Left := -TeamOffsetFromCenter}
                                Alignment := vector2{X := 1.0}
                                SizeToContent := false
                                Widget := canvas:
                                    Slots := array:
                                        canvas_slot:
                                            Offsets := ShadowOffset
                                            Alignment := vector2{X := 0.5}
                                            Widget := ShadowedTextBlock_TeamOne.ShadowBlock
                                        canvas_slot:
                                            Alignment := vector2{X := 0.5}
                                            Widget := ShadowedTextBlock_TeamOne.TextBlock
                                        canvas_slot:
                                            Offsets := LineHeightOffset + ShadowOffset
                                            Alignment := vector2{X := 0.5}
                                            Widget := ShadowedTextBlock_ScoreOne.ShadowBlock
                                        canvas_slot:
                                            Offsets := LineHeightOffset
                                            Alignment := vector2{X := 0.5}
                                            Widget := ShadowedTextBlock_ScoreOne.TextBlock
                            # TEAM TWO
                            canvas_slot:
                                Offsets := margin{Left := TeamOffsetFromCenter}
                                Alignment := vector2{X := 0.0}
                                SizeToContent := false
                                Widget := canvas:
                                    Slots := array:
                                        canvas_slot:
                                            Offsets := ShadowOffset
                                            Alignment := vector2{X := 0.5}
                                            Widget := ShadowedTextBlock_TeamTwo.ShadowBlock
                                        canvas_slot:
                                            Alignment := vector2{X := 0.5}
                                            Widget := ShadowedTextBlock_TeamTwo.TextBlock
                                        canvas_slot:
                                            Offsets := LineHeightOffset + ShadowOffset
                                            Alignment := vector2{X := 0.5}
                                            Widget := ShadowedTextBlock_ScoreTwo.ShadowBlock
                                        canvas_slot:
                                            Offsets := LineHeightOffset
                                            Alignment := vector2{X := 0.5}
                                            Widget := ShadowedTextBlock_ScoreTwo.TextBlock

        _BuiltCanvas := (Canvas, ShadowedTextBlock_ScoreOne, ShadowedTextBlock_ScoreTwo)
        option{set BuiltCanvas[Player] = _BuiltCanvas}
        return _BuiltCanvas
Leave a Comment