From b0d23b4ee915d81e52eba9d612c2397b2c68f989 Mon Sep 17 00:00:00 2001 From: nieznanysprawiciel Date: Wed, 13 Nov 2024 16:42:35 +0100 Subject: [PATCH] Binding Text to time counter --- .../TextShowcase/TextShowcase.vcxproj | 1 + swGUI/Prototypes/TextShowcase/Application.cpp | 22 ++++++++++++-- swGUI/Prototypes/TextShowcase/Application.h | 29 +++++++++++++++++++ swGUI/Prototypes/TextShowcase/ViewModel.cpp | 20 +++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 swGUI/Prototypes/TextShowcase/ViewModel.cpp diff --git a/swGUI/ProjectDir/Visual2015/Prototypes/TextShowcase/TextShowcase.vcxproj b/swGUI/ProjectDir/Visual2015/Prototypes/TextShowcase/TextShowcase.vcxproj index fa2c8b43..2846d3f1 100644 --- a/swGUI/ProjectDir/Visual2015/Prototypes/TextShowcase/TextShowcase.vcxproj +++ b/swGUI/ProjectDir/Visual2015/Prototypes/TextShowcase/TextShowcase.vcxproj @@ -158,6 +158,7 @@ + diff --git a/swGUI/Prototypes/TextShowcase/Application.cpp b/swGUI/Prototypes/TextShowcase/Application.cpp index 3cf43ac5..4d4fe1c4 100644 --- a/swGUI/Prototypes/TextShowcase/Application.cpp +++ b/swGUI/Prototypes/TextShowcase/Application.cpp @@ -10,6 +10,7 @@ #include "swGUI/Core/Media/Brushes/ImageBrush.h" #include "swGUI/Core/Media/Colors.h" +#include "swCommonLib/Common/Logging/Logger.h" #include "PrototyperUtils.h" @@ -51,7 +52,7 @@ TextBlock* AddText ( HostWindow* host, BrushPtr brush, BrushPtr pen, float wi // ================================ // // -void AddControls ( HostWindow* host ) +void Application::AddControls ( HostWindow* host ) { { auto background = std::make_shared< SolidColorBrush >( Colors::Transparent ); @@ -87,6 +88,20 @@ void AddControls ( HostWindow* host ) textBlock->SetFontSize( 80 ); } + { + auto pen = std::make_shared< SolidColorBrush >( Colors::BurlyWood ); + auto background = std::make_shared< SolidColorBrush >( Colors::Transparent ); + auto textBlock = AddText( host, background, pen, 300, 100, Position( 350, 350 ), L"" ); + textBlock->SetTextAlignment( sw::TextAlignment::Justify ); + textBlock->SetFontSize( 60 ); + textBlock->SetDataContext( &m_viewModel ); + + auto binding = Binding::Create( "Text", textBlock, "TimeCounter", BindingMode::TwoWay ); + auto result = textBlock->AddBinding( binding ); + if( !result.IsValid() ) + LOG_ERROR( fmt::format( "Binding failed: {}", result.GetErrorReason() ) ); + } + { auto background = std::make_shared< ImageBrush >(); @@ -135,6 +150,7 @@ void AddControls ( HostWindow* host ) // Application::Application ( int argc, char** argv, sw::gui::INativeGUI* gui ) : sw::gui::GUISystem( argc, argv, gui ) + , m_viewModel( ViewModel() ) {} @@ -180,4 +196,6 @@ void Application::OnClosing() /**@brief */ void Application::OnIdle( const sw::gui::FrameTime& frameTime ) -{ } +{ + m_viewModel.SetTimeCounter( std::to_wstring( frameTime.Time ) ); +} diff --git a/swGUI/Prototypes/TextShowcase/Application.h b/swGUI/Prototypes/TextShowcase/Application.h index 16f033b8..538cf74a 100644 --- a/swGUI/Prototypes/TextShowcase/Application.h +++ b/swGUI/Prototypes/TextShowcase/Application.h @@ -7,8 +7,32 @@ #include "swGUI/Core/System/GUISystem.h" +#include "swGUI/Core/System/DataBinding/DependencyObject.h" +#include "swGUI/Core/System/DataBinding/DependencyProperty.h" +/**@brief View of content that will be bound as DataContext to present information.*/ +class ViewModel : public sw::gui::DependencyObject +{ + RTTR_ENABLE( sw::gui::DependencyObject ) + RTTR_REGISTRATION_FRIEND + +private: + static sw::gui::DependencyProperty sTimeCounter; + +private: + + std::wstring m_timeCounter; + +public: + + explicit ViewModel() = default; + + const std::wstring& GetTimeCounter() const { return m_timeCounter; } + void SetTimeCounter( const std::wstring& value ) { SetValue( sTimeCounter, value, &ViewModel::m_timeCounter ); } + +}; + /**@brief Application template class. @@ -17,6 +41,9 @@ class Application : public sw::gui::GUISystem { private: protected: + + ViewModel m_viewModel; + public: explicit Application ( int argc, char** argv, sw::gui::INativeGUI* gui ); ~Application () = default; @@ -30,5 +57,7 @@ class Application : public sw::gui::GUISystem sw::ReturnResult OverridePaths (); + void AddControls ( sw::gui::HostWindow* host ); + }; diff --git a/swGUI/Prototypes/TextShowcase/ViewModel.cpp b/swGUI/Prototypes/TextShowcase/ViewModel.cpp new file mode 100644 index 00000000..35287953 --- /dev/null +++ b/swGUI/Prototypes/TextShowcase/ViewModel.cpp @@ -0,0 +1,20 @@ +/** +@file ViewModel.cpp +@author nieznanysprawiciel +@copyright File is part of Sleeping Wombat Libraries. +*/ + +#include "Application.h" + + +RTTR_REGISTRATION +{ + rttr::registration::class_< ViewModel >( "ViewModel" ) + .property( "TimeCounter", &ViewModel::GetTimeCounter, &ViewModel::SetTimeCounter ); +} + + +using namespace sw::gui; + + +DependencyProperty ViewModel::sTimeCounter = DependencyProperty::Register( "TimeCounter", TypeID::get< ViewModel >() );