b7d12f67fda8cd0155dde894564920ee13a52486
[platform/core/uifw/dali-demo.git] / examples / tilt / tilt-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/devel-api/adaptor-framework/tilt-sensor.h>
20
21 using namespace Dali;
22 using Dali::Toolkit::TextLabel;
23
24 // This example shows how to use sensor using a simple TextLabel
25 //
26 class TiltController : public ConnectionTracker
27 {
28 public:
29   TiltController( Application& application )
30   : mApplication( application )
31   {
32     // Connect to the Application's Init signal
33     mApplication.InitSignal().Connect( this, &TiltController::Create );
34   }
35
36   ~TiltController()
37   {
38     // Nothing to do here;
39   }
40
41   // The Init signal is received once (only) during the Application lifetime
42   void Create( Application& application )
43   {
44     // Get a handle to the window
45     Window window = application.GetWindow();
46     window.SetBackgroundColor( Color::BLUE);
47
48     mTextLabel = TextLabel::New( "Tilt Sensor Demo" );
49     mTextLabel.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
50     mTextLabel.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
51     mTextLabel.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, "CENTER" );
52     mTextLabel.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, "CENTER" );
53     mTextLabel.SetProperty( TextLabel::Property::TEXT_COLOR, Color::WHITE );
54     mTextLabel.SetProperty( TextLabel::Property::POINT_SIZE, 15.0f );
55     mTextLabel.SetProperty( Dali::Actor::Property::NAME, "tiltLabel" );
56     window.Add( mTextLabel );
57
58     // Respond to a click anywhere on the window
59     window.GetRootLayer().TouchSignal().Connect( this, &TiltController::OnTouch );
60
61     CreateSensor();
62
63     // Connect signals to allow Back and Escape to exit.
64     window.KeyEventSignal().Connect( this, &TiltController::OnKeyEvent );
65   }
66
67   void CreateSensor()
68   {
69     mTiltSensor = TiltSensor::Get();
70     if ( mTiltSensor.Start() )
71     {
72       // Get notifications when the device is tilted
73       mTiltSensor.TiltedSignal().Connect( this, &TiltController::OnTilted );
74     }
75   }
76
77   bool OnTouch( Actor actor, const TouchEvent& touch )
78   {
79     // quit the application
80     mApplication.Quit();
81     return true;
82   }
83
84   void OnTilted(const TiltSensor& sensor)
85   {
86     Quaternion pitchRot(Radian(Degree(sensor.GetPitch() * 90.0f)), Vector3(1, 0, 0));
87     Quaternion rollRot(Radian(Degree(sensor.GetRoll() * 90.0f)), Vector3(0, 1, 0));
88
89     mTextLabel.SetProperty( Actor::Property::ORIENTATION, Quaternion() );
90     mTextLabel.RotateBy(rollRot);
91     mTextLabel.RotateBy(pitchRot);;
92   }
93
94   /**
95    * @brief OnKeyEvent signal handler.
96    * @param[in] event The key event information
97    */
98   void OnKeyEvent( const KeyEvent& event )
99   {
100     if( event.GetState() == KeyEvent::DOWN )
101     {
102       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
103       {
104         mApplication.Quit();
105       }
106     }
107   }
108
109 private:
110   Application&  mApplication;
111   TiltSensor mTiltSensor;
112   TextLabel mTextLabel;
113 };
114
115 int DALI_EXPORT_API main( int argc, char **argv )
116 {
117   Application application = Application::New( &argc, &argv );
118   TiltController test( application );
119
120   application.MainLoop();
121   return 0;
122 }