Using migrated Public Visual API
[platform/core/uifw/dali-demo.git] / examples / simple-visuals-control / simple-visuals-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 // CLASS HEADER
18 #include "my-control.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali-toolkit/dali-toolkit.h>
22 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
23 #include <dali/integration-api/events/touch-event-integ.h>
24 #include <cstdio>
25 #include <sstream>
26
27 // INTERNAL INCLUDES
28 #include "simple-visuals-application.h"
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 namespace
34 {
35
36 }
37
38 namespace Demo
39 {
40
41 const char* ICON_IMAGE( DEMO_IMAGE_DIR  "application-icon-13.png" );
42
43 SimpleVisualsApplication::SimpleVisualsApplication( Application& application )
44 : mApplication( application ),
45   mMyControl()
46 {
47   application.InitSignal().Connect( this, &SimpleVisualsApplication::Create );
48 }
49
50 Dali::Actor SimpleVisualsApplication::OnKeyboardPreFocusChange( Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocus::Direction direction )
51 {
52   Actor nextFocusActor = proposed;
53
54   if( !current && !proposed  )
55   {
56     // Set the initial focus to the first tile in the current page should be focused.
57     nextFocusActor = mMyControl;
58   }
59   else
60   {
61     if ( current == mMyControl )
62     {
63       nextFocusActor = mMyControl2;
64     }
65     else
66     {
67       nextFocusActor = mMyControl;
68     }
69   }
70
71   return nextFocusActor;
72 }
73
74
75 void SimpleVisualsApplication::OnKeyEvent( const KeyEvent& keyEvent )
76 {
77   static int keyPressed = 0;
78
79   if( keyEvent.state == KeyEvent::Down)
80   {
81     if( keyPressed == 0 ) // Is this the first down event?
82     {
83       printf("Key pressed: %s %d\n", keyEvent.keyPressedName.c_str(), keyEvent.keyCode );
84
85       if( IsKey( keyEvent, DALI_KEY_ESCAPE) || IsKey( keyEvent, DALI_KEY_BACK ) )
86       {
87         mApplication.Quit();
88       }
89       else if( keyEvent.keyPressedName.compare("Return") == 0 )
90       {
91       }
92     }
93     keyPressed = 1;
94   }
95   else if( keyEvent.state == KeyEvent::Up )
96   {
97     keyPressed = 0;
98   }
99 }
100
101 void SimpleVisualsApplication::Create( Application& application )
102 {
103   Stage stage = Stage::GetCurrent();
104   stage.SetBackgroundColor( Vector4( 0.1f, 0.1f, 0.1f, 1.0f ) );
105
106   // Connect to key events so can quit application
107   stage.KeyEventSignal().Connect(this, &SimpleVisualsApplication::OnKeyEvent);
108
109   // Hide the indicator bar
110   application.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
111
112   // Create a table view to parent the 2 MyControls
113   TableView contentLayout = TableView::New( 2, 2 );
114   contentLayout.SetName("ContentLayout");
115   contentLayout.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
116   contentLayout.SetResizePolicy( ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT );
117   contentLayout.SetSizeModeFactor( Vector3( 1.0f, .5f, 1.0f ) );
118   contentLayout.SetAnchorPoint( AnchorPoint::CENTER );
119   contentLayout.SetParentOrigin( ParentOrigin::CENTER );
120   contentLayout.SetCellPadding( Vector2( 50.0f, 15.0f ) );
121   contentLayout.SetBackgroundColor( Vector4(0.949, 0.949, 0.949, 1.0) );
122
123   // Listen to focus change so can see Visual change from NORMAL to FOCUSED state
124   KeyboardFocusManager::Get().PreFocusChangeSignal().Connect( this, &SimpleVisualsApplication::OnKeyboardPreFocusChange );
125
126   stage.Add( contentLayout );
127
128   // Create 2 MyControls and add to table view.
129   mMyControl = MyControl::New();
130   mMyControl.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
131   mMyControl.SetParentOrigin(ParentOrigin::TOP_LEFT);
132
133   mMyControl2 = MyControl::New();
134   mMyControl2.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
135   mMyControl2.SetParentOrigin(ParentOrigin::CENTER);
136
137   contentLayout.AddChild( mMyControl2, TableView::CellPosition(0, 0) );
138   contentLayout.AddChild( mMyControl, TableView::CellPosition(0, 1) );
139 }
140
141 } // namespace Demo