Files
XCEngine/editor/src/Utils/UndoUtils.h

35 lines
967 B
C
Raw Normal View History

#pragma once
#include "Core/IEditorContext.h"
#include "Core/IUndoManager.h"
#include <type_traits>
#include <utility>
namespace XCEngine {
namespace Editor {
namespace UndoUtils {
template<typename Func>
auto ExecuteSceneCommand(IEditorContext& context, const std::string& label, Func&& func) {
auto& undoManager = context.GetUndoManager();
if (undoManager.HasPendingInteractiveChange()) {
undoManager.FinalizeInteractiveChange();
}
auto before = undoManager.CaptureCurrentState();
if constexpr (std::is_void_v<std::invoke_result_t<Func>>) {
std::forward<Func>(func)();
undoManager.PushCommand(label, std::move(before), undoManager.CaptureCurrentState());
} else {
auto result = std::forward<Func>(func)();
undoManager.PushCommand(label, std::move(before), undoManager.CaptureCurrentState());
return result;
}
}
} // namespace UndoUtils
} // namespace Editor
} // namespace XCEngine