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