Revert "[Tizen] Add codes for Dali Windows Backend"
[platform/core/uifw/dali-demo.git] / examples / layouting / absolute-example.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <string>
19 #include "absolute-example.h"
20 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
21 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
22 #include <dali-toolkit/devel-api/controls/control-devel.h>
23 #include <dali-toolkit/devel-api/layouting/absolute-layout.h>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28
29 namespace
30 {
31
32 struct ImageDetails
33 {
34   const char * name;
35   Vector2 position;
36   Size size;
37 };
38
39 ImageDetails IMAGES[] =
40 {
41   { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 0.0f,   0.0f ), Size( 100.0f, 100.0f ) },
42   { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 100.0f,   0.0f ), Size( 100.0f, 100.0f ) },
43   { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 0.0f, 100.0f ), Size( 100.0f, 100.0f ) },
44   { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 200.0f, 200.0f ), Size( 100.0f, 100.0f ) },
45 };
46 unsigned int IMAGE_COUNT=sizeof(IMAGES)/sizeof(IMAGES[0]);
47
48 // Helper function to create ImageViews with given filename and size.
49 void CreateChildImageView( ImageView& imageView, unsigned imageIndex )
50 {
51   imageView = ImageView::New();
52   Property::Map imagePropertyMap;
53   imagePropertyMap[ Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
54   imagePropertyMap[ ImageVisual::Property::URL ] = IMAGES[imageIndex].name;
55   imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = IMAGES[imageIndex].size.width ;
56   imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = IMAGES[imageIndex].size.height;
57   imageView.SetProperty( ImageView::Property::IMAGE , imagePropertyMap );
58   imageView.SetName("ImageView");
59   imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
60   imageView.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
61   imageView.SetProperty( Dali::Actor::Property::POSITION, Vector3( IMAGES[imageIndex].position ) );
62 }
63
64 } // namespace
65
66 namespace Demo
67 {
68
69 AbsoluteExample::AbsoluteExample()
70 : mRootLayoutControl(),
71   mAbsoluteLayoutContainer(),
72   mLayoutSizeToggleStatus( true ),
73   mToggleButton()
74 {
75
76 }
77
78 void AbsoluteExample::Create()
79 {
80   // Create a root layout, ideally Dali would have a default layout in the root layer.
81   // Without this root layer the mAbsoluteLayout (or any other layout) will not
82   // honour WIDTH_SPECIFICATION or HEIGHT_SPECIFICATION settings.
83   // It uses the default stage size and ideally should have a layout added to it.
84   auto stage = Stage::GetCurrent();
85   mRootLayoutControl = Control::New();
86   auto rootLayout = AbsoluteLayout::New();
87   DevelControl::SetLayout( mRootLayoutControl, rootLayout );
88   mRootLayoutControl.SetAnchorPoint( AnchorPoint::CENTER );
89   mRootLayoutControl.SetParentOrigin( ParentOrigin::CENTER );
90   stage.Add( mRootLayoutControl );
91
92   // Create an Absolute Layout to show ImageViews at explictly provided positions.
93   mAbsoluteLayoutContainer = Control::New();
94   mAbsoluteLayoutContainer.SetBackgroundColor( Color::WHITE );
95   auto absoluteLayout = AbsoluteLayout::New();
96   DevelControl::SetLayout( mAbsoluteLayoutContainer, absoluteLayout );
97   mAbsoluteLayoutContainer.SetName("AbsoluteLayout");
98
99   mAbsoluteLayoutContainer.SetAnchorPoint( AnchorPoint::CENTER );
100   mAbsoluteLayoutContainer.SetParentOrigin( ParentOrigin::CENTER );
101
102   // Initially absolute layout to use these specifications, toggle button will alter them.
103   mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
104   mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  ChildLayoutData::WRAP_CONTENT );
105
106   mRootLayoutControl.Add( mAbsoluteLayoutContainer );
107
108   for( unsigned int x = 0; x < NUMBER_OF_IMAGE_VIEWS; x++ )
109   {
110     CreateChildImageView( mImageViews[x], x%IMAGE_COUNT );
111     mAbsoluteLayoutContainer.Add( mImageViews[x] );
112   }
113
114   // Button toggles the size of the layout
115   mToggleButton = PushButton::New();
116   mToggleButton.SetProperty( Toolkit::Button::Property::LABEL, "Change layout size" );
117   mToggleButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
118   mToggleButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
119   mToggleButton.ClickedSignal().Connect( this, &Demo::AbsoluteExample::ChangeSizeClicked );
120   mToggleButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
121   mToggleButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
122
123   stage.Add( mToggleButton );
124
125 }
126
127 void AbsoluteExample::Remove()
128 {
129   UnparentAndReset( mAbsoluteLayoutContainer );
130   UnparentAndReset( mToggleButton );
131   UnparentAndReset( mRootLayoutControl );
132 }
133
134 bool AbsoluteExample::ChangeSizeClicked( Toolkit::Button button )
135 {
136   if ( true == mLayoutSizeToggleStatus )
137   {
138     mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, 480 );
139     mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  700 );
140     mLayoutSizeToggleStatus = false;
141   }
142   else
143   {
144     mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
145     mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  ChildLayoutData::WRAP_CONTENT );
146     mLayoutSizeToggleStatus = true;
147   }
148
149   return true;
150 }
151
152 } // namespace Demo