18.2 C
New York
Saturday, November 8, 2025

structure – How can I combine a library that expects backend occasions into my very own occasion system?


I am utilizing SDL2 in my recreation engine and have created customized occasion varieties that use the info from SDL_Event. This works nicely inside my very own code, nevertheless it turns into problematic with ImGui (and different libraries that depend on SDL) as a result of ImGui expects SDL_Event for ImGui_ImplSDL2_ProcessEvent(), however the editor (which makes use of ImGui) would not have entry to the unique occasion object.

Trying on the ImGui_ImplSDL2_ProcessEvent() implementation, it seems to be a comfort perform, so I ought to be capable of reuse the identical code with my very own occasions. Nonetheless, getting this to work has been a little bit of a trouble as a result of for issues like key presses I have to convert the important thing to a SDL key after which to a ImGui key, so I am curious what else I can do. I am pondering alongside the strains of exposing the SDL_Event in some way, however I am additionally questioning if maybe it is higher to only ditch the SDL abstraction/wrapping altogether?

class Occasion
{
public:
    digital ~Occasion() = default;
    digital EventType GetEventType() const = 0;
    bool WasHandled() const { return mHandled; }
    void SetHandled(bool dealt with) { mHandled = dealt with; }
personal:
    bool mHandled = false;
};

class KeyEvent : public Occasion
{
public:
    enum class KeyAction
    {
        Pressed,
        Launched,
        Repeat
    };

    int GetKeyCode() const { return mKeyCode; }
    int GetModifiers() const { return mModifiers; }
    KeyAction GetAction() const { return mAction; }

protected:
    KeyEvent(int keyCode, int modifiers, KeyAction motion) : mKeyCode(keyCode), mModifiers(modifiers), mAction(motion) 
    {
    }

personal:
    int mKeyCode;
    int mModifiers;
    KeyAction mAction;
};

class KeyPressedEvent : public KeyEvent
{
public:
    KeyPressedEvent(int keyCode, int modifiers, int repeatCount = 0) : KeyEvent(keyCode, modifiers, KeyAction::Pressed), mRepeatCount(repeatCount) 
    {
    }

    EventType GetEventType() const override { return EventType::KeyPressed; }
    static EventType GetStaticType() { return EventType::KeyPressed; }

    int GetRepeatCount() const { return mRepeatCount; }

personal:
    int mRepeatCount;
};

void Platform::PollInput()
{
    SDL_Event occasion;
    whereas (SDL_PollEvent(&occasion))
    {
        change (occasion.kind)
        {
        case SDL_KEYDOWN:
        {
            if (mEventCallback)
            {
                KeyPressedEvent keyPressedEvent(occasion.key.keysym.sym, occasion.key.repeat != 0);
                mEventCallback(keyPressedEvent);
            }
            break;
        }
        }
    }
}

void Utility::OnEvent(Occasion& occasion)
{
    EventDispatcher dispatcher(occasion);
    dispatcher.Dispatch([this](KeyReleasedEvent& e) {
        this->OnKeyReleased(e.GetKeyCode(), e.GetModifiers());
        return true;
        });
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles