Эх сурвалжийг харах

新增AlbeTree的操作集

maboren 2 сар өмнө
parent
commit
5d88f62a20

+ 108 - 0
Ability/Plugins/EzAbility/Source/EzAbilityEditor/Private/EzAbleTreeViewOperationMode.cpp

@@ -0,0 +1,108 @@
+// Fill out your copyright notice in the Description page of Project Settings.
+
+
+#include "EzAbleTreeViewOperationMode.h"
+#include "EzAbilityState.h"
+
+#define LOCTEXT_NAMESPACE "AbleTreeEditor"
+
+FEzAbleTreeViewOperationMode::FEzAbleTreeViewOperationMode()
+ : TreeDataWeak(nullptr)
+{
+	
+}
+
+
+FEzAbleTreeViewOperationMode::~FEzAbleTreeViewOperationMode()
+{
+	GEditor->UnregisterForUndo(this);
+
+	//UE::StateTree::Delegates::OnIdentifierChanged.RemoveAll(this);
+}
+
+void FEzAbleTreeViewOperationMode::Init(UEzAbilityEditorData* InTreeData)
+{
+	TreeDataWeak = InTreeData;
+
+	GEditor->RegisterForUndo(this);
+}
+
+void FEzAbleTreeViewOperationMode::PostUndo(bool bSuccess)
+{
+	// TODO: see if we can narrow this down.
+	//OnAssetChanged.Broadcast();
+}
+
+void FEzAbleTreeViewOperationMode::PostRedo(bool bSuccess)
+{
+	//OnAssetChanged.Broadcast();
+}
+
+void FEzAbleTreeViewOperationMode::AddState(UEzAbilityState* AfterState)
+{
+	UEzAbilityEditorData* TreeData = TreeDataWeak.Get();
+	if (TreeData == nullptr)
+	{
+		return;
+	}
+
+	const FScopedTransaction Transaction(LOCTEXT("AddStateTransaction", "Add State"));
+
+	UEzAbilityState* NewState = NewObject<UEzAbilityState>(TreeData, FName(), RF_Transactional);
+	UEzAbilityState* ParentState = nullptr;
+
+	if (AfterState == nullptr)
+	{
+		// If no subtrees, add a subtree, or add to the root state.
+		if (TreeData->SubTrees.IsEmpty())
+		{
+			TreeData->Modify();
+			TreeData->SubTrees.Add(NewState);
+		}
+		else
+		{
+			UEzAbilityState* RootState = TreeData->SubTrees[0];
+			if (ensureMsgf(RootState, TEXT("%s: Root state is empty."), *GetNameSafe(TreeData->GetOuter())))
+			{
+				RootState->Modify();
+				RootState->Children.Add(NewState);
+				NewState->Parent = RootState;
+				ParentState = RootState;
+			}
+		}
+	}
+	else
+	{
+		ParentState = AfterState->Parent;
+		if (ParentState != nullptr)
+		{
+			ParentState->Modify();
+		}
+		else
+		{
+			TreeData->Modify();
+		}
+
+		TArray<TObjectPtr<UEzAbilityState>>& ParentArray = ParentState ? ParentState->Children : TreeData->SubTrees;
+
+		const int32 TargetIndex = ParentArray.Find(AfterState);
+		if (TargetIndex != INDEX_NONE)
+		{
+			// Insert After
+			ParentArray.Insert(NewState, TargetIndex + 1);
+			NewState->Parent = ParentState;
+		}
+		else
+		{
+			// Fallback, should never happen.
+			ensureMsgf(false, TEXT("%s: Failed to find specified target state %s on state %s while adding new state."), *GetNameSafe(TreeData->GetOuter()), *GetNameSafe(AfterState), *GetNameSafe(ParentState));
+			ParentArray.Add(NewState);
+			NewState->Parent = ParentState;
+		}
+	}
+
+	OnStateAdded.Broadcast(ParentState, NewState);
+}
+
+
+#undef LOCTEXT_NAMESPACE

+ 38 - 0
Ability/Plugins/EzAbility/Source/EzAbilityEditor/Public/EzAbleTreeViewOperationMode.h

@@ -0,0 +1,38 @@
+// Fill out your copyright notice in the Description page of Project Settings.
+
+#pragma once
+
+#include "CoreMinimal.h"
+#include "UObject/NoExportTypes.h"
+#include "EditorUndoClient.h"
+#include "UObject/WeakObjectPtr.h"
+#include "EzAbilityEditorData.h"
+//#include "EzAbleTreeViewOperationMode.generated.h"
+
+/**
+ * 
+ */
+class EZABILITYEDITOR_API FEzAbleTreeViewOperationMode : public FEditorUndoClient , public TSharedFromThis<FEzAbleTreeViewOperationMode>
+{
+
+public:
+	
+	DECLARE_MULTICAST_DELEGATE_TwoParams(FOnStateAdded, UEzAbilityState* /*ParentState*/, UEzAbilityState* /*NewState*/);
+
+	FEzAbleTreeViewOperationMode();
+	virtual ~FEzAbleTreeViewOperationMode() override;
+
+	void Init(UEzAbilityEditorData* InTreeData);
+
+	virtual void PostUndo(bool bSuccess) override;
+	virtual void PostRedo(bool bSuccess) override;
+
+	void AddState(UEzAbilityState* AfterState);
+
+protected:
+
+	TWeakObjectPtr<UEzAbilityEditorData> TreeDataWeak;
+	TSet<TWeakObjectPtr<UEzAbilityState>> SelectedStates;
+
+	FOnStateAdded OnStateAdded;
+};