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