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