1 #ifndef __DALI_TOOLKIT_NINE_PATCH_MASK_EFFECT_H__
2 #define __DALI_TOOLKIT_NINE_PATCH_MASK_EFFECT_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/actors/image-actor.h>
31 * @brief NinePatchMaskEffect is used to control which parts of an image are visible, using the alpha channel of a separate mask image.
33 * The mask image is expected to be smaller than the main image being viewed.
34 * Conceptually the mask image is divided into a 3x3 grid (9 patches). The middle patch is stretched whilst the outer border is not.
39 * ImageActor actor = ImageActor::New( Image( EXAMPLE_IMAGE_PATH ) );
40 * NinePatchMaskEffect::Apply( actor, MASK_IMAGE_PATH );
43 * NinePatchMaskEffect is mutually exclusive with ImageActor::STYLE_NINE_PATCH i.e. the actor's main image should not be a nine-patch.
45 namespace NinePatchMaskEffect
48 static void NinePatchMaskEffectSizeConstraint( Vector2& current, const PropertyInputContainer& inputs )
50 const Vector3& actorSize = inputs[0]->GetVector3();
51 current.x = actorSize.x;
52 current.y = actorSize.y;
55 inline void DoApply( ImageActor actor, const std::string& maskImage, const Vector2& maskSize, Vector4 maskBorder )
57 const char* ALPHA_MASK_VERTEX_SHADER_SOURCE =
58 "precision mediump float;\n"
59 "uniform vec2 uImageSize; \n"
60 "uniform vec2 uMaskSize; \n"
61 "varying vec2 vMaskTexCoord; \n"
65 " gl_Position = uMvpMatrix * vec4(aPosition, 1.0); \n"
67 " // Ignore mask UVs for image \n"
69 " highp vec2 halfImageSize = uImageSize * 0.5; \n"
70 " vTexCoord = (aPosition.xy + halfImageSize) / uImageSize; \n"
72 " // UVs were calculated for image size, so convert for mask size \n"
74 " highp vec2 halfMaskSize = uMaskSize * 0.5; \n"
75 " highp vec2 halfSizeDelta = halfImageSize - halfMaskSize; \n"
77 " highp vec2 maskPosition = aPosition.xy; \n"
78 " maskPosition.x -= halfSizeDelta.x * sign(aPosition.x); \n"
79 " maskPosition.y -= halfSizeDelta.y * sign(aPosition.y); \n"
81 " vMaskTexCoord = (maskPosition + halfMaskSize) / uMaskSize; \n"
84 const char* ALPHA_MASK_FRAGMENT_SHADER_SOURCE =
85 "varying mediump vec2 vMaskTexCoord; \n"
89 " highp vec4 mask = texture2D(sEffect, vMaskTexCoord); \n"
90 " gl_FragColor = texture2D(sTexture, vTexCoord) * uColor * vec4(1,1,1,mask.a); \n"
93 ShaderEffect maskEffect = ShaderEffect::New(
94 ALPHA_MASK_VERTEX_SHADER_SOURCE,
95 ALPHA_MASK_FRAGMENT_SHADER_SOURCE,
96 GeometryType( GEOMETRY_TYPE_IMAGE ),
97 ShaderEffect::GeometryHints( ShaderEffect::HINT_BLENDING ) );
99 maskEffect.SetEffectImage( ResourceImage::New( maskImage ) );
101 maskEffect.SetUniform( "uImageSize", Vector2(0,0) /*Constrained to actor size*/ );
103 Constraint constraint = Constraint::New<Vector2>( maskEffect, maskEffect.GetPropertyIndex("uImageSize"), NinePatchMaskEffectSizeConstraint );
104 constraint.AddSource( Source(actor, Actor::Property::SIZE) );
107 maskEffect.SetUniform( "uMaskSize", maskSize );
109 // Actor must provide nine-patch style geometry for this effect to work
110 actor.SetStyle( ImageActor::STYLE_NINE_PATCH );
111 actor.SetNinePatchBorder( maskBorder );
113 actor.SetShaderEffect( maskEffect );
117 * @brief Apply the mask effect to an ImageActor.
119 * NinePatchMaskEffect is mutually exclusive with ImageActor::STYLE_NINE_PATCH i.e. the actor's main image should not be a nine-patch.
120 * @param [in] actor The actor which needs the effect. To remove the effect call actor.RemoveShaderEffect().
121 * @param [in] maskImage The path to a file containing the mask. The center pixels of the mask will be stretched.
123 inline void Apply( ImageActor actor, const std::string& maskImage )
125 const Uint16Pair maskSize = ResourceImage::GetImageSize( maskImage );
127 const float leftRight = (maskSize.GetWidth() - 1.0f) * 0.5f;
128 const float topBottom = (maskSize.GetHeight() - 1.0f) * 0.5f;
130 DoApply( actor, maskImage, Vector2( maskSize.GetWidth(), maskSize.GetHeight() ), Vector4( leftRight, topBottom, leftRight, topBottom ) );
134 * @brief Apply the mask effect to an ImageActor.
136 * NinePatchMaskEffect is mutually exclusive with ImageActor::STYLE_NINE_PATCH i.e. the actor's main image should not be a nine-patch.
137 * @param [in] actor The actor which needs the effect. To remove the effect call actor.RemoveShaderEffect().
138 * @param [in] maskImage The path to a file containing the mask.
139 * @param [in] maskBorder Specifies the part of the mask image that will be stretched (left, top, right, bottom).
141 inline void Apply( ImageActor actor, const std::string& maskImage, const Vector4& maskBorder )
143 const Uint16Pair maskSize = ResourceImage::GetImageSize( maskImage );
145 DoApply( actor, maskImage, Vector2( maskSize.GetWidth(), maskSize.GetHeight() ), maskBorder );
148 } // namespace NinePatchMaskEffect
150 } // namespace Toolkit
154 #endif // __DALI_TOOLKIT_NINE_PATCH_MASK_EFFECT_H__