Updated demos to use DALi clang-format
[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 namespace Demo
39 {
40 const char* ICON_IMAGE(DEMO_IMAGE_DIR "application-icon-13.png");
41
42 SimpleVisualsApplication::SimpleVisualsApplication(Application& application)
43 : mApplication(application),
44   mMyControl()
45 {
46   application.InitSignal().Connect(this, &SimpleVisualsApplication::Create);
47 }
48
49 Dali::Actor SimpleVisualsApplication::OnKeyboardPreFocusChange(Dali::Actor current, Dali::Actor proposed, Dali::Toolkit::Control::KeyboardFocus::Direction direction)
50 {
51   Actor nextFocusActor = proposed;
52
53   if(!current && !proposed)
54   {
55     // Set the initial focus to the first tile in the current page should be focused.
56     nextFocusActor = mMyControl;
57   }
58   else
59   {
60     if(current == mMyControl)
61     {
62       nextFocusActor = mMyControl2;
63     }
64     else
65     {
66       nextFocusActor = mMyControl;
67     }
68   }
69
70   return nextFocusActor;
71 }
72
73 void SimpleVisualsApplication::OnKeyEvent(const KeyEvent& keyEvent)
74 {
75   static int keyPressed = 0;
76
77   if(keyEvent.GetState() == KeyEvent::DOWN)
78   {
79     if(keyPressed == 0) // Is this the first down event?
80     {
81       printf("Key pressed: %s %d\n", keyEvent.GetKeyName().c_str(), keyEvent.GetKeyCode());
82
83       if(IsKey(keyEvent, DALI_KEY_ESCAPE) || IsKey(keyEvent, DALI_KEY_BACK))
84       {
85         mApplication.Quit();
86       }
87       else if(keyEvent.GetKeyName().compare("Return") == 0)
88       {
89       }
90     }
91     keyPressed = 1;
92   }
93   else if(keyEvent.GetState() == KeyEvent::UP)
94   {
95     keyPressed = 0;
96   }
97 }
98
99 void SimpleVisualsApplication::Create(Application& application)
100 {
101   Window window = application.GetWindow();
102   window.SetBackgroundColor(Vector4(0.1f, 0.1f, 0.1f, 1.0f));
103
104   // Connect to key events so can quit application
105   window.KeyEventSignal().Connect(this, &SimpleVisualsApplication::OnKeyEvent);
106
107   // Create a table view to parent the 2 MyControls
108   TableView contentLayout = TableView::New(2, 2);
109   contentLayout.SetProperty(Dali::Actor::Property::NAME, "ContentLayout");
110   contentLayout.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
111   contentLayout.SetResizePolicy(ResizePolicy::SIZE_RELATIVE_TO_PARENT, Dimension::HEIGHT);
112   contentLayout.SetProperty(Actor::Property::SIZE_MODE_FACTOR, Vector3(1.0f, .5f, 1.0f));
113   contentLayout.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
114   contentLayout.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
115   contentLayout.SetCellPadding(Vector2(50.0f, 15.0f));
116   contentLayout.SetBackgroundColor(Vector4(0.949, 0.949, 0.949, 1.0));
117
118   // Listen to focus change so can see Visual change from NORMAL to FOCUSED state
119   KeyboardFocusManager::Get().PreFocusChangeSignal().Connect(this, &SimpleVisualsApplication::OnKeyboardPreFocusChange);
120
121   window.Add(contentLayout);
122
123   // Create 2 MyControls and add to table view.
124   mMyControl = MyControl::New();
125   mMyControl.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
126   mMyControl.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
127
128   mMyControl2 = MyControl::New();
129   mMyControl2.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
130   mMyControl2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
131
132   contentLayout.AddChild(mMyControl2, TableView::CellPosition(0, 0));
133   contentLayout.AddChild(mMyControl, TableView::CellPosition(0, 1));
134 }
135
136 } // namespace Demo