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