1d0f0892c7a6c370be628c485456b759bec7e5f7
[platform/core/uifw/dali-demo.git] / examples / atlas / atlas-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/dali.h>
19 #include <dali/devel-api/images/atlas.h>
20
21 #include "shared/view.h"
22 #include <iostream>
23 #include <cstdio>
24
25 using namespace Dali;
26
27 class AtlasController;
28
29 namespace
30 {
31 const char * const BACKGROUND_IMAGE( DEMO_IMAGE_DIR "background-gradient.jpg" );
32 const char * const TOOLBAR_IMAGE( DEMO_IMAGE_DIR "top-bar.png" );
33 const char * const LOSE_CONTEXT_IMAGE( DEMO_IMAGE_DIR "icon-cluster-wobble.png" );
34 const char * const LOSE_CONTEXT_IMAGE_SELECTED( DEMO_IMAGE_DIR "icon-cluster-wobble-selected.png" );
35
36 Application gApplication;
37 AtlasController* gAtlasController(NULL);
38 }
39
40 class AtlasController : public ConnectionTracker
41 {
42 public:
43
44   AtlasController( Application& application )
45   : mApplication( application )
46   {
47     // Connect to the Application's Init signal
48     mApplication.InitSignal().Connect( this, &AtlasController::Create );
49   }
50
51   ~AtlasController()
52   {
53     // Nothing to do here;
54   }
55
56   void Create( Application& application )
57   {
58     // Get a handle to the stage
59     Stage stage = Stage::GetCurrent();
60     stage.SetBackgroundColor(Color::YELLOW);
61
62     // Respond to a click anywhere on the stage
63     stage.KeyEventSignal().Connect(this, &AtlasController::OnKeyEvent);
64
65     mApplication.GetWindow().ShowIndicator( Dali::Window::INVISIBLE );
66
67     mContentLayer = DemoHelper::CreateView( mApplication,
68         mView,
69         mToolBar,
70         BACKGROUND_IMAGE,
71         TOOLBAR_IMAGE,
72         "Atlas" );
73
74     mLoseContextButton = Toolkit::PushButton::New();
75     mLoseContextButton.SetUnselectedImage( LOSE_CONTEXT_IMAGE );
76     mLoseContextButton.SetSelectedImage( LOSE_CONTEXT_IMAGE_SELECTED );
77     mLoseContextButton.ClickedSignal().Connect( this, &AtlasController::OnLoseContextButtonClicked );
78     mToolBar.AddControl( mLoseContextButton, DemoHelper::DEFAULT_VIEW_STYLE.mToolBarButtonPercentage, Toolkit::Alignment::HorizontalRight, DemoHelper::DEFAULT_MODE_SWITCH_PADDING );
79
80     mAtlas = Atlas::New( 400,700, Pixel::RGBA8888);
81     mAtlas.Clear(Vector4(0.f,0.5f,0.5f,0.25f));
82     mAtlas.Upload( DEMO_IMAGE_DIR "icon-change.png", 50, 30 );
83     mAtlas.Upload( DEMO_IMAGE_DIR "icon-cluster-carousel.png", 100, 30 );
84     mAtlas.Upload( DEMO_IMAGE_DIR "icon-effects-on.png", 150, 30 );
85     mAtlas.Upload( DEMO_IMAGE_DIR "icon-effect-cross.png", 100, 80 );
86     mAtlas.Upload( DEMO_IMAGE_DIR "icon-effect-fold.png", 150, 80 );
87     mAtlas.Upload( DEMO_IMAGE_DIR "icon-effect-wave.png", 200, 80 );
88     mAtlas.Upload( DEMO_IMAGE_DIR "icon-item-view-layout-depth.png", 150, 130 );
89     mAtlas.Upload( DEMO_IMAGE_DIR "icon-item-view-layout-grid.png", 200, 130 );
90     mAtlas.Upload( DEMO_IMAGE_DIR "icon-item-view-layout-spiral.png", 250, 130 );
91
92     UploadBufferImages();
93     UploadPixelData();
94
95     mAtlas.Upload( DEMO_IMAGE_DIR "gallery-small-1.jpg", 4, 304 );
96     mAtlas.Upload( DEMO_IMAGE_DIR "gallery-small-2.jpg", 136, 304 );
97     mAtlas.Upload( DEMO_IMAGE_DIR "gallery-small-3.jpg", 268, 304 );
98     mAtlas.Upload( DEMO_IMAGE_DIR "gallery-small-4.jpg", 4, 436 );
99     mAtlas.Upload( DEMO_IMAGE_DIR "gallery-small-5.jpg", 136, 436 );
100     mAtlas.Upload( DEMO_IMAGE_DIR "gallery-small-6.jpg", 268, 436 );
101     mAtlas.Upload( DEMO_IMAGE_DIR "gallery-small-7.jpg", 4, 568 );
102     mAtlas.Upload( DEMO_IMAGE_DIR "gallery-small-7.jpg", 136, 568 );
103     mAtlas.Upload( DEMO_IMAGE_DIR "gallery-small-7.jpg", 268, 568 );
104
105
106     Toolkit::ImageView imageView = Toolkit::ImageView::New( mAtlas );
107
108     imageView.SetParentOrigin(ParentOrigin::CENTER);
109     mContentLayer.Add( imageView );
110
111     mPanGestureDetector = PanGestureDetector::New();
112     mPanGestureDetector.DetectedSignal().Connect( this, &AtlasController::OnPanGesture );
113     mPanGestureDetector.Attach( imageView );
114
115     stage.ContextLostSignal().Connect( this, &AtlasController::OnContextLost );
116     stage.ContextRegainedSignal().Connect( this, &AtlasController::OnContextRegained );
117   }
118
119   void UploadBufferImages()
120   {
121     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 1.f, 0.5f  ), 80, 90 ), 0, 210 );
122     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.75f, 0.5f  ), 80, 80 ), 40, 210 );
123     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.5f, 0.5f  ), 80, 70 ), 80, 210 );
124     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.25f, 0.5f  ), 80, 60 ), 120, 210 );
125     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.f, 0.5f  ), 80, 50 ), 160, 210 );
126     mAtlas.Upload( CreateBufferImage( Vector4(0.75f, 0.75f, 0.f, 0.5f  ), 80, 40 ), 200, 210 );
127     mAtlas.Upload( CreateBufferImage( Vector4(0.5f, 0.5f, 0.f, 0.5f  ), 80, 30 ), 240, 210 );
128     mAtlas.Upload( CreateBufferImage( Vector4(0.25f, 0.25f, 0.f, 0.5f  ), 80, 20 ), 280, 210 );
129     mAtlas.Upload( CreateBufferImage( Vector4(0.1f, 0.1f, 0.f, 0.5f  ), 80, 10 ), 320, 210 );
130   }
131
132   void UploadPixelData()
133   {
134     mAtlas.Upload( CreatePixelData( Vector3(1.f, 1.f, 0.f ), 40, 40 ), 320, 30 );
135     mAtlas.Upload( CreatePixelData( Vector3(0.f, 1.f, 1.f ), 40, 40 ), 320, 80 );
136     mAtlas.Upload( CreatePixelData( Vector3(1.f, 0.f, 1.f ), 40, 40 ), 320, 130 );
137   }
138
139   static void NewWindow(void)
140   {
141     PositionSize posSize(0, 0, 720, 1280);
142     gApplication.ReplaceWindow(posSize, "NewWindow"); // Generates a new window
143   }
144
145   bool OnLoseContextButtonClicked( Toolkit::Button button )
146   {
147     // Add as an idle callback to avoid ProcessEvents being recursively called.
148     mApplication.AddIdle(MakeCallback( AtlasController::NewWindow ));
149     return true;
150   }
151
152   void OnKeyEvent( const KeyEvent& event )
153   {
154     if(event.state == KeyEvent::Down)
155     {
156       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
157       {
158         mApplication.Quit();
159       }
160     }
161   }
162
163   void OnPanGesture( Actor actor, const PanGesture& gesture )
164   {
165     if( gesture.state == Gesture::Continuing )
166     {
167       actor.TranslateBy( Vector3( gesture.displacement ) );
168     }
169   }
170
171   void OnContextLost()
172   {
173     printf("Stage reporting context loss\n");
174   }
175
176   void OnContextRegained()
177   {
178     printf("Stage reporting context regain\n");
179     UploadBufferImages();
180     UploadPixelData();
181   }
182
183 private:
184
185   BufferImage CreateBufferImage( const Vector4& color, const unsigned int width, const unsigned int height )
186   {
187     BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
188
189     // Create the image
190     PixelBuffer* pixbuf = imageData.GetBuffer();
191     const unsigned int bitmapSize = width * height;
192     for( size_t i = 0; i < bitmapSize; ++i )
193     {
194       pixbuf[i*4+0] = 0xFF * color.r;
195       pixbuf[i*4+1] = 0xFF * color.g;
196       pixbuf[i*4+2] = 0xFF * color.b;
197       pixbuf[i*4+3] = 0xFF * color.a;
198     }
199
200     return imageData;
201   }
202
203   PixelData CreatePixelData( const Vector3& color, const unsigned int width, const unsigned int height )
204   {
205     unsigned int size = width*height;
206     unsigned char* pixels = new unsigned char [size*3u];
207     for( unsigned int i = 0; i < size; i++ )
208     {
209       pixels[i*3u] = 0xFF * color.x;
210       pixels[i*3u+1u] = 0xFF * color.y;
211       pixels[i*3u+2u] = 0xFF * color.z;
212     }
213
214     return PixelData::New( pixels, width, height, Pixel::RGB888, PixelData::DELETE_ARRAY );
215   }
216
217
218 private:
219   Application&  mApplication;
220   PanGestureDetector mPanGestureDetector;
221
222   Toolkit::Control           mView;                              ///< The View instance.
223   Toolkit::ToolBar           mToolBar;                           ///< The View's Toolbar.
224   Layer                      mContentLayer;                      ///< Content layer (scrolling cluster content)
225   Toolkit::PushButton        mLoseContextButton;
226   Atlas                      mAtlas;
227 };
228
229 void RunTest( Application& application )
230 {
231   gAtlasController = new AtlasController(application);
232   application.MainLoop(Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS);
233 }
234
235 // Entry point for Linux & Tizen applications
236 //
237 int DALI_EXPORT_API main( int argc, char **argv )
238 {
239   gApplication = Application::New( &argc, &argv, DEMO_THEME_PATH );
240
241   RunTest( gApplication );
242
243   return 0;
244 }