Skip to content

Commit

Permalink
Replace filesystem utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Mar 17, 2024
1 parent eb0cce4 commit 0c4234e
Show file tree
Hide file tree
Showing 24 changed files with 349 additions and 394 deletions.
148 changes: 38 additions & 110 deletions source/app/handlers/menu_event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <centurion/system.hpp>
#include <entt/signal/dispatcher.hpp>

#include "common/util/filesystem.hpp"
#include "handlers/event_handlers.hpp"
#include "model/event/all.hpp"
#include "model/file_history.hpp"
#include "model/model.hpp"
#include "model/settings.hpp"
#include "runtime/app_context.hpp"
#include "tactile/core/platform/filesystem.hpp"
#include "ui/dialog/about_dialog.hpp"
#include "ui/dialog/credits_dialog.hpp"
#include "ui/dialog/godot_export_dialog.hpp"
Expand All @@ -30,47 +30,31 @@ void dispatch_menu_action(const MenuAction action)
auto& settings = get_settings();

switch (action) {
case MenuAction::NewMap:
dispatcher.enqueue<ShowNewMapDialogEvent>();
break;
case MenuAction::NewMap: dispatcher.enqueue<ShowNewMapDialogEvent>(); break;

case MenuAction::OpenMap:
dispatcher.enqueue<ShowOpenMapDialogEvent>();
break;
case MenuAction::OpenMap: dispatcher.enqueue<ShowOpenMapDialogEvent>(); break;

case MenuAction::Save:
dispatcher.enqueue<SaveEvent>();
break;
case MenuAction::Save: dispatcher.enqueue<SaveEvent>(); break;

case MenuAction::SaveAs:
dispatcher.enqueue<OpenSaveAsDialogEvent>();
break;
case MenuAction::SaveAs: dispatcher.enqueue<OpenSaveAsDialogEvent>(); break;

case MenuAction::CloseDocument:
dispatcher.enqueue<CloseDocumentEvent>(model.get_active_document_id().value());
break;

case MenuAction::Quit:
dispatcher.enqueue<QuitEvent>();
break;
case MenuAction::Quit: dispatcher.enqueue<QuitEvent>(); break;

case MenuAction::ReopenLastClosedFile: {
// TODO this will need to be tweaked if tileset documents viewing will be supported
Path file_path {get_file_history().last_closed_file.value()};
dispatcher.enqueue<OpenMapEvent>(std::move(file_path));
break;
}
case MenuAction::ClearFileHistory:
clear_file_history();
break;
case MenuAction::ClearFileHistory: clear_file_history(); break;

case MenuAction::Undo:
dispatcher.enqueue<UndoEvent>();
break;
case MenuAction::Undo: dispatcher.enqueue<UndoEvent>(); break;

case MenuAction::Redo:
dispatcher.enqueue<RedoEvent>();
break;
case MenuAction::Redo: dispatcher.enqueue<RedoEvent>(); break;

case MenuAction::StampTool:
dispatcher.enqueue<SelectToolEvent>(ToolType::Stamp);
Expand Down Expand Up @@ -100,131 +84,75 @@ void dispatch_menu_action(const MenuAction action)
dispatcher.enqueue<SelectToolEvent>(ToolType::Point);
break;

case MenuAction::ComponentEditor:
ui::open_component_editor_dialog(model);
break;
case MenuAction::ComponentEditor: ui::open_component_editor_dialog(model); break;

case MenuAction::OpenSettings:
ui::open_settings_dialog();
break;
case MenuAction::OpenSettings: ui::open_settings_dialog(); break;

case MenuAction::CenterViewport:
dispatcher.enqueue<CenterViewportEvent>();
break;
case MenuAction::CenterViewport: dispatcher.enqueue<CenterViewportEvent>(); break;

case MenuAction::ToggleGrid:
dispatcher.enqueue<ToggleGridEvent>();
break;
case MenuAction::ToggleGrid: dispatcher.enqueue<ToggleGridEvent>(); break;

case MenuAction::IncreaseZoom:
dispatcher.enqueue<IncreaseZoomEvent>();
break;
case MenuAction::IncreaseZoom: dispatcher.enqueue<IncreaseZoomEvent>(); break;

case MenuAction::DecreaseZoom:
dispatcher.enqueue<DecreaseZoomEvent>();
break;
case MenuAction::DecreaseZoom: dispatcher.enqueue<DecreaseZoomEvent>(); break;

case MenuAction::ResetZoom:
dispatcher.enqueue<ResetZoomEvent>();
break;
case MenuAction::ResetZoom: dispatcher.enqueue<ResetZoomEvent>(); break;

case MenuAction::IncreaseFontSize:
dispatcher.enqueue<IncreaseFontSizeEvent>();
break;
case MenuAction::IncreaseFontSize: dispatcher.enqueue<IncreaseFontSizeEvent>(); break;

case MenuAction::DecreaseFontSize:
dispatcher.enqueue<DecreaseFontSizeEvent>();
break;
case MenuAction::DecreaseFontSize: dispatcher.enqueue<DecreaseFontSizeEvent>(); break;

case MenuAction::ResetFontSize:
dispatcher.enqueue<ResetFontSizeEvent>();
break;
case MenuAction::ResetFontSize: dispatcher.enqueue<ResetFontSizeEvent>(); break;

case MenuAction::PanUp:
dispatcher.enqueue<PanUpEvent>();
break;
case MenuAction::PanUp: dispatcher.enqueue<PanUpEvent>(); break;

case MenuAction::PanDown:
dispatcher.enqueue<PanDownEvent>();
break;
case MenuAction::PanDown: dispatcher.enqueue<PanDownEvent>(); break;

case MenuAction::PanRight:
dispatcher.enqueue<PanRightEvent>();
break;
case MenuAction::PanRight: dispatcher.enqueue<PanRightEvent>(); break;

case MenuAction::PanLeft:
dispatcher.enqueue<PanLeftEvent>();
break;
case MenuAction::PanLeft: dispatcher.enqueue<PanLeftEvent>(); break;

case MenuAction::HighlightLayer:
settings.negate_flag(SETTINGS_HIGHLIGHT_ACTIVE_LAYER_BIT);
break;

case MenuAction::ToggleUi:
dispatcher.enqueue<ToggleUiEvent>();
break;
case MenuAction::ToggleUi: dispatcher.enqueue<ToggleUiEvent>(); break;

case MenuAction::InspectMap:
dispatcher.enqueue<InspectMapEvent>();
break;
case MenuAction::InspectMap: dispatcher.enqueue<InspectMapEvent>(); break;

case MenuAction::AddTileset:
dispatcher.enqueue<ShowTilesetCreationDialogEvent>();
break;

case MenuAction::AddRow:
dispatcher.enqueue<AddRowEvent>();
break;
case MenuAction::AddRow: dispatcher.enqueue<AddRowEvent>(); break;

case MenuAction::AddColumn:
dispatcher.enqueue<AddColumnEvent>();
break;
case MenuAction::AddColumn: dispatcher.enqueue<AddColumnEvent>(); break;

case MenuAction::RemoveRow:
dispatcher.enqueue<RemoveRowEvent>();
break;
case MenuAction::RemoveRow: dispatcher.enqueue<RemoveRowEvent>(); break;

case MenuAction::RemoveColumn:
dispatcher.enqueue<RemoveColumnEvent>();
break;
case MenuAction::RemoveColumn: dispatcher.enqueue<RemoveColumnEvent>(); break;

case MenuAction::FixInvalidTiles:
dispatcher.enqueue<FixTilesInMapEvent>();
break;
case MenuAction::FixInvalidTiles: dispatcher.enqueue<FixTilesInMapEvent>(); break;

case MenuAction::ResizeMap:
dispatcher.enqueue<OpenResizeMapDialogEvent>();
break;
case MenuAction::ResizeMap: dispatcher.enqueue<OpenResizeMapDialogEvent>(); break;

case MenuAction::ExportGodotScene:
ui::open_godot_export_dialog();
break;
case MenuAction::ExportGodotScene: ui::open_godot_export_dialog(); break;

case MenuAction::InspectTileset:
dispatcher.enqueue<InspectTilesetEvent>();
break;
case MenuAction::InspectTileset: dispatcher.enqueue<InspectTilesetEvent>(); break;

case MenuAction::DemoWindow:
break;
case MenuAction::DemoWindow: break;

case MenuAction::StyleEditor:
break;
case MenuAction::StyleEditor: break;

case MenuAction::AboutTactile:
ui::open_about_dialog();
break;
case MenuAction::AboutTactile: ui::open_about_dialog(); break;

case MenuAction::AboutDearImGui:
ui::open_about_dear_imgui_dialog();
break;
case MenuAction::AboutDearImGui: ui::open_about_dear_imgui_dialog(); break;

case MenuAction::ReportIssue:
cen::open_url("https://github.com/albin-johansson/tactile/issues/new");
break;

case MenuAction::Credits:
ui::open_credits_dialog();
break;
case MenuAction::Credits: ui::open_credits_dialog(); break;
}
}

Expand Down
17 changes: 11 additions & 6 deletions source/base/inc/tactile/base/container/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@

#include <string> // basic_string, string
#include <string_view> // basic_string_view, string_view, wstring_view
#include <type_traits> // conditional_t

#include "tactile/base/container/path.hpp"
#include "tactile/base/prelude.hpp"

#if TACTILE_OS_WINDOWS
#define TACTILE_NATIVE_STR(Str) L##Str
#else
#define TACTILE_NATIVE_STR(Str) Str
#endif

namespace tactile {

using String = std::string;
using StringView = std::string_view;

using WString = std::wstring;
using WStringView = std::wstring_view;

using OsStrChar = Path::value_type;
using OsString = std::basic_string<OsStrChar>;
using OsStringView = std::basic_string_view<OsStrChar>;
using NativeChar = std::conditional_t<kOnWindows, wchar_t, char>;
using NativeString = std::basic_string<NativeChar>;
using NativeStringView = std::basic_string_view<NativeChar>;

} // namespace tactile
59 changes: 59 additions & 0 deletions source/core/inc/tactile/core/platform/filesystem.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/path.hpp"
#include "tactile/base/container/string.hpp"
#include "tactile/base/prelude.hpp"

namespace tactile {

/**
* Returns the file path to the persistent storage directory associated with the app.
*
* \return
* A directory path; or an error code if something went wrong.
*/
[[nodiscard]]
auto get_persistent_storage_directory() -> Result<Path>;

/**
* Returns the file path to the user home directory.
*
* \return
* A directory path; or an error code if something went wrong.
*/
[[nodiscard]]
auto get_user_home_directory() -> Result<NativeString>;

/**
* Converts a path to a string that is guaranteed to use forward slash characters.
*
* \details
* This function is useful when saving paths to files in a portable way. Since all
* relevant operating systems understand forward slashes, even if some operating
* systems prefer backslashes (most notably Windows).
*
* \param path A file path to convert.
*
* \return
* A file path string that uses forward slashes.
*/
[[nodiscard]]
auto normalize_path(const Path& path) -> String;

/**
* Replaces the user home directory prefix in a file path with '~'.
*
* \param path The file path.
* \param home_dir The user home directory path.
*
* \return
* A string with a shortened home directory prefix; or an error code if not applicable.
*/
[[nodiscard]]
auto strip_home_directory_prefix(const Path& path, const NativeString& home_dir)
-> Result<NativeString>;

} // namespace tactile
43 changes: 28 additions & 15 deletions source/core/inc/tactile/core/util/string_ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,40 @@

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/string.hpp"
#include "tactile/base/int.hpp"
#include "tactile/base/prelude.hpp"
#include "tactile/core/util/concepts.hpp"

namespace tactile {

/**
* Creates a string using the native filesystem character type.
*
* \param str The source string, may be null.
*
* \return
* A corresponding native string; or an error code if something went wrong.
*/
[[nodiscard]]
auto make_native_string(const char* str) -> Result<NativeString>;

/**
* Removes leading and trailing spaces from a given string.
*
* \note
* This function will only modify strings that have at least
* one non-space character.
*
* \param str The string to trim.
*
* \return
* A trimmed string.
*/
[[nodiscard]]
auto trim_string(String str) -> String;

/**
* Splits a string into a collection of tokens separated by a given character.
*
Expand Down Expand Up @@ -52,19 +80,4 @@ auto split_string(const StringView str, const char separator, const T& callback)
return true;
}

/**
* Removes leading and trailing spaces from a given string.
*
* \note
* This function will only modify strings that have at least
* one non-space character.
*
* \param str The string to trim.
*
* \return
* A trimmed string.
*/
[[nodiscard]]
auto trim_string(String str) -> String;

} // namespace tactile
Loading

0 comments on commit 0c4234e

Please sign in to comment.