Jump to content

Crash with CustomWidget


SpiderPig
 Share

Go to solution Solved by Josh,

Recommended Posts

I've only had this happen when using a custom widget.  If you hide the parent of the custom widget before at least one loop it will crash.

#include "Engine.h"

using namespace UltraEngine;

//Declare new style constants
enum CustomWidgetStyle
{
    CUSTOMWIDGET_DEFAULT = 0
};

//Declare new widget class
class CustomWidget : public Widget
{
    bool hover;

protected:

    virtual bool Initialize(const WString& text, const int x, const int y, const int width, const int height, shared_ptr<Widget> parent, const int style)
    {
        return Widget::Initialize(text, x, y, width, height, parent, style);
    }

    //Called when the mouse moves if this widget has the focus
    virtual void MouseMove(const int x, const int y) {}

    //Called when the mouse cursor enters the widget bounds
    virtual void MouseEnter(const int x, const int y)
    {
        hover = true;
        Redraw();
    }

    //Called when the mouse cursor leaves the widget bounds
    virtual void MouseLeave(const int x, const int y)
    {
        hover = false;
        Redraw();
    }

    //Called when the mouse button is pressed
    virtual void MouseDown(const MouseButton button, const int x, const int y)
    {
        if (button == MOUSE_LEFT) EmitEvent(EVENT_WIDGETACTION, Self());
    }

    //Called when the mouse button is released
    virtual void MouseUp(const MouseButton button, const int x, const int y) {}

    //Called when another widget becomes selected
    virtual void LoseFocus() {}

    //Called when mouse double-click occurs
    virtual void DoubleClick(const MouseButton button, const int x, const int y) {}

    //Called when mouse triple-click occurs
    virtual void TripleClick(const MouseButton button, const int x, const int y) {}

    //Called when widget is selected
    virtual void GainFocus() {}

    //Called when key is pressed
    virtual void KeyDown(const KeyCode key) {}

    //Called when key is released
    virtual void KeyUp(const KeyCode key) {}

    //Called for each keydown event
    virtual void KeyChar(const int keychar) {}

    //Called when mouse wheel turns and mouse is hovered over this widget
    virtual void MouseWheel(const int delta, const int x, const int y) {}

    //Called each time the widget is redrawn
    virtual void Draw(const int x, const int y, const int width, const int height)
    {
        blocks.clear();

        AddBlock("Text", iVec2(0), this->size, Vec4(1), TEXT_CENTER | TEXT_MIDDLE);
    }
public:

    //Constructor
    CustomWidget() : hover(false)
    {}

    friend shared_ptr<Widget> CreateCustomWidget(const WString&, const int, const int, const int, const int, shared_ptr<Widget>, const CustomWidgetStyle);
};

//Create function
shared_ptr<Widget> CreateCustomWidget(const WString& text, const int x, const int y, const int width, const int height, shared_ptr<Widget> parent, const CustomWidgetStyle style)
{
    auto widget = std::make_shared<CustomWidget>();
    widget->Initialize(text, x, y, width, height, parent, style);
    return widget;
}

int main(int argc, const char* argv[])
{
    auto displays = GetDisplays();
    auto window = CreateWindow("Ultra Engine", 0, 0, 800, 600, displays[0]);
    auto framebuffer = CreateFramebuffer(window);
    auto world = CreateWorld();

    auto camera = CreateCamera(world);
    camera->Move(0, 0, -3);
    camera->SetClearColor(1, 0, 0);

    auto box = CreateBox(world);

    auto default_font = LoadFont("Fonts\\arial.ttf");
    auto ui = CreateInterface(world, default_font, framebuffer->size);
    ui->SetRenderLayers(2);
    ui->root->SetColor(0.0f, 0.0f, 0.0f, 0.0f);

    auto ui_camera = CreateCamera(world, PROJECTION_ORTHOGRAPHIC);
    ui_camera->SetPosition((float)framebuffer->size.x * 0.5f, (float)framebuffer->size.y * 0.5f, 0);
    ui_camera->SetRenderLayers(2);
    ui_camera->SetClearMode(CLEAR_DEPTH);

    //Create widget
    auto panel = CreatePanel(10, 10, 100, 100, ui->root);
    auto widget = CreateCustomWidget("Custom", 20, 20, 50, 30, panel, CUSTOMWIDGET_DEFAULT);

    while (true)
    {
        panel->SetHidden(true);//Causes crash
        while (PeekEvent()) {
            auto ev = WaitEvent();
            ui->ProcessEvent(ev);
        }
       // panel->SetHidden(true);//No crash here

        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...