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