Add LoadConfigFile to Controller, include file_util src in build

This commit is contained in:
scawful
2024-11-02 10:35:26 -05:00
parent d6a4217118
commit 38dc061b75
3 changed files with 160 additions and 136 deletions

View File

@@ -12,6 +12,7 @@
#include "app/editor/editor_manager.h" #include "app/editor/editor_manager.h"
#include "app/gui/icons.h" #include "app/gui/icons.h"
#include "app/gui/style.h" #include "app/gui/style.h"
#include "core/utils/file_util.h"
#include "imgui/backends/imgui_impl_sdl2.h" #include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h" #include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui/imgui.h" #include "imgui/imgui.h"
@@ -127,50 +128,50 @@ void HandleKeyDown(SDL_Event &event, editor::EditorManager &editor) {
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
switch (event.key.keysym.sym) { switch (event.key.keysym.sym) {
case SDLK_BACKSPACE: case SDLK_BACKSPACE:
case SDLK_LSHIFT: case SDLK_LSHIFT:
case SDLK_LCTRL: case SDLK_LCTRL:
case SDLK_TAB: case SDLK_TAB:
io.KeysDown[event.key.keysym.scancode] = (event.type == SDL_KEYDOWN); io.KeysDown[event.key.keysym.scancode] = (event.type == SDL_KEYDOWN);
break; break;
case SDLK_z: case SDLK_z:
editor.emulator().snes().SetButtonState(1, 0, true); editor.emulator().snes().SetButtonState(1, 0, true);
break; break;
case SDLK_a: case SDLK_a:
editor.emulator().snes().SetButtonState(1, 1, true); editor.emulator().snes().SetButtonState(1, 1, true);
break; break;
case SDLK_RSHIFT: case SDLK_RSHIFT:
editor.emulator().snes().SetButtonState(1, 2, true); editor.emulator().snes().SetButtonState(1, 2, true);
break; break;
case SDLK_RETURN: case SDLK_RETURN:
editor.emulator().snes().SetButtonState(1, 3, true); editor.emulator().snes().SetButtonState(1, 3, true);
break; break;
case SDLK_UP: case SDLK_UP:
editor.emulator().snes().SetButtonState(1, 4, true); editor.emulator().snes().SetButtonState(1, 4, true);
break; break;
case SDLK_DOWN: case SDLK_DOWN:
editor.emulator().snes().SetButtonState(1, 5, true); editor.emulator().snes().SetButtonState(1, 5, true);
break; break;
case SDLK_LEFT: case SDLK_LEFT:
editor.emulator().snes().SetButtonState(1, 6, true); editor.emulator().snes().SetButtonState(1, 6, true);
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
editor.emulator().snes().SetButtonState(1, 7, true); editor.emulator().snes().SetButtonState(1, 7, true);
break; break;
case SDLK_x: case SDLK_x:
editor.emulator().snes().SetButtonState(1, 8, true); editor.emulator().snes().SetButtonState(1, 8, true);
break; break;
case SDLK_s: case SDLK_s:
editor.emulator().snes().SetButtonState(1, 9, true); editor.emulator().snes().SetButtonState(1, 9, true);
break; break;
case SDLK_d: case SDLK_d:
editor.emulator().snes().SetButtonState(1, 10, true); editor.emulator().snes().SetButtonState(1, 10, true);
break; break;
case SDLK_c: case SDLK_c:
editor.emulator().snes().SetButtonState(1, 11, true); editor.emulator().snes().SetButtonState(1, 11, true);
break; break;
default: default:
break; break;
} }
} }
@@ -185,44 +186,44 @@ void HandleKeyUp(SDL_Event &event, editor::EditorManager &editor) {
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0); io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
switch (event.key.keysym.sym) { switch (event.key.keysym.sym) {
case SDLK_z: case SDLK_z:
editor.emulator().snes().SetButtonState(1, 0, false); editor.emulator().snes().SetButtonState(1, 0, false);
break; break;
case SDLK_a: case SDLK_a:
editor.emulator().snes().SetButtonState(1, 1, false); editor.emulator().snes().SetButtonState(1, 1, false);
break; break;
case SDLK_RSHIFT: case SDLK_RSHIFT:
editor.emulator().snes().SetButtonState(1, 2, false); editor.emulator().snes().SetButtonState(1, 2, false);
break; break;
case SDLK_RETURN: case SDLK_RETURN:
editor.emulator().snes().SetButtonState(1, 3, false); editor.emulator().snes().SetButtonState(1, 3, false);
break; break;
case SDLK_UP: case SDLK_UP:
editor.emulator().snes().SetButtonState(1, 4, false); editor.emulator().snes().SetButtonState(1, 4, false);
break; break;
case SDLK_DOWN: case SDLK_DOWN:
editor.emulator().snes().SetButtonState(1, 5, false); editor.emulator().snes().SetButtonState(1, 5, false);
break; break;
case SDLK_LEFT: case SDLK_LEFT:
editor.emulator().snes().SetButtonState(1, 6, false); editor.emulator().snes().SetButtonState(1, 6, false);
break; break;
case SDLK_RIGHT: case SDLK_RIGHT:
editor.emulator().snes().SetButtonState(1, 7, false); editor.emulator().snes().SetButtonState(1, 7, false);
break; break;
case SDLK_x: case SDLK_x:
editor.emulator().snes().SetButtonState(1, 8, false); editor.emulator().snes().SetButtonState(1, 8, false);
break; break;
case SDLK_s: case SDLK_s:
editor.emulator().snes().SetButtonState(1, 9, false); editor.emulator().snes().SetButtonState(1, 9, false);
break; break;
case SDLK_d: case SDLK_d:
editor.emulator().snes().SetButtonState(1, 10, false); editor.emulator().snes().SetButtonState(1, 10, false);
break; break;
case SDLK_c: case SDLK_c:
editor.emulator().snes().SetButtonState(1, 11, false); editor.emulator().snes().SetButtonState(1, 11, false);
break; break;
default: default:
break; break;
} }
} }
@@ -246,7 +247,7 @@ void HandleMouseMovement(int &wheel) {
io.MouseWheel = static_cast<float>(wheel); io.MouseWheel = static_cast<float>(wheel);
} }
} // namespace } // namespace
absl::Status Controller::OnEntry(std::string filename) { absl::Status Controller::OnEntry(std::string filename) {
#if defined(__APPLE__) && defined(__MACH__) #if defined(__APPLE__) && defined(__MACH__)
@@ -280,32 +281,32 @@ void Controller::OnInput() {
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch (event.type) { switch (event.type) {
case SDL_KEYDOWN: case SDL_KEYDOWN:
HandleKeyDown(event, editor_manager_); HandleKeyDown(event, editor_manager_);
break;
case SDL_KEYUP:
HandleKeyUp(event, editor_manager_);
break;
case SDL_TEXTINPUT:
io.AddInputCharactersUTF8(event.text.text);
break;
case SDL_MOUSEWHEEL:
wheel = event.wheel.y;
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
active_ = false;
break; break;
case SDL_KEYUP: case SDL_WINDOWEVENT_SIZE_CHANGED:
HandleKeyUp(event, editor_manager_); ChangeWindowSizeEvent(event);
break;
case SDL_TEXTINPUT:
io.AddInputCharactersUTF8(event.text.text);
break;
case SDL_MOUSEWHEEL:
wheel = event.wheel.y;
break;
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
active_ = false;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
ChangeWindowSizeEvent(event);
break;
default:
break;
}
break; break;
default: default:
break; break;
}
break;
default:
break;
} }
} }
@@ -368,11 +369,11 @@ absl::Status Controller::CreateSDL_Window() {
int screenHeight = displayMode.h * 0.8; int screenHeight = displayMode.h * 0.8;
window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>( window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
SDL_CreateWindow("Yet Another Zelda3 Editor", // window title SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position SDL_WINDOWPOS_UNDEFINED, // initial y position
screenWidth, // width, in pixels screenWidth, // width, in pixels
screenHeight, // height, in pixels screenHeight, // height, in pixels
SDL_WINDOW_RESIZABLE), SDL_WINDOW_RESIZABLE),
core::SDL_Deleter()); core::SDL_Deleter());
if (window_ == nullptr) { if (window_ == nullptr) {
@@ -405,18 +406,18 @@ absl::Status Controller::CreateGuiContext() {
// Check if the assets/fonts directory exists in our CWD // Check if the assets/fonts directory exists in our CWD
// Otherwise, load the system fonts. // Otherwise, load the system fonts.
const auto assets_path = std::filesystem::path("assets"); // const auto assets_path = std::filesystem::path("assets");
if (std::filesystem::is_directory(assets_path)) { // if (std::filesystem::is_directory(assets_path)) {
RETURN_IF_ERROR(LoadFontFamilies()); RETURN_IF_ERROR(LoadFontFamilies());
} else { // } else {
#ifdef __APPLE__ // #ifdef __APPLE__
LoadSystemFonts(); // LoadSystemFonts();
#else // #else
return absl::InternalError( // return absl::InternalError(
"Could not find assets/fonts directory in the current working " // "Could not find assets/fonts directory in the current working "
"directory"); // "directory");
#endif // #endif
} //}
// Set the default style // Set the default style
gui::ColorsYaze(); gui::ColorsYaze();
@@ -428,6 +429,30 @@ absl::Status Controller::CreateGuiContext() {
return absl::OkStatus(); return absl::OkStatus();
} }
absl::Status Controller::LoadConfigFiles() {
// Create and load a dotfile for the application
// This will store the user's preferences and settings
std::string config_directory = GetConfigDirectory(platform_);
// Create the directory if it doesn't exist
if (!std::filesystem::exists(config_directory)) {
std::filesystem::create_directory(config_directory);
}
// Check if the config file exists
std::string config_file = config_directory + "yaze.cfg";
if (!std::filesystem::exists(config_file)) {
// Create the file if it doesn't exist
std::ofstream file(config_file);
if (!file.is_open()) {
return absl::InternalError(
absl::StrFormat("Failed to create config file %s", config_file));
}
}
return absl::OkStatus();
}
absl::Status Controller::LoadFontFamilies() const { absl::Status Controller::LoadFontFamilies() const {
ImGuiIO &io = ImGui::GetIO(); ImGuiIO &io = ImGui::GetIO();
@@ -437,8 +462,8 @@ absl::Status Controller::LoadFontFamilies() const {
static const char *DROID_SANS = "DroidSans.ttf"; static const char *DROID_SANS = "DroidSans.ttf";
static const char *NOTO_SANS_JP = "NotoSansJP.ttf"; static const char *NOTO_SANS_JP = "NotoSansJP.ttf";
static const char *IBM_PLEX_JP = "IBMPlexSansJP-Bold.ttf"; static const char *IBM_PLEX_JP = "IBMPlexSansJP-Bold.ttf";
static const float FONT_SIZE_DEFAULT = 14.0f; static const float FONT_SIZE_DEFAULT = 16.0f;
static const float FONT_SIZE_DROID_SANS = 16.0f; static const float FONT_SIZE_DROID_SANS = 18.0f;
static const float ICON_FONT_SIZE = 18.0f; static const float ICON_FONT_SIZE = 18.0f;
// Icon configuration // Icon configuration
@@ -457,8 +482,8 @@ absl::Status Controller::LoadFontFamilies() const {
icons_config.PixelSnapH = true; icons_config.PixelSnapH = true;
// List of fonts to be loaded // List of fonts to be loaded
std::vector<const char *> font_paths = {KARLA_REGULAR, ROBOTO_MEDIUM, std::vector<const char *> font_paths = {
COUSINE_REGULAR, IBM_PLEX_JP}; KARLA_REGULAR, ROBOTO_MEDIUM, COUSINE_REGULAR, IBM_PLEX_JP, DROID_SANS};
// Load fonts with associated icon and Japanese merges // Load fonts with associated icon and Japanese merges
for (const auto &font_path : font_paths) { for (const auto &font_path : font_paths) {
@@ -532,7 +557,7 @@ absl::Status Controller::LoadAudioDevice() {
want.format = AUDIO_S16; want.format = AUDIO_S16;
want.channels = 2; want.channels = 2;
want.samples = 2048; want.samples = 2048;
want.callback = NULL; // Uses the queue want.callback = NULL; // Uses the queue
audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0); audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (audio_device_ == 0) { if (audio_device_ == 0) {
return absl::InternalError( return absl::InternalError(
@@ -546,6 +571,6 @@ absl::Status Controller::LoadAudioDevice() {
return absl::OkStatus(); return absl::OkStatus();
} }
} // namespace core } // namespace core
} // namespace app } // namespace app
} // namespace yaze } // namespace yaze

View File

@@ -12,10 +12,9 @@
#include "imgui/imgui_internal.h" #include "imgui/imgui_internal.h"
#include "absl/status/status.h" #include "absl/status/status.h"
#include "app/core/platform/renderer.h" #include "app/core/platform/renderer.h"
#include "app/core/utils/file_util.h"
#include "app/editor/editor_manager.h" #include "app/editor/editor_manager.h"
#include "app/editor/utils/editor.h" #include "app/editor/utils/editor.h"
#include "app/gui/icons.h"
#include "app/gui/style.h"
int main(int argc, char **argv); int main(int argc, char **argv);
@@ -23,8 +22,6 @@ namespace yaze {
namespace app { namespace app {
namespace core { namespace core {
enum class Platform { kUnknown, kMacOS, kiOS, kWindows, kLinux };
/** /**
* @brief Main controller for the application. * @brief Main controller for the application.
* *
@@ -44,6 +41,7 @@ class Controller : public ExperimentFlags {
absl::Status CreateSDL_Window(); absl::Status CreateSDL_Window();
absl::Status CreateRenderer(); absl::Status CreateRenderer();
absl::Status CreateGuiContext(); absl::Status CreateGuiContext();
absl::Status LoadConfigFiles();
absl::Status LoadFontFamilies() const; absl::Status LoadFontFamilies() const;
absl::Status LoadAudioDevice(); absl::Status LoadAudioDevice();

View File

@@ -5,6 +5,7 @@ set(
app/core/labeling.cc app/core/labeling.cc
app/emu/emulator.cc app/emu/emulator.cc
app/core/message.cc app/core/message.cc
app/core/utils/file_util.cc
) )
if (WIN32 OR MINGW OR UNIX AND NOT APPLE) if (WIN32 OR MINGW OR UNIX AND NOT APPLE)