Changes required after Visual parent-origin & anchor-point defaults change
[platform/core/uifw/dali-demo.git] / examples / visual-transitions / transition-application.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  * @file transition-application.cpp
19  * @brief Application class for showing stylable transitions
20  */
21
22 // Class include
23 #include "transition-application.h"
24
25 // External includes
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
28 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
29 #include "beat-control.h"
30 #include <cstdio>
31 #include <sstream>
32
33 // Internal includes
34
35 using namespace Dali;
36 using namespace Dali::Toolkit;
37
38 namespace
39 {
40
41 void SetLabelText( Button button, const char* label )
42 {
43   button.SetProperty( Toolkit::Button::Property::LABEL, label );
44 }
45
46 }
47
48 namespace Demo
49 {
50
51 const char* TransitionApplication::DEMO_THEME_ONE_PATH( DEMO_STYLE_DIR "style-example-theme-one.json" );
52 const char* DALI_LOGO_PATH( DEMO_IMAGE_DIR "Logo-for-demo.png" );
53 const char* DALI_ROBOT_MODEL_PATH( DEMO_MODEL_DIR "ToyRobot-Metal.obj" );
54 const char* DALI_ROBOT_MATERIAL_PATH( DEMO_MODEL_DIR "ToyRobot-Metal.mtl" );
55
56 TransitionApplication::TransitionApplication( Application& application )
57 : mApplication( application ),
58   mTitle(),
59   mBeatControl(),
60   mActionButtons(),
61   mVisualIndex( Property::INVALID_INDEX ),
62   mActionIndex( Property::INVALID_INDEX )
63 {
64   application.InitSignal().Connect( this, &TransitionApplication::Create );
65 }
66
67 TransitionApplication::~TransitionApplication()
68 {
69 }
70
71 void TransitionApplication::Create( Application& application )
72 {
73   Stage stage = Stage::GetCurrent();
74   stage.KeyEventSignal().Connect(this, &TransitionApplication::OnKeyEvent);
75   stage.SetBackgroundColor( Vector4( 0.1f, 0.1f, 0.1f, 1.0f ) );
76
77   // Hide the indicator bar
78   application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
79
80   // Content panes:
81   TableView contentLayout = TableView::New( 4, 1 );
82   contentLayout.SetName("ContentLayout");
83   contentLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
84   contentLayout.SetAnchorPoint( AnchorPoint::TOP_LEFT );
85   contentLayout.SetParentOrigin( ParentOrigin::TOP_LEFT );
86   contentLayout.SetCellPadding( Vector2( 0.0f, 5.0f ) );
87
88   // Assign all rows the size negotiation property of fitting to children
89
90   stage.Add( contentLayout );
91
92   mTitle = TextLabel::New( "Custom Control Transition Example" );
93   mTitle.SetName( "Title" );
94   mTitle.SetStyleName("Title");
95   mTitle.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
96   mTitle.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
97   mTitle.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
98   contentLayout.Add( mTitle );
99   contentLayout.SetFitHeight(0); // Fill width
100
101   mBeatControl = BeatControl::New();
102   mBeatControl.SetName("BeatControl");
103   mBeatControl.SetProperty( BeatControl::Property::BEAT_VISUAL, Property::Map()
104                             .Add( DevelVisual::Property::TRANSFORM, Property::Map()
105                                   .Add( DevelVisual::Transform::Property::SIZE, Vector2(0.5f, 0.5f) ) ) );
106
107   mBeatControl.SetAnchorPoint( AnchorPoint::CENTER );
108   mBeatControl.SetParentOrigin( ParentOrigin::CENTER );
109   mBeatControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
110   contentLayout.Add( mBeatControl );
111   // beat control should fill the tableview cell, so no change to default parameters
112
113   TableView visualTypeLayout = TableView::New( 1, NUMBER_OF_VISUAL_BUTTONS );
114   visualTypeLayout.SetName("VisualTypeLayout");
115   visualTypeLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
116   visualTypeLayout.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
117   visualTypeLayout.SetFitHeight( 0 );
118
119   contentLayout.Add( visualTypeLayout );
120   contentLayout.SetFitHeight(2);
121
122   for( int i=0; i<NUMBER_OF_VISUAL_BUTTONS; ++i )
123   {
124     Property::Map map;
125     CreateVisualMap( i, map );
126     map.Add( DevelVisual::Property::TRANSFORM, Property::Map()
127              .Add( DevelVisual::Transform::Property::SIZE, Vector2(0.8f, 0.8f) )  );
128     mVisualButtons[i] = BeatControl::New();
129     mVisualButtons[i].SetProperty( BeatControl::Property::BEAT_VISUAL, map );
130     mVisualButtons[i].SetName("VisualButton");
131     mVisualButtons[i].SetStyleName("VisualButton");
132     mVisualButtons[i].SetSize(0, 50);
133     mVisualButtons[i].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
134     mVisualButtons[i].SetResizePolicy( ResizePolicy::FIXED, Dimension::HEIGHT );
135     mVisualIndex = mVisualButtons[i].RegisterProperty( "visualId", i, Property::READ_WRITE );
136     mVisualButtons[i].TouchSignal().Connect( this, &TransitionApplication::OnVisualButtonClicked );
137     visualTypeLayout.AddChild( mVisualButtons[i], TableView::CellPosition( 0, i ) );
138   }
139
140   TableView actionButtonLayout = TableView::New( 1, NUMBER_OF_ACTION_BUTTONS+1 );
141   actionButtonLayout.SetName("ThemeButtonsLayout");
142   actionButtonLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
143   actionButtonLayout.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
144   actionButtonLayout.SetFitHeight( 0 );
145
146   TextLabel label = TextLabel::New( "Action: ");
147   label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
148   label.SetStyleName("ActionLabel");
149   actionButtonLayout.AddChild( label, TableView::CellPosition( 0, 0 ) );
150   actionButtonLayout.SetCellAlignment( TableView::CellPosition( 0, 0 ), HorizontalAlignment::LEFT, VerticalAlignment::CENTER );
151
152   for( int i=0; i<NUMBER_OF_ACTION_BUTTONS; ++i )
153   {
154     mActionButtons[i] = PushButton::New();
155     mActionButtons[i].SetName("ActionButton");
156     mActionButtons[i].SetStyleName("ActionButton");
157     mActionButtons[i].SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
158     mActionButtons[i].SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
159     mActionIndex = mActionButtons[i].RegisterProperty( "actionId", i, Property::READ_WRITE );
160     mActionButtons[i].ClickedSignal().Connect( this, &TransitionApplication::OnActionButtonClicked );
161     actionButtonLayout.AddChild( mActionButtons[i], TableView::CellPosition( 0, 1+i ) );
162   }
163
164   SetLabelText( mActionButtons[0], "Bounce" );
165   SetLabelText( mActionButtons[1], "X" );
166   SetLabelText( mActionButtons[2], "Y" );
167   SetLabelText( mActionButtons[3], "Fade" );
168
169   contentLayout.Add( actionButtonLayout );
170   contentLayout.SetFitHeight(3);
171 }
172
173 void TransitionApplication::CreateVisualMap( int index, Property::Map& map )
174 {
175   switch(index)
176   {
177     case 0:
178     {
179       map[ Visual::Property::TYPE ] = Visual::COLOR;
180       map[ ColorVisual::Property::MIX_COLOR ] = Color::YELLOW;
181       break;
182     }
183     case 1:
184     {
185       map[ Visual::Property::TYPE ] = Visual::BORDER;
186       map[ BorderVisual::Property::COLOR ] = Color::GREEN;
187       map[ BorderVisual::Property::SIZE ] = 5;
188       break;
189     }
190     case 2:
191     {
192       map[ Visual::Property::TYPE ] = Visual::GRADIENT;
193
194       Property::Array stopOffsets;
195       stopOffsets.PushBack( 0.0f );
196       stopOffsets.PushBack( 0.3f );
197       stopOffsets.PushBack( 0.6f );
198       stopOffsets.PushBack( 0.8f );
199       stopOffsets.PushBack( 1.0f );
200       map[ GradientVisual::Property::STOP_OFFSET ] = stopOffsets;
201
202       Property::Array stopColors;
203       stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 255.f )/255.f );
204       stopColors.PushBack( Vector4( 196.f, 198.f, 71.f, 122.f )/255.f );
205       stopColors.PushBack( Vector4( 214.f, 37.f, 139.f, 191.f )/255.f );
206       stopColors.PushBack( Vector4( 129.f, 198.f, 193.f, 150.f )/255.f );
207       stopColors.PushBack( Color::YELLOW );
208       map[ GradientVisual::Property::STOP_COLOR ] = stopColors;
209       map[ GradientVisual::Property::START_POSITION ] = Vector2(-0.5f, -0.5f );
210       map[ GradientVisual::Property::END_POSITION ] = Vector2( 0.5f,  0.5f );
211       break;
212     }
213     case 3:
214     {
215       map[ Visual::Property::TYPE ] = Visual::IMAGE;
216       map[ ImageVisual::Property::URL ] = DALI_LOGO_PATH;
217       break;
218     }
219     case 4:
220     {
221       map[ Visual::Property::TYPE ] = Visual::IMAGE;
222       map[ ImageVisual::Property::URL ] = DEMO_IMAGE_DIR "preMultAlpha.png";
223       map[ DevelVisual::Property::PREMULTIPLIED_ALPHA ] = true;
224       break;
225     }
226
227     case 5:
228     {
229       map[ Visual::Property::TYPE ] = Visual::MESH;
230       map[ MeshVisual::Property::OBJECT_URL ] = DALI_ROBOT_MODEL_PATH;
231       map[ MeshVisual::Property::MATERIAL_URL ] = DALI_ROBOT_MATERIAL_PATH;
232       map[ MeshVisual::Property::TEXTURES_PATH ] = DEMO_IMAGE_DIR;
233       map[ MeshVisual::Property::SHADING_MODE ] = MeshVisual::ShadingMode::TEXTURED_WITH_DETAILED_SPECULAR_LIGHTING;
234       break;
235     }
236
237     case 6:
238     {
239       map[ Visual::Property::TYPE ] = Visual::PRIMITIVE;
240
241       map[ PrimitiveVisual::Property::SHAPE ] = PrimitiveVisual::Shape::BEVELLED_CUBE;
242       map[ PrimitiveVisual::Property::BEVEL_PERCENTAGE ] = 0.3f;
243       map[ PrimitiveVisual::Property::BEVEL_SMOOTHNESS ] = 0.0f;
244       map[ PrimitiveVisual::Property::SCALE_DIMENSIONS ] = Vector3(1.0f,1.0f,0.3f);
245       map[ PrimitiveVisual::Property::MIX_COLOR ]        = Vector4(0.7f, 0.5f, 0.05f, 1.0f);
246
247       break;
248     }
249
250     case 7:
251     {
252       // NPatch
253       map[ Visual::Property::TYPE ] = Visual::IMAGE;
254       map[ ImageVisual::Property::URL ] = DEMO_IMAGE_DIR "button-up-16.9.png";
255       break;
256     }
257     case 8:
258     {
259       // SVG
260       map[ Visual::Property::TYPE ] = Visual::IMAGE;
261       map[ ImageVisual::Property::URL ] = DEMO_IMAGE_DIR "Kid1.svg";
262       break;
263     }
264
265     case 9:
266     {
267       map[ Visual::Property::TYPE ] = DevelVisual::TEXT;
268       map[ TextVisual::Property::TEXT ] = "Text";
269       map[ TextVisual::Property::TEXT_COLOR ] = Color::CYAN;
270       map[ TextVisual::Property::POINT_SIZE ] = 10;
271       break;
272     }
273
274     default:
275     {
276       map[ Visual::Property::TYPE ] = Visual::COLOR;
277       map[ ColorVisual::Property::MIX_COLOR ] = Color::MAGENTA;
278       break;
279     }
280   }
281 }
282
283 bool TransitionApplication::OnVisualButtonClicked( Actor actor, const TouchData& touchData )
284 {
285   if( touchData.GetState(0) == PointState::FINISHED )
286   {
287     int visual = actor.GetProperty<int>( mVisualIndex );
288     Property::Map map;
289     CreateVisualMap( visual, map );
290     map.Add( DevelVisual::Property::TRANSFORM, Property::Map()
291              .Add( DevelVisual::Transform::Property::SIZE, Vector2( 0.5f, 0.5f ) ) );
292     mBeatControl.SetProperty( BeatControl::Property::BEAT_VISUAL, map );
293   }
294   return true;
295 }
296
297 bool TransitionApplication::OnActionButtonClicked( Button button )
298 {
299   int action = button.GetProperty<int>( mActionIndex );
300   switch( action )
301   {
302     case 0:
303     {
304       mBeatControl.StartBounceAnimation();
305       break;
306     }
307     case 1:
308     {
309       mBeatControl.StartXAnimation();
310       break;
311     }
312     case 2:
313     {
314       mBeatControl.StartYAnimation();
315       break;
316     }
317     case 3:
318     {
319       mBeatControl.StartFadeAnimation();
320       break;
321     }
322   }
323
324   return true;
325 }
326
327 void TransitionApplication::OnKeyEvent( const KeyEvent& keyEvent )
328 {
329   static int keyPressed = 0;
330
331   if( keyEvent.state == KeyEvent::Down)
332   {
333     if( keyPressed == 0 ) // Is this the first down event?
334     {
335       printf("Key pressed: %s %d\n", keyEvent.keyPressedName.c_str(), keyEvent.keyCode );
336
337       if( IsKey( keyEvent, DALI_KEY_ESCAPE) || IsKey( keyEvent, DALI_KEY_BACK ) )
338       {
339         mApplication.Quit();
340       }
341       else if( keyEvent.keyPressedName.compare("Return") == 0 )
342       {
343       }
344     }
345     keyPressed = 1;
346   }
347   else if( keyEvent.state == KeyEvent::Up )
348   {
349     keyPressed = 0;
350   }
351 }
352
353 } // namespace Demo