Skip to content

Commit

Permalink
Binding Text to time counter
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Nov 13, 2024
1 parent 23d3308 commit b0d23b4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<ClCompile Include="..\..\..\..\Prototypes\TextShowcase\Application.cpp" />
<ClCompile Include="..\..\..\..\Prototypes\TextShowcase\main.cpp" />
<ClCompile Include="..\..\..\..\Prototypes\TextShowcase\PrototyperUtils.cpp" />
<ClCompile Include="..\..\..\..\Prototypes\TextShowcase\ViewModel.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\..\Prototypes\TextShowcase\Application.h" />
Expand Down
22 changes: 20 additions & 2 deletions swGUI/Prototypes/TextShowcase/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 >();
Expand Down Expand Up @@ -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() )
{}


Expand Down Expand Up @@ -180,4 +196,6 @@ void Application::OnClosing()

/**@brief */
void Application::OnIdle( const sw::gui::FrameTime& frameTime )
{ }
{
m_viewModel.SetTimeCounter( std::to_wstring( frameTime.Time ) );
}
29 changes: 29 additions & 0 deletions swGUI/Prototypes/TextShowcase/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -30,5 +57,7 @@ class Application : public sw::gui::GUISystem

sw::ReturnResult OverridePaths ();

void AddControls ( sw::gui::HostWindow* host );

};

20 changes: 20 additions & 0 deletions swGUI/Prototypes/TextShowcase/ViewModel.cpp
Original file line number Diff line number Diff line change
@@ -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 >() );

0 comments on commit b0d23b4

Please sign in to comment.