Refactor Controller and File Dialog: streamline platform handling, remove redundant parameters, and enhance initialization logic

This commit is contained in:
scawful
2025-01-26 19:28:40 -05:00
parent db9fab83df
commit 646db9607a
6 changed files with 76 additions and 87 deletions

View File

@@ -7,10 +7,10 @@
#include "absl/status/status.h"
#include "absl/strings/str_format.h"
#include "app/core/platform/file_dialog.h"
#include "app/core/platform/font_loader.h"
#include "app/editor/editor_manager.h"
#include "app/gui/style.h"
#include "app/core/platform/file_dialog.h"
#include "imgui/backends/imgui_impl_sdl2.h"
#include "imgui/backends/imgui_impl_sdlrenderer2.h"
#include "imgui/imgui.h"
@@ -19,26 +19,17 @@ namespace yaze {
namespace core {
absl::Status Controller::OnEntry(std::string filename) {
#if defined(__APPLE__) && defined(__MACH__)
#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
platform_ = Platform::kiOS;
#elif TARGET_OS_MAC == 1
platform_ = Platform::kMacOS;
#endif
#elif defined(_WIN32)
platform_ = Platform::kWindows;
#elif defined(__linux__)
platform_ = Platform::kLinux;
#else
platform_ = Platform::kUnknown;
#endif
RETURN_IF_ERROR(CreateWindow())
RETURN_IF_ERROR(CreateRenderer())
RETURN_IF_ERROR(CreateGuiContext())
RETURN_IF_ERROR(LoadAudioDevice())
Initialize(filename);
return absl::OkStatus();
}
void Controller::Initialize(std::string filename) {
editor_manager_.Initialize(filename);
active_ = true;
return absl::OkStatus();
}
void Controller::OnInput() {
@@ -49,30 +40,30 @@ void Controller::OnInput() {
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
switch (event.type) {
case SDL_KEYDOWN:
case SDL_KEYUP: {
ImGuiIO &io = ImGui::GetIO();
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
break;
}
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
active_ = false;
case SDL_KEYDOWN:
case SDL_KEYUP: {
ImGuiIO &io = ImGui::GetIO();
io.KeyShift = ((SDL_GetModState() & KMOD_SHIFT) != 0);
io.KeyCtrl = ((SDL_GetModState() & KMOD_CTRL) != 0);
io.KeyAlt = ((SDL_GetModState() & KMOD_ALT) != 0);
io.KeySuper = ((SDL_GetModState() & KMOD_GUI) != 0);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
io.DisplaySize.x = static_cast<float>(event.window.data1);
io.DisplaySize.y = static_cast<float>(event.window.data2);
}
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_CLOSE:
active_ = false;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
io.DisplaySize.x = static_cast<float>(event.window.data1);
io.DisplaySize.y = static_cast<float>(event.window.data2);
break;
default:
break;
}
break;
default:
break;
}
break;
default:
break;
}
}
@@ -153,12 +144,12 @@ absl::Status Controller::CreateWindow() {
int screen_height = display_mode.h * 0.8;
window_ = std::unique_ptr<SDL_Window, core::SDL_Deleter>(
SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
screen_width, // width, in pixels
screen_height, // height, in pixels
SDL_WINDOW_RESIZABLE),
SDL_CreateWindow("Yet Another Zelda3 Editor", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
screen_width, // width, in pixels
screen_height, // height, in pixels
SDL_WINDOW_RESIZABLE),
core::SDL_Deleter());
if (window_ == nullptr) {
return absl::InternalError(
@@ -208,7 +199,7 @@ absl::Status Controller::LoadAudioDevice() {
want.format = AUDIO_S16;
want.channels = 2;
want.samples = 2048;
want.callback = NULL; // Uses the queue
want.callback = NULL; // Uses the queue
audio_device_ = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
if (audio_device_ == 0) {
return absl::InternalError(
@@ -225,7 +216,7 @@ absl::Status Controller::LoadAudioDevice() {
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_);
std::string config_directory = GetConfigDirectory();
// Create the directory if it doesn't exist
if (!std::filesystem::exists(config_directory)) {
@@ -250,5 +241,5 @@ absl::Status Controller::LoadConfigFiles() {
return absl::OkStatus();
}
} // namespace core
} // namespace yaze
} // namespace core
} // namespace yaze

View File

@@ -6,8 +6,8 @@
#include <memory>
#include "absl/status/status.h"
#include "app/core/platform/renderer.h"
#include "app/core/platform/file_dialog.h"
#include "app/core/platform/renderer.h"
#include "app/editor/editor.h"
#include "app/editor/editor_manager.h"
#include "imgui/backends/imgui_impl_sdl2.h"
@@ -30,6 +30,7 @@ class Controller {
public:
bool IsActive() const { return active_; }
absl::Status OnEntry(std::string filename = "");
void Initialize(std::string filename = "");
void OnInput();
absl::Status OnLoad();
absl::Status OnTestLoad();
@@ -43,13 +44,6 @@ class Controller {
absl::Status LoadAudioDevice();
absl::Status LoadConfigFiles();
void SetupScreen(std::string filename = "") {
editor_manager_.Initialize(filename);
}
auto editor_manager() -> editor::EditorManager & { return editor_manager_; }
auto renderer() -> SDL_Renderer * {
return Renderer::GetInstance().renderer();
}
auto window() -> SDL_Window * { return window_.get(); }
void init_test_editor(editor::Editor *editor) { test_editor_ = editor; }
void set_active(bool active) { active_ = active; }
@@ -59,7 +53,6 @@ class Controller {
friend int ::main(int argc, char **argv);
bool active_ = false;
Platform platform_ = Platform::kUnknown;
editor::Editor *test_editor_ = nullptr;
editor::EditorManager editor_manager_;

View File

@@ -56,7 +56,7 @@ std::string LoadConfigFile(const std::string &filename) {
#else
platform = Platform::kLinux;
#endif
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
std::string filepath = GetConfigDirectory() + "/" + filename;
std::ifstream file(filepath);
if (file.is_open()) {
std::stringstream buffer;
@@ -67,9 +67,8 @@ std::string LoadConfigFile(const std::string &filename) {
return contents;
}
void SaveFile(const std::string &filename, const std::string &contents,
Platform platform) {
std::string filepath = GetConfigDirectory(platform) + "/" + filename;
void SaveFile(const std::string &filename, const std::string &contents) {
std::string filepath = GetConfigDirectory() + "/" + filename;
std::ofstream file(filepath);
if (file.is_open()) {
file << contents;
@@ -77,8 +76,22 @@ void SaveFile(const std::string &filename, const std::string &contents,
}
}
std::string GetConfigDirectory(Platform platform) {
std::string GetConfigDirectory() {
std::string config_directory = ".yaze";
Platform platform;
#if defined(__APPLE__) && defined(__MACH__)
#if TARGET_IPHONE_SIMULATOR == 1 || TARGET_OS_IPHONE == 1
platform = Platform::kiOS;
#elif TARGET_OS_MAC == 1
platform = Platform::kMacOS;
#endif
#elif defined(_WIN32)
platform = Platform::kWindows;
#elif defined(__linux__)
platform = Platform::kLinux;
#else
platform = Platform::kUnknown;
#endif
switch (platform) {
case Platform::kWindows:
config_directory = "~/AppData/Roaming/yaze";

View File

@@ -38,10 +38,8 @@ std::string GetFileExtension(const std::string &filename);
std::string GetFileName(const std::string &filename);
std::string LoadFile(const std::string &filename);
std::string LoadConfigFile(const std::string &filename);
std::string GetConfigDirectory(Platform platform);
void SaveFile(const std::string &filename, const std::string &data,
Platform platform);
std::string GetConfigDirectory();
void SaveFile(const std::string &filename, const std::string &data);
} // namespace core
} // namespace yaze

View File

@@ -1,8 +1,6 @@
#ifndef YAZE_APP_GUI_ZEML_H
#define YAZE_APP_GUI_ZEML_H
#include "imgui/imgui.h"
#include <cctype>
#include <functional>
#include <map>
@@ -10,6 +8,8 @@
#include <string>
#include <vector>
#include "imgui/imgui.h"
namespace yaze {
namespace gui {
@@ -163,12 +163,6 @@ void BindSelectable(Node* node, bool* selected, std::function<void()> callback);
*/
WidgetType MapType(const std::string& type);
/**
* @brief Parse a zeml definition
*/
void ParseDefinitions(const std::vector<Token>& tokens, size_t& index,
std::map<std::string, Node>& definitions);
void ParseFlags(const WidgetType& type, const std::string& flags,
WidgetAttributes& flags_ptr);
@@ -206,7 +200,6 @@ std::string LoadFile(const std::string& filename);
} // namespace zeml
} // namespace gui
} // namespace yaze
#endif // YAZE_APP_GUI_YAZON_H_
#endif // YAZE_APP_GUI_ZEML_H

View File

@@ -46,7 +46,17 @@
abort();
}
_controller = new yaze::app::core::Controller();
_controller = new yaze::core::Controller();
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
yaze::gui::ColorsYaze();
SDL_SetMainReady();
SDL_iOSSetEventPump(SDL_TRUE);
@@ -76,23 +86,14 @@
abort();
}
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
yaze::app::gui::ColorsYaze();
ImGui_ImplSDL2_InitForSDLRenderer(_controller->window(), _controller->renderer());
ImGui_ImplSDLRenderer2_Init(_controller->renderer());
ImGui_ImplSDL2_InitForSDLRenderer(_controller->window(),
core::Renderer::GetInstance().renderer());
ImGui_ImplSDLRenderer2_Init(core::Renderer::GetInstance().renderer());
if (!_controller->LoadFontFamilies().ok()) {
abort();
}
_controller->SetupScreen(rom_filename);
_controller->Initialize("");
_controller->set_active(true);
_hoverGestureRecognizer =