leonid_golovko
Topic Author
Posts: 13
Joined: 13 Jun 2024, 15:06

Using instances of a class to use same get and set functions

07 Jul 2024, 16:14

I will add here simple example. Please tell me what I should do to make the four circles to print on the screen:
main.cpp

#include <NsCore/Noesis.h>
#include <NsApp/Application.h>
#include <NsGui/UserControl.h>
#include <NsGui/XamlProvider.h>
#include <NsApp/EmbeddedXamlProvider.h>
#include <NsGui/FontProvider.h>
#include <NsApp/EmbeddedFontProvider.h>
#include <NsApp/EmbeddedTextureProvider.h>
#include <NsGui/IntegrationAPI.h>
#include <NsApp/ApplicationLauncher.h>
#include <NsCore/ReflectionImplementEmpty.h>
#include <NsCore/RegisterComponent.h>
#include <NsApp/EntryPoint.h>
#include <NsApp/Window.h>
#include <NsGui/BitmapImage.h>
#include <NsGui/Image.h>

#include "App.xaml.bin.h"
#include "MainWindow.xaml.bin.h"


#include "beach.jpeg.bin.h"
#include "lightning.jpeg.bin.h"
#include "snow.jpeg.bin.h"
#include "day.jpeg.bin.h"

#include "MyUserControl.hpp"
#include "MyViewModel.hpp"


using namespace Noesis;
using namespace NoesisApp;
using namespace UserControlPart;
using namespace ViewModelPart;

namespace DataApp
{
class App : public Application
{
NS_IMPLEMENT_INLINE_REFLECTION_(App, Application, "Screen.App")
};


};


class AppLauncher final : public ApplicationLauncher
{
private:
void RegisterComponents() const override
{
RegisterComponent<DataApp::App>();
RegisterComponent<UserControlPart::UserControl>();
RegisterComponent<ViewModelPart::ViewModel>();
}

Noesis::Ptr<XamlProvider> GetXamlProvider() const override
{
EmbeddedXaml xamls[] =
{
{ "App.xaml", App_xaml },
{ "MainWindow.xaml", MainWindow_xaml }
};

return *new EmbeddedXamlProvider(xamls);
}
};



int NsMain(int argc, char** argv)
{
AppLauncher launcher;
launcher.SetArguments(argc, argv);
launcher.SetApplicationFile("App.xaml");

return launcher.Run();
}

MyUserControl.cpp


#include "MyUserControl.hpp"
#include <NsGui/Uri.h>

namespace UserControlPart
{
UserControl::UserControl()
{
Initialize();
InitAll();
}

void UserControl::Initialize()
{
Noesis::GUI::LoadComponent(this, Noesis::Uri("MainWindow.xaml"));
m_viewModel = Noesis::MakePtr<ViewModelPart::ViewModel>();
SetDataContext(m_viewModel);
}

void UserControl::InitAll() {
m_viewModel->GetGraphicElements()->Get(0)->SetX(100);
m_viewModel->GetGraphicElements()->Get(0)->SetY(200);

m_viewModel->GetGraphicElements()->Get(1)->SetX(150);
m_viewModel->GetGraphicElements()->Get(1)->SetY(250);

m_viewModel->GetGraphicElements()->Get(2)->SetX(445);
m_viewModel->GetGraphicElements()->Get(2)->SetY(555);

m_viewModel->GetGraphicElements()->Get(3)->SetX(111);
m_viewModel->GetGraphicElements()->Get(3)->SetY(222);
}

NS_IMPLEMENT_REFLECTION(UserControl, "Screen.MainWindow")
{
}
}

MyUserControl.hpp

#include <NsGui/IntegrationAPI.h>
#include <NsGui/UserControl.h>
#include "MyViewModel.hpp"

#ifndef HPP_USERCONTROL_HPP
#define HPP_USERCONTROL_HPP

namespace UserControlPart
{
class UserControl final : public Noesis::UserControl
{
public:
UserControl();
void Initialize();
void InitAll();
private:
Noesis::Ptr<ViewModelPart::ViewModel> m_viewModel;

NS_DECLARE_REFLECTION(UserControl, Noesis::UserControl)
};
}

#endif

MyViewModel.cpp

#include "MyViewModel.hpp"

namespace ViewModelPart
{
GraphicElement::GraphicElement(float x, float y)
: m_x(x), m_y(y) {}

float GraphicElement::GetX() const { return m_x; }
void GraphicElement::SetX(float x) { m_x = x; OnPropertyChanged("X"); }

float GraphicElement::GetY() const { return m_y; }
void GraphicElement::SetY(float y) { m_y = y; OnPropertyChanged("Y"); }

ViewModel::ViewModel()
{
m_graphicElements = *new Noesis::ObservableCollection<GraphicElement>();
for (int i = 0; i < 4; ++i)
{
m_graphicElements->Add(Noesis::MakePtr<GraphicElement>(i * 50 + 50, i * 50 + 50));
}
}

Noesis::ObservableCollection<GraphicElement>* ViewModel::GetGraphicElements() const { return m_graphicElements; }

NS_IMPLEMENT_REFLECTION(ViewModel)
{
NsProp("GraphicElements", &ViewModel::GetGraphicElements);
}

NS_IMPLEMENT_REFLECTION(GraphicElement)
{
NsProp("X", &GraphicElement::GetX, &GraphicElement::SetX);
NsProp("Y", &GraphicElement::GetY, &GraphicElement::SetY);
}
}

MyViewModel.hpp

#ifndef HPP_VIEWMODEL_HPP
#define HPP_VIEWMODEL_HPP

#include <NsCore/Noesis.h>
#include <NsCore/ReflectionImplement.h>
#include <NsApp/NotifyPropertyChangedBase.h>
#include <NsGui/ObservableCollection.h>

namespace ViewModelPart
{
class GraphicElement : public NoesisApp::NotifyPropertyChangedBase
{
public:
GraphicElement(float x = 0, float y = 0);

float GetX() const;
void SetX(float x);

float GetY() const;
void SetY(float y);

NS_DECLARE_REFLECTION(GraphicElement, NoesisApp::NotifyPropertyChangedBase)

private:
float m_x;
float m_y;
};

class ViewModel : public NoesisApp::NotifyPropertyChangedBase
{
public:
ViewModel();
Noesis::ObservableCollection<GraphicElement>* GetGraphicElements() const;

NS_DECLARE_REFLECTION(ViewModel, NoesisApp::NotifyPropertyChangedBase)

private:
Noesis::Ptr<Noesis::ObservableCollection<GraphicElement>> m_graphicElements;
};
}

#endif // HPP_VIEWMODEL_HPP

MainWindown.xaml

<UserControl x:Class="Screen.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006 ... esentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataModelApp"
Width="800" Height="800">

<Canvas x:Name="RootCanvas" Background="White" Margin="10">
<ItemsControl ItemsSource="{Binding GraphicElements}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Ellipse Stroke="Green" StrokeThickness="2" Width="50" Height="50"
Canvas.Left="{Binding X}"
Canvas.Top="{Binding Y}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Canvas>
</UserControl>
Last edited by leonid_golovko on 08 Jul 2024, 07:12, edited 2 times in total.
 
leonid_golovko
Topic Author
Posts: 13
Joined: 13 Jun 2024, 15:06

Re: Using instances of a class to use same get and set functions

07 Jul 2024, 17:12

Is it even possible to do?
 
User avatar
sfernandez
Site Admin
Posts: 3112
Joined: 22 Dec 2011, 19:20

Re: Using instances of a class to use same get and set functions

08 Jul 2024, 13:32

Hello,

An ItemsControl will wrap each data item into a ContentPresenter to place the items inside the ItemsPanel (a Canvas in your case). So the Canvas.Left and Top bindings should be done in the ContentPresenter in order to be correctly positioned.
<UserControl x:Class="Screen.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006 ... esentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DataModelApp"
    Width="800" Height="800">

    <Canvas x:Name="RootCanvas" Background="White" Margin="10">
        <ItemsControl ItemsSource="{Binding GraphicElements}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemContainerStyle>
                <Style TargetType="ContentPresenter">
                    <Setter Property="Canvas.Left" Value="{Binding X}"/>
                    <Setter Property="Canvas.Top" Value="{Binding Y}"/>
                </Style>
            </ItemsControl.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Ellipse Stroke="Green" StrokeThickness="2" Width="50" Height="50"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Canvas>
</UserControl>
Is this what you're looking for?
 
leonid_golovko
Topic Author
Posts: 13
Joined: 13 Jun 2024, 15:06

Re: Using instances of a class to use same get and set functions

11 Jul 2024, 11:32

Yes.
Thank you!

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest