nikobarli
Topic Author
Posts: 178
Joined: 26 Apr 2017, 06:23

Working example for creating UserControl in Native SDK (v2.0.1f1)

16 May 2017, 02:40

I am trying to create a new UserControl following the tutorial here: http://www.noesisengine.com/docs/Gui.Co ... orial.html

But it seems that the codes no longer compatible with v2.0 of SDK and I couldn't get it right. Could you please post an updated version of the tutorial so that we can learn using Noesis GUI faster ?

Thanks.
 
User avatar
sfernandez
Site Admin
Posts: 1911
Joined: 22 Dec 2011, 19:20

Re: Working example for creating UserControl in Native SDK (v2.0.1f1)

16 May 2017, 20:41

Some documentation is still referencing code from previous version of NoesisGUI.
Please use the following code to test UserControls in 2.0 version:
#define CONNECT_EVENT_HANDLER(type, event, handler) \
    if (String::Compare(eventName, #event) == 0 && String::Compare(handlerName, #handler) == 0) \
    { \
        ((type*)source)->event() += MakeDelegate(this, &SelfClass::handler); \
        return; \
    }

class NumericUpDown: public UserControl
{
public:
    NumericUpDown()
    {
        InitializeComponent();
    }

    /// Gets or sets numeric spinner value
    //@{
    NsInt32 GetValue() const
    {
        return DependencyObject::GetValue<NsInt32>(ValueProperty);
    }

    void SetValue(NsInt32 value)
    {
        DependencyObject::SetValue<NsInt32>(ValueProperty, value);
    }
    //@}

    /// Occurs when numeric value changes
    RoutedEvent_<RoutedPropertyChangedEventHandler<NsInt32>::Handler> ValueChanged()
    {
        return RoutedEvent_<RoutedPropertyChangedEventHandler<NsInt32>::Handler>(this,
            ValueChangedEvent);
    }

public:
    static const DependencyProperty* ValueProperty;
    static const DependencyProperty* MaxValueProperty;
    static const DependencyProperty* MinValueProperty;
    static const DependencyProperty* StepValueProperty;
    static const RoutedEvent* ValueChangedEvent;

protected:
    virtual void OnValueChanged(const RoutedPropertyChangedEventArgs<NsInt32>& args)
    {
        RaiseEvent(args);
    }

    // From DependencyObject
    //@{
    NsBool OnPropertyChanged(const Gui::DependencyPropertyChangedEventArgs& args)
    {
        NsBool handled = ParentClass::OnPropertyChanged(args);

        if (!handled)
        {
            if (args.prop == ValueProperty)
            {
                NsInt32 oldValue = *static_cast<const NsInt32*>(args.oldValue);
                NsInt32 newValue = *static_cast<const NsInt32*>(args.newValue);

                RoutedPropertyChangedEventArgs<NsInt32> e(this, ValueChangedEvent,
                    oldValue, newValue);
                OnValueChanged(e);

                return true;
            }
        }

        return handled;
    }
    //@}

private:
    void InitializeComponent()
    {
        GUI::LoadComponent(this, "NumericUpDown.xaml");
    }

    void Connect(BaseComponent* source, const NsChar* eventName, const NsChar* handlerName)
    {
        CONNECT_EVENT_HANDLER(Button, Click, UpButton_Click);
        CONNECT_EVENT_HANDLER(Button, Click, DownButton_Click);
    }

    void UpButton_Click(BaseComponent*, const Gui::RoutedEventArgs&)
    {
        NsInt32 step = DependencyObject::GetValue<NsInt32>(StepValueProperty);
        SetValue(GetValue() + step);
    }

    void DownButton_Click(BaseComponent*, const Gui::RoutedEventArgs&)
    {
        NsInt32 step = DependencyObject::GetValue<NsInt32>(StepValueProperty);
        SetValue(GetValue() - step);
    }

    static NsBool CoerceValue(const DependencyObject* object, const void* value, void* coercedValue)
    {
        NsInt32 maxValue = object->GetValue<NsInt32>(MaxValueProperty);
        NsInt32 minValue = object->GetValue<NsInt32>(MinValueProperty);

        NsInt32 newValue = *static_cast<const NsInt32*>(value);
        NsInt32& coerced = *static_cast<NsInt32*>(coercedValue);

        coerced = Math::Clip(newValue, minValue, maxValue);
        return true;
    }

    NS_IMPLEMENT_INLINE_REFLECTION(NumericUpDown, UserControl)
    {
        NsMeta<TypeId>("NumericUpDown");

        Ptr<UIElementData> data = NsMeta<UIElementData>(TypeOf<SelfClass>());
        data->RegisterProperty<NsInt32>(ValueProperty, "Value",
            FrameworkPropertyMetadata::Create(NsInt32(0), &CoerceValue));
        data->RegisterProperty<NsInt32>(MaxValueProperty, "MaxValue",
            FrameworkPropertyMetadata::Create(NsInt32(255), FrameworkOptions_None));
        data->RegisterProperty<NsInt32>(MinValueProperty, "MinValue",
            FrameworkPropertyMetadata::Create(NsInt32(0), FrameworkOptions_None));
        data->RegisterProperty<NsInt32>(StepValueProperty, "StepValue",
            FrameworkPropertyMetadata::Create(NsInt32(1), FrameworkOptions_None));

        data->RegisterEvent(ValueChangedEvent, "ValueChanged", RoutingStrategy_Bubbling);
    }
};

const DependencyProperty* NumericUpDown::ValueProperty;
const DependencyProperty* NumericUpDown::MaxValueProperty;
const DependencyProperty* NumericUpDown::MinValueProperty;
const DependencyProperty* NumericUpDown::StepValueProperty;
const RoutedEvent* NumericUpDown::ValueChangedEvent;

////////////////////////////////////////////////////////////////////////////////////////////////////
class MultiBinding: public Grid
{
public:
    MultiBinding()
    {
        InitializeComponent();
    }

private:
    void InitializeComponent()
    {
        GUI::LoadComponent(this, "sample.xaml");
    }

    void Connect(BaseComponent* source, const NsChar* eventName, const NsChar* handlerName)
    {
        CONNECT_EVENT_HANDLER(NumericUpDown, ValueChanged, BgR_ValueChanged);
        CONNECT_EVENT_HANDLER(NumericUpDown, ValueChanged, BgG_ValueChanged);
        CONNECT_EVENT_HANDLER(NumericUpDown, ValueChanged, BgB_ValueChanged);
        CONNECT_EVENT_HANDLER(NumericUpDown, ValueChanged, FgR_ValueChanged);
        CONNECT_EVENT_HANDLER(NumericUpDown, ValueChanged, FgG_ValueChanged);
        CONNECT_EVENT_HANDLER(NumericUpDown, ValueChanged, FgB_ValueChanged);
    }

    void BgR_ValueChanged(BaseComponent*, const RoutedPropertyChangedEventArgs<NsInt32>& args)
    {
        SolidColorBrush* bg = NsStaticCast<SolidColorBrush*>(FindName("BgColor"));
        Color color = bg->GetColor();
        bg->SetColor(Color(args.newValue, color.GetGreenI(), color.GetBlueI(), color.GetAlphaI()));
    }

    void BgG_ValueChanged(BaseComponent*, const RoutedPropertyChangedEventArgs<NsInt32>& args)
    {
        SolidColorBrush* bg = NsStaticCast<SolidColorBrush*>(FindName("BgColor"));
        Color color = bg->GetColor();
        bg->SetColor(Color(color.GetRedI(), args.newValue, color.GetBlueI(), color.GetAlphaI()));
    }

    void BgB_ValueChanged(BaseComponent*, const RoutedPropertyChangedEventArgs<NsInt32>& args)
    {
        SolidColorBrush* bg = NsStaticCast<SolidColorBrush*>(FindName("BgColor"));
        Color color = bg->GetColor();
        bg->SetColor(Color(color.GetRedI(), color.GetGreenI(), args.newValue, color.GetAlphaI()));
    }

    void FgR_ValueChanged(BaseComponent*, const RoutedPropertyChangedEventArgs<NsInt32>& args)
    {
        SolidColorBrush* fg = NsStaticCast<SolidColorBrush*>(FindName("FgColor"));
        Color color = fg->GetColor();
        fg->SetColor(Color(args.newValue, color.GetGreenI(), color.GetBlueI(), color.GetAlphaI()));
    }

    void FgG_ValueChanged(BaseComponent*, const RoutedPropertyChangedEventArgs<NsInt32>& args)
    {
        SolidColorBrush* fg = NsStaticCast<SolidColorBrush*>(FindName("FgColor"));
        Color color = fg->GetColor();
        fg->SetColor(Color(color.GetRedI(), args.newValue, color.GetBlueI(), color.GetAlphaI()));
    }

    void FgB_ValueChanged(BaseComponent*, const RoutedPropertyChangedEventArgs<NsInt32>& args)
    {
        SolidColorBrush* fg = NsStaticCast<SolidColorBrush*>(FindName("FgColor"));
        Color color = fg->GetColor();
        fg->SetColor(Color(color.GetRedI(), color.GetGreenI(), args.newValue, color.GetAlphaI()));
    }

    NS_IMPLEMENT_INLINE_REFLECTION(MultiBinding, Grid)
    {
        NsMeta<TypeId>("MultiBinding");
    }
};
Sorry for the inconveniences.
 
User avatar
jsantos
Site Admin
Posts: 2900
Joined: 20 Jan 2012, 17:18
Contact:

Re: Working example for creating UserControl in Native SDK (v2.0.1f1)

16 May 2017, 22:59

Tutorial will be updated in the next version. Sorry for the inconvenience.
 
nikobarli
Topic Author
Posts: 178
Joined: 26 Apr 2017, 06:23

Re: Working example for creating UserControl in Native SDK (v2.0.1f1)

17 May 2017, 02:23

Great. I can now use the sample user control. Thanks !
 
User avatar
jsantos
Site Admin
Posts: 2900
Joined: 20 Jan 2012, 17:18
Contact:

Re: Working example for creating UserControl in Native SDK (v2.0.1f1)

20 May 2017, 01:44

Updated in 2.0.2!

Who is online

Users browsing this forum: No registered users and 0 guests