新建一个平台相关的打开和保存文件的工具¶
#pragma once
#include <string>
namespace CryDust {
class FileDialogs
{
public:
// These return empty strings if cancelled
static std::string OpenFile(const char* filter);
static std::string SaveFile(const char* filter);
};
}
对应的WindowPlayformUtils(使用win窗口)¶
#include "cdpch.h"
#include "CryDust/Utils/PlatformUtils.h"
#include <commdlg.h>
#include <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#include "CryDust/Core/Application.h"
namespace CryDust {
std::string FileDialogs::OpenFile(const char* filter)
{
OPENFILENAMEA ofn;
CHAR szFile[260] = { 0 };
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = glfwGetWin32Window((GLFWwindow*)Application::Get().GetWindow().GetNativeWindow());
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = filter;
ofn.nFilterIndex = 1;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
if (GetOpenFileNameA(&ofn) == TRUE)
{
return ofn.lpstrFile;
}
return std::string();
}
std::string FileDialogs::SaveFile(const char* filter)
{
OPENFILENAMEA ofn;
CHAR szFile[260] = { 0 };
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = glfwGetWin32Window((GLFWwindow*)Application::Get().GetWindow().GetNativeWindow());
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = filter;
ofn.nFilterIndex = 1;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
if (GetSaveFileNameA(&ofn) == TRUE)
{
return ofn.lpstrFile;
}
return std::string();
}
}
EditorLayer¶
场景保存
private:
bool OnKeyPressed(KeyPressedEvent& e);
void NewScene();
void OpenScene();
void SaveSceneAs();
场景函数¶
创建:构造、设置场景,设置上下文 打开:读路径,判断不为空,创建,设置上下文,反序列化 保存:读路径,判断不为空,序列化
void EditorLayer::NewScene()
{
m_ActiveScene = CreateRef<Scene>();
m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);
m_SceneHierarchyPanel.SetContext(m_ActiveScene);
}
void EditorLayer::OpenScene()
{
std::string filepath = FileDialogs::OpenFile("Hazel Scene (*.hazel)\0*.hazel\0");
if (!filepath.empty())
{
m_ActiveScene = CreateRef<Scene>();
m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);
m_SceneHierarchyPanel.SetContext(m_ActiveScene);
SceneSerializer serializer(m_ActiveScene);
serializer.Deserialize(filepath);
}
}
void EditorLayer::SaveSceneAs()
{
std::string filepath = FileDialogs::SaveFile("Hazel Scene (*.hazel)\0*.hazel\0");
if (!filepath.empty())
{
SceneSerializer serializer(m_ActiveScene);
serializer.Serialize(filepath);
}
}
快捷键设置¶
ctrl+n创建,ctrl+o打开,ctrl+shift+s保存
void EditorLayer::OnEvent(CryDust::Event& e)
{
m_CameraController.OnEvent(e);
EventDispatcher dispatcher(e);
dispatcher.Dispatch<KeyPressedEvent>(CD_BIND_EVENT_FN(EditorLayer::OnKeyPressed));
}
bool EditorLayer::OnKeyPressed(KeyPressedEvent& e)
{
// Shortcuts
if (e.GetRepeatCount() > 0)
return false;
bool control = Input::IsKeyPressed(Key::LeftControl) || Input::IsKeyPressed(Key::RightControl);
bool shift = Input::IsKeyPressed(Key::LeftShift) || Input::IsKeyPressed(Key::RightShift);
switch (e.GetKeyCode())
{
case Key::N:
{
if (control)
NewScene();
break;
}
case Key::O:
{
if (control)
OpenScene();
break;
}
case Key::S:
{
if (control && shift)
SaveSceneAs();
break;
}
}
}
扩展后缀¶
// 更强力的保存:扩展后缀
ofn.lpstrDefExt = strchr(filter, '\0') + 1;