Changed some demo examples to use ImageView.
[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( DALI_IMAGE_DIR "background-gradient.jpg" );
32 const char * const TOOLBAR_IMAGE( DALI_IMAGE_DIR "top-bar.png" );
33 const char * const LOSE_CONTEXT_IMAGE( DALI_IMAGE_DIR "icon-cluster-wobble.png" );
34 const char * const LOSE_CONTEXT_IMAGE_SELECTED( DALI_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.SetButtonImage( ResourceImage::New( LOSE_CONTEXT_IMAGE ) );
76     mLoseContextButton.SetSelectedImage( ResourceImage::New( 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,300, Pixel::RGBA8888);
81     mAtlas.Clear(Vector4(0.f,0.5f,0.5f,0.5f));
82     mAtlas.Upload( DALI_IMAGE_DIR "icon-change.png", 50, 30 );
83     mAtlas.Upload( DALI_IMAGE_DIR "icon-cluster-carousel.png", 100, 30 );
84     mAtlas.Upload( DALI_IMAGE_DIR "icon-effects-on.png", 150, 30 );
85     mAtlas.Upload( DALI_IMAGE_DIR "icon-effect-cross.png", 100, 80 );
86     mAtlas.Upload( DALI_IMAGE_DIR "icon-effect-fold.png", 150, 80 );
87     mAtlas.Upload( DALI_IMAGE_DIR "icon-effect-wave.png", 200, 80 );
88     mAtlas.Upload( DALI_IMAGE_DIR "icon-item-view-layout-depth.png", 150, 130 );
89     mAtlas.Upload( DALI_IMAGE_DIR "icon-item-view-layout-grid.png", 200, 130 );
90     mAtlas.Upload( DALI_IMAGE_DIR "icon-item-view-layout-spiral.png", 250, 130 );
91     UploadBufferImages();
92
93     Toolkit::ImageView imageActor1 = Toolkit::ImageView::New( mAtlas );
94     imageActor1.SetY(-170.f);
95     imageActor1.SetParentOrigin(ParentOrigin::CENTER);
96     mContentLayer.Add( imageActor1 );
97
98     Atlas atlas2 = Atlas::New( 400,400, Pixel::RGB888);
99     atlas2.Clear( Color::RED );
100     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-1.jpg", 4, 4 );
101     atlas2.Clear( Color::BLUE );
102     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-2.jpg", 136, 4 );
103     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-3.jpg", 268, 4 );
104     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-4.jpg", 4, 136 );
105     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-5.jpg", 136, 136 );
106     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-6.jpg", 268, 135 );
107     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-7.jpg", 4, 268 );
108     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-7.jpg", 136, 268 );
109     atlas2.Upload( DALI_IMAGE_DIR "gallery-small-7.jpg", 268, 268 );
110
111
112     Toolkit::ImageView imageView = Toolkit::ImageView::New( DALI_IMAGE_DIR "gallery-small-1.jpg" );
113
114     imageView.SetY(200.f);
115     imageView.SetZ(-1.f);
116     imageView.SetParentOrigin(ParentOrigin::CENTER);
117     mContentLayer.Add( imageView );
118
119     mPanGestureDetector = PanGestureDetector::New();
120     mPanGestureDetector.DetectedSignal().Connect( this, &AtlasController::OnPanGesture );
121     mPanGestureDetector.Attach( imageActor1 );
122     mPanGestureDetector.Attach( imageView );
123
124     stage.ContextLostSignal().Connect( this, &AtlasController::OnContextLost );
125     stage.ContextRegainedSignal().Connect( this, &AtlasController::OnContextRegained );
126   }
127
128   void UploadBufferImages()
129   {
130     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 1.f, 0.5f  ), 80, 90 ), 0, 210 );
131     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.75f, 0.5f  ), 80, 80 ), 40, 210 );
132     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.5f, 0.5f  ), 80, 70 ), 80, 210 );
133     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.25f, 0.5f  ), 80, 60 ), 120, 210 );
134     mAtlas.Upload( CreateBufferImage( Vector4(1.f, 1.f, 0.f, 0.5f  ), 80, 50 ), 160, 210 );
135     mAtlas.Upload( CreateBufferImage( Vector4(0.75f, 0.75f, 0.f, 0.5f  ), 80, 40 ), 200, 210 );
136     mAtlas.Upload( CreateBufferImage( Vector4(0.5f, 0.5f, 0.f, 0.5f  ), 80, 30 ), 240, 210 );
137     mAtlas.Upload( CreateBufferImage( Vector4(0.25f, 0.25f, 0.f, 0.5f  ), 80, 20 ), 280, 210 );
138     mAtlas.Upload( CreateBufferImage( Vector4(0.1f, 0.1f, 0.f, 0.5f  ), 80, 10 ), 320, 210 );
139     BufferImage redBlock = CreateBufferImage( Color::RED, 40, 40 );
140     mAtlas.Upload(redBlock, 320, 30);
141     mAtlas.Upload(redBlock, 320, 80);
142     mAtlas.Upload(redBlock, 320, 130);
143   }
144
145   static void NewWindow(void)
146   {
147     PositionSize posSize(0, 0, 720, 1280);
148     gApplication.ReplaceWindow(posSize, "NewWindow"); // Generates a new window
149   }
150
151   bool OnLoseContextButtonClicked( Toolkit::Button button )
152   {
153     // Add as an idle callback to avoid ProcessEvents being recursively called.
154     mApplication.AddIdle(MakeCallback( AtlasController::NewWindow ));
155     return true;
156   }
157
158   void OnKeyEvent( const KeyEvent& event )
159   {
160     if(event.state == KeyEvent::Down)
161     {
162       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
163       {
164         mApplication.Quit();
165       }
166     }
167   }
168
169   void OnPanGesture( Actor actor, const PanGesture& gesture )
170   {
171     if( gesture.state == Gesture::Continuing )
172     {
173       actor.TranslateBy( Vector3( gesture.displacement ) );
174     }
175   }
176
177   void OnContextLost()
178   {
179     printf("Stage reporting context loss\n");
180   }
181
182   void OnContextRegained()
183   {
184     printf("Stage reporting context regain\n");
185     UploadBufferImages();
186   }
187
188 private:
189
190   BufferImage CreateBufferImage( const Vector4& color, const unsigned int width, const unsigned int height )
191   {
192     BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
193
194     // Create the image
195     PixelBuffer* pixbuf = imageData.GetBuffer();
196     const unsigned int bitmapSize = width * height;
197     for( size_t i = 0; i < bitmapSize; ++i )
198     {
199       pixbuf[i*4+0] = 0xFF * color.r;
200       pixbuf[i*4+1] = 0xFF * color.g;
201       pixbuf[i*4+2] = 0xFF * color.b;
202       pixbuf[i*4+3] = 0xFF * color.a;
203     }
204
205     imageData.Update();
206
207     return imageData;
208   }
209
210
211 private:
212   Application&  mApplication;
213   PanGestureDetector mPanGestureDetector;
214
215   Toolkit::Control           mView;                              ///< The View instance.
216   Toolkit::ToolBar           mToolBar;                           ///< The View's Toolbar.
217   Layer                      mContentLayer;                      ///< Content layer (scrolling cluster content)
218   Toolkit::PushButton        mLoseContextButton;
219   Atlas                      mAtlas;
220 };
221
222 void RunTest( Application& application )
223 {
224   gAtlasController = new AtlasController(application);
225   application.MainLoop(Configuration::APPLICATION_DOES_NOT_HANDLE_CONTEXT_LOSS);
226 }
227
228 // Entry point for Linux & Tizen applications
229 //
230 int main( int argc, char **argv )
231 {
232   gApplication = Application::New( &argc, &argv, DALI_DEMO_THEME_PATH );
233
234   RunTest( gApplication );
235
236   return 0;
237 }