2 * Copyright (c) 2020 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.
17 #include "color-transition-controller.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"
25 using namespace Dali::Toolkit;
30 const Vector4 BG_COLOR = Vector4(0.f, 0.f, 0.f, 0.f);
34 ColorTransitionController::ColorTransitionController(WeakHandle<RenderTaskList> window, Actor content, RenderTaskList tasks, Vector3 initialColor)
35 : mWeakRenderTasks(window)
37 auto contentSize = content.GetProperty(Actor::Property::SIZE).Get<Vector2>();
39 auto defaultTask = tasks.GetTask(0);
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));
45 auto fbo = FrameBuffer::New(rtt.GetWidth(), rtt.GetHeight(), FrameBuffer::Attachment::NONE);
46 fbo.AttachColorTexture(rtt);
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;
56 // renderer for the composite
57 ACTOR_DECL(composite);
58 CenterActor(composite);
59 composite.SetProperty(Actor::Property::SIZE, contentSize);
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);
66 auto geomComposite = CreateQuadGeometry();
68 auto tsComposite = TextureSet::New();
69 tsComposite.SetTexture(0, rtt);
71 Sampler flowSampler = Sampler::New();
72 flowSampler.SetWrapMode(WrapMode::REPEAT, WrapMode::REPEAT);
73 tsComposite.SetSampler(1, flowSampler);
75 auto shdComposite = Shader::New(SHADER_COLOR_TRANSITION_CONTROLLER_COMPOSITE_VERT, SHADER_COLOR_TRANSITION_CONTROLLER_COMPOSITE_FRAG);
77 auto compositeRenderer = CreateRenderer(tsComposite, geomComposite, shdComposite);
78 composite.AddRenderer(compositeRenderer);
80 mComposite = composite;
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);
90 ColorTransitionController::~ColorTransitionController()
92 if (auto renderTasks = mWeakRenderTasks.GetHandle())
94 renderTasks.RemoveTask(mRtCompositor);
98 Dali::Actor ColorTransitionController::GetComposite()
103 void ColorTransitionController::SetFlowMap(Texture flowMap)
105 auto renderer = mComposite.GetRendererAt(0);
106 auto texSet = renderer.GetTextures();
107 texSet.SetTexture(1, flowMap);
110 void ColorTransitionController::SetUvTransform(float initialRotation, float initialScale, float targetRotation, float targetScale)
112 mComposite.SetProperty(mPropUvTransform, Vector4(initialRotation, initialScale, targetRotation, targetScale));
115 void ColorTransitionController::RequestTransition(float duration, const Dali::Vector3& targetColor)
117 mComposite.SetProperty(mPropRgb[1], targetColor);
119 mAnimation.SetDuration(duration);
120 mAnimation.SetCurrentProgress(0.f);
124 void ColorTransitionController::SetOnFinished(OnFinished onFinished, void* data)
126 mOnFinished = onFinished;
127 mOnFinishedData = data;
130 void ColorTransitionController::OnTransitionFinished(Animation& anim)
132 // shift the secondary color down
133 Vector3 color1 = mComposite.GetProperty(mPropRgb[1]).Get<Vector3>();
134 mComposite.SetProperty(mPropRgb[0], color1);
138 mOnFinished(mOnFinishedData);