Files
UnrealEngineUWP/Engine/Source/Developer/SessionFrontend/Private/Widgets/Console/SSessionConsoleShortcutWindow.cpp
Max Preussner 3aece47882 Docs: Removed file comments and added missing code documentation
Please note that file comments had no purpose in nearly all cases and just added visual clutter. The two files that had meaningful file comments had their comments moved into the corresponding classes. There are still hundreds of file comments left in other files that will be removed over time.

Also cleaned up some random stuff along the way:
- relative paths to public headers within the same module are no longer necessary (automatically discovered by UBT now)
- header guards are deprecated, use #pragma once instead (all compilers support it now)
- space between multiple template brackets is no longer required (all compilers support >> now)
- NULL to nullptr, OVERRIDE to override
- spelling errors, whitespace, line breaks

[CL 2104067 by Max Preussner in Main branch]
2014-06-12 23:22:18 -04:00

268 lines
7.6 KiB
C++

// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#include "SessionFrontendPrivatePCH.h"
#define LOCTEXT_NAMESPACE "SSessionConsoleShortcutWindow"
/* SSessionConsoleShortcutWindow interface
*****************************************************************************/
void SSessionConsoleShortcutWindow::AddShortcut( const FString& InName, const FString& InCommandString )
{
AddShortcutInternal(InName, InCommandString);
SaveShortcuts();
}
void SSessionConsoleShortcutWindow::Construct( const FArguments& InArgs )
{
OnCommandSubmitted = InArgs._OnCommandSubmitted;
ChildSlot
[
SNew(SVerticalBox)
+ SVerticalBox::Slot()
.FillHeight(1.0f)
.Padding(6.0f, 0.0f, 0.0f, 0.0f)
[
SAssignNew(ShortcutListView, SListView<TSharedPtr<FConsoleShortcutData>>)
.ItemHeight(24.0f)
.ListItemsSource(&Shortcuts)
.SelectionMode(ESelectionMode::None)
.OnGenerateRow(this, &SSessionConsoleShortcutWindow::HandleShortcutListViewGenerateRow)
.HeaderRow
(
SNew(SHeaderRow)
+ SHeaderRow::Column("Command")
.DefaultLabel(LOCTEXT("ShortcutHeaderText", "Shortcuts"))
)
]
];
LoadShortcuts();
RebuildUI();
}
/* SSessionConsoleShortcutWindow implementation
*****************************************************************************/
void SSessionConsoleShortcutWindow::AddShortcutInternal( const FString& InName, const FString& InCommandString )
{
TSharedPtr<FConsoleShortcutData> NewCommand = MakeShareable(new FConsoleShortcutData());
NewCommand->Name = InName;
NewCommand->Command = InCommandString;
Shortcuts.Add( NewCommand );
RebuildUI();
}
void SSessionConsoleShortcutWindow::EditShortcut( TSharedPtr<FConsoleShortcutData> InShortcut, bool bInEditCommand, FString InPromptTitle )
{
FString DefaultString = bInEditCommand ? InShortcut->Command : InShortcut->Name;
EditedShortcut = InShortcut;
bEditCommand = bInEditCommand;
TSharedRef<STextEntryPopup> TextEntry = SNew(STextEntryPopup)
.Label(InPromptTitle)
.DefaultText(FText::FromString(*DefaultString))
.OnTextCommitted(this, &SSessionConsoleShortcutWindow::HandleShortcutTextEntryCommitted)
.SelectAllTextWhenFocused(true)
.ClearKeyboardFocusOnCommit(false);
NameEntryPopupWindow = FSlateApplication::Get().PushMenu(
SharedThis(this),
TextEntry,
FSlateApplication::Get().GetCursorPos(),
FPopupTransitionEffect(FPopupTransitionEffect::TypeInPopup)
);
}
FString SSessionConsoleShortcutWindow::GetShortcutFilename( ) const
{
FString Filename = FPaths::EngineSavedDir() + TEXT("ConsoleShortcuts.txt");
return Filename;
}
void SSessionConsoleShortcutWindow::LoadShortcuts( )
{
//clear out list of commands
Shortcuts.Empty();
//read file
FString Content;
FFileHelper::LoadFileToString( Content, *GetShortcutFilename() );
TSharedPtr<FJsonObject> ShortcutStream;
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create( Content );
bool bResult = FJsonSerializer::Deserialize( Reader, ShortcutStream );
if (ShortcutStream.IsValid())
{
int32 CommandCount = ShortcutStream->GetNumberField(TEXT("Count"));
for (int32 i = 0; i < CommandCount; ++i)
{
FString Name = ShortcutStream->GetStringField(FString::Printf(TEXT("Shortcut.%d.Name"), i));
FString Command = ShortcutStream->GetStringField(FString::Printf(TEXT("Shortcut.%d.Command"), i));
AddShortcut(Name, Command);
}
}
}
void SSessionConsoleShortcutWindow::RebuildUI( )
{
ShortcutListView->RequestListRefresh();
SetVisibility(Shortcuts.Num() > 0 ? EVisibility::Visible : EVisibility::Collapsed);
}
void SSessionConsoleShortcutWindow::RemoveShortcut( TSharedPtr<FConsoleShortcutData> InShortcut )
{
Shortcuts.Remove(InShortcut);
RebuildUI();
}
void SSessionConsoleShortcutWindow::SaveShortcuts( ) const
{
TSharedPtr<FJsonObject> ShortcutStream = MakeShareable( new FJsonObject );
ShortcutStream->SetNumberField(TEXT("Count"), Shortcuts.Num());
for (int32 i = 0; i < Shortcuts.Num(); ++i)
{
ShortcutStream->SetStringField(FString::Printf(TEXT("Shortcut.%d.Name"), i), Shortcuts[i]->Name);
ShortcutStream->SetStringField(FString::Printf(TEXT("Shortcut.%d.Command"), i), Shortcuts[i]->Command);
}
FString Content;
TSharedRef< TJsonWriter<>> Writer = TJsonWriterFactory<>::Create(&Content);
FJsonSerializer::Serialize(ShortcutStream.ToSharedRef(), Writer);
FFileHelper::SaveStringToFile(*Content, *GetShortcutFilename());
}
/* SSessionConsoleShortcutWindow callbacks
*****************************************************************************/
FReply SSessionConsoleShortcutWindow::HandleExecuteButtonClicked( TSharedPtr<FConsoleShortcutData> InShortcut )
{
if (OnCommandSubmitted.IsBound())
{
OnCommandSubmitted.Execute(InShortcut->Command);
}
return FReply::Handled();
}
TSharedRef<ITableRow> SSessionConsoleShortcutWindow::HandleShortcutListViewGenerateRow(TSharedPtr<FConsoleShortcutData> InItem, const TSharedRef<STableViewBase>& OwnerTable)
{
FMenuBuilder ContextMenuBuilder(true, NULL);
ContextMenuBuilder.BeginSection("SessionConsoleShortcut");
{
ContextMenuBuilder.AddMenuEntry(
NSLOCTEXT("SessionFrontend", "ContextMenu.EditName", "Edit Name"),
FText(),
FSlateIcon(),
FUIAction(FExecuteAction::CreateSP(this, &SSessionConsoleShortcutWindow::EditShortcut, InItem, false, LOCTEXT("ShortcutOptionsEditNameTitle", "Name:").ToString()))
);
ContextMenuBuilder.AddMenuEntry(
NSLOCTEXT("SessionFrontend", "ContextMenu.EditCommand", "Edit Command"),
FText(),
FSlateIcon(),
FUIAction(FExecuteAction::CreateSP(this, &SSessionConsoleShortcutWindow::EditShortcut, InItem, true, LOCTEXT("ShortcutOptionsEditCommandTitle", "Command:").ToString()))
);
}
ContextMenuBuilder.EndSection();
ContextMenuBuilder.BeginSection("SessionConsoleShortcut2");
{
ContextMenuBuilder.AddMenuEntry(
NSLOCTEXT("SessionFrontend", "ContextMenu.DeleteCommand", "Delete Command"),
FText(),
FSlateIcon(),
FUIAction(FExecuteAction::CreateSP(this, &SSessionConsoleShortcutWindow::RemoveShortcut, InItem))
);
}
ContextMenuBuilder.EndSection();
return SNew(STableRow<TSharedPtr<FConsoleShortcutData>>, OwnerTable)
.Padding(2)
[
SNew(SHorizontalBox)
+ SHorizontalBox::Slot()
.FillWidth(1.0f)
.Padding(0)
[
// execute button
SNew(SButton)
.VAlign(VAlign_Center)
.ToolTipText(InItem->Command)
.OnClicked(FOnClicked::CreateSP(this, &SSessionConsoleShortcutWindow::HandleExecuteButtonClicked, InItem))
[
SNew(STextBlock)
.Text(InItem->Name)
]
]
+ SHorizontalBox::Slot()
.AutoWidth()
.HAlign(HAlign_Right)
.VAlign(VAlign_Center)
[
// edit options pull-down
SNew(SComboButton)
.ButtonStyle(FEditorStyle::Get(), "NoBorder")
.ForegroundColor(FEditorStyle::GetSlateColor("DefaultForeground"))
.ContentPadding(FMargin(6.f, 2.f))
.MenuContent()
[
ContextMenuBuilder.MakeWidget()
]
]
];
}
void SSessionConsoleShortcutWindow::HandleShortcutTextEntryCommitted( const FText& CommandText, ETextCommit::Type CommitInfo )
{
if (NameEntryPopupWindow.IsValid())
{
NameEntryPopupWindow->RequestDestroyWindow();
int32 IndexOfShortcut = Shortcuts.IndexOfByKey(EditedShortcut);
if (EditedShortcut.IsValid() && (IndexOfShortcut != INDEX_NONE))
{
//make a new version of the command so the list view knows to refresh
TSharedPtr<FConsoleShortcutData> NewCommand = MakeShareable(new FConsoleShortcutData());
*NewCommand = *EditedShortcut;
Shortcuts[IndexOfShortcut] = NewCommand;
FString& StringToChange = bEditCommand ? NewCommand->Command : NewCommand->Name;
StringToChange = CommandText.ToString();
}
EditedShortcut.Reset();
}
RebuildUI();
SaveShortcuts();
}
#undef LOCTEXT_NAMESPACE