Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / remote-image-loading / remote-image-loading-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 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/dali.h>
20 #include <dali/devel-api/actors/actor-devel.h>
21 #include <shared/utility.h>
22 #include <stdlib.h>
23 #include <iostream>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 // This example shows the load-time image scaling and filtering features.
29 //
30 class MyTester : public ConnectionTracker
31 {
32 public:
33   MyTester(Application& application)
34   : mApplication(application)
35   {
36     // Connect to the Application's Init signal
37     mApplication.InitSignal().Connect(this, &MyTester::Create);
38   }
39
40   ~MyTester()
41   {
42     // Nothing to do here;
43   }
44
45   void ConnectEventSignal(Control control)
46   {
47     control.TouchedSignal().Connect(this, &MyTester::OnControlTouch);
48
49     control.SetProperty(Actor::Property::KEYBOARD_FOCUSABLE, true);
50     control.KeyEventSignal().Connect(this, &MyTester::OnControlKeyEvent);
51     control.KeyInputFocusGainedSignal().Connect(this, &MyTester::OnFocusSet);
52     control.KeyInputFocusLostSignal().Connect(this, &MyTester::OnFocusUnSet);
53   }
54
55   // The Init signal is received once (only) during the Application lifetime
56   void Create(Application& application)
57   {
58     mWindow = application.GetWindow();
59     mWindow.SetBackgroundColor(Color::BLACK);
60     mWindow.KeyEventSignal().Connect(this, &MyTester::OnKey);
61     mWindow.TouchedSignal().Connect(this, &MyTester::OnTouch);
62
63     TextLabel rubric = TextLabel::New("You will need a working internet connection to see the images below");
64     rubric.SetProperty(TextLabel::Property::MULTI_LINE, true);
65     rubric.SetProperty(TextLabel::Property::TEXT_COLOR, Color::WHITE);
66     rubric.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH);
67     rubric.SetResizePolicy(ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT);
68     rubric.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_CENTER);
69     rubric.SetProperty(Actor::Property::ANCHOR_POINT, ParentOrigin::TOP_CENTER);
70     mWindow.Add(rubric);
71
72     mImageView1 = Toolkit::ImageView::New("http://static.midomi.com/s/s/images/000/000/000/000/293/259/19/520_000000000000293259191500x1500_72dpi_RGB_q70.jpg");
73
74     mImageView1.SetProperty(Dali::Actor::Property::NAME, "mImageView1");
75     mImageView1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
76     mImageView1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
77     mImageView1.SetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT, false);
78     mImageView1.SetProperty(Actor::Property::POSITION, Vector2(0, 100));
79     mImageView1.SetBackgroundColor(Vector4(0.0f, 0.0f, 0.0f, 0.0f));
80     mWindow.Add(mImageView1);
81
82     mImageView2 = Toolkit::ImageView::New("http://static.midomi.com/s/s/images/000/000/000/000/212/651/88/520_000000000000212651881500x1500_72dpi_RGB_q70.jpg");
83     mImageView2.SetProperty(Dali::Actor::Property::NAME, "mImageView2");
84     mImageView2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
85     mImageView2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
86     mImageView2.SetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT, false);
87     mImageView2.SetProperty(Actor::Property::POSITION, Vector2(400, 100));
88     mImageView2.SetBackgroundColor(Vector4(0.0f, 0.0f, 0.0f, 0.0f));
89     mWindow.Add(mImageView2);
90
91     mImageView3 = Toolkit::ImageView::New("http://static.midomi.com/s/s/images/000/000/000/000/212/353/21/520_000000000000212353211500x1500_72dpi_RGB_q70.jpg");
92     mImageView3.SetProperty(Dali::Actor::Property::NAME, "mImageView3");
93     mImageView3.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
94     mImageView3.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
95     mImageView3.SetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT, false);
96     mImageView3.SetProperty(Actor::Property::POSITION, Vector2(0, 400));
97     mImageView3.SetBackgroundColor(Vector4(0.0f, 0.0f, 0.0f, 0.0f));
98     mWindow.Add(mImageView3);
99
100     mImageView4 = Toolkit::ImageView::New("http://d2k43l0oslhof9.cloudfront.net/platform/image/contents/vc/20/01/58/20170629100630071189_0bf6b911-a847-cba4-e518-be40fe2f579420170629192203240.jpg");
101     mImageView4.SetProperty(Dali::Actor::Property::NAME, "mImageView4");
102     mImageView4.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
103     mImageView4.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
104     mImageView4.SetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT, false);
105     mImageView4.SetProperty(Actor::Property::POSITION, Vector2(400, 400));
106     mImageView4.SetBackgroundColor(Vector4(0.0f, 0.0f, 0.0f, 0.0f));
107     mWindow.Add(mImageView4);
108
109     mImageView5 = Toolkit::ImageView::New("http://static.midomi.com/h/images/w/weather_sunny.png");
110     mImageView5.SetProperty(Dali::Actor::Property::NAME, "mImageView5");
111     mImageView4.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
112     mImageView5.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_CENTER);
113     mImageView5.SetProperty(Actor::Property::POSITION_USES_ANCHOR_POINT, false);
114     mImageView5.SetProperty(Actor::Property::POSITION, Vector2(800, 100));
115     mImageView5.SetBackgroundColor(Vector4(0.0f, 0.0f, 0.0f, 0.0f));
116     mWindow.Add(mImageView5);
117
118     // Tie-in input event handlers:
119     mWindow.KeyEventSignal().Connect(this, &MyTester::OnKeyEvent);
120   }
121
122   void OnAnimationEnd(Animation& source)
123   {
124     std::cout << "OnAnimationEnd" << std::endl;
125   }
126
127   void OnKeyEvent(const KeyEvent& event)
128   {
129     if(event.GetState() == KeyEvent::DOWN)
130     {
131       if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
132       {
133         mApplication.Quit();
134       }
135     }
136   }
137
138   void OnKey(const KeyEvent& event)
139   {
140     if(event.GetState() == KeyEvent::DOWN)
141     {
142       std::cout << "Window key : " << event.GetKeyCode() << std::endl;
143     }
144   }
145
146   void OnTouch(const TouchEvent& touch)
147   {
148     if(touch.GetState(0) == PointState::DOWN)
149     {
150       std::cout << "Window touch" << std::endl;
151     }
152   }
153
154   bool OnControlKeyEvent(Toolkit::Control control, const KeyEvent& event)
155   {
156     if(event.GetState() == KeyEvent::DOWN)
157     {
158       std::cout << "Control down key : " << control.GetProperty<std::string>(Dali::Actor::Property::NAME) << ", keyCode : " << event.GetKeyCode() << std::endl;
159     }
160     else
161     {
162       std::cout << "Control up key : " << control.GetProperty<std::string>(Dali::Actor::Property::NAME) << ", keyCode : " << event.GetKeyCode() << std::endl;
163     }
164     return false;
165   }
166
167   bool OnControlTouch(Actor actor, const TouchEvent& touch)
168   {
169     if(touch.GetState(0) == PointState::DOWN)
170     {
171       std::cout << "Control touch " << actor.GetProperty<std::string>(Dali::Actor::Property::NAME) << ", parent " << actor.GetParent().GetProperty<std::string>(Dali::Actor::Property::NAME) << std::endl;
172     }
173
174     return false;
175   }
176
177   void OnFocusSet(Control control)
178   {
179     std::cout << "OnFocusSet " << control.GetProperty<std::string>(Dali::Actor::Property::NAME) << std::endl;
180   }
181
182   void OnFocusUnSet(Control control)
183   {
184     std::cout << "OnFocusUnSet " << control.GetProperty<std::string>(Dali::Actor::Property::NAME) << std::endl;
185   }
186
187 private:
188   Window       mWindow;
189   Application& mApplication;
190
191   Control   mControl1;
192   Control   mControl2;
193   ImageView mImageView1;
194   ImageView mImageView2;
195   ImageView mImageView3;
196   ImageView mImageView4;
197   ImageView mImageView5;
198
199   TextLabel  mTextLabel1;
200   TextLabel  mTextLabel2;
201   TextField  mTextField;
202   TextEditor mTextEditor;
203
204   Animation mAnimation;
205   Timer     mTimer;
206 };
207
208 int DALI_EXPORT_API main(int argc, char** argv)
209 {
210   Application application = Application::New(&argc, &argv, "");
211   MyTester    test(application);
212   application.MainLoop();
213   return 0;
214 }