From 23d3308f0ea51970833191c2773fed639426e7be Mon Sep 17 00:00:00 2001 From: nieznanysprawiciel Date: Wed, 13 Nov 2024 16:33:49 +0100 Subject: [PATCH] Fix TwoWay binding --- .../Core/System/DataBinding/BindingsList.cpp | 6 ++-- .../Utils/ClassesUI/DependencyPropsClass.cpp | 6 +++- .../Utils/ClassesUI/DependencyPropsClass.h | 5 ++++ .../TestDependencyObjectBinding.cpp | 29 ++++++++++++++++++- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/swGUI/Core/System/DataBinding/BindingsList.cpp b/swGUI/Core/System/DataBinding/BindingsList.cpp index b902ef64..14c74976 100644 --- a/swGUI/Core/System/DataBinding/BindingsList.cpp +++ b/swGUI/Core/System/DataBinding/BindingsList.cpp @@ -72,11 +72,13 @@ ReturnResult BindingsList::AddBinding ( BindingPtr binding ) // void BindingsList::AddBindingLink ( const BindingInfoPtr& binding ) { - auto sourcePropInfo = FindBinding( binding->GetProperty() ); + // We are the source of the Binding soe our BIndingInfo object should be reverse of the target. + auto sourceProperty = binding->GetBinding()->GetSourceProperty(); + auto sourcePropInfo = FindBinding( sourceProperty ); // If BindingInfo object didn't exist, we create new one with empty binding. if( sourcePropInfo == nullptr ) - sourcePropInfo = std::make_shared< BindingInfo >( binding->GetProperty() ); + sourcePropInfo = std::make_shared< BindingInfo >( sourceProperty ); sourcePropInfo->AddBindingLink( binding ); m_bindings.push_back( sourcePropInfo ); diff --git a/swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.cpp b/swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.cpp index 6af1b403..a3ad2041 100644 --- a/swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.cpp +++ b/swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.cpp @@ -10,7 +10,9 @@ RTTR_REGISTRATION { rttr::registration::class_< sw::gui::DependencyPropsClass >( "sw::gui::DependencyPropsClass" ) .property( "NumberItems", &sw::gui::DependencyPropsClass::GetNumberItems, &sw::gui::DependencyPropsClass::SetNumberItems ) - .property( "ContainerName", &sw::gui::DependencyPropsClass::GetContainerName, &sw::gui::DependencyPropsClass::SetContainerName ); + .property( "ContainerName", &sw::gui::DependencyPropsClass::GetContainerName, &sw::gui::DependencyPropsClass::SetContainerName ) + .property( "RandomName", &sw::gui::DependencyPropsClass::GetRandomName, &sw::gui::DependencyPropsClass::SetRandomName ); + ; } @@ -22,6 +24,7 @@ namespace gui DependencyProperty DependencyPropsClass::sNumberItemsProperty = DependencyProperty::Register( "NumberItems", TypeID::get< DependencyPropsClass >() ); DependencyProperty DependencyPropsClass::sContainerNameProperty = DependencyProperty::Register( "ContainerName", TypeID::get< DependencyPropsClass >() ); +DependencyProperty DependencyPropsClass::sRandomNameProperty = DependencyProperty::Register( "RandomName", TypeID::get< DependencyPropsClass >() ); @@ -30,6 +33,7 @@ DependencyProperty DependencyPropsClass::sContainerNameProperty = DependencyProp DependencyPropsClass::DependencyPropsClass() : m_numberItems( 3 ) , m_containerName( "Name" ) + , m_randomName( "Blaaa" ) {} diff --git a/swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.h b/swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.h index 9e38513b..c95de1d0 100644 --- a/swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.h +++ b/swGUI/TestFramework/Utils/ClassesUI/DependencyPropsClass.h @@ -27,11 +27,13 @@ class DependencyPropsClass : public DependencyObject static DependencyProperty sNumberItemsProperty; static DependencyProperty sContainerNameProperty; + static DependencyProperty sRandomNameProperty; private: uint32 m_numberItems; std::string m_containerName; + std::string m_randomName; public: @@ -45,6 +47,9 @@ class DependencyPropsClass : public DependencyObject const std::string& GetContainerName () const { return m_containerName; } void SetContainerName ( const std::string& value ) { DependencyObject::SetValue( sContainerNameProperty, value, &DependencyPropsClass::m_containerName ); } + + const std::string& GetRandomName () const { return m_randomName; } + void SetRandomName ( const std::string& value ) { DependencyObject::SetValue( sRandomNameProperty, value, &DependencyPropsClass::m_randomName ); } }; diff --git a/swGUI/Tests/TestBindings/TestDependencyObject/TestDependencyObjectBinding.cpp b/swGUI/Tests/TestBindings/TestDependencyObject/TestDependencyObjectBinding.cpp index df691a32..b4d42244 100644 --- a/swGUI/Tests/TestBindings/TestDependencyObject/TestDependencyObjectBinding.cpp +++ b/swGUI/Tests/TestBindings/TestDependencyObject/TestDependencyObjectBinding.cpp @@ -141,6 +141,34 @@ TEST_CASE( "Binding_DependencyObject_TwoWayBinding", "[GUI][BindingSystem]" ) CHECK( child->GetContainerName() == "Value number 2" ); } +// ================================ // +// +TEST_CASE( "Binding.DependencyObject.TwoWayBinding.DifferentName", "[GUI][BindingSystem]" ) +{ + std::unique_ptr< gui::DependencyPropsClass > root = std::make_unique< gui::DependencyPropsClass >(); + std::unique_ptr< gui::DependencyPropsClass > child = std::make_unique< gui::DependencyPropsClass >(); + + root->SetDataContext( child.get() ); + auto initContainerName = root->GetContainerName(); + auto initRandomName = child->GetRandomName(); + + auto binding = gui::Binding::Create( "RandomName", root.get(), "ContainerName", gui::BindingMode::TwoWay ); + REQUIRE( root->AddBinding( binding ).IsValid() ); + + // To target direction. + CHECK_NOTHROW( child->SetContainerName( "Newly set value" ) ); + CHECK( root->GetRandomName() == "Newly set value" ); + CHECK( root->GetContainerName() == initContainerName ); // Shouldn't change. + CHECK( child->GetContainerName() == "Newly set value" ); + CHECK( child->GetRandomName() == initRandomName ); + + // To source direction. + CHECK_NOTHROW( root->SetRandomName( "Value number 2" ) ); + CHECK( root->GetRandomName() == "Value number 2" ); + CHECK( root->GetContainerName() == initContainerName ); // Shouldn't change. + CHECK( child->GetContainerName() == "Value number 2" ); + CHECK( child->GetRandomName() == initRandomName ); +} // ================================ // // Circular dependancies shouldn't cause dead lock. @@ -172,4 +200,3 @@ TEST_CASE( "Binding_DependencyObject_CircularDependencies_TwoWay", "[GUI][Bindin CHECK( child3->GetNumberItems() == 394 ); CHECK( child4->GetNumberItems() == 394 ); } -