DirectRendering demo
[platform/core/uifw/dali-demo.git] / examples / color-transition / color-transition-controller.cpp
1 /*
2  * Copyright (c) 2021 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 #include "color-transition-controller.h"
18 #include "dali-toolkit/dali-toolkit.h"
19 #include "dali/dali.h"
20 #include "generated/color-transition-controller-composite-frag.h"
21 #include "generated/color-transition-controller-composite-vert.h"
22 #include "utils.h"
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 namespace
28 {
29 const Vector4 BG_COLOR = Vector4(0.f, 0.f, 0.f, 0.f);
30
31 } // namespace
32
33 ColorTransitionController::ColorTransitionController(WeakHandle<RenderTaskList> window, Actor content, RenderTaskList tasks, Vector3 initialColor)
34 : mWeakRenderTasks(window)
35 {
36   auto contentSize = content.GetProperty(Actor::Property::SIZE).Get<Vector2>();
37
38   auto defaultTask = tasks.GetTask(0);
39
40   // create rendertarget and rendertask
41   auto rtt = Texture::New(TextureType::TEXTURE_2D, Pixel::Format::RGBA8888, static_cast<uint32_t>(contentSize.x), static_cast<uint32_t>(contentSize.y));
42
43   auto fbo = FrameBuffer::New(rtt.GetWidth(), rtt.GetHeight(), FrameBuffer::Attachment::NONE);
44   fbo.AttachColorTexture(rtt);
45
46   RenderTask rtCompositor = tasks.CreateTask();
47   rtCompositor.SetClearEnabled(true);
48   rtCompositor.SetClearColor(BG_COLOR);
49   rtCompositor.SetFrameBuffer(fbo);
50   rtCompositor.SetSourceActor(content);
51   rtCompositor.SetExclusive(true);
52   mRtCompositor = rtCompositor;
53
54   // renderer for the composite
55   ACTOR_DECL(composite);
56   CenterActor(composite);
57   composite.SetProperty(Actor::Property::SIZE, contentSize);
58
59   mPropFlow        = composite.RegisterProperty("uFlow", -1.f);
60   mPropUvTransform = composite.RegisterProperty("uUvTransform", Vector4(0.f, 0.f, 1.f, 1.f));
61   mPropRgb[0]      = composite.RegisterProperty("uRgb[0]", initialColor);
62   mPropRgb[1]      = composite.RegisterProperty("uRgb[1]", Vector3::ONE);
63
64   auto geomComposite = CreateQuadGeometry();
65
66   auto tsComposite = TextureSet::New();
67   tsComposite.SetTexture(0, rtt);
68
69   Sampler flowSampler = Sampler::New();
70   flowSampler.SetWrapMode(WrapMode::REPEAT, WrapMode::REPEAT);
71   tsComposite.SetSampler(1, flowSampler);
72
73   auto shdComposite = Shader::New(SHADER_COLOR_TRANSITION_CONTROLLER_COMPOSITE_VERT, SHADER_COLOR_TRANSITION_CONTROLLER_COMPOSITE_FRAG);
74
75   auto compositeRenderer = CreateRenderer(tsComposite, geomComposite, shdComposite);
76   composite.AddRenderer(compositeRenderer);
77
78   mComposite = composite;
79
80   // create transition animation
81   Animation anim = Animation::New(1.f);
82   anim.SetEndAction(Animation::EndAction::DISCARD);
83   anim.AnimateTo(Property(composite, mPropFlow), 1.f);
84   anim.FinishedSignal().Connect(this, &ColorTransitionController::OnTransitionFinished);
85   mAnimation = anim;
86 }
87
88 ColorTransitionController::~ColorTransitionController()
89 {
90   if(auto renderTasks = mWeakRenderTasks.GetHandle())
91   {
92     renderTasks.RemoveTask(mRtCompositor);
93   }
94 }
95
96 Dali::Actor ColorTransitionController::GetComposite()
97 {
98   return mComposite;
99 }
100
101 void ColorTransitionController::SetFlowMap(Texture flowMap)
102 {
103   auto renderer = mComposite.GetRendererAt(0);
104   auto texSet   = renderer.GetTextures();
105   texSet.SetTexture(1, flowMap);
106 }
107
108 void ColorTransitionController::SetUvTransform(float initialRotation, float initialScale, float targetRotation, float targetScale)
109 {
110   mComposite.SetProperty(mPropUvTransform, Vector4(initialRotation, initialScale, targetRotation, targetScale));
111 }
112
113 void ColorTransitionController::RequestTransition(float duration, const Dali::Vector3& targetColor)
114 {
115   mComposite.SetProperty(mPropRgb[1], targetColor);
116
117   mAnimation.SetDuration(duration);
118   mAnimation.SetCurrentProgress(0.f);
119   mAnimation.Play();
120 }
121
122 void ColorTransitionController::SetOnFinished(OnFinished onFinished, void* data)
123 {
124   mOnFinished     = onFinished;
125   mOnFinishedData = data;
126 }
127
128 void ColorTransitionController::OnTransitionFinished(Animation& anim)
129 {
130   // shift the secondary color down
131   Vector3 color1 = mComposite.GetProperty(mPropRgb[1]).Get<Vector3>();
132   mComposite.SetProperty(mPropRgb[0], color1);
133
134   if(mOnFinished)
135   {
136     mOnFinished(mOnFinishedData);
137   }
138 }