Merge "Added a simple application for testing Styling" into devel/master
[platform/core/uifw/dali-demo.git] / examples / styling / image-channel-control-impl.cpp
1 /*
2  * Copyright (c) 2016 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 "image-channel-control-impl.h"
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/public-api/object/type-registry-helper.h>
20 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
21
22 using namespace Dali; // Needed for macros
23
24 namespace Demo
25 {
26 namespace Internal
27 {
28
29 namespace
30 {
31
32 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
33   varying mediump vec2 vTexCoord;\n
34   uniform sampler2D sTexture;\n
35   uniform mediump vec4 uColor;\n
36   uniform mediump vec3 uChannels;\n
37   \n
38   void main()\n
39   {\n
40     gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor * vec4(uChannels, 1.0) ;\n
41   }\n
42 );
43
44 Dali::BaseHandle Create()
45 {
46   return Demo::ImageChannelControl::New();
47 }
48
49 DALI_TYPE_REGISTRATION_BEGIN( ImageChannelControl, Dali::Toolkit::Control, Create );
50
51 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "url", STRING, RESOURCE_URL );
52 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "redChannel", FLOAT, RED_CHANNEL );
53 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "greenChannel", FLOAT, GREEN_CHANNEL );
54 DALI_PROPERTY_REGISTRATION( Demo, ImageChannelControl, "blueChannel", FLOAT, BLUE_CHANNEL );
55
56 DALI_TYPE_REGISTRATION_END();
57
58 } // anonymous namespace
59
60
61 Internal::ImageChannelControl::ImageChannelControl()
62 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
63   mChannels( 1.0f, 1.0f, 1.0f )
64 {
65 }
66
67 Internal::ImageChannelControl::~ImageChannelControl()
68 {
69 }
70
71 Demo::ImageChannelControl Internal::ImageChannelControl::New()
72 {
73   IntrusivePtr<Internal::ImageChannelControl> impl = new Internal::ImageChannelControl();
74   Demo::ImageChannelControl handle = Demo::ImageChannelControl( *impl );
75   impl->Initialize();
76   return handle;
77 }
78
79 void ImageChannelControl::SetImage( const std::string& url )
80 {
81   mUrl = url;
82
83   Actor self = Self();
84
85   Property::Map properties;
86   Property::Map shader;
87   shader[Dali::Toolkit::Visual::Shader::Property::FRAGMENT_SHADER] = FRAGMENT_SHADER;
88   properties[Dali::Toolkit::Visual::Property::TYPE] = Dali::Toolkit::Visual::IMAGE;
89   properties[Dali::Toolkit::Visual::Property::SHADER]=shader;
90   properties[Dali::Toolkit::ImageVisual::Property::URL] = url;
91
92   Dali::Toolkit::InitializeVisual( self, mVisual, properties );
93
94   RelayoutRequest();
95 }
96
97 void ImageChannelControl::OnInitialize()
98 {
99   Actor self = Self();
100   mChannelIndex = self.RegisterProperty( "uChannels", Vector3(1.0f, 1.0f, 1.0f) );
101 }
102
103 void ImageChannelControl::OnStageConnection( int depth )
104 {
105   Control::OnStageConnection( depth );
106
107   if( mVisual )
108   {
109     CustomActor self = Self();
110     mVisual.SetOnStage( self );
111   }
112 }
113
114 void ImageChannelControl::OnStageDisconnection()
115 {
116   if( mVisual )
117   {
118     CustomActor self = Self();
119     mVisual.SetOffStage( self );
120   }
121
122   Control::OnStageDisconnection();
123 }
124
125 void ImageChannelControl::OnSizeSet( const Vector3& targetSize )
126 {
127   Control::OnSizeSet( targetSize );
128
129   if( mVisual )
130   {
131     Vector2 size( targetSize );
132     mVisual.SetSize( size );
133   }
134 }
135
136 Vector3 ImageChannelControl::GetNaturalSize()
137 {
138   if( mVisual )
139   {
140     Vector2 naturalSize;
141     mVisual.GetNaturalSize(naturalSize);
142     return Vector3(naturalSize);
143   }
144   return Vector3::ZERO;
145 }
146
147
148 ///////////////////////////////////////////////////////////
149 //
150 // Properties
151 //
152
153 void ImageChannelControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
154 {
155   Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
156
157   if ( imageChannelControl )
158   {
159     ImageChannelControl& impl = GetImpl( imageChannelControl );
160     Actor self = impl.Self();
161     switch ( index )
162     {
163       case Demo::ImageChannelControl::Property::RED_CHANNEL:
164       {
165         impl.mChannels[0] = value.Get<float>();
166         self.SetProperty( impl.mChannelIndex, impl.mChannels );
167         break;
168       }
169       case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
170       {
171         impl.mChannels[1] = value.Get<float>();
172         self.SetProperty( impl.mChannelIndex, impl.mChannels );
173         break;
174       }
175       case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
176       {
177         impl.mChannels[2] = value.Get<float>();
178         self.SetProperty( impl.mChannelIndex, impl.mChannels );
179         break;
180       }
181     }
182   }
183 }
184
185 Property::Value ImageChannelControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
186 {
187   Property::Value value;
188
189   Demo::ImageChannelControl imageChannelControl = Demo::ImageChannelControl::DownCast( Dali::BaseHandle( object ) );
190
191   if ( imageChannelControl )
192   {
193     ImageChannelControl& impl = GetImpl( imageChannelControl );
194     switch ( propertyIndex )
195     {
196       case Demo::ImageChannelControl::Property::RED_CHANNEL:
197       {
198         value = impl.mChannels[0];
199         break;
200       }
201       case Demo::ImageChannelControl::Property::GREEN_CHANNEL:
202       {
203         value = impl.mChannels[1];
204         break;
205       }
206       case Demo::ImageChannelControl::Property::BLUE_CHANNEL:
207       {
208         value = impl.mChannels[2];
209         break;
210       }
211     }
212   }
213
214   return value;
215 }
216
217 } // Internal
218 } // Demo