From 94209fff2d09075cb1dc0f0f52e741d2fa61240c Mon Sep 17 00:00:00 2001 From: Adeel Kazmi Date: Wed, 13 Jun 2018 14:44:19 +0100 Subject: [PATCH] (Layouting) Made it easier to add new layouting examples & toggle between all Change-Id: I18c43c68c43b5c29dda121d7c2ea55af562304cf --- examples/layouting/example.h | 42 +++++++++++++++++++ examples/layouting/layouting-examples.cpp | 70 ++++++++++++++----------------- examples/layouting/linear-example.h | 13 +++--- examples/layouting/padding-example.cpp | 24 +++++------ examples/layouting/padding-example.h | 10 +++-- 5 files changed, 99 insertions(+), 60 deletions(-) create mode 100644 examples/layouting/example.h diff --git a/examples/layouting/example.h b/examples/layouting/example.h new file mode 100644 index 0000000..d0a15f0 --- /dev/null +++ b/examples/layouting/example.h @@ -0,0 +1,42 @@ +#ifndef DALI_DEMO_LAYOUTING_EXAMPLE_H +#define DALI_DEMO_LAYOUTING_EXAMPLE_H + +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +namespace Demo +{ + +/** + * @brief Abstract base class for layouting examples. + */ +class Example +{ +public: + /// Should be overridden by deriving classes to create the required Layouting example + virtual void Create() = 0; + + /// Should be overridden by deriving classes to remove their layouting example from stage + virtual void Remove() = 0; + + /// Virtual destructor + virtual ~Example() = default; +}; + +} // namespace Demo + +#endif // DALI_DEMO_LAYOUTING_EXAMPLE_H diff --git a/examples/layouting/layouting-examples.cpp b/examples/layouting/layouting-examples.cpp index df94e65..745f92d 100644 --- a/examples/layouting/layouting-examples.cpp +++ b/examples/layouting/layouting-examples.cpp @@ -15,14 +15,19 @@ * */ +// EXTERNAL INCLUDES +#include #include -#include "shared/view.h" -#include "linear-example.h" -#include "padding-example.h" #include #include #include +// INTERNAL INCLUDES +#include "shared/view.h" +#include "linear-example.h" +#include "padding-example.h" +#include "example.h" + using namespace Dali; using namespace Dali::Toolkit; @@ -32,9 +37,19 @@ namespace const char* BACKGROUND_IMAGE( DEMO_IMAGE_DIR "lake_front.jpg" ); const char* TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" ); -const char* APPLICATION_TITLE( "Layout tester" ); +const char* APPLICATION_TITLE( "Layout Tester" ); -} // namespace +typedef std::unique_ptr< Demo::Example > ExamplePointer; +typedef std::vector< ExamplePointer > ExampleContainer; + +/// All layouting examples to be shown should be added to this method +void CreateExamples( ExampleContainer& container ) +{ + container.push_back( ExamplePointer( new Demo::LinearExample ) ); + container.push_back( ExamplePointer( new Demo::PaddingExample ) ); +} + +} // anonymous namespace class LayoutingExample: public ConnectionTracker { @@ -42,23 +57,17 @@ class LayoutingExample: public ConnectionTracker LayoutingExample( Application& application ) : mApplication( application ), - mLinearExample(), - mPaddedExample(), + mLayoutingExamples(), mLayoutIndex( 0 ) { // Connect to the Application's Init signal mApplication.InitSignal().Connect( this, &LayoutingExample::Create ); } - ~LayoutingExample() - { - // Nothing to do here - } +private: void Create( Application& application ) { - // The Init signal is received once (only) during the Application lifetime - auto stage = Stage::GetCurrent(); stage.KeyEventSignal().Connect( this, &LayoutingExample::OnKeyEvent ); @@ -88,38 +97,24 @@ class LayoutingExample: public ConnectionTracker mNextLayout.SetSize(175, 50); toolbar.Add( mNextLayout ); - mLinearExample.Demo::LinearExample::Create(); + CreateExamples( mLayoutingExamples ); + if( ! mLayoutingExamples.empty() ) + { + mLayoutingExamples[ mLayoutIndex ]->Create(); + } } bool ChangeLayout( Button button ) { - mLayoutIndex++; - - switch( mLayoutIndex ) + if( ! mLayoutingExamples.empty() ) { - case 1 : - { - mLinearExample.Remove(); - mPaddedExample.Create(); - break; - } - case 2 : - { - mPaddedExample.Remove(); - mNextLayout.SetProperty( Toolkit::Button::Property::LABEL, "end of test"); - mNextLayout.SetProperty( Toolkit::Button::Property::DISABLED, true ); - break; - } - default : - { - break; - } + mLayoutingExamples[ mLayoutIndex ]->Remove(); + mLayoutIndex = ( mLayoutIndex + 1 ) % mLayoutingExamples.size(); + mLayoutingExamples[ mLayoutIndex ]->Create(); } - return true; } -private: /** * Main key event handler */ @@ -137,8 +132,7 @@ private: private: Application& mApplication; - Demo::LinearExample mLinearExample; - Demo::PaddingExample mPaddedExample; + ExampleContainer mLayoutingExamples; PushButton mNextLayout; unsigned int mLayoutIndex; }; diff --git a/examples/layouting/linear-example.h b/examples/layouting/linear-example.h index 5779379..a1496bb 100644 --- a/examples/layouting/linear-example.h +++ b/examples/layouting/linear-example.h @@ -21,6 +21,8 @@ #include #include +#include "example.h" + using namespace Dali; using namespace Dali::Toolkit; @@ -32,16 +34,15 @@ namespace Demo * @brief Example of a Linear Layout with mirror feature and * tranisition from horizontal to vertical. */ -class LinearExample: public ConnectionTracker +class LinearExample final: public ConnectionTracker, public Example { - public: // Creates a Linear Layout Example and displays it. - void Create(); + virtual void Create() override; // Remove and destroy this layout - void Remove(); + virtual void Remove() override; private: @@ -55,10 +56,10 @@ private: PushButton mDirectionButton; PushButton mRotateButton; Control mLinearContainer; - bool mDirection; + bool mDirection = false; bool mIsHorizontal = true; }; // class LinearContainer } // namespace Demo -#endif //DALI_DEMO_LINEAR_CONTAINER_H \ No newline at end of file +#endif //DALI_DEMO_LINEAR_CONTAINER_H diff --git a/examples/layouting/padding-example.cpp b/examples/layouting/padding-example.cpp index 3c1cc45..9941ce9 100644 --- a/examples/layouting/padding-example.cpp +++ b/examples/layouting/padding-example.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * Copyright (c) 2018 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -69,18 +69,18 @@ void PaddingExample::Create() stage.Add( mHorizontalBox ); - for( unsigned int x = 0; x < NUMBER_OF_IMAGE_VIEWS; x++ ) - { - mToggleButton = Toolkit::PushButton::New(); - mToggleButton.SetProperty( Toolkit::Button::Property::LABEL, "Toggle Padding on #2" ); - mToggleButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER ); - mToggleButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER ); - mToggleButton.ClickedSignal().Connect( this, &Demo::PaddingExample::ChangePaddingClicked ); - mToggleButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); - mToggleButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT ); + mToggleButton = Toolkit::PushButton::New(); + mToggleButton.SetProperty( Toolkit::Button::Property::LABEL, "Toggle Padding on #2" ); + mToggleButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER ); + mToggleButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER ); + mToggleButton.ClickedSignal().Connect( this, &Demo::PaddingExample::ChangePaddingClicked ); + mToggleButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH ); + mToggleButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT ); - stage.Add( mToggleButton ); + stage.Add( mToggleButton ); + for( unsigned int x = 0; x < NUMBER_OF_IMAGE_VIEWS; x++ ) + { CreateChildImageView( mImageViews[x], DEMO_IMAGE_DIR "gallery-small-23.jpg" , Size(100.0f, 100.0f) ); // Set Padding for second ImageView @@ -125,4 +125,4 @@ bool PaddingExample::ChangePaddingClicked( Toolkit::Button button ) return true; } -} // namespace Demo \ No newline at end of file +} // namespace Demo diff --git a/examples/layouting/padding-example.h b/examples/layouting/padding-example.h index ce13eed..23a200a 100644 --- a/examples/layouting/padding-example.h +++ b/examples/layouting/padding-example.h @@ -2,7 +2,7 @@ #define DALI_DEMO_PADDING_EXAMPLE_H /* - * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * Copyright (c) 2018 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,8 @@ #include #include +#include "example.h" + using namespace Dali; using namespace Dali::Toolkit; @@ -33,17 +35,17 @@ namespace Demo * @brief Example of a Linear Layout with padding applied, enables updating of padding values for * one of the children. */ -class PaddingExample: public ConnectionTracker +class PaddingExample final: public ConnectionTracker, public Example { public: static const unsigned int NUMBER_OF_IMAGE_VIEWS = 3; // Create a Linear layout of ImagesViews, one with a Margin, One with padding. - void Create(); + void Create() override; // Remove created Layout - void Remove(); + void Remove() override; private: -- 2.7.4