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.
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"
25 using namespace Dali::Toolkit;
29 const Vector4 BG_COLOR = Vector4(0.f, 0.f, 0.f, 0.f);
33 ColorTransitionController::ColorTransitionController(WeakHandle<RenderTaskList> window, Actor content, RenderTaskList tasks, Vector3 initialColor)
34 : mWeakRenderTasks(window)
36 auto contentSize = content.GetProperty(Actor::Property::SIZE).Get<Vector2>();
38 auto defaultTask = tasks.GetTask(0);
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));
43 auto fbo = FrameBuffer::New(rtt.GetWidth(), rtt.GetHeight(), FrameBuffer::Attachment::NONE);
44 fbo.AttachColorTexture(rtt);
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;
54 // renderer for the composite
55 ACTOR_DECL(composite);
56 CenterActor(composite);
57 composite.SetProperty(Actor::Property::SIZE, contentSize);
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);
64 auto geomComposite = CreateQuadGeometry();
66 auto tsComposite = TextureSet::New();
67 tsComposite.SetTexture(0, rtt);
69 Sampler flowSampler = Sampler::New();
70 flowSampler.SetWrapMode(WrapMode::REPEAT, WrapMode::REPEAT);
71 tsComposite.SetSampler(1, flowSampler);
73 auto shdComposite = Shader::New(SHADER_COLOR_TRANSITION_CONTROLLER_COMPOSITE_VERT, SHADER_COLOR_TRANSITION_CONTROLLER_COMPOSITE_FRAG);
75 auto compositeRenderer = CreateRenderer(tsComposite, geomComposite, shdComposite);
76 composite.AddRenderer(compositeRenderer);
78 mComposite = composite;
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);
88 ColorTransitionController::~ColorTransitionController()
90 if(auto renderTasks = mWeakRenderTasks.GetHandle())
92 renderTasks.RemoveTask(mRtCompositor);
96 Dali::Actor ColorTransitionController::GetComposite()
101 void ColorTransitionController::SetFlowMap(Texture flowMap)
103 auto renderer = mComposite.GetRendererAt(0);
104 auto texSet = renderer.GetTextures();
105 texSet.SetTexture(1, flowMap);
108 void ColorTransitionController::SetUvTransform(float initialRotation, float initialScale, float targetRotation, float targetScale)
110 mComposite.SetProperty(mPropUvTransform, Vector4(initialRotation, initialScale, targetRotation, targetScale));
113 void ColorTransitionController::RequestTransition(float duration, const Dali::Vector3& targetColor)
115 mComposite.SetProperty(mPropRgb[1], targetColor);
117 mAnimation.SetDuration(duration);
118 mAnimation.SetCurrentProgress(0.f);
122 void ColorTransitionController::SetOnFinished(OnFinished onFinished, void* data)
124 mOnFinished = onFinished;
125 mOnFinishedData = data;
128 void ColorTransitionController::OnTransitionFinished(Animation& anim)
130 // shift the secondary color down
131 Vector3 color1 = mComposite.GetProperty(mPropRgb[1]).Get<Vector3>();
132 mComposite.SetProperty(mPropRgb[0], color1);
136 mOnFinished(mOnFinishedData);