2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/dali.h>
21 #include "generated/rendering-line-frag.h"
22 #include "generated/rendering-line-vert.h"
25 using namespace Toolkit;
27 // This example shows how to draw a line in actor's color
29 class DrawLineController : public ConnectionTracker
32 DrawLineController(Application& application)
33 : mApplication(application)
35 // Connect to the Application's Init signal
36 mApplication.InitSignal().Connect(this, &DrawLineController::Create);
41 // Nothing to do here;
44 // The Init signal is received once (only) during the Application lifetime
45 void Create(Application& application)
47 // Get a handle to the window
48 Window window = application.GetWindow();
49 window.SetBackgroundColor(Color::WHITE);
51 // Step 1. Create shader
54 // Step 2. Prepare geometry
57 // Step 3. Create a renderer
60 // Step 4. Create an Actor
63 // Respond to a click anywhere on the window
64 window.GetRootLayer().TouchedSignal().Connect(this, &DrawLineController::OnTouch);
66 // Respond to key events
67 window.KeyEventSignal().Connect(this, &DrawLineController::OnKeyEvent);
70 bool OnTouch(Actor actor, const TouchEvent& touch)
72 // quit the application
78 * @brief Called when any key event is received
80 * Will use this to quit the application if Back or the Escape key is received
81 * @param[in] event The key event information
83 void OnKeyEvent(const KeyEvent& event)
85 if(event.GetState() == KeyEvent::DOWN)
87 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
95 * This function creates a line geometry made of two vertices in order
96 * to draw a diagonal line.
98 void CreateLineGeometry()
100 Vector2 vertices[] = {
101 Vector2(-1.0f, -1.0f),
102 Vector2(1.0f, 1.0f)};
104 VertexBuffer vertexBuffer = VertexBuffer::New(Property::Map()
105 .Add("aPosition", Property::VECTOR2));
106 vertexBuffer.SetData(vertices, sizeof(vertices) / sizeof(Vector2));
108 mGeometry = Geometry::New();
109 mGeometry.AddVertexBuffer(vertexBuffer);
110 mGeometry.SetType(Geometry::LINES);
114 * Creates a shader using inlined variable SHADER_RENDERING_LINE_VERT and SHADER_RENDERING_LINE_FRAG
116 * Shaders are very basic and all they do is transforming vertices and applying actor's colour.
118 void CreateLineShader()
120 mShader = Shader::New(SHADER_RENDERING_LINE_VERT, SHADER_RENDERING_LINE_FRAG);
124 * Function creates renderer.
126 void CreateRenderer()
128 mRenderer = Renderer::New(mGeometry, mShader);
132 * Creates new actor and attaches renderer.
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);
149 Application& mApplication;
157 int DALI_EXPORT_API main(int argc, char** argv)
159 Application application = Application::New(&argc, &argv);
160 DrawLineController test(application);
161 application.MainLoop();