Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor singleton with global context #200

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions engine/source/editor/include/editor.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "runtime/core/base/public_singleton.h"
#include "runtime/core/math/vector2.h"

#include <memory>
Expand All @@ -10,12 +9,12 @@ namespace Pilot
class EditorUI;
class PilotEngine;

class PilotEditor : public PublicSingleton<PilotEditor>
class PilotEditor
{
friend class EditorUI;
friend class PublicSingleton<PilotEditor>;

public:
PilotEditor();
virtual ~PilotEditor();

void initialize(PilotEngine* engine_runtime);
Expand All @@ -24,8 +23,6 @@ namespace Pilot
void run();

protected:
PilotEditor();

std::shared_ptr<EditorUI> m_editor_ui;
PilotEngine* m_engine_runtime{ nullptr };
};
Expand Down
12 changes: 7 additions & 5 deletions engine/source/editor/include/editor_global_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ namespace Pilot
{
class WindowSystem* window_system;
class RenderSystem* render_system;
class PilotEngine* engine_runtime;
};

class EditorGlobalContext
{
public:
class EditorSceneManager* m_scene_manager{ nullptr };
class EditorInputManager* m_input_manager{ nullptr };
class RenderSystem* m_render_system{ nullptr };
class WindowSystem* m_window_system{ nullptr };
class EditorSceneManager* m_scene_manager {nullptr};
class EditorInputManager* m_input_manager {nullptr};
class RenderSystem* m_render_system {nullptr};
class WindowSystem* m_window_system {nullptr};
class PilotEngine* m_engine_runtime {nullptr};

public:
void initialize(const EditorGlobalContextInitInfo& init_info);
void clear();
};

extern EditorGlobalContext g_editor_global_context;
}
} // namespace Pilot
109 changes: 56 additions & 53 deletions engine/source/editor/include/editor_input_manager.h
Original file line number Diff line number Diff line change
@@ -1,66 +1,69 @@
#pragma once
#include <vector>
//#include <runtime/function/render/include/render/surface_io.h>
#include "runtime/function/render/render_camera.h"

#include "runtime/core/math/vector2.h"

#include "runtime/function/render/render_camera.h"

#include <vector>

namespace Pilot
{
class PilotEditor;
class PilotEditor;

enum class EditorCommand : unsigned int
{
camera_left = 1 << 0, // A
camera_back = 1 << 1, // S
camera_foward = 1 << 2, // W
camera_right = 1 << 3, // D
camera_up = 1 << 4, // Q
camera_down = 1 << 5, // E
translation_mode = 1 << 6, // T
rotation_mode = 1 << 7, // R
scale_mode = 1 << 8, // C
exit = 1 << 9, // Esc
delete_object = 1 << 10, // Delete
};

class EditorInputManager
{
public:
void initialize();
void tick(float delta_time);

enum class EditorCommand : unsigned int
{
camera_left = 1 << 0, // A
camera_back = 1 << 1, // S
camera_foward = 1 << 2, // W
camera_right = 1 << 3, // D
camera_up = 1 << 4, // Q
camera_down = 1 << 5, // E
translation_mode = 1 << 6, // T
rotation_mode = 1 << 7, // R
scale_mode = 1 << 8, // C
exit = 1 << 9, // Esc
delete_object = 1 << 10, // Delete
};
public:
void registerInput();
void updateCursorOnAxis(Vector2 cursor_uv);
void processEditorCommand();
void onKeyInEditorMode(int key, int scancode, int action, int mods);

class EditorInputManager
{
public:
void initialize();
void tick(float delta_time);
public:
void registerInput();
void updateCursorOnAxis(Vector2 cursor_uv);
void processEditorCommand();
void onKeyInEditorMode(int key, int scancode, int action, int mods);
void onKey(int key, int scancode, int action, int mods);
void onReset();
void onCursorPos(double xpos, double ypos);
void onCursorEnter(int entered);
void onScroll(double xoffset, double yoffset);
void onMouseButtonClicked(int key, int action);
void onWindowClosed();

bool isCursorInRect(Vector2 pos, Vector2 size) const;

void onKey(int key, int scancode, int action, int mods);
void onReset();
void onCursorPos(double xpos, double ypos);
void onCursorEnter(int entered);
void onScroll(double xoffset, double yoffset);
void onMouseButtonClicked(int key, int action);
void onWindowClosed();
public:
Vector2 getEngineWindowPos() const { return m_engine_window_pos; };
Vector2 getEngineWindowSize() const { return m_engine_window_size; };
float getCameraSpeed() const { return m_camera_speed; };

bool isCursorInRect(Vector2 pos, Vector2 size) const;
public:
Vector2 getEngineWindowPos() { return m_engine_window_pos; };
Vector2 getEngineWindowSize() { return m_engine_window_size; };
float getCameraSpeed() { return m_camera_speed; };
void setEngineWindowPos(Vector2 new_window_pos) { m_engine_window_pos = new_window_pos; };
void setEngineWindowSize(Vector2 new_window_size) { m_engine_window_size = new_window_size; };
void resetEditorCommand() { m_editor_command = 0; }

void setEngineWindowPos(Vector2 new_window_pos) { m_engine_window_pos = new_window_pos; };
void setEngineWindowSize(Vector2 new_window_size) { m_engine_window_size = new_window_size; };
void resetEditorCommand() { m_editor_command = 0; }
private:
Vector2 m_engine_window_pos{ 0.0f, 0.0f };
Vector2 m_engine_window_size{ 1280.0f, 768.0f };
float m_mouse_x{ 0.0f };
float m_mouse_y{ 0.0f };
float m_camera_speed{ 0.05f };
private:
Vector2 m_engine_window_pos {0.0f, 0.0f};
Vector2 m_engine_window_size {1280.0f, 768.0f};
float m_mouse_x {0.0f};
float m_mouse_y {0.0f};
float m_camera_speed {0.05f};

size_t m_cursor_on_axis{ 3 };
unsigned int m_editor_command{ 0 };
};
}
size_t m_cursor_on_axis {3};
unsigned int m_editor_command {0};
};
} // namespace Pilot
29 changes: 13 additions & 16 deletions engine/source/editor/source/editor.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "editor//include/editor.h"
#include "editor/include/editor_ui.h"
#include "editor/include/editor_scene_manager.h"
#include "editor/include/editor_input_manager.h"
#include "editor/include/editor_global_context.h"
#include "runtime/engine.h"

#include "runtime/function/render/render_system.h"
#include "runtime/engine.h"
#include "runtime/function/global/global_context.h"
#include "runtime/function/render/render_camera.h"
#include "runtime/function/render/render_system.h"

#include <cassert>
#include "editor/include/editor_global_context.h"
#include "editor/include/editor_input_manager.h"
#include "editor/include/editor_scene_manager.h"
#include "editor/include/editor_ui.h"

namespace Pilot
{
Expand All @@ -25,28 +25,25 @@ namespace Pilot

PilotEditor::~PilotEditor() {}


void PilotEditor::initialize(PilotEngine* engine_runtime)
{
assert(engine_runtime);

g_is_editor_mode = true;
m_engine_runtime = engine_runtime;

EditorGlobalContextInitInfo init_info = {engine_runtime->getWindowSystem().get(),engine_runtime->getRenderSystem().get()};
EditorGlobalContextInitInfo init_info = {g_runtime_global_context.m_window_system.get(),
g_runtime_global_context.m_render_system.get()};
g_editor_global_context.initialize(init_info);
g_editor_global_context.m_scene_manager->setEditorCamera(engine_runtime->m_render_system->getRenderCamera());
g_editor_global_context.m_scene_manager->setEditorCamera(g_runtime_global_context.m_render_system->getRenderCamera());
g_editor_global_context.m_scene_manager->uploadAxisResource();

m_editor_ui = std::make_shared<EditorUI>();
WindowUIInitInfo ui_init_info = {engine_runtime->m_window_system, engine_runtime->m_render_system};
m_editor_ui = std::make_shared<EditorUI>();
WindowUIInitInfo ui_init_info = {g_runtime_global_context.m_window_system, g_runtime_global_context.m_render_system};
m_editor_ui->initialize(ui_init_info);
}

void PilotEditor::clear()
{
g_editor_global_context.clear();
}
void PilotEditor::clear() { g_editor_global_context.clear(); }

void PilotEditor::run()
{
Expand Down
16 changes: 8 additions & 8 deletions engine/source/editor/source/editor_file_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

#include "runtime/platform/file_service/file_service.h"
#include "runtime/platform/path/path.h"

#include "runtime/resource/asset_manager/asset_manager.h"
#include "runtime/resource/config_manager/config_manager.h"

#include "runtime/function/global/global_context.h"

namespace Pilot
{
/// helper function: split the input string with separator, and filter the substring
Expand Down Expand Up @@ -42,16 +45,13 @@ namespace Pilot

void EditorFileService::buildEngineFileTree()
{
ConfigManager& config_manager = ConfigManager::getInstance();
Path& path_singleton = Path::getInstance();

std::string asset_folder = config_manager.getAssetFolder().generic_string();
const std::vector<std::filesystem::path> file_paths = FileService::getInstance().getFiles(asset_folder);
std::string asset_folder = g_runtime_global_context.m_config_manager->getAssetFolder().generic_string();
const std::vector<std::filesystem::path> file_paths = g_runtime_global_context.m_file_servcie->getFiles(asset_folder);
std::vector<std::vector<std::string>> all_file_segments;
for (const auto& path : file_paths)
{
const std::filesystem::path& relative_path = path_singleton.getRelativePath(asset_folder, path);
all_file_segments.emplace_back(path_singleton.getPathSegments(relative_path));
const std::filesystem::path& relative_path = Path::getRelativePath(asset_folder, path);
all_file_segments.emplace_back(Path::getPathSegments(relative_path));
}

std::vector<std::shared_ptr<EditorFileNode>> node_array;
Expand All @@ -78,7 +78,7 @@ namespace Pilot
}
else
{
const auto& extensions = path_singleton.getFileExtensions(file_paths[file_index]);
const auto& extensions = Path::getFileExtensions(file_paths[file_index]);
file_node->m_file_type = std::get<0>(extensions);
if (file_node->m_file_type.size() == 0)
continue;
Expand Down
16 changes: 9 additions & 7 deletions engine/source/editor/source/editor_global_context.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#include "editor/include/editor_global_context.h"

#include "editor/include/editor_scene_manager.h"
#include "editor/include/editor_input_manager.h"
#include "editor/include/editor_scene_manager.h"

#include "runtime/function/render/window_system.h"
#include "runtime/function/render/render_system.h"
#include "runtime/function/render/window_system.h"

namespace Pilot
{
EditorGlobalContext g_editor_global_context;

void EditorGlobalContext::initialize(const EditorGlobalContextInitInfo& init_info)
{
g_editor_global_context.m_window_system = init_info.window_system;
g_editor_global_context.m_render_system = init_info.render_system;
g_editor_global_context.m_window_system = init_info.window_system;
g_editor_global_context.m_render_system = init_info.render_system;
g_editor_global_context.m_engine_runtime = init_info.engine_runtime;

m_scene_manager = new EditorSceneManager();
m_input_manager = new EditorInputManager();
m_scene_manager->initialize();
Expand All @@ -22,7 +24,7 @@ namespace Pilot

void EditorGlobalContext::clear()
{
delete(m_scene_manager);
delete(m_input_manager);
delete (m_scene_manager);
delete (m_input_manager);
}
}
} // namespace Pilot
Loading