Changes after TouchedSignal changes
[platform/core/uifw/dali-demo.git] / examples / rendering-line / rendering-line.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/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20
21 using namespace Dali;
22 using namespace Toolkit;
23
24 namespace
25 {
26
27 /*
28  * Vertex shader
29  */
30 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
31 attribute mediump vec2 aPosition;\n // DALi shader builtin
32 uniform   mediump mat4 uMvpMatrix;\n // DALi shader builtin
33 uniform   mediump vec3 uSize;\n // DALi shader builtin
34 \n
35 void main()\n
36 {\n
37   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
38   vertexPosition.xyz *= uSize;\n
39   gl_Position = uMvpMatrix * vertexPosition;\n
40 }\n
41 );
42
43 /*
44  * Fragment shader
45  */
46 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
47 uniform mediump vec4 uColor;\n
48 \n
49 void main()\n
50 {\n
51   gl_FragColor = uColor;\n
52 }\n
53 );
54
55 }
56
57 // This example shows how to draw a line in actor's color
58 //
59 class DrawLineController : public ConnectionTracker
60 {
61 public:
62
63   DrawLineController( Application& application )
64   : mApplication( application )
65   {
66     // Connect to the Application's Init signal
67     mApplication.InitSignal().Connect( this, &DrawLineController::Create );
68   }
69
70   ~DrawLineController()
71   {
72     // Nothing to do here;
73   }
74
75   // The Init signal is received once (only) during the Application lifetime
76   void Create( Application& application )
77   {
78     // Get a handle to the window
79     Window window = application.GetWindow();
80     window.SetBackgroundColor( Color::WHITE );
81
82     // Step 1. Create shader
83     CreateLineShader();
84
85     // Step 2. Prepare geometry
86     CreateLineGeometry();
87
88     // Step 3. Create a renderer
89     CreateRenderer();
90
91     // Step 4. Create an Actor
92     CreateActor();
93
94     // Respond to a click anywhere on the window
95     window.GetRootLayer().TouchedSignal().Connect( this, &DrawLineController::OnTouch );
96
97     // Respond to key events
98     window.KeyEventSignal().Connect( this, &DrawLineController::OnKeyEvent );
99   }
100
101   bool OnTouch( Actor actor, const TouchEvent& touch )
102   {
103     // quit the application
104     mApplication.Quit();
105     return true;
106   }
107
108   /**
109    * @brief Called when any key event is received
110    *
111    * Will use this to quit the application if Back or the Escape key is received
112    * @param[in] event The key event information
113    */
114   void OnKeyEvent( const KeyEvent& event )
115   {
116     if( event.GetState() == KeyEvent::DOWN )
117     {
118       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
119       {
120         mApplication.Quit();
121       }
122     }
123   }
124
125   /**
126    * This function creates a line geometry made of two vertices in order
127    * to draw a diagonal line.
128    */
129   void CreateLineGeometry()
130   {
131     Vector2 vertices[] = {
132       Vector2( -1.0f, -1.0f ),
133       Vector2(  1.0f,  1.0f )
134     };
135
136     VertexBuffer vertexBuffer = VertexBuffer::New( Property::Map()
137                                                    .Add( "aPosition", Property::VECTOR2 ) );
138     vertexBuffer.SetData( vertices, sizeof(vertices) / sizeof(Vector2) );
139
140     mGeometry = Geometry::New();
141     mGeometry.AddVertexBuffer( vertexBuffer );
142     mGeometry.SetType( Geometry::LINES );
143   }
144
145   /**
146    * Creates a shader using inlined variable VERTEX_SHADER and FRAGMENT_SHADER
147    *
148    * Shaders are very basic and all they do is transforming vertices and applying actor's colour.
149    */
150   void CreateLineShader()
151   {
152     mShader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER );
153   }
154
155   /**
156    * Function creates renderer.
157    */
158   void CreateRenderer()
159   {
160     mRenderer = Renderer::New( mGeometry, mShader );
161   }
162
163   /**
164    * Creates new actor and attaches renderer.
165    */
166   void CreateActor()
167   {
168     Window window = mApplication.GetWindow();
169     Size size = Vector2(window.GetSize()) * 0.25f;
170     mActor = Actor::New();
171     mActor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
172     mActor.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
173     mActor.SetProperty( Actor::Property::POSITION, Vector3( 0.0f, 0.0f, 0.0f ) );
174     mActor.SetProperty( Actor::Property::COLOR, Color::BLACK );
175     mActor.SetProperty( Actor::Property::SIZE, Vector3( size.x, size.x, size.x ) );
176     mActor.AddRenderer( mRenderer );
177     window.Add( mActor );
178   }
179
180 private:
181   Application&  mApplication;
182
183   Renderer mRenderer;
184   Shader mShader;
185   Geometry mGeometry;
186   Actor mActor;
187 };
188
189 int DALI_EXPORT_API main( int argc, char **argv )
190 {
191   Application application = Application::New( &argc, &argv );
192   DrawLineController test( application );
193   application.MainLoop();
194   return 0;
195 }