Download

Moveable/Resizable widget in Slate

Hey

I’m trying to make Resizable/Moveable widget.

While the moveable part works I can’t get it to resize.Changing Size() attribute causes some flicker but widget stays at the same size…

Any ideas, what might be wrong ?


// Fill out your copyright notice in the Description page of Project Settings.

#include "SDraggableWindowWidget.h"
#include "SlateOptMacros.h"

#include "Slate.h"
#include "SlateCore.h"
#include "SBorder.h"
#include "Widgets/Layout/SGridPanel.h"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SDraggableDesktopWidget::Construct(const FArguments& InArgs)
{
    Container = SNew(SOverlay);
    ChildSlot
    
        Container.ToSharedRef()
    ];
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION

void SDraggableDesktopWidget::AddWindow(TSharedPtr<class SDraggableWindowWidget> InWindow)
{
    if (!Container.IsValid())
        return;

    Container->AddSlot()
    .HAlign(EHorizontalAlignment::HAlign_Fill)
    .VAlign(EVerticalAlignment::VAlign_Fill)
    
        InWindow.ToSharedRef()
    ];
}

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SDraggableWindowWidget::Construct(const FArguments& InArgs)
{
    CurrentSize = FVector2D(500, 100);
    FSimpleDelegate OnPressedDel = FSimpleDelegate::CreateSP(this, &SDraggableWindowWidget::OnPressed);
    FSimpleDelegate OnReleasedDel = FSimpleDelegate::CreateSP(this, &SDraggableWindowWidget::OnReleased);
    TAttribute<FVector2D> posAttr = TAttribute<FVector2D>::Create(TAttribute<FVector2D>::FGetter::CreateSP(this, &SDraggableWindowWidget::GetPosition));
    TAttribute<FVector2D> sizeAttr = TAttribute<FVector2D>::Create(TAttribute<FVector2D>::FGetter::CreateSP(this, &SDraggableWindowWidget::GetSize));


    FSimpleDelegate OnHorizontalPressedDel = FSimpleDelegate::CreateSP(this, &SDraggableWindowWidget::OnHorizontalResizePressed);
    FSimpleDelegate OnHorizontalReleasedDel = FSimpleDelegate::CreateSP(this, &SDraggableWindowWidget::OnHorizontalResizeReleased);
    FSimpleDelegate OnHorizontalLeftPressedDel = FSimpleDelegate::CreateSP(this, &SDraggableWindowWidget::OnHorizontalResizeLeftPressed);
    FSimpleDelegate OnHorizontalLeftReleasedDel = FSimpleDelegate::CreateSP(this, &SDraggableWindowWidget::OnHorizontalResizeLeftReleased);

    CurrentHeight = 200;
    CurrentWidth = 200;

    bDragging = false;
    DragStartPosition = FVector2D::ZeroVector;
    DragCurrentPosition = FVector2D::ZeroVector;

    ChildSlot
    
        SNew(SCanvas)
        + SCanvas::Slot()
        .HAlign(EHorizontalAlignment::HAlign_Left)
        .VAlign(EVerticalAlignment::VAlign_Top)
        .Position(posAttr)
        .Size(sizeAttr)
        
            SNew(SGridPanel)

            +SGridPanel::Slot(0, 0)
            
                SNew(SBox)
                .HeightOverride(3)
                .WidthOverride(3)
                    
                    SNew(SButton)
                ]
            ]
            + SGridPanel::Slot(1 ,0)
            
                SNew(SBox)
                .HeightOverride(3)
                
                    SNew(SButton)
                ]
            ]
            + SGridPanel::Slot(2, 0)
            
                SNew(SBox)
                .HeightOverride(3)
                .WidthOverride(3)
                
                    SNew(SButton)
                ]
            ]
            + SGridPanel::Slot(0, 1)
            
                SNew(SBox)
                .WidthOverride(3)
                
                    SNew(SButton)
                    .OnPressed(OnHorizontalLeftPressedDel)
                    .OnReleased(OnHorizontalLeftReleasedDel)
                ]
            ]
            + SGridPanel::Slot(1, 1)
            .HAlign(EHorizontalAlignment::HAlign_Fill)
            .VAlign(EVerticalAlignment::VAlign_Fill)
            
                SNew(SBox)
                .HAlign(EHorizontalAlignment::HAlign_Fill)
                .VAlign(EVerticalAlignment::VAlign_Fill)
                
                    SNew(SVerticalBox)
                    + SVerticalBox::Slot()
                    .FillHeight(0.2f)
                    
                        SNew(SHorizontalBox)
                        + SHorizontalBox::Slot()
                        
                            SNew(SButton)
                            .OnPressed(OnPressedDel)
                            .OnReleased(OnReleasedDel)
                        ]
                    ]
                    + SVerticalBox::Slot()
                    .FillHeight(0.8f)
                    
                        SAssignNew(Content, SOverlay)
                    ]
                ]

            ]
            + SGridPanel::Slot(2, 1)
            
                SNew(SBox)
                .WidthOverride(3)
                
                    SNew(SButton)
                    .OnPressed(OnHorizontalPressedDel)
                    .OnReleased(OnHorizontalReleasedDel)
                ]
            ]
            + SGridPanel::Slot(0, 2)
            
                SNew(SBox)
                .HeightOverride(3)
                .WidthOverride(3)
                
                    SNew(SButton)
                ]
            ]
            + SGridPanel::Slot(1, 2)
            
                SNew(SBox)
                .HeightOverride(3)
                
                    SNew(SButton)
                ]
            ]
            + SGridPanel::Slot(2, 2)
            
                SNew(SBox)
                .HeightOverride(3)
                .WidthOverride(3)
                
                    SNew(SButton)
                ]
            ]
        ]
        // Populate the widget
    ];

    Content->AddSlot()
        
            SNew(SButton)
            
                SNew(STextBlock)
                .Text(this, &SDraggableWindowWidget::GetCurrentX)
            ]
        ];

}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION
FVector2D SDraggableWindowWidget::ComputeDesiredSize(float) const
{
    return SCompoundWidget::ComputeDesiredSize(1);
    //return FVector2D(CurrentWidth, CurrentHeight);
}
void SDraggableWindowWidget::Tick(const FGeometry& AllottedGeometry, const double InCurrentTime, const float InDeltaTime)
{
    SCompoundWidget::Tick(AllottedGeometry, InCurrentTime, InDeltaTime);
    if (bDragging)
    {
        FVector2D locaPos = AllottedGeometry.AbsoluteToLocal(FSlateApplicationBase::Get().GetCursorPos());
        CurrentCursorPosition = locaPos;
    }
    if(ResizingState == EDDWResizing::HrozintalRight)
    {
        //if(ContentBox.IsValid())
        {
            FVector2D localStartPos = AllottedGeometry.AbsoluteToLocal(DragStartPosition);
            DragCurrentPosition = AllottedGeometry.AbsoluteToLocal(FSlateApplicationBase::Get().GetCursorPos());

            FVector2D AddOffset = DragCurrentPosition - CurrentSize.X;

            if(FMath::Abs(AddOffset.X) != FMath::Abs(LastX))
            {
                CurrentX = CurrentSize.X - DragCurrentPosition.X;

                CurrentSize.X -= CurrentX;
                CurrentWidth = CurrentWidth + CurrentX;
                LastX = AddOffset.X;
            }
        }
    }
    else if (ResizingState == EDDWResizing::HrozintalLeft)
    {
    }

}
void SDraggableWindowWidget::OnPressed()
{
    bDragging = true;
    //CurrentCursorPosition = FSlateApplication::Get().GetCursorPos();
}
void SDraggableWindowWidget::OnReleased()
{
    bDragging = false;
}

void SDraggableWindowWidget::OnHorizontalResizePressed()
{
    ResizingState = EDDWResizing::HrozintalRight;
    DragStartPosition = FSlateApplicationBase::Get().GetCursorPos();
}

void SDraggableWindowWidget::OnHorizontalResizeReleased()
{
    ResizingState = EDDWResizing::NoResize;
}
void SDraggableWindowWidget::OnHorizontalResizeLeftPressed()
{
    ResizingState = EDDWResizing::HrozintalLeft;
}
void SDraggableWindowWidget::OnHorizontalResizeLeftReleased()
{
    ResizingState = EDDWResizing::NoResize;
}
void SDraggableWindowWidget::OnVerticalResizePressed()
{
}

void SDraggableWindowWidget::OnVerticalResizeReleased()
{
}

void SDraggableWindowWidget::OnDirectionalResizePressed()
{
}

void SDraggableWindowWidget::OnDirectionalResizeReleased()
{
}

FVector2D SDraggableWindowWidget::GetPosition() const
{
    return CurrentCursorPosition;
}
FVector2D SDraggableWindowWidget::GetSize() const
{
    return CurrentSize;
}
FOptionalSize SDraggableWindowWidget::GetHeight() const
{
    return CurrentHeight;
}
FOptionalSize SDraggableWindowWidget::GetWidth() const
{
    return CurrentWidth;
}
FText SDraggableWindowWidget::GetCurrentX() const
{
    FString ret = FString("Right") + FString::FormatAsNumber(CurrentSize.X) + FString("
")
        + FString("CurrentX ") + FString::FormatAsNumber(CurrentX) + FString("
");
    return FText::FromString(ret);
}
/code