on
on

Pilot小引擎学习笔记(1)——Editor相关

Author:

Editor主体架构

namespace Pilot
{
    class EditorUI;
    class PilotEngine;

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

    public:
        virtual ~PilotEditor();

        void initialize(PilotEngine* engine_runtime);
        void clear();

        void run();

    protected:
        PilotEditor();

        void   onWindowChanged(float pos_x, float pos_y, float width, float height) const;
        size_t onUpdateCursorOnAxis(int axis_mode, const Vector2& cursor_uv, const Vector2& window_size) const;
        size_t getGuidOfPickedMesh(const Vector2& picked_uv) const;

        std::shared_ptr<EditorUI> m_editor_ui;
        PilotEngine*              m_engine_runtime {nullptr};
    };
} // namespace Pilot

Editor实例包括了run()循环接口以及 onWindowChanged ,getGuidOfPickedMesh和 onUpdatedCursorOnAxis三个回调函数,分别处理窗口大小更改,坐标轴更改,以及窗口中选中操作。
其中EditorUI实现使用listener模型,EditorUI 继承自render中SurfaceUI实现。
初始化过程中将自身传递 engine_runtime中的Render进行绑定。

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

        m_engine_runtime = engine_runtime;
        m_editor_ui      = std::make_shared<EditorUI>(this);

        std::shared_ptr<PilotRenderer> render = m_engine_runtime->getRender();
        assert(render);

        render->setSurfaceUI(m_editor_ui);
    }

Editor的Run方法调用engine_runtime中的main_loop run()
其余三个回调是对engine_runtime中render的方法的包装。

    void PilotEditor::onWindowChanged(float pos_x, float pos_y, float width, float height) const
    {
        std::shared_ptr<PilotRenderer> render = m_engine_runtime->getRender();
        assert(render);

        render->updateWindow(pos_x, pos_y, width, height);
    }

    size_t PilotEditor::onUpdateCursorOnAxis(int axis_mode, const Vector2& cursor_uv, const Vector2& window_size) const
    {
        std::shared_ptr<PilotRenderer> render = m_engine_runtime->getRender();
        assert(render);

        return render->updateCursorOnAxis(axis_mode, cursor_uv, window_size);
    }

    size_t PilotEditor::getGuidOfPickedMesh(const Vector2& picked_uv) const
    {
        std::shared_ptr<PilotRenderer> render = m_engine_runtime->getRender();
        assert(render);

        return render->getGuidOfPickedMesh(picked_uv);
    }

Editor_UI解析

EditorUI继承自Render中抽象类SurfaceUI, SurfaceUI定义了成员对象 shared_ptr<SurfaceIO> m_io,初始化函数initialize(std::shared_ptr<SurfaceIO> pio),状态变量指针UIState* m_tmp_uistate,以及UI用于渲染的接口void onTick(UIState* uistate),void registerInput(),以及获取窗口缩放比例函数float getContentScale。
SurfaceIO类是对窗口中具体响应事件的封装,基于GLFW的api,定义了用于响应GLFW窗口io的static回调函数,指向具体的回调实现。使用STL容器vector<onXxFunc>存储函数指针,批量处理对应回调的响应。
EditorUI的构造函数对资源creator进行了unordered_map的绑定。

主要是imgui部分的调用,部分通过指针调用了vulkan那边window的回调,实现了对象选择,axis显示之类的,这部分待续,直接看eigen部分去。