Open documents in one of three ways instead of two bools, and let dialogs know when they take many files
DocumentOpen (open, new, or restored from an autosave) replaces isNew and isRecent and Manager::new_. The *_OPEN dialogs pick several files without being told.
This commit is contained in:
1 parent
1fc6b064ae
commit
228a15b5b3
13 files changed
+36
-31
No files matched your search
+2
-2
@@ -213,7 +213,7 @@ static int draw_run(const Arguments& arguments)
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string error;
|
std::string error;
|
||||||
Document document(arguments.file, false, &error);
|
Document document(arguments.file, DOCUMENT_OPEN, &error);
|
||||||
if (!document.is_valid())
|
if (!document.is_valid())
|
||||||
{
|
{
|
||||||
std::println(stderr, "could not load {}: {}", path_to_utf8(arguments.file), error);
|
std::println(stderr, "could not load {}: {}", path_to_utf8(arguments.file), error);
|
||||||
@@ -309,7 +309,7 @@ static int convert_run(const Arguments& arguments)
|
|||||||
if (format == "xml")
|
if (format == "xml")
|
||||||
gl.emplace();
|
gl.emplace();
|
||||||
std::string error;
|
std::string error;
|
||||||
Document document(arguments.file, false, &error);
|
Document document(arguments.file, DOCUMENT_OPEN, &error);
|
||||||
auto fileUtf8 = path_to_utf8(arguments.file);
|
auto fileUtf8 = path_to_utf8(arguments.file);
|
||||||
if (!document.is_valid())
|
if (!document.is_valid())
|
||||||
{
|
{
|
||||||
|
|||||||
+4
-2
@@ -42,14 +42,16 @@ Dialog::Dialog(SDL_Window* window)
|
|||||||
this->window = window;
|
this->window = window;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dialog::file_open(Type type, bool isMany)
|
// The *_OPEN dialogs pick any number of files. The others pick one.
|
||||||
|
void Dialog::file_open(Type type)
|
||||||
{
|
{
|
||||||
if (type == Dialog::NONE)
|
if (type == Dialog::NONE)
|
||||||
return;
|
return;
|
||||||
path.clear();
|
path.clear();
|
||||||
paths.clear();
|
paths.clear();
|
||||||
SDL_ShowOpenFileDialog(callback, this, window, FILTERS[TYPE_FILTERS[type]], std::size(FILTERS[TYPE_FILTERS[type]]),
|
SDL_ShowOpenFileDialog(callback, this, window, FILTERS[TYPE_FILTERS[type]], std::size(FILTERS[TYPE_FILTERS[type]]),
|
||||||
nullptr, isMany);
|
nullptr,
|
||||||
|
type == ANM2_OPEN || type == SOUND_OPEN || type == LIBRARY_OPEN || type == SPRITESHEET_OPEN);
|
||||||
this->type = type;
|
this->type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -87,7 +87,7 @@ public:
|
|||||||
|
|
||||||
Dialog() = default;
|
Dialog() = default;
|
||||||
Dialog(SDL_Window*);
|
Dialog(SDL_Window*);
|
||||||
void file_open(Type type, bool isMany = false);
|
void file_open(Type type);
|
||||||
void file_save(Type type, const std::filesystem::path& defaultPath = {});
|
void file_save(Type type, const std::filesystem::path& defaultPath = {});
|
||||||
void folder_open(Type type);
|
void folder_open(Type type);
|
||||||
bool is_selected(Type type) const;
|
bool is_selected(Type type) const;
|
||||||
|
|||||||
+2
-1
@@ -186,8 +186,9 @@ static void frame_time_sync(Document& document)
|
|||||||
document.frameTime = item && frameIndex >= 0 ? frame_time_from_index_get(*item, frameIndex) : 0.0f;
|
document.frameTime = item && frameIndex >= 0 ? frame_time_from_index_get(*item, frameIndex) : 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
Document::Document(const std::filesystem::path& path, bool isNew, std::string* errorString)
|
Document::Document(const std::filesystem::path& path, DocumentOpen mode, std::string* errorString)
|
||||||
{
|
{
|
||||||
|
auto isNew = mode == DOCUMENT_NEW;
|
||||||
tabId = document_tab_id_next();
|
tabId = document_tab_id_next();
|
||||||
model = model_make();
|
model = model_make();
|
||||||
if (!isNew)
|
if (!isNew)
|
||||||
|
|||||||
+10
-1
@@ -21,6 +21,15 @@
|
|||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
|
|
||||||
class Manager;
|
class Manager;
|
||||||
|
|
||||||
|
// How a document comes to be open: read from its file, made new there, or restored from an autosave (kept out of the
|
||||||
|
// recent files).
|
||||||
|
enum DocumentOpen
|
||||||
|
{
|
||||||
|
DOCUMENT_OPEN,
|
||||||
|
DOCUMENT_NEW,
|
||||||
|
DOCUMENT_RESTORE
|
||||||
|
};
|
||||||
struct Command;
|
struct Command;
|
||||||
|
|
||||||
// Ids and labels for a combo box of content items.
|
// Ids and labels for a combo box of content items.
|
||||||
@@ -116,7 +125,7 @@ public:
|
|||||||
EditTarget editTarget = EditTarget::NONE;
|
EditTarget editTarget = EditTarget::NONE;
|
||||||
UidIndex index = {};
|
UidIndex index = {};
|
||||||
|
|
||||||
Document(const std::filesystem::path&, bool = false, std::string* = nullptr);
|
Document(const std::filesystem::path&, DocumentOpen = DOCUMENT_OPEN, std::string* = nullptr);
|
||||||
Document(const Document&) = delete;
|
Document(const Document&) = delete;
|
||||||
Document& operator=(const Document&) = delete;
|
Document& operator=(const Document&) = delete;
|
||||||
Document(Document&&) = default;
|
Document(Document&&) = default;
|
||||||
|
|||||||
+4
-9
@@ -148,10 +148,10 @@ void Manager::commands_run()
|
|||||||
command.runManager(*this);
|
command.runManager(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Document* Manager::open(const std::filesystem::path& path, bool isNew, bool isRecent)
|
Document* Manager::open(const std::filesystem::path& path, DocumentOpen mode)
|
||||||
{
|
{
|
||||||
std::string errorString;
|
std::string errorString;
|
||||||
documents.emplace_back(path, isNew, &errorString);
|
documents.emplace_back(path, mode, &errorString);
|
||||||
auto pathString = path_to_utf8(path);
|
auto pathString = path_to_utf8(path);
|
||||||
|
|
||||||
auto& document = documents.back();
|
auto& document = documents.back();
|
||||||
@@ -162,7 +162,7 @@ Document* Manager::open(const std::filesystem::path& path, bool isNew, bool isRe
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isRecent)
|
if (mode != DOCUMENT_RESTORE)
|
||||||
recent_file_add(path);
|
recent_file_add(path);
|
||||||
|
|
||||||
selected = (int)documents.size() - 1;
|
selected = (int)documents.size() - 1;
|
||||||
@@ -176,11 +176,6 @@ Document* Manager::open(const std::filesystem::path& path, bool isNew, bool isRe
|
|||||||
return &document;
|
return &document;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Manager::new_(const std::filesystem::path& path)
|
|
||||||
{
|
|
||||||
open(path, true, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Manager::save(int index, const std::filesystem::path& path)
|
bool Manager::save(int index, const std::filesystem::path& path)
|
||||||
{
|
{
|
||||||
if (auto document = get(index); document)
|
if (auto document = get(index); document)
|
||||||
@@ -482,7 +477,7 @@ void Manager::autosave_files_open()
|
|||||||
{
|
{
|
||||||
for (auto& path : autosaveFiles)
|
for (auto& path : autosaveFiles)
|
||||||
{
|
{
|
||||||
if (auto document = open(path, false, false))
|
if (auto document = open(path, DOCUMENT_RESTORE))
|
||||||
{
|
{
|
||||||
document->isForceDirty = true;
|
document->isForceDirty = true;
|
||||||
document->path = document->path_from_autosave_get(path);
|
document->path = document->path_from_autosave_get(path);
|
||||||
|
|||||||
+1
-2
@@ -88,8 +88,7 @@ public:
|
|||||||
Document* get(int = -1);
|
Document* get(int = -1);
|
||||||
void command_push(Command);
|
void command_push(Command);
|
||||||
void commands_run();
|
void commands_run();
|
||||||
Document* open(const std::filesystem::path&, bool = false, bool = true);
|
Document* open(const std::filesystem::path&, DocumentOpen = DOCUMENT_OPEN);
|
||||||
void new_(const std::filesystem::path&);
|
|
||||||
bool save(int, const std::filesystem::path& = {});
|
bool save(int, const std::filesystem::path& = {});
|
||||||
bool save(const std::filesystem::path& = {});
|
bool save(const std::filesystem::path& = {});
|
||||||
void autosave(Document&);
|
void autosave(Document&);
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ void libraries_update(Panel& panel)
|
|||||||
actions.add(ACTION_OPEN, isOne, [&]() { open(*selection.begin()); }, TOOLTIP_LIBRARY_OPEN);
|
actions.add(ACTION_OPEN, isOne, [&]() { open(*selection.begin()); }, TOOLTIP_LIBRARY_OPEN);
|
||||||
actions.add(ACTION_OPEN_DIRECTORY, isOne,
|
actions.add(ACTION_OPEN_DIRECTORY, isOne,
|
||||||
[&]() { directory_open(dialog, document, item_get(libraries, *selection.begin())->path); });
|
[&]() { directory_open(dialog, document, item_get(libraries, *selection.begin())->path); });
|
||||||
actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::LIBRARY_OPEN, true); }, TOOLTIP_LIBRARY_ADD);
|
actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::LIBRARY_OPEN); }, TOOLTIP_LIBRARY_ADD);
|
||||||
content_remove_action_add(actions, panel, ElementType::LIBRARY, SelectionKind::LIBRARIES);
|
content_remove_action_add(actions, panel, ElementType::LIBRARY, SelectionKind::LIBRARIES);
|
||||||
actions.add(
|
actions.add(
|
||||||
ACTION_RELOAD, [selection]() { return !selection.empty(); },
|
ACTION_RELOAD, [selection]() { return !selection.empty(); },
|
||||||
|
|||||||
+1
-1
@@ -59,7 +59,7 @@ void sounds_update(Panel& panel)
|
|||||||
Actions actions = {};
|
Actions actions = {};
|
||||||
actions.add(ACTION_PLAY, isOne, [&]() { play(*selection.begin()); }, STRING_UNDEFINED, -1);
|
actions.add(ACTION_PLAY, isOne, [&]() { play(*selection.begin()); }, STRING_UNDEFINED, -1);
|
||||||
actions.add(ACTION_OPEN_DIRECTORY, isOne, [&]() { open(*selection.begin()); });
|
actions.add(ACTION_OPEN_DIRECTORY, isOne, [&]() { open(*selection.begin()); });
|
||||||
actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::SOUND_OPEN, true); }, TOOLTIP_SOUND_ADD);
|
actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::SOUND_OPEN); }, TOOLTIP_SOUND_ADD);
|
||||||
content_remove_action_add(actions, panel, ElementType::SOUND_ELEMENT, SelectionKind::SOUNDS);
|
content_remove_action_add(actions, panel, ElementType::SOUND_ELEMENT, SelectionKind::SOUNDS);
|
||||||
actions.add(
|
actions.add(
|
||||||
ACTION_RELOAD, [selection]() { return !selection.empty(); },
|
ACTION_RELOAD, [selection]() { return !selection.empty(); },
|
||||||
|
|||||||
@@ -241,8 +241,7 @@ void spritesheets_update(Panel& panel, SpritesheetsPanel& spritesheets)
|
|||||||
Actions actions = {};
|
Actions actions = {};
|
||||||
actions.add(ACTION_OPEN_DIRECTORY, isOne, [&]() { open(*selection.begin()); });
|
actions.add(ACTION_OPEN_DIRECTORY, isOne, [&]() { open(*selection.begin()); });
|
||||||
actions.add(ACTION_SET_FILE_PATH, isOne, [&]() { dialog.file_save(Dialog::SPRITESHEET_PATH_SET); });
|
actions.add(ACTION_SET_FILE_PATH, isOne, [&]() { dialog.file_save(Dialog::SPRITESHEET_PATH_SET); });
|
||||||
actions.add(
|
actions.add(ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::SPRITESHEET_OPEN); }, TOOLTIP_ADD_SPRITESHEET);
|
||||||
ACTION_ADD, {}, [&]() { dialog.file_open(Dialog::SPRITESHEET_OPEN, true); }, TOOLTIP_ADD_SPRITESHEET);
|
|
||||||
content_remove_action_add(actions, panel, ElementType::SPRITESHEET, SelectionKind::SPRITESHEETS);
|
content_remove_action_add(actions, panel, ElementType::SPRITESHEET, SelectionKind::SPRITESHEETS);
|
||||||
actions.add(
|
actions.add(
|
||||||
ACTION_RELOAD, isAny,
|
ACTION_RELOAD, isAny,
|
||||||
|
|||||||
+3
-3
@@ -90,7 +90,7 @@ void Taskbar::update(Manager& manager, Settings& settings, Resources& resources,
|
|||||||
if (ImGui::MenuItem(localize.get(BASIC_NEW), settings.shortcutNew.c_str()))
|
if (ImGui::MenuItem(localize.get(BASIC_NEW), settings.shortcutNew.c_str()))
|
||||||
dialog.file_save(Dialog::ANM2_CREATE);
|
dialog.file_save(Dialog::ANM2_CREATE);
|
||||||
if (ImGui::MenuItem(localize.get(BASIC_OPEN), settings.shortcutOpen.c_str()))
|
if (ImGui::MenuItem(localize.get(BASIC_OPEN), settings.shortcutOpen.c_str()))
|
||||||
dialog.file_open(Dialog::ANM2_OPEN, true);
|
dialog.file_open(Dialog::ANM2_OPEN);
|
||||||
|
|
||||||
auto recentFiles = manager.recent_files_ordered();
|
auto recentFiles = manager.recent_files_ordered();
|
||||||
if (ImGui::BeginMenu(localize.get(LABEL_OPEN_RECENT), !recentFiles.empty()))
|
if (ImGui::BeginMenu(localize.get(LABEL_OPEN_RECENT), !recentFiles.empty()))
|
||||||
@@ -134,7 +134,7 @@ void Taskbar::update(Manager& manager, Settings& settings, Resources& resources,
|
|||||||
if (dialog.is_selected(Dialog::ANM2_CREATE))
|
if (dialog.is_selected(Dialog::ANM2_CREATE))
|
||||||
{
|
{
|
||||||
auto path = dialog.path;
|
auto path = dialog.path;
|
||||||
manager.command_push({.runManager = [path](Manager& manager) { manager.new_(path); }});
|
manager.command_push({.runManager = [path](Manager& manager) { manager.open(path, DOCUMENT_NEW); }});
|
||||||
dialog.reset();
|
dialog.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -446,7 +446,7 @@ void Taskbar::update(Manager& manager, Settings& settings, Resources& resources,
|
|||||||
if (shortcut(manager.chords[SHORTCUT_NEW], SCOPE_GLOBAL))
|
if (shortcut(manager.chords[SHORTCUT_NEW], SCOPE_GLOBAL))
|
||||||
dialog.file_save(Dialog::ANM2_CREATE);
|
dialog.file_save(Dialog::ANM2_CREATE);
|
||||||
if (shortcut(manager.chords[SHORTCUT_OPEN], SCOPE_GLOBAL))
|
if (shortcut(manager.chords[SHORTCUT_OPEN], SCOPE_GLOBAL))
|
||||||
dialog.file_open(Dialog::ANM2_OPEN, true);
|
dialog.file_open(Dialog::ANM2_OPEN);
|
||||||
if (shortcut(manager.chords[SHORTCUT_SAVE], SCOPE_GLOBAL))
|
if (shortcut(manager.chords[SHORTCUT_SAVE], SCOPE_GLOBAL))
|
||||||
{
|
{
|
||||||
if (settings.fileIsWarnOverwrite)
|
if (settings.fileIsWarnOverwrite)
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ void welcome_update(Manager& manager, Resources& resources, Dialog& dialog, Task
|
|||||||
dialog.file_save(Dialog::ANM2_CREATE); // handled in taskbar.cpp
|
dialog.file_save(Dialog::ANM2_CREATE); // handled in taskbar.cpp
|
||||||
ImGui::SameLine();
|
ImGui::SameLine();
|
||||||
if (ImGui::Button(localize.get(BASIC_OPEN), widgetSize))
|
if (ImGui::Button(localize.get(BASIC_OPEN), widgetSize))
|
||||||
dialog.file_open(Dialog::ANM2_OPEN, true); // handled in taskbar.cpp
|
dialog.file_open(Dialog::ANM2_OPEN); // handled in taskbar.cpp
|
||||||
|
|
||||||
if (ImGui::BeginChild("##Recent Files Child", {}, ImGuiChildFlags_Borders))
|
if (ImGui::BeginChild("##Recent Files Child", {}, ImGuiChildFlags_Borders))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ struct RenderFixture
|
|||||||
if (!gl.is_valid())
|
if (!gl.is_valid())
|
||||||
return;
|
return;
|
||||||
std::filesystem::create_directories(OUTPUT_DIR);
|
std::filesystem::create_directories(OUTPUT_DIR);
|
||||||
document.emplace(RENDER_TEST_DIR / file, false);
|
document.emplace(RENDER_TEST_DIR / file);
|
||||||
auto& info = SHADERS[SHADER_TEXTURE];
|
auto& info = SHADERS[SHADER_TEXTURE];
|
||||||
shader.emplace(info.vertex, info.fragment);
|
shader.emplace(info.vertex, info.fragment);
|
||||||
canvas.emplace(Vec2(1.0f));
|
canvas.emplace(Vec2(1.0f));
|
||||||
@@ -210,7 +210,7 @@ TEST_CASE("converting SVG spritesheets writes PNGs beside them")
|
|||||||
for (auto name : {"vector.anm2", "vector.svg"})
|
for (auto name : {"vector.anm2", "vector.svg"})
|
||||||
std::filesystem::copy_file(RENDER_TEST_DIR / name, directory / name);
|
std::filesystem::copy_file(RENDER_TEST_DIR / name, directory / name);
|
||||||
|
|
||||||
Document document(directory / "vector.anm2", false);
|
Document document(directory / "vector.anm2");
|
||||||
REQUIRE(document.is_valid());
|
REQUIRE(document.is_valid());
|
||||||
document.spritesheets_vector_convert({0});
|
document.spritesheets_vector_convert({0});
|
||||||
CHECK(document.model.content.spritesheets.front().path == "vector.png");
|
CHECK(document.model.content.spritesheets.front().path == "vector.png");
|
||||||
@@ -242,7 +242,7 @@ TEST_CASE("a Rebirth save flattens animation layers for the game")
|
|||||||
"Path=\"library/spin.anm2\"");
|
"Path=\"library/spin.anm2\"");
|
||||||
std::ofstream(directory / "nested.anm2") << text;
|
std::ofstream(directory / "nested.anm2") << text;
|
||||||
|
|
||||||
Document document(directory / "nested.anm2", false);
|
Document document(directory / "nested.anm2");
|
||||||
REQUIRE(document.is_valid());
|
REQUIRE(document.is_valid());
|
||||||
REQUIRE(document.save(directory / "rebirth.anm2"));
|
REQUIRE(document.save(directory / "rebirth.anm2"));
|
||||||
std::ifstream file(directory / "rebirth.anm2");
|
std::ifstream file(directory / "rebirth.anm2");
|
||||||
@@ -278,7 +278,7 @@ TEST_CASE("the game's copy bakes skew and blur into a spritesheet that draws the
|
|||||||
"root": [ { "pos": [2, 3], "skew": [25, 10], "scale": [150, 100], "delay": 2 } ],
|
"root": [ { "pos": [2, 3], "skew": [25, 10], "scale": [150, 100], "delay": 2 } ],
|
||||||
"layers": [ { "layer": 1, "frames": [ { "crop": [16, 0], "size": [16, 16], "pivot": [8, 8], "delay": 2 } ] } ] } } })";
|
"layers": [ { "layer": 1, "frames": [ { "crop": [16, 0], "size": [16, 16], "pivot": [8, 8], "delay": 2 } ] } ] } } })";
|
||||||
|
|
||||||
Document document(directory / "bake.anm2", false);
|
Document document(directory / "bake.anm2");
|
||||||
REQUIRE(document.is_valid());
|
REQUIRE(document.is_valid());
|
||||||
REQUIRE(document.save(directory / "saved.anm2"));
|
REQUIRE(document.save(directory / "saved.anm2"));
|
||||||
CHECK(std::filesystem::exists(directory / "saved.baked.png"));
|
CHECK(std::filesystem::exists(directory / "saved.baked.png"));
|
||||||
@@ -291,7 +291,7 @@ TEST_CASE("the game's copy bakes skew and blur into a spritesheet that draws the
|
|||||||
CHECK(game.find("SkewX") == std::string::npos);
|
CHECK(game.find("SkewX") == std::string::npos);
|
||||||
CHECK(game.find("Blur") == std::string::npos);
|
CHECK(game.find("Blur") == std::string::npos);
|
||||||
std::ofstream(directory / "game.anm2") << game;
|
std::ofstream(directory / "game.anm2") << game;
|
||||||
Document gameDocument(directory / "game.anm2", false);
|
Document gameDocument(directory / "game.anm2");
|
||||||
REQUIRE(gameDocument.is_valid());
|
REQUIRE(gameDocument.is_valid());
|
||||||
|
|
||||||
auto& info = SHADERS[SHADER_TEXTURE];
|
auto& info = SHADERS[SHADER_TEXTURE];
|
||||||
|
|||||||
Reference in new issue
Block a user