Add Layout complex animation.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / layouting / measure-spec.h
1 #ifndef DALI_TOOLKIT_LAYOUTING_MEASURE_SPEC_H
2 #define DALI_TOOLKIT_LAYOUTING_MEASURE_SPEC_H
3
4 /*
5  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 // EXTERNAL INCLUDES
21 #include <sstream>
22 #include <dali-toolkit/public-api/dali-toolkit-common.h>
23 #include <dali/public-api/common/dali-common.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/devel-api/layouting/layout-length.h>
27 #include <dali-toolkit/public-api/dali-toolkit-common.h>
28
29 namespace Dali
30 {
31 namespace Toolkit
32 {
33
34 /**
35  * A MeasureSpec is used during the Measure pass by a LayoutGroup to inform it's children
36  * how to be measured. For instance, it may measure a child with an exact width and an unspecified
37  * height in order to determine height for width.
38  */
39 class DALI_TOOLKIT_API MeasureSpec
40 {
41 public:
42
43   enum class Mode
44   {
45     UNSPECIFIED, ///< This is used by a parent to determine the desired dimension of a child layout.
46     EXACTLY, /** This is used by a parent to impose an exact size on the child. The child must use
47                  this size, and guarantee that all of its descendants will fit within this size */
48     AT_MOST /** This is used by the parent to impose a maximum size on the child. The child must guarantee
49              * that it and all of it's descendants will fit within this size. */
50   };
51
52   MeasureSpec( LayoutLength measureSpec, MeasureSpec::Mode mode )
53   : mSize( measureSpec ),
54     mMode( mode )
55   {
56   }
57
58   MeasureSpec( LayoutLength measureSpec )
59   : mSize( measureSpec ),
60     mMode( Mode::UNSPECIFIED )
61   {
62   }
63
64   ~MeasureSpec() = default;
65
66   MeasureSpec& operator=( const MeasureSpec& rhs )
67   {
68     if( this != &rhs )
69     {
70       this->mSize = rhs.mSize;
71       this->mMode = rhs.mMode;
72     }
73     return *this;
74   }
75
76   bool operator==( MeasureSpec value )
77   {
78     return mSize == value.mSize;
79   }
80
81   bool operator!=( MeasureSpec value )
82   {
83     return mSize != value.mSize;
84   }
85
86   /**
87    * @brief Set the mode of the measure spec.
88    *
89    * @param mode The mode to set
90    */
91   void SetMode( MeasureSpec::Mode mode )
92   {
93     mMode = mode;
94   }
95
96   /**
97    * @brief Get the mode of the measure spec.
98    *
99    * @return The mode of the measure spec
100    */
101   MeasureSpec::Mode GetMode() const
102   {
103     return mMode;
104   }
105
106   /**
107    * @brief Set the size of the measure spec
108    *
109    * @param size the size to set
110    */
111   void SetSize( LayoutLength size )
112   {
113     mSize = size;
114   }
115
116   /**
117    * @brief Get the size of the measure spec
118    *
119    * @return the size of the measure spec
120    */
121   LayoutLength GetSize() const
122   {
123     return mSize;
124   }
125
126   /**
127    * @brief Adjust the measure size by the given delta.
128    *
129    * Used only for EXACT and AT_MOST modes.
130    * @param[in] measureSpec the measure spec to adjust
131    * @param[in] delta A positive or negative value to adjust the measure spec by.
132    *
133    * @note if the adjusted size is negative, it is zeroed.
134    * @return A new measure spec with the adjusted values.
135    */
136   static MeasureSpec Adjust( MeasureSpec measureSpec, int delta )
137   {
138     auto mode = measureSpec.GetMode();
139     auto size = measureSpec.GetSize();
140
141     if( mode == MeasureSpec::Mode::UNSPECIFIED )
142     {
143       return MeasureSpec( size, MeasureSpec::Mode::UNSPECIFIED );
144     }
145
146     if( delta < 0 && measureSpec.mSize < abs(delta) )
147     {
148       size = 0;
149     }
150     else
151     {
152       size += delta;
153     }
154     return MeasureSpec( size, mode );
155   }
156
157 private:
158
159   LayoutLength  mSize; ///< The specified size
160   Mode     mMode; ///< The measure mode
161
162 };
163
164 inline std::ostream& operator<< (std::ostream& o, const MeasureSpec& measureSpec )
165 {
166   return o << ( (measureSpec.GetMode() == MeasureSpec::Mode::UNSPECIFIED ? "Unspecified"
167                  : (measureSpec.GetMode() == MeasureSpec::Mode::EXACTLY ? "Exactly":"At most" ) ) )
168            << " " << measureSpec.GetSize();
169 }
170
171 } //namespace Toolkit
172 } //namespace Dali
173
174
175 #endif // DALI_TOOLKIT_LAYOUTING_MEASURE_SPEC_H