Add EditorContext, refactor utils, add system managers

This commit is contained in:
scawful
2024-11-19 23:10:23 -05:00
parent 0b9f7ee6fa
commit 21314702e8
27 changed files with 191 additions and 154 deletions

View File

@@ -8,14 +8,13 @@
#include "app/editor/code/assembly_editor.h"
#include "app/editor/code/memory_editor.h"
#include "app/editor/dungeon/dungeon_editor.h"
#include "app/editor/editor.h"
#include "app/editor/graphics/graphics_editor.h"
#include "app/editor/graphics/palette_editor.h"
#include "app/editor/graphics/screen_editor.h"
#include "app/editor/music/music_editor.h"
#include "app/editor/overworld/overworld_editor.h"
#include "app/editor/sprite/sprite_editor.h"
#include "app/editor/editor.h"
#include "app/editor/utils/gfx_context.h"
#include "app/emu/emulator.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"

View File

@@ -14,7 +14,6 @@ set(
app/editor/graphics/palette_editor.cc
app/editor/graphics/tile16_editor.cc
app/editor/graphics/gfx_group_editor.cc
app/editor/utils/gfx_context.cc
app/editor/overworld/entity.cc
app/editor/system/settings_editor.cc
app/editor/system/command_manager.cc

View File

@@ -1,7 +1,14 @@
#ifndef YAZE_APP_CORE_EDITOR_H
#define YAZE_APP_CORE_EDITOR_H
#include <array>
#include "absl/status/status.h"
#include "app/editor/system/command_manager.h"
#include "app/editor/system/constant_manager.h"
#include "app/editor/system/extension_manager.h"
#include "app/editor/system/history_manager.h"
#include "app/editor/system/resource_manager.h"
namespace yaze {
namespace app {
@@ -12,6 +19,14 @@ namespace app {
*/
namespace editor {
struct EditorContext {
static ConstantManager constant_manager;
static CommandManager command_manager;
static ExtensionManager extension_manager;
static HistoryManager history_manager;
static ResourceManager resource_manager;
};
enum class EditorType {
kAssembly,
kDungeon,
@@ -25,7 +40,7 @@ enum class EditorType {
kSettings,
};
constexpr std::array<const char*, 10> kEditorNames = {
constexpr std::array<const char *, 10> kEditorNames = {
"Assembly", "Dungeon", "Graphics", "Music", "Overworld",
"Palette", "Screen", "Sprite", "Message", "Settings",
};
@@ -37,7 +52,7 @@ constexpr std::array<const char*, 10> kEditorNames = {
* Provides basic editing operations that each editor should implement.
*/
class Editor {
public:
public:
Editor() = default;
virtual ~Editor() = default;
@@ -54,8 +69,9 @@ class Editor {
EditorType type() const { return type_; }
protected:
protected:
EditorType type_;
EditorContext context_;
};
/**
@@ -82,8 +98,8 @@ typedef struct EditorLayoutParams {
absl::Status DrawEditor(EditorLayoutParams *params);
} // namespace editor
} // namespace app
} // namespace yaze
} // namespace editor
} // namespace app
} // namespace yaze
#endif // YAZE_APP_CORE_EDITOR_H
#endif // YAZE_APP_CORE_EDITOR_H

View File

@@ -13,7 +13,7 @@
#include "app/editor/music/music_editor.h"
#include "app/editor/overworld/overworld_editor.h"
#include "app/editor/sprite/sprite_editor.h"
#include "app/editor/utils/flags.h"
#include "app/editor/system/flags.h"
#include "app/emu/emulator.h"
#include "app/gui/icons.h"
#include "app/gui/input.h"
@@ -54,6 +54,7 @@ void EditorManager::SetupScreen(std::string filename) {
PRINT_IF_ERROR(rom()->LoadFromFile(filename));
}
overworld_editor_.InitializeZeml();
InitializeCommands();
}
absl::Status EditorManager::Update() {
@@ -246,14 +247,13 @@ void EditorManager::ManageActiveEditors() {
absl::Status EditorManager::DrawDynamicLayout() {
// Dynamic layout for multiple editors to be open at once
// Allows for tiling and resizing of editors using ImGui
return DrawEditor(&root_layout_);
}
void EditorManager::ManageKeyboardShortcuts() {
bool ctrl_or_super = (GetIO().KeyCtrl || GetIO().KeySuper);
command_manager_.ShowWhichKey();
editor_context_.command_manager.ShowWhichKey();
// If CMD + R is pressed, reload the top result of recent files
if (IsKeyDown(ImGuiKey_R) && ctrl_or_super) {
@@ -313,7 +313,7 @@ void EditorManager::InitializeCommands() {
if (root_layout_.editor == nullptr) {
root_layout_.editor = &overworld_editor_;
}
// New editor popup for window management commands
static EditorLayoutParams new_layout;
if (ImGui::BeginPopup("NewEditor")) {
@@ -362,25 +362,26 @@ void EditorManager::InitializeCommands() {
ImGui::EndPopup();
}
command_manager_.RegisterPrefix("window", 'w', "window management", "");
command_manager_.RegisterSubcommand(
editor_context_.command_manager.RegisterPrefix("window", 'w',
"window management", "");
editor_context_.command_manager.RegisterSubcommand(
"window", "vsplit", '/', "vertical split",
"split windows vertically and place editor in new window", [this]() {
ImGui::OpenPopup("NewEditor");
root_layout_.v_split = true;
});
command_manager_.RegisterSubcommand(
editor_context_.command_manager.RegisterSubcommand(
"window", "hsplit", '-', "horizontal split",
"split windows horizontally and place editor in new window", [this]() {
ImGui::OpenPopup("NewEditor");
root_layout_.h_split = true;
});
command_manager_.RegisterSubcommand("window", "close", 'd', "close",
"close the current editor", [this]() {
if (root_layout_.editor != nullptr) {
root_layout_.editor = nullptr;
}
});
editor_context_.command_manager.RegisterSubcommand(
"window", "close", 'd', "close", "close the current editor", [this]() {
if (root_layout_.editor != nullptr) {
root_layout_.editor = nullptr;
}
});
}
void EditorManager::DrawStatusPopup() {

View File

@@ -15,14 +15,10 @@
#include "app/editor/music/music_editor.h"
#include "app/editor/overworld/overworld_editor.h"
#include "app/editor/sprite/sprite_editor.h"
#include "app/editor/system/command_manager.h"
#include "app/editor/system/constant_manager.h"
#include "app/editor/system/extension_manager.h"
#include "app/editor/system/settings_editor.h"
#include "app/emu/emulator.h"
#include "app/gui/input.h"
#include "app/rom.h"
#include "yaze.h"
namespace yaze {
namespace app {
@@ -33,18 +29,11 @@ namespace editor {
* @brief The EditorManager controls the main editor window and manages the
* various editor classes.
*
* This class inherits from SharedRom and ExperimentFlags, and
* provides functionality for setting up the screen, updating the editor, and
* shutting down the editor. It also includes methods for drawing various menus
* and popups, saving the Rom, and managing editor-specific flags.
*
* The EditorManager class contains instances of various editor classes such as
* AssemblyEditor, DungeonEditor, GraphicsEditor, MusicEditor, OverworldEditor,
* PaletteEditor, ScreenEditor, and SpriteEditor. The current_editor_ member
* variable points to the currently active editor in the tab view.
*
* @note This class assumes the presence of an SDL_Renderer object for rendering
* graphics.
*/
class EditorManager : public SharedRom, public core::ExperimentFlags {
public:
@@ -101,13 +90,12 @@ class EditorManager : public SharedRom, public core::ExperimentFlags {
std::vector<Editor *> active_editors_;
std::vector<EditorLayoutParams> active_layouts_;
EditorLayoutParams root_layout_;
Project current_project_;
CommandManager command_manager_;
ConstantManager constant_manager_;
ExtensionManager extension_manager_;
EditorContext editor_context_;
Editor *current_editor_ = nullptr;
EditorLayoutParams root_layout_;
AssemblyEditor assembly_editor_;
DungeonEditor dungeon_editor_;
GraphicsEditor graphics_editor_;
@@ -119,8 +107,6 @@ class EditorManager : public SharedRom, public core::ExperimentFlags {
SettingsEditor settings_editor_;
MessageEditor message_editor_;
MemoryEditorWithDiffChecker memory_editor_;
yaze_editor_context editor_context_;
};
} // namespace editor

View File

@@ -13,6 +13,7 @@
#include "app/gfx/snes_tile.h"
#include "app/gui/canvas.h"
#include "app/gui/color.h"
#include "app/gui/icons.h"
#include "app/gui/input.h"
#include "app/gui/modules/asset_browser.h"
#include "app/gui/style.h"

View File

@@ -398,6 +398,23 @@ void ScreenEditor::DrawDungeonMapsEditor() {
}
}
if (ImGui::BeginTable("##DungeonMapToolset", 2, ImGuiTableFlags_SizingFixedFit)) {
ImGui::TableSetupColumn("Draw Mode");
ImGui::TableSetupColumn("Edit Mode");
ImGui::TableNextColumn();
if (ImGui::Button(ICON_MD_DRAW)) {
current_mode_ = EditingMode::DRAW;
}
ImGui::TableNextColumn();
if (ImGui::Button(ICON_MD_EDIT)) {
current_mode_ = EditingMode::EDIT;
}
ImGui::EndTable();
}
static std::vector<std::string> dungeon_names = {
"Sewers/Sanctuary", "Hyrule Castle", "Eastern Palace",
"Desert Palace", "Tower of Hera", "Agahnim's Tower",
@@ -483,13 +500,20 @@ void ScreenEditor::DrawDungeonMapsEditor() {
ImGui::EndChild();
ImGui::TableNextColumn();
tilemap_canvas_.DrawBackground(ImVec2(128 * 2 + 2, (192 * 2) + 4));
tilemap_canvas_.DrawBackground();
tilemap_canvas_.DrawContextMenu();
tilemap_canvas_.DrawTileSelector(8.f);
if (tilemap_canvas_.DrawTileSelector(16.f)) {
// Get the tile8 ID to use for the tile16 drawing above
selected_tile8_ = tilemap_canvas_.GetTileIdFromMousePos();
}
tilemap_canvas_.DrawBitmapTable(sheets_);
tilemap_canvas_.DrawGrid();
tilemap_canvas_.DrawOverlay();
ImGui::Text("Selected tile8: %d", selected_tile8_);
ImGui::Separator();
ImGui::Text("For use with custom inserted graphics assembly patches.");
if (ImGui::Button("Load GFX from BIN file")) LoadBinaryGfx();
ImGui::EndTable();

View File

@@ -4,7 +4,6 @@
#include "absl/status/status.h"
#include "app/core/common.h"
#include "app/editor/graphics/palette_editor.h"
#include "app/editor/utils/gfx_context.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"
@@ -20,7 +19,7 @@ namespace editor {
/**
* @brief Popup window to edit Tile16 data
*/
class Tile16Editor : public context::GfxContext, public SharedRom {
class Tile16Editor : public GfxContext, public SharedRom {
public:
absl::Status InitBlockset(const gfx::Bitmap& tile16_blockset_bmp,
const gfx::Bitmap& current_gfx_bmp,

View File

@@ -1,5 +1,6 @@
#include "app/editor/overworld/entity.h"
#include "app/gui/icons.h"
#include "app/gui/input.h"
#include "app/gui/style.h"

View File

@@ -8,12 +8,11 @@
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "app/editor/editor.h"
#include "app/editor/graphics/gfx_group_editor.h"
#include "app/editor/graphics/palette_editor.h"
#include "app/editor/graphics/tile16_editor.h"
#include "app/editor/overworld/entity.h"
#include "app/editor/editor.h"
#include "app/editor/utils/gfx_context.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"
@@ -111,7 +110,7 @@ class EntranceContext {
class OverworldEditor : public Editor,
public SharedRom,
public EntranceContext,
public context::GfxContext,
public GfxContext,
public core::ExperimentFlags {
public:
OverworldEditor() { type_ = EditorType::kOverworld; }

View File

@@ -2,7 +2,6 @@
#include <fstream>
#include "app/editor/editor.h"
#include "imgui/imgui.h"
namespace yaze {
@@ -74,11 +73,7 @@ ImGuiKey MapKeyToImGuiKey(char key) {
// When the player presses Space, a popup will appear fixed to the bottom of the
// ImGui window with a list of the available key commands which can be used.
void CommandManager::ShowWhichKey(EditorLayoutParams *editor_layout) {
if (commands_.empty()) {
InitializeDefaults();
}
void CommandManager::ShowWhichKey() {
if (ImGui::IsKeyPressed(ImGuiKey_Space)) {
ImGui::OpenPopup("WhichKey");
}
@@ -117,20 +112,8 @@ void CommandManager::ShowWhichKey(EditorLayoutParams *editor_layout) {
}
ImGui::EndPopup();
}
// if the user presses the shortcut key, execute the command
// or if it is a subcommand prefix, wait for the next key
// to determine the subcommand
static bool subcommand_section = false;
for (const auto &[shortcut, info] : commands_) {
if (ImGui::IsKeyPressed(MapKeyToImGuiKey(shortcut[0]))) {
info.command_info.command();
}
}
}
void CommandManager::InitializeDefaults() {}
void CommandManager::SaveKeybindings(const std::string &filepath) {
std::ofstream out(filepath);
if (out.is_open()) {

View File

@@ -5,7 +5,6 @@
#include <string>
#include <unordered_map>
#include "app/editor/editor.h"
#include "imgui/imgui.h"
namespace yaze {
@@ -65,9 +64,7 @@ public:
}
}
void ShowWhichKey(EditorLayoutParams *editor_layout = nullptr);
void InitializeDefaults();
void ShowWhichKey();
void SaveKeybindings(const std::string &filepath);
void LoadKeybindings(const std::string &filepath);

View File

@@ -2,19 +2,7 @@
#define YAZE_APP_EDITOR_SYSTEM_CONSTANT_MANAGER_H
#include <cstddef>
#include <cstdint>
#include <memory>
#include <unordered_map>
#include <vector>
#include "absl/status/status.h"
#include "app/editor/utils/gfx_context.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"
#include "app/rom.h"
#include "app/zelda3/common.h"
#include "app/zelda3/overworld/overworld.h"
#include "app/zelda3/overworld/overworld_map.h"
#include "imgui/imgui.h"
@@ -86,4 +74,4 @@ class ConstantManager {
} // namespace app
} // namespace yaze
#endif // YAZE_APP_EDITOR_SYSTEM_CONSTANT_MANAGER_H
#endif // YAZE_APP_EDITOR_SYSTEM_CONSTANT_MANAGER_H

View File

@@ -0,0 +1,32 @@
#ifndef YAZE_APP_EDITOR_SYSTEM_HISTORY_MANAGER_H
#define YAZE_APP_EDITOR_SYSTEM_HISTORY_MANAGER_H
#include <cstddef>
#include <stack>
#include <vector>
namespace yaze {
namespace app {
namespace editor {
// System history manager, undo and redo.
class HistoryManager {
public:
HistoryManager() = default;
~HistoryManager() = default;
void Add(const char* data);
void Undo();
void Redo();
private:
std::vector<const char*> history_;
std::stack<const char*> undo_;
std::stack<const char*> redo_;
};
} // namespace editor
} // namespace app
} // namespace yaze
#endif // YAZE_APP_EDITOR_SYSTEM_HISTORY_MANAGER_H

View File

@@ -0,0 +1,19 @@
#include "popup_manager.h"
#include "imgui/imgui.h"
namespace yaze {
namespace app {
namespace editor {
PopupManager::PopupManager() {
}
PopupManager::~PopupManager() {
}
} // namespace editor
} // namespace app
} // namespace yaze

View File

@@ -0,0 +1,21 @@
#ifndef YAZE_APP_EDITOR_POPUP_MANAGER_H
#define YAZE_APP_EDITOR_POPUP_MANAGER_H
namespace yaze {
namespace app {
namespace editor {
// ImGui popup manager.
class PopupManager {
public:
PopupManager();
~PopupManager();
void Show(const char* name);
};
} // namespace editor
} // namespace app
} // namespace yaze
#endif // YAZE_APP_EDITOR_POPUP_MANAGER_H

View File

@@ -0,0 +1,30 @@
#ifndef YAZE_APP_EDITOR_SYSTEM_RESOURCE_MANAGER_H
#define YAZE_APP_EDITOR_SYSTEM_RESOURCE_MANAGER_H
#include <cstddef>
namespace yaze {
namespace app {
namespace editor {
// System resource manager.
class ResourceManager {
public:
ResourceManager();
~ResourceManager();
void Load(const char* path);
void Unload(const char* path);
void UnloadAll();
size_t Count() const;
private:
size_t count_;
};
} // namespace editor
} // namespace app
} // namespace yaze
#endif // YAZE_APP_EDITOR_SYSTEM_RESOURCE_MANAGER_H

View File

@@ -2,7 +2,7 @@
#include "app/editor/system/settings_editor.h"
#include "absl/status/status.h"
#include "app/editor/utils/flags.h"
#include "app/editor/system/flags.h"
#include "imgui/imgui.h"
namespace yaze {

View File

@@ -1,26 +0,0 @@
#include "app/editor/utils/gfx_context.h"
#include "imgui/imgui.h"
#include <cmath>
#include "app/editor/graphics/palette_editor.h"
#include "app/editor/editor.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"
#include "app/gui/canvas.h"
#include "app/gui/icons.h"
#include "app/rom.h"
namespace yaze {
namespace app {
namespace editor {
namespace context {
std::unordered_map<uint8_t, gfx::Paletteset> GfxContext::palettesets_;
}
} // namespace editor
} // namespace app
} // namespace yaze

View File

@@ -1,36 +0,0 @@
#ifndef YAZE_APP_EDITOR_VRAM_CONTEXT_H
#define YAZE_APP_EDITOR_VRAM_CONTEXT_H
#include "imgui/imgui.h"
#include <cmath>
#include <vector>
#include "app/editor/editor.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"
#include "app/gui/canvas.h"
#include "app/gui/icons.h"
#include "app/rom.h"
namespace yaze {
namespace app {
namespace editor {
namespace context {
/**
* @brief Shared graphical context across editors.
*/
class GfxContext {
protected:
// Palettesets for the tile16 individual tiles
static std::unordered_map<uint8_t, gfx::Paletteset> palettesets_;
};
} // namespace context
} // namespace editor
} // namespace app
} // namespace yaze
#endif // YAZE_APP_EDITOR_VRAM_CONTEXT_H

View File

@@ -351,5 +351,6 @@ absl::Status LoadAllPalettes(const std::vector<uint8_t> &rom_data,
}
} // namespace gfx
std::unordered_map<uint8_t, gfx::Paletteset> GfxContext::palettesets_;
} // namespace app
} // namespace yaze

View File

@@ -405,7 +405,17 @@ struct Paletteset {
gfx::SnesPalette composite; /**< The composite palette. */
};
} // namespace gfx
} // namespace gfx
/**
* @brief Shared graphical context across editors.
*/
class GfxContext {
protected:
// Palettesets for the tile16 individual tiles
static std::unordered_map<uint8_t, gfx::Paletteset> palettesets_;
};
} // namespace app
} // namespace yaze

View File

@@ -2,16 +2,12 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <unordered_map>
#include <vector>
#include "app/editor/utils/gfx_context.h"
#include "app/gfx/bitmap.h"
#include "app/gfx/snes_tile.h"
#include "app/rom.h"
#include "app/zelda3/overworld/overworld.h"
#include "imgui/imgui.h"
namespace yaze {
namespace app {

View File

@@ -6,7 +6,6 @@
#include <vector>
#include "absl/status/status.h"
#include "app/editor/utils/gfx_context.h"
#include "app/gfx/snes_palette.h"
#include "app/gfx/snes_tile.h"
#include "app/rom.h"
@@ -60,7 +59,7 @@ constexpr int OverworldCustomTileGFXGroupEnabled = 0x140148;
/**
* @brief Represents a single Overworld map screen.
*/
class OverworldMap : public editor::context::GfxContext {
class OverworldMap : public GfxContext {
public:
OverworldMap() = default;
OverworldMap(int index, Rom& rom, bool load_custom_data = false);

View File

@@ -7,7 +7,6 @@ add_executable(
app/rom.cc
app/core/common.cc
app/core/project.cc
app/editor/utils/gfx_context.cc
app/core/platform/file_path.mm
${YAZE_APP_EMU_SRC}
${YAZE_APP_GFX_SRC}

View File

@@ -11,7 +11,6 @@ add_executable(
test/zelda3/sprite_builder_test.cc
app/rom.cc
app/core/common.cc
app/editor/utils/gfx_context.cc
${ASAR_STATIC_SRC}
${YAZE_APP_CORE_SRC}
${YAZE_APP_EMU_SRC}