Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / text-label-multi-language / text-label-multi-language-example.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 /**
19  * @file text-label-example.cpp
20  * @brief Basic usage of TextLabel control
21  */
22
23 // EXTERNAL INCLUDES
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali-toolkit/devel-api/controls/table-view/table-view.h>
26
27 // INTERNAL INCLUDES
28 #include "shared/multi-language-strings.h"
29 #include "shared/view.h"
30
31 using namespace Dali;
32 using namespace Dali::Toolkit;
33 using namespace MultiLanguageStrings;
34
35 /**
36  * @brief The main class of the demo.
37  */
38 class TextLabelMultiLanguageExample : public ConnectionTracker
39 {
40 public:
41   TextLabelMultiLanguageExample(Application& application)
42   : mApplication(application),
43     mLastPoint(0.f)
44   {
45     // Connect to the Application's Init signal
46     mApplication.InitSignal().Connect(this, &TextLabelMultiLanguageExample::Create);
47   }
48
49   ~TextLabelMultiLanguageExample()
50   {
51     // Nothing to do here.
52   }
53
54   /**
55    * One-time setup in response to Application InitSignal.
56    */
57   void Create(Application& application)
58   {
59     Window window = application.GetWindow();
60
61     window.KeyEventSignal().Connect(this, &TextLabelMultiLanguageExample::OnKeyEvent);
62     window.SetBackgroundColor(Color::WHITE);
63
64     mTableView = Toolkit::TableView::New(NUMBER_OF_LANGUAGES, 1);
65     mTableView.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
66     mTableView.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT);
67     mTableView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
68     mTableView.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
69     mTableView.TouchedSignal().Connect(this, &TextLabelMultiLanguageExample::OnTouch);
70     window.Add(mTableView);
71
72     for(unsigned int index = 0u; index < NUMBER_OF_LANGUAGES; ++index)
73     {
74       const Language& language = LANGUAGES[index];
75
76       TextLabel label = TextLabel::New();
77       label.SetProperty(TextLabel::Property::MULTI_LINE, true);
78
79       const std::string text = language.languageName + " " + language.languageRomanName + " " + language.text;
80       label.SetProperty(TextLabel::Property::TEXT, text);
81
82       mTableView.SetFitHeight(index);
83       mTableView.AddChild(label, Toolkit::TableView::CellPosition(index, 0));
84     }
85   }
86
87   bool OnTouch(Actor actor, const TouchEvent& event)
88   {
89     if(1u == event.GetPointCount())
90     {
91       const PointState::Type state = event.GetState(0);
92
93       // Clamp to integer values; this is to reduce flicking due to pixel misalignment
94       const float localPoint = static_cast<float>(static_cast<int>(event.GetLocalPosition(0).y));
95
96       if(PointState::DOWN == state)
97       {
98         mLastPoint = localPoint;
99         mAnimation = Animation::New(0.25f);
100       }
101       else if(PointState::MOTION == state)
102       {
103         if(mAnimation)
104         {
105           mAnimation.AnimateBy(Property(mTableView, Actor::Property::POSITION), Vector3(0.f, localPoint - mLastPoint, 0.f), AlphaFunction::LINEAR);
106           mAnimation.Play();
107           mLastPoint = localPoint;
108         }
109       }
110     }
111
112     return true;
113   }
114
115   /**
116    * Main key event handler
117    */
118   void OnKeyEvent(const KeyEvent& event)
119   {
120     if(event.GetState() == KeyEvent::DOWN)
121     {
122       if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
123       {
124         mApplication.Quit();
125       }
126     }
127   }
128
129 private:
130   Application& mApplication;
131   TableView    mTableView;
132   Animation    mAnimation;
133   float        mLastPoint;
134 };
135
136 int DALI_EXPORT_API main(int argc, char** argv)
137 {
138   Application                   application = Application::New(&argc, &argv, DEMO_THEME_PATH);
139   TextLabelMultiLanguageExample test(application);
140   application.MainLoop();
141   return 0;
142 }