mardi 26 avril 2016

OnMouseDown event handler

Im trying to add a event handler to my directx 9 menu so that i can tell when the mouse is over the control item and if it clicks/leaves

Problem im having is the way im doing it at the moment seems messy and i would rather cut down on the functions, plus only call OnMouseDown if it has been added to the events.

class MEventHandler
{
public:
    bool inBounds = false;
    virtual void CheckForEvents() = 0;
};

class MBaseObject : public MEventHandler
{
public:
    std::string name, caption;
    Vector2D position, size;
    bool visible = false;
    MBaseObject() {}
    MBaseObject(std::string name, std::string caption, Vector2D pos, Vector2D size)
    {
        this->name = name;
        this->caption = caption;
        this->position = pos;
        this->size = size;
    }
    virtual void Draw() = 0;
};

class MWindow : public MBaseObject
{
public:
    std::vector<MBaseObject*> Children;
    MCategory* CurrentCategory;
    MWindow(std::string name, Vector2D pos, Vector2D size, LPDIRECT3DDEVICE9 pDevice);
    ~MWindow();
    void AddChildControl(MBaseObject * Child);
    virtual void Draw() override;
    virtual void CheckForEvents() override;

    std::thread CheckForEventsThread;
    bool CheckForEventsRunning;
    static std::shared_ptr<Renderer> renderer;
    static POINT CursorPos;
    static bool Clicked(POINT CursorPos, Vector2D position, Vector2D bounds);
    static bool InBounds(POINT CursorPos, Vector2D position, Vector2D bounds);
    Vector2D TitleSize;
};

class MCheckbox : public MBaseObject
{
public:
    MCheckbox() {}
    int Var;
    MTab* parent;
    MCheckbox::MCheckbox(MTab* parent, std::string name, std::string caption, int variable, Vector2D pos);
    virtual void Draw() override;
    virtual void CheckForEvents() override;
    void OnlButtonDown();
    void OnMouseHover();
    void OnMouseLeave();

    operator bool() const
    {
        return this->Var != 0;
    }

};

.

MWindow::MWindow(std::string name, Vector2D pos, Vector2D size, LPDIRECT3DDEVICE9 pDevice)
{
    this->name = name;
    this->position = pos;
    this->size = size;
    this->visible = true;
    this->TitleSize = Vector2D(0, 50);
    this->CurrentCategory = nullptr;
    CheckForEventsRunning = false;

    renderer = std::make_shared<Renderer>(pDevice);

    //create a thread
    CheckForEventsThread = std::thread([this]()
    {
        this->CheckForEventsRunning = true;

        while (CheckForEventsRunning)
        {
            Mouse::instance().update();
            Keyboard::instance().update();

            if (Keyboard::instance().keyPressed(Keyboard::KEY_HOME))
                this->visible = !this->visible;

            if (this->visible)
            {
                GetCursorPos(&this->CursorPos);
                D3DDEVICE_CREATION_PARAMETERS CParams;
                MWindow::renderer->GetDevice()->GetCreationParameters(&CParams);
                ScreenToClient(CParams.hFocusWindow, &this->CursorPos);

                this->CheckForEvents();
            }

            Sleep(10);
        }

    });

    renderer->addFont("Lucida Console", 14, D3DFONT_BOLD);
    renderer->addFont("Arial", 10);
    renderer->addFont("Lucida Console", 7);
}

MWindow::~MWindow()
{
    this->CheckForEventsRunning = false;

    if (CheckForEventsThread.joinable())
        CheckForEventsThread.join();
}

void MWindow::AddChildControl(MBaseObject* Child)
{
    Children.push_back(Child);
}

void MWindow::Draw()
{
    if (this->visible)
    {
        Renderer::GuardText TextGuard(renderer->ptr());
        Renderer::Guard guard(renderer->ptr(), D3DPT_TRIANGLELIST);

        renderer->drawOutlinedRect(this->position.x, this->position.y, this->size.x, this->size.y, 1.f, D3DCOLOR_ARGB(255, 000, 000, 000), D3DCOLOR_ARGB(255, 44, 44, 44));
        renderer->drawOutlinedRect(this->position.x + 125, this->position.y + 3, this->size.x - 128, this->size.y - 6, 0.5f, D3DCOLOR_ARGB(255, 1, 87, 155), D3DCOLOR_ARGB(255, 033, 033, 033));
        renderer->drawText(this->position.x + this->size.x / 8, this->position.y + (this->TitleSize.y / 2), D3DCOLOR_ARGB(255, 002, 136, 209), TEXT_CENTERED, MenuTitle, "%s", this->name.c_str());

        for (int i = 0; i < Children.size(); i++)
            Children[i]->Draw();
    }
}

void MWindow::CheckForEvents()
{
    if (InBounds(MWindow::CursorPos, this->position, this->size))
    {
        for (auto child : Children)
        {
            child->CheckForEvents();
        }
    }
}

.

void MCheckbox::CheckForEvents()
{
    if (MWindow::InBounds(MWindow::CursorPos, this->position, this->size))
    {
        this->inBounds = true;

        this->OnMouseHover();

        if (MWindow::Clicked(MWindow::CursorPos, this->position, this->size))
            this->OnlButtonDown();
    }
    else if (this->inBounds)
    {
        this->inBounds = false;

        this->OnMouseLeave();
    }
}

void MCheckbox::OnlButtonDown()
{
    if (this->Var == 0)
        this->Var = 1;
    else
        this->Var = 0;
}

void MCheckbox::OnMouseHover()
{
}

void MCheckbox::OnMouseLeave()
{

}

Aucun commentaire:

Enregistrer un commentaire