UI Toy

Share your helpful Urho3D code snippets, samples and tutorials here.

UI Toy

PostPosted by artgolf1000 » 31 Dec 2016, 12:04

Hi,

I wrote this when I started to learn Urho3D, you may like it:)

If you have a UI control, such as a button or something else, you can add behaviors to it by UI toys, like click, drag, zoom, and whatever behaviors you can imagine.

In this way, you can create your own common behavior library to reuse later.

C++
Code: Select all
BaseUIToy* toy;
Button* button = GetSubsystem<UI>()->GetRoot()->GetChildStaticCast<Button>(String("Button"), true);
toy = new MyClickBegin(context_);
button->AddChild(toy);
toy->Start();
toy = new MyDrag(context_);
button->AddChild(toy);
toy->Start();
toy = new MyZoom(context_);
button->AddChild(toy);
toy->Start();


BaseUIToy.h
Code: Select all
#pragma once

#include <Urho3D/Urho3DAll.h>

/// Custom logic component for UI element.
class BaseUIToy : public UIElement
{
    URHO3D_OBJECT(BaseUIToy, UIElement);
   
public:
    /// Construct.
    BaseUIToy(Context* context) :
    UIElement(context)
    {
        SetFixedSize(0, 0);
    }
    ~BaseUIToy()
    {
        Stop();
    }
    virtual void Start() = 0;
    virtual void Stop() {}
};


MyClick.h
Code: Select all
#pragma once

#include <Urho3D/Urho3DAll.h>
#include "BaseUIToy.h"

/// Custom logic component for rotating a scene node.
class MyClick : public BaseUIToy
{
    URHO3D_OBJECT(MyClick, BaseUIToy);
   
public:
    /// Construct.
    MyClick(Context* context) :
    BaseUIToy(context)
    {
    }
   
    /// Called when the component is added to a scene node. Other components may not yet exist.
    virtual void Start()
    {
        if (parent_) {
            Button* button = static_cast<Button*>(parent_);
            // Subscribe to button release (following a 'press') events
            SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(MyClick, HandleButtonPressed));
        }
    }
   
    /// Called when the component is detached from a scene node, usually on destruction. Note that you will no longer have access to the node and scene at that point.
    virtual void Stop()
    {
        // Remove all events
        UnsubscribeFromAllEvents();
    }
   
    virtual void HandleButtonPressed(StringHash eventType, VariantMap& eventData) = 0;

private:
};


MyClickBegin.h
Code: Select all
#pragma once

#include <Urho3D/Urho3DAll.h>
#include "MyClick.h"

/// Custom logic component for rotating a scene node.
class MyClickBegin : public MyClick
{
    URHO3D_OBJECT(MyClickBegin, MyClick);
   
public:
    /// Construct.
    MyClickBegin(Context* context) :
    MyClick(context)
    {
    }
   
    void HandleButtonPressed(StringHash eventType, VariantMap& eventData)
    {
        // Do something here
    }

private:
};


MyDrag.h
Code: Select all
#pragma once

#include <Urho3D/Urho3DAll.h>
#include "BaseUIToy.h"

/// Custom logic component for rotating a scene node.
class MyDrag : public BaseUIToy
{
    URHO3D_OBJECT(MyDrag, BaseUIToy);
   
public:
    /// Construct.
    MyDrag(Context* context) :
    BaseUIToy(context)
    {
    }
   
    /// Called when the component is added to a scene node. Other components may not yet exist.
    virtual void Start()
    {
        if (parent_) {
            UIElement* element = static_cast<UIElement*>(parent_);
            SubscribeToEvent(element, E_DRAGBEGIN, URHO3D_HANDLER(MyDrag, HandleDragBegin));
            SubscribeToEvent(element, E_DRAGMOVE, URHO3D_HANDLER(MyDrag, HandleDragMove));
            SubscribeToEvent(element, E_DRAGEND, URHO3D_HANDLER(MyDrag, HandleDragEnd));
        }
    }
   
    /// Called when the component is detached from a scene node, usually on destruction. Note that you will no longer have access to the node and scene at that point.
    virtual void Stop()
    {
        // Remove all events
        UnsubscribeFromAllEvents();
    }
   
    void HandleDragBegin(StringHash eventType, VariantMap& eventData)
    {
        // Get UIElement relative position where input (touch or click) occurred (top-left = IntVector2(0,0))
        UIElement* draggedElement = static_cast<UIElement*>(eventData["Element"].GetPtr());
        dragBeginElementPosition_ = draggedElement->GetPosition();
        dragBeginPosition_ = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
    }
   
    void HandleDragMove(StringHash eventType, VariantMap& eventData)
    {
        IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
        UIElement* draggedElement = static_cast<UIElement*>(eventData["Element"].GetPtr());
        draggedElement->SetPosition(dragBeginElementPosition_ + (dragCurrentPosition - dragBeginPosition_));
    }
   
    void HandleDragEnd(StringHash eventType, VariantMap& eventData)
    {
    }

private:
    /// Remembered drag begin position.
    IntVector2 dragBeginElementPosition_;
    IntVector2 dragBeginPosition_;
};


MyZoom.h
Code: Select all
#pragma once

#include <Urho3D/Urho3DAll.h>
#include "BaseUIToy.h"

/// Custom logic component for rotating a scene node.
class MyZoom : public BaseUIToy
{
    URHO3D_OBJECT(MyZoom, BaseUIToy);
   
public:
    /// Construct.
    MyZoom(Context* context) :
    BaseUIToy(context)
    {
    }
   
    /// Called when the component is added to a scene node. Other components may not yet exist.
    virtual void Start()
    {
        counter = 0;
        increasing = true;

        // Subscribe HandleUpdate() function for processing update events
        SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(MyZoom, HandleUpdate));
    }
   
    /// Called when the component is detached from a scene node, usually on destruction. Note that you will no longer have access to the node and scene at that point.
    virtual void Stop()
    {
        // Remove all events
        UnsubscribeFromAllEvents();
    }
   
    /// Handle the logic update event.
    void HandleUpdate(StringHash eventType, VariantMap& eventData)
    {
        if (parent_) {
            UIElement* element = parent_;
            element->SetSize(48 + counter, 48 + counter);
           
            if (increasing) {
                counter++;
            } else {
                counter--;
            }
            if (counter % 16 == 0) {
                increasing = !increasing;
            }
        }
    }
   
private:
    int counter;
    bool increasing;
};
User avatar
artgolf1000
Have some posts
Have some posts
 
Posts: 38
Joined: 31 Aug 2016, 08:49

Re: UI Toy

PostPosted by sabotage3d » 31 Dec 2016, 15:10

Thanks. That would be really useful for mobile.
User avatar
sabotage3d
Have many posts
Have many posts
 
Posts: 515
Joined: 25 Oct 2014, 13:26


Return to Code Exchange

Who is online

Users browsing this forum: No registered users and 0 guests

cron