35a71fce07dd27ceb78df3873551a7444bb07b2f
[platform/core/uifw/dali-demo.git] / examples / alpha-blending-cpu / alpha-blending-cpu-example.cpp
1 /*
2  * Copyright (c) 2015 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
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
20 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
21 #include <cstring>
22
23 using namespace Dali;
24
25 namespace
26 {
27 const char* const IMAGE_PATH_1 ( DEMO_IMAGE_DIR "people-small-7b.jpg" ); // 100x100
28 const char* const IMAGE_PATH_2 ( DEMO_IMAGE_DIR "people-medium-7.jpg" );
29 const char* const IMAGE_PATH_3 ( DEMO_IMAGE_DIR "people-medium-7-rgb565.png" ); // is compressed
30 const char* const IMAGE_PATH_4 ( DEMO_IMAGE_DIR "people-medium-7-masked.png" ); // has alpha channel
31 const char* const MASK_IMAGE_PATH_1 ( DEMO_IMAGE_DIR "store_mask_profile_n.png" ); // 300x300
32 const char* const MASK_IMAGE_PATH_2 ( DEMO_IMAGE_DIR "store_mask_profile_f.png" );
33 }
34
35 class ImageViewAlphaBlendApp : public ConnectionTracker
36 {
37 public:
38   ImageViewAlphaBlendApp( Application& application )
39   : mApplication( application ),
40     mImageCombinationIndex( 0 )
41   {
42     // Connect to the Application's Init signal
43     mApplication.InitSignal().Connect( this, &ImageViewAlphaBlendApp::Create );
44   }
45
46   ~ImageViewAlphaBlendApp()
47   {
48     // Nothing to do here;
49   }
50
51 private:
52   // The Init signal is received once (only) during the Application lifetime
53   void Create( Application& application )
54   {
55     // This creates an image view with one of 3 images, and one of 2 masks.
56     // Clicking the screen will cycle through each combination of mask and image.
57
58     // Get a handle to the stage
59     Stage stage = Stage::GetCurrent();
60     stage.KeyEventSignal().Connect(this, &ImageViewAlphaBlendApp::OnKeyEvent);
61     stage.SetBackgroundColor( Color::WHITE );
62
63     mImageView = Toolkit::ImageView::New();
64
65     mImageView.SetSize(200, 200);
66     mImageView.SetParentOrigin( ParentOrigin::CENTER );
67     stage.Add(mImageView);
68
69     mImageLabel = Toolkit::TextLabel::New();
70     mImageLabel.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
71     mImageLabel.SetAnchorPoint( ParentOrigin::BOTTOM_CENTER );
72     mImageLabel.SetPosition( Vector3( 0.0f, -50.0f, 0.0f ) );
73     mImageLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Color::BLACK );
74     stage.Add(mImageLabel);
75
76     mMaskLabel = Toolkit::TextLabel::New();
77     mMaskLabel.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
78     mMaskLabel.SetAnchorPoint( ParentOrigin::BOTTOM_CENTER );
79     mMaskLabel.SetPosition( Vector3( 0.0f, 0.0f, 0.0f ) );
80     mMaskLabel.SetProperty( Toolkit::TextLabel::Property::TEXT_COLOR, Color::BLACK );
81     stage.Add(mMaskLabel);
82
83     LoadImages();
84
85     stage.TouchSignal().Connect( this, &ImageViewAlphaBlendApp::OnTouched );
86   }
87
88   void OnTouched( const TouchData& touchData )
89   {
90     static bool touched = false;
91     if( touchData.GetState( 0 ) == PointState::DOWN )
92     {
93       touched = true;
94     }
95
96     if( touchData.GetState( 0 ) == PointState::UP && touched)
97     {
98       mImageCombinationIndex++;
99       touched = false;
100       LoadImages();
101     }
102   }
103
104   void LoadImages()
105   {
106     const char* images[4] = { IMAGE_PATH_1, IMAGE_PATH_2, IMAGE_PATH_3, IMAGE_PATH_4 };
107     const char*  masks[2] = { MASK_IMAGE_PATH_1, MASK_IMAGE_PATH_2 };
108
109     const char* mask  = masks[mImageCombinationIndex%2 ]; // Cycle through masks
110     const char* image = images[(mImageCombinationIndex/2)%4]; // then images
111
112     Property::Map map;
113     map.Add( Toolkit::Visual::Property::TYPE, Toolkit::Visual::Type::IMAGE );
114     map.Add( Toolkit::ImageVisual::Property::URL, image );
115     map.Add( Toolkit::DevelImageVisual::Property::ALPHA_MASK_URL, mask );
116
117     if( mImageCombinationIndex%2 == 0 )
118     {
119       map.Add( Toolkit::DevelImageVisual::Property::MASK_CONTENT_SCALE, 1.f );
120       map.Add( Toolkit::DevelImageVisual::Property::CROP_TO_MASK, false );
121     }
122     else
123     {
124       map.Add( Toolkit::DevelImageVisual::Property::MASK_CONTENT_SCALE, 1.6f );
125       map.Add( Toolkit::DevelImageVisual::Property::CROP_TO_MASK, true );
126     }
127
128     mImageView.SetProperty( Toolkit::ImageView::Property::IMAGE, map );
129
130     mImageLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, strrchr(image, '/') );
131     mMaskLabel.SetProperty( Toolkit::TextLabel::Property::TEXT, strrchr(mask, '/') );
132   }
133
134   void OnKeyEvent(const KeyEvent& event)
135   {
136     if(event.state == KeyEvent::Down)
137     {
138       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
139       {
140         mApplication.Quit();
141       }
142     }
143   }
144
145
146 private:
147   Application&  mApplication;
148   Toolkit::ImageView mImageView;
149   Toolkit::TextLabel mImageLabel;
150   Toolkit::TextLabel mMaskLabel;
151
152   int mImageCombinationIndex;
153 };
154
155 void RunTest( Application& application )
156 {
157   ImageViewAlphaBlendApp test( application );
158
159   application.MainLoop();
160 }
161
162 // Entry point for Linux & Tizen applications
163 //
164 int DALI_EXPORT_API main( int argc, char **argv )
165 {
166   Application application = Application::New( &argc, &argv );
167
168   RunTest( application );
169
170   return 0;
171 }