Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / transitions / transition-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 /**
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/controls/control-devel.h>
28 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
29 #include <cstdio>
30 #include <sstream>
31 #include "shadow-button.h"
32
33 // Internal includes
34
35 using namespace Dali;
36 using namespace Dali::Toolkit;
37
38 namespace
39 {
40 void SetLabelText(Button button, const char* label)
41 {
42   button.SetProperty(Toolkit::Button::Property::LABEL, label);
43 }
44
45 } // namespace
46
47 namespace Demo
48 {
49 const char* TransitionApplication::DEMO_THEME_ONE_PATH(DEMO_STYLE_DIR "style-example-theme-one.json");
50 const char* TransitionApplication::DEMO_THEME_TWO_PATH(DEMO_STYLE_DIR "style-example-theme-two.json");
51
52 TransitionApplication::TransitionApplication(Application& application)
53 : mApplication(application),
54   mTitle(),
55   mShadowButton(),
56   mActionButtons(),
57   mVisualIndex(Property::INVALID_INDEX),
58   mActionIndex(Property::INVALID_INDEX)
59 {
60   application.InitSignal().Connect(this, &TransitionApplication::Create);
61 }
62
63 TransitionApplication::~TransitionApplication()
64 {
65 }
66
67 void TransitionApplication::Create(Application& application)
68 {
69   Window window = application.GetWindow();
70   window.KeyEventSignal().Connect(this, &TransitionApplication::OnKeyEvent);
71   window.SetBackgroundColor(Vector4(0.1f, 0.1f, 0.1f, 1.0f));
72
73   // Content panes:
74   TableView contentLayout = TableView::New(3, 1);
75   contentLayout.SetProperty(Dali::Actor::Property::NAME, "ContentLayout");
76   contentLayout.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
77   contentLayout.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
78   contentLayout.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
79   contentLayout.SetCellPadding(Vector2(0.0f, 5.0f));
80   contentLayout.SetBackgroundColor(Vector4(0.949, 0.949, 0.949, 1.0));
81   // Assign all rows the size negotiation property of fitting to children
82
83   window.Add(contentLayout);
84
85   mTitle = TextLabel::New("Custom Control Transition Example");
86   mTitle.SetProperty(Dali::Actor::Property::NAME, "Title");
87   mTitle.SetStyleName("Title");
88   mTitle.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
89   mTitle.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
90   mTitle.SetProperty(TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER");
91   contentLayout.Add(mTitle);
92   contentLayout.SetFitHeight(0); // Fill width
93
94   // Provide some padding around the center cell
95   TableView buttonLayout = TableView::New(3, 3);
96   buttonLayout.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
97   buttonLayout.SetFixedHeight(1, 100);
98   buttonLayout.SetFixedWidth(1, 350);
99   contentLayout.Add(buttonLayout);
100
101   mShadowButton = ShadowButton::New();
102   mShadowButton.SetProperty(Dali::Actor::Property::NAME, "ShadowButton");
103   mShadowButton.SetActiveState(false);
104   mShadowButton.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
105   mShadowButton.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
106   mShadowButton.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
107   mShadowButton.SetProperty(DevelControl::Property::STATE, DevelControl::DISABLED);
108   mShadowButton.SetProperty(DevelControl::Property::SUB_STATE, "UNCHECKED");
109
110   buttonLayout.AddChild(mShadowButton, TableView::CellPosition(1, 1));
111
112   TableView actionButtonLayout = TableView::New(1, NUMBER_OF_ACTION_BUTTONS + 1);
113   actionButtonLayout.SetProperty(Dali::Actor::Property::NAME, "ThemeButtonsLayout");
114   actionButtonLayout.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
115   actionButtonLayout.SetResizePolicy(ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT);
116   actionButtonLayout.SetFitHeight(0);
117
118   TextLabel label = TextLabel::New("Action: ");
119   label.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
120   label.SetStyleName("ActionLabel");
121   actionButtonLayout.AddChild(label, TableView::CellPosition(0, 0));
122   actionButtonLayout.SetCellAlignment(TableView::CellPosition(0, 0), HorizontalAlignment::LEFT, VerticalAlignment::CENTER);
123
124   for(int i = 0; i < NUMBER_OF_ACTION_BUTTONS; ++i)
125   {
126     mActionButtons[i] = PushButton::New();
127     mActionButtons[i].SetProperty(Dali::Actor::Property::NAME, "ActionButton");
128     mActionButtons[i].SetStyleName("ActionButton");
129     mActionButtons[i].SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
130     mActionButtons[i].SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
131     mActionIndex = mActionButtons[i].RegisterProperty("actionId", i, Property::READ_WRITE);
132     mActionButtons[i].ClickedSignal().Connect(this, &TransitionApplication::OnActionButtonClicked);
133     actionButtonLayout.AddChild(mActionButtons[i], TableView::CellPosition(0, 1 + i));
134   }
135   SetLabelText(mActionButtons[0], "Enable");
136   SetLabelText(mActionButtons[1], "Check");
137   mActionButtons[1].SetProperty(Button::Property::DISABLED, true);
138
139   contentLayout.Add(actionButtonLayout);
140   contentLayout.SetFitHeight(2);
141 }
142
143 bool TransitionApplication::OnActionButtonClicked(Button button)
144 {
145   int action = button.GetProperty<int>(mActionIndex);
146   switch(action)
147   {
148     case 0:
149     {
150       bool activeState = mShadowButton.GetActiveState();
151       mShadowButton.SetActiveState(!activeState);
152       if(activeState)
153       {
154         SetLabelText(button, "Enable");
155         mShadowButton.SetProperty(DevelControl::Property::STATE, DevelControl::DISABLED);
156       }
157       else
158       {
159         SetLabelText(button, "Disable");
160         mShadowButton.SetProperty(DevelControl::Property::STATE, DevelControl::NORMAL);
161       }
162       mActionButtons[1].SetProperty(Button::Property::DISABLED, activeState);
163       break;
164     }
165     case 1:
166     {
167       bool checkState = mShadowButton.GetCheckState();
168       mShadowButton.SetCheckState(!checkState);
169       if(checkState)
170       {
171         SetLabelText(button, "Check");
172         mShadowButton.SetProperty(DevelControl::Property::SUB_STATE, "UNCHECKED");
173       }
174       else
175       {
176         SetLabelText(button, "Uncheck");
177         mShadowButton.SetProperty(DevelControl::Property::SUB_STATE, "CHECKED");
178       }
179       break;
180     }
181     case 2:
182     {
183       break;
184     }
185     case 3:
186     {
187       break;
188     }
189   }
190
191   return true;
192 }
193
194 void TransitionApplication::OnKeyEvent(const KeyEvent& keyEvent)
195 {
196   static int keyPressed = 0;
197
198   if(keyEvent.GetState() == KeyEvent::DOWN)
199   {
200     if(keyPressed == 0) // Is this the first down event?
201     {
202       printf("Key pressed: %s %d\n", keyEvent.GetKeyName().c_str(), keyEvent.GetKeyCode());
203
204       if(IsKey(keyEvent, DALI_KEY_ESCAPE) || IsKey(keyEvent, DALI_KEY_BACK))
205       {
206         mApplication.Quit();
207       }
208       else if(keyEvent.GetKeyName().compare("Return") == 0)
209       {
210       }
211     }
212     keyPressed = 1;
213   }
214   else if(keyEvent.GetState() == KeyEvent::UP)
215   {
216     keyPressed = 0;
217   }
218 }
219
220 } // namespace Demo